Skip to content

Commit

Permalink
add cpu contention benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jun 22, 2022
1 parent 63b3b55 commit a743d3a
Show file tree
Hide file tree
Showing 5 changed files with 508 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.out

60 changes: 60 additions & 0 deletions contention.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

#include "ntime.h"
#include "utils.h"

#define N 200
#define LOOP_NUM 20000000

int thread_timings[N];

pthread_barrier_t barrier;

void loopFunc() {
int x = 0;
int i;
for (i = 0; i < LOOP_NUM; i++) {
x++;
}
return;
}

void* threadLoopFunc(void* args) {
pthread_barrier_wait(&barrier);
uint64_t st = get_time_in_nsecs();
loopFunc();
uint64_t ed = get_time_in_nsecs();
int el = (int) ((ed-st) / (uint64_t) 1000000);
*(int *)args = el;
return NULL;
}

int main() {
int j;
for (j = 0; j < 5; j++) loopFunc();
uint64_t st = get_time_in_nsecs();
loopFunc();
uint64_t ed = get_time_in_nsecs();
int el = (int)(ed-st) / 1000000;
printf("Took %d msecs with only 1 thread running\n", el);

pthread_barrier_init(&barrier, N);
pthread_t threads[N];
int i;
for (i = 0; i < N; i++) {
pthread_create(&threads[i], NULL, threadLoopFunc, &thread_timings[i]);
}
for (i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
int total_msecs = 0;
for (i = 0; i < N; i++) {
total_msecs += thread_timings[i];
}
printf("Took %d msecs with %d threads", total_msecs / N, N);
return 0;
}

Loading

0 comments on commit a743d3a

Please sign in to comment.