Commit | Line | Data |
---|---|---|
ae46d0f5 JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #include <pthread.h> | |
5 | ||
6 | static struct thread_data { | |
4870138d | 7 | unsigned long mib; |
ae46d0f5 JA |
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 | ||
4870138d | 18 | size = td->mib * 1024UL * 1024UL; |
ae46d0f5 JA |
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) { | |
4870138d | 25 | printf("loop%d: did %lu MiB\n", i+1, size/(1024UL*1024UL)); |
ae46d0f5 JA |
26 | first = 0; |
27 | } | |
28 | } | |
a404c9bd | 29 | free(buf); |
ae46d0f5 JA |
30 | return NULL; |
31 | } | |
32 | ||
33 | int main(int argc, char *argv[]) | |
34 | { | |
4870138d | 35 | unsigned long mib, threads; |
ae46d0f5 JA |
36 | pthread_t *pthreads; |
37 | int i; | |
38 | ||
39 | if (argc < 3) { | |
4870138d | 40 | printf("%s: <MiB per thread> <threads>\n", argv[0]); |
ae46d0f5 JA |
41 | return 1; |
42 | } | |
43 | ||
4870138d | 44 | mib = strtoul(argv[1], NULL, 10); |
ae46d0f5 | 45 | threads = strtoul(argv[2], NULL, 10); |
63ef965d BVA |
46 | if (threads < 1 || threads > 65536) { |
47 | printf("%s: invalid 'threads' argument\n", argv[0]); | |
48 | return 1; | |
49 | } | |
ae46d0f5 JA |
50 | |
51 | pthreads = calloc(threads, sizeof(pthread_t)); | |
4870138d | 52 | td.mib = mib; |
ae46d0f5 JA |
53 | |
54 | for (i = 0; i < threads; i++) | |
55 | pthread_create(&pthreads[i], NULL, worker, &td); | |
56 | ||
57 | for (i = 0; i < threads; i++) { | |
58 | void *ret; | |
59 | ||
60 | pthread_join(pthreads[i], &ret); | |
61 | } | |
62 | return 0; | |
63 | } |