Commits

Anonymous committed 1a52e63 Merge

Merge

Comments (0)

Files changed (6)

sembench/Makefile

 CC=gcc
 CFLAGS=-O2 -Wall -pthread
 
-default: sembench pingpong
-
-sembench: sembench.o config.h
-	$(CC) -o $@ $^ $(CFLAGS)
+default: pingpong
 
 pingpong: pingpong.o config.h
 	$(CC) -o $@ $^ $(CFLAGS)
 
 clean:
 	rm -rf *.o
-	rm -rf sembench pingpong
+	rm -rf pingpong
 #ifndef _BENCH_H_
 #define _BENCH_H_
 
-#include <sched.h>
-
 static int
 affinity_set(int cpu)
 {
     return ((uint64_t) a) | (((uint64_t) d) << 32);
 }
 
-typedef uint64_t atomic_t;
-
-#define LOCK_PREFIX \
-		".section .smp_locks,\"a\"\n"	\
-		"  .align 4\n"			\
-		"  .long 661f\n" /* address */	\
-		".previous\n"			\
-	       	"661:\n\tlock; "
-
-static __inline__ void
-atomic_add(int i, uint64_t *v)
-{
-	__asm__ __volatile__(
-		LOCK_PREFIX "addl %1,%0"
-		:"+m" (v)
-		:"ir" (i));
-}
 
 #endif /* _BENCH_H_ */

sembench/config.h

 #define _CONFIG_H_
 
 #define NR_PINGPONGS 100000
+#define MAX_CORES    32
 
 #endif /* _CONFIG_H_ */

sembench/pingpong.c

+#define _GNU_SOURCE
+
 #include <sys/types.h>
 #include <unistd.h>   
 #include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/wait.h>
 #include <sys/sem.h>
+#include <stdint.h>
+#include <sched.h>
 
 #include "config.h"
