libuv/test/test-thread-affinity.c
Kiran Pamnany a0f7241059 thread: support for affinity and detach
int uv_thread_setaffinity(uv_thread_t *tid,
                          char *cpumask,
                          char *oldmask,
                          size_t mask_size);
Sets the specified thread's affinity to cpumask, which must be
specified in bytes. Returns the previous affinity setting in oldmask,
if provided. On Unix, uses pthread_getaffinity_np() to get the
affinity setting and maps the cpu_set_t to bytes in oldmask. Then
maps the bytes in cpumask to a cpu_set_t and uses pthread_setaffinity_np().
On Windows, maps the bytes in cpumask to a bitmask and uses
SetThreadAffinityMask() which returns the previous affinity setting.

int uv_thread_getaffinity(uv_thread_t *tid,
                          char *cpumask,
                          size_t mask_size);
Gets the specified thread's affinity setting. On Unix, maps the
cpu_set_t returned by pthread_getaffinity_np() to bytes in cpumask.
Unsupported on Windows, which doesn't have any way to get the current
affinity setting.

int uv_thread_detach(uv_thread_t *tid);
Detaches the specified thread so it will be cleaned up on exit
automatically; joining it is no longer necessary or possible. Uses
pthread_detach() on Unix and CloseHandle() on Windows.

Empty implementations (returning -ENOTSUP) on non-supported platforms
(such as OS X and AIX).

Refs: https://github.com/libuv/libuv/pull/273
Refs: https://github.com/libuv/libuv/pull/280
Refs: https://github.com/libuv/libuv/pull/597
PR-URL: https://github.com/libuv/libuv/pull/1654
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
2018-11-10 18:01:50 -05:00

117 lines
2.6 KiB
C

/* Copyright libuv project contributors. All rights reserved.
*/
#include "uv.h"
#include "task.h"
#include <string.h>
#ifndef NO_CPU_AFFINITY
static void check_affinity(void* arg) {
int r;
char* cpumask;
int cpumasksize;
uv_thread_t tid;
cpumask = (char*)arg;
cpumasksize = uv_cpumask_size();
ASSERT(cpumasksize > 0);
tid = uv_thread_self();
r = uv_thread_setaffinity(&tid, cpumask, NULL, cpumasksize);
ASSERT(r == 0);
r = uv_thread_setaffinity(&tid, cpumask + cpumasksize, cpumask, cpumasksize);
ASSERT(r == 0);
}
TEST_IMPL(thread_affinity) {
int t1first;
int t1second;
int t2first;
int t2second;
int cpumasksize;
char* cpumask;
int ncpus;
int r;
uv_thread_t threads[3];
#ifdef _WIN32
/* uv_thread_self isn't defined for the main thread on Windows */
threads[0] = GetCurrentThread();
#else
threads[0] = uv_thread_self();
#endif
cpumasksize = uv_cpumask_size();
ASSERT(cpumasksize > 0);
cpumask = calloc(4 * cpumasksize, 1);
ASSERT(cpumask);
r = uv_thread_getaffinity(&threads[0], cpumask, cpumasksize);
ASSERT(r == 0);
ASSERT(cpumask[0] && "test must be run with cpu 0 affinity");
ncpus = 0;
while (cpumask[++ncpus]) { }
memset(cpumask, 0, 4 * cpumasksize);
t1first = cpumasksize * 0;
t1second = cpumasksize * 1;
t2first = cpumasksize * 2;
t2second = cpumasksize * 3;
cpumask[t1second + 0] = 1;
cpumask[t2first + 0] = 1;
cpumask[t1first + (ncpus >= 2)] = 1;
cpumask[t2second + (ncpus >= 2)] = 1;
#ifdef __linux__
cpumask[t1second + 2] = 1;
cpumask[t2first + 2] = 1;
cpumask[t1first + 3] = 1;
cpumask[t2second + 3] = 1;
#else
if (ncpus >= 3) {
cpumask[t1second + 2] = 1;
cpumask[t2first + 2] = 1;
}
if (ncpus >= 4) {
cpumask[t1first + 3] = 1;
cpumask[t2second + 3] = 1;
}
#endif
ASSERT(0 == uv_thread_create(threads + 1,
check_affinity,
&cpumask[t1first]));
ASSERT(0 == uv_thread_create(threads + 2,
check_affinity,
&cpumask[t2first]));
ASSERT(0 == uv_thread_join(threads + 1));
ASSERT(0 == uv_thread_join(threads + 2));
ASSERT(cpumask[t1first + 0] == (ncpus == 1));
ASSERT(cpumask[t1first + 1] == (ncpus >= 2));
ASSERT(cpumask[t1first + 2] == 0);
ASSERT(cpumask[t1first + 3] == (ncpus >= 4));
ASSERT(cpumask[t2first + 0] == 1);
ASSERT(cpumask[t2first + 1] == 0);
ASSERT(cpumask[t2first + 2] == (ncpus >= 3));
ASSERT(cpumask[t2first + 3] == 0);
free(cpumask);
return 0;
}
#else
TEST_IMPL(thread_affinity) {
int cpumasksize;
cpumasksize = uv_cpumask_size();
ASSERT(cpumasksize == UV_ENOTSUP);
return 0;
}
#endif