Motr  M0
tx_credit.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "be/tx_credit.h"
24 
25 #include "lib/assert.h" /* M0_PRE */
26 #include "lib/arith.h" /* max_check */
27 #include "lib/misc.h" /* m0_forall */
28 
43 
44 M0_INTERNAL void m0_be_tx_credit_add(struct m0_be_tx_credit *c0,
45  const struct m0_be_tx_credit *c1)
46 {
47  c0->tc_reg_nr += c1->tc_reg_nr;
48  c0->tc_reg_size += c1->tc_reg_size;
49  if (M0_DEBUG_BE_CREDITS) {
51  c0->tc_balance[i] += c1->tc_balance[i], true);
52  }
53 }
54 
55 M0_INTERNAL void m0_be_tx_credit_sub(struct m0_be_tx_credit *c0,
56  const struct m0_be_tx_credit *c1)
57 {
58  int i;
59 
60  M0_PRE(c0->tc_reg_nr >= c1->tc_reg_nr);
61  M0_PRE(c0->tc_reg_size >= c1->tc_reg_size);
62 
63  c0->tc_reg_nr -= c1->tc_reg_nr;
64  c0->tc_reg_size -= c1->tc_reg_size;
65  if (M0_DEBUG_BE_CREDITS) {
66  for (i = 0; i < M0_BE_CU_NR; ++i) {
67  M0_PRE(c0->tc_balance[i] >= c1->tc_balance[i]);
68  c0->tc_balance[i] -= c1->tc_balance[i];
69  }
70  }
71 }
72 
73 M0_INTERNAL void m0_be_tx_credit_mul(struct m0_be_tx_credit *c, m0_bcount_t k)
74 {
75  c->tc_reg_nr *= k;
76  c->tc_reg_size *= k;
77 
79  m0_forall(i, M0_BE_CU_NR, c->tc_balance[i] *= k, true);
80 }
81 
82 M0_INTERNAL void m0_be_tx_credit_mul_bp(struct m0_be_tx_credit *c, unsigned bp)
83 {
84  c->tc_reg_nr = c->tc_reg_nr * bp / 10000;
85  c->tc_reg_size = c->tc_reg_size * bp / 10000;
86 }
87 
88 M0_INTERNAL void m0_be_tx_credit_mac(struct m0_be_tx_credit *c,
89  const struct m0_be_tx_credit *c1,
90  m0_bcount_t k)
91 {
92  struct m0_be_tx_credit c1_k = *c1;
93 
94  m0_be_tx_credit_mul(&c1_k, k);
95  m0_be_tx_credit_add(c, &c1_k);
96 }
97 
98 M0_INTERNAL bool m0_be_tx_credit_le(const struct m0_be_tx_credit *c0,
99  const struct m0_be_tx_credit *c1)
100 {
101  return c0->tc_reg_nr <= c1->tc_reg_nr &&
102  c0->tc_reg_size <= c1->tc_reg_size;
103 }
104 
105 M0_INTERNAL bool m0_be_tx_credit_eq(const struct m0_be_tx_credit *c0,
106  const struct m0_be_tx_credit *c1)
107 {
108  return c0->tc_reg_nr == c1->tc_reg_nr &&
109  c0->tc_reg_size == c1->tc_reg_size;
110 }
111 
112 M0_INTERNAL void m0_be_tx_credit_max(struct m0_be_tx_credit *c,
113  const struct m0_be_tx_credit *c0,
114  const struct m0_be_tx_credit *c1)
115 {
117  max_check(c0->tc_reg_size, c1->tc_reg_size));
118 }
119 
120 M0_INTERNAL void m0_be_tx_credit_add_max(struct m0_be_tx_credit *c,
121  const struct m0_be_tx_credit *c0,
122  const struct m0_be_tx_credit *c1)
123 {
124  struct m0_be_tx_credit cred;
125 
126  m0_be_tx_credit_max(&cred, c0, c1);
127  m0_be_tx_credit_add(c, &cred);
128 }
129 
132 /*
133  * Local variables:
134  * c-indentation-style: "K&R"
135  * c-basic-offset: 8
136  * tab-width: 8
137  * fill-column: 80
138  * scroll-step: 1
139  * End:
140  */
141 /*
142  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
143  */
M0_INTERNAL void m0_be_tx_credit_add_max(struct m0_be_tx_credit *c, const struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:120
#define M0_PRE(cond)
#define M0_DEBUG_BE_CREDITS
Definition: tx_credit.h:67
M0_INTERNAL void m0_be_tx_credit_sub(struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:55
const struct m0_be_tx_credit m0_be_tx_credit_invalid
Definition: tx_credit.c:41
#define max_check(a, b)
Definition: arith.h:95
uint64_t m0_bcount_t
Definition: types.h:77
#define M0_BE_TX_CREDIT(nr, size)
Definition: tx_credit.h:94
int i
Definition: dir.c:1033
M0_INTERNAL bool m0_be_tx_credit_eq(const struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:105
M0_INTERNAL void m0_be_tx_credit_mac(struct m0_be_tx_credit *c, const struct m0_be_tx_credit *c1, m0_bcount_t k)
Definition: tx_credit.c:88
unsigned tc_balance[M0_BE_CU_NR]
Definition: tx_credit.h:88
static struct m0_addb2_callback c
Definition: consumer.c:41
M0_INTERNAL void m0_be_tx_credit_mul(struct m0_be_tx_credit *c, m0_bcount_t k)
Definition: tx_credit.c:73
M0_INTERNAL void m0_be_tx_credit_add(struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:44
m0_bcount_t tc_reg_nr
Definition: tx_credit.h:84
#define m0_forall(var, nr,...)
Definition: misc.h:112
static struct bulkio_params * bp
Definition: bulkio_ut.c:44
M0_INTERNAL bool m0_be_tx_credit_le(const struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:98
M0_INTERNAL void m0_be_tx_credit_mul_bp(struct m0_be_tx_credit *c, unsigned bp)
Definition: tx_credit.c:82
M0_INTERNAL void m0_be_tx_credit_max(struct m0_be_tx_credit *c, const struct m0_be_tx_credit *c0, const struct m0_be_tx_credit *c1)
Definition: tx_credit.c:112
m0_bcount_t tc_reg_size
Definition: tx_credit.h:86