summaryrefslogtreecommitdiffstats
path: root/lib/librte_jobstats/rte_jobstats.c
blob: b64bc53bb3052a57055d451edb7f0a083e730a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2015 Intel Corporation
 */

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <rte_errno.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_log.h>
#include <rte_cycles.h>
#include <rte_branch_prediction.h>

#include "rte_jobstats.h"

#define ADD_TIME_MIN_MAX(obj, type, value) do {      \
	typeof(value) tmp = (value);                     \
	(obj)->type ## _time += tmp;                     \
	if (tmp < (obj)->min_ ## type ## _time)          \
		(obj)->min_ ## type ## _time = tmp;          \
	if (tmp > (obj)->max_ ## type ## _time)          \
		(obj)->max_ ## type ## _time = tmp;          \
} while (0)

#define RESET_TIME_MIN_MAX(obj, type) do {           \
	(obj)->type ## _time = 0;                        \
	(obj)->min_ ## type ## _time = UINT64_MAX;       \
	(obj)->max_ ## type ## _time = 0;                \
} while (0)

static inline uint64_t
get_time(void)
{
	rte_rmb();
	return rte_get_timer_cycles();
}

/* Those are steps used to adjust job period.
 * Experiments show that for forwarding apps the up step must be less than down
 * step to achieve optimal performance.
 */
#define JOB_UPDATE_STEP_UP    1
#define JOB_UPDATE_STEP_DOWN  4

/*
 * Default update function that implements simple period adjustment.
 */
static void
default_update_function(struct rte_jobstats *job, int64_t result)
{
	int64_t err = job->target - result;

	/* Job is happy. Nothing to do */
	if (err == 0)
		return;

	if (err > 0) {
		if (job->period + JOB_UPDATE_STEP_UP < job->max_period)
			job->period += JOB_UPDATE_STEP_UP;
	} else {
		if (job->min_period + JOB_UPDATE_STEP_DOWN < job->period)
			job->period -= JOB_UPDATE_STEP_DOWN;
	}
}

int
rte_jobstats_context_init(struct rte_jobstats_context *ctx)
{
	if (ctx == NULL)
		return -EINVAL;

	/* Init only needed parameters. Zero out everything else. */
	memset(ctx, 0, sizeof(struct rte_jobstats_context));

	rte_jobstats_context_reset(ctx);

	return 0;
}

void
rte_jobstats_context_start(struct rte_jobstats_context *ctx)
{
	uint64_t now;

	ctx->loop_executed_jobs = 0;

	now = get_time();
	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
	ctx->state_time = now;
}

void
rte_jobstats_context_finish(struct rte_jobstats_context *ctx)
{
	uint64_t now;

	if (likely(ctx->loop_executed_jobs))
		ctx->loop_cnt++;

	now = get_time();
	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
	ctx->state_time = now;
}

void
rte_jobstats_context_reset(struct rte_jobstats_context *ctx)
{
	RESET_TIME_MIN_MAX(ctx, exec);
	RESET_TIME_MIN_MAX(ctx, management);
	ctx->start_time = get_time();
	ctx->state_time = ctx->start_time;
	ctx->job_exec_cnt = 0;
	ctx->loop_cnt = 0;
}

void
rte_jobstats_set_target(struct rte_jobstats *job, int64_t target)
{
	job->target = target;
}

int
rte_jobstats_start(struct rte_jobstats_context *ctx, struct rte_jobstats *job)
{
	uint64_t now;

	/* Some sanity check. */
	if (unlikely(ctx == NULL || job == NULL || job->context != NULL))
		return -EINVAL;

	/* Link job with context object. */
	job->context = ctx;

	now = get_time();
	ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
	ctx->state_time = now;

	return 0;
}

int
rte_jobstats_abort(struct rte_jobstats *job)
{
	struct rte_jobstats_context *ctx;
	uint64_t now, exec_time;

	/* Some sanity check. */
	if (unlikely(job == NULL || job->context == NULL))
		return -EINVAL;

	ctx = job->context;
	now = get_time();
	exec_time = now - ctx->state_time;
	ADD_TIME_MIN_MAX(ctx, management, exec_time);
	ctx->state_time = now;
	job->context = NULL;

	return 0;
}

int
rte_jobstats_finish(struct rte_jobstats *job, int64_t job_value)
{
	struct rte_jobstats_context *ctx;
	uint64_t now, exec_time;
	int need_update;

	/* Some sanity check. */
	if (unlikely(job == NULL || job->context == NULL))
		return -EINVAL;

	need_update = job->target != job_value;
	/* Adjust period only if job is unhappy of its current period. */
	if (need_update)
		(*job->update_period_cb)(job, job_value);

	ctx = job->context;

	/* Update execution time is considered as runtime so get time after it is
	 * executed. */
	now = get_time();
	exec_time = now - ctx->state_time;
	ADD_TIME_MIN_MAX(job, exec, exec_time);
	ADD_TIME_MIN_MAX(ctx, exec, exec_time);

	ctx->state_time = now;

	ctx->loop_executed_jobs++;
	ctx->job_exec_cnt++;

	job->exec_cnt++;
	job->context = NULL;

	return need_update;
}

void
rte_jobstats_set_period(struct rte_jobstats *job, uint64_t period,
		uint8_t saturate)
{
	if (saturate != 0) {
		if (period < job->min_period)
			period = job->min_period;
		else if (period > job->max_period)
			period = job->max_period;
	}

	job->period = period;
}

void
rte_jobstats_set_min(struct rte_jobstats *job, uint64_t period)
{
	job->min_period = period;
	if (job->period < period)
		job->period = period;
}

void
rte_jobstats_set_max(struct rte_jobstats *job, uint64_t period)
{
	job->max_period = period;
	if (job->period > period)
		job->period = period;
}

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)
{
	if (job == NULL)
		return -EINVAL;

	job->period = initial_period;
	job->min_period = min_period;
	job->max_period = max_period;
	job->target = target;
	job->update_period_cb = &default_update_function;
	rte_jobstats_reset(job);
	snprintf(job->name, RTE_DIM(job->name), "%s", name == NULL ? "" : name);
	job->context = NULL;

	return 0;
}

void
rte_jobstats_set_update_period_function(struct rte_jobstats *job,
		rte_job_update_period_cb_t update_period_cb)
{
	if (update_period_cb == NULL)
		update_period_cb = default_update_function;

	job->update_period_cb = update_period_cb;
}

void
rte_jobstats_reset(struct rte_jobstats *job)
{
	RESET_TIME_MIN_MAX(job, exec);
	job->exec_cnt = 0;
}