forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththr.c
executable file
·332 lines (301 loc) · 8.41 KB
/
thr.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
*
* Copyright (C) 2012 Heechul Yun <[email protected]>
* 2012 Zheng <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.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>
/**************************************************************************
* Public Definitions
**************************************************************************/
#define ADDR_2ND_RANK 0x40000000 /* the address of the 2nd Rank(1GB) of
memory of 1st Mem Controller */
#define CACHE_LINE_SIZE 64 /* cache Line size is 64 byte */
/**************************************************************************
* Public Types
**************************************************************************/
enum access_type { READ, WRITE, RDWR, WRST};
/**************************************************************************
* Global Variables
**************************************************************************/
int g_mem_size = 8192 * 1024; /* memory size */
int g_indx = 0; /* global index used for accessing the array */
int g_next = (CACHE_LINE_SIZE/4); /* incrementing the next element in the array
(Sequtial Default) */
int *g_mem_ptr = 0; /* pointer to allocated memory region */
FILE *g_fd = NULL;
char *g_label = NULL;
uint64_t g_nread = 0; /* number of bytes read */
unsigned int g_start; /* starting time */
/**************************************************************************
* Public Functions
**************************************************************************/
unsigned int get_usecs()
{
static struct timeval base;
struct timeval time;
gettimeofday(&time, NULL);
if (!base.tv_usec) {
base = time;
}
if (base.tv_usec > time.tv_usec)
return ((time.tv_sec - 1 - base.tv_sec) * 1000000 +
(1000000 + time.tv_usec - base.tv_usec));
else
return ((time.tv_sec - base.tv_sec) * 1000000 +
(time.tv_usec - base.tv_usec));
}
void quit(int param)
{
float dur_in_sec;
float bw;
float dur = get_usecs() - g_start;
dur_in_sec = (float)dur / 1000000;
printf("g_nread(bytes read) = %ld\n", g_nread);
printf("elapsed = %.2f sec ( %.0f usec )\n", dur_in_sec, dur);
bw = (float)g_nread / dur_in_sec / 1024 / 1024;
printf("B/W = %.2f MB/s | ", bw);
printf("average = %.2f ns\n", (dur*1000)/(g_nread/CACHE_LINE_SIZE));
if (g_fd) {
fprintf(g_fd, "%s %d\n", g_label, (int)bw);
fclose(g_fd);
}
exit(0);
}
int bench_read()
{
int i;
int sum = 0;
for ( i = g_indx; i < g_mem_size/4; i+=g_next ) {
sum += g_mem_ptr[i];
g_nread += CACHE_LINE_SIZE ;
}
return sum;
}
int bench_write()
{
int i;
for ( i = g_indx; i < g_mem_size/4; i+=g_next ) {
g_nread += CACHE_LINE_SIZE ;
g_mem_ptr[i] = i;
}
return 1;
}
int bench_rdwr()
{
int i;
int sum = 0;
for ( i = g_indx; i < g_mem_size/4; i+=g_next ) {
g_mem_ptr[i] = i;
sum += g_mem_ptr[i];
g_nread += CACHE_LINE_SIZE;
}
return sum;
}
void usage(int argc, char *argv[])
{
printf("Usage: $ %s [<option>]*\n\n", argv[0]);
printf("-m: memory size in KB. deafult=8192\n");
printf("-a: access type - read, write, rdwr. default=read\n");
printf("-n: addressing pattern - Seq, Row, Bank. default=Seq\n");
printf("-t: time to run in sec. 0 means indefinite. default=5. \n");
printf("-c: CPU to run.\n");
printf("-i: iterations. 0 means intefinite. default=0\n");
printf("-p: priority\n");
printf("-l: log label. use together with -f\n");
printf("-f: log file name\n");
printf("-x: memory map to /dev/mem. !!! DANGEROUS !!!\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);
}
int main(int argc, char *argv[])
{
int opt;
int cpuid = 0;
int prio = 0;
int num_processors;
cpu_set_t cmask;
struct sched_param param;
int acc_type = READ;
uint64_t sum = 0;
unsigned finish = 5;
int use_mmap = 0;
int iterations = 0;
int i;
/*
* get command line options
*/
while ((opt = getopt(argc, argv, "m:a:n:t:c:i:p:o:f:l:xh")) != -1) {
switch (opt) {
case 'm': /* set memory size */
g_mem_size = 1024 * strtol(optarg, NULL, 0);
break;
case 'a': /* set access type */
if (!strcmp(optarg, "read"))
acc_type = READ;
else if (!strcmp(optarg, "write"))
acc_type = WRITE;
else if (!strcmp(optarg, "rdwr"))
acc_type = RDWR;
else
exit(1);
break;
case 'n': /* set access pattern */
/* sequential */
if( strcmp(optarg,"Seq") == 0 ) {
g_indx = 0;
g_next = (CACHE_LINE_SIZE/4);
}
/* same bank */
else if( strcmp(optarg,"Row") == 0 ) {
g_indx = 0;
g_next = (CACHE_LINE_SIZE/4) * 1024;
}
/* diff bank */
else if( strcmp(optarg,"Bank") == 0 ) {
g_indx = cpuid*128;
g_next = (CACHE_LINE_SIZE/4) * 1024;
}
else
exit(1);
break;
case 't': /* set time in secs to run */
finish = strtol(optarg, NULL, 0);
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");
else
fprintf(stderr, "assigned to cpu %d\n", cpuid);
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 'p': /* set priority */
prio = strtol(optarg, NULL, 0);
if (setpriority(PRIO_PROCESS, 0, prio) < 0)
perror("error");
else
fprintf(stderr, "assigned priority %d\n", prio);
break;
case 'i': /* iterations */
iterations = strtol(optarg, NULL, 0);
break;
case 'l': /* set label */
g_label = strdup(optarg);
break;
case 'f': /* set file descriptor */
g_fd = fopen(optarg, "a+");
if (g_fd == NULL)
perror("error");
break;
case 'x': /* mapping to /dev/mem. !! DANGEROUS !! */
use_mmap = 1;
break;
case 'h':
usage(argc, argv);
break;
}
}
/*
* allocate contiguous region of memory
*/
if (use_mmap) {
/* open /dev/mem for accessing memory in physical addr. */
int fd = -1;
unsigned long offset;
printf("Use mmap\n");
fd = open("/dev/mem", O_RDWR | O_SYNC);
if(fd == -1) {
fprintf(stderr, "ERROR Opening /dev/mem\n");
exit(1);
}
/* offset variable is used to allocate each cpu to a different offset
from each other */
offset = ADDR_2ND_RANK + cpuid*g_mem_size;
/* use mmap to allocate each cpu to the specific address in memory */
g_mem_ptr = (int *)mmap(NULL, g_mem_size, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, offset);
if(g_mem_ptr == NULL) {
fprintf(stderr, "could not allocate memarea");
exit(1);
}
} else {
printf("Use standard malloc\n");
g_mem_ptr = (int *)malloc(g_mem_size);
}
memset(g_mem_ptr, 1, g_mem_size);
/* print experiment info before starting */
printf("memsize=%d KB, type=%s, cpuid=%d\n",
g_mem_size/1024,
((acc_type==READ) ?"read":
(acc_type==WRITE)? "write" :
(acc_type==RDWR) ? "rdwr" : "worst"),
cpuid);
printf("stop at %d\n", finish);
/* set signals to terminate once time has been reached */
signal(SIGINT, &quit);
if (finish > 0) {
signal(SIGALRM, &quit);
alarm(finish);
}
/*
* actual memory access
*/
g_start = get_usecs();
for (i=0;; i++) {
switch (acc_type) {
case READ:
sum = bench_read();
break;
case WRITE:
sum = bench_write();
break;
case RDWR:
sum = bench_rdwr();
break;
}
if (iterations > 0 && i >= iterations)
break;
}
quit(0);
}