return 0;
}
-int virProcessGetAffinity(pid_t pid,
- virBitmapPtr *map,
- int maxcpu)
+virBitmapPtr
+virProcessGetAffinity(pid_t pid)
{
size_t i;
-# ifdef CPU_ALLOC
- /* New method dynamically allocates cpu mask, allowing unlimted cpus */
- int numcpus = 1024;
- size_t masklen;
cpu_set_t *mask;
+ size_t masklen;
+ virBitmapPtr ret = NULL;
- /* Not only may the statically allocated cpu_set_t be too small,
- * but there is no way to ask the kernel what size is large enough.
- * So you have no option but to pick a size, try, catch EINVAL,
- * enlarge, and re-try.
- *
- * http://lkml.org/lkml/2009/7/28/620
- */
- realloc:
- masklen = CPU_ALLOC_SIZE(numcpus);
- mask = CPU_ALLOC(numcpus);
+# ifdef CPU_ALLOC
+ /* 262144 cpus ought to be enough for anyone */
+ masklen = CPU_ALLOC_SIZE(1024 << 8);
+ mask = CPU_ALLOC(1024 << 8);
if (!mask) {
virReportOOMError();
- return -1;
+ return NULL;
}
CPU_ZERO_S(masklen, mask);
+# else
+ if (VIR_ALLOC(mask) < 0)
+ return NULL;
+
+ masklen = sizeof(*mask);
+ CPU_ZERO(mask);
+# endif
+
if (sched_getaffinity(pid, masklen, mask) < 0) {
- CPU_FREE(mask);
- if (errno == EINVAL &&
- numcpus < (1024 << 8)) { /* 262144 cpus ought to be enough for anyone */
- numcpus = numcpus << 2;
- goto realloc;
- }
virReportSystemError(errno,
_("cannot get CPU affinity of process %d"), pid);
- return -1;
+ goto cleanup;
}
- *map = virBitmapNew(maxcpu);
- if (!*map)
- return -1;
+ if (!(ret = virBitmapNew(masklen * 8)))
+ goto cleanup;
- for (i = 0; i < maxcpu; i++)
+ for (i = 0; i < masklen * 8; i++) {
+# ifdef CPU_ALLOC
if (CPU_ISSET_S(i, masklen, mask))
- ignore_value(virBitmapSetBit(*map, i));
- CPU_FREE(mask);
+ ignore_value(virBitmapSetBit(ret, i));
# else
- /* Legacy method uses a fixed size cpu mask, only allows up to 1024 cpus */
- cpu_set_t mask;
-
- CPU_ZERO(&mask);
- if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) {
- virReportSystemError(errno,
- _("cannot get CPU affinity of process %d"), pid);
- return -1;
+ if (CPU_ISSET(i, mask))
+ ignore_value(virBitmapSetBit(ret, i));
+# endif
}
- for (i = 0; i < maxcpu; i++)
- if (CPU_ISSET(i, &mask))
- ignore_value(virBitmapSetBit(*map, i));
+ cleanup:
+# ifdef CPU_ALLOC
+ CPU_FREE(mask);
+# else
+ VIR_FREE(mask);
# endif
- return 0;
+ return ret;
}
#elif defined(HAVE_BSD_CPU_AFFINITY)
return 0;
}
-int virProcessGetAffinity(pid_t pid,
- virBitmapPtr *map,
- int maxcpu)
+virBitmapPtr
+virProcessGetAffinity(pid_t pid)
{
size_t i;
cpuset_t mask;
-
- if (!(*map = virBitmapNew(maxcpu)))
- return -1;
+ virBitmapPtr ret = NULL;
CPU_ZERO(&mask);
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
sizeof(mask), &mask) != 0) {
virReportSystemError(errno,
_("cannot get CPU affinity of process %d"), pid);
- return -1;
+ return NULL;
}
- for (i = 0; i < maxcpu; i++)
+ if (!(*map = virBitmapNew(sizeof(mask) * 8)))
+ return NULL;
+
+ for (i = 0; i < sizeof(mask) * 8; i++)
if (CPU_ISSET(i, &mask))
- ignore_value(virBitmapSetBit(*map, i));
+ ignore_value(virBitmapSetBit(ret, i));
- return 0;
+ return ret;
}
#else /* HAVE_SCHED_GETAFFINITY */
return -1;
}
-int virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED,
- virBitmapPtr *map ATTRIBUTE_UNUSED,
- int maxcpu ATTRIBUTE_UNUSED)
+virBitmapPtr
+virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Process CPU affinity is not supported on this platform"));
- return -1;
+ return NULL;
}
#endif /* HAVE_SCHED_GETAFFINITY */