-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrecycle.c
112 lines (88 loc) · 2.52 KB
/
recycle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* recycle.c
*
* Copyright (C) 2007 Scott Schneider, Christos Antonopoulos
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <numa.h>
size_t min_size;
size_t max_size;
int iterations = (int)1e8;
int rate;
double random_number()
{
static long int seed = 547845897;
static long int m = 2147483647; // m is the modulus, m = 2 ^ 31 - 1
static long int a = 16807; // a is the multiplier, a = 7 ^ 5
static long int q = 127773; // q is the floor of m / a
static long int r = 2836; // r is m mod a
long int temp = a * (seed % q) - r * (seed / q);
if (temp > 0) {
seed = temp;
}
else {
seed = temp + m;
}
return (double)seed / (double)m;
}
void* simulate_work(void* arg)
{
unsigned long** reserve = malloc(rate * sizeof(void*));
int i;
int j;
double rand;
size_t object_size;
for (i = 0; i < iterations; ++i) {
if (i % rate == 0 && i != 0) {
for (j = 0; j < rate; ++j) {
free(reserve[j]);
}
}
rand = random_number();
object_size = min_size + (rand * (max_size - min_size));
reserve[i % rate] = malloc(object_size);
}
free(reserve);
return NULL;
}
void numa_start(void);
int main(int argc, char* argv[])
{
pthread_t* threads;
//numa_start();
if (argc < 5) {
printf("correct usage: recycle <num threads> <min alloc size> <max alloc size> <alloc rate>\n");
exit(0);
}
int num_threads = atoi(argv[1]);
min_size = atoi(argv[2]);
max_size = atoi(argv[3]);
rate = atoi(argv[4]);
iterations /= num_threads;
threads = (pthread_t*)malloc(num_threads * sizeof(pthread_t));
int i;
for (i = 0; i < num_threads-1; ++i) {
pthread_create(&threads[i], NULL, simulate_work, NULL);
}
simulate_work(NULL);
for (i = 0; i < num_threads-1; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}