fnmatch: fix "[/b" test
[libc-test] / src / malloc / bench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pthread.h>
5 #include "test.h"
6
7 enum { Len = 1000 };
8
9 void bench_malloc_sparse(int N) {
10         void *p[Len];
11         int i,j;
12
13         for (j = 0; j < N; j++) {
14                 for (i=0; i<sizeof p/sizeof *p; i++) {
15                         p[i] = malloc(4000);
16                         memset(p[i], 0, 4000);
17                 }
18                 for (i=0; i<sizeof p/sizeof *p; i++)
19                         if (i%150) free(p[i]);
20                 for (i=0; i<sizeof p/sizeof *p; i+=150)
21                         free(p[i]);
22         }
23 }
24
25 void bench_malloc_bubble(int N) {
26         void *p[Len];
27         int i,j;
28
29         for (j = 0; j < N; j++) {
30                 for (i=0; i<sizeof p/sizeof *p; i++) {
31                         p[i] = malloc(4000);
32                         memset(p[i], 0, 4000);
33                 }
34                 for (i=0; i<sizeof p/sizeof *p; i++)
35                         free(p[i]);
36         }
37 }
38
39 void bench_malloc_tiny1(int N) {
40         void *p[Len];
41         int i,j;
42
43         for (j=0; j<N; j++) {
44                 for (i=0; i<sizeof p/sizeof *p; i++) {
45                         p[i] = malloc((i%4+1)*16);
46                 }
47                 for (i=0; i<sizeof p/sizeof *p; i++) {
48                         free(p[i]);
49                 }
50         }
51 }
52
53 void bench_malloc_tiny2(int N) {
54         void *p[Len];
55         int i,j;
56
57         for (j=0; j<N; j++) {
58                 for (i=0; i<sizeof p/sizeof *p; i++) {
59                         p[i] = malloc((i%4+1)*16);
60                 }
61                 for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
62                         free(p[i]);
63                 free(p[0]);
64         }
65 }
66
67 void bench_malloc_big1(int N) {
68         void *p[Len];
69         int i,j;
70
71         for (j = 0;  j < N; j++) {
72                 for (i=0; i<sizeof p/sizeof *p; i++) {
73                         p[i] = malloc((i%4+1)*16384);
74                 }
75                 for (i=0; i<sizeof p/sizeof *p; i++) {
76                         free(p[i]);
77                 }
78         }
79 }
80
81 void bench_malloc_big2(int N) {
82         void *p[Len];
83         int i,j;
84
85         for (j = 0; j < N; j++) {
86                 for (i=0; i<sizeof p/sizeof *p; i++) {
87                         p[i] = malloc((i%4+1)*16384);
88                 }
89                 for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
90                         free(p[i]);
91                 free(p[0]);
92         }
93 }
94
95
96 #define SH_COUNT 300
97 #define PV_COUNT 300
98 #define MAX_SZ 500
99 #define DEF_SZ 40
100
101 struct foo {
102         void *mem;
103         pthread_mutex_t lock;
104 };
105
106 static unsigned rng(unsigned *r)
107 {
108         return *r = *r * 1103515245 + 12345;
109 }
110
111 static int N;
112
113 static void *stress(void *arg)
114 {
115         struct foo *foo = arg;
116         unsigned r = (unsigned)pthread_self();
117         int i, j;
118         size_t sz;
119         void *p;
120
121         for (i=0; i<N; i++) {
122                 j = rng(&r) % SH_COUNT;
123                 sz = rng(&r) % MAX_SZ;
124                 pthread_mutex_lock(&foo[j].lock);
125                 p = foo[j].mem;
126                 foo[j].mem = 0;
127                 pthread_mutex_unlock(&foo[j].lock);
128                 free(p);
129                 if (!p) {
130                         p = malloc(sz);
131                         pthread_mutex_lock(&foo[j].lock);
132                         if (!foo[j].mem) foo[j].mem = p, p = 0;
133                         pthread_mutex_unlock(&foo[j].lock);
134                         free(p);
135                 }
136         }
137         return 0;
138 }
139
140 void bench_malloc_thread_stress(int n) {
141         struct foo foo[SH_COUNT] = {{0}};
142         pthread_t td1, td2;
143         void *res;
144
145         N = n;
146         pthread_create(&td1, 0, stress, foo);
147         pthread_create(&td2, 0, stress, foo);
148         pthread_join(td1, &res);
149         pthread_join(td2, &res);
150 }
151
152 void bench_malloc_thread_local(int n) {
153         struct foo foo1[SH_COUNT] = {{0}};
154         struct foo foo2[SH_COUNT] = {{0}};
155         pthread_t td1, td2;
156         void *res;
157
158         N = n;
159         pthread_create(&td1, 0, stress, foo1);
160         pthread_create(&td2, 0, stress, foo2);
161         pthread_join(td1, &res);
162         pthread_join(td2, &res);
163 }