forked from rezass/HMMsim-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceHandler.cpp
More file actions
executable file
·457 lines (422 loc) · 14.2 KB
/
TraceHandler.cpp
File metadata and controls
executable file
·457 lines (422 loc) · 14.2 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Copyright (c) 2015 Santiago Bock
*
* See the file LICENSE.txt for copying permission.
*/
#include "Error.H"
#include "TraceHandler.H"
#include <iomanip>
#include <cassert>
TraceReaderBase::TraceReaderBase(){
numInstr = 0;
numReads = 0;
numWrites = 0;
}
TraceReader::TraceReader(const string& filename) : TraceReaderBase(){
trace = fopen(filename.c_str(), "r");
if (trace == 0){
error("Could not open file '%s'", filename.c_str());
}
currentEntry = BUFFER_SIZE;
lastEntry = -1;
}
TraceReader::~TraceReader(){
fclose(trace);
}
bool TraceReader::readEntry(TraceEntry *entry){
if (currentEntry == BUFFER_SIZE){
size_t r = fread(&entries, sizeof(struct TraceEntry), BUFFER_SIZE, trace);
if (r == BUFFER_SIZE){
currentEntry = 0;
} else {
if (feof(trace)){
currentEntry = 0;
lastEntry = r;
}
if (ferror(trace)){
error("Error reading trace file");
}
}
}
if (lastEntry == currentEntry){
return false;
}
*entry = entries[currentEntry];
currentEntry++;
if (entry->instr){
numInstr++;
} else {
if (entry->read){
numReads++;
} else {
numWrites++;
}
}
return true;
}
CompressedTraceReader::TraceMerger::TraceMerger(const string& prefix, CompressionType compressionArg){
compression = compressionArg;
if (compression == GZIP){
string filename = prefix+"-time.gz";
if((gzTimestampFile = gzopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
filename = prefix+"-addr.gz";
if((gzAddressFile = gzopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
filename = prefix+"-size.gz";
if((gzSizeFile = gzopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
} else if (compression == BZIP2){
string filename = prefix+"-time.bz2";
int bzerror;
if((timestampFile = fopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((timestampTrace = BZ2_bzReadOpen(&bzerror, timestampFile, 0, 0, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
filename = prefix+"-addr.bz2";
if((addressFile = fopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((addressTrace = BZ2_bzReadOpen(&bzerror, addressFile, 0, 0, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
filename = prefix+"-size.bz2";
if((sizeFile = fopen(filename.c_str(), "r")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((sizeTrace = BZ2_bzReadOpen(&bzerror, sizeFile, 0, 0, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
}
timestampEntries = new uint64[BUFFER_SIZE];
addressEntries = new addrint[BUFFER_SIZE];
sizeEntries = new uint8[BUFFER_SIZE];
currentEntry = BUFFER_SIZE;
lastEntry = -1;
currentTimestamp = 0;
}
CompressedTraceReader::TraceMerger::~TraceMerger(){
if (compression == GZIP){
gzclose(gzTimestampFile);
gzclose(gzAddressFile);
gzclose(gzSizeFile);
} else if (compression == BZIP2){
int timestampError, addressError, sizeError;
BZ2_bzReadClose(×tampError, timestampTrace);
BZ2_bzReadClose(&addressError, addressTrace);
BZ2_bzReadClose(&sizeError, sizeTrace);
if (timestampError != BZ_OK || addressError != BZ_OK || sizeError != BZ_OK){
error("Error closing compressed file");
}
fclose(timestampFile);
fclose(addressFile);
fclose(sizeFile);
}
delete [] timestampEntries;
delete [] addressEntries;
delete [] sizeEntries;
}
bool CompressedTraceReader::TraceMerger::readEntry(uint64 *timestamp, addrint *addr, uint8 *size){
static long testFileRead;
if (currentEntry == BUFFER_SIZE){
if (compression == GZIP){
testFileRead++;
//cout<<"RD: "<<testFileRead<<endl;
int timestampRead = gzread(gzTimestampFile, timestampEntries, BUFFER_SIZE*sizeof(uint64))/sizeof(uint64);
int addressRead = gzread(gzAddressFile, addressEntries, BUFFER_SIZE*sizeof(addrint))/sizeof(addrint);
int sizeRead = gzread(gzSizeFile, sizeEntries, BUFFER_SIZE*sizeof(uint8))/sizeof(uint8);
if (timestampRead != addressRead || timestampRead != sizeRead){
error("Bytes read from all three files are not the same (timestamp: %d, address: %d and size: %d)", timestampRead, addressRead, sizeRead);
}
if (timestampRead == -1){
error("Error reading trace file");
} else if (timestampRead == BUFFER_SIZE){
currentEntry = 0;
} else {
currentEntry = 0;
lastEntry = timestampRead;
}
} else if (compression == BZIP2){
int timestampError, addressError, sizeError;
int timestampRead = BZ2_bzRead(×tampError, timestampTrace, timestampEntries, BUFFER_SIZE*sizeof(uint64))/sizeof(uint64);
int addressRead = BZ2_bzRead(&addressError, addressTrace, addressEntries, BUFFER_SIZE*sizeof(addrint))/sizeof(addrint);
int sizeRead = BZ2_bzRead(&sizeError, sizeTrace, sizeEntries, BUFFER_SIZE*sizeof(uint8))/sizeof(uint8);
if (timestampError != addressError || timestampError != sizeError){
error("Errors from all three files are not the same (timestamp: %d, address: %d and size: %d)", timestampError, addressError, sizeError);
}
if (timestampRead != addressRead || timestampRead != sizeRead){
error("Bytes read from all three files are not the same (timestamp: %d, address: %d and size: %d)", timestampRead, addressRead, sizeRead);
}
if (timestampError == BZ_OK){
assert(timestampRead == BUFFER_SIZE);
currentEntry = 0;
} else if (timestampError == BZ_STREAM_END){
currentEntry = 0;
lastEntry = timestampRead;
} else {
error("Error reading trace file");
}
}
}
if (lastEntry == currentEntry){
return false;
}
*timestamp = timestampEntries[currentEntry];
*addr = addressEntries[currentEntry];
*size = sizeEntries[currentEntry];
currentEntry++;
return true;
}
CompressedTraceReader::CompressedTraceReader(const string& prefix, CompressionType compressionArg) : TraceReaderBase(), instrMerger(prefix+"-instr", compressionArg), readMerger(prefix+"-read", compressionArg), writeMerger(prefix+"-write", compressionArg){
instrValid = instrMerger.readEntry(&instrTimestamp, &instrAddress, &instrSize);
readValid = readMerger.readEntry(&readTimestamp, &readAddress, &readSize);
writeValid = writeMerger.readEntry(&writeTimestamp, &writeAddress, &writeSize);
}
bool CompressedTraceReader::readEntry(TraceEntry *entry){
//static int i;
//i++;
// cout<<i<<" "<<entry->address<<"Time: "<<entry->timestamp<<endl;
int type; //0:instr, 1: read, 2: write, 3: invalid
if (instrValid){
if (readValid){
if (writeValid){
if (instrTimestamp <= readTimestamp){
if (instrTimestamp <= writeTimestamp){
type = 0;
} else {
type = 2;
}
} else {
if (readTimestamp <= writeTimestamp){
type = 1;
} else {
type = 2;
}
}
} else {
if (instrTimestamp <= readTimestamp){
type = 0;
} else {
type = 1;
}
}
} else {
if (writeValid){
if (instrTimestamp <= writeTimestamp){
type = 0;
} else {
type = 2;
}
} else {
type = 0;
}
}
} else {
if (readValid){
if (writeValid){
if (readTimestamp <= writeTimestamp){
type = 1;
} else {
type = 2;
}
} else {
type = 1;
}
} else {
if (writeValid){
type = 2;
} else {
type = 3;
}
}
}
uint64 ts;
if (type == 0){
entry->timestamp = instrTimestamp;
entry->address = instrAddress;
entry->size = instrSize;
entry->read = true;
entry->instr = true;
instrValid = instrMerger.readEntry(&ts, &instrAddress, &instrSize);
instrTimestamp += ts;
numInstr++;
return true;
} else if (type == 1){
entry->timestamp = readTimestamp;
entry->address = readAddress;
entry->size = readSize;
entry->read = true;
entry->instr = false;
readValid = readMerger.readEntry(&ts, &readAddress, &readSize);
readTimestamp += ts;
numReads++;
return true;
} else if (type == 2){
entry->timestamp = writeTimestamp;
entry->address = writeAddress;
entry->size = writeSize;
entry->read = false;
entry->instr = false;
writeValid = writeMerger.readEntry(&ts, &writeAddress, &writeSize);
writeTimestamp += ts;
numWrites++;
return true;
} else if (type == 3){
return false;
} else {
error("Error merging entries");
return false;
}
}
TraceWriter::TraceWriter(const string& filename){
if((trace = fopen(filename.c_str(), "w")) == 0){
error("Could not open file '%s'", filename.c_str());
}
currentEntry = 0;
}
TraceWriter::~TraceWriter(){
fwrite(&entries, sizeof(struct TraceEntry), currentEntry, trace);
fclose(trace);
}
void TraceWriter::writeEntry(TraceEntry *entry){
entries[currentEntry] = *entry;
// entries[currentEntry].timestamp = timestamp;
// entries[currentEntry].address = addr;
// entries[currentEntry].size = size;
// entries[currentEntry].read = read;
// entries[currentEntry].instr = instr;
currentEntry++;
if (currentEntry >= BUFFER_SIZE){
fwrite(&entries, sizeof(struct TraceEntry), BUFFER_SIZE, trace);
currentEntry = 0;
}
}
CompressedTraceWriter::TraceSplitter::TraceSplitter(const string& prefix, CompressionType compressionArg){
compression = compressionArg;
if (compression == GZIP){
string filename = prefix+"-time.gz";
if((gzTimestampFile = gzopen(filename.c_str(), "w1")) == 0){
error("Could not open file '%s'", filename.c_str());
}
filename = prefix+"-addr.gz";
if((gzAddressFile = gzopen(filename.c_str(), "w1")) == 0){
error("Could not open file '%s'", filename.c_str());
}
filename = prefix+"-size.gz";
if((gzSizeFile = gzopen(filename.c_str(), "w1")) == 0){
error("Could not open file '%s'", filename.c_str());
}
} else if (compression == BZIP2){
string filename = prefix+"-time.bz2";
int bzerror;
if((timestampFile = fopen(filename.c_str(), "w")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((timestampTrace = BZ2_bzWriteOpen(&bzerror, timestampFile, 9, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
filename = prefix+"-addr.bz2";
if((addressFile = fopen(filename.c_str(), "w")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((addressTrace = BZ2_bzWriteOpen(&bzerror, addressFile, 9, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
filename = prefix+"-size.bz2";
if((sizeFile = fopen(filename.c_str(), "w")) == 0){
error("Could not open file '%s'", filename.c_str());
}
if((sizeTrace = BZ2_bzWriteOpen(&bzerror, sizeFile, 9, 0, 0)) == 0){
error("Could not prepare for reading compressed file '%s': %d", filename.c_str(), bzerror);
}
} else {
error("Invalid compression type");
}
timestampEntries = new uint64[BUFFER_SIZE];
addressEntries = new addrint[BUFFER_SIZE];
sizeEntries = new uint8[BUFFER_SIZE];
currentEntry = 0;
lastTimestamp = 0;
}
CompressedTraceWriter::TraceSplitter::~TraceSplitter(){
if (compression == GZIP){
if (currentEntry != 0){
int timestampWritten = gzwrite(gzTimestampFile, timestampEntries, currentEntry*sizeof(uint64));
int addressWritten = gzwrite(gzAddressFile, addressEntries, currentEntry*sizeof(addrint));
int sizeWritten = gzwrite(gzSizeFile, sizeEntries, currentEntry*sizeof(uint8));
if (timestampWritten == 0 || addressWritten == 0 || sizeWritten == 0){
error("Error writing to compressed file");
}
}
gzclose(gzTimestampFile);
gzclose(gzAddressFile);
gzclose(gzSizeFile);
} else if (compression == BZIP2){
int timestampError, addressError, sizeError;
if (currentEntry != 0){
BZ2_bzWrite(×tampError, timestampTrace, timestampEntries, currentEntry*sizeof(uint64));
BZ2_bzWrite(&addressError, addressTrace, addressEntries, currentEntry*sizeof(addrint));
BZ2_bzWrite(&sizeError, sizeTrace, sizeEntries, currentEntry*sizeof(uint8));
if (timestampError != BZ_OK || addressError != BZ_OK || sizeError != BZ_OK){
error("Error writing to compressed file");
}
}
BZ2_bzWriteClose(×tampError, timestampTrace, 0, 0, 0);
BZ2_bzWriteClose(&addressError, addressTrace, 0, 0, 0);
BZ2_bzWriteClose(&sizeError, sizeTrace, 0, 0, 0);
if (timestampError != BZ_OK || addressError != BZ_OK || sizeError != BZ_OK){
error("Error closing compressed file");
}
fclose(timestampFile);
fclose(addressFile);
fclose(sizeFile);
}
delete [] timestampEntries;
delete [] addressEntries;
delete [] sizeEntries;
}
void CompressedTraceWriter::TraceSplitter::writeEntry(uint64 timestamp, addrint addr, uint8 size){
uint64 ts = timestamp - lastTimestamp;
lastTimestamp = timestamp;
timestampEntries[currentEntry] = ts;
addressEntries[currentEntry] = addr;
sizeEntries[currentEntry] = size;
currentEntry++;
if (currentEntry == BUFFER_SIZE){
if (compression == GZIP){
int timestampWritten = gzwrite(gzTimestampFile, timestampEntries, BUFFER_SIZE*sizeof(uint64));
int addressWritten = gzwrite(gzAddressFile, addressEntries, BUFFER_SIZE*sizeof(addrint));
int sizeWritten = gzwrite(gzSizeFile, sizeEntries, BUFFER_SIZE*sizeof(uint8));
if (timestampWritten == 0 || addressWritten == 0 || sizeWritten == 0){
error("Error writing to compressed file");
}
} else if (compression == BZIP2){
int timestampError, addressError, sizeError;
BZ2_bzWrite(×tampError, timestampTrace, timestampEntries, BUFFER_SIZE*sizeof(uint64));
BZ2_bzWrite(&addressError, addressTrace, addressEntries, BUFFER_SIZE*sizeof(addrint));
BZ2_bzWrite(&sizeError, sizeTrace, sizeEntries, BUFFER_SIZE*sizeof(uint8));
if (timestampError != BZ_OK || addressError != BZ_OK || sizeError != BZ_OK){
error("Error writing to compressed file");
}
}
currentEntry = 0;
}
}
CompressedTraceWriter::CompressedTraceWriter(const string& prefix, CompressionType compressionArg) : instrSplitter(prefix+"-instr", compressionArg), readSplitter(prefix+"-read", compressionArg), writeSplitter(prefix+"-write", compressionArg) {
}
void CompressedTraceWriter::writeEntry(TraceEntry *entry){
if (entry->instr){
instrSplitter.writeEntry(entry->timestamp, entry->address, entry->size);
} else {
if (entry->read){
readSplitter.writeEntry(entry->timestamp, entry->address, entry->size);
} else {
writeSplitter.writeEntry(entry->timestamp, entry->address, entry->size);
}
}
}