Created
May 12, 2013 17:10
-
-
Save Rhomboid/5564255 to your computer and use it in GitHub Desktop.
Syscall overhead demonstration
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
| #define _GNU_SOURCE | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <assert.h> | |
| #include <unistd.h> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include <time.h> | |
| #include <string.h> | |
| #define BUFFER_SIZE 128 | |
| #define NUM_ITERATIONS 5000000 | |
| void __attribute__((noinline)) append_and_send(int fd, unsigned char *buf1, unsigned char *buf2) | |
| { | |
| memcpy(buf1 + BUFFER_SIZE, buf2, BUFFER_SIZE); | |
| send(fd, buf1, 2 * BUFFER_SIZE, 0); | |
| } | |
| static void send_twice(int fd, unsigned char *buf1, unsigned char *buf2) | |
| { | |
| send(fd, buf1, BUFFER_SIZE, 0); | |
| send(fd, buf2, BUFFER_SIZE, 0); | |
| } | |
| static double timediff(const struct timespec *before, const struct timespec *after) | |
| { | |
| return after->tv_sec - before->tv_sec + (after->tv_nsec - before->tv_nsec) / 1000000000.; | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| assert(argc == 3); | |
| struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(atoi(argv[2])) }; | |
| inet_aton(argv[1], &addr.sin_addr); | |
| int sockfd = socket(PF_INET, SOCK_STREAM, 0); | |
| assert(sockfd != -1); | |
| assert(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == 0); | |
| unsigned char buf1[BUFFER_SIZE * 2], buf2[BUFFER_SIZE]; | |
| memset(buf1, 0xaa, sizeof(buf1)); | |
| memset(buf2, 0x55, sizeof(buf2)); | |
| struct timespec before, after; | |
| double t1, t2; | |
| clock_gettime(CLOCK_MONOTONIC, &before); | |
| for(size_t cnt = 0; cnt < NUM_ITERATIONS; ++cnt) | |
| append_and_send(sockfd, buf1, buf2); | |
| clock_gettime(CLOCK_MONOTONIC, &after); | |
| printf(" with memcpy: %.3f seconds\n", t1 = timediff(&before, &after)); | |
| clock_gettime(CLOCK_MONOTONIC, &before); | |
| for(size_t cnt = 0; cnt < NUM_ITERATIONS; ++cnt) | |
| send_twice(sockfd, buf1, buf2); | |
| clock_gettime(CLOCK_MONOTONIC, &after); | |
| printf("with 2x send: %.3f seconds\n", t2 = timediff(&before, &after)); | |
| printf(" difference: %.3fx\n", t2 / t1); | |
| close(sockfd); | |
| } |
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
| $ gcc -Wall -pedantic -std=c99 -O2 20130512.c -lrt | |
| $ nc -l localhost 8888 >/dev/null & sleep 0.5; ./a.out localhost 8888 | |
| [1] 17687 | |
| with memcpy: 2.020 seconds | |
| with 2x send: 3.532 seconds | |
| difference: 1.748x | |
| [1]+ Done nc -l localhost 8888 > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment