From c1f4bba6b2576f42bf0ad0c24ac13c35b5ee1964 Mon Sep 17 00:00:00 2001 From: Ajay Mohan Date: Sun, 6 Mar 2016 21:17:18 -0800 Subject: [PATCH] Thread Ctx Switches --- memory/thd_ctx_mtx.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ memory/thd_ctxsw.c | 53 +++++++++++++++++++++++++++++++ rasperf.h | 15 +++++++-- 3 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 memory/thd_ctx_mtx.c create mode 100644 memory/thd_ctxsw.c diff --git a/memory/thd_ctx_mtx.c b/memory/thd_ctx_mtx.c new file mode 100644 index 0000000..7dafb9a --- /dev/null +++ b/memory/thd_ctx_mtx.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include "rasperf.h" +#define NUM_THREADS 2 +#define ITERATIONS 1000 + +pthread_mutex_t LOCK; +pthread_mutex_t TRIGGER; +pthread_cond_t COND; +volatile unsigned int cs_count = 0; + +// Uptil now TRIGGER WAS LOCKED AND THEN RELEASED +void * function(void *id){ + int t_id = *(int *)id; + + // Wait for trigger from parent + pthread_mutex_lock(&TRIGGER); + pthread_mutex_unlock(&TRIGGER); // So that both start at the same time + // Get lock + pthread_mutex_lock(&LOCK); //only one thread passes in + + // Wake up other thread if context switch count is greater than 0 + if (cs_count > 0){ + pthread_cond_signal(&COND); + } + + while(cs_count < ITERATIONS) { + + // Wait for other thread to wake up us + cs_count++; //Increment cs count + pthread_cond_wait(&COND, &LOCK); //release LOCK and wait for COND + + // wake up other thread + pthread_cond_signal(&COND); + } + pthread_mutex_unlock(&LOCK); + pthread_exit(NULL); +} + + +int main(){ + + pthread_t threads[NUM_THREADS]; + int rc, i,id1=1, id2=2; + time start, end; + int t; + + pthread_mutex_init(&LOCK, NULL); // to protect CS + pthread_mutex_init(&TRIGGER, NULL); //This is to start at the same time + pthread_cond_init(&COND, NULL); // to wake each other + + pthread_mutex_lock(&TRIGGER); //to stop the threads we are going to create + + + rc = pthread_create(&threads[0], NULL, function, (void*)&id1); //create first thread + if (rc){ + perror("Thread creation failed!"); + } + rc = pthread_create(&threads[1], NULL, function, (void*)&id2); //create second thread + if (rc){ + perror("Thread creation failed!"); + } + + getTime(start); // start time + pthread_mutex_unlock(&TRIGGER); // by releasing the trigger start the threads + + // Wait for threads to finish + for(i=0; i +#include "rasperf.h" +#include +#include +#include +#include +#define STACK_SIZE 4096 +int pipe_fd[2], tid1, tid2; +char buffer[10]; +int reader(void* arg); +int writer(void* arg); +int controller(void *arg) +{ + void *tid1_stack = malloc(STACK_SIZE); + void *tid2_stack = malloc(STACK_SIZE); + tid1 = clone(&writer, tid1_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + tid2 = clone(&reader, tid2_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + return 0; +} +int reader(void* arg) +{ + read(pipe_fd[0], buffer, 1); + return 0; +} +int writer(void* arg) +{ + write(pipe_fd[1], "a", 1); + return 0; +} +int main(){ + + unsigned int regr; + time start, end; + unsigned int avg; + int status, i; + void *child_stack = malloc(STACK_SIZE); + int thread_pid; + + //asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); + //printf("regr: %x\n", regr); + + //initialize communication pipe + for (i = 0; i < 1000; i++) + { + getTime(start); + thread_pid = clone(&controller, child_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + waitpid(thread_pid, 0, 0); + getTime(end); + printf("\nTime Taken %d", diff(end,start)); + } + return 0; +} diff --git a/rasperf.h b/rasperf.h index 57422ad..e0e76b4 100644 --- a/rasperf.h +++ b/rasperf.h @@ -1,6 +1,9 @@ #ifndef __RASPERF_H__ #define __RASPERF_H__ - +#define PI 1 +#define x86 0 +#if defined(PI) && PI +#define time unsigned int #define getTime(var) \ asm volatile (\ "mrc p15, 0, %0, c9, c13, 0":\ @@ -9,5 +12,13 @@ asm volatile (\ "mrc p15, 0, %0, c9, c14, 0":\ "=r" (var)); - +#define diff(end, begin) (end - begin) +#endif +#if defined(x86) && x86 +#define time struct timeval +#define getTime(var) gettimeofday(&var, NULL) +#define diff(end, begin) \ + (end.tv_sec*1e6+end.tv_usec - begin.tv_sec*1e6+ begin.tv_usec) +#endif +#endif -- 1.9.1