Commit c1f4bba6b2576f42bf0ad0c24ac13c35b5ee1964

Authored by Ajay Mohan
1 parent b20e8571ee
Exists in master

Thread Ctx Switches

Showing 3 changed files with 155 additions and 2 deletions Side-by-side Diff

memory/thd_ctx_mtx.c View file @ c1f4bba
  1 +#include<stdio.h>
  2 +#include<stdlib.h>
  3 +#include<unistd.h>
  4 +#include<pthread.h>
  5 +#include<sys/time.h>
  6 +#include "rasperf.h"
  7 +#define NUM_THREADS 2
  8 +#define ITERATIONS 1000
  9 +
  10 +pthread_mutex_t LOCK;
  11 +pthread_mutex_t TRIGGER;
  12 +pthread_cond_t COND;
  13 +volatile unsigned int cs_count = 0;
  14 +
  15 +// Uptil now TRIGGER WAS LOCKED AND THEN RELEASED
  16 +void * function(void *id){
  17 + int t_id = *(int *)id;
  18 +
  19 + // Wait for trigger from parent
  20 + pthread_mutex_lock(&TRIGGER);
  21 + pthread_mutex_unlock(&TRIGGER); // So that both start at the same time
  22 + // Get lock
  23 + pthread_mutex_lock(&LOCK); //only one thread passes in
  24 +
  25 + // Wake up other thread if context switch count is greater than 0
  26 + if (cs_count > 0){
  27 + pthread_cond_signal(&COND);
  28 + }
  29 +
  30 + while(cs_count < ITERATIONS) {
  31 +
  32 + // Wait for other thread to wake up us
  33 + cs_count++; //Increment cs count
  34 + pthread_cond_wait(&COND, &LOCK); //release LOCK and wait for COND
  35 +
  36 + // wake up other thread
  37 + pthread_cond_signal(&COND);
  38 + }
  39 + pthread_mutex_unlock(&LOCK);
  40 + pthread_exit(NULL);
  41 +}
  42 +
  43 +
  44 +int main(){
  45 +
  46 + pthread_t threads[NUM_THREADS];
  47 + int rc, i,id1=1, id2=2;
  48 + time start, end;
  49 + int t;
  50 +
  51 + pthread_mutex_init(&LOCK, NULL); // to protect CS
  52 + pthread_mutex_init(&TRIGGER, NULL); //This is to start at the same time
  53 + pthread_cond_init(&COND, NULL); // to wake each other
  54 +
  55 + pthread_mutex_lock(&TRIGGER); //to stop the threads we are going to create
  56 +
  57 +
  58 + rc = pthread_create(&threads[0], NULL, function, (void*)&id1); //create first thread
  59 + if (rc){
  60 + perror("Thread creation failed!");
  61 + }
  62 + rc = pthread_create(&threads[1], NULL, function, (void*)&id2); //create second thread
  63 + if (rc){
  64 + perror("Thread creation failed!");
  65 + }
  66 +
  67 + getTime(start); // start time
  68 + pthread_mutex_unlock(&TRIGGER); // by releasing the trigger start the threads
  69 +
  70 + // Wait for threads to finish
  71 + for(i=0; i<NUM_THREADS; i++) {
  72 + pthread_join(threads[i], NULL);
  73 + }
  74 +
  75 + getTime(end);
  76 + t = diff(end, start);
  77 + //printf("%f\n",(t/(CPU_FREQ*ITERATIONS)) - TIME_MEASUREMENT_OVERHEAD);
  78 +
  79 + printf("Time Taken %d",t);
  80 + // Clean up
  81 + pthread_mutex_destroy(&LOCK);
  82 + pthread_mutex_destroy(&TRIGGER);
  83 + pthread_cond_destroy(&COND);
  84 + pthread_exit(NULL);
  85 +}
  86 +
  87 +
  88 +
memory/thd_ctxsw.c View file @ c1f4bba
  1 +#define _GNU_SOURCE
  2 +#include<sched.h>
  3 +#include "rasperf.h"
  4 +#include<stdio.h>
  5 +#include<unistd.h>
  6 +#include<stdlib.h>
  7 +#include<signal.h>
  8 +#define STACK_SIZE 4096
  9 +int pipe_fd[2], tid1, tid2;
  10 +char buffer[10];
  11 +int reader(void* arg);
  12 +int writer(void* arg);
  13 +int controller(void *arg)
  14 +{
  15 + void *tid1_stack = malloc(STACK_SIZE);
  16 + void *tid2_stack = malloc(STACK_SIZE);
  17 + tid1 = clone(&writer, tid1_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL);
  18 + tid2 = clone(&reader, tid2_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL);
  19 + return 0;
  20 +}
  21 +int reader(void* arg)
  22 +{
  23 + read(pipe_fd[0], buffer, 1);
  24 + return 0;
  25 +}
  26 +int writer(void* arg)
  27 +{
  28 + write(pipe_fd[1], "a", 1);
  29 + return 0;
  30 +}
  31 +int main(){
  32 +
  33 + unsigned int regr;
  34 + time start, end;
  35 + unsigned int avg;
  36 + int status, i;
  37 + void *child_stack = malloc(STACK_SIZE);
  38 + int thread_pid;
  39 +
  40 + //asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
  41 + //printf("regr: %x\n", regr);
  42 +
  43 + //initialize communication pipe
  44 + for (i = 0; i < 1000; i++)
  45 + {
  46 + getTime(start);
  47 + thread_pid = clone(&controller, child_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL);
  48 + waitpid(thread_pid, 0, 0);
  49 + getTime(end);
  50 + printf("\nTime Taken %d", diff(end,start));
  51 + }
  52 + return 0;
  53 +}
1 1 #ifndef __RASPERF_H__
2 2 #define __RASPERF_H__
3   -
  3 +#define PI 1
  4 +#define x86 0
  5 +#if defined(PI) && PI
  6 +#define time unsigned int
4 7 #define getTime(var) \
5 8 asm volatile (\
6 9 "mrc p15, 0, %0, c9, c13, 0":\
... ... @@ -9,4 +12,13 @@
9 12 asm volatile (\
10 13 "mrc p15, 0, %0, c9, c14, 0":\
11 14 "=r" (var));
  15 +#define diff(end, begin) (end - begin)
  16 +#endif
  17 +#if defined(x86) && x86
  18 +#define time struct timeval
  19 +#define getTime(var) gettimeofday(&var, NULL)
  20 +#define diff(end, begin) \
  21 + (end.tv_sec*1e6+end.tv_usec - begin.tv_sec*1e6+ begin.tv_usec)
  22 +#endif
  23 +#endif