Commits

Anonymous committed 220cddf

Init commit

Comments (0)

Files changed (11)

dirmaker/Makefile

+CC=gcc
+CFLAGS=-O2 -Wall -pthread
+
+default: dirmaker dirmaker_fork
+
+dirmaker: dirmaker.o
+	$(CC) -o $@ $^ $(CFLAGS)
+
+dirmaker_fork: dirmaker_fork.o
+	$(CC) -o $@ $^ $(CFLAGS)
+
+clean:
+	rm -rf *.o
+	rm -rf dirmaker
+#ifndef _BENCH_H_
+#define _BENCH_H_
+
+#include <sched.h>
+
+static int
+affinity_set(int cpu)
+{
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(cpu, &cpuset);
+    return sched_setaffinity(0, sizeof(cpuset), &cpuset);
+}
+
+static uint64_t
+get_cpu_freq(void)
+{
+    FILE *fd;
+    uint64_t freq = 0;
+    float freqf = 0;
+    char *line = NULL;
+    size_t len = 0;
+
+    fd = fopen("/proc/cpuinfo", "r");
+    if (!fd) {
+	fprintf(stderr, "failed to get cpu frequecy\n");
+	perror(NULL);
+	return freq;
+    }
+
+    while (getline(&line, &len, fd) != EOF) {
+	if (sscanf(line, "cpu MHz\t: %f", &freqf) == 1) {
+	    freqf = freqf * 1000000UL;
+	    freq = (uint64_t)freqf;
+	    break;
+	}
+    }
+
+    fclose(fd);
+    return freq;
+}
+
+static uint64_t
+read_tsc(void)
+{
+    uint32_t a, d;
+    __asm __volatile("rdtsc" : "=a" (a), "=d" (d));
+    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_ */

dirmaker/config.h

+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#define PATH_PREFIX "/tmp/_dirs/"
+#define NR_SUBDIRS  20
+#define NR_ALLDIRS  (NR_SUBDIRS*NR_SUBDIRS*NR_SUBDIRS)
+
+#endif /* _CONFIG_H_ */

dirmaker/dirmaker.c

+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "bench.h"
+
+volatile uint64_t count = 0;
+int nr_threads = 2;
+
+static int
+create_dir(const char *name)
+{
+    int fd;
+    
+    if ((fd = open(name, O_RDONLY)) < 0) {
+        if ((fd = mkdir(name, S_IRUSR | S_IWUSR | S_IXUSR)) < 0) {
+            /* perror(""); */
+            fd = open(name, O_RDONLY);
+        } else {
+            /* printf("%s created.\n", name); */
+            count ++;
+        }
+    } else {
+    }
+
+    close(fd);
+
+    return fd;
+}
+
+void *
+worker(void *args)
+{
+    int id = (long) args;
+    int l1n, l2n, l3n;
+    int i;
+    char dirname[1024];
+
+    affinity_set(id);
+
+    for (i = NR_ALLDIRS / nr_threads * id; i < NR_ALLDIRS / nr_threads * (id+1); i++) {
+        l1n = i / NR_SUBDIRS / NR_SUBDIRS;
+        l2n = (i - l1n * NR_SUBDIRS * NR_SUBDIRS) / NR_SUBDIRS;
+        l3n = i - l1n * NR_SUBDIRS * NR_SUBDIRS - l2n * NR_SUBDIRS;
+
+        sprintf(dirname, "%s%d", PATH_PREFIX, l1n);
+        create_dir(dirname);
+        sprintf(dirname, "%s%d/%d", PATH_PREFIX, l1n, l2n);
+        create_dir(dirname);
+        sprintf(dirname, "%s%d/%d/%d", PATH_PREFIX, l1n, l2n, l3n);
+        create_dir(dirname);
+    }
+
+    return NULL;
+}
+
+int
+main(int argc, char **argv)
+{
+    pthread_t tid[32];
+    int i;
+    uint64_t start, end, usec;
+
+    if (argc > 1) {
+        nr_threads = atoi(argv[1]);
+    }
+    
+    printf("Create directories...\n");
+    start = read_tsc();
+    create_dir(PATH_PREFIX);
+    for (i = 0; i < nr_threads; i++) {
+        pthread_create(&tid[i], NULL, worker, (void *) (long) i);
+    }
+
+    for (i = 0; i < nr_threads; i++) {
+        pthread_join(tid[i], NULL);
+    }
+    
+    end = read_tsc();
+    usec = (end - start) * 1000000 / get_cpu_freq();
+    printf("usec: %ld\t\n", usec);
+
+    printf("Cleanup directories...\n");
+    system("rm -rf /tmp/_dirs");
+    
+    return 0;
+}

dirmaker/dirmaker_fork

Binary file added.

dirmaker/dirmaker_fork.c

+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "bench.h"
+
+volatile uint64_t count = 0;
+int nr_threads = 2;
+
+static int
+create_dir(const char *name)
+{
+    int fd;
+    
+    if ((fd = open(name, O_RDONLY)) < 0) {
+        if ((fd = mkdir(name, S_IRUSR | S_IWUSR | S_IXUSR)) < 0) {
+            /* perror(""); */
+            fd = open(name, O_RDONLY);
+        } else {
+            /* printf("%s created.\n", name); */
+            count ++;
+        }
+    } else {
+    }
+
+    close(fd);
+
+    return fd;
+}
+
+void *
+worker(void *args)
+{
+    int id = (long) args;
+    int l1n, l2n, l3n;
+    int i;
+    char dirname[1024];
+
+    affinity_set(id);
+
+    for (i = NR_ALLDIRS / nr_threads * id; i < NR_ALLDIRS / nr_threads * (id+1); i++) {
+        l1n = i / NR_SUBDIRS / NR_SUBDIRS;
+        l2n = (i - l1n * NR_SUBDIRS * NR_SUBDIRS) / NR_SUBDIRS;
+        l3n = i - l1n * NR_SUBDIRS * NR_SUBDIRS - l2n * NR_SUBDIRS;
+
+        sprintf(dirname, "%s%d", PATH_PREFIX, l1n);
+        create_dir(dirname);
+        sprintf(dirname, "%s%d/%d", PATH_PREFIX, l1n, l2n);
+        create_dir(dirname);
+        sprintf(dirname, "%s%d/%d/%d", PATH_PREFIX, l1n, l2n, l3n);
+        create_dir(dirname);
+    }
+
+    return NULL;
+}
+
+int
+main(int argc, char **argv)
+{
+    pid_t pid[32], p;
+    int i;
+    uint64_t start, end, usec;
+
+    printf("Create directories...\n");
+    start = read_tsc();
+    create_dir(PATH_PREFIX);
+    for (i = 0; i < nr_threads; i++) {
+        p = fork();
+        if (p == 0) {
+            worker((void *) (long) i);
+            exit(0);
+        }
+        pid[i] = p;
+    }
+
+    for (i = 0; i < nr_threads; i++) {
+        waitpid(pid[i], NULL, 0);
+    }
+    
+    end = read_tsc();
+    usec = (end - start) * 1000000 / get_cpu_freq();
+    printf("usec: %ld\t\n", usec);
+
+    printf("Cleanup directories...\n");
+    /* system("rm -rf /tmp/_dirs"); */
+    
+    return 0;
+}
+CC=gcc
+CFLAGS=-O2 -Wall -pthread
+
+socketrun: socketrun.o helper.o
+	$(CC) -o $@ $^ $(CFLAGS)
+
+clean:
+	rm -rf *.o
+	rm -rf socketrun