4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_errno.h>
39 #include <rte_common.h>
42 #include <rte_cycles.h>
43 #include <rte_branch_prediction.h>
45 #include "rte_jobstats.h"
47 #define ADD_TIME_MIN_MAX(obj, type, value) do { \
48 typeof(value) tmp = (value); \
49 (obj)->type ## _time += tmp; \
50 if (tmp < (obj)->min_ ## type ## _time) \
51 (obj)->min_ ## type ## _time = tmp; \
52 if (tmp > (obj)->max_ ## type ## _time) \
53 (obj)->max_ ## type ## _time = tmp; \
56 #define RESET_TIME_MIN_MAX(obj, type) do { \
57 (obj)->type ## _time = 0; \
58 (obj)->min_ ## type ## _time = UINT64_MAX; \
59 (obj)->max_ ## type ## _time = 0; \
62 static inline uint64_t
66 return rte_get_timer_cycles();
69 /* Those are steps used to adjust job period.
70 * Experiments show that for forwarding apps the up step must be less than down
71 * step to achieve optimal performance.
73 #define JOB_UPDATE_STEP_UP 1
74 #define JOB_UPDATE_STEP_DOWN 4
77 * Default update function that implements simple period adjustment.
80 default_update_function(struct rte_jobstats *job, int64_t result)
82 int64_t err = job->target - result;
84 /* Job is happy. Nothing to do */
89 if (job->period + JOB_UPDATE_STEP_UP < job->max_period)
90 job->period += JOB_UPDATE_STEP_UP;
92 if (job->min_period + JOB_UPDATE_STEP_DOWN < job->period)
93 job->period -= JOB_UPDATE_STEP_DOWN;
98 rte_jobstats_context_init(struct rte_jobstats_context *ctx)
103 /* Init only needed parameters. Zero out everything else. */
104 memset(ctx, 0, sizeof(struct rte_jobstats_context));
106 rte_jobstats_context_reset(ctx);
112 rte_jobstats_context_start(struct rte_jobstats_context *ctx)
116 ctx->loop_executed_jobs = 0;
119 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
120 ctx->state_time = now;
124 rte_jobstats_context_finish(struct rte_jobstats_context *ctx)
128 if (likely(ctx->loop_executed_jobs))
132 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
133 ctx->state_time = now;
137 rte_jobstats_context_reset(struct rte_jobstats_context *ctx)
139 RESET_TIME_MIN_MAX(ctx, exec);
140 RESET_TIME_MIN_MAX(ctx, management);
141 ctx->start_time = get_time();
142 ctx->state_time = ctx->start_time;
143 ctx->job_exec_cnt = 0;
148 rte_jobstats_set_target(struct rte_jobstats *job, int64_t target)
150 job->target = target;
154 rte_jobstats_start(struct rte_jobstats_context *ctx, struct rte_jobstats *job)
158 /* Some sanity check. */
159 if (unlikely(ctx == NULL || job == NULL || job->context != NULL))
162 /* Link job with context object. */
166 ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
167 ctx->state_time = now;
173 rte_jobstats_abort(struct rte_jobstats *job)
175 struct rte_jobstats_context *ctx;
176 uint64_t now, exec_time;
178 /* Some sanity check. */
179 if (unlikely(job == NULL || job->context == NULL))
184 exec_time = now - ctx->state_time;
185 ADD_TIME_MIN_MAX(ctx, management, exec_time);
186 ctx->state_time = now;
193 rte_jobstats_finish(struct rte_jobstats *job, int64_t job_value)
195 struct rte_jobstats_context *ctx;
196 uint64_t now, exec_time;
199 /* Some sanity check. */
200 if (unlikely(job == NULL || job->context == NULL))
203 need_update = job->target != job_value;
204 /* Adjust period only if job is unhappy of its current period. */
206 (*job->update_period_cb)(job, job_value);
210 /* Update execution time is considered as runtime so get time after it is
213 exec_time = now - ctx->state_time;
214 ADD_TIME_MIN_MAX(job, exec, exec_time);
215 ADD_TIME_MIN_MAX(ctx, exec, exec_time);
217 ctx->state_time = now;
219 ctx->loop_executed_jobs++;
229 rte_jobstats_set_period(struct rte_jobstats *job, uint64_t period,
233 if (period < job->min_period)
234 period = job->min_period;
235 else if (period > job->max_period)
236 period = job->max_period;
239 job->period = period;
243 rte_jobstats_set_min(struct rte_jobstats *job, uint64_t period)
245 job->min_period = period;
246 if (job->period < period)
247 job->period = period;
251 rte_jobstats_set_max(struct rte_jobstats *job, uint64_t period)
253 job->max_period = period;
254 if (job->period > period)
255 job->period = period;
259 rte_jobstats_init(struct rte_jobstats *job, const char *name,
260 uint64_t min_period, uint64_t max_period, uint64_t initial_period,
266 job->period = initial_period;
267 job->min_period = min_period;
268 job->max_period = max_period;
269 job->target = target;
270 job->update_period_cb = &default_update_function;
271 rte_jobstats_reset(job);
272 snprintf(job->name, RTE_DIM(job->name), "%s", name == NULL ? "" : name);
279 rte_jobstats_set_update_period_function(struct rte_jobstats *job,
280 rte_job_update_period_cb_t update_period_cb)
282 if (update_period_cb == NULL)
283 update_period_cb = default_update_function;
285 job->update_period_cb = update_period_cb;
289 rte_jobstats_reset(struct rte_jobstats *job)
291 RESET_TIME_MIN_MAX(job, exec);