File: resctrl_schemata.c

package info (click to toggle)
intel-cmt-cat 26.03-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,748 kB
  • sloc: ansic: 60,484; makefile: 2,116; python: 1,484; perl: 527; sh: 2
file content (845 lines) | stat: -rw-r--r-- 26,981 bytes parent folder | download
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
/*
 * BSD LICENSE
 *
 * Copyright(c) 2019-2026 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 "resctrl_schemata.h"

#include "cpuinfo.h"
#include "log.h"
#include "resctrl_utils.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

/*
 * @brief Structure to hold parsed schemata
 */
struct resctrl_schemata {
        unsigned l3ids_num; /**< Number of L3 */
        unsigned *l3ids;
        struct pqos_l3ca *l3ca; /**< L3 COS definitions */

        unsigned mbaids_num; /**< Number of mba ids */
        unsigned *mbaids;
        struct pqos_mba *mba; /**< MBA COS definitions */

        unsigned smbaids_num; /**< Number of smba ids */
        unsigned *smbaids;
        struct pqos_mba *smba; /**< SMBA COS definitions */

        unsigned l2ids_num; /**< Number of L2 clusters */
        unsigned *l2ids;
        struct pqos_l2ca *l2ca; /**< L2 COS definitions */
};

void
resctrl_schemata_free(struct resctrl_schemata *schemata)
{
        if (schemata == NULL)
                return;

        if (schemata->l2ca != NULL)
                free(schemata->l2ca);
        if (schemata->l3ca != NULL)
                free(schemata->l3ca);
        if (schemata->mba != NULL)
                free(schemata->mba);
        if (schemata->l2ids != NULL)
                free(schemata->l2ids);
        if (schemata->l3ids != NULL)
                free(schemata->l3ids);
        if (schemata->mbaids != NULL)
                free(schemata->mbaids);
        free(schemata);
}

struct resctrl_schemata *
resctrl_schemata_alloc(const struct pqos_cap *cap,
                       const struct pqos_cpuinfo *cpu)
{
        struct resctrl_schemata *schemata;
        int retval;
        const struct pqos_capability *cap_l2ca;
        const struct pqos_capability *cap_l3ca;
        const struct pqos_capability *cap_mba;
        const struct pqos_capability *cap_smba;
        unsigned i;

        schemata = (struct resctrl_schemata *)calloc(1, sizeof(*schemata));
        if (schemata == NULL)
                return NULL;

        /* L2 */
        retval = pqos_cap_get_type(cap, PQOS_CAP_TYPE_L2CA, &cap_l2ca);
        if (retval == PQOS_RETVAL_OK && cap_l2ca != NULL) {
                schemata->l2ids = pqos_cpu_get_l2ids(cpu, &schemata->l2ids_num);
                if (schemata->l2ids == NULL)
                        goto resctrl_schemata_alloc_error;

                schemata->l2ca =
                    calloc(schemata->l2ids_num, sizeof(struct pqos_l2ca));
                if (schemata->l2ca == NULL)
                        goto resctrl_schemata_alloc_error;
        }

        /* L3 */
        retval = pqos_cap_get_type(cap, PQOS_CAP_TYPE_L3CA, &cap_l3ca);
        if (retval == PQOS_RETVAL_OK && cap_l3ca != NULL) {
                schemata->l3ids =
                    pqos_cpu_get_l3cat_ids(cpu, &schemata->l3ids_num);
                if (schemata->l3ids == NULL)
                        goto resctrl_schemata_alloc_error;

                schemata->l3ca =
                    calloc(schemata->l3ids_num, sizeof(struct pqos_l3ca));
                if (schemata->l3ca == NULL)
                        goto resctrl_schemata_alloc_error;
        }

        /* MBA */
        retval = pqos_cap_get_type(cap, PQOS_CAP_TYPE_MBA, &cap_mba);
        if (retval == PQOS_RETVAL_OK && cap_mba != NULL) {
                int ctrl_enabled;

                if (schemata->mbaids == NULL) {
                        schemata->mbaids =
                            pqos_cpu_get_mba_ids(cpu, &schemata->mbaids_num);
                        if (schemata->mbaids == NULL)
                                goto resctrl_schemata_alloc_error;
                }

                schemata->mba =
                    calloc(schemata->mbaids_num, sizeof(struct pqos_mba));
                if (schemata->mba == NULL)
                        goto resctrl_schemata_alloc_error;

                retval = pqos_mba_ctrl_enabled(cap, NULL, &ctrl_enabled);
                if (retval != PQOS_RETVAL_OK)
                        goto resctrl_schemata_alloc_error;

                /* fill ctrl and class_id */
                for (i = 0; i < schemata->mbaids_num; i++)
                        schemata->mba[i].ctrl = ctrl_enabled;
        }

        /* sMBA */
        retval = pqos_cap_get_type(cap, PQOS_CAP_TYPE_SMBA, &cap_smba);
        if (retval == PQOS_RETVAL_OK && cap_smba != NULL) {

                if (schemata->smbaids == NULL) {
                        schemata->smbaids =
                            pqos_cpu_get_smba_ids(cpu, &schemata->smbaids_num);
                        if (schemata->smbaids == NULL)
                                goto resctrl_schemata_alloc_error;
                }

                schemata->smba =
                    calloc(schemata->smbaids_num, sizeof(struct pqos_mba));
                if (schemata->smba == NULL)
                        goto resctrl_schemata_alloc_error;

                /* fill smba flag */
                for (i = 0; i < schemata->smbaids_num; i++)
                        schemata->smba[i].smba = 1;
        }

        return schemata;

resctrl_schemata_alloc_error:
        resctrl_schemata_free(schemata);
        return NULL;
}

