Commit c71458d20f17ef747cb314f2526701454e35335a

Authored by Sathya Narayanan
1 parent 4d907d60f9
Exists in master

Context switch working

Showing 1 changed file with 28 additions and 40 deletions Inline Diff

osproject_cswitch.c View file @ c71458d
/* 1 1 /*
* cswitch.c -- 2 2 * cswitch.c --
* 3 3 *
* Simple program to test context switching by sending short 4 4 * Simple program to test context switching by sending short
* messages back and forth through a pipe. Invocation: 5 5 * messages back and forth through a pipe. Invocation:
* 6 6 *
* cswitch count 7 7 * cswitch count
* 8 8 *
* Count tells how many times a one-byte message should be sent 9 9 * Count tells how many times a one-byte message should be sent
* back and forth between two processes. 10 10 * back and forth between two processes.
* 11 11 *
* Copyright 1987, 1989 Regents of the University of California 12 12 * Copyright 1987, 1989 Regents of the University of California
* Permission to use, copy, modify, and distribute this 13 13 * Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without 14 14 * software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright 15 15 * fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California 16 16 * notice appear in all copies. The University of California
* makes no representations about the suitability of this 17 17 * makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without 18 18 * software for any purpose. It is provided "as is" without
* express or implied warranty. 19 19 * express or implied warranty.
*/ 20 20 */
21 21
#ifndef lint 22
static char rcsid[] = "$Header: /sprite/src/benchmarks/cswitch/RCS/cswitch.c,v 1.1 89/08/31 13:19:37 ouster Exp $ SPRITE (Berkeley)"; 23
#endif not lint 24
25
#include <stdio.h> 26 22 #include <stdio.h>
#include <sys/time.h> 27 23 int main(int argc, char **argv)
28
main(argc,argv) 29
int argc; 30
char **argv; 31
{ 32 24 {
int count, i, toSlave[2], fromSlave[2]; 33 25 int count, i, toSlave[2], fromSlave[2];
int pid; 34 26 int pid;
char buffer[10]; 35 27 char buffer[10];
struct timeval begin ,end; 36 28 unsigned int begin ,end;
int micros; 37
double timePer; 38 29 double timePer;
extern int errno; 39 30 unsigned int regr;
31 asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
32 printf("regr: %x\n", regr);
40 33
if (argc != 2 ) { 41 34 count = 10;
fprintf(stderr, "Ping takes one argument: count.\n"); 42
return; 43
} 44
count = 0; 45
sscanf(argv[1], "%d", &count); 46
pipe(toSlave); 47 35 pipe(toSlave);
pipe(fromSlave); 48 36 pipe(fromSlave);
pid = fork(); 49 37 pid = fork();
if (pid == 0) { 50 38 if (pid == 0) {
write(fromSlave[1], "a", 1); 51 39 write(fromSlave[1], "a", 1);
while (1) { 52 40 while (1) {
read(toSlave[0], buffer, 1); 53 41 read(toSlave[0], buffer, 1);
if (buffer[0] != 'a') { 54 42 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (end));
exit(0); 55 43 if (buffer[0] != 'a') {
} 56 44 printf("Exiting\n");
write(fromSlave[1], "a", 1); 57 45 return -1;
} 58 46 }
47 //write(fromSlave[1], "a", 1);
48 printf("end: %u\n", end);
49 }
} 59 50 }
if (pid == -1) { 60 51 if (pid == -1) {
fprintf(stderr, "Couldn't fork slave process: error %d.\n", 61 52 printf("Couldn't fork slave process: error %d.\n");
errno); 62 53 return;
return; 63 54 }
} 64
read(fromSlave[0], buffer, 1); 65 55 read(fromSlave[0], buffer, 1);
gettimeofday(&begin, (struct timezone *) NULL); 66
for (i = 0; i < count; i++) { 67 56 for (i = 0; i < count; i++) {
write(toSlave[1], "a", 1); 68 57 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (begin));
read(fromSlave[0], buffer, 1); 69 58 write(toSlave[1], "a", 1);
59 //read(fromSlave[0], buffer, 1);
60 usleep(10000);
61 printf("Start: %u\n", begin);
} 70 62 }
gettimeofday(&end, (struct timezone *) NULL); 71
micros = 1000000*(end.tv_sec - begin.tv_sec) 72
+ end.tv_usec - begin.tv_usec; 73
write(toSlave[1], "b", 1); 74 63 write(toSlave[1], "b", 1);
timePer = micros; 75
timePer /= count * 1000; 76
77 64
65 timePer = (end - begin)/count;
printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n", 78 66 printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n",
timePer); 79 67 timePer);
} 80 68 }
81 69