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