Compare View

switch
from
...
to
 
Commits (3)

Diff

Showing 7 changed files Side-by-side Diff

memory_access_time.c View file @ 6c373d6
... ... @@ -0,0 +1,45 @@
  1 +#include<stdio.h>
  2 +#include <sys/time.h>
  3 +#include <stdlib.h>
  4 +
  5 +#define KB 1024
  6 +#define MB 1024 * KB
  7 +#define MAX_ARRAY_SIZE 32*MB
  8 +#define STRIDE_SIZE 16
  9 +#define CPU_FREQ 900
  10 +
  11 +typedef unsigned long long int u64;
  12 +
  13 +u64 access_time_1(u64 size, int stride_size){
  14 + struct timeval start, end;
  15 + struct timezone tz;
  16 +
  17 + int *array = (int *)malloc(size);
  18 + int len = (size)/sizeof(int);
  19 + unsigned int i;
  20 +
  21 + gettimeofday(&start, &tz);
  22 + for(i=0; i < MAX_ARRAY_SIZE; i++) {
  23 + array[( i * stride_size) % len]++;
  24 + }
  25 + gettimeofday(&end, &tz);
  26 +
  27 + free(array);
  28 + return ((end.tv_sec * 1e6) + end.tv_usec - (start.tv_sec * 1e6) - start.tv_usec);
  29 +}
  30 +
  31 +int main()
  32 +{
  33 + int i, j;
  34 +
  35 + for (j = 4; j <= 256; j*=2) {
  36 + printf("Stride size: %u\n", j);
  37 + for (i = 1024; i <= 32 * MB; i*=2) {
  38 + u64 access_time = access_time_1(i, j);
  39 + u64 overhead = 10;
  40 + double avg = (access_time * 1000.0)/(2 * MAX_ARRAY_SIZE);
  41 + printf("Size: %uKB access: %lluus, avg: %gns\n", i >> 10, access_time, avg - overhead);
  42 + }
  43 + }
  44 + return 0;
  45 +}
osproject_memory_ram_access.c View file @ 6c373d6
... ... @@ -0,0 +1,19 @@
  1 +#include <stdio.h>
  2 +
  3 +int main()
  4 +{
  5 + unsigned int regr;
  6 + unsigned int a, b, i;
  7 + int arraya[8192] = {0};
  8 + //int arrayb[8192];
  9 + unsigned int avg = 0;
  10 + int temp;
  11 +
  12 + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
  13 + printf("regr: %x\n", regr);
  14 +
  15 + regr = 0;
  16 +
  17 + printf("Time for 256: %u\n", avg/1024);
  18 + return 1;
  19 +}
osproject_syscall.c View file @ 6c373d6
... ... @@ -5,23 +5,14 @@ int main()
5 5 unsigned int regr;
6 6 unsigned int i;
7 7 unsigned int a, b;
8   - float avg = 0.0;
9 8  
10 9 asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
11 10 printf("regr: %x\n", regr);
12 11  
13 12 regr = 0;
14   - while ( regr < 10000)
15   - {
16   - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a));
17   - i = getpid();
18   - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b));
19   - if (regr==0)
20   - printf("PID: %u, Time for getpid: %u\n", i, b - a);
21   - else
22   - avg+=(b-a);
23   - regr++;
24   - }
25   - printf("Time: %f\n", avg/9999);
  13 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a));
  14 + i = getpid();
  15 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b));
  16 + printf("PID: %u, Time for getpid: %u\n", i, b - a);
26 17 return 1;
27 18 }
osproject_thread_creation.c View file @ 6c373d6
... ... @@ -0,0 +1,29 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <pthread.h>
  4 +
  5 +int func(void *arg) {
  6 + return 0;
  7 +}
  8 +
  9 +#define STACK_SIZE 4096
  10 +
  11 +int main()
  12 +{
  13 + unsigned int regr;
  14 + unsigned int a, b;
  15 + unsigned int avg;
  16 + int status;
  17 + void *child_stack = malloc(STACK_SIZE);
  18 + int thread_pid;
  19 +
  20 + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
  21 + printf("regr: %x\n", regr);
  22 +
  23 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (a));
  24 + thread_pid = clone(&func, child_stack+STACK_SIZE, CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES, NULL);
  25 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (b));
  26 + printf("Time for fork: %u\n", b - a);
  27 +
  28 + return 0;
  29 +}
