Skip to content

Commit

Permalink
Benchmarking code used to submit draft 0 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanshcsingh authored Feb 20, 2020
1 parent 10332e0 commit e146123
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ cs:
#for testing I would use below

test:
make ARGS="-DITERATIONS=100 -DTRIALS=10 -DICACHE_HITS=10"
make ARGS="-DITERATIONS=1 -DTRIALS=450 -DICACHE_HITS=10"
71 changes: 42 additions & 29 deletions context_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,69 @@
#include <unistd.h>
#include "constants.h"

#define SWITCHS 10000
#define SWITCHS 1000

struct PipeFD {
typedef struct {
int readFD;
int writeFD;
};
} PipeFd;

struct Pipes {
struct PipeFD parentFD;
struct PipeFD childFD;
};
typedef struct {
PipeFd parentFD;
PipeFd childFD;
} Pipes;

struct Pipes getPipes() {
Pipes getPipes() {

int parent_child_fd[2];
int child_parent_fd[2];
int pipe1_fd[2];
int pipe2_fd[2];

pipe(parent_child_fd);
pipe(child_parent_fd);
pipe(pipe1_fd);
pipe(pipe2_fd);

struct PipeFD parentfd = {
.readFD = child_parent_fd[0],
.writeFD = parent_child_fd[1]
// parent writes to where child reads
// child writes to where parent reads
PipeFd parentfd = {
.readFD = pipe1_fd[0],
.writeFD = pipe2_fd[1]
};

struct PipeFD childfd = {
.readFD = parent_child_fd[0],
.writeFD = child_parent_fd[1]
PipeFd childfd = {
.readFD = pipe2_fd[0],
.writeFD = pipe1_fd[1]
};


struct Pipes pipes = {
Pipes cs_pipes = {
.parentFD = parentfd,
.childFD = childfd
};

return pipes;
return cs_pipes;
}

void testContextSwitchTime(struct PipeFD pipefd) {
void testContextSwitchTime(PipeFd pipefd) {

char buf[1];

for (int i = 0; i < SWITCHS; ++i)
{
read(pipefd.readFD, buf, 1);
write(pipefd.writeFD, buf, 1);
// fprintf(stderr, "process id: %d, finishing switch: %d\n", getpid(), i);
}
}

void* threadStart(void *arg) {
struct PipeFD * pipefd = (struct PipeFD *)arg;
PipeFd * pipefd = (PipeFd *)arg;
testContextSwitchTime(*pipefd);
close(pipefd->readFD);
close(pipefd->writeFD);
return NULL;
}

uint64_t cs_benchmarkHelper(struct PipeFD pipefd, pthread_t* childThread) {
uint64_t cs_benchmarkHelper(PipeFd pipefd, pthread_t* childThread) {

char buf[2] = {'x'};
char buf[1] = {'8'};
write(pipefd.writeFD, buf, 1);

uint32_t cycles_high0, cycles_low0, cycles_low1, cycles_high1;
Expand Down Expand Up @@ -105,22 +109,31 @@ uint64_t cs_benchmarkHelper(struct PipeFD pipefd, pthread_t* childThread) {
//TODO: check return values of system calls
uint64_t benchmarkContextSwitchThread(fun_ptr _ignore)
{
struct Pipes pipes = getPipes();
Pipes pipes = getPipes();

pthread_t childThread;
pthread_create(&childThread, NULL, threadStart, (void *)&(pipes.childFD));

return cs_benchmarkHelper(pipes.parentFD, &childThread);
uint64_t cycles = cs_benchmarkHelper(pipes.parentFD, &childThread);
close(pipes.parentFD.readFD);
close(pipes.childFD.writeFD);
return cycles;
}

uint64_t benchmarkContextSwitchProcess(fun_ptr _ignore) {
struct Pipes pipes = getPipes();
Pipes pipes = getPipes();

if (fork() == 0) {
testContextSwitchTime(pipes.childFD);
close(pipes.childFD.readFD);
close(pipes.childFD.writeFD);
exit(0);
}
else {
return cs_benchmarkHelper(pipes.parentFD, NULL);
uint64_t cycles = cs_benchmarkHelper(pipes.parentFD, NULL);
close(pipes.parentFD.readFD);
close(pipes.childFD.writeFD);

return cycles;
}
}
22 changes: 18 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#define TRIALS 1000
#endif

void runTest(ben_ptr ben, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials);
void runTest(ben_ptr, fun_ptr, const char*, uint32_t, uint32_t);
void runTestQuarantine(ben_ptr, fun_ptr, const char*, uint32_t, uint32_t);

int main() {
// runTest(benchmarkCycles, testMeasurementOverhead, "Measurement Overhead", ITERATIONS, TRIALS);
Expand All @@ -27,13 +28,14 @@ int main() {
// runTest(benchmarkCycles, testCall4, "Call method with 4 parameters", ITERATIONS, TRIALS);
// runTest(benchmarkCycles, testCall5, "Call method with 5 parameters", ITERATIONS, TRIALS);
// runTest(benchmarkCycles, testCall6, "Call method with 6 parameters", ITERATIONS, TRIALS);
// runTest(benchmarkCycles, testCall7, "Call method with 7 parameters", ITERATIONS, TRIALS);
runTest(benchmarkCycles, testCall7, "Call method with 7 parameters", ITERATIONS, TRIALS);
// runTest(benchmarkCycles, testSystemCall, "System call: clock_gettime", ITERATIONS, TRIALS);
// runTest(benchmarkThread, NULL, "System call: test kernel thread", ITERATIONS, TRIALS);
// runTest(benchmarkThread, NULL, "Test kernel thread", ITERATIONS, TRIALS);
// runTest(benchmarkFork, NULL, "Test fork thread", ITERATIONS, TRIALS);
runTest(benchmarkContextSwitchThread, NULL, "Context Switch Thread", ITERATIONS, TRIALS);
runTest(benchmarkContextSwitchProcess, NULL, "Context switch Process", ITERATIONS, TRIALS);
// runTest(benchmarkContextSwitchThread, NULL, "Context Switch Thread", ITERATIONS, TRIALS);
// runTest(benchmarkContextSwitchProcess, NULL, "Context switch Process", ITERATIONS, TRIALS);
// runTest(benchmarkContextSwitchThread, NULL, "Context Switch Thread", ITERATIONS, TRIALS);
}

void runTest(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials) {
Expand All @@ -51,6 +53,8 @@ void runTest(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterati
for (int i=0; i < trials; i++) {
for (int j=0; j< iterations; j++) {
iteration_results[j] = benchmark(test);

fprintf(stderr, "#%d\tFILE DESCRIPTOR SIZE IS: %d\n", i, getdtablesize());
}

// select median of all iteration tests per trial
Expand All @@ -67,3 +71,13 @@ void runTest(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterati
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
}

void runTestQuarantine(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials) {
fprintf(stderr, "First run\n");
benchmark(test);

fprintf(stderr, "Second run\n");
benchmark(test);

fprintf(stderr, "tests completed correctly");
}

0 comments on commit e146123

Please sign in to comment.