aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMahircan Gul <mahircg@mpi-sws.org>2016-07-18 10:05:20 -0400
committerBjoern Brandenburg <bbb@mpi-sws.org>2016-07-18 10:46:23 -0400
commitcf1d1f6e4b1138a5a841a47a2cba4aacdcaf0d72 (patch)
tree0f78c6a6a131700800c2f9dc7bf4768a775857e9 /src
parent11a2acc387b274d2323104272e585a7059b96f4f (diff)
Add unit-test for CPU mapping parsing
- Decompose read_mapping into two functions: *read_mapping* which reads CPU mapping data as string from file, and *set_mapping* that actually sets the cpu_set_t according to the mapping - Add unit test for setting cpu_set_t to various CPU indexes
Diffstat (limited to 'src')
-rw-r--r--src/migration.c67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/migration.c b/src/migration.c
index c1e5a80..efd8d81 100644
--- a/src/migration.c
+++ b/src/migration.c
@@ -27,43 +27,17 @@ int num_online_cpus()
27 return sysconf(_SC_NPROCESSORS_ONLN); 27 return sysconf(_SC_NPROCESSORS_ONLN);
28} 28}
29 29
30static int read_mapping(int idx, const char* which, cpu_set_t** set, size_t *sz) 30void set_mapping(char* buf, int len, cpu_set_t** set, size_t *sz)
31{ 31{
32 /* Max CPUs = 4096 */ 32 int nbits;
33 33 char *chunk_str;
34 int ret = -1;
35 char buf[4096/4 /* enough chars for hex data (4 CPUs per char) */
36 + 4096/(4*8) /* for commas (separate groups of 8 chars) */
37 + 1] = {0}; /* for \0 */
38 char fname[80] = {0};
39
40 char* chunk_str;
41 int len, nbits;
42 int i; 34 int i;
43 35
44 /* init vals returned to callee */ 36 /* init vals returned to callee */
45 *set = NULL; 37 *set = NULL;
46 *sz = 0; 38 *sz = 0;
47 39
48 if (num_online_cpus() > 4096)
49 goto out;
50
51 /* Read string is in the format of <mask>[,<mask>]*. All <mask>s following
52 a comma are 8 chars (representing a 32-bit mask). The first <mask> may
53 have fewer chars. Bits are MSB to LSB, left to right. */
54 snprintf(fname, sizeof(fname), "/proc/litmus/%s/%d", which, idx);
55 ret = read_file(fname, &buf, sizeof(buf)-1);
56 if (ret <= 0)
57 goto out;
58
59 len = strnlen(buf, sizeof(buf));
60 /* if there is, omit newline at the end of string */
61 if (buf[len-1] == '\n') {
62 buf[len-1] = '\0';
63 len -= 1;
64 }
65 nbits = 32*(len/9) + 4*(len%9); /* compute bits, accounting for commas */ 40 nbits = 32*(len/9) + 4*(len%9); /* compute bits, accounting for commas */
66
67 *set = CPU_ALLOC(nbits); 41 *set = CPU_ALLOC(nbits);
68 *sz = CPU_ALLOC_SIZE(nbits); 42 *sz = CPU_ALLOC_SIZE(nbits);
69 CPU_ZERO_S(*sz, *set); 43 CPU_ZERO_S(*sz, *set);
@@ -74,7 +48,7 @@ static int read_mapping(int idx, const char* which, cpu_set_t** set, size_t *sz)
74 do { 48 do {
75 unsigned long chunk; 49 unsigned long chunk;
76 /* since strtoul stops processing the string with occurrence of 50 /* since strtoul stops processing the string with occurrence of
77 first non-digit character, it is necessary to read 8-bytes 51 first non-digit character, it is necessary to read 8-bytes
78 on first iteration for ignoring the leading comma*/ 52 on first iteration for ignoring the leading comma*/
79 chunk_str -= (9 + ((i == 0) ? -1 : 0)); 53 chunk_str -= (9 + ((i == 0) ? -1 : 0));
80 if (chunk_str < buf) 54 if (chunk_str < buf)
@@ -88,6 +62,39 @@ static int read_mapping(int idx, const char* which, cpu_set_t** set, size_t *sz)
88 } 62 }
89 i += 1; 63 i += 1;
90 } while (chunk_str > buf); 64 } while (chunk_str > buf);
65}
66
67static int read_mapping(int idx, const char* which, cpu_set_t** set, size_t *sz)
68{
69 /* Max CPUs = 4096 */
70
71 int ret = -1;
72 char buf[4096/4 /* enough chars for hex data (4 CPUs per char) */
73 + 4096/(4*8) /* for commas (separate groups of 8 chars) */
74 + 1] = {0}; /* for \0 */
75 char fname[80] = {0};
76
77 int len;
78
79 if (num_online_cpus() > 4096)
80 goto out;
81
82 /* Read string is in the format of <mask>[,<mask>]*. All <mask>s following
83 a comma are 8 chars (representing a 32-bit mask). The first <mask> may
84 have fewer chars. Bits are MSB to LSB, left to right. */
85 snprintf(fname, sizeof(fname), "/proc/litmus/%s/%d", which, idx);
86 ret = read_file(fname, &buf, sizeof(buf)-1);
87 if (ret <= 0)
88 goto out;
89
90 len = strnlen(buf, sizeof(buf));
91 /* if there is, omit newline at the end of string */
92 if (buf[len-1] == '\n') {
93 buf[len-1] = '\0';
94 len -= 1;
95 }
96
97 set_mapping(buf, len, set, sz);
91 98
92 ret = 0; 99 ret = 0;
93 100