Motr  M0
common_u.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 <string.h> /* strlen */
24 #include <limits.h> /* ULONG_MAX */
25 
26 #include "lib/misc.h" /* M0_SET0 */
27 #include "lib/assert.h" /* M0_ASSERT */
28 #include "lib/memory.h" /* m0_alloc */
29 #include "net/lnet/lnet.h" /* m0_net_lnet_ifaces_get */
30 #include "net/net.h" /* m0_net_domain */
31 
33 
44 
45 char *m0_net_test_u_str_copy(const char *str)
46 {
47  size_t len = strlen(str) + 1;
48  char *copy = m0_alloc(len);
49 
50  M0_ASSERT(copy != NULL);
51  return strncpy(copy, str, len);
52 }
53 
54 void m0_net_test_u_str_free(char *str)
55 {
56  m0_free(str);
57 }
58 
59 void m0_net_test_u_print_error(const char *s, int code)
60 {
61  if (code != 0)
62  fprintf(stderr, "%s, error %d: %s\n", s, code, strerror(-code));
63 }
64 
65 void m0_net_test_u_print_s(const char *fmt, const char *str)
66 {
67  m0_net_test_u_printf_v(fmt, str == NULL ? "NULL" : str);
68 }
69 
71 {
72  uint64_t ns = m0_time_nanoseconds(time);
73 
74  m0_net_test_u_printf_v("%s\t= %lus", name, m0_time_seconds(time));
75  if (ns != 0)
76  m0_net_test_u_printf_v(" %luns", ns);
78 }
79 
80 void m0_net_test_u_print_bsize(double bsize)
81 {
82  /*
83  * 0123456
84  * 1B
85  * 1023B
86  * 1.00KiB
87  * 9.99KiB
88  * 10.0KiB
89  * 99.9KiB
90  * 100KiB
91  * 1023KiB
92  * 1.00MiB
93  */
94  char *fmt;
95  int i;
96  static const struct {
97  unsigned long max;
98  char *name;
99  } suffix[] = {
100  { .max = 1ul << 20, .name = "KiB" },
101  { .max = 1ul << 30, .name = "MiB" },
102  { .max = 1ul << 40, .name = "GiB" },
103  { .max = 1ul << 50, .name = "TiB" },
104  { .max = 1ul << 60, .name = "PiB" },
105  { .max = ULONG_MAX, .name = "EiB" },
106  };
107 
108  if (bsize < 1023.5) {
109  m0_net_test_u_printf("%4dB ", (int) bsize);
110  } else {
111  for (i = 0; i < ARRAY_SIZE(suffix) - 1; ++i) {
112  if (bsize < suffix[i].max - .5)
113  break;
114  }
115  bsize /= suffix[i].max / (1 << 10);
116  fmt = bsize < 9.995 ? "%4.2f%s" :
117  bsize < 99.95 ? "%4.1f%s" : "%4.0f%s";
118  m0_net_test_u_printf(fmt, bsize, suffix[i].name);
119  };
120 }
121 
123 {
124  struct m0_net_domain dom;
125  int rc;
126  char **nidstrs;
127  int i;
128 
129  M0_SET0(&dom);
131  M0_ASSERT(rc == 0);
132 
134  "m0_net_domain_get_max_buffer_size() = %lu\n",
137  "m0_net_domain_get_max_buffer_segments() = %i\n",
140  "m0_net_domain_get_max_buffer_segment_size() = %lu\n",
142  rc = m0_net_lnet_ifaces_get(&dom, &nidstrs);
143  if (rc != 0) {
144  m0_net_test_u_printf("m0_net_lnet_ifaces_get() failed.\n");
145  } else {
146  m0_net_test_u_printf("List of LNET interfaces:\n");
147  for (i = 0; nidstrs != NULL && nidstrs[i] != NULL; ++i)
148  m0_net_test_u_printf("NID %d:\t%s\n", i, nidstrs[i]);
149  m0_net_test_u_printf("End of list.\n");
150  m0_net_lnet_ifaces_put(&dom, &nidstrs);
151  }
152 
154 }
155 
156 static int net_test_u_printf(bool _verbose, const char *fmt, va_list argp)
157 {
158  if (m0_net_test_u_printf_verbose || !_verbose)
159  return vprintf(fmt, argp);
160  return 0;
161 }
162 
163 int m0_net_test_u_printf(const char *fmt, ...)
164 {
165  va_list argp;
166  int rc = 0;
167 
168  va_start(argp, fmt);
169  rc = net_test_u_printf(false, fmt, argp);
170  va_end(argp);
171  return rc;
172 }
173 
174 int m0_net_test_u_printf_v(const char *fmt, ...)
175 {
176  va_list argp;
177  int rc = 0;
178 
179  va_start(argp, fmt);
180  rc = net_test_u_printf(true, fmt, argp);
181  va_end(argp);
182  return rc;
183 }
184 
189 /*
190  * Local variables:
191  * c-indentation-style: "K&R"
192  * c-basic-offset: 8
193  * tab-width: 8
194  * fill-column: 79
195  * scroll-step: 1
196  * End:
197  */
M0_INTERNAL m0_bcount_t m0_net_domain_get_max_buffer_segment_size(struct m0_net_domain *dom)
int m0_net_test_u_printf_v(const char *fmt,...)
Definition: common_u.c:174
void m0_net_domain_fini(struct m0_net_domain *dom)
Definition: domain.c:71
#define NULL
Definition: misc.h:38
void m0_net_test_u_print_bsize(double bsize)
Definition: common_u.c:80
bool m0_net_test_u_printf_verbose
Definition: common_u.c:43
uint64_t m0_time_t
Definition: time.h:37
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
static struct m0t1fs_fsync_interactions copy
Definition: fsync.c:75
void m0_net_test_u_lnet_info(void)
Definition: common_u.c:122
#define M0_SET0(obj)
Definition: misc.h:64
void m0_net_test_u_print_time(char *name, m0_time_t time)
Definition: common_u.c:70
int i
Definition: dir.c:1033
const char * name
Definition: trace.c:110
#define M0_ASSERT(cond)
int m0_net_test_u_printf(const char *fmt,...)
Definition: common_u.c:163
M0_INTERNAL int m0_net_lnet_ifaces_get(struct m0_net_domain *dom, char ***addrs)
Definition: lnet_main.c:955
static struct m0_stob_domain * dom
Definition: storage.c:38
static long long max(long long a, long long b)
Definition: crate.c:196
struct m0_net_xprt * m0_net_xprt_default_get(void)
Definition: net.c:151
char * fmt(const char *format,...) __attribute__((format(printf
void * m0_alloc(size_t size)
Definition: memory.c:126
M0_INTERNAL void m0_net_lnet_ifaces_put(struct m0_net_domain *dom, char ***addrs)
Definition: lnet_main.c:966
M0_INTERNAL m0_bcount_t m0_net_domain_get_max_buffer_size(struct m0_net_domain *dom)
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
int m0_net_domain_init(struct m0_net_domain *dom, const struct m0_net_xprt *xprt)
Definition: domain.c:36
void m0_net_test_u_print_error(const char *s, int code)
Definition: common_u.c:59
void m0_net_test_u_print_s(const char *fmt, const char *str)
Definition: common_u.c:65
M0_INTERNAL int32_t m0_net_domain_get_max_buffer_segments(struct m0_net_domain *dom)
void m0_net_test_u_str_free(char *str)
Definition: common_u.c:54
char * m0_net_test_u_str_copy(const char *str)
Definition: common_u.c:45
void m0_free(void *data)
Definition: memory.c:146
static struct m0_addb2_source * s
Definition: consumer.c:39
static int net_test_u_printf(bool _verbose, const char *fmt, va_list argp)
Definition: common_u.c:156
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45