9 void bench_malloc_sparse(int N) {
13 for (j = 0; j < N; j++) {
14 for (i=0; i<sizeof p/sizeof *p; i++) {
16 memset(p[i], 0, 4000);
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)
25 void bench_malloc_bubble(int N) {
29 for (j = 0; j < N; j++) {
30 for (i=0; i<sizeof p/sizeof *p; i++) {
32 memset(p[i], 0, 4000);
34 for (i=0; i<sizeof p/sizeof *p; i++)
39 void bench_malloc_tiny1(int N) {
44 for (i=0; i<sizeof p/sizeof *p; i++) {
45 p[i] = malloc((i%4+1)*16);
47 for (i=0; i<sizeof p/sizeof *p; i++) {
53 void bench_malloc_tiny2(int N) {
58 for (i=0; i<sizeof p/sizeof *p; i++) {
59 p[i] = malloc((i%4+1)*16);
61 for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
67 void bench_malloc_big1(int N) {
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);
75 for (i=0; i<sizeof p/sizeof *p; i++) {
81 void bench_malloc_big2(int N) {
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);
89 for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
103 pthread_mutex_t lock;
106 static unsigned rng(unsigned *r)
108 return *r = *r * 1103515245 + 12345;
113 static void *stress(void *arg)
115 struct foo *foo = arg;
116 unsigned r = (unsigned)pthread_self();
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);
127 pthread_mutex_unlock(&foo[j].lock);
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);
140 void bench_malloc_thread_stress(int n) {
141 struct foo foo[SH_COUNT] = {{0}};
146 pthread_create(&td1, 0, stress, foo);
147 pthread_create(&td2, 0, stress, foo);
148 pthread_join(td1, &res);
149 pthread_join(td2, &res);
152 void bench_malloc_thread_local(int n) {
153 struct foo foo1[SH_COUNT] = {{0}};
154 struct foo foo2[SH_COUNT] = {{0}};
159 pthread_create(&td1, 0, stress, foo1);
160 pthread_create(&td2, 0, stress, foo2);
161 pthread_join(td1, &res);
162 pthread_join(td2, &res);