This repository was archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnvramsim.cpp
399 lines (346 loc) · 11.8 KB
/
nvramsim.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Collect an address trace and estimate the execution time of an application.
* When the buffer gets full, process the trace and empty it.
* Repeat the process until the application finishes.
*
*/
/*
* A memory trace (Ip of memory accessing instruction and address of memory access - see
* struct MEMREF) is collected by inserting Pin buffering API code into the application code,
* via calls to INS_InsertFillBuffer. This analysis code writes a MEMREF into the
* buffer being filled, and calls the registered BufferFull function (see call to
* PIN_DefineTraceBuffer which defines the buffer and registers the BufferFull function)
* when the buffer becomes full.
* The BufferFull function processes the buffer and returns it to Pin to be filled again.
*
* Each application thread has it's own buffer - so multiple application threads do NOT
* block each other on buffer accesses
*
* This tool is similar to memtrace_simple, but uses the Pin Buffering API
*/
//#include <assert.h>
#include <string.h>
#include <sstream>
//#include <iostream>
//#include <fstream>
//#include <map>
//#include <set>
#include "cache-sim/cache.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "pin.H"
#include "portability.H"
using namespace std;
// Latencies in number of cycles
const size_t L1Latency = 2;
const size_t L2Latency = 16;
const size_t DDRLatency = 80; // 40 ns at 2GHz
const size_t PCMLatency = 4000; // 2 us at 2GHz
// 1024x8 = 8 MB
// 2048x8 = 16 MB
// 4096x8 = 32 MB
// 8192x8 = 64 MB
// 16348x8 = 128 MB
// 32768x8 = 256 MB
// 1024x16 = 16 MB
// 2048x16 = 32 MB
// 4096x16 = 64 MB
// 8192x16 = 128 MB
// 16348x16 = 256 MB
const size_t DDR_size_MB = 128; // please change this parameter!
const size_t DDR_line_bytes = 1024;
const size_t DDR_associativity = 8; // number of ways; probably should be fixed
const size_t DDR_sets = (DDR_size_MB*1024*1024)/DDR_line_bytes * (128/DDR_associativity);
// 1024x8 = 512 KB
// 2048x8 = 1 MB
// 4096x8 = 2 MB
// 8192x8 = 4 MB
// 16348x8 = 8 MB <===
// 32768x8 = 16 MB
// 1024x16 = 1 MB
// 2048x16 = 2 MB
// 4096x16 = 4 MB
// 8192x16 = 8 MB
// 8192x16 = 8 MB
const size_t L2_sets = 16*1024;
const size_t L2_ways = 8; // the associativity in each set
const size_t L2_line_bytes = 64;
// 256x2 = 32 KB
// 128x4 = 32 KB
// 512x2 = 64 KB
// 256x4 = 64 KB
// 1024x2 = 128 KB
// 512x4 = 128 KB <===
// 2048x2 = 256 KB
// 1024x4 = 256 KB
// 2048x4 = 512 KB
// 4096x2 = 512 KB
// 8192x2 = 1 MB
// 4096x4 = 1 MB
const size_t L1_sets = 512;
const size_t L1_ways = 4; // the associativity in each set
const size_t L1_line_bytes = 64;
MainMemory PCM(addr_space, PCMLatency);
Cache DDR( "DDR", // string with cache instance name
&PCM, // parent memory
DDR_sets,
DDR_associativity,
L2_line_bytes,
DDRLatency,
IS_WRITEBACK_CACHE
);
Cache L2( "L2", // string with cache instance name
&DDR, // parent memory
L2_sets,
L2_ways,
L2_line_bytes,
L2Latency,
IS_WRITEBACK_CACHE
);
Cache L1( "L1", // string with cache instance name
&L2, // parent layer in the memory hierarchy
L1_sets,
L1_ways,
L1_line_bytes,
L1Latency,
IS_WRITEBACK_CACHE
);
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "memtrace.out", "output file");
KNOB<UINT32> KnobNumPagesInBuffer(KNOB_MODE_WRITEONCE, "pintool", "num_pages_in_buffer", "256", "number of pages in buffer");
uint64_t num_instr = 0;
uint64_t num_memrefs = 0;
uint64_t cycles_memref = 0;
char base_directory[1024];
std::stringstream cmdline;
VOID stats_print()
{
double exec_time = double(0.42*num_instr + cycles_memref) / (2*1024*1024*1024LLU);
char fname_stats[sizeof(base_directory)+255];
char *pos = strcpy(fname_stats, base_directory) + strlen(base_directory);
*pos = '/';
pos++;
snprintf(pos, sizeof(fname_stats) - (pos - fname_stats), "nvramsim_stats_%d.txt", PIN_GetPid());
fprintf(stderr, "NVRAMSIM: process %d is saving statistics to file '%s'\n", PIN_GetPid(), fname_stats);
FILE *fstats = fopen(fname_stats, "wb");
fprintf(fstats, "Command line,Instructions,Total memory references," \
"Avg cycles/mem ref,PCM read KB,PCM 64B reads,PCM 128B reads," \
"PCM write KB,PCM 64B writes,PCM 128B writes," \
"Estimated exec. time at 2GHz\n");
fprintf(fstats, "\"%s\",%lu,%lu,%6.2lf,%lu,%lu,%lu,%lu,%lu,%lu,%4.2lf\n",
cmdline.str().c_str(),
num_instr, num_memrefs, double(cycles_memref)/num_memrefs, PCM.stats.hits_rd /* each read is for 1KB */,
PCM.stats.hits_rd*DDR_line_bytes/64, /* when PCM is in 64B blocks */
PCM.stats.hits_rd*DDR_line_bytes/128, /* when PCM is in 128B blocks */
PCM.stats.hits_wr, /* each write is for 1KB */
PCM.stats.hits_wr*DDR_line_bytes/64, /* when PCM is in 64B blocks */
PCM.stats.hits_wr*DDR_line_bytes/128, /* when PCM is in 128B blocks */
exec_time);
fprintf(fstats, "\n==== Verbose description ====\n");
fprintf(fstats, "Executed command: %s\n", cmdline.str().c_str());
fprintf(fstats, "Process ID: %d\n", PIN_GetPid());
fprintf(fstats, "Instructions: %lu (0.42 Cycles per Instruction; compile-time fixed)\n", num_instr);
fprintf(fstats, "Total memory references: %lu (%6.2lf Cycles per Memory Reference; workload-dependent)\n", num_memrefs, double(cycles_memref)/num_memrefs);
fprintf(fstats, "PCM reads: %lu KB. 64B reqs %lu 128B reqs: %lu\n",
PCM.stats.hits_rd, PCM.stats.hits_rd*DDR_line_bytes/64,
PCM.stats.hits_rd*DDR_line_bytes/128);
fprintf(fstats, "PCM writes: %lu KB. 64B reqs %lu 128B reqs: %lu\n",
PCM.stats.hits_wr, PCM.stats.hits_wr*DDR_line_bytes/64,
PCM.stats.hits_wr*DDR_line_bytes/128);
fprintf(fstats, "Estimated execution time on an in-order processor at 2GHz: %4.2lf seconds\n", exec_time);
fclose(fstats);
PCM.dump_stats();
}
/*
* Struct of memory reference written to the buffer
*/
struct MEMREF
{
ADDRINT ea;
BOOL read;
};
// The buffer ID returned by the one call to PIN_DefineTraceBuffer
BUFFER_ID bufId;
// the Pin TLS slot that an application-thread will use to hold the APP_THREAD_REPRESENTITVE
// object that it owns
TLS_KEY appThreadRepresentitiveKey;
UINT32 totalBuffersFilled = 0;
UINT64 totalElementsProcessed = 0;
/*
*
* APP_THREAD_REPRESENTITVE
*
* Each application thread, creates an object of this class and saves it in it's Pin TLS
* slot (appThreadRepresentitiveKey).
*/
class APP_THREAD_REPRESENTITVE
{
public:
APP_THREAD_REPRESENTITVE(THREADID tid) {
_numBuffersFilled = 0;
_numElementsProcessed = 0;
}
~APP_THREAD_REPRESENTITVE() {}
VOID ProcessBuffer(VOID *buf, UINT64 numElements);
UINT32 NumBuffersFilled() {return _numBuffersFilled;}
UINT32 NumElementsProcessed() {return _numElementsProcessed;}
private:
UINT32 _numBuffersFilled;
UINT32 _numElementsProcessed;
};
VOID APP_THREAD_REPRESENTITVE::ProcessBuffer(VOID *buf, UINT64 numElements)
{
_numBuffersFilled++;
struct MEMREF * memref=(struct MEMREF*)buf;
uint8_t *data;
for(UINT64 i=0; i<numElements; i++, memref++)
{
// if (memref->read)
// cerr << "Recorded read @" << (void*)memref->ea << "\n";
// else
// cerr << "Recorded write @" << (void*)memref->ea << "\n";
L1.line_get(memref->ea, (memref->read)?LINE_SHR:LINE_MOD, cycles_memref, data);
num_memrefs++;
}
_numElementsProcessed += (UINT32)numElements;
}
VOID CountInstr(UINT32 numInstInBbl)
{
num_instr += numInstInBbl;
}
/*
* Insert code to write data to a thread-specific buffer for instructions
* that access memory.
*/
VOID Trace(TRACE trace, VOID *v)
{
// Insert a call to record the effective address.
for(BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl=BBL_Next(bbl))
{
const uint64_t num_instr_bbl = BBL_NumIns(bbl);
for(INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins=INS_Next(ins))
{
// Log every memory references of the instruction
if (INS_IsMemoryRead(ins))
{
INS_InsertFillBuffer(ins, IPOINT_BEFORE, bufId,
IARG_MEMORYREAD_EA,
offsetof(struct MEMREF, ea),
IARG_BOOL, true,
offsetof(struct MEMREF, read),
IARG_END);
}
if (INS_IsMemoryWrite(ins))
{
INS_InsertFillBuffer(ins, IPOINT_BEFORE, bufId,
IARG_MEMORYWRITE_EA,
offsetof(struct MEMREF, ea),
IARG_BOOL, false,
offsetof(struct MEMREF, read),
IARG_END);
}
if (INS_HasMemoryRead2(ins))
{
INS_InsertFillBuffer(ins, IPOINT_BEFORE, bufId,
IARG_MEMORYREAD2_EA,
offsetof(struct MEMREF, ea),
IARG_BOOL, true,
offsetof(struct MEMREF, read),
IARG_END);
}
}
BBL_InsertCall(bbl, IPOINT_ANYWHERE, (AFUNPTR)CountInstr, IARG_UINT32, num_instr_bbl, IARG_END);
}
}
/**************************************************************************
*
* Callback Routines
*
**************************************************************************/
/*!
* Called when a buffer fills up, or the thread exits, so we can process it or pass it off
* as we see fit.
* @param[in] id buffer handle
* @param[in] tid id of owning thread
* @param[in] ctxt application context
* @param[in] buf actual pointer to buffer
* @param[in] numElements number of records
* @param[in] v callback value
* @return A pointer to the buffer to resume filling.
*/
VOID * BufferFull(BUFFER_ID id, THREADID tid, const CONTEXT *ctxt, VOID *buf,
UINT64 numElements, VOID *v)
{
APP_THREAD_REPRESENTITVE * appThreadRepresentitive = static_cast<APP_THREAD_REPRESENTITVE*>( PIN_GetThreadData( appThreadRepresentitiveKey, tid ) );
appThreadRepresentitive->ProcessBuffer(buf, numElements);
return buf;
}
VOID ThreadStart(THREADID tid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
// There is a new APP_THREAD_REPRESENTITVE for every thread.
APP_THREAD_REPRESENTITVE * appThreadRepresentitive = new APP_THREAD_REPRESENTITVE(tid);
// A thread will need to look up its APP_THREAD_REPRESENTITVE, so save pointer in TLS
PIN_SetThreadData(appThreadRepresentitiveKey, appThreadRepresentitive, tid);
}
VOID ThreadFini(THREADID tid, const CONTEXT *ctxt, INT32 code, VOID *v)
{
APP_THREAD_REPRESENTITVE * appThreadRepresentitive = static_cast<APP_THREAD_REPRESENTITVE*>(PIN_GetThreadData(appThreadRepresentitiveKey, tid));
totalBuffersFilled += appThreadRepresentitive->NumBuffersFilled();
totalElementsProcessed += appThreadRepresentitive->NumElementsProcessed();
delete appThreadRepresentitive;
PIN_SetThreadData(appThreadRepresentitiveKey, 0, tid);
}
VOID Fini(INT32 code, VOID *v)
{
stats_print();
printf ("totalBuffersFilled %u totalElementsProcessed %14.0f\n", (totalBuffersFilled),
static_cast<double>(totalElementsProcessed));
}
INT32 Usage()
{
puts("\nThis tool estimates the execution time, using a simple memory model\n");
puts(KNOB_BASE::StringKnobSummary().c_str());
return -1;
}
int main(int argc, char * argv[])
{
// Initialize PIN library. Print help message if -h(elp) is specified
// in the command line or the command line is invalid
if( PIN_Init(argc,argv) )
{
return Usage();
}
PIN_InitSymbols();
if (!getcwd(base_directory, sizeof(base_directory)))
perror("getcwd() error");
{
// get everything in the command line after '--'
bool is_cmd = false;
for (int i=0; i<argc; i++) {
if (strcmp(argv[i], "--") == 0)
is_cmd = true;
else {
if (is_cmd)
cmdline << argv[i] << " ";
}
}
}
bufId = PIN_DefineTraceBuffer(sizeof(struct MEMREF), KnobNumPagesInBuffer,
BufferFull, 0);
if(bufId == BUFFER_ID_INVALID)
{
printf ("Error: could not allocate initial buffer\n");
return 1;
}
// Initialize thread-specific data not handled by buffering api.
appThreadRepresentitiveKey = PIN_CreateThreadDataKey(0);
// add an instrumentation function
TRACE_AddInstrumentFunction(Trace, 0);
// add callbacks
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}