-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_art.c
247 lines (202 loc) · 3.85 KB
/
mem_art.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#define _GNU_SOURCE
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <pthread.h>
#include <sched.h>
#define max(a,b) ((a) > (b) ? (a) : (b))
struct list
{
struct list *next;
/* Romiar wypełnienia jest zmienny */
long pad[0];
} *list;
#define CLOCK_TYPE CLOCK_MONOTONIC_RAW
#define N (10*1000*1000)
#define MAXTHREADS 16
static pthread_barrier_t barrier;
static unsigned iterations;
static float times[MAXTHREADS];
static int write;
uint64_t getclock()
{
struct timespec ts;
if (clock_gettime
(CLOCK_TYPE, &ts) != 0)
{
perror("clock_gettime");
exit(1);
}
return (uint64_t) ts.tv_sec *
1000000000 + ts.tv_nsec;
}
void permutation(unsigned *l, size_t n)
{
int i, k;
unsigned tmp;
for (k = n - 1; k > 1; k--)
{
i = rand() % k;
tmp = l[i];
l[i] = l[k];
l[k] = tmp;
}
}
static struct list *meminit(size_t size,
size_t line,
int shuffle)
{
unsigned i;
unsigned n = size / line;
struct list *p;
char *l;
unsigned *words;
l = mmap(NULL, size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE |
MAP_ANONYMOUS, -1, 0);
if (l == NULL)
{
perror("mmap");
exit(1);
}
if (madvise(l, size, MADV_NOHUGEPAGE))
{
perror("madvice");
exit(1);
}
words =
malloc((n + 1) * sizeof(*words));
for (i = 1; i < n; i++)
words[i] = n - i - 1;
words[0] = n - 1;
if (shuffle)
permutation(words, n - 1);
p = (struct list *) l;
for (i = 0; i < n; i++)
{
p->next =
(struct list *)
&l[words[i] * line];
p = p->next;
}
free(words);
return (struct list *) l;
}
void benchmark(long id)
{
struct list *l = list;
unsigned iters = iterations;
int w = write;
while (iters--)
{
if (w)
l->pad[id] += 1;
l = l->next;
}
asm volatile (""::"r" (l));
}
void *thread(void *arg)
{
uint64_t t1, t2;
long id = (long) arg;
pthread_barrier_wait(&barrier);
t1 = getclock();
benchmark(id);
t2 = getclock();
times[id] =
((t2 - t1) * 1.0) / iterations;
return NULL;
}
float
memtest(size_t size, size_t line,
int shuffle, unsigned nthreads)
{
unsigned n = size / line;
unsigned i;
pthread_t threads[nthreads];
pthread_attr_t attr;
cpu_set_t c;
iterations = max(N, 2 * n);
/* Alokacja i wypełnienie lity */
list = meminit(size, line, shuffle);
pthread_barrier_init(&barrier,
NULL, nthreads);
pthread_attr_init(&attr);
for (i = 1; i < nthreads; i++)
{
CPU_ZERO(&c);
CPU_SET(i, &c);
pthread_attr_setaffinity_np
(&attr, sizeof(c), &c);
pthread_create(&threads
[i],
&attr,
thread,
(void *) (long) i);
}
CPU_ZERO(&c);
CPU_SET(0, &c);
pthread_setaffinity_np
(pthread_self(), sizeof(c), &c);
thread((long) 0);
//printf("%.2f\n", times[0]);
for (i = 1; i < nthreads; i++)
{
pthread_join(threads[i], NULL);
//printf("%.2f\n", times[i]);
}
return times[0];
}
int main(int argc, char **argv)
{
float time;
unsigned long size, line;
char unit = 'b';
int shuffle = 1;
unsigned nthreads = 1;
if (argc < 3)
return 1;
if (sscanf
(argv[1], "%lu%c", &size,
&unit) < 1)
return 1;
switch (unit)
{
case 'k':
size <<= 10;
break;
case 'm':
size <<= 20;
break;
}
if (sscanf(argv[2], "%lu", &line) < 1)
return 1;
if (size < line || line < 8)
return 1;
if (argc > 3
&& strcmp(argv[3], "-l") == 0)
{
shuffle = 0;
}
else if (argc > 3
&& sscanf(argv[3],
"%u",
&nthreads) == 1)
{
shuffle = 0;
}
if (argc > 4
&& strcmp(argv[4], "-w") == 0)
{
write = 1;
}
time =
memtest(size, line, shuffle,
nthreads);
printf("%lu %.2f\n", size, time);
return 0;
}