int
resctrl_schemata_reset(struct resctrl_schemata *schmt,
                       const struct pqos_cap_l3ca *l3ca_cap,
                       const struct pqos_cap_l2ca *l2ca_cap,
                       const struct pqos_cap_mba *mba_cap,
                       const struct pqos_cap_mba *smba_cap)
{
        unsigned j;

        /* Reset L3 CAT */
        if (l3ca_cap != NULL) {
                uint64_t default_l3ca = ((uint64_t)1 << l3ca_cap->num_ways) - 1;

                for (j = 0; j < schmt->l3ids_num; j++) {
                        if (l3ca_cap->cdp_on) {
                                schmt->l3ca[j].cdp = 1;
                                schmt->l3ca[j].u.s.code_mask = default_l3ca;
                                schmt->l3ca[j].u.s.data_mask = default_l3ca;
                        } else {
                                schmt->l3ca[j].cdp = 0;
                                schmt->l3ca[j].u.ways_mask = default_l3ca;
                        }
                }
        }

        /* Reset L2 CAT */
        if (l2ca_cap != NULL) {
                uint64_t default_l2ca = ((uint64_t)1 << l2ca_cap->num_ways) - 1;

                for (j = 0; j < schmt->l2ids_num; j++) {
                        if (l2ca_cap->cdp_on) {
                                schmt->l2ca[j].cdp = 1;
                                schmt->l2ca[j].u.s.code_mask = default_l2ca;
                                schmt->l2ca[j].u.s.data_mask = default_l2ca;
                        } else {
                                schmt->l2ca[j].cdp = 0;
                                schmt->l2ca[j].u.ways_mask = default_l2ca;
                        }
                }
        }

        /* Reset MBA */
        if (mba_cap != NULL) {
                const struct cpuinfo_config *vconfig;
                uint32_t default_mba;

                cpuinfo_get_config(&vconfig);
                default_mba = vconfig->mba_max;

                /* kernel always rounds up value to MBA granularity */
                if (mba_cap->ctrl_on)
                        default_mba =
                            UINT32_MAX - UINT32_MAX % mba_cap->throttle_step;

                for (j = 0; j < schmt->mbaids_num; j++)
                        schmt->mba[j].mb_max = default_mba;
        }

        /* Reset SMBA */
        if (smba_cap != NULL) {
                const struct cpuinfo_config *vconfig;
                uint32_t default_smba;

                cpuinfo_get_config(&vconfig);
                default_smba = vconfig->mba_max;

                /* kernel always rounds up value to MBA granularity */
                if (smba_cap->ctrl_on)
                        default_smba =
                            UINT32_MAX - UINT32_MAX % smba_cap->throttle_step;

                for (j = 0; j < schmt->mbaids_num; j++) {
                        schmt->smba[j].mb_max = default_smba;
                        schmt->smba[j].smba = 1;
                }
        }

        return PQOS_RETVAL_OK;
}

static int
get_l2_index(const struct resctrl_schemata *schemata, unsigned resource_id)
{
        unsigned i;

        ASSERT(schemata->l2ids != NULL);

        for (i = 0; i < schemata->l2ids_num; ++i)
                if (schemata->l2ids[i] == resource_id)
                        return i;

        return -1;
}

