1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606 * Copyright(c) 2010-2014 Intel Corporation
3a9643ea8Slogwang */
4a9643ea8Slogwang
5a9643ea8Slogwang #include <stdio.h>
6a9643ea8Slogwang #include <string.h>
7a9643ea8Slogwang #include <stdint.h>
8a9643ea8Slogwang #include <errno.h>
9a9643ea8Slogwang #include <sys/queue.h>
10a9643ea8Slogwang
11a9643ea8Slogwang #include <rte_common.h>
12a9643ea8Slogwang #include <rte_memory.h>
13a9643ea8Slogwang #include <rte_launch.h>
14a9643ea8Slogwang #include <rte_eal.h>
15a9643ea8Slogwang #include <rte_per_lcore.h>
16a9643ea8Slogwang #include <rte_lcore.h>
17a9643ea8Slogwang #include <rte_cycles.h>
18a9643ea8Slogwang #include <rte_timer.h>
19a9643ea8Slogwang #include <rte_debug.h>
20a9643ea8Slogwang
21a9643ea8Slogwang #define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */
22a9643ea8Slogwang
23a9643ea8Slogwang static struct rte_timer timer0;
24a9643ea8Slogwang static struct rte_timer timer1;
25a9643ea8Slogwang
26a9643ea8Slogwang /* timer0 callback */
27a9643ea8Slogwang static void
timer0_cb(__rte_unused struct rte_timer * tim,__rte_unused void * arg)28*2d9fd380Sjfb8856606 timer0_cb(__rte_unused struct rte_timer *tim,
29*2d9fd380Sjfb8856606 __rte_unused void *arg)
30a9643ea8Slogwang {
31a9643ea8Slogwang static unsigned counter = 0;
32a9643ea8Slogwang unsigned lcore_id = rte_lcore_id();
33a9643ea8Slogwang
34a9643ea8Slogwang printf("%s() on lcore %u\n", __func__, lcore_id);
35a9643ea8Slogwang
36a9643ea8Slogwang /* this timer is automatically reloaded until we decide to
37a9643ea8Slogwang * stop it, when counter reaches 20. */
38a9643ea8Slogwang if ((counter ++) == 20)
39a9643ea8Slogwang rte_timer_stop(tim);
40a9643ea8Slogwang }
41a9643ea8Slogwang
42a9643ea8Slogwang /* timer1 callback */
43a9643ea8Slogwang static void
timer1_cb(__rte_unused struct rte_timer * tim,__rte_unused void * arg)44*2d9fd380Sjfb8856606 timer1_cb(__rte_unused struct rte_timer *tim,
45*2d9fd380Sjfb8856606 __rte_unused void *arg)
46a9643ea8Slogwang {
47a9643ea8Slogwang unsigned lcore_id = rte_lcore_id();
48a9643ea8Slogwang uint64_t hz;
49a9643ea8Slogwang
50a9643ea8Slogwang printf("%s() on lcore %u\n", __func__, lcore_id);
51a9643ea8Slogwang
52a9643ea8Slogwang /* reload it on another lcore */
53a9643ea8Slogwang hz = rte_get_timer_hz();
54a9643ea8Slogwang lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
55a9643ea8Slogwang rte_timer_reset(tim, hz/3, SINGLE, lcore_id, timer1_cb, NULL);
56a9643ea8Slogwang }
57a9643ea8Slogwang
58*2d9fd380Sjfb8856606 static __rte_noreturn int
lcore_mainloop(__rte_unused void * arg)59*2d9fd380Sjfb8856606 lcore_mainloop(__rte_unused void *arg)
60a9643ea8Slogwang {
61a9643ea8Slogwang uint64_t prev_tsc = 0, cur_tsc, diff_tsc;
62a9643ea8Slogwang unsigned lcore_id;
63a9643ea8Slogwang
64a9643ea8Slogwang lcore_id = rte_lcore_id();
65a9643ea8Slogwang printf("Starting mainloop on core %u\n", lcore_id);
66a9643ea8Slogwang
67a9643ea8Slogwang while (1) {
68a9643ea8Slogwang /*
69a9643ea8Slogwang * Call the timer handler on each core: as we don't
70a9643ea8Slogwang * need a very precise timer, so only call
71a9643ea8Slogwang * rte_timer_manage() every ~10ms (at 2Ghz). In a real
72a9643ea8Slogwang * application, this will enhance performances as
73a9643ea8Slogwang * reading the HPET timer is not efficient.
74a9643ea8Slogwang */
75a9643ea8Slogwang cur_tsc = rte_rdtsc();
76a9643ea8Slogwang diff_tsc = cur_tsc - prev_tsc;
77a9643ea8Slogwang if (diff_tsc > TIMER_RESOLUTION_CYCLES) {
78a9643ea8Slogwang rte_timer_manage();
79a9643ea8Slogwang prev_tsc = cur_tsc;
80a9643ea8Slogwang }
81a9643ea8Slogwang }
82a9643ea8Slogwang }
83a9643ea8Slogwang
84a9643ea8Slogwang int
main(int argc,char ** argv)85a9643ea8Slogwang main(int argc, char **argv)
86a9643ea8Slogwang {
87a9643ea8Slogwang int ret;
88a9643ea8Slogwang uint64_t hz;
89a9643ea8Slogwang unsigned lcore_id;
90a9643ea8Slogwang
91a9643ea8Slogwang /* init EAL */
92a9643ea8Slogwang ret = rte_eal_init(argc, argv);
93a9643ea8Slogwang if (ret < 0)
94a9643ea8Slogwang rte_panic("Cannot init EAL\n");
95a9643ea8Slogwang
96a9643ea8Slogwang /* init RTE timer library */
97a9643ea8Slogwang rte_timer_subsystem_init();
98a9643ea8Slogwang
99a9643ea8Slogwang /* init timer structures */
100a9643ea8Slogwang rte_timer_init(&timer0);
101a9643ea8Slogwang rte_timer_init(&timer1);
102a9643ea8Slogwang
103*2d9fd380Sjfb8856606 /* load timer0, every second, on main lcore, reloaded automatically */
104a9643ea8Slogwang hz = rte_get_timer_hz();
105a9643ea8Slogwang lcore_id = rte_lcore_id();
106a9643ea8Slogwang rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);
107a9643ea8Slogwang
108a9643ea8Slogwang /* load timer1, every second/3, on next lcore, reloaded manually */
109a9643ea8Slogwang lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
110a9643ea8Slogwang rte_timer_reset(&timer1, hz/3, SINGLE, lcore_id, timer1_cb, NULL);
111a9643ea8Slogwang
112*2d9fd380Sjfb8856606 /* call lcore_mainloop() on every worker lcore */
113*2d9fd380Sjfb8856606 RTE_LCORE_FOREACH_WORKER(lcore_id) {
114a9643ea8Slogwang rte_eal_remote_launch(lcore_mainloop, NULL, lcore_id);
115a9643ea8Slogwang }
116a9643ea8Slogwang
117*2d9fd380Sjfb8856606 /* call it on main lcore too */
118a9643ea8Slogwang (void) lcore_mainloop(NULL);
119a9643ea8Slogwang
120a9643ea8Slogwang return 0;
121a9643ea8Slogwang }
122