2009-11-16 15:22:34 +00:00
|
|
|
/*
|
2012-04-02 04:26:44 +00:00
|
|
|
* Copyright (C) 2009-2010, 2012 Red Hat, Inc.
|
2009-11-16 15:22:34 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-11-16 15:22:34 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2010-05-03 21:00:43 +00:00
|
|
|
#include <sched.h>
|
2009-11-16 15:22:34 +00:00
|
|
|
|
|
|
|
#include "processinfo.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
#if HAVE_SCHED_GETAFFINITY
|
|
|
|
|
2012-09-14 07:46:59 +00:00
|
|
|
int virProcessInfoSetAffinity(pid_t pid, virBitmapPtr map)
|
2009-11-16 15:22:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2012-09-14 07:46:59 +00:00
|
|
|
bool set = false;
|
2010-03-09 18:22:22 +00:00
|
|
|
# ifdef CPU_ALLOC
|
2009-11-16 16:08:29 +00:00
|
|
|
/* New method dynamically allocates cpu mask, allowing unlimted cpus */
|
|
|
|
int numcpus = 1024;
|
|
|
|
size_t masklen;
|
|
|
|
cpu_set_t *mask;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
if (!mask) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-11-16 16:08:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPU_ZERO_S(masklen, mask);
|
2012-09-14 07:46:59 +00:00
|
|
|
for (i = 0 ; i < virBitmapSize(map); i++) {
|
|
|
|
if (virBitmapGetBit(map, i, &set) < 0)
|
|
|
|
return -1;
|
|
|
|
if (set)
|
2009-11-16 16:08:29 +00:00
|
|
|
CPU_SET_S(i, masklen, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setaffinity(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;
|
|
|
|
}
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-11-16 16:08:29 +00:00
|
|
|
_("cannot set CPU affinity on process %d"), pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CPU_FREE(mask);
|
2010-03-09 18:22:22 +00:00
|
|
|
# else
|
2009-11-16 16:08:29 +00:00
|
|
|
/* Legacy method uses a fixed size cpu mask, only allows upto 1024 cpus */
|
2009-11-16 15:22:34 +00:00
|
|
|
cpu_set_t mask;
|
|
|
|
|
|
|
|
CPU_ZERO(&mask);
|
2012-09-14 07:46:59 +00:00
|
|
|
for (i = 0 ; i < virBitmapSize(map); i++) {
|
|
|
|
if (virBitmapGetBit(map, i, &set) < 0)
|
|
|
|
return -1;
|
|
|
|
if (set)
|
2009-11-16 15:22:34 +00:00
|
|
|
CPU_SET(i, &mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setaffinity(pid, sizeof(mask), &mask) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-11-16 15:22:34 +00:00
|
|
|
_("cannot set CPU affinity on process %d"), pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-09 18:22:22 +00:00
|
|
|
# endif
|
2009-11-16 15:22:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virProcessInfoGetAffinity(pid_t pid,
|
2012-09-14 07:46:59 +00:00
|
|
|
virBitmapPtr *map,
|
2009-11-16 15:22:34 +00:00
|
|
|
int maxcpu)
|
|
|
|
{
|
|
|
|
int i;
|
2010-03-09 18:22:22 +00:00
|
|
|
# ifdef CPU_ALLOC
|
2009-11-16 16:08:29 +00:00
|
|
|
/* New method dynamically allocates cpu mask, allowing unlimted cpus */
|
|
|
|
int numcpus = 1024;
|
|
|
|
size_t masklen;
|
|
|
|
cpu_set_t *mask;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
if (!mask) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-11-16 16:08:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPU_ZERO_S(masklen, mask);
|
|
|
|
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;
|
|
|
|
}
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2010-03-09 13:59:01 +00:00
|
|
|
_("cannot get CPU affinity of process %d"), pid);
|
2009-11-16 16:08:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-14 07:46:59 +00:00
|
|
|
*map = virBitmapNew(maxcpu);
|
|
|
|
if (!map) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-16 16:08:29 +00:00
|
|
|
for (i = 0 ; i < maxcpu ; i++)
|
|
|
|
if (CPU_ISSET_S(i, masklen, mask))
|
2012-09-14 07:46:59 +00:00
|
|
|
ignore_value(virBitmapSetBit(*map, i));
|
2012-04-02 04:26:44 +00:00
|
|
|
CPU_FREE(mask);
|
2010-03-09 18:22:22 +00:00
|
|
|
# else
|
2009-11-16 16:08:29 +00:00
|
|
|
/* Legacy method uses a fixed size cpu mask, only allows upto 1024 cpus */
|
2009-11-16 15:22:34 +00:00
|
|
|
cpu_set_t mask;
|
|
|
|
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2010-03-09 13:59:01 +00:00
|
|
|
_("cannot get CPU affinity of process %d"), pid);
|
2009-11-16 15:22:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < maxcpu ; i++)
|
|
|
|
if (CPU_ISSET(i, &mask))
|
|
|
|
VIR_USE_CPU(map, i);
|
2010-03-09 18:22:22 +00:00
|
|
|
# endif
|
2009-11-16 15:22:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_SCHED_GETAFFINITY */
|
|
|
|
|
|
|
|
int virProcessInfoSetAffinity(pid_t pid ATTRIBUTE_UNUSED,
|
2012-09-14 07:46:59 +00:00
|
|
|
virBitmapPtr map ATTRIBUTE_UNUSED)
|
2009-11-16 15:22:34 +00:00
|
|
|
{
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(ENOSYS, "%s",
|
2009-11-16 15:22:34 +00:00
|
|
|
_("Process CPU affinity is not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virProcessInfoGetAffinity(pid_t pid ATTRIBUTE_UNUSED,
|
2012-09-14 07:46:59 +00:00
|
|
|
virBitmapPtr *map ATTRIBUTE_UNUSED,
|
2009-11-16 15:22:34 +00:00
|
|
|
int maxcpu ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(ENOSYS, "%s",
|
2009-11-16 15:22:34 +00:00
|
|
|
_("Process CPU affinity is not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SCHED_GETAFFINITY */
|