osproject_thread_time.c View file @ 6c373d6
... ... @@ -0,0 +1,51 @@
  1 +#include <sched.h>
  2 +#include <pthread.h>
  3 +#include <unistd.h>
  4 +#include <stdio.h>
  5 +#include <stdlib.h>
  6 +#include <time.h>
  7 +#include <string.h>
  8 +#include <errno.h>
  9 +
  10 +static inline long long unsigned time_ns(struct timespec* const ts) {
  11 + if (clock_gettime(CLOCK_REALTIME, ts)) {
  12 + exit(1);
  13 + }
  14 + return ((long long unsigned) ts->tv_sec) * 1000000000LLU
  15 + + (long long unsigned) ts->tv_nsec;
  16 +}
  17 +
  18 +static const int iterations = 500000;
  19 +
  20 +static void* thread(void*ctx) {
  21 + (void)ctx;
  22 + int i;
  23 + for (i = 0; i < iterations; i++)
  24 + sched_yield();
  25 + return NULL;
  26 +}
  27 +
  28 +int main(void) {
  29 + struct sched_param param;
  30 + int i;
  31 + param.sched_priority = 1;
  32 + if (sched_setscheduler(getpid(), SCHED_FIFO, &param))
  33 + fprintf(stderr, "sched_setscheduler(): %s\n", strerror(errno));
  34 +
  35 + struct timespec ts;
  36 + pthread_t thd;
  37 + if (pthread_create(&thd, NULL, thread, NULL)) {
  38 + return 1;
  39 + }
  40 +
  41 + long long unsigned start_ns = time_ns(&ts);
  42 + for (i = 0; i < iterations; i++)
  43 + sched_yield();
  44 + long long unsigned delta = time_ns(&ts) - start_ns;
  45 +
  46 + const int nswitches = iterations << 2;
  47 + printf("%i thread context switches in %lluns (%.1fns/ctxsw)\n",
  48 + nswitches, delta, (delta / (float) nswitches));
  49 + return 0;
  50 +}
  51 +
osproject_tswitch.c View file @ 6c373d6
... ... @@ -0,0 +1,82 @@
  1 +/*
  2 + * cswitch.c --
  3 + *
  4 + * Simple program to test context switching by sending short
  5 + * messages back and forth through a pipe. Invocation:
  6 + *
  7 + * cswitch count
  8 + *
  9 + * Count tells how many times a one-byte message should be sent
  10 + * back and forth between two processes.
  11 + *
  12 + * Copyright 1987, 1989 Regents of the University of California
  13 + * Permission to use, copy, modify, and distribute this
  14 + * software and its documentation for any purpose and without
  15 + * fee is hereby granted, provided that the above copyright
  16 + * notice appear in all copies. The University of California
  17 + * makes no representations about the suitability of this
  18 + * software for any purpose. It is provided "as is" without
  19 + * express or implied warranty.
  20 + */
  21 +
  22 +#include <stdio.h>
  23 +int count, i, toSlave[2], fromSlave[2];
  24 +
  25 +void *fn1()
  26 +{
  27 + char buffer[10];
  28 + unsigned int end;
  29 + write(fromSlave[1], "a", 1);
  30 + while (1) {
  31 + read(toSlave[0], buffer, 1);
  32 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (end));
  33 + printf("end: %u\n", end);
  34 + if (buffer[0] != 'a') {
  35 + return NULL;
  36 + }
  37 + //write(fromSlave[1], "a", 1);
  38 + }
  39 +}
  40 +
  41 +void *fn2()
  42 +{
  43 + unsigned int begin;
  44 + char buffer[10];
  45 + read(fromSlave[0], buffer, 1);
  46 + for (i = 0; i < count; i++) {
  47 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (begin));
  48 + write(toSlave[1], "a", 1);
  49 + //read(fromSlave[0], buffer, 1);
  50 + printf("Start: %u\n", begin);
  51 +// usleep(10000);
  52 + }
  53 + write(toSlave[1], "b", 1);
  54 +}
  55 +
  56 +int main(int argc, char **argv)
  57 +{
  58 + int pid;
  59 + char buffer[10];
  60 + unsigned int begin ,end;
  61 + double timePer;
  62 + unsigned int regr;
  63 + int thread1, thread2;
  64 + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
  65 + printf("regr: %x\n", regr);
  66 +
  67 + count = 10;
  68 + pipe(toSlave);
  69 + pipe(fromSlave);
  70 + if(pthread_create(&thread1, NULL, &fn1, NULL)) {
  71 + printf("1 not created\n");
  72 + exit(1);
  73 + }
  74 +
  75 + if(pthread_create(&thread2, NULL, &fn2, NULL)) {
  76 + printf("2 not created\n");
  77 + exit(1);
  78 + }
  79 + pthread_join(thread1, NULL);
  80 + pthread_join(thread2, NULL);
  81 + return 0;
  82 +}