int
resctrl_schemata_l2ca_get(const struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          struct pqos_l2ca *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->l2ca != NULL);

        index = get_l2_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        *ca = schemata->l2ca[index];

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_l2ca_set(struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          const struct pqos_l2ca *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->l2ca != NULL);

        index = get_l2_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        schemata->l2ca[index] = *ca;

        return PQOS_RETVAL_OK;
}

static int
get_l3_index(const struct resctrl_schemata *schemata, unsigned resource_id)
{
        unsigned i;

        ASSERT(schemata->l3ids != NULL);

        for (i = 0; i < schemata->l3ids_num; ++i)
                if (schemata->l3ids[i] == resource_id)
                        return i;

        return -1;
}

int
resctrl_schemata_l3ca_get(const struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          struct pqos_l3ca *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->l3ca != NULL);

        index = get_l3_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        *ca = schemata->l3ca[index];

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_l3ca_set(struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          const struct pqos_l3ca *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->l3ca != NULL);

        index = get_l3_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        schemata->l3ca[index] = *ca;

        return PQOS_RETVAL_OK;
}

static int
get_mba_index(const struct resctrl_schemata *schemata, unsigned resource_id)
{
        unsigned i;

        ASSERT(schemata->mbaids != NULL);

        for (i = 0; i < schemata->mbaids_num; ++i)
                if (schemata->mbaids[i] == resource_id)
                        return i;

        return -1;
}

int
resctrl_schemata_mba_get(const struct resctrl_schemata *schemata,
                         unsigned resource_id,
                         struct pqos_mba *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->mba != NULL);

        index = get_mba_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        *ca = schemata->mba[index];

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_mba_set(struct resctrl_schemata *schemata,
                         unsigned resource_id,
                         const struct pqos_mba *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->mba != NULL);

        index = get_mba_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        schemata->mba[index] = *ca;

        return PQOS_RETVAL_OK;
}

static int
get_smba_index(const struct resctrl_schemata *schemata, unsigned resource_id)
{
        unsigned i;

        ASSERT(schemata->smbaids != NULL);

        for (i = 0; i < schemata->smbaids_num; ++i)
                if (schemata->smbaids[i] == resource_id)
                        return i;

        return -1;
}

int
resctrl_schemata_smba_get(const struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          struct pqos_mba *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->smba != NULL);

        index = get_smba_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        *ca = schemata->smba[index];

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_smba_set(struct resctrl_schemata *schemata,
                          unsigned resource_id,
                          const struct pqos_mba *ca)
{
        int index;

        ASSERT(schemata != NULL);
        ASSERT(ca != NULL);
        ASSERT(schemata->smba != NULL);

        index = get_smba_index(schemata, resource_id);
        if (index < 0)
                return PQOS_RETVAL_ERROR;

        schemata->smba[index] = *ca;

        return PQOS_RETVAL_OK;
}

/**
 * @brief Schemata type
 */
enum resctrl_schemata_type {
        RESCTRL_SCHEMATA_TYPE_NONE,   /**< unknown */
        RESCTRL_SCHEMATA_TYPE_L2,     /**< L2 CAT without CDP */
        RESCTRL_SCHEMATA_TYPE_L2CODE, /**< L2 CAT code */
        RESCTRL_SCHEMATA_TYPE_L2DATA, /**< L2 CAT data */
        RESCTRL_SCHEMATA_TYPE_L3,     /**< L3 CAT without CDP */
        RESCTRL_SCHEMATA_TYPE_L3CODE, /**< L3 CAT code */
        RESCTRL_SCHEMATA_TYPE_L3DATA, /**< L3 CAT data */
        RESCTRL_SCHEMATA_TYPE_MB,     /**< MBA data */
        RESCTRL_SCHEMATA_TYPE_SMBA,   /**< SMBA data */
};

/**
 * @brief Determine allocation type
 *
 * @param [in] str resctrl label
 *
 * @return Allocation type
 */
