Skip to content

Commit 7c74d2b

Browse files
watologo1Dominik Brodowski
authored andcommitted
cpupower: Better detect offlined CPUs
Before, checking for offlined CPUs was done dirty and it was checked whether topology parsing returned -1 values. But this is a valid case on a Xen (and possibly other) kernels. Do proper online/offline checking, also take CONFIG_HOTPLUG_CPU option into account (no /sys/devices/../cpuX/online file). Signed-off-by: Thomas Renninger <trenn@suse.de> Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
1 parent 88f984e commit 7c74d2b

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎tools/power/cpupower/utils/helpers/helpers.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ struct cpupower_topology {
9696
int pkg;
9797
int core;
9898
int cpu;
99+
100+
/* flags */
101+
unsigned int is_online:1;
99102
} *core_info;
100103
};
101104

‎tools/power/cpupower/utils/helpers/sysfs.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,56 @@ static unsigned int sysfs_write_file(const char *path,
5656
return (unsigned int) numwrite;
5757
}
5858

59+
/*
60+
* Detect whether a CPU is online
61+
*
62+
* Returns:
63+
* 1 -> if CPU is online
64+
* 0 -> if CPU is offline
65+
* negative errno values in error case
66+
*/
67+
int sysfs_is_cpu_online(unsigned int cpu)
68+
{
69+
char path[SYSFS_PATH_MAX];
70+
int fd;
71+
ssize_t numread;
72+
unsigned long long value;
73+
char linebuf[MAX_LINE_LEN];
74+
char *endp;
75+
struct stat statbuf;
76+
77+
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
78+
79+
if (stat(path, &statbuf) != 0)
80+
return 0;
81+
82+
/*
83+
* kernel without CONFIG_HOTPLUG_CPU
84+
* -> cpuX directory exists, but not cpuX/online file
85+
*/
86+
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
87+
if (stat(path, &statbuf) != 0)
88+
return 1;
89+
90+
fd = open(path, O_RDONLY);
91+
if (fd == -1)
92+
return -errno;
93+
94+
numread = read(fd, linebuf, MAX_LINE_LEN - 1);
95+
if (numread < 1) {
96+
close(fd);
97+
return -EIO;
98+
}
99+
linebuf[numread] = '\0';
100+
close(fd);
101+
102+
value = strtoull(linebuf, &endp, 0);
103+
if (value > 1 || value < 0)
104+
return -EINVAL;
105+
106+
return value;
107+
}
108+
59109
/* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
60110

61111
/*

‎tools/power/cpupower/utils/helpers/sysfs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
extern unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen);
99

10+
extern int sysfs_is_cpu_online(unsigned int cpu);
11+
1012
extern unsigned long sysfs_get_idlestate_latency(unsigned int cpu,
1113
unsigned int idlestate);
1214
extern unsigned long sysfs_get_idlestate_usage(unsigned int cpu,

‎tools/power/cpupower/utils/helpers/topology.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct cpuid_core_info {
4141
unsigned int pkg;
4242
unsigned int thread;
4343
unsigned int cpu;
44+
/* flags */
45+
unsigned int is_online:1;
4446
};
4547

4648
static int __compare(const void *t1, const void *t2)
@@ -78,14 +80,15 @@ int get_cpu_topology(struct cpupower_topology *cpu_top)
7880
return -ENOMEM;
7981
cpu_top->pkgs = cpu_top->cores = 0;
8082
for (cpu = 0; cpu < cpus; cpu++) {
83+
cpu_top->core_info[cpu].cpu = cpu;
84+
cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu);
8185
cpu_top->core_info[cpu].pkg =
8286
sysfs_topology_read_file(cpu, "physical_package_id");
8387
if ((int)cpu_top->core_info[cpu].pkg != -1 &&
8488
cpu_top->core_info[cpu].pkg > cpu_top->pkgs)
8589
cpu_top->pkgs = cpu_top->core_info[cpu].pkg;
8690
cpu_top->core_info[cpu].core =
8791
sysfs_topology_read_file(cpu, "core_id");
88-
cpu_top->core_info[cpu].cpu = cpu;
8992
}
9093
cpu_top->pkgs++;
9194

‎tools/power/cpupower/utils/idle_monitor/cpupower-monitor.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,13 @@ void print_results(int topology_depth, int cpu)
190190
}
191191
}
192192
}
193-
/* cpu offline */
194-
if (cpu_top.core_info[cpu].pkg == -1 ||
195-
cpu_top.core_info[cpu].core == -1) {
193+
/*
194+
* The monitor could still provide useful data, for example
195+
* AMD HW counters partly sit in PCI config space.
196+
* It's up to the monitor plug-in to check .is_online, this one
197+
* is just for additional info.
198+
*/
199+
if (!cpu_top.core_info[cpu].is_online) {
196200
printf(_(" *is offline\n"));
197201
return;
198202
} else

0 commit comments

Comments
 (0)