|
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 |
f4b844 |
enum { Len = 1000 };
|
|
nsz |
f4b844 |
|
|
nsz |
7d87d3 |
void bench_malloc_sparse(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j = 0; j < N; j++) {
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc(4000);
|
|
nsz |
f4b844 |
memset(p[i], 0, 4000);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
if (i%150) free(p[i]);
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_bubble(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j = 0; j < N; j++) {
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc(4000);
|
|
nsz |
f4b844 |
memset(p[i], 0, 4000);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_tiny1(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j=0; j
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc((i%4+1)*16);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
f4b844 |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_tiny2(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j=0; j
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc((i%4+1)*16);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
f4b844 |
free(p[0]);
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_big1(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j = 0; j < N; j++) {
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc((i%4+1)*16384);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
f4b844 |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
}
|
|
nsz |
dc655a |
|
|
nsz |
7d87d3 |
void bench_malloc_big2(int N) {
|
|
nsz |
f4b844 |
void *p[Len];
|
|
nsz |
f4b844 |
int i,j;
|
|
nsz |
f4b844 |
|
|
nsz |
f4b844 |
for (j = 0; j < N; j++) {
|
|
nsz |
f4b844 |
for (i=0; i
|
|
nsz |
f4b844 |
p[i] = malloc((i%4+1)*16384);
|
|
nsz |
f4b844 |
}
|
|
nsz |
f4b844 |
for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
|
|
nsz |
f4b844 |
free(p[i]);
|
|
nsz |
f4b844 |
free(p[0]);
|
|
nsz |
dc655a |
}
|
|
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 |
}
|