forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.c
207 lines (182 loc) · 5.22 KB
/
fps.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
/**
* 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 <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <inttypes.h>
#include <signal.h>
#include <sys/resource.h>
#include <string.h>
/**************************************************************************
* Public Definitions
**************************************************************************/
#define CACHE_LINE_SIZE 64
#define CACHE_LINE_BITS 6
#define FRAME_LENGTH (1920*1080)
#define min(x,y) ((x > y) ? y: x)
#define max(x,y) ((x > y) ? x: y)
/**************************************************************************
* Public Types
**************************************************************************/
typedef struct {
int64_t max;
int64_t min;
int64_t cur;
int64_t ewma;
int64_t tot;
int64_t cnt;
int miss;
} stat_t;
/**************************************************************************
* Global Variables
**************************************************************************/
int *frames[2];
int g_frame_length = FRAME_LENGTH;
stat_t t;
/**************************************************************************
* Public Function Prototypes
**************************************************************************/
void init_stat(stat_t *ts)
{
ts->min = 0x0fffffff;
ts->max = ts->tot = ts->cnt = ts->miss = ts->cur = ts->ewma = 0;
}
void print_fps()
{
int64_t avgtime = t.tot / t.cnt;
float fps = (float)1000000000/avgtime;
fprintf(stdout, "fps: %.1f %d MB/s avg/min/max: %ld/%ld/%ld(us) miss:%d%%(%d/%ld)\n", fps, (int)(fps*g_frame_length*4/1024/1024),
avgtime/1000, t.min/1000, t.max/1000, t.miss*100/(int)t.cnt, t.miss, t.cnt);
fflush(stdout);
}
void quit()
{
print_fps();
exit(0);
}
int compute(int *frame)
{
int sum = 0;
int i;
/* read frame */
for (i = 0; i < g_frame_length; i+=(CACHE_LINE_SIZE/4))
sum += frame[i];
return sum;
}
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;
}
int main(int argc, char* argv[])
{
int i, j;
struct timespec start, end;
uint64_t readsum = 0;
int iterations = 0;
int cpuid = 0;
struct sched_param param;
cpu_set_t cmask;
int num_processors;
int opt;
int sum[2];
char *ptr;
int prio;
int deadline = 10;
while ((opt = getopt(argc, argv, "m:d:n:t:c:i:p:f:l:xh")) != -1) {
switch(opt) {
case 'm': /* set memory size */
g_frame_length = 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 'd':
deadline = strtol(optarg, NULL, 0);
fprintf(stderr, "new deadline: %d ms\n", deadline);
break;
case 'i': /* iterations */
iterations = strtol(optarg, NULL, 0);
break;
case 'p': /* set priority (nice value: -20 ~ 19) */
prio = strtol(optarg, NULL, 0);
if (setpriority(PRIO_PROCESS, 0, prio) < 0)
perror("error");
else
fprintf(stderr, "assigned priority %d\n", prio);
break;
}
}
srand(0);
#if 0
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
}
#endif
/* set signals to terminate once time has been reached */
signal(SIGINT, &quit);
/* allocate frames */
ptr = (char *)malloc(g_frame_length * 4 * 2);
frames[0] = (int *)ptr;
frames[1] = (int *)(ptr + (g_frame_length * 4));
/* the most important thing.
w/o initialization, read doesn't access actual memory */
memset(ptr, 1, g_frame_length*4*2);
/* actual access */
sum[0] = compute(frames[0]);
sum[1] = compute(frames[1]);
init_stat(&t);
fprintf(stdout, "deadline: %d, req. b/w(MB/s): %.1f\n", deadline,
(float)g_frame_length * 4 * 1000 / deadline / 1024 / 1024);
while (1) {
if (iterations > 0 && t.cnt >= iterations)
break;
clock_gettime(CLOCK_REALTIME, &start);
if (sum[j%2] != compute(frames[j%2])) {
fprintf(stderr, "mismatch !!!\n");
}
clock_gettime(CLOCK_REALTIME, &end);
t.cur = get_elapsed(&start, &end);
if (t.cur > deadline * 1000000)
t.miss++;
t.min = min(t.cur, t.min);
t.max = max(t.cur, t.max);
t.tot += t.cur;
t.cnt ++;
if (t.tot > 1000000000) {
print_fps();
init_stat(&t);
}
}
quit(0);
}