osproject_tswitch_aravind.c View file @ 6c373d6
... ... @@ -0,0 +1,81 @@
  1 +/*
  2 + * cswitch.c --
  3 + *
  4 + * Simple program to test context switching by sending short
  5 + * messages back and forth through a pipe. Invocation:
  6 + *
  7 + * cswitch count
  8 + *
  9 + * Count tells how many times a one-byte message should be sent
  10 + * back and forth between two processes.
  11 + *
  12 + * Copyright 1987, 1989 Regents of the University of California
  13 + * Permission to use, copy, modify, and distribute this
  14 + * software and its documentation for any purpose and without
  15 + * fee is hereby granted, provided that the above copyright
  16 + * notice appear in all copies. The University of California
  17 + * makes no representations about the suitability of this
  18 + * software for any purpose. It is provided "as is" without
  19 + * express or implied warranty.
  20 + */
  21 +
  22 +#include <stdio.h>
  23 +#include <stdlib.h>
  24 +#include <pthread.h>
  25 +
  26 +int func(void *arg) {
  27 + return 0;
  28 +}
  29 +
  30 +#define STACK_SIZE 4096
  31 +
  32 +
  33 +int main(int argc, char **argv)
  34 +{
  35 + int count, i, toSlave[2], fromSlave[2];
  36 + int pid;
  37 + char buffer[10];
  38 + unsigned int begin ,end;
  39 + double timePer;
  40 + unsigned int regr;
  41 + void *child_stack = malloc(STACK_SIZE);
  42 + int thread_pid;
  43 + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
  44 + printf("regr: %x\n", regr);
  45 +
  46 + count = 10;
  47 + pipe(toSlave);
  48 + pipe(fromSlave);
  49 + //pid = fork();
  50 + pid = clone(&func, child_stack+STACK_SIZE, CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES, NULL);
  51 + if (pid == 0) {
  52 + write(fromSlave[1], "a", 1);
  53 + while (1) {
  54 + read(toSlave[0], buffer, 1);
  55 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (end));
  56 + if (buffer[0] != 'a') {
  57 + printf("Exiting\n");
  58 + return -1;
  59 + }
  60 + //write(fromSlave[1], "a", 1);
  61 + printf("end: %u\n", end);
  62 + }
  63 + }
  64 + if (pid == -1) {
  65 + printf("Couldn't fork slave process: error %d.\n");
  66 + return;
  67 + }
  68 + read(fromSlave[0], buffer, 1);
  69 + for (i = 0; i < count; i++) {
  70 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (begin));
  71 + write(toSlave[1], "a", 1);
  72 + //read(fromSlave[0], buffer, 1);
  73 + usleep(10000);
  74 + printf("Start: %u\n", begin);
  75 + }
  76 + write(toSlave[1], "b", 1);
  77 +
  78 + timePer = (end - begin)/count;
  79 + printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n",
  80 + timePer);
  81 +}