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