forked from intel/intel-cmt-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmba_sc.c
494 lines (415 loc) · 13.5 KB
/
mba_sc.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* BSD LICENSE
*
* Copyright(c) 2018-2023 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mba_sc.h"
#include "common.h"
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
static const struct pqos_cap *m_cap;
static const struct pqos_cpuinfo *m_cpu;
static const struct pqos_capability *m_cap_mba;
static const struct pqos_capability *m_cap_mon;
struct mba_sc_state {
struct pqos_mon_data group;
cpu_set_t cpumask;
unsigned prev_rate;
uint64_t prev_time;
uint64_t max_bw;
uint64_t prev_bw;
unsigned delta_comp;
uint64_t delta_bw;
uint64_t reg_start_time;
};
static struct mba_sc_state *state = NULL;
static unsigned state_num;
static int supported = 0;
/**
* @brief Start LMBM monitoring
*
* @param[in] cpumask cores to monitor
* @param[out] group monitoring group pointer
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
mba_sc_mon_start(const cpu_set_t cpumask, struct pqos_mon_data *group)
{
unsigned *cores = NULL;
unsigned num_cores = 0;
int i;
int ret;
cores = malloc(CPU_SETSIZE * sizeof(unsigned));
if (cores == NULL) {
DBG("MBA SC: memory allocation failed\n");
return -EFAULT;
}
for (i = 0; i < CPU_SETSIZE; i++) {
if (CPU_ISSET(i, &cpumask) != 1)
continue;
cores[num_cores++] = i;
}
ret = pqos_mon_start(num_cores, cores, PQOS_MON_EVENT_LMEM_BW, NULL,
group);
if (ret != PQOS_RETVAL_OK)
ret = -EFAULT;
if (cores != NULL)
free(cores);
return ret;
}
/**
* @brief Stop LMBM monitoring
*
* @param group monitoring group pointer
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
mba_sc_mon_stop(struct pqos_mon_data *group)
{
int ret;
ret = pqos_mon_stop(group);
if (ret != PQOS_RETVAL_OK)
return -EFAULT;
return 0;
}
/**
* @brief Poll mon values
*
* @param group monitoring group
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
mba_sc_mon_poll(struct pqos_mon_data *group)
{
int ret;
ret = pqos_mon_poll(&group, 1);
if (ret != PQOS_RETVAL_OK)
return -EFAULT;
return 0;
}
int
mba_sc_init(void)
{
int ret = 0;
const struct pqos_monitor *cap_lmbm;
if (m_cap != NULL || m_cpu != NULL) {
DBG("MBA SC: module already initialized!\n");
ret = -EEXIST;
goto err;
}
if (g_cfg.interface != PQOS_INTER_MSR) {
DBG("MBA SC: Supported only for MSR interface\n");
ret = -EFAULT;
goto err;
}
/* Get capability and CPU info pointer */
ret = pqos_cap_get(&m_cap, &m_cpu);
if (ret != PQOS_RETVAL_OK) {
DBG("MBA SC: Error retrieving PQoS capabilities!\n");
ret = -EFAULT;
goto err;
}
/* Get MBA capabilities */
ret = pqos_cap_get_type(m_cap, PQOS_CAP_TYPE_MBA, &m_cap_mba);
if (ret != PQOS_RETVAL_OK || !m_cap_mba->u.mba->is_linear) {
DBG("MBA SC: MBA not supported or not linear.\n");
ret = -EFAULT;
goto err;
}
/* Get mon capabilities */
ret = pqos_cap_get_type(m_cap, PQOS_CAP_TYPE_MON, &m_cap_mon);
if (ret != PQOS_RETVAL_OK) {
DBG("MBA SC: monitoring not supported.\n");
ret = -EFAULT;
goto err;
}
/* Check if LMBM monitoring is supported */
ret = pqos_cap_get_event(m_cap, PQOS_MON_EVENT_LMEM_BW, &cap_lmbm);
if (ret == PQOS_RETVAL_OK && cap_lmbm != NULL)
supported = 1;
else {
DBG("MBA SC: local BW monitoring not supported.\n");
ret = -EFAULT;
goto err;
}
supported = 1;
return 0;
err:
/* deallocate all the resources */
mba_sc_fini();
return ret;
}
void
mba_sc_fini(void)
{
if (m_cap == NULL && m_cpu == NULL)
return;
m_cap = NULL;
m_cpu = NULL;
m_cap_mba = NULL;
}
static int
mba_sc_stop(void)
{
unsigned i;
int ret = 0;
if (state == NULL)
return 0;
for (i = 0; i < state_num; i++) {
int retval = mba_sc_mon_stop(&state[i].group);
if (retval < 0)
ret = retval;
}
free(state);
state = NULL;
return ret;
}
void
mba_sc_exit(void)
{
mba_sc_stop();
}
/**
* @brief Check if child process is still running
*
* @param[in] pid Child pid
*
* @return status
*/
static int
mba_sc_running(const pid_t pid)
{
int status;
int ret;
if (pid != -1) {
ret = waitpid(pid, &status, WNOHANG);
if (ret == 0)
return 1;
if (ret == pid &&
(!WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS))
exit(EXIT_FAILURE);
} else if (!g_cfg.command) {
unsigned i;
/* Send sig-null to check if pid is still running */
for (i = 0; i < g_cfg.pid_count; i++) {
ret = kill(g_cfg.pids[i], 0);
if (ret == 0)
return 1;
}
}
return 0;
}
int
mba_sc_mode(const struct rdtset *cfg)
{
unsigned i;
if (cfg->interface != PQOS_INTER_MSR)
return 0;
for (i = 0; i < cfg->config_count; i++)
if (cfg->config[i].mba.ctrl == 1)
return 1;
return 0;
}
/**
* @brief Sets MBA class of service defined by \a mba_cfg on cores in \a cpumask
*
* @param [in] cpumask set of lcores
* @param [in] mba_cfg mba configuration to be set
*
* @return status
*/
static int
mba_sc_mba_set(const cpu_set_t cpumask, struct pqos_mba *mba_cfg)
{
int ret;
int lcore;
unsigned cluster_id = 0;
unsigned cluster_array[CPU_SETSIZE] = {0};
for (lcore = 0; lcore < CPU_SETSIZE; lcore++) {
if (CPU_ISSET(lcore, &cpumask) != 1)
continue;
ret = pqos_cpu_get_clusterid(m_cpu, lcore, &cluster_id);
if (ret != PQOS_RETVAL_OK) {
DBG("MBA SC: error while reading cluster id "
"for lcore %d\n",
lcore);
return -EFAULT;
}
if (cluster_array[cluster_id])
continue;
ret = pqos_alloc_assoc_get(lcore, &mba_cfg->class_id);
if (ret != PQOS_RETVAL_OK) {
DBG("MBA SC: error while reading assoc for lcore %d\n",
lcore);
return -EFAULT;
}
ret = pqos_mba_set(cluster_id, 1, mba_cfg, NULL);
if (ret != PQOS_RETVAL_OK) {
DBG("MBA SC: error while setting mba for cluster %u\n",
cluster_id);
return -EFAULT;
}
cluster_array[cluster_id]++;
}
return 0;
}
/**
* @brief Get number of MBA SC instances
*
* @param[in] cfg rdtset configuration
*
* @return Number of MBA SC instances
*/
static unsigned
mba_sc_count(const struct rdtset *cfg)
{
unsigned i;
unsigned count = 0;
for (i = 0; i < cfg->config_count; i++)
if (cfg->config[i].mba.ctrl == 1)
count++;
return count;
}
static int
mba_sc_update(struct mba_sc_state *state)
{
uint64_t cur_time;
uint64_t delta_time;
struct pqos_mba mba_cfg;
uint64_t prev_bw = state->prev_bw;
uint64_t cur_bw;
int ret;
const struct pqos_event_values *pv = &state->group.values;
const unsigned min_rate = m_cap_mba->u.mba->throttle_step;
const unsigned step_rate = m_cap_mba->u.mba->throttle_step;
const unsigned max_rate = 100;
mba_cfg.ctrl = 0;
ret = mba_sc_mon_poll(&state->group);
if (ret != 0)
return ret;
cur_time = get_time_usec();
delta_time = cur_time - state->prev_time;
state->prev_time = cur_time;
/* calculate bw in bytes per second */
cur_bw = pv->mbm_local_delta * 1000000 / delta_time;
state->prev_bw = cur_bw;
if (state->delta_comp) {
state->delta_comp = 0;
if (cur_bw >= prev_bw)
state->delta_bw = cur_bw - prev_bw;
else
state->delta_bw = prev_bw - cur_bw;
}
DBG("MBA SC: Current BW %lluMBps",
(unsigned long long)bytes_to_mb(cur_bw));
if (state->prev_rate > min_rate && cur_bw > state->max_bw) {
DBG(" > %lluMBps",
(unsigned long long)bytes_to_mb(state->max_bw));
mba_cfg.mb_max = state->prev_rate - step_rate;
} else if (state->prev_rate < max_rate &&
(cur_bw + state->delta_bw) < state->max_bw) {
DBG(" < %lluMBps",
(unsigned long long)bytes_to_mb(state->max_bw));
mba_cfg.mb_max = state->prev_rate + step_rate;
} else {
if (state->reg_start_time) {
DBG(" Max BW %lluMBps, regulation took %.1fs\n",
(unsigned long long)bytes_to_mb(state->max_bw),
(cur_time - state->reg_start_time) / 1000000.0);
state->reg_start_time = 0;
} else
DBG("\n");
return 0;
}
DBG(", setting MBA to %u%%\n", mba_cfg.mb_max);
ret = mba_sc_mba_set(state->cpumask, &mba_cfg);
if (ret != 0) {
DBG(" Failed to update mba rate!\n");
return ret;
}
state->prev_rate = mba_cfg.mb_max;
state->delta_comp = 1;
if (!state->reg_start_time)
state->reg_start_time = get_time_usec();
return 0;
}
int
mba_sc_main(pid_t pid)
{
int ret;
unsigned i;
unsigned index;
if (!supported)
return PQOS_RETVAL_RESOURCE;
/* allocate memory for state struct */
state_num = mba_sc_count(&g_cfg);
state = calloc(state_num, sizeof(*state));
if (state == NULL) {
DBG("MBA SC: memory allocation failed\n");
return -EFAULT;
}
for (i = 0, index = 0; i < g_cfg.config_count; i++) {
const struct rdt_config *config = &g_cfg.config[i];
if (config->mba.ctrl == 1) {
ret = mba_sc_mon_start(config->cpumask,
&state[index].group);
if (ret != 0) {
DBG("MBA SC: failed to start monitoring\n");
goto err;
}
state[index].max_bw = mb_to_bytes(config->mba.mb_max);
state[index].prev_rate = MBA_SC_DEF_INIT_MBA;
state[index].cpumask = config->cpumask;
index++;
}
}
for (i = 0; i < state_num; i++)
state[i].prev_time = get_time_usec();
while (mba_sc_running(pid)) {
usleep(MBA_SC_SAMPLING_INTERVAL * 1000);
for (i = 0; i < state_num; i++)
mba_sc_update(&state[i]);
}
err:
ret = mba_sc_stop();
return ret;
}