Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 #include <bpf/btf.h>
0004 #include "bpf/libbpf_internal.h"
0005 
0006 static int duration = 0;
0007 
0008 static void validate_mask(int case_nr, const char *exp, bool *mask, int n)
0009 {
0010     int i;
0011 
0012     for (i = 0; exp[i]; i++) {
0013         if (exp[i] == '1') {
0014             if (CHECK(i + 1 > n, "mask_short",
0015                   "case #%d: mask too short, got n=%d, need at least %d\n",
0016                   case_nr, n, i + 1))
0017                 return;
0018             CHECK(!mask[i], "cpu_not_set",
0019                   "case #%d: mask differs, expected cpu#%d SET\n",
0020                   case_nr, i);
0021         } else {
0022             CHECK(i < n && mask[i], "cpu_set",
0023                   "case #%d: mask differs, expected cpu#%d UNSET\n",
0024                   case_nr, i);
0025         }
0026     }
0027     CHECK(i < n, "mask_long",
0028           "case #%d: mask too long, got n=%d, expected at most %d\n",
0029           case_nr, n, i);
0030 }
0031 
0032 static struct {
0033     const char *cpu_mask;
0034     const char *expect;
0035     bool fails;
0036 } test_cases[] = {
0037     { "0\n", "1", false },
0038     { "0,2\n", "101", false },
0039     { "0-2\n", "111", false },
0040     { "0-2,3-4\n", "11111", false },
0041     { "0", "1", false },
0042     { "0-2", "111", false },
0043     { "0,2", "101", false },
0044     { "0,1-3", "1111", false },
0045     { "0,1,2,3", "1111", false },
0046     { "0,2-3,5", "101101", false },
0047     { "3-3", "0001", false },
0048     { "2-4,6,9-10", "00111010011", false },
0049     /* failure cases */
0050     { "", "", true },
0051     { "0-", "", true },
0052     { "0 ", "", true },
0053     { "0_1", "", true },
0054     { "1-0", "", true },
0055     { "-1", "", true },
0056 };
0057 
0058 void test_cpu_mask()
0059 {
0060     int i, err, n;
0061     bool *mask;
0062 
0063     for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
0064         mask = NULL;
0065         err = parse_cpu_mask_str(test_cases[i].cpu_mask, &mask, &n);
0066         if (test_cases[i].fails) {
0067             CHECK(!err, "should_fail",
0068                   "case #%d: parsing should fail!\n", i + 1);
0069         } else {
0070             if (CHECK(err, "parse_err",
0071                   "case #%d: cpu mask parsing failed: %d\n",
0072                   i + 1, err))
0073                 continue;
0074             validate_mask(i + 1, test_cases[i].expect, mask, n);
0075         }
0076         free(mask);
0077     }
0078 }