Motr  M0
stats_ut_svc.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 #include "lib/memory.h"
23 #include "fop/fop.h"
24 #include "fop/fom.h"
25 #include "motr/setup.h"
26 #include "stats/stats_fops.h"
27 #include "reqh/reqh_service.h"
28 #include "stats/stats_api.h"
29 
30 #include "stats/stats_srv.c"
31 #include "rpc/ut/clnt_srv_ctx.c"
36 
37 static char *stats_ut_server_argv[] = {
38  "rpclib_ut", "-T", "AD", "-D", SERVER_DB_NAME, "-w", "10",
39  "-f", M0_UT_CONF_PROCESS,
42  "-c", M0_UT_PATH("conf.xc")
43 };
44 
45 enum stats_id {
49 };
50 
51 enum fop_type {
54 };
55 
56 /* Fake stats */
57 struct fop_rate {
58  uint64_t fr_rate;
60 } f_rate;
61 
62 struct read_size {
63  uint64_t rs_avg_size;
64 } r_size;
65 
66 struct disk_stats {
67  uint64_t ds_total;
68  uint64_t ds_free;
69  uint64_t ds_used;
70 } d_stats;
71 
73 uint64_t stats_ids[] = {
77 };
78 
79 static void fill_stats_input()
80 {
81 
82  f_rate.fr_rate = 3000;
85  stats_sum[0].ss_data.se_nr = 2;
86  stats_sum[0].ss_data.se_data = (uint64_t *)&f_rate;
87 
88  r_size.rs_avg_size = 8196;
90  stats_sum[1].ss_data.se_nr = 1;
91  stats_sum[1].ss_data.se_data = (uint64_t *)&r_size;
92 
93  d_stats.ds_free = 2678901234;
94  d_stats.ds_used = 3578901234;
95  d_stats.ds_total = 6257802468;
97  stats_sum[2].ss_data.se_nr = 3;
98  stats_sum[2].ss_data.se_data = (uint64_t *)&d_stats;
99 }
100 
101 void check_stats(struct m0_tl *stats_list, int count)
102 {
103  int i;
104  int id;
105 
106  for (i = 0, id = 1; i < count; ++i, ++id) {
107  struct m0_stats *stats = m0_stats_get(stats_list, id);
108  M0_UT_ASSERT(stats != NULL);
109  }
110 }
111 
113 {
114  struct m0_reqh *reqh;
115  struct m0_reqh_service *stats_srv;
116  /*
117  * Test 1: Check stats service start on motr server start
118  * 1. Start motr client-server
119  * 2. verify it's status
120  */
122 
125 
127 
130  M0_UT_ASSERT(stats_srv != NULL);
132 
133  /*
134  * Test 2: Check individual stats service stop
135  * 1. stop stats service
136  * 2. verify it's status
137  */
139  m0_reqh_service_stop(stats_srv);
141  m0_reqh_service_fini(stats_srv);
142 
144 
146 }
147 
148 void fop_release(struct m0_ref *ref)
149 {
150  struct m0_fop *fop = container_of(ref, struct m0_fop, f_ref);
151 
152  M0_UT_ASSERT(fop != NULL);
153  m0_fop_fini(fop);
154  m0_free(fop);
155 }
156 
158 {
163 }
164 
169 static const struct m0_fom_ops ut_stats_update_fom_ops = {
171  .fo_home_locality = stats_fom_home_locality,
172  .fo_fini = test_state_update_fom_fini
173 };
174 
175 static struct m0_fop *get_fake_stats_fop(uint32_t nsum, enum fop_type type)
176 {
177  struct m0_fop *fop;
178  struct m0_stats_update_fop *ufop;
179  struct m0_stats_query_fop *qfop;
180 
181 
182  M0_ALLOC_PTR(fop);
183  M0_UT_ASSERT(fop != NULL);
184 
185  switch (type) {
186  case UPDATE_FOP:
187  M0_ALLOC_PTR(ufop);
188  M0_UT_ASSERT(ufop != NULL);
189 
190  M0_ALLOC_ARR(ufop->suf_stats.sf_stats, nsum);
192 
193  m0_fop_init(fop, &m0_fop_stats_update_fopt, (void *)ufop,
194  fop_release);
195  break;
196  case QUERY_FOP:
197  M0_ALLOC_PTR(qfop);
198  M0_UT_ASSERT(qfop != NULL);
199 
200  m0_fop_init(fop, &m0_fop_stats_query_fopt, (void *)qfop,
201  fop_release);
202  break;
203  }
204 
205  return fop;
206 }
207 
208 static void update_fom_test(struct stats_svc *srv, struct m0_reqh *reqh,
209  int count)
210 {
211  struct m0_fop *fop;
212  struct m0_fom *fom;
213  struct m0_stats_update_fop *ufop;
214  int i;
215  int rc;
216 
220 
222  M0_UT_ASSERT(fop != NULL);
223 
225  ufop->suf_stats.sf_nr = count;
226  for (i = 0; i < count; ++i)
228 
231  M0_UT_ASSERT(rc == 0);
232 
234 
235  fom->fo_ops = &ut_stats_update_fom_ops;
236  fom->fo_local = true;
237 
239 
240  m0_fom_queue(fom);
242 
245 
246  check_stats(&srv->ss_stats, count);
248 }
249 
251 {
252  struct m0_reqh *reqh;
253  struct m0_reqh_service *reqh_srv;
254  struct stats_svc *srv;
255 
257 
260 
262 
265  M0_UT_ASSERT(reqh_srv != NULL);
267 
268  srv = container_of(reqh_srv, struct stats_svc, ss_reqhs);
270 
271  /*
272  * Test 1:
273  * verify creation of new stats type
274  */
275  f_rate.fr_rate = 1000;
278  stats_sum[0].ss_data.se_nr = 2;
279  stats_sum[0].ss_data.se_data = (uint64_t *)&f_rate;
280 
281  update_fom_test(srv, reqh, 1);
282 
283  /*
284  * Test 2:
285  * verify creation (new stats type) & update (existing stats type)
286  * from stats list
287  */
288  f_rate.fr_rate = 2000;
291  stats_sum[0].ss_data.se_nr = 2;
292  stats_sum[0].ss_data.se_data = (uint64_t *)&f_rate;
293 
294  r_size.rs_avg_size = 1024;
296  stats_sum[1].ss_data.se_nr = 1;
297  stats_sum[1].ss_data.se_data = (uint64_t *)&r_size;
298 
299  update_fom_test(srv, reqh, 2);
300 
301  /*
302  * Test 3:
303  * verify creation (new stats type) & multiple update
304  * (existing stats type) from stats list
305  */
307 
308  update_fom_test(srv, reqh, 3);
309 
311 
313 }
314 
316 {
317  int i;
318  struct m0_stats_query_fop *qfop;
319  struct m0_stats_query_rep_fop *qfop_rep;
320 
321  qfop = m0_stats_query_fop_get(fom->fo_fop);
322  qfop_rep = m0_stats_query_rep_fop_get(fom->fo_rep_fop);
323 
324  M0_UT_ASSERT(qfop_rep->sqrf_stats.sf_nr == qfop->sqf_ids.se_nr);
325 
326  for (i = 0; i < qfop_rep->sqrf_stats.sf_nr; ++i) {
327  struct m0_stats_sum *sum;
328  sum = &(qfop_rep->sqrf_stats.sf_stats[i]);
329  M0_UT_ASSERT(sum->ss_id == stats_sum[i].ss_id);
330  }
331 
332  fom->fo_rep_fop->f_item.ri_rmachine = &ut_stats_machine;
334 
338 }
339 
344 static const struct m0_fom_ops ut_stats_query_fom_ops = {
346  .fo_home_locality = stats_fom_home_locality,
347  .fo_fini = test_state_query_fom_fini
348 };
349 
350 static void query_fom_test(struct stats_svc *srv, struct m0_reqh *reqh,
351  int count)
352 {
353  struct m0_fop *fop;
354  struct m0_fom *fom;
355  struct m0_stats_query_fop *qfop;
356  int rc;
357 
361 
363  M0_UT_ASSERT(fop != NULL);
364 
365  qfop = m0_stats_query_fop_get(fop);
366 
368  M0_UT_ASSERT(qfop->sqf_ids.se_data != NULL);
369  qfop->sqf_ids.se_nr = count;
370  memcpy(qfop->sqf_ids.se_data, stats_ids, count * sizeof(uint64_t));
371 
374  M0_UT_ASSERT(rc == 0);
375 
377 
378  fom->fo_ops = &ut_stats_query_fom_ops;
379  fom->fo_local = true;
380 
382 
383  m0_fom_queue(fom);
385 
389 }
390 
392 {
393  struct m0_reqh *reqh;
394  struct m0_reqh_service *reqh_srv;
395  struct stats_svc *srv;
396 
398 
401 
403 
406  M0_UT_ASSERT(reqh_srv != NULL);
408 
409  srv = container_of(reqh_srv, struct stats_svc, ss_reqhs);
411 
412  /*
413  * Populate stats on stats service.
414  */
416  update_fom_test(srv, reqh, 3);
417 
418  /* Test 1 : Query single stats. */
419  query_fom_test(srv, reqh, 1);
420 
421  /* Test 2 : Query two stats. */
422  query_fom_test(srv, reqh, 2);
423 
424  /* Test 3 : Query all stats. */
425  query_fom_test(srv, reqh, 3);
426 
427  /*
428  * Test 4 : Query stats with some undfined stats ids.
429  * Reply fops should get ss_id for queried stats is
430  * M0_UNDEF_STATS.
431  */
432  stats_ids[1] = 9999;
433  query_fom_test(srv, reqh, 3);
435 
437 
439 }
440 
442 {
443  struct m0_uint64_seq *ids;
444 
445  M0_PRE(count != 0);
446 
447  M0_ALLOC_PTR(ids);
448  M0_UT_ASSERT(ids != NULL);
449 
450  ids->se_nr = count;
451  M0_ALLOC_ARR(ids->se_data, ids->se_nr);
452  M0_UT_ASSERT(ids->se_data != NULL);
453 
454  memcpy(ids->se_data, stats_ids, count * sizeof(uint64_t));
455 
456  return ids;
457 }
458 
459 static void check_stats_recs(struct m0_stats_recs *recs, int num)
460 {
461  int i;
462 
463  M0_UT_ASSERT(recs->sf_nr == num);
464  for (i = 0; i < recs->sf_nr; ++i) {
466  }
467 }
468 
469 static void stats_svc_query_api()
470 {
471  struct m0_reqh *reqh;
472  struct m0_reqh_service *reqh_srv;
473  struct stats_svc *srv;
474  struct m0_uint64_seq *ids;
475  struct m0_stats_recs *stats_recs = NULL;
476  int rc;
477 
479 
482 
484 
487  M0_UT_ASSERT(reqh_srv != NULL);
489 
490  srv = container_of(reqh_srv, struct stats_svc, ss_reqhs);
492 
493  /*
494  * Populate stats on stats service.
495  */
497 
498  update_fom_test(srv, reqh, 3);
499 
500  /* Test 1 : Query single stats. */
502  M0_UT_ASSERT(ids != NULL);
503  rc = m0_stats_query(&cctx.rcx_session, ids, &stats_recs);
504  M0_UT_ASSERT(rc == 0);
505  check_stats_recs(stats_recs, 1);
506  m0_stats_free(stats_recs);
507 
508  /* Test 2 : Query two stats. */
510  M0_UT_ASSERT(ids != NULL);
511  rc = m0_stats_query(&cctx.rcx_session, ids, &stats_recs);
512  M0_UT_ASSERT(rc == 0);
513  check_stats_recs(stats_recs, 2);
514  m0_stats_free(stats_recs);
515 
516  /* Test 3 : Query all stats. */
518  M0_UT_ASSERT(ids != NULL);
519  rc = m0_stats_query(&cctx.rcx_session, ids, &stats_recs);
520  M0_UT_ASSERT(rc == 0);
521  check_stats_recs(stats_recs, 3);
522  m0_stats_free(stats_recs);
523 
524  /*
525  * Test 4 : Query stats with some undfined stats ids.
526  * Reply fops should get ss_id for queried stats is
527  * M0_UNDEF_STATS.
528  */
529  stats_ids[1] = 9999;
531  M0_UT_ASSERT(ids != NULL);
532  rc = m0_stats_query(&cctx.rcx_session, ids, &stats_recs);
533  M0_UT_ASSERT(rc == 0);
534  check_stats_recs(stats_recs, 3);
535  m0_stats_free(stats_recs);
537 
539 
541 }
542 
543 /*
544  * Local variables:
545  * c-indentation-style: "K&R"
546  * c-basic-offset: 8
547  * tab-width: 8
548  * fill-column: 80
549  * scroll-step: 1
550  * End:
551  */
M0_INTERNAL int m0_reqh_service_state_get(const struct m0_reqh_service *s)
Definition: reqh_service.c:560
uint64_t id
Definition: cob.h:2380
Definition: cond.h:99
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
static const struct m0_fom_ops ut_stats_update_fom_ops
Definition: stats_ut_svc.c:169
struct m0_reqh * m0_cs_reqh_get(struct m0_motr *cctx)
Definition: setup.c:1762
M0_INTERNAL void m0_reqh_service_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:402
#define NULL
Definition: misc.h:38
struct m0_rpc_machine ut_stats_machine
Definition: stats_ut_svc.c:35
uint32_t ss_id
Definition: stats_fops.h:58
int(* fo_tick)(struct m0_fom *fom)
Definition: fom.h:663
M0_INTERNAL void m0_fop_init(struct m0_fop *fop, struct m0_fop_type *fopt, void *data, void(*fop_release)(struct m0_ref *))
Definition: fop.c:79
struct m0_uint64_seq ss_data
Definition: stats_fops.h:60
char ** rsx_argv
Definition: rpclib.h:77
M0_INTERNAL void m0_reqh_service_prepare_to_stop(struct m0_reqh_service *service)
Definition: reqh_service.c:375
uint64_t ds_total
Definition: stats_ut_svc.c:67
static void stats_query_fom_fini(struct m0_fom *fom)
Definition: stats_srv.c:708
void fop_release(struct m0_ref *ref)
Definition: stats_ut_svc.c:148
uint64_t rs_avg_size
Definition: stats_ut_svc.c:63
static void stats_ut_svc_query_fom()
Definition: stats_ut_svc.c:391
struct m0_stats_recs sqrf_stats
Definition: stats_fops.h:84
static int sum
Definition: rwlock.c:53
static struct m0_rpc_client_ctx cctx
Definition: rconfc.c:69
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
#define SERVER_STOB_NAME
Definition: filterc_ut.c:53
M0_INTERNAL void m0_sm_group_fini(struct m0_sm_group *grp)
Definition: sm.c:65
M0_INTERNAL void m0_cond_init(struct m0_cond *cond, struct m0_mutex *mutex)
Definition: cond.c:40
static m0_bcount_t count
Definition: xcode.c:167
static void stats_svc_query_api()
Definition: stats_ut_svc.c:469
struct m0_sm_group rm_sm_grp
Definition: rpc_machine.h:82
#define SERVER_DB_NAME
Definition: filterc_ut.c:52
struct disk_stats d_stats
uint64_t * se_data
Definition: stats_fops.h:51
struct m0_uint64_seq sqf_ids
Definition: stats_fops.h:78
static int stats_update_fom_tick(struct m0_fom *fom)
Definition: stats_srv.c:485
int i
Definition: dir.c:1033
void m0_fop_rpc_machine_set(struct m0_fop *fop, struct m0_rpc_machine *mach)
Definition: fop.c:352
static void check_stats_recs(struct m0_stats_recs *recs, int num)
Definition: stats_ut_svc.c:459
Definition: refs.h:34
static void query_fom_test(struct stats_svc *srv, struct m0_reqh *reqh, int count)
Definition: stats_ut_svc.c:350
static int stats_update_fom_create(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: stats_srv.c:458
static int stats_sum_copy(struct m0_stats_sum *s, struct m0_stats_sum *d)
Definition: stats_srv.c:380
struct m0_cond ut_stats_cond
Definition: stats_ut_svc.c:33
static int stats_query_fom_tick(struct m0_fom *fom)
Definition: stats_srv.c:679
M0_INTERNAL void m0_sm_group_init(struct m0_sm_group *grp)
Definition: sm.c:53
struct m0_rpc_server_ctx stats_ut_sctx_bk
Definition: stats_ut_svc.c:34
M0_INTERNAL void m0_cond_fini(struct m0_cond *cond)
Definition: cond.c:46
M0_INTERNAL void m0_reqh_service_fini(struct m0_reqh_service *service)
Definition: reqh_service.c:457
Definition: tlist.h:251
struct m0_stats_sum * sf_stats
Definition: stats_fops.h:67
M0_INTERNAL struct m0_stats_query_rep_fop * m0_stats_query_rep_fop_get(struct m0_fop *fop)
Definition: stats_fops.c:114
M0_INTERNAL void m0_cond_signal(struct m0_cond *cond)
Definition: cond.c:94
static void stop_rpc_client_and_server(void)
Definition: note.c:126
static size_t stats_fom_home_locality(const struct m0_fom *fom)
Definition: stats_srv.c:526
static struct m0_rpc_server_ctx sctx
Definition: console.c:88
struct m0_fop_type m0_fop_stats_update_fopt
Definition: stats_fops.c:41
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
static char * stats_ut_server_argv[]
Definition: stats_ut_svc.c:37
uint32_t se_nr
Definition: stats_fops.h:49
#define SERVER_ENDPOINT
Definition: common.h:31
int m0_stats_query(struct m0_rpc_session *session, struct m0_stats_recs **stats)
Definition: stats_api.c:107
#define SERVER_ADDB_STOB_NAME
Definition: filterc_ut.c:54
Definition: reqh.h:94
Definition: dump.c:103
M0_INTERNAL void m0_fop_fini(struct m0_fop *fop)
Definition: fop.c:136
M0_INTERNAL struct m0_stats_query_fop * m0_stats_query_fop_get(struct m0_fop *fop)
Definition: stats_fops.c:108
stats_id
Definition: stats_ut_svc.c:45
struct m0_addb2__id_intrp ids[]
Definition: dump.c:1074
struct m0_stats_recs suf_stats
Definition: stats_fops.h:72
static struct m0_uint64_seq * create_stats_id_seq(int count)
Definition: stats_ut_svc.c:441
static bool stats_svc_invariant(const struct stats_svc *svc)
Definition: stats_srv.c:216
struct m0_mutex ut_stats_mutex
Definition: stats_ut_svc.c:32
uint64_t ds_used
Definition: stats_ut_svc.c:69
Definition: fom.h:481
static void stats_ut_svc_start_stop()
Definition: stats_ut_svc.c:112
M0_INTERNAL void m0_cond_wait(struct m0_cond *cond)
Definition: cond.c:52
uint64_t fr_avg_turnaround_time_ns
Definition: stats_ut_svc.c:59
M0_INTERNAL struct m0_reqh_service * m0_reqh_service_find(const struct m0_reqh_service_type *st, const struct m0_reqh *reqh)
Definition: reqh_service.c:538
struct m0_reqh reqh
Definition: rm_foms.c:48
struct m0_rpc_session rcx_session
Definition: rpclib.h:147
struct m0_ref f_ref
Definition: fop.h:80
uint64_t ds_free
Definition: stats_ut_svc.c:68
static void update_fom_test(struct stats_svc *srv, struct m0_reqh *reqh, int count)
Definition: stats_ut_svc.c:208
void check_stats(struct m0_tl *stats_list, int count)
Definition: stats_ut_svc.c:101
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
struct m0_stats_sum stats_sum[3]
Definition: stats_ut_svc.c:72
Definition: beck.c:130
#define M0_UT_CONF_PROCESS
Definition: misc.h:45
struct read_size r_size
static struct m0_fop * get_fake_stats_fop(uint32_t nsum, enum fop_type type)
Definition: stats_ut_svc.c:175
static const struct m0_fom_ops ut_stats_query_fom_ops
Definition: stats_ut_svc.c:344
uint64_t sf_nr
Definition: stats_fops.h:65
uint64_t fr_rate
Definition: stats_ut_svc.c:58
void m0_stats_free(struct m0_stats_recs *stats)
Definition: stats_api.c:139
struct m0_reqh_service_type m0_stats_svc_type
Definition: stats_srv.c:297
void m0_fop_put_lock(struct m0_fop *fop)
Definition: fop.c:199
static struct m0_fop * fop
Definition: item.c:57
struct m0_reqh_service ss_reqhs
Definition: stats_srv.h:101
M0_INTERNAL void m0_fom_queue(struct m0_fom *fom)
Definition: fom.c:624
#define SERVER_ENDPOINT_ADDR
Definition: common.h:30
static void fill_stats_input()
Definition: stats_ut_svc.c:79
static void stats_update_fom_fini(struct m0_fom *fom)
Definition: stats_srv.c:512
static void stats_ut_svc_update_fom()
Definition: stats_ut_svc.c:250
M0_INTERNAL struct m0_stats * m0_stats_get(struct m0_tl *stats_list, uint64_t id)
Definition: stats_srv.c:371
#define M0_UT_PATH(name)
Definition: misc.h:41
struct m0_fop_type m0_fop_stats_query_fopt
Definition: stats_fops.c:42
int type
Definition: dir.c:1031
struct fop_rate f_rate
int num
Definition: bulk_if.c:54
void m0_free(void *data)
Definition: memory.c:146
Definition: mutex.h:47
M0_INTERNAL struct m0_stats_update_fop * m0_stats_update_fop_get(struct m0_fop *fop)
Definition: stats_fops.c:102
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
static void start_rpc_client_and_server(void)
Definition: note.c:111
#define M0_UT_ASSERT(a)
Definition: ut.h:46
struct m0_motr rsx_motr_ctx
Definition: rpclib.h:84
fop_type
Definition: stats_ut_svc.c:51
Definition: fop.h:79
static void test_state_update_fom_fini(struct m0_fom *fom)
Definition: stats_ut_svc.c:157
static struct net_srv srv
Definition: net_test.c:52
static int stats_query_fom_create(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: stats_srv.c:617
static void test_state_query_fom_fini(struct m0_fom *fom)
Definition: stats_ut_svc.c:315
uint64_t stats_ids[]
Definition: stats_ut_svc.c:73