| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <pthread.h> |
| 5 | |
| 6 | static struct thread_data { |
| 7 | unsigned long mb; |
| 8 | } td; |
| 9 | |
| 10 | static void *worker(void *data) |
| 11 | { |
| 12 | struct thread_data *td = data; |
| 13 | unsigned long index; |
| 14 | size_t size; |
| 15 | char *buf; |
| 16 | int i, first = 1; |
| 17 | |
| 18 | size = td->mb * 1024UL * 1024UL; |
| 19 | buf = malloc(size); |
| 20 | |
| 21 | for (i = 0; i < 100000; i++) { |
| 22 | for (index = 0; index + 4096 < size; index += 4096) |
| 23 | memset(&buf[index+512], 0x89, 512); |
| 24 | if (first) { |
| 25 | printf("loop%d: did %lu MB\n", i+1, size/(1024UL*1024UL)); |
| 26 | first = 0; |
| 27 | } |
| 28 | } |
| 29 | return NULL; |
| 30 | } |
| 31 | |
| 32 | int main(int argc, char *argv[]) |
| 33 | { |
| 34 | unsigned long mb, threads; |
| 35 | pthread_t *pthreads; |
| 36 | int i; |
| 37 | |
| 38 | if (argc < 3) { |
| 39 | printf("%s: <mb per thread> <threads>\n", argv[0]); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | mb = strtoul(argv[1], NULL, 10); |
| 44 | threads = strtoul(argv[2], NULL, 10); |
| 45 | |
| 46 | pthreads = calloc(threads, sizeof(pthread_t)); |
| 47 | td.mb = mb; |
| 48 | |
| 49 | for (i = 0; i < threads; i++) |
| 50 | pthread_create(&pthreads[i], NULL, worker, &td); |
| 51 | |
| 52 | for (i = 0; i < threads; i++) { |
| 53 | void *ret; |
| 54 | |
| 55 | pthread_join(pthreads[i], &ret); |
| 56 | } |
| 57 | return 0; |
| 58 | } |