From 17110dd658a61de166c9fcee007d68575b7d271b Mon Sep 17 00:00:00 2001 From: Sathya Narayanan Date: Mon, 1 Feb 2016 09:10:56 -0800 Subject: [PATCH] Latest 2/1 --- loopoverhead.c | 20 +++++++ osproject.c | 7 ++- osproject_pipe.c | 86 ---------------------------- osproject_syscall.c | 10 +++- osproject_task.c | 24 +++----- osproject_thread.c | 40 ++++++------- osproject_threadpipe.c | 69 ----------------------- osproject_time.c | 149 +++++++++++++++++++++++++++---------------------- 8 files changed, 139 insertions(+), 266 deletions(-) create mode 100644 loopoverhead.c delete mode 100644 osproject_pipe.c delete mode 100644 osproject_threadpipe.c diff --git a/loopoverhead.c b/loopoverhead.c new file mode 100644 index 0000000..a79e32e --- /dev/null +++ b/loopoverhead.c @@ -0,0 +1,20 @@ +#include + +int main() +{ + int time1, time2, sum=0; + int i; + int avg = 0; + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (i)); + printf("regr: %x\n", i); + + while(sum < 1000) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time1)); + for (i = 0; i < 100; i++); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time2)); + sum++; + avg += (time2 - time1 - 5); + } + + printf("Loop overhead: %u\n", avg/(1000)); +} diff --git a/osproject.c b/osproject.c index 8ce65f9..b1530b9 100644 --- a/osproject.c +++ b/osproject.c @@ -39,15 +39,16 @@ int main() unsigned int i; unsigned int a, b; - printf("CSE237A: Hello world.\n"); asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); printf("regr: %x\n", regr); - for (i = 0; i < 10; i++) { + regr = 0; + for (i = 0; i < 1000; i++) { asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("c[%u]: %u\n", i, b - a); + regr += b - a; } + printf("Measurement overhead: %u\n", regr); return 1; diff --git a/osproject_pipe.c b/osproject_pipe.c deleted file mode 100644 index dcf7ecf..0000000 --- a/osproject_pipe.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include -#include - -/* Read characters from the pipe and echo them to stdout. */ - -unsigned int time1, time2; -unsigned int avg = 0; - -int read_from_pipe (int file) -{ - - unsigned int c; - FILE *stream = fdopen (file, "r"); - fread(&c, sizeof(c), 1, stream); - //while ((c = fgetc (stream)) != EOF) - // putchar (c); - fclose (stream); - return c; - -} - -/* Write some random text to the pipe. */ - -void write_to_pipe (int file) -{ - unsigned int c; - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (c)); - FILE *stream; - stream = fdopen (file, "w"); - fwrite(&c, sizeof(c), 1, stream); -// fprintf (stream, "hello, world!\n"); -// fprintf (stream, "goodbye, world!\n"); - fclose (stream); - -} - -int main (void) -{ - pid_t pid; - int mypipe[2]; - int i = 0; - unsigned int regr; - - /* Create the pipe. */ - if (pipe (mypipe)) { - printf ("Pipe failed.\n"); - return EXIT_FAILURE; - } - - asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); - printf("regr: %x\n", regr); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (regr)); - /* Create the child process. */ - if ((pid = fork()) == -1) - exit(1); - - if (pid) { - while(i < 100) { - /* This is the child process. Close other end first. */ - printf("parent process\n"); - close (mypipe[1]); - time1 = read_from_pipe (mypipe[0]); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time2)); - printf("time2: %u, time1: %u, avg: %u\n", time2, time1, time2 - time1); - avg += (time2 - time1); - i++; - } - //kill(pid, SIGKILL); - } else { - while( i < 100) { - /* This is the parent process. Close other end first. */ - printf("child process\n"); - close (mypipe[0]); - //asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time1)); - write_to_pipe (mypipe[1]); - usleep(1000); - i++; - } - } - - printf("Avg switching time: %u\n", avg); - return 0; -} diff --git a/osproject_syscall.c b/osproject_syscall.c index 5238a36..f5d0587 100644 --- a/osproject_syscall.c +++ b/osproject_syscall.c @@ -5,19 +5,23 @@ int main() unsigned int regr; unsigned int i; unsigned int a, b; + float avg = 0.0; asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); printf("regr: %x\n", regr); regr = 0; - while ( regr < 10) + while ( regr < 10000) { asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); i = getpid(); asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - - printf("PID: %u, Time for getpid: %u\n", i, b - a); + if (regr==0) + printf("PID: %u, Time for getpid: %u\n", i, b - a); + else + avg+=(b-a); regr++; } + printf("Time: %f\n", avg/9999); return 1; } diff --git a/osproject_task.c b/osproject_task.c index b1f8e49..64432f5 100644 --- a/osproject_task.c +++ b/osproject_task.c @@ -1,31 +1,21 @@ #include +#include int main() { unsigned int regr; unsigned int a, b; unsigned int avg; + int status; asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); printf("regr: %x\n", regr); - for (regr = 0; regr < 100; regr++) { - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - regr += (b - a); - } - - regr /= 100; - printf("Measurement overhead: %u\n", regr); - regr = 0; - while (regr < 100) { asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - fork(); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Time for fork: %u\n", b - a); - avg += (b - a); - regr++; + int pid = fork(); + if (pid) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); + printf("Time for fork: %u\n", b - a); } - printf("Fork avg: %u\n", avg); - return 1; + return 0; } diff --git a/osproject_thread.c b/osproject_thread.c index 730b009..eb1588c 100644 --- a/osproject_thread.c +++ b/osproject_thread.c @@ -5,9 +5,14 @@ #include pthread_t tid[100]; - -void* doSomeThing(void *arg) +int avg =0; +static const int iterations = 50000; +int b=0; //Nasty +void* doSomeThing(void *somectx) { + int i; + for(i =0;i< iterations; i++) + sched_yield(); } int main(void) @@ -15,25 +20,20 @@ int main(void) int i = 0; int err; int avg = 0; - int a, b; - - printf("Entering the thing\n"); + int a; - asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (err)); - printf("regr: %x\n", err); - - while(i < 100) { - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - if (err != 0) - printf("Error in thread creation\n"); - //printf("Time for fork: %u\n", b - a); - avg += (b - a); - pthread_cancel(tid[i]); - i++; + printf("Thread Creation Overhead\n"); + //Create only one thread + if(pthread_create(&(tid[i]), NULL, &doSomeThing, NULL)){ + exit(1); } - - printf("Avg thread creation time is %d\n", avg/100); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + for(i=0; i < iterations; i++) + sched_yield(); + asm volatile("mrc p15, 0, %0, c9, c13, 0": "=r" (b)); + //2 because there are two context switches for every iteration + printf("Before:%d, After:%d",a,b); + printf("Avg thread creation time: %d\n ms", (b-a)/(iterations*2)); + return 1; } diff --git a/osproject_threadpipe.c b/osproject_threadpipe.c deleted file mode 100644 index 73afa27..0000000 --- a/osproject_threadpipe.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int fd[2]; -int n = 0; -long now = 0; - -unsigned int time1, time2, time3; -unsigned int avg = 0; - -void * writeThread() -{ -// for(n=1000;n>0;n--) -// { - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time1)); - write(fd[1],(void*)&time1,sizeof(time1)); - - usleep(1000); -// } - return 0; -} - -void * readThread() -{ - unsigned int switchTime; -// for(n=1000;n>0;n--) -// { - read(fd[0],(void*)&time3,sizeof(time3)); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time2)); - switchTime = time2 - time3; - avg = avg + switchTime; -// } - avg = avg/1000; - return 0; -} - - - - -int main(int argc, char ** argv) { - - pthread_t tid1,tid2; - int i = 0; - unsigned int regr; - - /* Create the pipe. */ - if (pipe (fd)) { - printf ("Pipe failed.\n"); - return EXIT_FAILURE; - } - - asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); - printf("regr: %x\n", regr); - if (regr == 0) - exit(1); - printf("here\n"); - pthread_create(&tid1,NULL,writeThread,NULL); - pthread_create(&tid2,NULL,readThread,NULL); - /*pthread_join(tid1,NULL); - * pthread_join(tid2,NULL); - * */ - printf("%u",avg); - return 0; -} diff --git a/osproject_time.c b/osproject_time.c index 10b7a39..65c22c0 100644 --- a/osproject_time.c +++ b/osproject_time.c @@ -1,51 +1,36 @@ #include -/* + static void dummy() { } -*/ -int dummy1(int a) +void dummy1(int a) { - int i = a; - return i; } -int dummy2(int a, int b) +void dummy2(int a, int b) { - int i = b; - return i; } -int dummy3(int a, int b, int c) +void dummy3(int a, int b, int c) { - int i = c; - return i; } -int dummy4(int a, int b, int c, int d) +void dummy4(int a, int b, int c, int d) { - int i = d; - return i; } -int dummy5(int a, int b, int c, int d, int e) +void dummy5(int a, int b, int c, int d, int e) { - int i = e; - return i; } -int dummy6(int a, int b, int c, int d, int e, int f) +void dummy6(int a, int b, int c, int d, int e, int f) { - int i = f; - return i; } -int dummy7(int a, int b, int c, int d, int e, int f, int g) +void dummy7(int a, int b, int c, int d, int e, int f, int g) { - int i = g; - return i; } int main() @@ -53,59 +38,87 @@ int main() unsigned int regr; unsigned int a, b; unsigned int c1, c2, c3, c4, c5, c6, c7; - int d; + int d, i; + float c[8]; + for (i=0; i<8; i++) + c[i]=0; - printf("CSE237A: Hello world.\n"); asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); printf("regr: %x\n", regr); -/* - for (i = 0; i < 10; i++) { + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy(); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); + c[0] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[0]/50000); + for (i = 0; i < 50000; i++) { asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy1(1); asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("c[%u]: %u\n", i, b - a); +// printf("Procedure call overhead with one arg: %f\n", b - a); + c[1] += b - a - 5; } + printf("Procedure call overhead: %f\n", c[1]/50000); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - dummy(); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead: %u\n", b - a); -*/ - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy1(1); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with one arg: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy2(c1, 2); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 2 args: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy3(c1, c2, 3); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 3 args: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy4(c1, c2, c3, 4); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 4 args: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy5(c1, c2, c3, c4, 5); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 5 args: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy6(c1, c2, c3, c4, c5, 6); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 6 args: %u\n", b - a); - - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); - d = dummy7(c1, c2, c3, c4, c5, c6, 7); - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); - printf("Procedure call overhead with 7 args: %u\n", b - a); - printf("d: %d\n", d); + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy2(c1, 2); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 2 args: %f\n", b - a); + c[2] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[2]/50000); + + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy3(c1, c2, 3); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 3 args: %f\n", b - a); + c[3] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[3]/50000); + + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy4(c1, c2, c3, 4); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 4 args: %f\n", b - a); + c[4] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[4]/50000); + + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy5(c1, c2, c3, c4, 5); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 5 args: %f\n", b - a); + c[5] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[5]/50000); + + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy6(c1, c2, c3, c4, c5, 6); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 6 args: %f\n", b - a); + c[6] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[6]/50000); + for (i = 0; i < 50000; i++) { + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a)); + dummy7(c1, c2, c3, c4, c5, c6, 7); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b)); +// printf("Procedure call overhead with 7 args: %f\n", b - a); + c[7] += b - a - 5; + } + printf("Procedure call overhead: %f\n", c[7]/50000); +/* + for (i = 0; i < 8; i++) + printf("i: %d, time: %f\n", i, c[i]); +*/ return 0; } -- 1.9.1