Skip to content

Commit 8c3dd2c

Browse files
committed
tools/power/turbostat: Abstrct function for parsing cpu string
Abstract parse_cpu_str() which can update any specified cpu_set by a given cpu string. This can be used to handle further CPU limitations from other sources like cgroup. The cpu string parsing code is also enhanced to handle the strings that have an extra '\n' before string terminator. Signed-off-by: Zhang Rui <[email protected]>
1 parent c25ef0e commit 8c3dd2c

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,59 @@ int get_physical_node_id(struct cpu_topology *thiscpu)
35653565
return -1;
35663566
}
35673567

3568+
static int parse_cpu_str(char *cpu_str, cpu_set_t *cpu_set, int cpu_set_size)
3569+
{
3570+
unsigned int start, end;
3571+
char *next = cpu_str;
3572+
3573+
while (next && *next) {
3574+
3575+
if (*next == '-') /* no negative cpu numbers */
3576+
return 1;
3577+
3578+
start = strtoul(next, &next, 10);
3579+
3580+
if (start >= CPU_SUBSET_MAXCPUS)
3581+
return 1;
3582+
CPU_SET_S(start, cpu_set_size, cpu_set);
3583+
3584+
if (*next == '\0' || *next == '\n')
3585+
break;
3586+
3587+
if (*next == ',') {
3588+
next += 1;
3589+
continue;
3590+
}
3591+
3592+
if (*next == '-') {
3593+
next += 1; /* start range */
3594+
} else if (*next == '.') {
3595+
next += 1;
3596+
if (*next == '.')
3597+
next += 1; /* start range */
3598+
else
3599+
return 1;
3600+
}
3601+
3602+
end = strtoul(next, &next, 10);
3603+
if (end <= start)
3604+
return 1;
3605+
3606+
while (++start <= end) {
3607+
if (start >= CPU_SUBSET_MAXCPUS)
3608+
return 1;
3609+
CPU_SET_S(start, cpu_set_size, cpu_set);
3610+
}
3611+
3612+
if (*next == ',')
3613+
next += 1;
3614+
else if (*next != '\0' && *next != '\n')
3615+
return 1;
3616+
}
3617+
3618+
return 0;
3619+
}
3620+
35683621
int get_thread_siblings(struct cpu_topology *thiscpu)
35693622
{
35703623
char path[80], character;
@@ -6384,9 +6437,6 @@ void probe_sysfs(void)
63846437
*/
63856438
void parse_cpu_command(char *optarg)
63866439
{
6387-
unsigned int start, end;
6388-
char *next;
6389-
63906440
if (!strcmp(optarg, "core")) {
63916441
if (cpu_subset)
63926442
goto error;
@@ -6409,52 +6459,8 @@ void parse_cpu_command(char *optarg)
64096459

64106460
CPU_ZERO_S(cpu_subset_size, cpu_subset);
64116461

6412-
next = optarg;
6413-
6414-
while (next && *next) {
6415-
6416-
if (*next == '-') /* no negative cpu numbers */
6417-
goto error;
6418-
6419-
start = strtoul(next, &next, 10);
6420-
6421-
if (start >= CPU_SUBSET_MAXCPUS)
6422-
goto error;
6423-
CPU_SET_S(start, cpu_subset_size, cpu_subset);
6424-
6425-
if (*next == '\0')
6426-
break;
6427-
6428-
if (*next == ',') {
6429-
next += 1;
6430-
continue;
6431-
}
6432-
6433-
if (*next == '-') {
6434-
next += 1; /* start range */
6435-
} else if (*next == '.') {
6436-
next += 1;
6437-
if (*next == '.')
6438-
next += 1; /* start range */
6439-
else
6440-
goto error;
6441-
}
6442-
6443-
end = strtoul(next, &next, 10);
6444-
if (end <= start)
6445-
goto error;
6446-
6447-
while (++start <= end) {
6448-
if (start >= CPU_SUBSET_MAXCPUS)
6449-
goto error;
6450-
CPU_SET_S(start, cpu_subset_size, cpu_subset);
6451-
}
6452-
6453-
if (*next == ',')
6454-
next += 1;
6455-
else if (*next != '\0')
6456-
goto error;
6457-
}
6462+
if (parse_cpu_str(optarg, cpu_subset, cpu_subset_size))
6463+
goto error;
64586464

64596465
return;
64606466

0 commit comments

Comments
 (0)