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