forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hrt.c
308 lines (265 loc) · 7.62 KB
/
hrt.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* DRAM access latency measurement program
*
* Copyright (C) 2012 Heechul Yun <heechul@illinois.edu>
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
/**************************************************************************
* Conditional Compilation Options
**************************************************************************/
/**************************************************************************
* Included Files
**************************************************************************/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "list.h"
/**************************************************************************
* Public Definitions
**************************************************************************/
#define CACHE_LINE_SIZE 64
#define CACHE_LINE_BITS 6
#define NUM_FRAMES 5
/**************************************************************************
* Public Types
**************************************************************************/
struct item {
int data;
int in_use;
struct list_head list;
} __attribute__((aligned(CACHE_LINE_SIZE)));;
/**************************************************************************
* Global Variables
**************************************************************************/
int g_mem_size = 8192*1024;
volatile int quit_signal;
static int mark_fd = -1;
static __thread char buff[BUFSIZ+1];
/**************************************************************************
* Public Function Prototypes
**************************************************************************/
static void setup_ftrace_marker(void)
{
struct stat st;
char *files[] = {
"/sys/kernel/debug/tracing/trace_marker",
"/debug/tracing/trace_marker",
"/debugfs/tracing/trace_marker",
};
int ret;
int i;
for (i = 0; i < (sizeof(files) / sizeof(char *)); i++) {
ret = stat(files[i], &st);
if (ret >= 0)
goto found;
}
/* todo, check mounts system */
return;
found:
mark_fd = open(files[i], O_WRONLY);
}
static void ftrace_write(const char *fmt, ...)
{
va_list ap;
int n;
int ret;
if (mark_fd < 0)
return;
va_start(ap, fmt);
n = vsnprintf(buff, BUFSIZ, fmt, ap);
va_end(ap);
ret = write(mark_fd, buff, n);
}
uint64_t get_elapsed(struct timespec *start, struct timespec *end)
{
uint64_t dur;
if (start->tv_nsec > end->tv_nsec)
dur = (uint64_t)(end->tv_sec - 1 - start->tv_sec) * 1000000000 +
(1000000000 + end->tv_nsec - start->tv_nsec);
else
dur = (uint64_t)(end->tv_sec - start->tv_sec) * 1000000000 +
(end->tv_nsec - start->tv_nsec);
return dur;
}
void usage(int argc, char *argv[])
{
printf("Usage: $ %s [<option>]*\n\n", argv[0]);
printf("-C: \n");
printf("-s: turn serial access mode on\n");
printf("-c: CPU to run.\n");
printf("-i: iterations. 0 means intefinite. default=0\n");
printf("-p: priority\n");
printf("-h: help\n");
printf("\nExamples: \n$ bandwidth -m 8192 -a read -t 1 -c 2\n <- 8MB read for 1 second on CPU 2\n");
exit(1);
}
void quit(int param)
{
quit_signal = 1;
}
int main(int argc, char* argv[])
{
struct item *list[NUM_FRAMES];
int workingset_size = 1024;
int compute_load;
float interval_ms = 0.0;
int i, j;
struct list_head head[NUM_FRAMES];
struct list_head *pos;
struct timespec start, end;
uint64_t nsdiff;
int64_t avglat;
uint64_t readsum = 0, cnt;
int serial = 0;
int repeat = 1;
int cpuid = 0;
struct sched_param param;
cpu_set_t cmask;
int num_processors;
int opt, prio;
signal(SIGINT, &quit);
/*
* get command line options
*/
while ((opt = getopt(argc, argv, "sc:i:C:I:p:o:h")) != -1) {
switch (opt) {
case 's': /* set access type */
serial = 1;
break;
case 'c': /* set CPU affinity */
cpuid = strtol(optarg, NULL, 0);
num_processors = sysconf(_SC_NPROCESSORS_CONF);
CPU_ZERO(&cmask);
CPU_SET(cpuid % num_processors, &cmask);
if (sched_setaffinity(0, num_processors, &cmask) < 0) {
perror("error");
exit(1);
} else
fprintf(stderr, "assigned to cpu %d\n", cpuid);
break;
case 'p': /* set priority */
prio = strtol(optarg, NULL, 0);
if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
perror("error");
exit(2);
}else
fprintf(stderr, "assigned priority %d\n", prio);
break;
case 'o': /* SCHED_BATCH */
if (!strcmp(optarg, "batch")) {
param.sched_priority = 0;
if(sched_setscheduler(0, SCHED_BATCH, ¶m) == -1) {
perror("sched_setscheduler failed");
exit(1);
}
} else if (!strcmp(optarg, "fifo")) {
param.sched_priority = 1;
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
exit(1);
}
}
break;
case 'i': /* iterations */
repeat = strtol(optarg, NULL, 0);
fprintf(stderr, "repeat=%d\n", repeat);
break;
case 'I': /* interval (period) */
interval_ms = strtof(optarg, NULL);
fprintf(stderr, "I(interval)=%f(ms)\n", interval_ms);
break;
case 'h':
usage(argc, argv);
break;
}
}
setup_ftrace_marker();
workingset_size = g_mem_size / CACHE_LINE_SIZE;
srand(0);
/* allocate */
for (i = 0; i < NUM_FRAMES; i++) {
INIT_LIST_HEAD(&head[i]);
list[i] = (struct item *)malloc(sizeof(struct item)
* workingset_size + CACHE_LINE_SIZE);
for (j = 0; j < workingset_size; j++) {
list[i][j].data = j;
list[i][j].in_use = 0;
INIT_LIST_HEAD(&list[i][j].list);
}
// printf("%d 0x%x\n", list[i].data, &list[i].data);
}
fprintf(stderr, "allocated: wokingsetsize=%d entries\n", workingset_size);
ftrace_write("PGM: begin permutation\n");
/* initialize. TODO: random permutation algorithm */
int *perm = (int *)malloc(workingset_size * sizeof(int));
for (i = 0; i < workingset_size; i++)
perm[i] = i;
if (!serial) {
for (i = 0; i < workingset_size; i++) {
int tmp = perm[i];
int next = rand() % workingset_size;
perm[i] = perm[next];
perm[next] = tmp;
}
}
for (i = 0; i < NUM_FRAMES; i++) {
for (j = 0; j < workingset_size; j++) {
list_add(&list[i][perm[j]].list, &head[i]);
}
}
ftrace_write("PGM: end permutation\n");
/* add marker */
/* actual access */
nsdiff = 0; j = 0; i = 0;
quit_signal = 0;
cnt = 0;
ftrace_write("PGM: begin main loop\n");
clock_gettime(CLOCK_REALTIME, &start);
while (1) {
uint64_t tmpdiff;
list_for_each(pos, &head[i % NUM_FRAMES]) {
struct item *tmp = list_entry(pos, struct item, list);
readsum += tmp->data;
cnt++;
}
clock_gettime(CLOCK_REALTIME, &end);
tmpdiff = get_elapsed(&start, &end);
ftrace_write("PGM: iter %d took %lld ns\n", i, tmpdiff);
printf("%4d %.2f\n", i, (double) tmpdiff/1000000);
fprintf(stderr, "%4d %.2f --------------------------------------------------------------------------------------------------------\n",
i, (double) tmpdiff/1000000);
nsdiff += tmpdiff;
if (++i == repeat || quit_signal)
goto out;
double remain_us = (interval_ms * 1000 - tmpdiff / 1000);
if (remain_us > 0) {
usleep((useconds_t)remain_us);
}
clock_gettime(CLOCK_REALTIME, &start);
}
out:
avglat = (int64_t)(nsdiff/cnt);
fprintf(stderr, "duration %lldus\naverage %lldns | ",
(long long)nsdiff/1000, (long long)avglat);
fprintf(stderr, "bandwidth %lld MB (%lld MiB)/s\n",
(long long)64*1000/avglat,
(long long)64*1000000000/avglat/1024/1024);
fprintf(stderr, "readsum %lld\n", (unsigned long long)readsum);
return 0;
}