processinfo.c 5.3 KB
Newer Older
1
/*
2
 * Copyright (C) 2009-2010, 2012 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * License along with this library.  If not, see
O
Osier Yang 已提交
16
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23
 *
 * Authors:
 *     Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

24
#include <stdlib.h>
E
Eric Blake 已提交
25
#include <sched.h>
26 27 28 29 30 31 32 33

#include "processinfo.h"
#include "virterror_internal.h"

#define VIR_FROM_THIS VIR_FROM_NONE

#if HAVE_SCHED_GETAFFINITY

34
int virProcessInfoSetAffinity(pid_t pid, virBitmapPtr map)
35 36
{
    int i;
37
    bool set = false;
38
# ifdef CPU_ALLOC
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    /* 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) {
56
        virReportOOMError();
57 58 59 60
        return -1;
    }

    CPU_ZERO_S(masklen, mask);
61 62 63 64
    for (i = 0 ; i < virBitmapSize(map); i++) {
        if (virBitmapGetBit(map, i, &set) < 0)
            return -1;
        if (set)
65 66 67 68 69 70 71 72 73 74
            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;
        }
75
        virReportSystemError(errno,
76 77 78 79
                             _("cannot set CPU affinity on process %d"), pid);
        return -1;
    }
    CPU_FREE(mask);
80
# else
J
Ján Tomko 已提交
81
    /* Legacy method uses a fixed size cpu mask, only allows up to 1024 cpus */
82 83 84
    cpu_set_t mask;

    CPU_ZERO(&mask);
85 86 87 88
    for (i = 0 ; i < virBitmapSize(map); i++) {
        if (virBitmapGetBit(map, i, &set) < 0)
            return -1;
        if (set)
89 90 91 92
            CPU_SET(i, &mask);
    }

    if (sched_setaffinity(pid, sizeof(mask), &mask) < 0) {
93
        virReportSystemError(errno,
94 95 96
                             _("cannot set CPU affinity on process %d"), pid);
        return -1;
    }
97
# endif
98 99 100 101 102

    return 0;
}

int virProcessInfoGetAffinity(pid_t pid,
103
                              virBitmapPtr *map,
104 105 106
                              int maxcpu)
{
    int i;
107
# ifdef CPU_ALLOC
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /* 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) {
125
        virReportOOMError();
126 127 128 129 130 131 132 133 134 135 136
        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;
        }
137
        virReportSystemError(errno,
138
                             _("cannot get CPU affinity of process %d"), pid);
139 140 141
        return -1;
    }

142 143 144 145 146 147
    *map = virBitmapNew(maxcpu);
    if (!map) {
        virReportOOMError();
        return -1;
    }

148 149
    for (i = 0 ; i < maxcpu ; i++)
        if (CPU_ISSET_S(i, masklen, mask))
150
            ignore_value(virBitmapSetBit(*map, i));
151
    CPU_FREE(mask);
152
# else
J
Ján Tomko 已提交
153
    /* Legacy method uses a fixed size cpu mask, only allows up to 1024 cpus */
154 155 156 157
    cpu_set_t mask;

    CPU_ZERO(&mask);
    if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) {
158
        virReportSystemError(errno,
159
                             _("cannot get CPU affinity of process %d"), pid);
160 161 162 163 164
        return -1;
    }

    for (i = 0 ; i < maxcpu ; i++)
        if (CPU_ISSET(i, &mask))
165
            ignore_value(virBitmapSetBit(*map, i));
166
# endif
167 168 169 170 171 172 173

    return 0;
}

#else /* HAVE_SCHED_GETAFFINITY */

int virProcessInfoSetAffinity(pid_t pid ATTRIBUTE_UNUSED,
174
                              virBitmapPtr map ATTRIBUTE_UNUSED)
175
{
176
    virReportSystemError(ENOSYS, "%s",
177 178 179 180 181
                         _("Process CPU affinity is not supported on this platform"));
    return -1;
}

int virProcessInfoGetAffinity(pid_t pid ATTRIBUTE_UNUSED,
182
                              virBitmapPtr *map ATTRIBUTE_UNUSED,
183 184
                              int maxcpu ATTRIBUTE_UNUSED)
{
185
    virReportSystemError(ENOSYS, "%s",
186 187 188 189
                         _("Process CPU affinity is not supported on this platform"));
    return -1;
}
#endif /* HAVE_SCHED_GETAFFINITY */