|
nsz |
dc655a |
#include <stdio.h>
|
|
nsz |
dc655a |
#include <stdlib.h>
|
|
nsz |
dc655a |
#include <string.h>
|
|
nsz |
dc655a |
#include <pthread.h>
|
|
nsz |
dc655a |
#include "test.h"
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_sparse(int N) {
|
|
nsz |
dc655a |
void *p[N];
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc(4000);
|
|
nsz |
dc655a |
memset(p[i], 0, 4000);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
if (i%150) free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_bubble(int N) {
|
|
nsz |
dc655a |
void *p[N];
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc(4000);
|
|
nsz |
dc655a |
memset(p[i], 0, 4000);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_tiny1(int N) {
|
|
nsz |
dc655a |
void **p = malloc(N * sizeof *p);
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc((i%4+1)*16);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
free(p);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_tiny2(int N) {
|
|
nsz |
dc655a |
void **p = malloc(N * sizeof *p);
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc((i%4+1)*16);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
if (N>1) for (i=1; i; i = (i+1999)%N)
|
|
nsz |
dc655a |
free(p[i]);
|
|
nsz |
dc655a |
free(p[0]);
|
|
nsz |
dc655a |
free(p);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_big1(int N) {
|
|
nsz |
dc655a |
void *p[N];
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc((i%4+1)*16384);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_big2(int N) {
|
|
nsz |
dc655a |
void *p[N];
|
|
nsz |
dc655a |
size_t i;
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
p[i] = malloc((i%4+1)*16384);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
if (N>1) for (i=1; i; i = (i+1999)%(sizeof p/sizeof *p))
|
|
nsz |
dc655a |
free(p[i]);
|
|
nsz |
dc655a |
free(p[0]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
#define SH_COUNT 300
|
|
nsz |
dc655a |
#define PV_COUNT 300
|
|
nsz |
dc655a |
#define MAX_SZ 500
|
|
nsz |
dc655a |
#define DEF_SZ 40
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
struct foo {
|
|
nsz |
dc655a |
void *mem;
|
|
nsz |
dc655a |
pthread_mutex_t lock;
|
|
nsz |
dc655a |
};
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
static unsigned rng(unsigned *r)
|
|
nsz |
dc655a |
{
|
|
nsz |
dc655a |
return *r = *r * 1103515245 + 12345;
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
static int N;
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
static void *stress(void *arg)
|
|
nsz |
dc655a |
{
|
|
nsz |
dc655a |
struct foo *foo = arg;
|
|
nsz |
dc655a |
unsigned r = (unsigned)pthread_self();
|
|
nsz |
dc655a |
int i, j;
|
|
nsz |
dc655a |
size_t sz;
|
|
nsz |
dc655a |
void *p;
|
|
nsz |
dc655a |
|
|
nsz |
dc655a |
for (i=0; i
|
|
nsz |
dc655a |
j = rng(&r) % SH_COUNT;
|
|
nsz |
dc655a |
sz = rng(&r) % MAX_SZ;
|
|
nsz |
dc655a |
pthread_mutex_lock(&foo[j].lock);
|
|
nsz |
dc655a |
p = foo[j].mem;
|
|
nsz |
dc655a |
foo[j].mem = 0;
|
|
nsz |
dc655a |
pthread_mutex_unlock(&foo[j].lock);
|
|
nsz |
dc655a |
free(p);
|
|
nsz |
dc655a |
if (!p) {
|
|
nsz |
dc655a |
p = malloc(sz);
|
|
nsz |
dc655a |
pthread_mutex_lock(&foo[j].lock);
|
|
nsz |
dc655a |
if (!foo[j].mem) foo[j].mem = p, p = 0;
|
|
nsz |
dc655a |
pthread_mutex_unlock(&foo[j].lock);
|
|
nsz |
dc655a |
free(p);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
return 0;
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_thread_stress(int n) {
|
|
nsz |
dc655a |
struct foo foo[SH_COUNT] = {{0}};
|
|
nsz |
dc655a |
pthread_t td1, td2;
|
|
nsz |
dc655a |
void *res;
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
N = n;
|
|
nsz |
dc655a |
pthread_create(&td1, 0, stress, foo);
|
|
nsz |
dc655a |
pthread_create(&td2, 0, stress, foo);
|
|
nsz |
dc655a |
pthread_join(td1, &res;;
|
|
nsz |
dc655a |
pthread_join(td2, &res;;
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_thread_local(int n) {
|
|
nsz |
dc655a |
struct foo foo1[SH_COUNT] = {{0}};
|
|
nsz |
dc655a |
struct foo foo2[SH_COUNT] = {{0}};
|
|
nsz |
dc655a |
pthread_t td1, td2;
|
|
nsz |
dc655a |
void *res;
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
N = n;
|
|
nsz |
dc655a |
pthread_create(&td1, 0, stress, foo1);
|
|
nsz |
dc655a |
pthread_create(&td2, 0, stress, foo2);
|
|
nsz |
dc655a |
pthread_join(td1, &res;;
|
|
nsz |
dc655a |
pthread_join(td2, &res;;
|
|
nsz |
dc655a |
}
|