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