1
0
Fork 0
Browse Source

Add a program to test the performance of signals

master
Gavin Howard 2 years ago
parent
commit
a8028bbb61
Signed by: gavin
GPG Key ID: C08038BDF280D33E
  1. 2
      .gitignore
  2. 16
      Makefile
  3. 113
      signals.c
  4. 6
      test.c
  5. 6
      time.c

2
.gitignore vendored

@ -2,3 +2,5 @@ shmem
shmem_affinity
kernel
kernel_affinity
signals
signals_affinity

16
Makefile

@ -1,5 +1,5 @@
all: shmem shmem_affinity kernel kernel_affinity
run: run_shmem run_kernel run_shmem_affinity run_kernel_affinity
all: shmem shmem_affinity kernel kernel_affinity signals signals_affinity
run: run_shmem run_kernel run_shmem_affinity run_kernel_affinity run_signals
shmem:
$(CC) -std=c11 -DSHMEM $(CFLAGS) -lpthread -lrt -o shmem test.c
@ -25,5 +25,15 @@ run_kernel: kernel
run_kernel_affinity: kernel_affinity
@./kernel_affinity
signals:
$(CC) -std=c11 $(CFLAGS) -lpthread -o signals signals.c
signals_affinity:
$(CC) -std=c11 -DAFFINITY $(CFLAGS) -lpthread -o signals_affinity signals.c
run_signals:
@./signals
@./signals_affinity
clean:
rm -rf shmem shmem_affinity kernel kernel_affinity
rm -rf shmem shmem_affinity kernel kernel_affinity signals signals_affinity

113
signals.c

@ -0,0 +1,113 @@
#define _GNU_SOURCE
#define _XOPEN_SOURCE (700)
#include <stdlib.h>
#include <stdint.h>
#include <sched.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <stdbool.h>
#include <semaphore.h>
#include <stdio.h>
#include "time.c"
#define CONSUMER(p) ((p) == 0)
#ifdef AFFINITY
#define STR "AFFINITY "
#else // AFFINITY
#define STR ""
#endif // AFFINITY
#define LIMIT ((size_t) 100000000)
#define LIMIT2 (LIMIT / 2)
unsigned int nsignals;
pid_t parent;
pid_t child;
sem_t sem;
void sighandler(int sig)
{
nsignals += 1;
if (CONSUMER(child))
{
kill(parent, SIGUSR1);
if (nsignals >= LIMIT2) sem_post(&sem);
}
else if (nsignals < LIMIT2) kill(child, SIGUSR1);
else sem_post(&sem);
}
int main()
{
cpu_set_t set;
TimeSpec start, end, diff;
struct sigaction sa;
int status;
parent = getpid();
nice(-20);
sigemptyset(&sa.sa_mask);
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
child = fork();
if (child < 0) err("fork failed", 1);
sem_init(&sem, 0, 0);
#ifdef AFFINITY
CPU_ZERO(&set);
if (CONSUMER(child)) CPU_SET(1, &set);
else CPU_SET(0, &set);
if (sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
{
err("could not set cpu affinity", 2);
}
#endif // AFFINITY
if (!CONSUMER(child))
{
ts_time(&start);
kill(child, SIGUSR1);
}
sem_wait(&sem);
if (!CONSUMER(child))
{
ts_time(&end);
waitpid(child, &status, 0);
if (WEXITSTATUS(status) != 0) err("child exited bad", 11);
diff = ts_diff(end, start);
printf(STR "SIGNALS: ");
ts_print(diff, LIMIT);
printf("\n");
}
sem_destroy(&sem);
return 0;
}

6
test.c

@ -82,12 +82,6 @@ typedef union size
#endif // SHMEM
static void err(char* msg, int exit_code)
{
fprintf(stderr, "error: %s\n", msg);
exit(exit_code);
}
#ifdef SHMEM
static Shmem* shmem_create(void)
{

6
time.c

@ -1,6 +1,12 @@
#include <stdio.h>
#include <time.h>
static void err(char* msg, int exit_code)
{
fprintf(stderr, "error: %s\n", msg);
exit(exit_code);
}
typedef struct timespec TimeSpec;
unsigned long pow10(unsigned long p)

Loading…
Cancel
Save