-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathapi.c
More file actions
1533 lines (1285 loc) · 50.9 KB
/
api.c
File metadata and controls
1533 lines (1285 loc) · 50.9 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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD LICENSE
*
* Copyright(c) 2017-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 "api.h"
#include "allocation.h"
#include "cap.h"
#include "cpuinfo.h"
#include "hw_monitoring.h"
#include "lock.h"
#include "log.h"
#include "monitoring.h"
#include "os_allocation.h"
#include "os_monitoring.h"
#include "pqos_internal.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
/**
* Value marking monitoring group structure as "valid".
* Group becomes "valid" after successful pqos_mon_start() or
* pqos_mon_start_pid() call.
*/
#define GROUP_VALID_MARKER (0x00DEAD00)
/**
* PQoS API functions
*/
static struct pqos_api {
/** Reads RMID association lcore */
int (*mon_assoc_get)(const unsigned lcore, pqos_rmid_t *rmid);
/** Gets RMID association of the channel */
int (*mon_assoc_get_channel)(const pqos_channel_t channel_id,
pqos_rmid_t *rmid);
/** Starts resource monitoring on selected group of cores */
int (*mon_start_cores)(const unsigned num_cores,
const unsigned *cores,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data *group,
const struct pqos_mon_options *opt);
/** Starts resource monitoring of selected pids */
int (*mon_start_pids)(const unsigned num_pids,
const pid_t *pids,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data *group);
/** Starts resource monitoring on selected channels */
int (*mon_start_channels)(const unsigned num_channels,
const pqos_channel_t *channels,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data *group,
const struct pqos_mon_options *opt);
/** Adds pids to the resource monitoring grpup */
int (*mon_add_pids)(const unsigned num_pids,
const pid_t *pids,
struct pqos_mon_data *group);
/** Remove pids from the resource monitoring group */
int (*mon_remove_pids)(const unsigned num_pids,
const pid_t *pids,
struct pqos_mon_data *group);
/** Starts uncore monitoring */
int (*mon_start_uncore)(const unsigned num_sockets,
const unsigned *sockets,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data *group);
/** Stops resource monitoring data for selected monitoring group */
int (*mon_stop)(struct pqos_mon_data *group);
/** Resets monitoring */
int (*mon_reset)(const struct pqos_mon_config *cfg);
/** Associates lcore with given class of service */
int (*alloc_assoc_set)(const unsigned lcore, const unsigned class_id);
/** Reads association of lcore with class of service */
int (*alloc_assoc_get)(const unsigned lcore, unsigned *class_id);
/** Associate task with given class of service */
int (*alloc_assoc_set_pid)(const pid_t task, const unsigned class_id);
/** Read association of task with class of service */
int (*alloc_assoc_get_pid)(const pid_t task, unsigned *class_id);
/** Reads association of channel with class of service */
int (*alloc_assoc_get_channel)(const pqos_channel_t channel,
unsigned *class_id);
/** Associates channel with given class of service */
int (*alloc_assoc_set_channel)(const pqos_channel_t channel,
const unsigned class_id);
/** Assign first available COS */
int (*alloc_assign)(const unsigned technology,
const unsigned *core_array,
const unsigned core_num,
unsigned *class_id);
/** Reassign cores to default COS */
int (*alloc_release)(const unsigned *core_array,
const unsigned core_num);
/** Assign first available COS to tasks */
int (*alloc_assign_pid)(const unsigned technology,
const pid_t *task_array,
const unsigned task_num,
unsigned *class_id);
/** Reassign tasks to default COS */
int (*alloc_release_pid)(const pid_t *task_array,
const unsigned task_num);
/** Resets configuration of allocation technologies */
int (*alloc_reset)(const struct pqos_alloc_config *cfg);
/** Sets L3 classes of service */
int (*l3ca_set)(const unsigned l3cat_id,
const unsigned num_cos,
const struct pqos_l3ca *ca);
/** Reads L3 classes of service */
int (*l3ca_get)(const unsigned l3cat_id,
const unsigned max_num_ca,
unsigned *num_ca,
struct pqos_l3ca *ca);
/** Get minimum L3 CBM bits */
int (*l3ca_get_min_cbm_bits)(unsigned *min_cbm_bits);
/** Sets L2 classes of service */
int (*l2ca_set)(const unsigned l2id,
const unsigned num_cos,
const struct pqos_l2ca *ca);
/** Reads L2 classes of service */
int (*l2ca_get)(const unsigned l2id,
const unsigned max_num_ca,
unsigned *num_ca,
struct pqos_l2ca *ca);
/** Get minimum L2 CBM bits */
int (*l2ca_get_min_cbm_bits)(unsigned *min_cbm_bits);
/** Set MBA */
int (*mba_get)(const unsigned mba_id,
const unsigned max_num_cos,
unsigned *num_cos,
struct pqos_mba *mba_tab);
/** Get MBA mask */
int (*mba_set)(const unsigned mba_id,
const unsigned num_cos,
const struct pqos_mba *requested,
struct pqos_mba *actual);
/** Retrieves tasks associated with COS */
unsigned *(*pid_get_pid_assoc)(const unsigned class_id,
unsigned *count);
} api;
/*
* =======================================
* Init module
* =======================================
*/
int
api_init(int interface, enum pqos_vendor vendor)
{
if (interface != PQOS_INTER_MSR && interface != PQOS_INTER_OS &&
interface != PQOS_INTER_OS_RESCTRL_MON)
return PQOS_RETVAL_PARAM;
memset(&api, 0, sizeof(api));
if (interface == PQOS_INTER_MSR) {
api.mon_reset = hw_mon_reset;
api.mon_assoc_get = hw_mon_assoc_get_core;
api.mon_assoc_get_channel = hw_mon_assoc_get_channel;
api.mon_start_cores = hw_mon_start_cores;
api.mon_start_channels = hw_mon_start_channels;
api.mon_stop = hw_mon_stop;
api.mon_reset = hw_mon_reset;
api.alloc_assoc_set = hw_alloc_assoc_set;
api.alloc_assoc_get = hw_alloc_assoc_get;
api.alloc_assoc_get_channel = hw_alloc_assoc_get_channel;
api.alloc_assoc_set_channel = hw_alloc_assoc_set_channel;
api.alloc_assign = hw_alloc_assign;
api.alloc_release = hw_alloc_release;
api.alloc_reset = hw_alloc_reset;
api.l3ca_set = hw_l3ca_set;
api.l3ca_get = hw_l3ca_get;
api.l3ca_get_min_cbm_bits = hw_l3ca_get_min_cbm_bits;
api.l2ca_set = hw_l2ca_set;
api.l2ca_get = hw_l2ca_get;
api.l2ca_get_min_cbm_bits = hw_l2ca_get_min_cbm_bits;
if (vendor == PQOS_VENDOR_AMD || vendor == PQOS_VENDOR_HYGON) {
api.mba_get = hw_mba_get_amd;
api.mba_set = hw_mba_set_amd;
} else {
api.mon_start_uncore = hw_mon_start_uncore;
api.mba_get = hw_mba_get;
api.mba_set = hw_mba_set;
}
#ifdef __linux__
} else if (interface == PQOS_INTER_OS ||
interface == PQOS_INTER_OS_RESCTRL_MON) {
api.mon_reset = os_mon_reset;
api.mon_start_cores = os_mon_start_cores;
api.mon_start_pids = os_mon_start_pids;
api.mon_add_pids = os_mon_add_pids;
api.mon_remove_pids = os_mon_remove_pids;
api.mon_stop = os_mon_stop;
api.alloc_assoc_set = os_alloc_assoc_set;
api.alloc_assoc_get = os_alloc_assoc_get;
api.alloc_assoc_set_pid = os_alloc_assoc_set_pid;
api.alloc_assoc_get_pid = os_alloc_assoc_get_pid;
api.alloc_assign = os_alloc_assign;
api.alloc_release = os_alloc_release;
api.alloc_assign_pid = os_alloc_assign_pid;
api.alloc_release_pid = os_alloc_release_pid;
api.alloc_reset = os_alloc_reset;
api.l3ca_set = os_l3ca_set;
api.l3ca_get = os_l3ca_get;
api.l3ca_get_min_cbm_bits = os_l3ca_get_min_cbm_bits;
api.l2ca_set = os_l2ca_set;
api.l2ca_get = os_l2ca_get;
api.l2ca_get_min_cbm_bits = os_l2ca_get_min_cbm_bits;
if (vendor == PQOS_VENDOR_AMD || vendor == PQOS_VENDOR_HYGON) {
api.mba_get = os_mba_get_amd;
api.mba_set = os_mba_set_amd;
} else {
api.mba_get = os_mba_get;
api.mba_set = os_mba_set;
}
api.pid_get_pid_assoc = os_pid_get_pid_assoc;
#endif
}
return PQOS_RETVAL_OK;
}
#define UNSUPPORTED_INTERFACE "Interface not supported!\n"
#define API_CALL(API, PARAMS...) \
({ \
int ret; \
\
lock_get(); \
do { \
ret = _pqos_check_init(1); \
if (ret != PQOS_RETVAL_OK) \
break; \
\
if (api.API != NULL) \
ret = api.API(PARAMS); \
else { \
LOG_INFO(UNSUPPORTED_INTERFACE); \
ret = PQOS_RETVAL_RESOURCE; \
} \
} while (0); \
lock_release(); \
\
ret; \
})
/*
* =======================================
* Allocation Technology
* =======================================
*/
int
pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id)
{
return API_CALL(alloc_assoc_set, lcore, class_id);
}
int
pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id)
{
if (class_id == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assoc_get, lcore, class_id);
}
int
pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id)
{
return API_CALL(alloc_assoc_set_pid, task, class_id);
}
int
pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id)
{
if (class_id == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assoc_get_pid, task, class_id);
}
int
pqos_alloc_assign(const unsigned technology,
const unsigned *core_array,
const unsigned core_num,
unsigned *class_id)
{
const int l2_req = ((technology & (1 << PQOS_CAP_TYPE_L2CA)) != 0);
const int l3_req = ((technology & (1 << PQOS_CAP_TYPE_L3CA)) != 0);
const int mba_req = ((technology & (1 << PQOS_CAP_TYPE_MBA)) != 0);
const int smba_req = ((technology & (1 << PQOS_CAP_TYPE_SMBA)) != 0);
if (core_num == 0 || core_array == NULL || class_id == NULL ||
!(l2_req || l3_req || mba_req || smba_req))
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assign, technology, core_array, core_num,
class_id);
}
int
pqos_alloc_release(const unsigned *core_array, const unsigned core_num)
{
if (core_num == 0 || core_array == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_release, core_array, core_num);
}
int
pqos_alloc_assign_pid(const unsigned technology,
const pid_t *task_array,
const unsigned task_num,
unsigned *class_id)
{
if (task_array == NULL || task_num == 0 || class_id == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assign_pid, technology, task_array, task_num,
class_id);
}
int
pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num)
{
if (task_array == NULL || task_num == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_release_pid, task_array, task_num);
}
int
pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg,
const enum pqos_cdp_config l2_cdp_cfg,
const enum pqos_mba_config mba_cfg)
{
struct pqos_alloc_config cfg;
memset(&cfg, 0, sizeof(cfg));
cfg.l3_cdp = l3_cdp_cfg;
cfg.l2_cdp = l2_cdp_cfg;
cfg.mba = mba_cfg;
cfg.mba40 = PQOS_FEATURE_ANY;
return pqos_alloc_reset_config(&cfg);
}
int
pqos_alloc_reset_config(const struct pqos_alloc_config *cfg)
{
/* validate parameters */
if (cfg != NULL) {
if (cfg->l3_cdp != PQOS_REQUIRE_CDP_ON &&
cfg->l3_cdp != PQOS_REQUIRE_CDP_OFF &&
cfg->l3_cdp != PQOS_REQUIRE_CDP_ANY) {
LOG_ERROR(
"Unrecognized L3 CDP configuration setting %d!\n",
cfg->l3_cdp);
return PQOS_RETVAL_PARAM;
}
if (cfg->l3_iordt != PQOS_REQUIRE_IORDT_ON &&
cfg->l3_iordt != PQOS_REQUIRE_IORDT_OFF &&
cfg->l3_iordt != PQOS_REQUIRE_IORDT_ANY) {
LOG_ERROR("Unrecognized L3 I/O RDT configuration "
"setting %d!\n",
cfg->l3_iordt);
return PQOS_RETVAL_PARAM;
}
if (cfg->l2_cdp != PQOS_REQUIRE_CDP_ON &&
cfg->l2_cdp != PQOS_REQUIRE_CDP_OFF &&
cfg->l2_cdp != PQOS_REQUIRE_CDP_ANY) {
LOG_ERROR(
"Unrecognized L2 CDP configuration setting %d!\n",
cfg->l2_cdp);
return PQOS_RETVAL_PARAM;
}
if (cfg->mba != PQOS_MBA_ANY && cfg->mba != PQOS_MBA_DEFAULT &&
cfg->mba != PQOS_MBA_CTRL) {
LOG_ERROR(
"Unrecognized MBA configuration setting %d!\n",
cfg->mba);
return PQOS_RETVAL_PARAM;
}
if (cfg->mba40 != PQOS_FEATURE_ANY &&
cfg->mba40 != PQOS_FEATURE_ON &&
cfg->mba40 != PQOS_FEATURE_OFF) {
LOG_ERROR(
"Unrecognized MBA 4.0 configuration setting %d!\n",
cfg->mba40);
return PQOS_RETVAL_PARAM;
}
if (cfg->mba != PQOS_MBA_ANY && cfg->mba != PQOS_MBA_DEFAULT &&
cfg->mba != PQOS_MBA_CTRL) {
LOG_ERROR(
"Unrecognized MBA configuration setting %d!\n",
cfg->mba);
return PQOS_RETVAL_PARAM;
}
if (cfg->smba != PQOS_MBA_ANY &&
cfg->smba != PQOS_MBA_DEFAULT &&
cfg->smba != PQOS_MBA_CTRL) {
LOG_ERROR(
"Unrecognized SMBA configuration setting %d!\n",
cfg->mba);
return PQOS_RETVAL_PARAM;
}
}
return API_CALL(alloc_reset, cfg);
}
unsigned *
pqos_pid_get_pid_assoc(const unsigned class_id, unsigned *count)
{
int ret;
unsigned *tasks = NULL;
if (count == NULL)
return NULL;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return NULL;
}
if (api.pid_get_pid_assoc != NULL) {
tasks = api.pid_get_pid_assoc(class_id, count);
if (tasks == NULL)
LOG_ERROR("Error retrieving task information!\n");
} else
LOG_INFO(UNSUPPORTED_INTERFACE);
lock_release();
return tasks;
}
/*
* =======================================
* L3 cache allocation
* =======================================
*/
int
pqos_l3ca_set(const unsigned l3cat_id,
const unsigned num_cos,
const struct pqos_l3ca *ca)
{
unsigned i;
if (ca == NULL || num_cos == 0)
return PQOS_RETVAL_PARAM;
/**
* Check if class bitmasks are zero.
*/
for (i = 0; i < num_cos; i++) {
int is_non_zero = 0;
if (ca[i].cdp)
is_non_zero =
ca[i].u.s.data_mask && ca[i].u.s.code_mask;
else
is_non_zero = ca[i].u.ways_mask;
if (!is_non_zero) {
LOG_ERROR("L3 COS%u bit mask is 0!\n", ca[i].class_id);
return PQOS_RETVAL_PARAM;
}
}
return API_CALL(l3ca_set, l3cat_id, num_cos, ca);
}
int
pqos_l3ca_get(const unsigned l3cat_id,
const unsigned max_num_ca,
unsigned *num_ca,
struct pqos_l3ca *ca)
{
if (num_ca == NULL || ca == NULL || max_num_ca == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(l3ca_get, l3cat_id, max_num_ca, num_ca, ca);
}
int
pqos_l3ca_get_min_cbm_bits(unsigned *min_cbm_bits)
{
if (min_cbm_bits == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(l3ca_get_min_cbm_bits, min_cbm_bits);
}
/*
* =======================================
* L2 cache allocation
* =======================================
*/
int
pqos_l2ca_set(const unsigned l2id,
const unsigned num_cos,
const struct pqos_l2ca *ca)
{
unsigned i;
if (ca == NULL || num_cos == 0)
return PQOS_RETVAL_PARAM;
/**
* Check if class bitmasks are zero.
*/
for (i = 0; i < num_cos; i++) {
int is_non_zero = 0;
if (ca[i].cdp)
is_non_zero =
ca[i].u.s.data_mask && ca[i].u.s.code_mask;
else
is_non_zero = ca[i].u.ways_mask;
if (!is_non_zero) {
LOG_ERROR("L2 COS%u bit mask is 0!\n", ca[i].class_id);
return PQOS_RETVAL_PARAM;
}
}
return API_CALL(l2ca_set, l2id, num_cos, ca);
}
int
pqos_l2ca_get(const unsigned l2id,
const unsigned max_num_ca,
unsigned *num_ca,
struct pqos_l2ca *ca)
{
if (num_ca == NULL || ca == NULL || max_num_ca == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(l2ca_get, l2id, max_num_ca, num_ca, ca);
}
int
pqos_l2ca_get_min_cbm_bits(unsigned *min_cbm_bits)
{
if (min_cbm_bits == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(l2ca_get_min_cbm_bits, min_cbm_bits);
}
/*
* =======================================
* Memory Bandwidth Allocation
* =======================================
*/
int
pqos_mba_set(const unsigned mba_id,
const unsigned num_cos,
const struct pqos_mba *requested,
struct pqos_mba *actual)
{
int ret;
unsigned i;
if (requested == NULL || num_cos == 0)
return PQOS_RETVAL_PARAM;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return ret;
}
/**
* Check if MBA rate is within allowed range
*/
for (i = 0; i < num_cos; i++) {
const struct cpuinfo_config *vconfig;
cpuinfo_get_config(&vconfig);
if (requested[i].ctrl == 0 &&
(requested[i].mb_max == 0 ||
requested[i].mb_max > vconfig->mba_max)) {
LOG_ERROR("MBA COS%u rate out of range (from 1-%d)!\n",
requested[i].class_id, vconfig->mba_max);
lock_release();
return PQOS_RETVAL_PARAM;
}
}
if (api.mba_set != NULL)
ret = api.mba_set(mba_id, num_cos, requested, actual);
else {
LOG_INFO(UNSUPPORTED_INTERFACE);
ret = PQOS_RETVAL_RESOURCE;
}
lock_release();
return ret;
}
int
pqos_mba_get(const unsigned mba_id,
const unsigned max_num_cos,
unsigned *num_cos,
struct pqos_mba *mba_tab)
{
if (num_cos == NULL || mba_tab == NULL || max_num_cos == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(mba_get, mba_id, max_num_cos, num_cos, mba_tab);
}
/*
* =======================================
* IO RDT Allocation
* =======================================
*/
int
pqos_alloc_assoc_get_channel(const pqos_channel_t channel, unsigned *class_id)
{
if (class_id == NULL || channel == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assoc_get_channel, channel, class_id);
}
int
pqos_alloc_assoc_get_dev(const uint16_t segment,
const uint16_t bdf,
const unsigned vc,
unsigned *class_id)
{
int ret;
if (class_id == NULL || vc >= PQOS_DEV_MAX_CHANNELS)
return PQOS_RETVAL_PARAM;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return ret;
}
if (api.alloc_assoc_get_channel != NULL) {
const struct pqos_devinfo *dev = _pqos_get_dev();
pqos_channel_t channel_id =
pqos_devinfo_get_channel_id(dev, segment, bdf, vc);
if (channel_id == 0)
ret = PQOS_RETVAL_PARAM;
else
ret = api.alloc_assoc_get_channel(channel_id, class_id);
} else {
LOG_INFO(UNSUPPORTED_INTERFACE);
ret = PQOS_RETVAL_RESOURCE;
}
lock_release();
return ret;
}
int
pqos_alloc_assoc_set_channel(const pqos_channel_t channel,
const unsigned class_id)
{
if (channel == 0)
return PQOS_RETVAL_PARAM;
return API_CALL(alloc_assoc_set_channel, channel, class_id);
}
int
pqos_alloc_assoc_set_dev(const uint16_t segment,
const uint16_t bdf,
const unsigned vc,
const unsigned class_id)
{
int ret;
if (vc >= PQOS_DEV_MAX_CHANNELS)
return PQOS_RETVAL_PARAM;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return ret;
}
if (api.alloc_assoc_set_channel != NULL) {
const struct pqos_devinfo *dev = _pqos_get_dev();
pqos_channel_t channel_id =
pqos_devinfo_get_channel_id(dev, segment, bdf, vc);
if (channel_id == 0)
ret = PQOS_RETVAL_PARAM;
else {
int shared;
/* Check if channel is shared */
ret = pqos_devinfo_get_channel_shared(dev, channel_id,
&shared);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return ret;
}
if (shared)
LOG_WARN("Changing association of shared "
"channel %lX\n",
channel_id);
ret = api.alloc_assoc_set_channel(channel_id, class_id);
}
} else {
LOG_INFO(UNSUPPORTED_INTERFACE);
ret = PQOS_RETVAL_RESOURCE;
}
lock_release();
return ret;
}
/*
* =======================================
* Monitoring
* =======================================
*/
int
pqos_mon_reset(void)
{
return pqos_mon_reset_config(NULL);
}
int
pqos_mon_reset_config(const struct pqos_mon_config *cfg)
{
/* validate parameters */
if (cfg != NULL) {
if (cfg->l3_iordt != PQOS_REQUIRE_IORDT_ON &&
cfg->l3_iordt != PQOS_REQUIRE_IORDT_OFF &&
cfg->l3_iordt != PQOS_REQUIRE_IORDT_ANY) {
LOG_ERROR("Unrecognized I/O RDT Monitoring "
"configuration setting %d!\n",
cfg->l3_iordt);
return PQOS_RETVAL_PARAM;
}
if (cfg->snc != PQOS_REQUIRE_SNC_LOCAL &&
cfg->snc != PQOS_REQUIRE_SNC_TOTAL &&
cfg->snc != PQOS_REQUIRE_SNC_ANY) {
LOG_ERROR("Unrecognized SNC Monitoring "
"configuration setting %d!\n",
cfg->snc);
return PQOS_RETVAL_PARAM;
}
}
return API_CALL(mon_reset, cfg);
}
int
pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid)
{
if (rmid == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(mon_assoc_get, lcore, rmid);
}
int
pqos_mon_assoc_get_channel(const pqos_channel_t channel_id, pqos_rmid_t *rmid)
{
if (channel_id == 0 || rmid == NULL)
return PQOS_RETVAL_PARAM;
return API_CALL(mon_assoc_get_channel, channel_id, rmid);
}
int
pqos_mon_assoc_get_dev(const uint16_t segment,
const uint16_t bdf,
const unsigned vc,
pqos_rmid_t *rmid)
{
int ret;
if (rmid == NULL)
return PQOS_RETVAL_PARAM;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
return ret;
}
if (api.mon_assoc_get_channel != NULL) {
const struct pqos_devinfo *dev = _pqos_get_dev();
pqos_channel_t channel_id =
pqos_devinfo_get_channel_id(dev, segment, bdf, vc);
if (channel_id == 0)
ret = PQOS_RETVAL_PARAM;
else
ret = api.mon_assoc_get_channel(channel_id, rmid);
} else {
LOG_INFO(UNSUPPORTED_INTERFACE);
ret = PQOS_RETVAL_RESOURCE;
}
lock_release();
return ret;
}
int
pqos_mon_start(const unsigned num_cores,
const unsigned *cores,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data *group)
{
int ret;
struct pqos_mon_options opt;
if (group == NULL || cores == NULL || num_cores == 0 || event == 0)
return PQOS_RETVAL_PARAM;
if (group->valid == GROUP_VALID_MARKER)
return PQOS_RETVAL_PARAM;
/**
* Validate event parameter
* - only combinations of events allowed
* - do not allow non-PQoS events to be monitored on its own
*/
if (event & (~(PQOS_MON_EVENT_L3_OCCUP | PQOS_MON_EVENT_LMEM_BW |
PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW |
PQOS_PERF_EVENT_IPC | PQOS_PERF_EVENT_LLC_MISS |
PQOS_PERF_EVENT_LLC_REF)))
return PQOS_RETVAL_PARAM;
if ((event & (PQOS_MON_EVENT_L3_OCCUP | PQOS_MON_EVENT_LMEM_BW |
PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW)) == 0 &&
(event & (PQOS_PERF_EVENT_IPC | PQOS_PERF_EVENT_LLC_MISS |
PQOS_PERF_EVENT_LLC_REF)) != 0) {
LOG_ERROR("Only PMU events selected for monitoring\n");
return PQOS_RETVAL_PARAM;
}
struct pqos_mon_data_internal *mem = malloc(sizeof(*group->intl));
if (mem == NULL)
return PQOS_RETVAL_RESOURCE;
lock_get();
ret = _pqos_check_init(1);
if (ret != PQOS_RETVAL_OK) {
lock_release();
free(mem);
return ret;
}
memset(group, 0, sizeof(*group));
group->intl = mem;
memset(group->intl, 0, sizeof(*group->intl));
memset(&opt, 0, sizeof(opt));
if (api.mon_start_cores != NULL)
ret = api.mon_start_cores(num_cores, cores, event, context,
group, &opt);
else {
LOG_INFO(UNSUPPORTED_INTERFACE);
ret = PQOS_RETVAL_RESOURCE;
}
if (ret == PQOS_RETVAL_OK)
group->valid = GROUP_VALID_MARKER;
else if (mem != NULL)
free(mem);
lock_release();
return ret;
}
int
pqos_mon_start_cores(const unsigned num_cores,
const unsigned *cores,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data **group)
{
struct pqos_mon_options opt;
memset(&opt, 0, sizeof(opt));
return pqos_mon_start_cores_ext(num_cores, cores, event, context, group,
&opt);
}
int
pqos_mon_start_cores_ext(const unsigned num_cores,
const unsigned *cores,
const enum pqos_mon_event event,
void *context,
struct pqos_mon_data **group,
const struct pqos_mon_options *opt)
{
int ret;
struct pqos_mon_data *data = NULL;
if (group == NULL || cores == NULL || num_cores == 0 || event == 0 ||
opt == NULL)
return PQOS_RETVAL_PARAM;
/**
* Validate event parameter
* - only combinations of events allowed
* - do not allow non-PQoS events to be monitored on its own
*/
if (event & (~(PQOS_MON_EVENT_L3_OCCUP | PQOS_MON_EVENT_LMEM_BW |
PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW |
PQOS_PERF_EVENT_IPC | PQOS_PERF_EVENT_LLC_MISS |
PQOS_PERF_EVENT_LLC_REF)))
return PQOS_RETVAL_PARAM;
if ((event & (PQOS_MON_EVENT_L3_OCCUP | PQOS_MON_EVENT_LMEM_BW |
PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW)) == 0 &&
(event & (PQOS_PERF_EVENT_IPC | PQOS_PERF_EVENT_LLC_MISS |
PQOS_PERF_EVENT_LLC_REF)) != 0) {
LOG_ERROR("Only PMU events selected for monitoring\n");
return PQOS_RETVAL_PARAM;