mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-04 11:42:28 +00:00
Write more tests for signal handling
There's now a much stronger level of assurance that signaling on Windows will be atomic, low-latency, low tail latency, and shall never deadlock.
This commit is contained in:
parent
0e59afb403
commit
dd8c4dbd7d
19 changed files with 407 additions and 75 deletions
105
test/posix/signal_fight_test.c
Normal file
105
test/posix/signal_fight_test.c
Normal file
|
@ -0,0 +1,105 @@
|
|||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @fileoverview Tests two threads killing each other won't deadlock.
|
||||
*
|
||||
* Our Windows implementation of signals has surprisingly high
|
||||
* throughput on this test. About 10x more signals get delivered than
|
||||
* any other OS and in the same amount of time. The only exception was
|
||||
* OpenBSD, which delivered a similar number of signals, but it took 10x
|
||||
* longer for the process to execute.
|
||||
*/
|
||||
|
||||
#define ITERATIONS 10000
|
||||
|
||||
int gotsigs[2];
|
||||
pthread_t threads[2];
|
||||
pthread_t thread_ids[2];
|
||||
pthread_barrier_t barrier;
|
||||
pthread_barrier_t barrier2;
|
||||
|
||||
void sig_handler(int signo) {
|
||||
if (pthread_equal(pthread_self(), threads[0]))
|
||||
++gotsigs[0];
|
||||
if (pthread_equal(pthread_self(), threads[1]))
|
||||
++gotsigs[1];
|
||||
}
|
||||
|
||||
void *thread_func(void *arg) {
|
||||
int idx = *(int *)arg;
|
||||
int other_idx = 1 - idx;
|
||||
|
||||
thread_ids[idx] = pthread_self();
|
||||
|
||||
int s = pthread_barrier_wait(&barrier);
|
||||
if (s != 0 && s != PTHREAD_BARRIER_SERIAL_THREAD)
|
||||
exit(1);
|
||||
|
||||
pthread_t other_thread = thread_ids[other_idx];
|
||||
|
||||
for (int i = 0; i < ITERATIONS; ++i)
|
||||
if (pthread_kill(other_thread, SIGUSR1))
|
||||
exit(2);
|
||||
|
||||
s = pthread_barrier_wait(&barrier2);
|
||||
if (s != 0 && s != PTHREAD_BARRIER_SERIAL_THREAD)
|
||||
exit(1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = sig_handler;
|
||||
sa.sa_flags = 0;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
|
||||
if (sigaction(SIGUSR1, &sa, 0) == -1)
|
||||
exit(3);
|
||||
|
||||
if (pthread_barrier_init(&barrier, 0, 2))
|
||||
exit(4);
|
||||
if (pthread_barrier_init(&barrier2, 0, 2))
|
||||
exit(4);
|
||||
|
||||
int idx0 = 0, idx1 = 1;
|
||||
|
||||
if (pthread_create(&threads[0], 0, thread_func, &idx0))
|
||||
exit(5);
|
||||
if (pthread_create(&threads[1], 0, thread_func, &idx1))
|
||||
exit(6);
|
||||
|
||||
if (pthread_join(threads[0], 0))
|
||||
exit(7);
|
||||
if (pthread_join(threads[1], 0))
|
||||
exit(8);
|
||||
|
||||
if (pthread_barrier_destroy(&barrier2))
|
||||
exit(9);
|
||||
if (pthread_barrier_destroy(&barrier))
|
||||
exit(9);
|
||||
|
||||
if (!gotsigs[0])
|
||||
exit(10);
|
||||
if (!gotsigs[1])
|
||||
exit(11);
|
||||
|
||||
return 0;
|
||||
}
|
170
test/posix/signal_latency_test.c
Normal file
170
test/posix/signal_latency_test.c
Normal file
|
@ -0,0 +1,170 @@
|
|||
// Copyright 2024 Justine Alexandra Roberts Tunney
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for
|
||||
// any purpose with or without fee is hereby granted, provided that the
|
||||
// above copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
#define ITERATIONS 10000
|
||||
|
||||
pthread_t sender_thread;
|
||||
pthread_t receiver_thread;
|
||||
struct timespec send_time;
|
||||
double latencies[ITERATIONS];
|
||||
|
||||
void sender_signal_handler(int signo) {
|
||||
// Empty handler to unblock sigsuspend()
|
||||
}
|
||||
|
||||
void receiver_signal_handler(int signo) {
|
||||
struct timespec receive_time;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &receive_time) == -1)
|
||||
exit(1);
|
||||
|
||||
long sec_diff = receive_time.tv_sec - send_time.tv_sec;
|
||||
long nsec_diff = receive_time.tv_nsec - send_time.tv_nsec;
|
||||
double latency_ns = sec_diff * 1e9 + nsec_diff;
|
||||
|
||||
static int iteration = 0;
|
||||
if (iteration < ITERATIONS)
|
||||
latencies[iteration++] = latency_ns;
|
||||
|
||||
// Send SIGUSR2 back to sender_thread
|
||||
if (pthread_kill(sender_thread, SIGUSR2))
|
||||
exit(2);
|
||||
|
||||
// Exit if we're done.
|
||||
if (iteration >= ITERATIONS)
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
void *sender_func(void *arg) {
|
||||
// Block SIGUSR2
|
||||
sigset_t block_set;
|
||||
sigemptyset(&block_set);
|
||||
sigaddset(&block_set, SIGUSR2);
|
||||
if (pthread_sigmask(SIG_BLOCK, &block_set, 0))
|
||||
exit(3);
|
||||
|
||||
// Install signal handler for SIGUSR2
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = sender_signal_handler;
|
||||
sa.sa_flags = 0;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGUSR2, &sa, 0))
|
||||
exit(4);
|
||||
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &send_time))
|
||||
exit(5);
|
||||
|
||||
// Send SIGUSR1 to receiver_thread
|
||||
if (pthread_kill(receiver_thread, SIGUSR1))
|
||||
exit(6);
|
||||
|
||||
// Unblock SIGUSR2 and wait for it
|
||||
sigset_t wait_set;
|
||||
sigemptyset(&wait_set);
|
||||
if (sigsuspend(&wait_set) && errno != EINTR)
|
||||
exit(7);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *receiver_func(void *arg) {
|
||||
// Install signal handler for SIGUSR1
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = receiver_signal_handler;
|
||||
sa.sa_flags = 0;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGUSR1, &sa, 0))
|
||||
exit(8);
|
||||
|
||||
// Block all signals except SIGUSR1
|
||||
sigset_t block_set;
|
||||
sigfillset(&block_set);
|
||||
sigdelset(&block_set, SIGUSR1);
|
||||
if (pthread_sigmask(SIG_SETMASK, &block_set, 0))
|
||||
exit(9);
|
||||
|
||||
// Wait indefinitely for signals
|
||||
while (1)
|
||||
pause();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b) {
|
||||
const double *x = a, *y = b;
|
||||
if (*x < *y)
|
||||
return -1;
|
||||
else if (*x > *y)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// Block SIGUSR1 and SIGUSR2 in main thread
|
||||
sigset_t block_set;
|
||||
sigemptyset(&block_set);
|
||||
sigaddset(&block_set, SIGUSR1);
|
||||
sigaddset(&block_set, SIGUSR2);
|
||||
if (pthread_sigmask(SIG_BLOCK, &block_set, 0))
|
||||
exit(10);
|
||||
|
||||
// Create receiver thread first
|
||||
if (pthread_create(&receiver_thread, 0, receiver_func, 0))
|
||||
exit(11);
|
||||
|
||||
// Create sender thread
|
||||
if (pthread_create(&sender_thread, 0, sender_func, 0))
|
||||
exit(12);
|
||||
|
||||
// Wait for threads to finish
|
||||
if (pthread_join(sender_thread, 0))
|
||||
exit(13);
|
||||
if (pthread_join(receiver_thread, 0))
|
||||
exit(14);
|
||||
|
||||
// Compute mean latency
|
||||
double total_latency = 0;
|
||||
for (int i = 0; i < ITERATIONS; i++)
|
||||
total_latency += latencies[i];
|
||||
double mean_latency = total_latency / ITERATIONS;
|
||||
|
||||
// Sort latencies to compute percentiles
|
||||
qsort(latencies, ITERATIONS, sizeof(double), compare);
|
||||
|
||||
double p50 = latencies[(int)(0.50 * ITERATIONS)];
|
||||
double p90 = latencies[(int)(0.90 * ITERATIONS)];
|
||||
double p95 = latencies[(int)(0.95 * ITERATIONS)];
|
||||
double p99 = latencies[(int)(0.99 * ITERATIONS)];
|
||||
|
||||
printf("Mean latency: %.2f ns\n", mean_latency);
|
||||
printf("50th percentile latency: %.2f ns\n", p50);
|
||||
printf("90th percentile latency: %.2f ns\n", p90);
|
||||
printf("95th percentile latency: %.2f ns\n", p95);
|
||||
printf("99th percentile latency: %.2f ns\n", p99);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue