ixgbe: get queue info and descriptor limits
[dpdk.git] / lib / librte_jobstats / rte_jobstats.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37
38 #include <rte_errno.h>
39 #include <rte_common.h>
40 #include <rte_eal.h>
41 #include <rte_log.h>
42 #include <rte_cycles.h>
43 #include <rte_branch_prediction.h>
44
45 #include "rte_jobstats.h"
46
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;          \
54 } while (0)
55
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;                \
60 } while (0)
61
62 static inline uint64_t
63 get_time(void)
64 {
65         rte_rmb();
66         return rte_get_timer_cycles();
67 }
68
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.
72  */
73 #define JOB_UPDATE_STEP_UP    1
74 #define JOB_UPDATE_STEP_DOWN  4
75
76 /*
77  * Default update function that implements simple period adjustment.
78  */
79 static void
80 default_update_function(struct rte_jobstats *job, int64_t result)
81 {
82         int64_t err = job->target - result;
83
84         /* Job is happy. Nothing to do */
85         if (err == 0)
86                 return;
87
88         if (err > 0) {
89                 if (job->period + JOB_UPDATE_STEP_UP < job->max_period)
90                         job->period += JOB_UPDATE_STEP_UP;
91         } else {
92                 if (job->min_period + JOB_UPDATE_STEP_DOWN < job->period)
93                         job->period -= JOB_UPDATE_STEP_DOWN;
94         }
95 }
96
97 int
98 rte_jobstats_context_init(struct rte_jobstats_context *ctx)
99 {
100         if (ctx == NULL)
101                 return -EINVAL;
102
103         /* Init only needed parameters. Zero out everything else. */
104         memset(ctx, 0, sizeof(struct rte_jobstats_context));
105
106         rte_jobstats_context_reset(ctx);
107
108         return 0;
109 }
110
111 void
112 rte_jobstats_context_start(struct rte_jobstats_context *ctx)
113 {
114         uint64_t now;
115
116         ctx->loop_executed_jobs = 0;
117
118         now = get_time();
119         ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
120         ctx->state_time = now;
121 }
122
123 void
124 rte_jobstats_context_finish(struct rte_jobstats_context *ctx)
125 {
126         uint64_t now;
127
128         if (likely(ctx->loop_executed_jobs))
129                 ctx->loop_cnt++;
130
131         now = get_time();
132         ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
133         ctx->state_time = now;
134 }
135
136 void
137 rte_jobstats_context_reset(struct rte_jobstats_context *ctx)
138 {
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;
144         ctx->loop_cnt = 0;
145 }
146
147 void
148 rte_jobstats_set_target(struct rte_jobstats *job, int64_t target)
149 {
150         job->target = target;
151 }
152
153 int
154 rte_jobstats_start(struct rte_jobstats_context *ctx, struct rte_jobstats *job)
155 {
156         uint64_t now;
157
158         /* Some sanity check. */
159         if (unlikely(ctx == NULL || job == NULL || job->context != NULL))
160                 return -EINVAL;
161
162         /* Link job with context object. */
163         job->context = ctx;
164
165         now = get_time();
166         ADD_TIME_MIN_MAX(ctx, management, now - ctx->state_time);
167         ctx->state_time = now;
168
169         return 0;
170 }
171
172 int
173 rte_jobstats_finish(struct rte_jobstats *job, int64_t job_value)
174 {
175         struct rte_jobstats_context *ctx;
176         uint64_t now, exec_time;
177         int need_update;
178
179         /* Some sanity check. */
180         if (unlikely(job == NULL || job->context == NULL))
181                 return -EINVAL;
182
183         need_update = job->target != job_value;
184         /* Adjust period only if job is unhappy of its current period. */
185         if (need_update)
186                 (*job->update_period_cb)(job, job_value);
187
188         ctx = job->context;
189
190         /* Update execution time is considered as runtime so get time after it is
191          * executed. */
192         now = get_time();
193         exec_time = now - ctx->state_time;
194         ADD_TIME_MIN_MAX(job, exec, exec_time);
195         ADD_TIME_MIN_MAX(ctx, exec, exec_time);
196
197         ctx->state_time = now;
198
199         ctx->loop_executed_jobs++;
200         ctx->job_exec_cnt++;
201
202         job->exec_cnt++;
203         job->context = NULL;
204
205         return need_update;
206 }
207
208 void
209 rte_jobstats_set_period(struct rte_jobstats *job, uint64_t period,
210                 uint8_t saturate)
211 {
212         if (saturate != 0) {
213                 if (period < job->min_period)
214                         period = job->min_period;
215                 else if (period > job->max_period)
216                         period = job->max_period;
217         }
218
219         job->period = period;
220 }
221
222 void
223 rte_jobstats_set_min(struct rte_jobstats *job, uint64_t period)
224 {
225         job->min_period = period;
226         if (job->period < period)
227                 job->period = period;
228 }
229
230 void
231 rte_jobstats_set_max(struct rte_jobstats *job, uint64_t period)
232 {
233         job->max_period = period;
234         if (job->period > period)
235                 job->period = period;
236 }
237
238 int
239 rte_jobstats_init(struct rte_jobstats *job, const char *name,
240                 uint64_t min_period, uint64_t max_period, uint64_t initial_period,
241                 int64_t target)
242 {
243         if (job == NULL)
244                 return -EINVAL;
245
246         job->period = initial_period;
247         job->min_period = min_period;
248         job->max_period = max_period;
249         job->target = target;
250         job->update_period_cb = &default_update_function;
251         rte_jobstats_reset(job);
252         snprintf(job->name, RTE_DIM(job->name), "%s", name == NULL ? "" : name);
253         job->context = NULL;
254
255         return 0;
256 }
257
258 void
259 rte_jobstats_set_update_period_function(struct rte_jobstats *job,
260                 rte_job_update_period_cb_t update_period_cb)
261 {
262         if (update_period_cb == NULL)
263                 update_period_cb = default_update_function;
264
265         job->update_period_cb = update_period_cb;
266 }
267
268 void
269 rte_jobstats_reset(struct rte_jobstats *job)
270 {
271         RESET_TIME_MIN_MAX(job, exec);
272         job->exec_cnt = 0;
273 }