1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
9 #include <rte_string_fns.h>
10 #include <rte_errno.h>
11 #include <rte_common.h>
14 #include <rte_cycles.h>
15 #include <rte_branch_prediction.h>
17 #include "rte_jobstats.h"
19 #define ADD_TIME_MIN_MAX(obj, type, value) do { \
20 typeof(value) tmp = (value); \
21 (obj)->type ## _time += tmp; \
22 if (tmp < (obj)->min_ ## type ## _time) \
23 (obj)->min_ ## type ## _time = tmp; \
24 if (tmp > (obj)->max_ ## type ## _time) \
25 (obj)->max_ ## type ## _time = tmp; \
28 #define RESET_TIME_MIN_MAX(obj, type) do { \
29 (obj)->type ## _time = 0; \
30 (obj)->min_ ## type ## _time = UINT64_MAX; \
31 (obj)->max_ ## type ## _time = 0; \
34 static inline uint64_t
38 return rte_get_timer_cycles();
41 /* Those are steps used to adjust job period.
42 * Experiments show that for forwarding apps the up step must be less than down
43 * step to achieve optimal performance.
45 #define JOB_UPDATE_STEP_UP 1
46 #define JOB_UPDATE_STEP_DOWN 4
49 * Default update function that implements simple period adjustment.
52 default_update_function(struct rte_jobstats
*job
, int64_t result
)
54 int64_t err
= job
->target
- result
;
56 /* Job is happy. Nothing to do */
61 if (job
->period
+ JOB_UPDATE_STEP_UP
< job
->max_period
)
62 job
->period
+= JOB_UPDATE_STEP_UP
;
64 if (job
->min_period
+ JOB_UPDATE_STEP_DOWN
< job
->period
)
65 job
->period
-= JOB_UPDATE_STEP_DOWN
;
70 rte_jobstats_context_init(struct rte_jobstats_context
*ctx
)
75 /* Init only needed parameters. Zero out everything else. */
76 memset(ctx
, 0, sizeof(struct rte_jobstats_context
));
78 rte_jobstats_context_reset(ctx
);
84 rte_jobstats_context_start(struct rte_jobstats_context
*ctx
)
88 ctx
->loop_executed_jobs
= 0;
91 ADD_TIME_MIN_MAX(ctx
, management
, now
- ctx
->state_time
);
92 ctx
->state_time
= now
;
96 rte_jobstats_context_finish(struct rte_jobstats_context
*ctx
)
100 if (likely(ctx
->loop_executed_jobs
))
104 ADD_TIME_MIN_MAX(ctx
, management
, now
- ctx
->state_time
);
105 ctx
->state_time
= now
;
109 rte_jobstats_context_reset(struct rte_jobstats_context
*ctx
)
111 RESET_TIME_MIN_MAX(ctx
, exec
);
112 RESET_TIME_MIN_MAX(ctx
, management
);
113 ctx
->start_time
= get_time();
114 ctx
->state_time
= ctx
->start_time
;
115 ctx
->job_exec_cnt
= 0;
120 rte_jobstats_set_target(struct rte_jobstats
*job
, int64_t target
)
122 job
->target
= target
;
126 rte_jobstats_start(struct rte_jobstats_context
*ctx
, struct rte_jobstats
*job
)
130 /* Some sanity check. */
131 if (unlikely(ctx
== NULL
|| job
== NULL
|| job
->context
!= NULL
))
134 /* Link job with context object. */
138 ADD_TIME_MIN_MAX(ctx
, management
, now
- ctx
->state_time
);
139 ctx
->state_time
= now
;
145 rte_jobstats_abort(struct rte_jobstats
*job
)
147 struct rte_jobstats_context
*ctx
;
148 uint64_t now
, exec_time
;
150 /* Some sanity check. */
151 if (unlikely(job
== NULL
|| job
->context
== NULL
))
156 exec_time
= now
- ctx
->state_time
;
157 ADD_TIME_MIN_MAX(ctx
, management
, exec_time
);
158 ctx
->state_time
= now
;
165 rte_jobstats_finish(struct rte_jobstats
*job
, int64_t job_value
)
167 struct rte_jobstats_context
*ctx
;
168 uint64_t now
, exec_time
;
171 /* Some sanity check. */
172 if (unlikely(job
== NULL
|| job
->context
== NULL
))
175 need_update
= job
->target
!= job_value
;
176 /* Adjust period only if job is unhappy of its current period. */
178 (*job
->update_period_cb
)(job
, job_value
);
182 /* Update execution time is considered as runtime so get time after it is
185 exec_time
= now
- ctx
->state_time
;
186 ADD_TIME_MIN_MAX(job
, exec
, exec_time
);
187 ADD_TIME_MIN_MAX(ctx
, exec
, exec_time
);
189 ctx
->state_time
= now
;
191 ctx
->loop_executed_jobs
++;
201 rte_jobstats_set_period(struct rte_jobstats
*job
, uint64_t period
,
205 if (period
< job
->min_period
)
206 period
= job
->min_period
;
207 else if (period
> job
->max_period
)
208 period
= job
->max_period
;
211 job
->period
= period
;
215 rte_jobstats_set_min(struct rte_jobstats
*job
, uint64_t period
)
217 job
->min_period
= period
;
218 if (job
->period
< period
)
219 job
->period
= period
;
223 rte_jobstats_set_max(struct rte_jobstats
*job
, uint64_t period
)
225 job
->max_period
= period
;
226 if (job
->period
> period
)
227 job
->period
= period
;
231 rte_jobstats_init(struct rte_jobstats
*job
, const char *name
,
232 uint64_t min_period
, uint64_t max_period
, uint64_t initial_period
,
238 job
->period
= initial_period
;
239 job
->min_period
= min_period
;
240 job
->max_period
= max_period
;
241 job
->target
= target
;
242 job
->update_period_cb
= &default_update_function
;
243 rte_jobstats_reset(job
);
244 strlcpy(job
->name
, name
== NULL
? "" : name
, RTE_DIM(job
->name
));
251 rte_jobstats_set_update_period_function(struct rte_jobstats
*job
,
252 rte_job_update_period_cb_t update_period_cb
)
254 if (update_period_cb
== NULL
)
255 update_period_cb
= default_update_function
;
257 job
->update_period_cb
= update_period_cb
;
261 rte_jobstats_reset(struct rte_jobstats
*job
)
263 RESET_TIME_MIN_MAX(job
, exec
);