diff --git a/osproject.c b/osproject.c new file mode 100644 index 0000000..8ce65f9 --- /dev/null +++ b/osproject.c @@ -0,0 +1,54 @@ +/* + * * hello.c The simplest kernel module. + * */ +#include <stdio.h> + +#define ARMV7_PMNC_E (1 << 0) /* Enable all counters */ +#define ARMV7_PMNC_P (1 << 1) /* Reset all counters */ +#define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */ +#define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */ +#define ARMV7_PMNC_X (1 << 4) /* Export to ETM */ +#define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/ +#define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */ +#define ARMV7_PMNC_N_MASK 0x1f +#define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */ + +/*101b9173 +static u32 armv7_pmnc_read(void) +{ + u32 val; + asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val)); + return val; +} + +static void armv7_pmnc_write(u32 val) +{ + val &= ARMV7_PMNC_MASK; + isb(); + asm volatile("mcr p15, 0, %0, c9, c12, 0" :: "r"(val)); +} +*/ + +static void dummy() +{ +} + +int main() +{ + unsigned int regr; + 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++) { + 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); + } + + + return 1; +} diff --git a/osproject_cswitch.c b/osproject_cswitch.c new file mode 100644 index 0000000..2bcdf62 --- /dev/null +++ b/osproject_cswitch.c @@ -0,0 +1,80 @@ +/* + * cswitch.c -- + * + * Simple program to test context switching by sending short + * messages back and forth through a pipe. Invocation: + * + * cswitch count + * + * Count tells how many times a one-byte message should be sent + * back and forth between two processes. + * + * Copyright 1987, 1989 Regents of the University of California + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies. The University of California + * makes no representations about the suitability of this + * software for any purpose. It is provided "as is" without + * express or implied warranty. + */ + +#ifndef lint +static char rcsid[] = "$Header: /sprite/src/benchmarks/cswitch/RCS/cswitch.c,v 1.1 89/08/31 13:19:37 ouster Exp $ SPRITE (Berkeley)"; +#endif not lint + +#include <stdio.h> +#include <sys/time.h> + +main(argc,argv) + int argc; + char **argv; +{ + int count, i, toSlave[2], fromSlave[2]; + int pid; + char buffer[10]; + struct timeval begin ,end; + int micros; + double timePer; + extern int errno; + + if (argc != 2 ) { + fprintf(stderr, "Ping takes one argument: count.\n"); + return; + } + count = 0; + sscanf(argv[1], "%d", &count); + pipe(toSlave); + pipe(fromSlave); + pid = fork(); + if (pid == 0) { + write(fromSlave[1], "a", 1); + while (1) { + read(toSlave[0], buffer, 1); + if (buffer[0] != 'a') { + exit(0); + } + write(fromSlave[1], "a", 1); + } + } + if (pid == -1) { + fprintf(stderr, "Couldn't fork slave process: error %d.\n", + errno); + return; + } + read(fromSlave[0], buffer, 1); + gettimeofday(&begin, (struct timezone *) NULL); + for (i = 0; i < count; i++) { + write(toSlave[1], "a", 1); + read(fromSlave[0], buffer, 1); + } + gettimeofday(&end, (struct timezone *) NULL); + micros = 1000000*(end.tv_sec - begin.tv_sec) + + end.tv_usec - begin.tv_usec; + write(toSlave[1], "b", 1); + timePer = micros; + timePer /= count * 1000; + + printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n", + timePer); +} diff --git a/osproject_pipe.c b/osproject_pipe.c new file mode 100644 index 0000000..dcf7ecf --- /dev/null +++ b/osproject_pipe.c @@ -0,0 +1,86 @@ +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> + +/* 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 new file mode 100644 index 0000000..5238a36 --- /dev/null +++ b/osproject_syscall.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int main() +{ + unsigned int regr; + unsigned int i; + unsigned int a, b; + + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); + printf("regr: %x\n", regr); + + regr = 0; + while ( regr < 10) + { + 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); + regr++; + } + return 1; +} diff --git a/osproject_task.c b/osproject_task.c new file mode 100644 index 0000000..b1f8e49 --- /dev/null +++ b/osproject_task.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +int main() +{ + unsigned int regr; + unsigned int a, b; + unsigned int avg; + 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++; + } + + printf("Fork avg: %u\n", avg); + return 1; +} diff --git a/osproject_thread.c b/osproject_thread.c new file mode 100644 index 0000000..730b009 --- /dev/null +++ b/osproject_thread.c @@ -0,0 +1,39 @@ +#include<stdio.h> +#include<string.h> +#include<pthread.h> +#include<stdlib.h> +#include<unistd.h> + +pthread_t tid[100]; + +void* doSomeThing(void *arg) +{ +} + +int main(void) +{ + int i = 0; + int err; + int avg = 0; + int a, b; + + printf("Entering the thing\n"); + + 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("Avg thread creation time is %d\n", avg/100); + return 1; +} diff --git a/osproject_threadpipe.c b/osproject_threadpipe.c new file mode 100644 index 0000000..73afa27 --- /dev/null +++ b/osproject_threadpipe.c @@ -0,0 +1,69 @@ +#include<stdio.h> +#include<string.h> +#include<pthread.h> +#include<stdlib.h> +#include<unistd.h> +#include<sys/types.h> +#include<sys/time.h> + +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 new file mode 100644 index 0000000..10b7a39 --- /dev/null +++ b/osproject_time.c @@ -0,0 +1,111 @@ +#include <stdio.h> + +/* +static void dummy() +{ +} +*/ + +int dummy1(int a) +{ + int i = a; + return i; +} + +int dummy2(int a, int b) +{ + int i = b; + return i; +} + +int dummy3(int a, int b, int c) +{ + int i = c; + return i; +} + +int 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) +{ + int i = e; + return i; +} + +int 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) +{ + int i = g; + return i; +} + +int main() +{ + unsigned int regr; + unsigned int a, b; + unsigned int c1, c2, c3, c4, c5, c6, c7; + int d; + + 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++) { + 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); + } + + 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); + + return 0; +}