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