1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 
9 #include <rte_string_fns.h>
10 #include <rte_errno.h>
11 #include <rte_common.h>
12 #include <rte_eal.h>
13 #include <rte_log.h>
14 #include <rte_cycles.h>
15 #include <rte_branch_prediction.h>
16 
17 #include "rte_jobstats.h"
18 
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;          \
26 } while (0)
27 
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;                \
32 } while (0)
33 
34 static inline uint64_t
get_time(void)35 get_time(void)
36 {
37 	rte_rmb();
38 	return rte_get_timer_cycles();
39 }
40 
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.
44  */
45 #define JOB_UPDATE_STEP_UP    1
46 #define JOB_UPDATE_STEP_DOWN  4
47 
48 /*
49  * Default update function that implements simple period adjustment.
50  */
51 static void
default_update_function(struct rte_jobstats * job,int64_t result)52 default_update_function(struct rte_jobstats *job, int64_t result)
53 {
54 	int64_t err = job->target - result;
55 
56 	/* Job is happy. Nothing to do */
57 	if (err == 0)
58 		return;
59 
60 	if (err > 0) {
61 		if (job->period + JOB_UPDATE_STEP_UP < job->max_period)
62 			job->period += JOB_UPDATE_STEP_UP;
63 	} else {
64 		if (job->min_period + JOB_UPDATE_STEP_DOWN < job->period)
65 			job->period -= JOB_UPDATE_STEP_DOWN;
66 	}
67 }
68 
69 int
rte_jobstats_context_init(struct rte_jobstats_context * ctx)70 rte_jobstats_context_init(struct rte_jobstats_context *ctx)
71 {
72 	if (ctx == NULL)
73 		return -EINVAL;
74 
75 	/* Init only needed parameters. Zero out everything else. */
76 	memset(ctx, 0, sizeof(struct rte_jobstats_context));
77 
78 	rte_jobstats_context_reset(ctx);
79 
80 	return 0;
81 }
82 
83 void
rte_jobstats_context_start(struct rte_jobstats_context * ctx)84 rte_jobstats_context_start(struct rte_jobstats_context *ctx)
85 {
86 	uint64_t now;
87 
88 	ctx->loop_executed_jobs = 0;
89 
90 	now = get_time();
91 	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
92 	ctx->state_time = now;
93 }
94 
95 void
rte_jobstats_context_finish(struct rte_jobstats_context * ctx)96 rte_jobstats_context_finish(struct rte_jobstats_context *ctx)
97 {
98 	uint64_t now;
99 
100 	if (likely(ctx->loop_executed_jobs))
101 		ctx->loop_cnt++;
102 
103 	now = get_time();
104 	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
105 	ctx->state_time = now;
106 }
107 
108 void
rte_jobstats_context_reset(struct rte_jobstats_context * ctx)109 rte_jobstats_context_reset(struct rte_jobstats_context *ctx)
110 {
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;
116 	ctx->loop_cnt = 0;
117 }
118 
119 void
rte_jobstats_set_target(struct rte_jobstats * job,int64_t target)120 rte_jobstats_set_target(struct rte_jobstats *job, int64_t target)
121 {
122 	job->target = target;
123 }
124 
125 int
rte_jobstats_start(struct rte_jobstats_context * ctx,struct rte_jobstats * job)126 rte_jobstats_start(struct rte_jobstats_context *ctx, struct rte_jobstats *job)
127 {
128 	uint64_t now;
129 
130 	/* Some sanity check. */
131 	if (unlikely(ctx == NULL || job == NULL || job->context != NULL))
132 		return -EINVAL;
133 
134 	/* Link job with context object. */
135 	job->context = ctx;
136 
137 	now = get_time();
138 	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
139 	ctx->state_time = now;
140 
141 	return 0;
142 }
143 
144 int
rte_jobstats_abort(struct rte_jobstats * job)145 rte_jobstats_abort(struct rte_jobstats *job)
146 {
147 	struct rte_jobstats_context *ctx;
148 	uint64_t now, exec_time;
149 
150 	/* Some sanity check. */
151 	if (unlikely(job == NULL || job->context == NULL))
152 		return -EINVAL;
153 
154 	ctx = job->context;
155 	now = get_time();
156 	exec_time = now - ctx->state_time;
157 	ADD_TIME_MIN_MAX(ctx, management, exec_time);
158 	ctx->state_time = now;
159 	job->context = NULL;
160 
161 	return 0;
162 }
163 
164 int
rte_jobstats_finish(struct rte_jobstats * job,int64_t job_value)165 rte_jobstats_finish(struct rte_jobstats *job, int64_t job_value)
166 {
167 	struct rte_jobstats_context *ctx;
168 	uint64_t now, exec_time;
169 	int need_update;
170 
171 	/* Some sanity check. */
172 	if (unlikely(job == NULL || job->context == NULL))
173 		return -EINVAL;
174 
175 	need_update = job->target != job_value;
176 	/* Adjust period only if job is unhappy of its current period. */
177 	if (need_update)
178 		(*job->update_period_cb)(job, job_value);
179 
180 	ctx = job->context;
181 
182 	/* Update execution time is considered as runtime so get time after it is
183 	 * executed. */
184 	now = get_time();
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);
188 
189 	ctx->state_time = now;
190 
191 	ctx->loop_executed_jobs++;
192 	ctx->job_exec_cnt++;
193 
194 	job->exec_cnt++;
195 	job->context = NULL;
196 
197 	return need_update;
198 }
199 
200 void
rte_jobstats_set_period(struct rte_jobstats * job,uint64_t period,uint8_t saturate)201 rte_jobstats_set_period(struct rte_jobstats *job, uint64_t period,
202 		uint8_t saturate)
203 {
204 	if (saturate != 0) {
205 		if (period < job->min_period)
206 			period = job->min_period;
207 		else if (period > job->max_period)
208 			period = job->max_period;
209 	}
210 
211 	job->period = period;
212 }
213 
214 void
rte_jobstats_set_min(struct rte_jobstats * job,uint64_t period)215 rte_jobstats_set_min(struct rte_jobstats *job, uint64_t period)
216 {
217 	job->min_period = period;
218 	if (job->period < period)
219 		job->period = period;
220 }
221 
222 void
rte_jobstats_set_max(struct rte_jobstats * job,uint64_t period)223 rte_jobstats_set_max(struct rte_jobstats *job, uint64_t period)
224 {
225 	job->max_period = period;
226 	if (job->period > period)
227 		job->period = period;
228 }
229 
230 int
rte_jobstats_init(struct rte_jobstats * job,const char * name,uint64_t min_period,uint64_t max_period,uint64_t initial_period,int64_t target)231 rte_jobstats_init(struct rte_jobstats *job, const char *name,
232 		uint64_t min_period, uint64_t max_period, uint64_t initial_period,
233 		int64_t target)
234 {
235 	if (job == NULL)
236 		return -EINVAL;
237 
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));
245 	job->context = NULL;
246 
247 	return 0;
248 }
249 
250 void
rte_jobstats_set_update_period_function(struct rte_jobstats * job,rte_job_update_period_cb_t update_period_cb)251 rte_jobstats_set_update_period_function(struct rte_jobstats *job,
252 		rte_job_update_period_cb_t update_period_cb)
253 {
254 	if (update_period_cb == NULL)
255 		update_period_cb = default_update_function;
256 
257 	job->update_period_cb = update_period_cb;
258 }
259 
260 void
rte_jobstats_reset(struct rte_jobstats * job)261 rte_jobstats_reset(struct rte_jobstats *job)
262 {
263 	RESET_TIME_MIN_MAX(job, exec);
264 	job->exec_cnt = 0;
265 }
266