Created
January 24, 2013 15:59
-
-
Save masami256/4623750 to your computer and use it in GitHub Desktop.
sync()にどの程度かかるか計測用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Howto build | |
// $ gcc sync_test.c -lrt -lpthread -Wall | |
// rt for clock_gettime() | |
// pthread for pthread libraries | |
// | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <pthread.h> | |
#include <time.h> | |
#define DATA_SIZE (1024 * 1024) | |
static double *results; | |
static void show_result(int count) | |
{ | |
int i = 0; | |
double average = 0.0f; | |
double total = 0.0f; | |
for (i = 0; i < count; i++) { | |
total += results[i]; | |
// printf("Writng 1MiB data took %5.5fs\n", results[i]); | |
} | |
average = total / (double) count; | |
printf("Writing %d files average is %5.5fs\n", count, average); | |
} | |
static inline double get_msec(const struct timespec *st) | |
{ | |
return (double) st->tv_sec + (double) st->tv_nsec * 1e-9; | |
} | |
static void *start(void *arg) | |
{ | |
int fd = 0; | |
char *buf = NULL; | |
char name[256] = { 0 }; | |
struct timespec start, end; | |
long cnt = (long) arg; | |
snprintf(name, sizeof(name) - 1, "./%010ld_test.bin", cnt); | |
fd = open(name, O_CREAT | O_RDWR); | |
assert(fd > 0); | |
buf = malloc(DATA_SIZE); | |
assert(buf != NULL); | |
memset(buf, 0x0, DATA_SIZE); | |
clock_gettime(CLOCK_REALTIME, &start); | |
write(fd, buf, DATA_SIZE); | |
sync(); | |
clock_gettime(CLOCK_REALTIME, &end); | |
results[cnt] = get_msec(&end) - get_msec(&start); | |
free(buf); | |
close(fd); | |
return NULL; | |
} | |
int main(int argc, char **argv) | |
{ | |
int count; | |
pthread_t *threads; | |
int ret; | |
long i; | |
if (argc != 2) { | |
printf("usage: %s [number of threads]\n", argv[0]); | |
exit(-1); | |
} | |
count = atoi(argv[1]); | |
assert(count > 0); | |
results = malloc(sizeof(double) * count); | |
threads = malloc(sizeof(pthread_t) * count); | |
for (i = 0; i < count; i++) { | |
ret = pthread_create(&threads[i], NULL, &start, (void *) i); | |
assert(ret == 0); | |
} | |
for (i = 0; i < count; i++) { | |
ret = pthread_join(threads[i], NULL); | |
assert(ret == 0); | |
} | |
show_result(count); | |
free(results); | |
free(threads); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment