├── LICENSE ├── README.md └── src ├── INDEX ├── Makefile ├── lab001.c ├── lab002.c ├── lab003.c ├── lab004.c ├── lab005.c ├── lab006.c ├── lab007.c ├── lab008.c └── lab013.c /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Brendan Gregg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # perf-labs 2 | -------------------------------------------------------------------------------- /src/INDEX: -------------------------------------------------------------------------------- 1 | lab001 - CPU hot stack. 2 | lab002 - waiting on accept(). 3 | lab003 - 0 byte successful reads. 4 | lab004 - short lived cksum processes. 5 | lab005 - sync disk writes to a file. 6 | lab006 - CPU hot stacks. 7 | lab007 - asleep in function call. 8 | lab008 - sync disk writes to a file, with (syscall) latency outliers. 9 | lab013 - out of memory by recursion. tricky. 10 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | CC=cc 2 | CFLAGS=-O0 3 | 4 | all: lab001 lab002 lab003 lab004 lab005 lab006 lab007 lab008 lab013 5 | 6 | %.o: %.c 7 | echo $(CC) -o $@ $(CFLAGS) 8 | $(CC) -o $@ $< $(CFLAGS) 9 | 10 | index: *.c 11 | sed -n '/\* lab[0-9]* - /s/...//p' *.c > INDEX 12 | 13 | clean: 14 | rm -f lab??? *.data 15 | -------------------------------------------------------------------------------- /src/lab001.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab001 - CPU hot stack. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | void 8 | func_c() 9 | { 10 | for(;;){} 11 | } 12 | 13 | void 14 | func_b() 15 | { 16 | func_c(); 17 | } 18 | 19 | void 20 | func_a() 21 | { 22 | func_b(); 23 | } 24 | 25 | int 26 | main(int argc, char *argv[]) 27 | { 28 | func_a(); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /src/lab002.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab002 - waiting on accept(). 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define BUFSIZE 256 16 | #define PORT 12340 17 | 18 | void 19 | connection_handler(int connection_fd) 20 | { 21 | int nbytes; 22 | char buffer[BUFSIZE]; 23 | 24 | nbytes = read(connection_fd, buffer, BUFSIZE); 25 | buffer[nbytes] = 0; 26 | 27 | nbytes = sprintf(buffer, "Shhh... I'm sleeping.\n"); 28 | write(connection_fd, buffer, nbytes); 29 | } 30 | 31 | void 32 | wait_for_work(int socket_fd) 33 | { 34 | int connection_fd; 35 | struct sockaddr_in client; 36 | socklen_t length; 37 | 38 | length = sizeof (client); 39 | 40 | while((connection_fd = accept(socket_fd, 41 | (struct sockaddr *) &client, &length)) > -1) { 42 | connection_handler(connection_fd); 43 | close(connection_fd); 44 | } 45 | 46 | } 47 | 48 | int 49 | main(int argc, char *argv[]) 50 | { 51 | struct sockaddr_in address; 52 | int socket_fd, connection_fd; 53 | size_t address_length; 54 | pid_t child; 55 | struct hostent *server; 56 | 57 | if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 58 | printf("ERROR: socket() failed\n"); 59 | exit(1); 60 | } 61 | 62 | bzero((char *) &address, sizeof(address)); 63 | address.sin_family = AF_INET; 64 | address.sin_port = htons(PORT); 65 | address.sin_addr.s_addr = INADDR_ANY; 66 | 67 | if (bind(socket_fd, (struct sockaddr *) &address, sizeof(address)) < 0) 68 | { 69 | printf("ERROR: bind() failed"); 70 | exit(2); 71 | } 72 | 73 | if (listen(socket_fd, 5) != 0) { 74 | printf("ERROR: listen() failed\n"); 75 | exit(3); 76 | } 77 | 78 | wait_for_work(socket_fd); 79 | 80 | close(socket_fd); 81 | return (0); 82 | } 83 | -------------------------------------------------------------------------------- /src/lab003.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab003 - 0 byte successful reads. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | const char *datafile = "lab003.data"; 15 | 16 | #define BUFSIZE (8 * 1024) 17 | #define FILESIZE (10 * 1024 * 1024) 18 | 19 | void 20 | init_data() 21 | { 22 | int fd; 23 | long long i; 24 | char buf[BUFSIZE]; 25 | 26 | if ((fd = open(datafile, O_CREAT | O_WRONLY, 0644)) < 0) { 27 | printf("ERROR: Can't open datafile: %s\n", datafile); 28 | exit(1); 29 | } 30 | 31 | for (i = 0; i < FILESIZE / BUFSIZE; i++) { 32 | if (write(fd, buf, BUFSIZE) < 0) { 33 | printf("ERROR: write error.\n"); 34 | exit(2); 35 | } 36 | } 37 | 38 | close(fd); 39 | } 40 | 41 | void 42 | load_data() 43 | { 44 | int fd; 45 | char buf[1]; 46 | 47 | if ((fd = open(datafile, O_RDONLY)) < 0) { 48 | printf("ERROR: Can't open database: %s\n", datafile); 49 | exit(2); 50 | } 51 | 52 | for (;;) { read(fd, &buf, 0); } 53 | 54 | (void) close(fd); 55 | } 56 | 57 | void 58 | import_database() 59 | { 60 | load_data(); 61 | } 62 | 63 | int 64 | main() 65 | { 66 | init_data(); 67 | 68 | for (;;) { 69 | import_database(); 70 | } 71 | 72 | return (0); 73 | } 74 | -------------------------------------------------------------------------------- /src/lab004.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab004 - short lived cksum processes. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | const char *datafile = "lab004.data"; 15 | 16 | #define BUFSIZE (8 * 1024) 17 | #define FILESIZE (10 * 1024 * 1024) 18 | 19 | void 20 | init_data() 21 | { 22 | int fd; 23 | long long i; 24 | char buf[BUFSIZE]; 25 | 26 | if ((fd = open(datafile, O_CREAT | O_WRONLY, 0644)) < 0) { 27 | printf("ERROR: Can't open datafile: %s\n", datafile); 28 | exit(1); 29 | } 30 | 31 | for (i = 0; i < FILESIZE / BUFSIZE; i++) { 32 | if (write(fd, buf, BUFSIZE) < 0) { 33 | printf("ERROR: write error.\n"); 34 | exit(2); 35 | } 36 | } 37 | 38 | close(fd); 39 | } 40 | 41 | void 42 | check_log() 43 | { 44 | char cmd[256]; 45 | snprintf(cmd, sizeof (cmd), "cksum %s >/dev/null 2>&1", datafile); 46 | 47 | system(cmd); 48 | } 49 | 50 | int 51 | main() 52 | { 53 | init_data(); 54 | 55 | for (;;) { 56 | check_log(); 57 | } 58 | 59 | return (0); 60 | } 61 | -------------------------------------------------------------------------------- /src/lab005.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab005 - sync disk writes to a file. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | const char *datafile = "lab005.data"; 15 | 16 | #define BUFSIZE (8 * 1024) 17 | #define FILESIZE (10 * 1024 * 1024) 18 | 19 | void 20 | write_log(int fd) 21 | { 22 | char buf[BUFSIZE]; 23 | int i; 24 | 25 | for (;;) { 26 | for (i = 0; i < FILESIZE / BUFSIZE; i++) { 27 | if (write(fd, buf, BUFSIZE) < 0) { 28 | printf("ERROR: write error.\n"); 29 | exit(2); 30 | } 31 | } 32 | 33 | if (lseek(fd, 0, SEEK_SET) < 0) { 34 | printf("ERROR: seek() failed.\n"); 35 | exit(3); 36 | } 37 | } 38 | } 39 | 40 | int 41 | main() 42 | { 43 | int fd; 44 | 45 | if ((fd = open(datafile, O_CREAT | O_WRONLY | O_SYNC, 0644)) < 0) { 46 | printf("ERROR: writing to %s\n", datafile); 47 | exit(1); 48 | } 49 | 50 | write_log(fd); 51 | 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /src/lab006.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab006 - CPU hot stacks. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #define SPINS (1000 * 1000) /* tune as desired */ 8 | 9 | void 10 | func_c() 11 | { 12 | int i; 13 | for (i = 0; i < SPINS; i++) { } 14 | } 15 | 16 | void 17 | func_b() 18 | { 19 | func_c(); 20 | } 21 | 22 | void 23 | func_e() 24 | { 25 | int i; 26 | for (i = 0; i < 5 * SPINS; i++) { } 27 | } 28 | 29 | void 30 | func_a() 31 | { 32 | for(;;) { 33 | func_b(); 34 | func_e(); 35 | } 36 | } 37 | 38 | int 39 | main(int argc, char *argv[]) 40 | { 41 | func_a(); 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /src/lab007.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab007 - asleep in function call. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | 9 | #define SPINS (10 * 1000 * 1000) /* tune to ~1% CPU */ 10 | 11 | void 12 | func_l() 13 | { 14 | int i, j; 15 | 16 | for(;;) { 17 | usleep(1000 * 1000); 18 | for (i = 0, j = 0; i < SPINS; i++) { j++; } 19 | } 20 | } 21 | 22 | void 23 | func_k() 24 | { 25 | func_l(); 26 | } 27 | 28 | void 29 | func_j() 30 | { 31 | func_k(); 32 | } 33 | 34 | int 35 | main(int argc, char *argv[]) 36 | { 37 | func_j(); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /src/lab008.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab008 - sync disk writes to a file, with (syscall) latency outliers. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | const char *datafile = "lab008.data"; 16 | 17 | #define BUFSIZE (8 * 1024) 18 | #define BIGSIZE (10 * 1024 * 1024) 19 | #define FILESIZE (10 * 1024 * 1024) 20 | 21 | void 22 | write_log(int fd) 23 | { 24 | char *buf, *big; 25 | long long i; 26 | int ret, j; 27 | 28 | buf = malloc(BUFSIZE); 29 | big = malloc(BIGSIZE); 30 | if (buf == NULL || big == NULL) { 31 | printf("ERROR: malloc buffers.\n"); 32 | exit(1); 33 | } 34 | bzero(buf, BUFSIZE); 35 | bzero(big, BIGSIZE); 36 | 37 | for (;;) { 38 | for (i = 0, j = 0; i < FILESIZE;) { 39 | if ((j++ % 100) == 0) { 40 | ret = write(fd, big, BIGSIZE); 41 | i += BIGSIZE; 42 | } else { 43 | ret = write(fd, buf, BUFSIZE); 44 | i += BUFSIZE; 45 | } 46 | 47 | if (ret < 0) { 48 | printf("ERROR: write error.\n"); 49 | exit(2); 50 | } 51 | } 52 | 53 | if (lseek(fd, 0, SEEK_SET) < 0) { 54 | printf("ERROR: seek() failed.\n"); 55 | exit(3); 56 | } 57 | } 58 | 59 | free(buf); 60 | free(big); 61 | } 62 | 63 | int 64 | main() 65 | { 66 | int fd; 67 | 68 | if ((fd = open(datafile, O_CREAT | O_WRONLY | O_SYNC, 0644)) < 0) { 69 | printf("ERROR: writing to %s\n", datafile); 70 | exit(1); 71 | } 72 | 73 | write_log(fd); 74 | 75 | return (0); 76 | } 77 | -------------------------------------------------------------------------------- /src/lab013.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lab013 - out of memory by recursion. tricky. 3 | * 4 | * 21-May-2015 Brendan Gregg Created this. 5 | */ 6 | 7 | void 8 | func_r() 9 | { 10 | int i; 11 | for (i = 0; i < 100; i++) { } 12 | func_r(); 13 | } 14 | 15 | int 16 | main(int argc, char *argv[]) 17 | { 18 | func_r(); 19 | return (0); 20 | } 21 | --------------------------------------------------------------------------------