+#include "bench.h"
 
 static int
 ping(int semid)
 int 
 main(int argc, char *argv[]) 
 {
-    int semid;
-    key_t semkey;
-    pid_t pid;
-    int i;
+    int semid[MAX_CORES];
+    key_t semkey[MAX_CORES], key;
+    pid_t pid[MAX_CORES * 2];
+    int i, id;
+    uint64_t start, end, usec;
+    int ncores;
 
     if (argc > 1) {
-        semkey = atoi(argv[1]);
-        semid = semget(semkey, 2, 0666);
+        ncores = atoi(argv[1]);
     } else {
-        semkey = 1001;
-        if ((semid = semget(semkey, 2, 0666 | IPC_CREAT)) < 0)
-            exit(-1);
-        /* Init semephore array with {1, 0}, so the ping process will run first */
-        semctl(semid, 0, SETVAL, 1);
-        semctl(semid, 1, SETVAL, 0);
+        printf("Usage: ./pingpong <number of cores>\n");
+        exit(-1);
     }
 
-    printf("pingpong begin with semkey=%d, semid=%d\n", semkey, semid);
-    pid = fork();
-    if (pid == 0) {
-        for (i = 0; i < NR_PINGPONGS; i++) {
-            ping(semid);
+    /* Init semephores */
+    key = 1000;
+    for (i = 0; i < ncores; i++) {
+        do {
+            key ++;
+        } while ((semid[i] = semget(key, 2, 0666 | IPC_CREAT)) < 0);
+        /* Init semephore array with {1, 0}, so the ping process will run first */
+        semkey[i] = key;
+        semctl(semid[i], 0, SETVAL, 1);
+        semctl(semid[i], 1, SETVAL, 0);
+    }
+    
+    start = read_tsc();
+    for (id = 0; id < ncores; id++) {
+        if ((pid[id * 2] = fork()) == 0) {
+            affinity_set(id);
+            /* printf("ping begin with semkey=%d, semid=%d on core#%d\n", semkey[id], semid[id], id); */
+            for (i = 0; i < NR_PINGPONGS; i++) {
+                ping(semid[id]);
+            }
+            exit(0);
+        } else {
+            if ((pid[id * 2 + 1] = fork()) == 0) {
+                affinity_set(id);
+                /* printf("pong begin with semkey=%d, semid=%d on core#%d\n", semkey[id], semid[id], id); */
+                for (i = 0; i < NR_PINGPONGS; i++) {
+                    pong(semid[id]);
+                }
+                exit(0);
+            }
         }
-    } else {
-        for (i = 0; i < NR_PINGPONGS; i++) {
-            pong(semid);
-        }
-        waitpid(pid, NULL, 0);
     }
 
+    for (i = 0; i < ncores * 2; i++) {
+        waitpid(pid[i], NULL, 0);
+    }
+
+    end = read_tsc();
+    usec = (end - start) * 1000000 / get_cpu_freq();
+    printf("usec: %ld\t\n", usec);
+
     return 0;
 }

sembench/runtest.py

 
 def warmup():
     print('Warming up...')
-    p = Popen('./sembench 1', shell=True, stdout=PIPE)
+    p = Popen('./pingpong 1', shell=True, stdout=PIPE)
     os.waitpid(p.pid, 0)
 
 def test():
     for n in corelist:
         stats = []
         for i in xrange(repeat):
-            p = Popen('./sembench %d' % n, shell=True, stdout=PIPE)
+            p = Popen('./pingpong %d' % n, shell=True, stdout=PIPE)
             os.waitpid(p.pid, 0)
             output = p.stdout.read().strip()
             usec = int(pattern.search(output).group(1))
             stats.append(usec)
             log.write('%d: %d\n' % (n, usec))
-            print('%d: %d' % (n, usec))
+            # print('%d: %d' % (n, usec))
         print('Core #%d: averge %.0f, std deviation%% %.2f' % (n, avg(stats), (stddev(stats) / avg(stats))))
 
 warmup()

sembench/sembench.c

-#define _GNU_SOURCE
-
-#include <sys/socket.h>       /*  socket definitions        */
-#include <sys/types.h>        /*  socket types              */
-#include <arpa/inet.h>        /*  inet (3) funtions         */
-#include <unistd.h>           /*  misc. UNIX functions      */
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <pthread.h>
-#include <assert.h>
-#include <netdb.h>
-#include <sys/sem.h>
-#include <sys/ipc.h>
-#include <sys/wait.h>
-#include <sched.h>
-
-#include "bench.h"
-
-int 
-main(int argc, char *argv[]) {
-    int nchilds = 1;
-    int i;
-    int semid[32], semkey[32];
-    key_t key;
-    uint64_t start, end, usec;
-    pid_t pid[32];
-    char buf[1024];
-
-    if (argv[1] != NULL) {
-        nchilds = atoi(argv[1]);
-    }
-
-    key = 1000;
-    for (i = 0; i < nchilds; i++) {
-        printf("Getting semaphore #%d...", i);
-        do {
-            key ++;
-        } while ((semid[i] = semget(key, 2, 0666 | IPC_CREAT)) < 0);
-        printf("<%d>\n", key);
-        semkey[i] = key;
-        /* Init semephore array with {1, 0}, so the ping process will run first */
-        semctl(semid[i], 0, SETVAL, 1);
-        semctl(semid[i], 1, SETVAL, 0);
-    }
-
-    printf("Running ping pong programs...\n");
-    start = read_tsc();
-    for (i = 0; i < nchilds; i++) {
-        pid[i] = fork();
-        affinity_set(i);
-        if (pid[i] == 0) {
-            sprintf(buf, "%d", semkey[i]);
-            execlp("./pingpong", "./pingpong", buf, (char *) NULL);
-            perror("execlp");
-            exit(-1);
-        }
-    }
-
-    for (i = 0; i < nchilds; i++) {
-        if (waitpid(pid[i], (int *) NULL, 0) < 0)
-            perror("waitpid");
-    }
-    
-    end = read_tsc();
-    usec = (end - start) * 1000000 / get_cpu_freq();
-    printf("usec: %ld\t\n", usec);
-
-    return 0;
-}