Created
May 3, 2011 20:03
-
-
Save davisp/954111 to your computer and use it in GitHub Desktop.
Webscale writes!
This file contains hidden or 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
#include <fcntl.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#include <sys/time.h> | |
#define SAMPLES 10000 | |
int | |
main() | |
{ | |
void* dest = NULL; | |
size_t len = 1024; | |
size_t pos = 0; | |
int prot = PROT_READ | PROT_WRITE; | |
int flags = MAP_FILE | MAP_PRIVATE; | |
unsigned char* buf[1024]; | |
ssize_t bread; | |
// Unfortunately mmaping /dev/null fails. :( | |
//int fd = open("/dev/null", O_RDWR); | |
int fd = open("foo", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | |
int count; | |
unsigned long samples[SAMPLES]; | |
double rate; | |
struct timeval before; | |
struct timeval after; | |
int i; | |
if(fd < 0) { | |
perror("foo"); | |
fprintf(stderr, "Failed to open webscale portal.\n"); | |
goto done; | |
} | |
if(ftruncate(fd, len) != 0) { | |
fprintf(stderr, "Failed to initialize webscale portal.\n"); | |
goto done; | |
} | |
dest = mmap(NULL, len, prot, flags, fd, 0); | |
if(dest == NULL) { | |
fprintf(stderr, "Failed to map webscale portal.\n"); | |
goto done; | |
} | |
fprintf(stderr, "Webscale writes!\n"); | |
count = 0; | |
do { | |
gettimeofday(&before, NULL); | |
bread = fread(buf, 1, 1024, stdin); | |
if(pos + bread > len) { | |
memcpy(dest+pos, buf, len-pos); | |
pos = (len-pos); | |
memcpy(dest, buf+pos, bread-pos); | |
} else if(bread > 0) { | |
memcpy(dest+pos, buf, bread); | |
pos += bread; | |
} | |
gettimeofday(&after, NULL); | |
count = (count + 1) % SAMPLES; | |
samples[count] = (after.tv_sec * 1000000 + after.tv_usec) - | |
(before.tv_sec * 1000000 + before.tv_usec); | |
rate = 0.0; | |
for(i = 0; i < SAMPLES; i++) { | |
rate += (double) samples[i] / 1000000.0; | |
} | |
rate = (SAMPLES * 1024.0) / rate; | |
rate /= (1024.0 * 1024.0); | |
if(count == 0) { | |
fprintf(stderr, " \r%0.2f Mib/s", rate); | |
} | |
} while(bread > 0); | |
done: | |
if(fd >= 0) close(fd); | |
fprintf(stderr, "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment