Skip to content

Commit b734eec

Browse files
committed
Test restricting tests to single cpu
1 parent fe01642 commit b734eec

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

libc/testlib/BUILD.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,15 @@ LIBC_TESTMAIN_DIRECTDEPS = \
215215
LIBC_LOG \
216216
LIBC_MEM \
217217
LIBC_NEXGEN32E \
218+
LIBC_PROC \
218219
LIBC_RUNTIME \
219220
LIBC_STDIO \
220221
LIBC_SYSV \
221222
LIBC_SYSV_CALLS \
222223
LIBC_TESTLIB \
223224
LIBC_TESTLIB_RUNNER \
224225
THIRD_PARTY_DLMALLOC \
225-
THIRD_PARTY_GETOPT
226+
THIRD_PARTY_GETOPT \
226227

227228
LIBC_TESTMAIN_DEPS := \
228229
$(call uniq,$(foreach x,$(LIBC_TESTMAIN_DIRECTDEPS),$($(x))))

libc/testlib/testmain.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/assert.h"
2020
#include "libc/calls/calls.h"
21+
#include "libc/calls/struct/cpuset.h"
2122
#include "libc/calls/struct/rlimit.h"
2223
#include "libc/calls/struct/sigaction.h"
2324
#include "libc/calls/struct/siginfo.h"
@@ -31,6 +32,7 @@
3132
#include "libc/intrin/strace.h"
3233
#include "libc/intrin/ubsan.h"
3334
#include "libc/intrin/weaken.h"
35+
#include "libc/limits.h"
3436
#include "libc/log/log.h"
3537
#include "libc/macros.h"
3638
#include "libc/mem/leaks.h"
@@ -52,6 +54,8 @@
5254
#include "libc/thread/tls.h"
5355
#include "third_party/getopt/getopt.internal.h"
5456

57+
#pragma weak main
58+
5559
#define USAGE \
5660
" [FLAGS]\n\
5761
\n\
@@ -88,7 +92,41 @@ static void GetOpts(int argc, char *argv[]) {
8892
}
8993
}
9094

91-
#pragma weak main
95+
static int rando(void) {
96+
return _rand64() & INT_MAX;
97+
}
98+
99+
static void limit_process_to_single_cpu(void) {
100+
extern int disable_limit_process_to_single_cpu;
101+
if (_weaken(disable_limit_process_to_single_cpu))
102+
return;
103+
if (!(IsLinux() || IsFreebsd() || IsNetbsd() || IsWindows()))
104+
return;
105+
if (IsFreebsd() && getuid())
106+
return;
107+
cpu_set_t legal;
108+
if (sched_getaffinity(0, sizeof(cpu_set_t), &legal) == -1) {
109+
perror("sched_setaffinity failed");
110+
exit(1);
111+
}
112+
int count = CPU_COUNT(&legal);
113+
cpu_set_t newset;
114+
CPU_ZERO(&newset);
115+
bool done = false;
116+
while (!done) {
117+
for (int i = 0; i < CPU_SETSIZE; ++i) {
118+
if (CPU_ISSET(i, &legal) && !(rando() % count)) {
119+
CPU_SET(rando() % count, &newset);
120+
done = true;
121+
break;
122+
}
123+
}
124+
}
125+
if (sched_setaffinity(0, sizeof(cpu_set_t), &newset) == -1) {
126+
perror("sched_setaffinity failed");
127+
exit(1);
128+
}
129+
}
92130

93131
/**
94132
* Generic test program main function.
@@ -108,8 +146,11 @@ int main(int argc, char *argv[]) {
108146
return 1;
109147
}
110148

149+
// // this sometimes helps tease out mt bugs
150+
// limit_process_to_single_cpu();
151+
111152
// test huge pointers by enabling pml5t
112-
if (_rand64() % 2) {
153+
if (rando() % 2) {
113154
errno_t e = errno;
114155
mmap((char *)0x80000000000000, 1, PROT_NONE, //
115156
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

test/libc/proc/sched_getaffinity_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "libc/thread/thread.h"
3131
#include "libc/thread/thread2.h"
3232

33+
int disable_limit_process_to_single_cpu;
34+
3335
void SetUp(void) {
3436
if (!IsLinux() && !IsFreebsd() && !IsWindows()) {
3537
exit(0);

0 commit comments

Comments
 (0)