New upstream version 17.11.1
[deb_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 #define SERVICE_F_START_CHECK   (1 << 2)
58
59 /* runstates for services and lcores, denoting if they are active or not */
60 #define RUNSTATE_STOPPED 0
61 #define RUNSTATE_RUNNING 1
62
63 /* internal representation of a service */
64 struct rte_service_spec_impl {
65         /* public part of the struct */
66         struct rte_service_spec spec;
67
68         /* atomic lock that when set indicates a service core is currently
69          * running this service callback. When not set, a core may take the
70          * lock and then run the service callback.
71          */
72         rte_atomic32_t execute_lock;
73
74         /* API set/get-able variables */
75         int8_t app_runstate;
76         int8_t comp_runstate;
77         uint8_t internal_flags;
78
79         /* per service statistics */
80         rte_atomic32_t num_mapped_cores;
81         uint64_t calls;
82         uint64_t cycles_spent;
83 } __rte_cache_aligned;
84
85 /* the internal values of a service core */
86 struct core_state {
87         /* map of services IDs are run on this core */
88         uint64_t service_mask;
89         uint8_t runstate; /* running or stopped */
90         uint8_t is_service_core; /* set if core is currently a service core */
91
92         /* extreme statistics */
93         uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
94 } __rte_cache_aligned;
95
96 static uint32_t rte_service_count;
97 static struct rte_service_spec_impl *rte_services;
98 static struct core_state *lcore_states;
99 static uint32_t rte_service_library_initialized;
100
101 int32_t rte_service_init(void)
102 {
103         if (rte_service_library_initialized) {
104                 printf("service library init() called, init flag %d\n",
105                         rte_service_library_initialized);
106                 return -EALREADY;
107         }
108
109         rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
110                         sizeof(struct rte_service_spec_impl),
111                         RTE_CACHE_LINE_SIZE);
112         if (!rte_services) {
113                 printf("error allocating rte services array\n");
114                 goto fail_mem;
115         }
116
117         lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
118                         sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
119         if (!lcore_states) {
120                 printf("error allocating core states array\n");
121                 goto fail_mem;
122         }
123
124         int i;
125         int count = 0;
126         struct rte_config *cfg = rte_eal_get_configuration();
127         for (i = 0; i < RTE_MAX_LCORE; i++) {
128                 if (lcore_config[i].core_role == ROLE_SERVICE) {
129                         if ((unsigned int)i == cfg->master_lcore)
130                                 continue;
131                         rte_service_lcore_add(i);
132                         count++;
133                 }
134         }
135
136         rte_service_library_initialized = 1;
137         return 0;
138 fail_mem:
139         if (rte_services)
140                 rte_free(rte_services);
141         if (lcore_states)
142                 rte_free(lcore_states);
143         return -ENOMEM;
144 }
145
146 /* returns 1 if service is registered and has not been unregistered
147  * Returns 0 if service never registered, or has been unregistered
148  */
149 static inline int
150 service_valid(uint32_t id)
151 {
152         return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
153 }
154
155 /* validate ID and retrieve service pointer, or return error value */
156 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do {          \
157         if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))            \
158                 return retval;                                          \
159         service = &rte_services[id];                                    \
160 } while (0)
161
162 /* returns 1 if statistics should be collected for service
163  * Returns 0 if statistics should not be collected for service
164  */
165 static inline int
166 service_stats_enabled(struct rte_service_spec_impl *impl)
167 {
168         return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
169 }
170
171 static inline int
172 service_mt_safe(struct rte_service_spec_impl *s)
173 {
174         return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
175 }
176
177 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enabled)
178 {
179         struct rte_service_spec_impl *s;
180         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
181
182         if (enabled)
183                 s->internal_flags |= SERVICE_F_STATS_ENABLED;
184         else
185                 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
186
187         return 0;
188 }
189
190 int32_t rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
191 {
192         struct rte_service_spec_impl *s;
193         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
194
195         if (enabled)
196                 s->internal_flags |= SERVICE_F_START_CHECK;
197         else
198                 s->internal_flags &= ~(SERVICE_F_START_CHECK);
199
200         return 0;
201 }
202
203 uint32_t
204 rte_service_get_count(void)
205 {
206         return rte_service_count;
207 }
208
209 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id)
210 {
211         if (!service_id)
212                 return -EINVAL;
213
214         int i;
215         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
216                 if (service_valid(i) &&
217                                 strcmp(name, rte_services[i].spec.name) == 0) {
218                         *service_id = i;
219                         return 0;
220                 }
221         }
222
223         return -ENODEV;
224 }
225
226 const char *
227 rte_service_get_name(uint32_t id)
228 {
229         struct rte_service_spec_impl *s;
230         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
231         return s->spec.name;
232 }
233
234 int32_t
235 rte_service_probe_capability(uint32_t id, uint32_t capability)
236 {
237         struct rte_service_spec_impl *s;
238         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
239         return !!(s->spec.capabilities & capability);
240 }
241
242 int32_t
243 rte_service_component_register(const struct rte_service_spec *spec,
244                                uint32_t *id_ptr)
245 {
246         uint32_t i;
247         int32_t free_slot = -1;
248
249         if (spec->callback == NULL || strlen(spec->name) == 0)
250                 return -EINVAL;
251
252         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
253                 if (!service_valid(i)) {
254                         free_slot = i;
255                         break;
256                 }
257         }
258
259         if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
260                 return -ENOSPC;
261
262         struct rte_service_spec_impl *s = &rte_services[free_slot];
263         s->spec = *spec;
264         s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
265
266         rte_smp_wmb();
267         rte_service_count++;
268
269         if (id_ptr)
270                 *id_ptr = free_slot;
271
272         return 0;
273 }
274
275 int32_t
276 rte_service_component_unregister(uint32_t id)
277 {
278         uint32_t i;
279         struct rte_service_spec_impl *s;
280         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
281
282         rte_service_count--;
283         rte_smp_wmb();
284
285         s->internal_flags &= ~(SERVICE_F_REGISTERED);
286
287         /* clear the run-bit in all cores */
288         for (i = 0; i < RTE_MAX_LCORE; i++)
289                 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
290
291         memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
292
293         return 0;
294 }
295
296 int32_t
297 rte_service_component_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->comp_runstate = RUNSTATE_RUNNING;
304         else
305                 s->comp_runstate = RUNSTATE_STOPPED;
306
307         rte_smp_wmb();
308         return 0;
309 }
310
311 int32_t
312 rte_service_runstate_set(uint32_t id, uint32_t runstate)
313 {
314         struct rte_service_spec_impl *s;
315         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
316
317         if (runstate)
318                 s->app_runstate = RUNSTATE_RUNNING;
319         else
320                 s->app_runstate = RUNSTATE_STOPPED;
321
322         rte_smp_wmb();
323         return 0;
324 }
325
326 int32_t
327 rte_service_runstate_get(uint32_t id)
328 {
329         struct rte_service_spec_impl *s;
330         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
331         rte_smp_rmb();
332
333         int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
334         int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
335
336         return (s->app_runstate == RUNSTATE_RUNNING) &&
337                 (s->comp_runstate == RUNSTATE_RUNNING) &&
338                 (check_disabled | lcore_mapped);
339 }
340
341 static inline void
342 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
343                                struct core_state *cs, uint32_t service_idx)
344 {
345         void *userdata = s->spec.callback_userdata;
346
347         if (service_stats_enabled(s)) {
348                 uint64_t start = rte_rdtsc();
349                 s->spec.callback(userdata);
350                 uint64_t end = rte_rdtsc();
351                 s->cycles_spent += end - start;
352                 cs->calls_per_service[service_idx]++;
353                 s->calls++;
354         } else
355                 s->spec.callback(userdata);
356 }
357
358
359 static inline int32_t
360 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
361 {
362         if (!service_valid(i))
363                 return -EINVAL;
364         struct rte_service_spec_impl *s = &rte_services[i];
365         if (s->comp_runstate != RUNSTATE_RUNNING ||
366                         s->app_runstate != RUNSTATE_RUNNING ||
367                         !(service_mask & (UINT64_C(1) << i)))
368                 return -ENOEXEC;
369
370         /* check do we need cmpset, if MT safe or <= 1 core
371          * mapped, atomic ops are not required.
372          */
373         const int use_atomics = (service_mt_safe(s) == 0) &&
374                                 (rte_atomic32_read(&s->num_mapped_cores) > 1);
375         if (use_atomics) {
376                 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
377                         return -EBUSY;
378
379                 rte_service_runner_do_callback(s, cs, i);
380                 rte_atomic32_clear(&s->execute_lock);
381         } else
382                 rte_service_runner_do_callback(s, cs, i);
383
384         return 0;
385 }
386
387 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
388                 uint32_t serialize_mt_unsafe)
389 {
390         /* run service on calling core, using all-ones as the service mask */
391         if (!service_valid(id))
392                 return -EINVAL;
393
394         struct core_state *cs = &lcore_states[rte_lcore_id()];
395         struct rte_service_spec_impl *s = &rte_services[id];
396
397         /* Atomically add this core to the mapped cores first, then examine if
398          * we can run the service. This avoids a race condition between
399          * checking the value, and atomically adding to the mapped count.
400          */
401         if (serialize_mt_unsafe)
402                 rte_atomic32_inc(&s->num_mapped_cores);
403
404         if (service_mt_safe(s) == 0 &&
405                         rte_atomic32_read(&s->num_mapped_cores) > 1) {
406                 if (serialize_mt_unsafe)
407                         rte_atomic32_dec(&s->num_mapped_cores);
408                 return -EBUSY;
409         }
410
411         int ret = service_run(id, cs, UINT64_MAX);
412
413         if (serialize_mt_unsafe)
414                 rte_atomic32_dec(&s->num_mapped_cores);
415
416         return ret;
417 }
418
419 static int32_t
420 rte_service_runner_func(void *arg)
421 {
422         RTE_SET_USED(arg);
423         uint32_t i;
424         const int lcore = rte_lcore_id();
425         struct core_state *cs = &lcore_states[lcore];
426
427         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
428                 const uint64_t service_mask = cs->service_mask;
429
430                 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
431                         /* return value ignored as no change to code flow */
432                         service_run(i, cs, service_mask);
433                 }
434
435                 rte_smp_rmb();
436         }
437
438         lcore_config[lcore].state = WAIT;
439
440         return 0;
441 }
442
443 int32_t
444 rte_service_lcore_count(void)
445 {
446         int32_t count = 0;
447         uint32_t i;
448         for (i = 0; i < RTE_MAX_LCORE; i++)
449                 count += lcore_states[i].is_service_core;
450         return count;
451 }
452
453 int32_t
454 rte_service_lcore_list(uint32_t array[], uint32_t n)
455 {
456         uint32_t count = rte_service_lcore_count();
457         if (count > n)
458                 return -ENOMEM;
459
460         if (!array)
461                 return -EINVAL;
462
463         uint32_t i;
464         uint32_t idx = 0;
465         for (i = 0; i < RTE_MAX_LCORE; i++) {
466                 struct core_state *cs = &lcore_states[i];
467                 if (cs->is_service_core) {
468                         array[idx] = i;
469                         idx++;
470                 }
471         }
472
473         return count;
474 }
475
476 int32_t
477 rte_service_lcore_count_services(uint32_t lcore)
478 {
479         if (lcore >= RTE_MAX_LCORE)
480                 return -EINVAL;
481
482         struct core_state *cs = &lcore_states[lcore];
483         if (!cs->is_service_core)
484                 return -ENOTSUP;
485
486         return __builtin_popcountll(cs->service_mask);
487 }
488
489 int32_t
490 rte_service_start_with_defaults(void)
491 {
492         /* create a default mapping from cores to services, then start the
493          * services to make them transparent to unaware applications.
494          */
495         uint32_t i;
496         int ret;
497         uint32_t count = rte_service_get_count();
498
499         int32_t lcore_iter = 0;
500         uint32_t ids[RTE_MAX_LCORE] = {0};
501         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
502
503         if (lcore_count == 0)
504                 return -ENOTSUP;
505
506         for (i = 0; (int)i < lcore_count; i++)
507                 rte_service_lcore_start(ids[i]);
508
509         for (i = 0; i < count; i++) {
510                 /* do 1:1 core mapping here, with each service getting
511                  * assigned a single core by default. Adding multiple services
512                  * should multiplex to a single core, or 1:1 if there are the
513                  * same amount of services as service-cores
514                  */
515                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
516                 if (ret)
517                         return -ENODEV;
518
519                 lcore_iter++;
520                 if (lcore_iter >= lcore_count)
521                         lcore_iter = 0;
522
523                 ret = rte_service_runstate_set(i, 1);
524                 if (ret)
525                         return -ENOEXEC;
526         }
527
528         return 0;
529 }
530
531 static int32_t
532 service_update(struct rte_service_spec *service, uint32_t lcore,
533                 uint32_t *set, uint32_t *enabled)
534 {
535         uint32_t i;
536         int32_t sid = -1;
537
538         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
539                 if ((struct rte_service_spec *)&rte_services[i] == service &&
540                                 service_valid(i)) {
541                         sid = i;
542                         break;
543                 }
544         }
545
546         if (sid == -1 || lcore >= RTE_MAX_LCORE)
547                 return -EINVAL;
548
549         if (!lcore_states[lcore].is_service_core)
550                 return -EINVAL;
551
552         uint64_t sid_mask = UINT64_C(1) << sid;
553         if (set) {
554                 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
555                         sid_mask;
556
557                 if (*set && !lcore_mapped) {
558                         lcore_states[lcore].service_mask |= sid_mask;
559                         rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
560                 }
561                 if (!*set && lcore_mapped) {
562                         lcore_states[lcore].service_mask &= ~(sid_mask);
563                         rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
564                 }
565         }
566
567         if (enabled)
568                 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
569
570         rte_smp_wmb();
571
572         return 0;
573 }
574
575 int32_t
576 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
577 {
578         struct rte_service_spec_impl *s;
579         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
580         uint32_t on = enabled > 0;
581         return service_update(&s->spec, lcore, &on, 0);
582 }
583
584 int32_t
585 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
586 {
587         struct rte_service_spec_impl *s;
588         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
589         uint32_t enabled;
590         int ret = service_update(&s->spec, lcore, 0, &enabled);
591         if (ret == 0)
592                 return enabled;
593         return ret;
594 }
595
596 static void
597 set_lcore_state(uint32_t lcore, int32_t state)
598 {
599         /* mark core state in hugepage backed config */
600         struct rte_config *cfg = rte_eal_get_configuration();
601         cfg->lcore_role[lcore] = state;
602
603         /* mark state in process local lcore_config */
604         lcore_config[lcore].core_role = state;
605
606         /* update per-lcore optimized state tracking */
607         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
608 }
609
610 int32_t rte_service_lcore_reset_all(void)
611 {
612         /* loop over cores, reset all to mask 0 */
613         uint32_t i;
614         for (i = 0; i < RTE_MAX_LCORE; i++) {
615                 if (lcore_states[i].is_service_core) {
616                         lcore_states[i].service_mask = 0;
617                         set_lcore_state(i, ROLE_RTE);
618                         lcore_states[i].runstate = RUNSTATE_STOPPED;
619                 }
620         }
621         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
622                 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
623
624         rte_smp_wmb();
625
626         return 0;
627 }
628
629 int32_t
630 rte_service_lcore_add(uint32_t lcore)
631 {
632         if (lcore >= RTE_MAX_LCORE)
633                 return -EINVAL;
634         if (lcore_states[lcore].is_service_core)
635                 return -EALREADY;
636
637         set_lcore_state(lcore, ROLE_SERVICE);
638
639         /* ensure that after adding a core the mask and state are defaults */
640         lcore_states[lcore].service_mask = 0;
641         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
642
643         rte_smp_wmb();
644
645         return rte_eal_wait_lcore(lcore);
646 }
647
648 int32_t
649 rte_service_lcore_del(uint32_t lcore)
650 {
651         if (lcore >= RTE_MAX_LCORE)
652                 return -EINVAL;
653
654         struct core_state *cs = &lcore_states[lcore];
655         if (!cs->is_service_core)
656                 return -EINVAL;
657
658         if (cs->runstate != RUNSTATE_STOPPED)
659                 return -EBUSY;
660
661         set_lcore_state(lcore, ROLE_RTE);
662
663         rte_smp_wmb();
664         return 0;
665 }
666
667 int32_t
668 rte_service_lcore_start(uint32_t lcore)
669 {
670         if (lcore >= RTE_MAX_LCORE)
671                 return -EINVAL;
672
673         struct core_state *cs = &lcore_states[lcore];
674         if (!cs->is_service_core)
675                 return -EINVAL;
676
677         if (cs->runstate == RUNSTATE_RUNNING)
678                 return -EALREADY;
679
680         /* set core to run state first, and then launch otherwise it will
681          * return immediately as runstate keeps it in the service poll loop
682          */
683         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
684
685         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
686         /* returns -EBUSY if the core is already launched, 0 on success */
687         return ret;
688 }
689
690 int32_t
691 rte_service_lcore_stop(uint32_t lcore)
692 {
693         if (lcore >= RTE_MAX_LCORE)
694                 return -EINVAL;
695
696         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
697                 return -EALREADY;
698
699         uint32_t i;
700         uint64_t service_mask = lcore_states[lcore].service_mask;
701         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
702                 int32_t enabled = service_mask & (UINT64_C(1) << i);
703                 int32_t service_running = rte_service_runstate_get(i);
704                 int32_t only_core = (1 ==
705                         rte_atomic32_read(&rte_services[i].num_mapped_cores));
706
707                 /* if the core is mapped, and the service is running, and this
708                  * is the only core that is mapped, the service would cease to
709                  * run if this core stopped, so fail instead.
710                  */
711                 if (enabled && service_running && only_core)
712                         return -EBUSY;
713         }
714
715         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
716
717         return 0;
718 }
719
720 static void
721 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
722                      uint64_t all_cycles, uint32_t reset)
723 {
724         /* avoid divide by zero */
725         if (all_cycles == 0)
726                 all_cycles = 1;
727
728         int calls = 1;
729         if (s->calls != 0)
730                 calls = s->calls;
731
732         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
733                         PRIu64"\tavg: %"PRIu64"\n",
734                         s->spec.name, service_stats_enabled(s), s->calls,
735                         s->cycles_spent, s->cycles_spent / calls);
736
737         if (reset) {
738                 s->cycles_spent = 0;
739                 s->calls = 0;
740         }
741 }
742
743 static void
744 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
745 {
746         uint32_t i;
747         struct core_state *cs = &lcore_states[lcore];
748
749         fprintf(f, "%02d\t", lcore);
750         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
751                 if (!service_valid(i))
752                         continue;
753                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
754                 if (reset)
755                         cs->calls_per_service[i] = 0;
756         }
757         fprintf(f, "\n");
758 }
759
760 int32_t rte_service_dump(FILE *f, uint32_t id)
761 {
762         uint32_t i;
763         int print_one = (id != UINT32_MAX);
764
765         uint64_t total_cycles = 0;
766
767         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
768                 if (!service_valid(i))
769                         continue;
770                 total_cycles += rte_services[i].cycles_spent;
771         }
772
773         /* print only the specified service */
774         if (print_one) {
775                 struct rte_service_spec_impl *s;
776                 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
777                 fprintf(f, "Service %s Summary\n", s->spec.name);
778                 uint32_t reset = 0;
779                 rte_service_dump_one(f, s, total_cycles, reset);
780                 return 0;
781         }
782
783         /* print all services, as UINT32_MAX was passed as id */
784         fprintf(f, "Services Summary\n");
785         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
786                 if (!service_valid(i))
787                         continue;
788                 uint32_t reset = 1;
789                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
790         }
791
792         fprintf(f, "Service Cores Summary\n");
793         for (i = 0; i < RTE_MAX_LCORE; i++) {
794                 if (lcore_config[i].core_role != ROLE_SERVICE)
795                         continue;
796
797                 uint32_t reset = 1;
798                 service_dump_calls_per_lcore(f, i, reset);
799         }
800
801         return 0;
802 }