forked from intel/intel-cmt-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.c
More file actions
672 lines (595 loc) · 22.1 KB
/
Copy pathalloc.c
File metadata and controls
672 lines (595 loc) · 22.1 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
* BSD LICENSE
*
* Copyright(c) 2014-2015 Intel Corporation. All rights reserved.
* 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.
*/
/**
* @brief Platform QoS utility - allocation module
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pqos.h"
#include "main.h"
#include "alloc.h"
/**
* Defines used to identify CAT mask definitions
*/
#define CAT_UPDATE_SCOPE_BOTH 0 /**< update COS code & data masks */
#define CAT_UPDATE_SCOPE_DATA 1 /**< update COS data mask */
#define CAT_UPDATE_SCOPE_CODE 2 /**< update COS code mask */
/**
* Number of classes of service (COS) selected for modification
* (L3 cache allocation)
*/
static int sel_l3ca_cos_num = 0;
/**
* New settings for L3 cache
* allocation classes of service
*/
struct l3ca_cos_sock {
unsigned cos_num;
struct pqos_l3ca cos_tab[PQOS_MAX_L3CA_COS];
};
/**
* Table of COS settings for selected sockets
*/
static struct l3ca_cos_sock sel_l3ca_cos_tab[PQOS_MAX_SOCKETS];
/**
* COS settings for all sockets
*/
static struct l3ca_cos_sock sel_l3ca_cos_all;
/**
* Number of core to COS associations to be done
*/
static int sel_l3ca_assoc_num = 0;
/**
* Core to L3 COS associations details
*/
static struct {
unsigned core;
unsigned class_id;
} sel_l3ca_assoc_tab[PQOS_MAX_CORES];
/**
* @brief Sets up allocation classes of service on selected CPU sockets
*
* @param sock_count number of CPU sockets
* @param sockets arrays with CPU socket id's
*
* @return Number of classes of service set
* @retval 0 no class of service set, no class selected for change
* @retval negative error
* @retval positive success
*/
static int
set_allocation_class(const unsigned sock_count, const unsigned *sockets)
{
unsigned i, set = 0;
/**
* Check if selected sockets exist on the system
*/
for (i = 0; i < DIM(sel_l3ca_cos_tab); i++) {
unsigned j;
if (sel_l3ca_cos_tab[i].cos_num == 0)
continue;
for (j = 0; j < sock_count; j++)
if (sockets[j] == i)
break;
if (j == sock_count) {
printf("Socket %u not detected!\n", i);
return -1;
}
}
/**
* Set selected classes of service
*/
for (i = 0; i < sock_count; i++) {
unsigned s = sockets[i];
int ret;
if (s >= DIM(sel_l3ca_cos_tab))
continue;
/**
* Set COS that apply to all sockets
*/
if (sel_l3ca_cos_all.cos_num) {
ret = pqos_l3ca_set(s, sel_l3ca_cos_all.cos_num,
sel_l3ca_cos_all.cos_tab);
ASSERT(ret == PQOS_RETVAL_OK);
if (ret != PQOS_RETVAL_OK)
goto set_cos_failure;
set += sel_l3ca_cos_all.cos_num;
}
/**
* Set COS that apply to this socket
*/
if (sel_l3ca_cos_tab[s].cos_num) {
ret = pqos_l3ca_set(s, sel_l3ca_cos_tab[s].cos_num,
sel_l3ca_cos_tab[s].cos_tab);
ASSERT(ret == PQOS_RETVAL_OK);
if (ret != PQOS_RETVAL_OK)
goto set_cos_failure;
set += sel_l3ca_cos_tab[s].cos_num;
}
}
return set;
set_cos_failure:
printf("Setting up cache allocation "
"class of service failed!\n");
return -1;
}
/**
* @brief Converts string describing CAT COS into ID and mask scope
*
* Current string format is: <ID>[CcDd]
*
* Some examples:
* 1d - class 1, data mask
* 5C - class 5, code mask
* 0 - class 0, common mask for code & data
*
* @param [in] str string describing CAT COS. Function may modify
* the last character of the string in some conditions.
* @param [out] scope indicates if string \a str refers to both COS masks
* or just one of them
* @param [out] id class ID referred to in the string \a str
*/
static void
parse_cos_mask_type(char *str, int *scope, unsigned *id)
{
size_t len = 0;
ASSERT(str != NULL);
ASSERT(scope != NULL);
ASSERT(id != NULL);
len = strlen(str);
if (len > 1 && (str[len - 1] == 'c' || str[len - 1] == 'C')) {
*scope = CAT_UPDATE_SCOPE_CODE;
str[len - 1] = '\0';
} else if (len > 1 && (str[len - 1] == 'd' || str[len - 1] == 'D')) {
*scope = CAT_UPDATE_SCOPE_DATA;
str[len - 1] = '\0';
} else {
*scope = CAT_UPDATE_SCOPE_BOTH;
}
*id = (unsigned) strtouint64(str);
}
/**
* @brief Updates the COS table with specified COS for a socket
*
* @param sock pointer to socket COS structure to be updated
* @param id COS id to be added to the table
* @param mask CBM to be added to the table
* @param scope CDP config update scope
*/
static void
update_l3ca_cos_tab(struct l3ca_cos_sock *sock, const unsigned id,
const uint64_t mask, const int scope)
{
unsigned i;
for (i = 0; i < sock->cos_num; i++)
if (sock->cos_tab[i].class_id == id)
break;
if (i < sock->cos_num) {
/**
* This class is already on the list.
* Don't allow for mixing CDP and non-CDP formats.
*/
if ((sock->cos_tab[i].cdp && scope == CAT_UPDATE_SCOPE_BOTH) ||
(!sock->cos_tab[i].cdp && scope != CAT_UPDATE_SCOPE_BOTH)) {
printf("error: COS%u defined twice using CDP and "
"non-CDP format\n", id);
exit(EXIT_FAILURE);
}
switch (scope) {
case CAT_UPDATE_SCOPE_BOTH:
sock->cos_tab[i].ways_mask = mask;
break;
case CAT_UPDATE_SCOPE_CODE:
sock->cos_tab[i].code_mask = mask;
break;
case CAT_UPDATE_SCOPE_DATA:
sock->cos_tab[i].data_mask = mask;
break;
}
} else {
/**
* New class selected - extend the list
*/
unsigned k = sock->cos_num;
if (k >= PQOS_MAX_L3CA_COS) {
printf("too many allocation classes selected!\n");
exit(EXIT_FAILURE);
}
sock->cos_tab[k].class_id = id;
switch (scope) {
case CAT_UPDATE_SCOPE_BOTH:
sock->cos_tab[k].cdp = 0;
sock->cos_tab[k].ways_mask = mask;
break;
case CAT_UPDATE_SCOPE_CODE:
sock->cos_tab[k].cdp = 1;
sock->cos_tab[k].code_mask = mask;
/**
* This will result in error during set operation
* if data mask is not defined by the user.
*/
sock->cos_tab[k].data_mask = (uint64_t) (-1LL);
break;
case CAT_UPDATE_SCOPE_DATA:
sock->cos_tab[k].cdp = 1;
sock->cos_tab[k].data_mask = mask;
/**
* This will result in error during set operation
* if code mask is not defined by the user.
*/
sock->cos_tab[k].code_mask = (uint64_t) (-1LL);
break;
}
sock->cos_num++;
sel_l3ca_cos_num++;
}
}
/**
* @brief Verifies and translates definition of single allocation
* class of service from text string into internal configuration
* for specified sockets.
*
* @param str fragment of string passed to -e command line option
* @param sockets array of sockets to set COS tables
* @param sock_num number of sockets in array
*/
static void
parse_allocation_cos(char *str, const uint64_t *sockets,
const unsigned sock_num)
{
char *p = NULL;
uint64_t mask = 0;
unsigned i, class_id = 0;
int update_scope = CAT_UPDATE_SCOPE_BOTH;
p = strchr(str, '=');
if (p == NULL)
parse_error(str, "Invalid class of service definition");
*p = '\0';
parse_cos_mask_type(str, &update_scope, &class_id);
mask = strtouint64(p+1);
/**
* Update all sockets COS table
*/
if (sockets == NULL) {
update_l3ca_cos_tab(&sel_l3ca_cos_all, class_id,
mask, update_scope);
return;
}
/**
* Update COS tables
*/
for (i = 0; i < sock_num; i++) {
unsigned s = (unsigned) sockets[i];
/**
* Update specified socket COS table
*/
update_l3ca_cos_tab(&sel_l3ca_cos_tab[s], class_id,
mask, update_scope);
}
}
/**
* @brief Verifies and translates definition of allocation class of service
* from text string into internal configuration.
*
* @param str string passed to -e command line option
*/
static void
parse_allocation_class(char *str)
{
char *p = NULL;
char *saveptr = NULL;
uint64_t *sp = NULL;
uint64_t sockets[PQOS_MAX_SOCKETS];
unsigned i, n = 1;
/**
* Set up selected sockets table
*/
p = strchr(str, ':');
if (p == NULL)
parse_error(str, "Unrecognized allocation format");
if (strncasecmp(str, "llc@", 4) == 0) {
char *s = strdup(str);
/**
* Socket ID's selected
* - add selected sockets to the table
*/
if (s == NULL)
s = str;
*p = '\0';
str += strlen("llc@");
n = strlisttotab(str, sockets, DIM(sockets));
if (n == 0)
parse_error(s, "No socket ID specified");
/**
* Check selected sockets are within range
*/
for (i = 0; i < n; i++)
if (sockets[i] >= PQOS_MAX_SOCKETS)
parse_error(s, "Socket ID out of range");
sp = sockets;
free(s);
} else {
/**
* No sockets selected
* - apply COS to all sockets
*/
if (strncasecmp(str, "llc:", 4) != 0)
parse_error(str, "Unrecognized allocation type");
}
/**
* Parse COS masks and apply to selected sockets
*/
for (++p; ; p = NULL) {
char *token = NULL;
token = strtok_r(p, ",", &saveptr);
if (token == NULL)
break;
parse_allocation_cos(token, sp, n);
}
}
void selfn_allocation_class(const char *arg)
{
char *cp = NULL, *str = NULL;
char *saveptr = NULL;
if (arg == NULL)
parse_error(arg, "NULL pointer!");
if (strlen(arg) <= 0)
parse_error(arg, "Empty string!");
selfn_strdup(&cp, arg);
for (str = cp; ; str = NULL) {
char *token = NULL;
token = strtok_r(str, ";", &saveptr);
if (token == NULL)
break;
parse_allocation_class(token);
}
free(cp);
}
/**
* @brief Sets up association between cores and allocation classes of service
*
* @return Number of associations made
* @retval 0 no association made (nor requested)
* @retval negative error
* @retval positive success
*/
static int
set_allocation_assoc(void)
{
int i;
int ret;
for (i = 0; i < sel_l3ca_assoc_num; i++) {
ret = pqos_l3ca_assoc_set(sel_l3ca_assoc_tab[i].core,
sel_l3ca_assoc_tab[i].class_id);
ASSERT(ret == PQOS_RETVAL_OK);
if (ret != PQOS_RETVAL_OK) {
printf("Setting allocation class of service "
"association failed!\n");
return -1;
}
}
return sel_l3ca_assoc_num;
}
/**
* @brief Verifies and translates allocation association config string into
* internal configuration.
*
* @param str string passed to -a command line option
*/
static void
parse_allocation_assoc(char *str)
{
uint64_t cores[PQOS_MAX_CORES];
unsigned i = 0, n = 0, cos = 0;
char *p = NULL;
if (strncasecmp(str, "llc:", 4) != 0)
parse_error(str, "Unrecognized allocation type");
str += strlen("llc:");
p = strchr(str, '=');
if (p == NULL)
parse_error(str,
"Invalid allocation class of service "
"association format");
*p = '\0';
cos = (unsigned) strtouint64(str);
n = strlisttotab(p+1, cores, DIM(cores));
if (n == 0)
return;
if (sel_l3ca_assoc_num <= 0) {
for (i = 0; i < n; i++) {
if (i >= DIM(sel_l3ca_assoc_tab))
parse_error(str,
"too many cores selected for "
"allocation association");
sel_l3ca_assoc_tab[i].core = (unsigned) cores[i];
sel_l3ca_assoc_tab[i].class_id = cos;
}
sel_l3ca_assoc_num = (int) n;
return;
}
for (i = 0; i < n; i++) {
int j;
for (j = 0; j < sel_l3ca_assoc_num; j++)
if (sel_l3ca_assoc_tab[j].core == (unsigned) cores[i])
break;
if (j < sel_l3ca_assoc_num) {
/**
* this core is already on the list
* - update COS but warn about it
*/
printf("warn: updating COS for core %u from %u to %u\n",
(unsigned) cores[i],
sel_l3ca_assoc_tab[j].class_id, cos);
sel_l3ca_assoc_tab[j].class_id = cos;
} else {
/**
* New core is selected - extend the list
*/
unsigned k = (unsigned) sel_l3ca_assoc_num;
if (k >= DIM(sel_l3ca_assoc_tab))
parse_error(str,
"too many cores selected for "
"allocation association");
sel_l3ca_assoc_tab[k].core = (unsigned) cores[i];
sel_l3ca_assoc_tab[k].class_id = cos;
sel_l3ca_assoc_num++;
}
}
}
void selfn_allocation_assoc(const char *arg)
{
char *cp = NULL, *str = NULL;
char *saveptr = NULL;
if (arg == NULL)
parse_error(arg, "NULL pointer!");
if (strlen(arg) <= 0)
parse_error(arg, "Empty string!");
selfn_strdup(&cp, arg);
for (str = cp; ; str = NULL) {
char *token = NULL;
token = strtok_r(str, ";", &saveptr);
if (token == NULL)
break;
parse_allocation_assoc(token);
}
free(cp);
}
void alloc_print_config(const struct pqos_capability *cap_mon,
const struct pqos_capability *cap_l3ca,
const unsigned sock_count,
const unsigned *sockets,
const struct pqos_cpuinfo *cpu_info)
{
int ret;
unsigned i;
for (i = 0; (i < sock_count) && (cap_l3ca != NULL); i++) {
struct pqos_l3ca tab[PQOS_MAX_L3CA_COS];
unsigned num = 0;
unsigned n = 0;
ret = pqos_l3ca_get(sockets[i], PQOS_MAX_L3CA_COS,
&num, tab);
if (ret != PQOS_RETVAL_OK)
continue;
printf("L3CA COS definitions for Socket %u:\n",
sockets[i]);
for (n = 0; n < num; n++) {
if (tab[n].cdp) {
printf(" L3CA COS%u => DATA 0x%llx,"
"CODE 0x%llx\n",
tab[n].class_id,
(unsigned long long)tab[n].data_mask,
(unsigned long long)tab[n].code_mask);
} else {
printf(" L3CA COS%u => MASK 0x%llx\n",
tab[n].class_id,
(unsigned long long)tab[n].ways_mask);
}
}
}
for (i = 0; i < sock_count; i++) {
unsigned lcores[PQOS_MAX_SOCKET_CORES];
unsigned lcount = 0, n = 0;
ret = pqos_cpu_get_cores(cpu_info, sockets[i],
PQOS_MAX_SOCKET_CORES,
&lcount, &lcores[0]);
if (ret != PQOS_RETVAL_OK) {
printf("Error retrieving core information!\n");
return;
}
ASSERT(ret == PQOS_RETVAL_OK);
printf("Core information for socket %u:\n",
sockets[i]);
for (n = 0; n < lcount; n++) {
unsigned class_id = 0;
pqos_rmid_t rmid = 0;
int ret1 = PQOS_RETVAL_OK;
int ret2 = PQOS_RETVAL_OK;
if (cap_l3ca != NULL)
ret1 = pqos_l3ca_assoc_get(lcores[n],
&class_id);
if (cap_mon != NULL)
ret2 = pqos_mon_assoc_get(lcores[n], &rmid);
if (ret1 == PQOS_RETVAL_OK && ret2 == PQOS_RETVAL_OK) {
if (cap_l3ca != NULL && cap_mon != NULL)
printf(" Core %u => COS%u, RMID%u\n",
lcores[n], class_id,
(unsigned)rmid);
if (cap_l3ca == NULL && cap_mon != NULL)
printf(" Core %u => RMID%u\n",
lcores[n], (unsigned)rmid);
if (cap_l3ca != NULL && cap_mon == NULL)
printf(" Core %u => COS%u\n",
lcores[n], class_id);
} else {
printf(" Core %u => ERROR\n", lcores[n]);
}
}
}
}
int alloc_apply(const struct pqos_capability *cap_l3ca,
unsigned sock_count, unsigned *sockets)
{
if (cap_l3ca != NULL) {
/**
* If allocation config changed then exit.
* For monitoring, start the program again unless
* config file was provided
*/
int ret_assoc = 0, ret_cos = 0;
ret_cos = set_allocation_class(sock_count, sockets);
if (ret_cos < 0) {
printf("Allocation configuration error!\n");
return -1;
}
ret_assoc = set_allocation_assoc();
if (ret_assoc < 0) {
printf("CAT association error!\n");
return -1;
}
/**
* Check if any allocation configuration has changed
*/
if (ret_assoc > 0 || ret_cos > 0) {
printf("Allocation configuration altered.\n");
return 1;
}
} else {
if (sel_l3ca_assoc_num > 0 || sel_l3ca_cos_num > 0) {
printf("Allocation capability not detected!\n");
return -1;
}
}
return 0;
}