static int
resctrl_schemata_type_get(const char *str)
{
        int type = RESCTRL_SCHEMATA_TYPE_NONE;

        if (strcasecmp(str, "L2") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L2;
        else if (strcasecmp(str, "L2CODE") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L2CODE;
        else if (strcasecmp(str, "L2DATA") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L2DATA;
        else if (strcasecmp(str, "L3") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L3;
        else if (strcasecmp(str, "L3CODE") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L3CODE;
        else if (strcasecmp(str, "L3DATA") == 0)
                type = RESCTRL_SCHEMATA_TYPE_L3DATA;
        else if (strcasecmp(str, "MB") == 0)
                type = RESCTRL_SCHEMATA_TYPE_MB;
        else if (strcasecmp(str, "SMBA") == 0)
                type = RESCTRL_SCHEMATA_TYPE_SMBA;

        return type;
}

/**
 * @brief Fill schemata structure
 *
 * @param [in] res_id Resource id
 * @param [in] value Ways mask/Memory B/W rate
 * @param [in] type Schemata type
 * @param [out] schemata Schemata structure
 *
 * @return Operational status
 * @retval PQOS_RETVAL_OK on success
 */
static int
resctrl_schemata_set(const unsigned res_id,
                     const uint64_t value,
                     const int type,
                     struct resctrl_schemata *schemata)
{
        int index = -1;

        switch (type) {
        case RESCTRL_SCHEMATA_TYPE_L2:
        case RESCTRL_SCHEMATA_TYPE_L2CODE:
        case RESCTRL_SCHEMATA_TYPE_L2DATA:
                index = get_l2_index(schemata, res_id);
                break;
        case RESCTRL_SCHEMATA_TYPE_L3:
        case RESCTRL_SCHEMATA_TYPE_L3CODE:
        case RESCTRL_SCHEMATA_TYPE_L3DATA:
                index = get_l3_index(schemata, res_id);
                break;
        case RESCTRL_SCHEMATA_TYPE_MB:
                index = get_mba_index(schemata, res_id);
                break;
        case RESCTRL_SCHEMATA_TYPE_SMBA:
                index = get_smba_index(schemata, res_id);
                break;
        }

        if (index < 0)
                return PQOS_RETVAL_ERROR;

        switch (type) {
        case RESCTRL_SCHEMATA_TYPE_L2:
                schemata->l2ca[index].cdp = 0;
                schemata->l2ca[index].u.ways_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_L2CODE:
                schemata->l2ca[index].cdp = 1;
                schemata->l2ca[index].u.s.code_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_L2DATA:
                schemata->l2ca[index].cdp = 1;
                schemata->l2ca[index].u.s.data_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_L3:
                schemata->l3ca[index].cdp = 0;
                schemata->l3ca[index].u.ways_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_L3CODE:
                schemata->l3ca[index].cdp = 1;
                schemata->l3ca[index].u.s.code_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_L3DATA:
                schemata->l3ca[index].cdp = 1;
                schemata->l3ca[index].u.s.data_mask = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_MB:
                schemata->mba[index].mb_max = value;
                break;
        case RESCTRL_SCHEMATA_TYPE_SMBA:
                schemata->smba[index].mb_max = value;
                break;
        }

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_read(FILE *fd, struct resctrl_schemata *schemata)
{
        int ret = PQOS_RETVAL_OK;
        int type = RESCTRL_SCHEMATA_TYPE_NONE;
        char *p = NULL, *q = NULL, *saveptr = NULL;
        const size_t buf_size = 16 * 1024;
        char *buf = calloc(buf_size, sizeof(*buf));

        if (buf == NULL) {
                ret = PQOS_RETVAL_ERROR;
                goto resctrl_schemata_read_exit;
        }

        while (fgets(buf, buf_size, fd) != NULL) {
                q = buf;
                /**
                 * Trim white spaces
                 */
                while (isspace(*q))
                        q++;

                /**
                 * Determine allocation type
                 */
                p = strchr(q, ':');
                if (p == NULL) {
                        ret = PQOS_RETVAL_ERROR;
                        break;
                }
                *p = '\0';
                type = resctrl_schemata_type_get(q);

                /* Skip unknown label */
                if (type == RESCTRL_SCHEMATA_TYPE_NONE)
                        continue;

                /**
                 * Parse COS masks
                 */
                for (++p;; p = NULL) {
                        char *token = NULL;
                        uint64_t id = 0;
                        uint64_t value = 0;
                        unsigned base = ((type == RESCTRL_SCHEMATA_TYPE_MB ||
                                          type == RESCTRL_SCHEMATA_TYPE_SMBA)
                                             ? 10
                                             : 16);

                        token = strtok_r(p, ";", &saveptr);
                        if (token == NULL)
                                break;

                        q = strchr(token, '=');
                        if (q == NULL) {
                                ret = PQOS_RETVAL_ERROR;
                                goto resctrl_schemata_read_exit;
                        }
                        *q = '\0';

                        ret = resctrl_utils_strtouint64(token, 10, &id);
                        if (ret != PQOS_RETVAL_OK)
                                goto resctrl_schemata_read_exit;

                        ret = resctrl_utils_strtouint64(q + 1, base, &value);
                        if (ret != PQOS_RETVAL_OK)
                                goto resctrl_schemata_read_exit;

                        ret = resctrl_schemata_set(id, value, type, schemata);
                        if (ret != PQOS_RETVAL_OK)
                                goto resctrl_schemata_read_exit;
                }
        }

resctrl_schemata_read_exit:
        if (buf != NULL)
                free(buf);

        return ret;
}

int
resctrl_schemata_write(FILE *fd, const struct resctrl_schemata *schemata)
{
        resctrl_schemata_l2ca_write(fd, schemata);
        resctrl_schemata_l3ca_write(fd, schemata);
        resctrl_schemata_mba_write(fd, schemata);
        resctrl_schemata_smba_write(fd, schemata);

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_l3ca_write(FILE *fd, const struct resctrl_schemata *schemata)
{
        unsigned i;

        if (schemata->l3ca == NULL)
                return PQOS_RETVAL_OK;

        /* L3 without CDP */
        if (!schemata->l3ca[0].cdp) {
                fprintf(fd, "L3:");
                for (i = 0; i < schemata->l3ids_num; i++) {
                        unsigned id = schemata->l3ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l3ca[i].u.ways_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\n");
        }

        /* L3 with CDP */
        if (schemata->l3ca[0].cdp) {
                fprintf(fd, "L3CODE:");
                for (i = 0; i < schemata->l3ids_num; i++) {
                        unsigned id = schemata->l3ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l3ca[i].u.s.code_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\nL3DATA:");
                for (i = 0; i < schemata->l3ids_num; i++) {
                        unsigned id = schemata->l3ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l3ca[i].u.s.data_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\n");
        }

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_l2ca_write(FILE *fd, const struct resctrl_schemata *schemata)
{
        unsigned i;

        if (schemata->l2ca == NULL)
                return PQOS_RETVAL_OK;

        /* L2 without CDP */
        if (!schemata->l2ca[0].cdp) {
                fprintf(fd, "L2:");
                for (i = 0; i < schemata->l2ids_num; i++) {
                        unsigned id = schemata->l2ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l2ca[i].u.ways_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\n");
        }

        /* L2 with CDP */
        if (schemata->l2ca[0].cdp) {
                fprintf(fd, "L2CODE:");
                for (i = 0; i < schemata->l2ids_num; i++) {
                        unsigned id = schemata->l2ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l2ca[i].u.s.code_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\nL2DATA:");
                for (i = 0; i < schemata->l2ids_num; i++) {
                        unsigned id = schemata->l2ids[i];
                        unsigned long long mask =
                            (unsigned long long)schemata->l2ca[i].u.s.data_mask;

                        if (i > 0)
                                fprintf(fd, ";");
                        fprintf(fd, "%u=%llx", id, mask);
                }
                fprintf(fd, "\n");
        }

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_mba_write(FILE *fd, const struct resctrl_schemata *schemata)
{
        unsigned i;
        int separator = 0;

        if (schemata->mba == NULL)
                return PQOS_RETVAL_OK;

        fprintf(fd, "MB:");
        for (i = 0; i < schemata->mbaids_num; i++) {
                unsigned id = schemata->mbaids[i];
                unsigned value = schemata->mba[i].mb_max;

                /* Do not rewrite default value it will be rounded up to
                 * MBA granularity */
                if (value == UINT32_MAX)
                        continue;

                if (separator)
                        fprintf(fd, ";");
                separator = 1;
                fprintf(fd, "%u=%u", id, value);
        }
        fprintf(fd, "\n");

        return PQOS_RETVAL_OK;
}

int
resctrl_schemata_smba_write(FILE *fd, const struct resctrl_schemata *schemata)
{
        unsigned i;

        if (schemata->smba == NULL)
                return PQOS_RETVAL_OK;

        fprintf(fd, "SMBA:");
        for (i = 0; i < schemata->smbaids_num; i++) {
                unsigned id = schemata->smbaids[i];

                if (i > 0)
                        fprintf(fd, ";");
                fprintf(fd, "%u=%u", id, schemata->smba[i].mb_max);
        }
        fprintf(fd, "\n");

        return PQOS_RETVAL_OK;
}