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