forked from intel/pcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.cpp
282 lines (211 loc) · 8.19 KB
/
realtime.cpp
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
/*
Copyright (c) 2009-2012, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// written by Roman Dementiev
//
#include "cpucounters.h"
#include "cpuasynchcounter.h"
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <sys/time.h>
/*! \file realtime.cpp
\brief Two use-cases: realtime data structure performance analysis and memory-bandwidth aware scheduling
*/
using std::cout;
inline double my_timestamp()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return double(tp.tv_sec) + tp.tv_usec / 1000000.;
}
long long int fib(long long int num)
{
long long int result = 1, a = 1, b = 1;
for (long long int i = 3; i <= num; ++i)
{
result = a + b;
a = b;
b = result;
}
return result;
}
SystemCounterState before_sstate, after_sstate;
double before_time, after_time;
AsynchronCounterState counters;
long long int all_fib = 0;
void CPU_intensive_task()
{
cout << "CPU task\n";
all_fib += fib(80000000ULL + (rand() % 2));
}
template <class DS>
void Memory_intensive_task(DS & ds)
{
cout << "Mem task\n";
std::find(ds.begin(), ds.end(), ds.size());
}
double currentMemoryBandwidth()
{
return (counters.getSystem<uint64, ::getBytesReadFromMC>() + counters.getSystem<uint64, ::getBytesWrittenToMC>()) / (1024 * 1024);
}
template <class DS>
void measure(DS & ds, size_t repeat, size_t nelements)
{
SystemCounterState before_sstate, after_sstate;
double before_ts, after_ts;
// warm up
std::find(ds.begin(), ds.end(), nelements);
double before1_ts;
#if 0
for (int kkk = 1000; kkk > 0; --kkk)
{
::sleep(1);
before1_ts = my_timestamp();
// start measuring
before_sstate = getSystemCounterState();
before_ts = my_timestamp();
cout << "Response time of getSystemCounterState(): " << 1000. * (before_ts - before1_ts) << " ms\n";
}
#endif
for (int j = 0; j < repeat; ++j) std::find(ds.begin(), ds.end(), nelements);
// stop measuring
after_sstate = getSystemCounterState();
after_ts = my_timestamp();
cout << "\nSearch runtime: " << ((after_ts - before_ts) * 1000. / repeat) << " ms \n";
cout << "Search runtime per element: " << ((after_ts - before_ts) * 1000000000. / repeat) / nelements << " ns \n";
cout << "Number of L2 cache misses per 1000 elements: "
<< (1000. * getL2CacheMisses(before_sstate, after_sstate) / repeat) / nelements <<
" \nL2 Cache hit ratio : " << getL2CacheHitRatio(before_sstate, after_sstate) * 100. << " %\n";
cout << "Number of L3 cache misses per 1000 elements: "
<< (1000. * getL3CacheMisses(before_sstate, after_sstate) / repeat) / nelements <<
" \nL3 Cache hit ratio : " << getL3CacheHitRatio(before_sstate, after_sstate) * 100. << " %\n";
cout << "Bytes written to memory controller per element: " <<
(double(getBytesWrittenToMC(before_sstate, after_sstate)) / repeat) / nelements << "\n";
cout << "Bytes read from memory controller per element : " <<
(double(getBytesReadFromMC(before_sstate, after_sstate)) / repeat) / nelements << "\n";
cout << "Used memory bandwidth: " <<
((getBytesReadFromMC(before_sstate, after_sstate) + getBytesWrittenToMC(before_sstate, after_sstate)) / (after_ts - before_ts)) / (1024 * 1024) << " MByte/sec\n";
cout << "Instructions retired: " << getInstructionsRetired(before_sstate, after_sstate) / 1000000 << "mln\n";
cout << "CPU cycles: " << getCycles(before_sstate, after_sstate) / 1000000 << "mln\n";
cout << "Instructions per cycle: " << getCoreIPC(before_sstate, after_sstate) << "\n";
cout << flush;
}
#if 0
typedef int T;
#else
struct T
{
int key[1];
int data[15];
T() { }
T(int a) { key[0] = a; }
bool operator == (const T & k) const
{
return k.key[0] == key[0];
}
};
#endif
int main(int argc, char * argv[])
{
PCM * m = PCM::getInstance();
if (!m->good())
{
cout << "Can not access CPU counters\n";
cout << "Try to execute 'modprobe msr' as root user and then\n";
cout << "you also must have read and write permissions for /dev/cpu/?/msr devices (the 'chown' command can help).";
return -1;
}
if (m->program() != PCM::Success) {
cout << "Program was not successful...\n";
delete m;
return -1;
}
int nelements = atoi(argv[1]);
#if 1 /* use-case: compare data structures in real-time */
std::list<T> list;
std::vector<T> vector;
int i = 0;
for ( ; i < nelements; ++i)
{
list.push_back(i);
vector.push_back(i);
}
unsigned long long int totalops = 200000ULL * 1000ULL * 64ULL / sizeof(T);
int repeat = totalops / nelements, j;
cout << "\n\nElements to traverse: " << totalops << "\n";
cout << "Items in data structure: " << nelements << "\n";
cout << "Elements data size: " << sizeof(T) * nelements / 1024 << " KB\n";
cout << "Test repetions: " << repeat << "\n";
cout << "\n*List data structure*\n";
measure(list, repeat, nelements);
cout << "\n\n*Vector/array data structure*\n";
measure(vector, repeat, nelements);
#else
/* use-case: memory bandwidth-aware scheduling */
std::vector<T> vector;
nelements = 13000000;
int i = 0;
cout << "Elements data size: " << sizeof(T) * nelements / 1024 << " KB\n";
for ( ; i < nelements; ++i)
{
vector.push_back(i);
}
double before_ts, after_ts;
before_ts = my_timestamp();
{
int m_tasks = 1000;
int c_tasks = 1000;
while (m_tasks + c_tasks != 0)
{
if (m_tasks > 0)
{
Memory_intensive_task(vector);
--m_tasks;
continue;
}
if (c_tasks > 0)
{
CPU_intensive_task();
--c_tasks;
}
}
}
after_ts = my_timestamp();
cout << "In order scheduling, Running time: " << (after_ts - before_ts) << " seconds\n";
before_ts = my_timestamp();
{
int m_tasks = 1000;
int c_tasks = 1000;
while (m_tasks + c_tasks != 0)
{
double band = currentMemoryBandwidth();
//cout << "Mem band: " << band << " MB/sec\n";
if (m_tasks > 0 && (band < (25 * 1024 /* MB/sec*/)
|| c_tasks == 0))
{
Memory_intensive_task(vector);
--m_tasks;
continue;
}
if (c_tasks > 0)
{
CPU_intensive_task();
--c_tasks;
continue;
}
}
}
after_ts = my_timestamp();
cout << "CPU monitoring conscoius scheduling, Running time: " << (after_ts - before_ts) << " seconds\n";
#endif
m->cleanup();
return 0;
}