libuv-svace-build
154 строки · 4.1 Кб
1/* Copyright libuv project contributors. All rights reserved.
2*
3* Permission is hereby granted, free of charge, to any person obtaining a copy
4* of this software and associated documentation files (the "Software"), to
5* deal in the Software without restriction, including without limitation the
6* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7* sell copies of the Software, and to permit persons to whom the Software is
8* furnished to do so, subject to the following conditions:
9*
10* The above copyright notice and this permission notice shall be included in
11* all copies or substantial portions of the Software.
12*
13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19* IN THE SOFTWARE.
20*/
21
22#include "uv.h"23#include "task.h"24
25#include <string.h>26
27#ifndef NO_CPU_AFFINITY28
29static void check_affinity(void* arg) {30int r;31char* cpumask;32int cpumasksize;33uv_thread_t tid;34
35cpumask = (char*)arg;36cpumasksize = uv_cpumask_size();37ASSERT_GT(cpumasksize, 0);38tid = uv_thread_self();39r = uv_thread_setaffinity(&tid, cpumask, NULL, cpumasksize);40ASSERT_OK(r);41r = uv_thread_setaffinity(&tid, cpumask + cpumasksize, cpumask, cpumasksize);42ASSERT_OK(r);43}
44
45
46TEST_IMPL(thread_affinity) {47int t1first;48int t1second;49int t2first;50int t2second;51int cpumasksize;52char* cpumask;53int ncpus;54int r;55int c;56int i;57uv_thread_t threads[3];58
59#ifdef _WIN3260/* uv_thread_self isn't defined for the main thread on Windows */61threads[0] = GetCurrentThread();62#else63threads[0] = uv_thread_self();64#endif65cpumasksize = uv_cpumask_size();66ASSERT_GT(cpumasksize, 0);67
68cpumask = calloc(4 * cpumasksize, 1);69ASSERT(cpumask);70
71r = uv_thread_getaffinity(&threads[0], cpumask, cpumasksize);72ASSERT_OK(r);73ASSERT(cpumask[0] && "test must be run with cpu 0 affinity");74ncpus = 0;75while (cpumask[++ncpus]) { }76memset(cpumask, 0, 4 * cpumasksize);77
78t1first = cpumasksize * 0;79t1second = cpumasksize * 1;80t2first = cpumasksize * 2;81t2second = cpumasksize * 3;82
83cpumask[t1second + 0] = 1;84cpumask[t2first + 0] = 1;85cpumask[t1first + (ncpus >= 2)] = 1;86cpumask[t2second + (ncpus >= 2)] = 1;87#ifdef __linux__88cpumask[t1second + 2] = 1;89cpumask[t2first + 2] = 1;90cpumask[t1first + 3] = 1;91cpumask[t2second + 3] = 1;92#else93if (ncpus >= 3) {94cpumask[t1second + 2] = 1;95cpumask[t2first + 2] = 1;96}97if (ncpus >= 4) {98cpumask[t1first + 3] = 1;99cpumask[t2second + 3] = 1;100}101#endif102
103ASSERT_OK(uv_thread_create(threads + 1,104check_affinity,105&cpumask[t1first]));106ASSERT_OK(uv_thread_create(threads + 2,107check_affinity,108&cpumask[t2first]));109ASSERT_OK(uv_thread_join(threads + 1));110ASSERT_OK(uv_thread_join(threads + 2));111
112ASSERT(cpumask[t1first + 0] == (ncpus == 1));113ASSERT(cpumask[t1first + 1] == (ncpus >= 2));114ASSERT_OK(cpumask[t1first + 2]);115ASSERT(cpumask[t1first + 3] == (ncpus >= 4));116
117ASSERT_EQ(1, cpumask[t2first + 0]);118ASSERT_OK(cpumask[t2first + 1]);119ASSERT(cpumask[t2first + 2] == (ncpus >= 3));120ASSERT_OK(cpumask[t2first + 3]);121
122c = uv_thread_getcpu();123ASSERT_GE(c, 0);124
125memset(cpumask, 0, cpumasksize);126cpumask[c] = 1;127r = uv_thread_setaffinity(&threads[0], cpumask, NULL, cpumasksize);128ASSERT_OK(r);129
130memset(cpumask, 0, cpumasksize);131r = uv_thread_getaffinity(&threads[0], cpumask, cpumasksize);132ASSERT_OK(r);133for (i = 0; i < cpumasksize; i++) {134if (i == c)135ASSERT_EQ(1, cpumask[i]);136else137ASSERT_OK(cpumask[i]);138}139
140free(cpumask);141
142return 0;143}
144
145#else146
147TEST_IMPL(thread_affinity) {148int cpumasksize;149cpumasksize = uv_cpumask_size();150ASSERT_EQ(cpumasksize, UV_ENOTSUP);151return 0;152}
153
154#endif155