Commit 8a0c6a1f7218a89e38f9d022820c5e95e5c21141

Authored by Ajay Mohan
1 parent c4520c7e90
Exists in master

TCP Rtt Server

Showing 5 changed files with 192 additions and 0 deletions Side-by-side Diff

cpu/thd_ctx_mtx.c View file @ 8a0c6a1
  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 +
cpu/thd_ctxsw View file @ 8a0c6a1

No preview for this file type

cpu/thd_ctxsw.c View file @ 8a0c6a1
  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 +}

No preview for this file type

network/tcp_rtt_server.c View file @ 8a0c6a1
  1 +#include "../rasperf.h"
  2 +#include<arpa/inet.h>
  3 +#include<sys/typesh>
  4 +#include<sys/socket.h>
  5 +#include<netdb.h>
  6 +
  7 +//Macro definitions
  8 +#define handle_error(msg) \
  9 + do { perror(msg); exit(EXIT_FAILURE);} while(0)
  10 +#define LISTEN_BACKLOG 5
  11 +int main(int argc, char*argv[])
  12 +{
  13 + int socket_fd, rc;
  14 + if(argc<1){
  15 + handle_error("<usage>:");
  16 + }
  17 + //Retrieve the protocol number mapping
  18 + //from /etc/protocols file
  19 + struct protoent *proto_entry;
  20 + proto_entry = getprotobyname("tcp");
  21 +
  22 + //Open an socket end-point for this TCP Server
  23 + socket_fd = socket(AF_INET, SOCK_STREAM, proto_entry->p_proto);
  24 + if(socket_fd == -1){
  25 + handle_error("Errno: %d,Socket descriptor creation failed", errorno);
  26 + }
  27 +
  28 + //My address information to bind to
  29 + struct sockaddr sock_addr;
  30 + memset(&sock_addr, 0, sizeof(struct sockaddr));
  31 + sock_addr.sa_family = AF_INET;
  32 + sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  33 + sock_addr.sin_port = htons(8000);
  34 +
  35 +
  36 + rc = bind(sock_fd, &sock_addr, sizeof(sock_addr));
  37 + if(rc == -1){
  38 + handle_error("Errno: %d, Bind failure", errorno);
  39 + }
  40 + rc = listen(socket_fd, LISTEN_BACKLOG);
  41 + if(rc == -1){
  42 + handle_error("Errno: %d, Listen Error", errorno);
  43 + }
  44 +
  45 + int addr_size = sizeof(struct sockaddr);
  46 + rc= accept(sock_fd, &sock_addr, &addr_size);
  47 + if( rc == -1){
  48 + handle_error("Error: %d,Accept failed", errorno);
  49 + }
  50 +