API Reference Manual 1.51.0
Loading...
Searching...
No Matches
odp_timer_simple.c
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2016-2018 Linaro Limited
3 */
4
13#include <string.h>
14#include <stdlib.h>
15#include <inttypes.h>
16
17/* ODP main header */
18#include <odp_api.h>
19
20#include <odp/helper/odph_api.h>
21
22int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED)
23{
24 odp_instance_t instance;
25 odp_pool_param_t params;
27 odp_pool_t timeout_pool;
28 odp_timer_pool_t timer_pool;
29 odp_queue_param_t qparam;
30 odp_queue_t queue;
32 odp_timer_t tim;
33 uint64_t sched_tmo;
34 int i, rc;
35 uint64_t period;
36 uint64_t tick;
37 odp_timeout_t tmo;
38 int ret = 0;
39 odp_timer_capability_t timer_capa;
40
41 /*
42 * Init ODP app
43 */
44 if (odp_init_global(&instance, NULL, NULL))
45 goto err_global;
46
48 goto err_local;
49
50 /*
51 * Create pool for timeouts
52 */
53 odp_pool_param_init(&params);
54 params.tmo.num = 10;
55 params.type = ODP_POOL_TIMEOUT;
56
57 timeout_pool = odp_pool_create("timeout_pool", &params);
58 if (timeout_pool == ODP_POOL_INVALID) {
59 ret += 1;
60 goto err_tp;
61 }
62
63 /*
64 * Create pool of timeouts
65 */
66 if (odp_timer_capability(ODP_CLOCK_DEFAULT, &timer_capa)) {
67 ret += 1;
68 goto err_tp;
69 }
70
72 tparams.res_ns = ODPH_MAX(10 * ODP_TIME_MSEC_IN_NS,
73 timer_capa.highest_res_ns);
74 tparams.min_tmo = 10 * ODP_TIME_MSEC_IN_NS;
75 tparams.max_tmo = 1 * ODP_TIME_SEC_IN_NS;
76 tparams.num_timers = 1; /* One timer per worker */
77 tparams.priv = 0; /* Shared */
78 tparams.clk_src = ODP_CLOCK_DEFAULT;
79 timer_pool = odp_timer_pool_create("timer_pool", &tparams);
80 if (timer_pool == ODP_TIMER_POOL_INVALID) {
81 ret += 1;
82 goto err;
83 }
84
85 if (odp_timer_pool_start_multi(&timer_pool, 1) != 1) {
86 ODPH_ERR("Timer pool start failed\n");
87 ret += 1;
88 goto err;
89 }
90
91 /* Configure scheduler */
93
94 /*
95 * Create a queue for timer test
96 */
97 odp_queue_param_init(&qparam);
102
103 queue = odp_queue_create("timer_queue", &qparam);
104 if (queue == ODP_QUEUE_INVALID) {
105 ret += 1;
106 goto err;
107 }
108
109 tim = odp_timer_alloc(timer_pool, queue, NULL);
110 if (tim == ODP_TIMER_INVALID) {
111 ODPH_ERR("Failed to allocate timer\n");
112 ret += 1;
113 goto err;
114 }
115
116 tmo = odp_timeout_alloc(timeout_pool);
117 if (tmo == ODP_TIMEOUT_INVALID) {
118 ODPH_ERR("Failed to allocate timeout\n");
119 return -1;
120 }
121
122 ev = odp_timeout_to_event(tmo);
123
124 /* Calculate timer period in ticks */
125 period = odp_timer_ns_to_tick(timer_pool, 100 * ODP_TIME_MSEC_IN_NS);
126
127 /* Wait time to return from odp_schedule() if there are no
128 * events
129 */
131
132 for (i = 0; i < 5; i++) {
133 odp_time_t time;
134 odp_timer_start_t start_param;
135
136 /* Program timeout action on current tick + period */
137 tick = odp_timer_current_tick(timer_pool);
138
139 start_param.tick_type = ODP_TIMER_TICK_ABS;
140 start_param.tick = tick + period;
141 start_param.tmo_ev = ev;
142
143 rc = odp_timer_start(tim, &start_param);
144 /* Too early or too late timeout requested */
146 ODPH_ABORT("odp_timer_start() failed: %d\n", rc);
147
148 /* Wait for 2 seconds for timeout action to be generated */
149 ev = odp_schedule(&queue, sched_tmo);
150 if (ev == ODP_EVENT_INVALID)
151 ODPH_ABORT("Invalid event\n");
152 if (odp_event_type(ev) != ODP_EVENT_TIMEOUT)
153 ODPH_ABORT("Unexpected event type (%u) received\n",
154 odp_event_type(ev));
155
156 time = odp_time_global();
157 printf("timer tick %d, time ns %" PRIu64 "\n",
158 i, odp_time_to_ns(time));
159
160 /* Do not free current event, just go back to loop and program
161 * the next timeout.
162 */
163 }
164
165 /* Destroy created resources */
166 odp_event_free(ev);
167
168 if (odp_timer_free(tim))
169 ret++;
170
171 if (odp_queue_destroy(queue))
172 ret++;
173err:
174 odp_timer_pool_destroy(timer_pool);
175err_tp:
176 if (odp_pool_destroy(timeout_pool))
177 ret++;
178
179 if (odp_term_local())
180 ret++;
181err_local:
182 if (odp_term_global(instance))
183 ret++;
184err_global:
185 return ret;
186}
#define odp_unlikely(x)
Branch unlikely taken.
Definition spec/hints.h:64
#define ODP_UNUSED
Intentionally unused variables of functions.
Definition spec/hints.h:54
void odp_event_free(odp_event_t event)
Free event.
odp_event_type_t odp_event_type(odp_event_t event)
Event type of an event.
#define ODP_EVENT_INVALID
Invalid event.
int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type)
Thread local ODP initialization.
int odp_init_global(odp_instance_t *instance, const odp_init_t *params, const odp_platform_init_t *platform_params)
Global ODP initialization.
int odp_term_local(void)
Thread local ODP termination.
int odp_term_global(odp_instance_t instance)
Global ODP termination.
uint64_t odp_instance_t
ODP instance ID.
odp_pool_t odp_pool_create(const char *name, const odp_pool_param_t *param)
Create a pool.
void odp_pool_param_init(odp_pool_param_t *param)
Initialize pool params.
int odp_pool_destroy(odp_pool_t pool)
Destroy a pool previously created by odp_pool_create()
#define ODP_POOL_INVALID
Invalid pool.
@ ODP_POOL_TIMEOUT
Timeout pool.
void odp_queue_param_init(odp_queue_param_t *param)
Initialize queue params.
#define ODP_QUEUE_INVALID
Invalid queue.
odp_queue_t odp_queue_create(const char *name, const odp_queue_param_t *param)
Queue create.
int odp_queue_destroy(odp_queue_t queue)
Destroy ODP queue.
@ ODP_QUEUE_TYPE_SCHED
Scheduled queue.
#define ODP_SCHED_SYNC_PARALLEL
Parallel scheduled queues.
int odp_schedule_default_prio(void)
Default scheduling priority level.
int odp_schedule_config(const odp_schedule_config_t *config)
Global schedule configuration.
uint64_t odp_schedule_wait_time(uint64_t ns)
Schedule wait time.
odp_event_t odp_schedule(odp_queue_t *from, uint64_t wait)
Schedule an event.
#define ODP_SCHED_GROUP_ALL
Group of all threads.
@ ODP_THREAD_CONTROL
Control thread.
uint64_t odp_time_to_ns(odp_time_t time)
Convert time to nanoseconds.
#define ODP_TIME_SEC_IN_NS
A second in nanoseconds.
odp_time_t odp_time_global(void)
Current global time.
#define ODP_TIME_MSEC_IN_NS
A millisecond in nanoseconds.
int odp_timer_pool_start_multi(odp_timer_pool_t timer_pool[], int num)
Start timer pools.
odp_timeout_t odp_timeout_alloc(odp_pool_t pool)
Timeout alloc.
int odp_timer_free(odp_timer_t timer)
Free a timer.
#define ODP_TIMER_POOL_INVALID
Invalid timer pool handle.
odp_timer_pool_t odp_timer_pool_create(const char *name, const odp_timer_pool_param_t *params)
Create a timer pool.
uint64_t odp_timer_current_tick(odp_timer_pool_t timer_pool)
Current tick value.
int odp_timer_capability(odp_timer_clk_src_t clk_src, odp_timer_capability_t *capa)
Query timer capabilities per clock source.
uint64_t odp_timer_ns_to_tick(odp_timer_pool_t timer_pool, uint64_t ns)
Convert nanoseconds to timer ticks.
int odp_timer_start(odp_timer_t timer, const odp_timer_start_t *start_param)
Start a single shot timer.
odp_event_t odp_timeout_to_event(odp_timeout_t tmo)
Convert timeout handle to event handle.
#define ODP_TIMEOUT_INVALID
Invalid timeout handle.
odp_timer_t odp_timer_alloc(odp_timer_pool_t timer_pool, odp_queue_t queue, const void *user_ptr)
Allocate a single shot timer.
#define ODP_CLOCK_DEFAULT
The default clock source.
#define ODP_TIMER_INVALID
Invalid timer handle.
void odp_timer_pool_param_init(odp_timer_pool_param_t *param)
Initialize timer pool parameters.
void odp_timer_pool_destroy(odp_timer_pool_t timer_pool)
Destroy a timer pool.
@ ODP_TIMER_SUCCESS
Timer operation succeeded.
@ ODP_TIMER_TICK_ABS
Absolute ticks.
The OpenDataPlane API.
uint32_t num
Number of buffers in the pool.
odp_pool_type_t type
Pool type.
struct odp_pool_param_t::@140 tmo
Parameters for timeout pools.
ODP Queue parameters.
odp_schedule_param_t sched
Scheduler parameters.
odp_queue_type_t type
Queue type.
odp_schedule_group_t group
Thread group.
odp_schedule_prio_t prio
Priority level.
odp_schedule_sync_t sync
Synchronization method.
uint64_t highest_res_ns
Highest timer resolution in nanoseconds.
Timer pool parameters.
uint64_t res_ns
Timeout resolution in nanoseconds.
int priv
Thread private timer pool.
uint64_t min_tmo
Minimum relative timeout in nanoseconds.
uint32_t num_timers
Number of timers in the pool.
odp_timer_clk_src_t clk_src
Clock source for timers.
uint64_t max_tmo
Maximum relative timeout in nanoseconds.
Timer start parameters.
uint64_t tick
Expiration time in ticks.
odp_event_t tmo_ev
Timeout event.
odp_timer_tick_type_t tick_type
Tick type.