1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
10 #include <rte_common.h>
13 #include <rte_cycles.h>
14 #include <rte_branch_prediction.h>
16 #include "rte_jobstats.h"
18 #define ADD_TIME_MIN_MAX(obj, type, value) do { \
19 typeof(value) tmp = (value); \
20 (obj)->type ## _time += tmp; \
21 if (tmp < (obj)->min_ ## type ## _time) \
22 (obj)->min_ ## type ## _time = tmp; \
23 if (tmp > (obj)->max_ ## type ## _time) \
24 (obj)->max_ ## type ## _time = tmp; \
27 #define RESET_TIME_MIN_MAX(obj, type) do { \
28 (obj)->type ## _time = 0; \
29 (obj)->min_ ## type ## _time = UINT64_MAX; \
30 (obj)->max_ ## type ## _time = 0; \
33 static inline uint64_t
37 return rte_get_timer_cycles();
40 /* Those are steps used to adjust job period.
41 * Experiments show that for forwarding apps the up step must be less than down
42 * step to achieve optimal performance.
44 #define JOB_UPDATE_STEP_UP 1
45 #define JOB_UPDATE_STEP_DOWN 4
48 * Default update function that implements simple period adjustment.
51 default_update_function(struct rte_jobstats *job, int64_t result)
53 int64_t err = job->target - result;
55 /* Job is happy. Nothing to do */
60 if (job->period + JOB_UPDATE_STEP_UP < job->max_period)
61 job->period += JOB_UPDATE_STEP_UP;
63 if (job->min_period + JOB_UPDATE_STEP_DOWN < job->period)
64 job->period -= JOB_UPDATE_STEP_DOWN;
69 rte_jobstats_context_init(struct rte_jobstats_context *ctx)
74 /* Init only needed parameters. Zero out everything else. */
75 memset(ctx, 0, sizeof(struct rte_jobstats_context));
77 rte_jobstats_context_reset(ctx);
83 rte_jobstats_context_start(struct rte_jobstats_context *ctx)
87 ctx->loop_executed_jobs = 0;
90 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
91 ctx->state_time = now;
95 rte_jobstats_context_finish(struct rte_jobstats_context *ctx)
99 if (likely(ctx->loop_executed_jobs))
103 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
104 ctx->state_time = now;
108 rte_jobstats_context_reset(struct rte_jobstats_context *ctx)
110 RESET_TIME_MIN_MAX(ctx, exec);
111 RESET_TIME_MIN_MAX(ctx, management);
112 ctx->start_time = get_time();
113 ctx->state_time = ctx->start_time;
114 ctx->job_exec_cnt = 0;
119 rte_jobstats_set_target(struct rte_jobstats *job, int64_t target)
121 job->target = target;
125 rte_jobstats_start(struct rte_jobstats_context *ctx, struct rte_jobstats *job)
129 /* Some sanity check. */
130 if (unlikely(ctx == NULL || job == NULL || job->context != NULL))
133 /* Link job with context object. */
137 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
138 ctx->state_time = now;
144 rte_jobstats_abort(struct rte_jobstats *job)
146 struct rte_jobstats_context *ctx;
147 uint64_t now, exec_time;
149 /* Some sanity check. */
150 if (unlikely(job == NULL || job->context == NULL))
155 exec_time = now - ctx->state_time;
156 ADD_TIME_MIN_MAX(ctx, management, exec_time);
157 ctx->state_time = now;
164 rte_jobstats_finish(struct rte_jobstats *job, int64_t job_value)
166 struct rte_jobstats_context *ctx;
167 uint64_t now, exec_time;
170 /* Some sanity check. */
171 if (unlikely(job == NULL || job->context == NULL))
174 need_update = job->target != job_value;
175 /* Adjust period only if job is unhappy of its current period. */
177 (*job->update_period_cb)(job, job_value);
181 /* Update execution time is considered as runtime so get time after it is
184 exec_time = now - ctx->state_time;
185 ADD_TIME_MIN_MAX(job, exec, exec_time);
186 ADD_TIME_MIN_MAX(ctx, exec, exec_time);
188 ctx->state_time = now;
190 ctx->loop_executed_jobs++;
200 rte_jobstats_set_period(struct rte_jobstats *job, uint64_t period,
204 if (period < job->min_period)
205 period = job->min_period;
206 else if (period > job->max_period)
207 period = job->max_period;
210 job->period = period;
214 rte_jobstats_set_min(struct rte_jobstats *job, uint64_t period)
216 job->min_period = period;
217 if (job->period < period)
218 job->period = period;
222 rte_jobstats_set_max(struct rte_jobstats *job, uint64_t period)
224 job->max_period = period;
225 if (job->period > period)
226 job->period = period;
230 rte_jobstats_init(struct rte_jobstats *job, const char *name,
231 uint64_t min_period, uint64_t max_period, uint64_t initial_period,
237 job->period = initial_period;
238 job->min_period = min_period;
239 job->max_period = max_period;
240 job->target = target;
241 job->update_period_cb = &default_update_function;
242 rte_jobstats_reset(job);
243 snprintf(job->name, RTE_DIM(job->name), "%s", name == NULL ? "" : name);
250 rte_jobstats_set_update_period_function(struct rte_jobstats *job,
251 rte_job_update_period_cb_t update_period_cb)
253 if (update_period_cb == NULL)
254 update_period_cb = default_update_function;
256 job->update_period_cb = update_period_cb;
260 rte_jobstats_reset(struct rte_jobstats *job)
262 RESET_TIME_MIN_MAX(job, exec);