Motr  M0
crate_utils.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-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 
29 #include <string.h>
30 #include <err.h>
31 #include "lib/trace.h"
33 #include "motr/m0crate/logger.h"
34 
35 /* XXX: io checks are disabled */
36 #if 0
37 #include <openssl/md5.h>
38 unsigned char *calc_md5sum (char *buffer, int blocksize)
39 {
40  unsigned char *sum;
41 
42  MD5_CTX mdContext;
43  MD5_Init (&mdContext);
44  sum = malloc(sizeof(unsigned char)*MD5_DIGEST_LENGTH);
45  if (sum == NULL)
46  return sum;
47 
48  MD5_Update (&mdContext, buffer, blocksize);
49  MD5_Final (sum, &mdContext);
50  return sum;
51 }
52 #else
53 unsigned char *calc_md5sum (char *buffer, int blocksize)
54 {
55  assert(0 && "NotImplemented");
56  return NULL;
57 }
58 #endif
59 
60 #define NN 312
61 #define MM 156
62 #define MATRIX_A 0xB5026F5AA96619E9ULL
63 #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
64 #define LM 0x7FFFFFFFULL /* Least significant 31 bits */
65 
66 /* The array for the state vector */
67 static unsigned long long mt[NN];
68 
69 /* mti==NN+1 means mt[NN] is not initialized */
70 static int mti = NN+1;
71 
72 /* initializes mt[NN] with a seed */
73 void init_genrand64(unsigned long long seed)
74 {
75  mt[0] = seed;
76  for (mti = 1; mti < NN; mti++)
77  mt[mti] = (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
78 }
79 
80 /* initialize by an array with array-length */
81 /* init_key is the array for initializing keys */
82 /* key_length is its length */
83 void init_by_array64(unsigned long long init_key[],
84  unsigned long long key_length,
85  unsigned long long seed)
86 {
87  unsigned long long i, j, k;
89  i = 1; j = 0;
90  k = (NN > key_length ? NN : key_length);
91  for (; k; k--) {
92  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 3935559000370003845ULL))
93  + init_key[j] + j; /* non linear */
94  i++; j++;
95  if (i >= NN) { mt[0] = mt[NN-1]; i=1; }
96  if (j >= key_length) j=0;
97  }
98  for (k=NN-1; k; k--) {
99  mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 2862933555777941757ULL))
100  - i; /* non linear */
101  i++;
102  if (i>=NN) {
103  mt[0] = mt[NN-1];
104  i=1;
105  }
106  }
107 
108  mt[0] = 1ULL << 63; /* MSB is 1; assuring non-zero initial array */
109 }
110 
111 /* generates a random number on [0, 2^64-1]-interval */
112 unsigned long long genrand64_int64(void)
113 {
114  int i;
115  unsigned long long x;
116  static unsigned long long mag01[2]={0x100000ULL, MATRIX_A};
117 
118  if (mti >= NN) { /* generate NN words at one time */
119 
120  /* if init_genrand64() has not been called, */
121  /* a default initial seed is used */
122  if (mti == NN+1)
123  init_genrand64(5489ULL);
124 
125  for (i=0;i<NN-MM;i++) {
126  x = (mt[i]&UM)|(mt[i+1]&LM);
127  mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
128  }
129  for (;i<NN-1;i++) {
130  x = (mt[i]&UM)|(mt[i+1]&LM);
131  mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
132  }
133  x = (mt[NN-1]&UM)|(mt[0]&LM);
134  mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
135 
136  mti = 0;
137  }
138 
139  x = mt[mti++];
140 
141  x ^= (x >> 29) & 0x5555555555555555ULL;
142  x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
143  x ^= (x << 37) & 0xFFF7EEE000000000ULL;
144  x ^= (x >> 43);
145 
146  return x;
147 }
148 
149 /* generates a random number on [0, 2^63-1]-interval */
150 long long genrand64_int63(void)
151 {
152  return (long long)(genrand64_int64() >> 1);
153 }
154 
155 /* generates a random number on [0,1]-real-interval */
156 double genrand64_real1(void)
157 {
158  return (genrand64_int64() >> 11) * (1.0/9007199254740991.0);
159 }
160 
161 /* generates a random number on [0,1)-real-interval */
162 double genrand64_real2(void)
163 {
164  return (genrand64_int64() >> 11) * (1.0/9007199254740992.0);
165 }
166 
167 /* generates a random number on (0,1)-real-interval */
168 double genrand64_real3(void)
169 {
170  return ((genrand64_int64() >> 12) + 0.5) * (1.0/4503599627370496.0);
171 }
172 
173 void init_rand_generator(unsigned long long seed)
174 {
175  unsigned long long init[4] = {0x12345ULL, 0x23456ULL, 0x34567ULL, 0x45678ULL};
176  unsigned long long length = 4;
177 
178  init_by_array64(init, length, seed);
179 }
180 
181 int generate_fid(int seed, unsigned long *low, unsigned long *high)
182 {
183  *low = (unsigned long)genrand64_int64();
184  *high = (unsigned long)genrand64_int64();
185 
186  return 0;
187 }
188 
189 void timeval_diff(const struct timeval *start, const struct timeval *end,
190  struct timeval *diff)
191 {
192  diff->tv_sec += end->tv_sec - start->tv_sec;
193  /* this relies on tv_usec being signed */
194  diff->tv_usec += end->tv_usec - start->tv_usec;
196 }
197 
198 void timeval_add(struct timeval *sum, struct timeval *term)
199 {
200  sum->tv_sec += term->tv_sec;
201  sum->tv_usec += term->tv_usec;
202  timeval_norm(sum);
203 }
204 
205 void timeval_sub(struct timeval *end, struct timeval *start)
206 {
207  end->tv_sec -= start->tv_sec;
208  end->tv_usec -= start->tv_usec; /* safe, as usec is signed */
209  timeval_norm(end);
210 }
211 
212 double tsec(const struct timeval *tval)
213 {
214  return tval->tv_sec + ((double)tval->tv_usec)/1000000;
215 }
216 
217 double rate(bcnt_t items, const struct timeval *tval, int scale)
218 {
219  return ((double)items)/tsec(tval)/scale;
220 }
221 
222 unsigned long long getnum(const char *str, const char *msg)
223 {
224  char *end = NULL;
225  bcnt_t num;
226  char *pos;
227  static const char suffix[] = "bkmgBKMG";
228 
229  static const bcnt_t multiplier[] = {
230  1 << 9,
231  1 << 10,
232  1 << 20,
233  1 << 30,
234  500,
235  1000,
236  1000 * 1000,
237  1000 * 1000 * 1000
238  };
239 
240  num = strtoull(str, &end, 0);
241 
242  if (*end != 0) {
243  pos = strchr(suffix, *end);
244  if (pos != NULL)
245  num *= multiplier[pos - suffix];
246  else
247  errx(1, "conversion of \"%s\" to \"%s\" failed\n",
248  str, msg);
249  }
250  cr_log(CLL_DEBUG, "converting \"%s\" to \"%s\": %llu\n", str, msg, num);
251  return num;
252 }
253 
254 
257 /*
258  * Local variables:
259  * c-indentation-style: "K&R"
260  * c-basic-offset: 8
261  * tab-width: 8
262  * fill-column: 80
263  * scroll-step: 1
264  * End:
265  */
266 /*
267  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
268  */
static int(* diff[M0_PARITY_CAL_ALGO_NR])(struct m0_parity_math *math, struct m0_buf *old, struct m0_buf *new, struct m0_buf *parity, uint32_t index)
Definition: parity_math.c:290
#define NULL
Definition: misc.h:38
#define MATRIX_A
Definition: crate_utils.c:62
#define LM
Definition: crate_utils.c:64
double tsec(const struct timeval *tval)
Definition: crate_utils.c:212
unsigned char * calc_md5sum(char *buffer, int blocksize)
Definition: crate_utils.c:53
static bool x
Definition: sm.c:168
void init_genrand64(unsigned long long seed)
Definition: crate_utils.c:73
static void low(struct m0_net_buffer_pool *bp)
double rate(bcnt_t items, const struct timeval *tval, int scale)
Definition: crate_utils.c:217
#define NN
Definition: crate_utils.c:60
static int sum
Definition: rwlock.c:53
double genrand64_real1(void)
Definition: crate_utils.c:156
void init_rand_generator(unsigned long long seed)
Definition: crate_utils.c:173
unsigned long long getnum(const char *str, const char *msg)
Definition: crate_utils.c:222
#define UM
Definition: crate_utils.c:63
double genrand64_real2(void)
Definition: crate_utils.c:162
int i
Definition: dir.c:1033
unsigned long long genrand64_int64(void)
Definition: crate_utils.c:112
void init_by_array64(unsigned long long init_key[], unsigned long long key_length, unsigned long long seed)
Definition: crate_utils.c:83
void timeval_sub(struct timeval *end, struct timeval *start)
Definition: crate_utils.c:205
unsigned long long bcnt_t
Definition: crate_utils.h:44
void timeval_add(struct timeval *sum, struct timeval *term)
Definition: crate_utils.c:198
static int mti
Definition: crate_utils.c:70
int init(struct workload *w)
static void timeval_norm(struct timeval *t)
Definition: ub.c:79
Definition: addb2.c:200
void cr_log(enum cr_log_level lev, const char *fmt,...)
Definition: logger.c:39
static int start(struct m0_fom *fom)
Definition: trigger_fom.c:321
long long genrand64_int63(void)
Definition: crate_utils.c:150
struct m0_rpc_fop_session_terminate term
Definition: session.c:53
int generate_fid(int seed, unsigned long *low, unsigned long *high)
Definition: crate_utils.c:181
int num
Definition: bulk_if.c:54
void timeval_diff(const struct timeval *start, const struct timeval *end, struct timeval *diff)
Definition: crate_utils.c:189
static unsigned long long mt[NN]
Definition: crate_utils.c:67
double genrand64_real3(void)
Definition: crate_utils.c:168
#define MM
Definition: crate_utils.c:61