return ret;
}
+
+/* Determine the maximum cpu id from a Linux sysfs cpu/present file. */
+static int
+linuxParseCPUmax(const char *path)
+{
+ char *str = NULL;
+ char *tmp;
+ int ret = -1;
+
+ if (virFileReadAll(path, 5 * VIR_DOMAIN_CPUMASK_LEN, &str) < 0) {
+ virReportOOMError();
+ goto cleanup;
+ }
+
+ tmp = str;
+ do {
+ if (virStrToLong_i(tmp, &tmp, 10, &ret) < 0 ||
+ !strchr(",-\n", *tmp)) {
+ virReportError(VIR_ERR_NO_SUPPORT,
+ _("failed to parse %s"), path);
+ ret = -1;
+ goto cleanup;
+ }
+ } while (*tmp++ != '\n');
+ ret++;
+
+cleanup:
+ VIR_FREE(str);
+ return ret;
+}
+
/*
- * Linux maintains cpu bit map. For example, if cpuid=5's flag is not set
- * and max cpu is 7. The map file shows 0-4,6-7. This function parses
- * it and returns cpumap.
+ * Linux maintains cpu bit map under cpu/online. For example, if
+ * cpuid=5's flag is not set and max cpu is 7, the map file shows
+ * 0-4,6-7. This function parses it and returns cpumap.
*/
static virBitmapPtr
-linuxParseCPUmap(int *max_cpuid, const char *path)
+linuxParseCPUmap(int max_cpuid, const char *path)
{
virBitmapPtr map = NULL;
char *str = NULL;
- int max_id = 0, i;
if (virFileReadAll(path, 5 * VIR_DOMAIN_CPUMASK_LEN, &str) < 0) {
virReportOOMError();
goto error;
}
- if (virBitmapParse(str, 0, &map,
- VIR_DOMAIN_CPUMASK_LEN) < 0) {
+ if (virBitmapParse(str, 0, &map, max_cpuid) < 0)
goto error;
- }
-
- i = -1;
- while ((i = virBitmapNextSetBit(map, i)) >= 0) {
- max_id = i;
- }
-
- *max_cpuid = max_id;
VIR_FREE(str);
return map;
}
virBitmapPtr
-nodeGetCPUmap(virConnectPtr conn ATTRIBUTE_UNUSED,
- int *max_id ATTRIBUTE_UNUSED,
- const char *mapname ATTRIBUTE_UNUSED)
+nodeGetCPUBitmap(virConnectPtr conn ATTRIBUTE_UNUSED,
+ int *max_id ATTRIBUTE_UNUSED)
{
#ifdef __linux__
- char *path;
virBitmapPtr cpumap;
-
- if (virAsprintf(&path, SYSFS_SYSTEM_PATH "/cpu/%s", mapname) < 0) {
- virReportOOMError();
+ int present;
+
+ present = linuxParseCPUmax(SYSFS_SYSTEM_PATH "/cpu/present");
+ /* XXX should we also work on older kernels, like RHEL5, that lack
+ * cpu/present and cpu/online files? Those kernels also lack cpu
+ * hotplugging, so it would be a matter of finding the largest
+ * cpu/cpuNN directory, and creating a map that size with all bits
+ * set. */
+ if (present < 0)
return NULL;
- }
-
- cpumap = linuxParseCPUmap(max_id, path);
- VIR_FREE(path);
+ cpumap = linuxParseCPUmap(present, SYSFS_SYSTEM_PATH "/cpu/online");
+ if (max_id && cpumap)
+ *max_id = present;
return cpumap;
#else
virReportError(VIR_ERR_NO_SUPPORT, "%s",
if (nparams == 0 && ncpus != 0)
return QEMU_NB_PER_CPU_STAT_PARAM;
- /* To parse account file, we need "present" cpu map. */
- map = nodeGetCPUmap(domain->conn, &max_id, "present");
+ /* To parse account file, we need bitmap of online cpus. */
+ map = nodeGetCPUBitmap(domain->conn, &max_id);
if (!map)
return rv;
goto cleanup;
/* Check that the mapping of online cpus didn't change mid-parse. */
- map2 = nodeGetCPUmap(domain->conn, &max_id, "present");
+ map2 = nodeGetCPUBitmap(domain->conn, &max_id);
if (!map2 || !virBitmapEqual(map, map2)) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
_("the set of online cpus changed while reading"));