service: use id in service stats functions
[dpdk.git] / lib / librte_eal / common / rte_service.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <inttypes.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <dirent.h>
40
41 #include <rte_service.h>
42 #include "include/rte_service_component.h"
43
44 #include <rte_eal.h>
45 #include <rte_lcore.h>
46 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_cycles.h>
49 #include <rte_atomic.h>
50 #include <rte_memory.h>
51 #include <rte_malloc.h>
52
53 #define RTE_SERVICE_NUM_MAX 64
54
55 #define SERVICE_F_REGISTERED    (1 << 0)
56 #define SERVICE_F_STATS_ENABLED (1 << 1)
57
58 /* runstates for services and lcores, denoting if they are active or not */
59 #define RUNSTATE_STOPPED 0
60 #define RUNSTATE_RUNNING 1
61
62 /* internal representation of a service */
63 struct rte_service_spec_impl {
64         /* public part of the struct */
65         struct rte_service_spec spec;
66
67         /* atomic lock that when set indicates a service core is currently
68          * running this service callback. When not set, a core may take the
69          * lock and then run the service callback.
70          */
71         rte_atomic32_t execute_lock;
72
73         /* API set/get-able variables */
74         int32_t runstate;
75         uint8_t internal_flags;
76
77         /* per service statistics */
78         uint32_t num_mapped_cores;
79         uint64_t calls;
80         uint64_t cycles_spent;
81 } __rte_cache_aligned;
82
83 /* the internal values of a service core */
84 struct core_state {
85         /* map of services IDs are run on this core */
86         uint64_t service_mask;
87         uint8_t runstate; /* running or stopped */
88         uint8_t is_service_core; /* set if core is currently a service core */
89
90         /* extreme statistics */
91         uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
92 } __rte_cache_aligned;
93
94 static uint32_t rte_service_count;
95 static struct rte_service_spec_impl *rte_services;
96 static struct core_state *lcore_states;
97 static uint32_t rte_service_library_initialized;
98
99 int32_t rte_service_init(void)
100 {
101         if (rte_service_library_initialized) {
102                 printf("service library init() called, init flag %d\n",
103                         rte_service_library_initialized);
104                 return -EALREADY;
105         }
106
107         rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
108                         sizeof(struct rte_service_spec_impl),
109                         RTE_CACHE_LINE_SIZE);
110         if (!rte_services) {
111                 printf("error allocating rte services array\n");
112                 return -ENOMEM;
113         }
114
115         lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
116                         sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
117         if (!lcore_states) {
118                 printf("error allocating core states array\n");
119                 return -ENOMEM;
120         }
121
122         int i;
123         int count = 0;
124         struct rte_config *cfg = rte_eal_get_configuration();
125         for (i = 0; i < RTE_MAX_LCORE; i++) {
126                 if (lcore_config[i].core_role == ROLE_SERVICE) {
127                         if ((unsigned int)i == cfg->master_lcore)
128                                 continue;
129                         rte_service_lcore_add(i);
130                         count++;
131                 }
132         }
133
134         rte_service_library_initialized = 1;
135         return 0;
136 }
137
138 /* returns 1 if service is registered and has not been unregistered
139  * Returns 0 if service never registered, or has been unregistered
140  */
141 static inline int
142 service_valid(uint32_t id)
143 {
144         return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
145 }
146
147 /* validate ID and retrieve service pointer, or return error value */
148 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do {          \
149         if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))            \
150                 return retval;                                          \
151         service = &rte_services[id];                                    \
152 } while (0)
153
154 /* returns 1 if statistics should be colleced for service
155  * Returns 0 if statistics should not be collected for service
156  */
157 static inline int
158 service_stats_enabled(struct rte_service_spec_impl *impl)
159 {
160         return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
161 }
162
163 static inline int
164 service_mt_safe(struct rte_service_spec_impl *s)
165 {
166         return s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE;
167 }
168
169 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enabled)
170 {
171         struct rte_service_spec_impl *s;
172         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
173
174         if (enabled)
175                 s->internal_flags |= SERVICE_F_STATS_ENABLED;
176         else
177                 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
178
179         return 0;
180 }
181
182 uint32_t
183 rte_service_get_count(void)
184 {
185         return rte_service_count;
186 }
187
188 struct rte_service_spec *
189 rte_service_get_by_id(uint32_t id)
190 {
191         struct rte_service_spec *service = NULL;
192         if (id < rte_service_count)
193                 service = (struct rte_service_spec *)&rte_services[id];
194
195         return service;
196 }
197
198 struct rte_service_spec *rte_service_get_by_name(const char *name)
199 {
200         struct rte_service_spec *service = NULL;
201         int i;
202         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
203                 if (service_valid(i) &&
204                                 strcmp(name, rte_services[i].spec.name) == 0) {
205                         service = (struct rte_service_spec *)&rte_services[i];
206                         break;
207                 }
208         }
209
210         return service;
211 }
212
213 const char *
214 rte_service_get_name(uint32_t id)
215 {
216         struct rte_service_spec_impl *s;
217         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
218         return s->spec.name;
219 }
220
221 int32_t
222 rte_service_probe_capability(uint32_t id, uint32_t capability)
223 {
224         struct rte_service_spec_impl *s;
225         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
226         return s->spec.capabilities & capability;
227 }
228
229 int32_t
230 rte_service_component_register(const struct rte_service_spec *spec,
231                                uint32_t *id_ptr)
232 {
233         uint32_t i;
234         int32_t free_slot = -1;
235
236         if (spec->callback == NULL || strlen(spec->name) == 0)
237                 return -EINVAL;
238
239         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
240                 if (!service_valid(i)) {
241                         free_slot = i;
242                         break;
243                 }
244         }
245
246         if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
247                 return -ENOSPC;
248
249         struct rte_service_spec_impl *s = &rte_services[free_slot];
250         s->spec = *spec;
251         s->internal_flags |= SERVICE_F_REGISTERED;
252
253         rte_smp_wmb();
254         rte_service_count++;
255
256         if (id_ptr)
257                 *id_ptr = free_slot;
258
259         return 0;
260 }
261
262 int32_t
263 rte_service_unregister(struct rte_service_spec *spec)
264 {
265         struct rte_service_spec_impl *s = NULL;
266         struct rte_service_spec_impl *spec_impl =
267                 (struct rte_service_spec_impl *)spec;
268
269         uint32_t i;
270         uint32_t service_id;
271         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
272                 if (&rte_services[i] == spec_impl) {
273                         s = spec_impl;
274                         service_id = i;
275                         break;
276                 }
277         }
278
279         if (!s)
280                 return -EINVAL;
281
282         rte_service_count--;
283         rte_smp_wmb();
284
285         s->internal_flags &= ~(SERVICE_F_REGISTERED);
286
287         for (i = 0; i < RTE_MAX_LCORE; i++)
288                 lcore_states[i].service_mask &= ~(UINT64_C(1) << service_id);
289
290         memset(&rte_services[service_id], 0,
291                         sizeof(struct rte_service_spec_impl));
292
293         return 0;
294 }
295
296 int32_t
297 rte_service_runstate_set(uint32_t id, uint32_t runstate)
298 {
299         struct rte_service_spec_impl *s;
300         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
301
302         if (runstate)
303                 s->runstate = RUNSTATE_RUNNING;
304         else
305                 s->runstate = RUNSTATE_STOPPED;
306
307         rte_smp_wmb();
308         return 0;
309 }
310
311 int32_t
312 rte_service_runstate_get(uint32_t id)
313 {
314         struct rte_service_spec_impl *s;
315         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
316
317         return (s->runstate == RUNSTATE_RUNNING) && (s->num_mapped_cores > 0);
318 }
319
320 static int32_t
321 rte_service_runner_func(void *arg)
322 {
323         RTE_SET_USED(arg);
324         uint32_t i;
325         const int lcore = rte_lcore_id();
326         struct core_state *cs = &lcore_states[lcore];
327
328         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
329                 const uint64_t service_mask = cs->service_mask;
330                 for (i = 0; i < rte_service_count; i++) {
331                         struct rte_service_spec_impl *s = &rte_services[i];
332                         if (s->runstate != RUNSTATE_RUNNING ||
333                                         !(service_mask & (UINT64_C(1) << i)))
334                                 continue;
335
336                         /* check do we need cmpset, if MT safe or <= 1 core
337                          * mapped, atomic ops are not required.
338                          */
339                         const int need_cmpset = !((service_mt_safe(s) == 0) &&
340                                                 (s->num_mapped_cores > 1));
341                         uint32_t *lock = (uint32_t *)&s->execute_lock;
342
343                         if (need_cmpset || rte_atomic32_cmpset(lock, 0, 1)) {
344                                 void *userdata = s->spec.callback_userdata;
345
346                                 if (service_stats_enabled(s)) {
347                                         uint64_t start = rte_rdtsc();
348                                         s->spec.callback(userdata);
349                                         uint64_t end = rte_rdtsc();
350                                         s->cycles_spent += end - start;
351                                         cs->calls_per_service[i]++;
352                                         s->calls++;
353                                 } else
354                                         s->spec.callback(userdata);
355
356                                 if (need_cmpset)
357                                         rte_atomic32_clear(&s->execute_lock);
358                         }
359                 }
360
361                 rte_smp_rmb();
362         }
363
364         lcore_config[lcore].state = WAIT;
365
366         return 0;
367 }
368
369 int32_t
370 rte_service_lcore_count(void)
371 {
372         int32_t count = 0;
373         uint32_t i;
374         for (i = 0; i < RTE_MAX_LCORE; i++)
375                 count += lcore_states[i].is_service_core;
376         return count;
377 }
378
379 int32_t
380 rte_service_lcore_list(uint32_t array[], uint32_t n)
381 {
382         uint32_t count = rte_service_lcore_count();
383         if (count > n)
384                 return -ENOMEM;
385
386         if (!array)
387                 return -EINVAL;
388
389         uint32_t i;
390         uint32_t idx = 0;
391         for (i = 0; i < RTE_MAX_LCORE; i++) {
392                 struct core_state *cs = &lcore_states[i];
393                 if (cs->is_service_core) {
394                         array[idx] = i;
395                         idx++;
396                 }
397         }
398
399         return count;
400 }
401
402 int32_t
403 rte_service_lcore_count_services(uint32_t lcore)
404 {
405         if (lcore >= RTE_MAX_LCORE)
406                 return -EINVAL;
407
408         struct core_state *cs = &lcore_states[lcore];
409         if (!cs->is_service_core)
410                 return -ENOTSUP;
411
412         return __builtin_popcountll(cs->service_mask);
413 }
414
415 int32_t
416 rte_service_start_with_defaults(void)
417 {
418         /* create a default mapping from cores to services, then start the
419          * services to make them transparent to unaware applications.
420          */
421         uint32_t i;
422         int ret;
423         uint32_t count = rte_service_get_count();
424
425         int32_t lcore_iter = 0;
426         uint32_t ids[RTE_MAX_LCORE];
427         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
428
429         if (lcore_count == 0)
430                 return -ENOTSUP;
431
432         for (i = 0; (int)i < lcore_count; i++)
433                 rte_service_lcore_start(ids[i]);
434
435         for (i = 0; i < count; i++) {
436                 struct rte_service_spec *s = rte_service_get_by_id(i);
437                 if (!s)
438                         return -EINVAL;
439
440                 /* do 1:1 core mapping here, with each service getting
441                  * assigned a single core by default. Adding multiple services
442                  * should multiplex to a single core, or 1:1 if there are the
443                  * same amount of services as service-cores
444                  */
445                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
446                 if (ret)
447                         return -ENODEV;
448
449                 lcore_iter++;
450                 if (lcore_iter >= lcore_count)
451                         lcore_iter = 0;
452
453                 ret = rte_service_runstate_set(i, 1);
454                 if (ret)
455                         return -ENOEXEC;
456         }
457
458         return 0;
459 }
460
461 static int32_t
462 service_update(struct rte_service_spec *service, uint32_t lcore,
463                 uint32_t *set, uint32_t *enabled)
464 {
465         uint32_t i;
466         int32_t sid = -1;
467
468         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
469                 if ((struct rte_service_spec *)&rte_services[i] == service &&
470                                 service_valid(i)) {
471                         sid = i;
472                         break;
473                 }
474         }
475
476         if (sid == -1 || lcore >= RTE_MAX_LCORE)
477                 return -EINVAL;
478
479         if (!lcore_states[lcore].is_service_core)
480                 return -EINVAL;
481
482         uint64_t sid_mask = UINT64_C(1) << sid;
483         if (set) {
484                 if (*set) {
485                         lcore_states[lcore].service_mask |= sid_mask;
486                         rte_services[sid].num_mapped_cores++;
487                 } else {
488                         lcore_states[lcore].service_mask &= ~(sid_mask);
489                         rte_services[sid].num_mapped_cores--;
490                 }
491         }
492
493         if (enabled)
494                 *enabled = (lcore_states[lcore].service_mask & (sid_mask));
495
496         rte_smp_wmb();
497
498         return 0;
499 }
500
501 int32_t
502 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
503 {
504         struct rte_service_spec_impl *s;
505         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
506         uint32_t on = enabled > 0;
507         return service_update(&s->spec, lcore, &on, 0);
508 }
509
510 int32_t
511 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
512 {
513         struct rte_service_spec_impl *s;
514         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
515         uint32_t enabled;
516         int ret = service_update(&s->spec, lcore, 0, &enabled);
517         if (ret == 0)
518                 return enabled;
519         return ret;
520 }
521
522 int32_t rte_service_lcore_reset_all(void)
523 {
524         /* loop over cores, reset all to mask 0 */
525         uint32_t i;
526         for (i = 0; i < RTE_MAX_LCORE; i++) {
527                 lcore_states[i].service_mask = 0;
528                 lcore_states[i].is_service_core = 0;
529                 lcore_states[i].runstate = RUNSTATE_STOPPED;
530         }
531         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
532                 rte_services[i].num_mapped_cores = 0;
533
534         rte_smp_wmb();
535
536         return 0;
537 }
538
539 static void
540 set_lcore_state(uint32_t lcore, int32_t state)
541 {
542         /* mark core state in hugepage backed config */
543         struct rte_config *cfg = rte_eal_get_configuration();
544         cfg->lcore_role[lcore] = state;
545
546         /* mark state in process local lcore_config */
547         lcore_config[lcore].core_role = state;
548
549         /* update per-lcore optimized state tracking */
550         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
551 }
552
553 int32_t
554 rte_service_lcore_add(uint32_t lcore)
555 {
556         if (lcore >= RTE_MAX_LCORE)
557                 return -EINVAL;
558         if (lcore_states[lcore].is_service_core)
559                 return -EALREADY;
560
561         set_lcore_state(lcore, ROLE_SERVICE);
562
563         /* ensure that after adding a core the mask and state are defaults */
564         lcore_states[lcore].service_mask = 0;
565         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
566
567         rte_smp_wmb();
568         return 0;
569 }
570
571 int32_t
572 rte_service_lcore_del(uint32_t lcore)
573 {
574         if (lcore >= RTE_MAX_LCORE)
575                 return -EINVAL;
576
577         struct core_state *cs = &lcore_states[lcore];
578         if (!cs->is_service_core)
579                 return -EINVAL;
580
581         if (cs->runstate != RUNSTATE_STOPPED)
582                 return -EBUSY;
583
584         set_lcore_state(lcore, ROLE_RTE);
585
586         rte_smp_wmb();
587         return 0;
588 }
589
590 int32_t
591 rte_service_lcore_start(uint32_t lcore)
592 {
593         if (lcore >= RTE_MAX_LCORE)
594                 return -EINVAL;
595
596         struct core_state *cs = &lcore_states[lcore];
597         if (!cs->is_service_core)
598                 return -EINVAL;
599
600         if (cs->runstate == RUNSTATE_RUNNING)
601                 return -EALREADY;
602
603         /* set core to run state first, and then launch otherwise it will
604          * return immediately as runstate keeps it in the service poll loop
605          */
606         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
607
608         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
609         /* returns -EBUSY if the core is already launched, 0 on success */
610         return ret;
611 }
612
613 int32_t
614 rte_service_lcore_stop(uint32_t lcore)
615 {
616         if (lcore >= RTE_MAX_LCORE)
617                 return -EINVAL;
618
619         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
620                 return -EALREADY;
621
622         uint32_t i;
623         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
624                 int32_t enabled =
625                         lcore_states[i].service_mask & (UINT64_C(1) << i);
626                 int32_t service_running = rte_services[i].runstate !=
627                                                 RUNSTATE_STOPPED;
628                 int32_t only_core = rte_services[i].num_mapped_cores == 1;
629
630                 /* if the core is mapped, and the service is running, and this
631                  * is the only core that is mapped, the service would cease to
632                  * run if this core stopped, so fail instead.
633                  */
634                 if (enabled && service_running && only_core)
635                         return -EBUSY;
636         }
637
638         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
639
640         return 0;
641 }
642
643 static void
644 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
645                      uint64_t all_cycles, uint32_t reset)
646 {
647         /* avoid divide by zero */
648         if (all_cycles == 0)
649                 all_cycles = 1;
650
651         int calls = 1;
652         if (s->calls != 0)
653                 calls = s->calls;
654
655         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
656                         PRIu64"\tavg: %"PRIu64"\n",
657                         s->spec.name, service_stats_enabled(s), s->calls,
658                         s->cycles_spent, s->cycles_spent / calls);
659
660         if (reset) {
661                 s->cycles_spent = 0;
662                 s->calls = 0;
663         }
664 }
665
666 static void
667 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
668 {
669         uint32_t i;
670         struct core_state *cs = &lcore_states[lcore];
671
672         fprintf(f, "%02d\t", lcore);
673         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
674                 if (!service_valid(i))
675                         continue;
676                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
677                 if (reset)
678                         cs->calls_per_service[i] = 0;
679         }
680         fprintf(f, "\n");
681 }
682
683 int32_t rte_service_dump(FILE *f, uint32_t id)
684 {
685         uint32_t i;
686         int print_one = (id != UINT32_MAX);
687
688         uint64_t total_cycles = 0;
689         for (i = 0; i < rte_service_count; i++) {
690                 if (!service_valid(i))
691                         continue;
692                 total_cycles += rte_services[i].cycles_spent;
693         }
694
695         /* print only the specified service */
696         if (print_one) {
697                 struct rte_service_spec_impl *s;
698                 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
699                 fprintf(f, "Service %s Summary\n", s->spec.name);
700                 uint32_t reset = 0;
701                 rte_service_dump_one(f, s, total_cycles, reset);
702                 return 0;
703         }
704
705         /* print all services, as UINT32_MAX was passed as id */
706         fprintf(f, "Services Summary\n");
707         for (i = 0; i < rte_service_count; i++) {
708                 uint32_t reset = 1;
709                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
710         }
711
712         fprintf(f, "Service Cores Summary\n");
713         for (i = 0; i < RTE_MAX_LCORE; i++) {
714                 if (lcore_config[i].core_role != ROLE_SERVICE)
715                         continue;
716
717                 uint32_t reset = 0;
718                 service_dump_calls_per_lcore(f, i, reset);
719         }
720
721         return 0;
722 }