-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbt9_reader.h
1816 lines (1565 loc) · 65.3 KB
/
bt9_reader.h
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 Samsung Austin Semiconductor, LLC.
*/
/*!
* \file bt9_reader.h
* \brief This is a reader library for Branch Trace version 9 (BT9) format.
*/
#ifndef __BT9_READER_H__
#define __BT9_READER_H__
#include <stdio.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
#include <string>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include "bt9.h"
namespace bt9 {
/*!
* \class BT9ReaderHeader
* \brief It inherits from the BasicHeader, and is used by the BT9 reader library
*/
class BT9ReaderHeader : public BasicHeader
{
public:
using Dictionary = std::unordered_map<std::string, std::string>;
BT9ReaderHeader() = default;
BT9ReaderHeader(const BT9ReaderHeader &) = default;
/// Get the value(string) of user-defined key-value pairs
bool getFieldValueStr(const std::string & name, std::string & value) const
{
auto it = unclassified_fields_.find(name);
if (it != unclassified_fields_.end()) {
value = it->second;
return true;
}
return false;
}
friend class BT9Reader;
protected:
Dictionary unclassified_fields_;
};
/*!
* \class BT9ReaderNodeRecord
* \brief It inherits from the BasicNodeRecord, and is used by the BT9 reader library
*/
class BT9ReaderNodeRecord : public BasicNodeRecord
{
public:
using Dictionary = std::unordered_map<std::string, std::string>;
BT9ReaderNodeRecord() = default;
BT9ReaderNodeRecord(const BT9ReaderNodeRecord &) = default;
/// Get dynamically observed branch target counts for this branch
uint32_t brObservedTargetCnt() const { return br_tgt_cnt_; }
/// Get the value(string) of user-defined key-value pairs
bool getFieldValueStr(const std::string & name, std::string & value) const
{
auto it = unclassified_fields_.find(name);
if (it != unclassified_fields_.end()) {
value = it->second;
return true;
}
return false;
}
/*!
* \brief Print optional fields of branch node record
*
* This overrides the counterpart in its base class, customized for BT9Reader
*/
void printOptionalFields(std::ostream& os) const override
{
std::ios_base::fmtflags saveflags = os.flags();
std::streamsize prec = os.precision();
std::streamsize width = os.width();
// Bypass for dummy source/sink node
if (opcode_size_ == 0) {
return;
}
// Sanity Check
if (br_class_.directness == BrClass::Directness::DIRECT &&
br_behavior_.indirectness == BrBehavior::Indirectness::INDIRECT) {
std::cerr << "BrClass:" << BrClass::Directness::DIRECT
<< " can never lead to BrBehavior: " << BrBehavior::Indirectness::INDIRECT
<< std::endl;
exit(-1);
}
// Print pre-defined key-value pairs
BasicNodeRecord::printOptionalFields(os);
os << std::setw(2) << " "
<< "tgt_cnt: " << std::setw(4) << std::dec << br_tgt_cnt_ << std::setw(2) << " "
<< "mnemonic: \"" << mnemonic_ << "\"";
// Print user-defined key-value pairs
for (const auto & field : unclassified_fields_) {
os << field.first << " " << field.second;
}
os.flags(saveflags);
os.precision(prec);
os.width(width);
}
friend class BT9Reader;
protected:
uint32_t br_tgt_cnt_ = 0;
Dictionary unclassified_fields_;
};
/*!
* \class BT9ReaderEdgeRecord
* \brief This inherits from the BasicEdgeRecord, and is used by the BT9 reader library
*/
class BT9ReaderEdgeRecord : public BasicEdgeRecord
{
public:
BT9ReaderEdgeRecord() = default;
BT9ReaderEdgeRecord(const BT9ReaderEdgeRecord &) = default;
using Dictionary = std::unordered_map<std::string, std::string>;
/// Get the value(string) of user-defined key-value pairs
bool getFieldValueStr(const std::string & name, std::string & value) const
{
auto it = unclassified_fields_.find(name);
if (it != unclassified_fields_.end()) {
value = it->second;
return true;
}
return false;
}
/*!
* \brief Print optional fields of branch edge record
*
* This overrides the counterpart in its base class, customized for BT9Reader
*/
void printOptionalFields(std::ostream& os) const override
{
BasicEdgeRecord::printOptionalFields(os);
for (const auto & field : unclassified_fields_) {
os << field.first << " " << field.second;
}
}
friend class BT9Reader;
protected:
Dictionary unclassified_fields_;
};
/*!
* \class BT9BranchInstance
* \brief The dereferenced data type returned by BT9Reader BranchInstanceIterator
* \note It contains const raw pointers to edge record, src/dest branch node records
*/
class BT9BranchInstance {
public:
/// Default constructor
BT9BranchInstance() = default;
/*!
* \brief Constructor
* \param src_node_ptr Raw pointer to source node record
* \param dest_node_ptr Raw pointer to destination node record
* \param edge_ptr Raw pointer to edge record
*/
BT9BranchInstance(const BT9ReaderNodeRecord * src_node_ptr,
const BT9ReaderNodeRecord * dest_node_ptr,
const BT9ReaderEdgeRecord * edge_ptr) :
src_node_rec_ptr_(src_node_ptr),
dest_node_rec_ptr_(dest_node_ptr),
edge_rec_ptr_(edge_ptr),
valid_(true)
{}
/// Copy constructor
BT9BranchInstance(const BT9BranchInstance & rhs) :
src_node_rec_ptr_(rhs.src_node_rec_ptr_),
dest_node_rec_ptr_(rhs.dest_node_rec_ptr_),
edge_rec_ptr_(rhs.edge_rec_ptr_),
valid_(rhs.valid_)
{}
/// Indicate if this BT9BranchInstance is valid
bool isValid() const { return valid_; }
/// Get the raw pointer to source node record
const BT9ReaderNodeRecord * getSrcNode() const {
assert(src_node_rec_ptr_ != nullptr);
return src_node_rec_ptr_;
}
/// Get the raw pointer to destination node record
const BT9ReaderNodeRecord * getDestNode() const {
assert(dest_node_rec_ptr_ != nullptr);
return dest_node_rec_ptr_;
}
/// Get the raw pointer to edge record
const BT9ReaderEdgeRecord * getEdge() const {
assert(edge_rec_ptr_ != nullptr);
return edge_rec_ptr_;
}
friend class BT9Reader;
private:
/// Invalidate BT9BranchInstance
void invalidate_()
{
valid_ = false;
src_node_rec_ptr_ = nullptr;
dest_node_rec_ptr_ = nullptr;
edge_rec_ptr_ = nullptr;
}
/// Update BT9BranchInstance
void update_(const BT9ReaderNodeRecord * src_node_ptr,
const BT9ReaderNodeRecord * dest_node_ptr,
const BT9ReaderEdgeRecord * edge_ptr)
{
src_node_rec_ptr_ = src_node_ptr;
dest_node_rec_ptr_ = dest_node_ptr;
edge_rec_ptr_ = edge_ptr;
valid_ = true;
}
const BT9ReaderNodeRecord * src_node_rec_ptr_ = nullptr;
const BT9ReaderNodeRecord * dest_node_rec_ptr_ = nullptr;
const BT9ReaderEdgeRecord * edge_rec_ptr_ = nullptr;
bool valid_ = false;
};
/*!
* \class BT9Reader
* \brief This is the reader library for Branch Trace Version 9 (BT9) file.
*/
class BT9Reader {
public:
/*!
* \brief Constructor
* \param filename BT9 trace file name
* \param buffer_size BT9 edge (i.e. branch instance) sequence list access window size
*/
BT9Reader(const std::string & name,
const uint64_t & buffer_size = 1024) :
node_table(this),
edge_table(this),
tracefile_name_(name),
fpstream_(openBT9TraceFile_(tracefile_name_), boost::iostreams::close_handle),
pinfile_(&fpstream_),
buffer_(buffer_size)
{
readBT9Header_();
readBT9NodeTable_();
readBT9EdgeTable_();
initBT9EdgeSeqListAccessWindow_();
}
BT9Reader() = delete;
BT9Reader(const BT9Reader &) = delete;
BT9Reader(BT9Reader &&) = delete;
BT9Reader & operator=(const BT9Reader &) = delete;
~BT9Reader()
{
}
class NodeTableIterator;
friend class NodeTableIterator;
/*!
* \class NodeTableIterator
* \brief This is the iterator for internal branch node table.
* \note This is supposed to be a random-access iterator
*/
class NodeTableIterator {
public:
NodeTableIterator() = delete;
/*!
* \brief Constructor
* \param rd Pointer to its associated BT9 reader
* \param idx Node record id
*/
NodeTableIterator(const BT9Reader * rd, const uint32_t & idx) :
bt9_reader_(rd),
index_(idx)
{
assert(bt9_reader_ != nullptr);
}
/// Copy constructor
NodeTableIterator(const NodeTableIterator & rhs) :
bt9_reader_(rhs.bt9_reader_),
index_(rhs.index_)
{
assert(bt9_reader_ != nullptr);
}
/// Copy assignment operator
NodeTableIterator & operator=(const NodeTableIterator & rhs)
{
bt9_reader_ = rhs.bt9_reader_;
index_ = rhs.index_;
assert(bt9_reader_ != nullptr);
}
/// Pre-increment operator
NodeTableIterator & operator++() {
++index_;
return *this;
}
/// Post-increment operator
NodeTableIterator operator++(int) {
NodeTableIterator it(*this);
++index_;
return it;
}
/// Equal operator
bool operator==(const NodeTableIterator & rhs) const {
return ((bt9_reader_ == rhs.bt9_reader_) && (index_ == rhs.index_));
}
/// Not-equal operator
bool operator!=(const NodeTableIterator & rhs) const {
return !(this->operator==(rhs));
}
/*!
* \brief Dereference operator
* \note Bound-checking is supported. If the itertor internal index is invalid,
* the std::invalid_argument exception is thrown.
*/
BT9ReaderNodeRecord & operator*() {
if (bt9_reader_->isValidNodeIndex_(index_)) {
return *(bt9_reader_->node_order_vector_[index_]);
}
else {
throw std::invalid_argument("Invalid Node Index!\n");
}
}
/*!
* \brief Dereference operator
* \note Bound-checking is supported. If the itertor internal index is invalid,
* the std::invalid_argument exception is thrown.
*/
BT9ReaderNodeRecord * operator->() {
if (bt9_reader_->isValidNodeIndex_(index_)) {
return bt9_reader_->node_order_vector_[index_];
}
else {
throw std::invalid_argument("Invalid Node Index!\n");
}
}
NodeTableIterator operator+(uint32_t offset) const {
NodeTableIterator it(bt9_reader_, index_+offset);
return it;
}
NodeTableIterator operator-(uint32_t offset) const {
NodeTableIterator it(bt9_reader_, index_-offset);
return it;
}
int64_t operator-(const NodeTableIterator & rhs) const {
return (index_ - rhs.index_);
}
bool operator<(const NodeTableIterator & rhs) const {
return (index_ < rhs.index_);
}
bool operator>(const NodeTableIterator & rhs) const {
return (index_ > rhs.index_);
}
bool operator<=(const NodeTableIterator & rhs) const {
return !this->operator>(rhs);
}
bool operator>=(const NodeTableIterator & rhs) const {
return !this->operator<(rhs);
}
NodeTableIterator operator+=(uint32_t offset) {
index_ += offset;
return *this;
}
NodeTableIterator operator-=(uint32_t offset) {
index_ -= offset;
return *this;
}
BT9ReaderNodeRecord & operator[](uint32_t idx) {
if (bt9_reader_->isValidNodeIndex_(idx)) {
return *(bt9_reader_->node_order_vector_[idx]);
}
else {
throw std::invalid_argument("Invalid Node Index!\n");
}
}
const BT9ReaderNodeRecord & operator[](uint32_t idx) const {
if (bt9_reader_->isValidNodeIndex_(idx)) {
return *(bt9_reader_->node_order_vector_[idx]);
}
else {
throw std::invalid_argument("Invalid Node Index!\n");
}
}
private:
const BT9Reader * bt9_reader_ = nullptr;
uint32_t index_ = 0;
};
/*!
* \class NodeTable
* \brief This is a wrapper class of internal node table
* \note It provides the iterator interface that is accessible to the user
*/
class NodeTable
{
public:
NodeTableIterator begin() const { return bt9_reader_->nodeTableBegin_(); }
NodeTableIterator end() const { return bt9_reader_->nodeTableEnd_(); }
friend std::ostream & operator<<(std::ostream &, const NodeTable &);
friend BT9Reader;
private:
void print_(std::ostream & os) const
{
assert(bt9_reader_ != nullptr);
os << "BT9_NODES\n";
os << "#NODE "
<< std::setw(4) << " id "
<< std::setw(20) << " virtual_address "
<< std::setw(20) << " physical_address "
<< std::setw(16) << " opcode "
<< std::setw(4) << " size "
<< '\n';
for (const auto & node : bt9_reader_->node_table) {
os << "NODE: " << node << '\n';
}
}
NodeTable(const BT9Reader * rd) : bt9_reader_(rd) {};
NodeTable(const NodeTable &) = delete;
NodeTable(NodeTable &&) = delete;
NodeTable & operator=(const NodeTable &) = delete;
const BT9Reader * bt9_reader_ = nullptr;
};
class EdgeTableIterator;
friend class EdgeTableIterator;
/*!
* \class EdgeTableIterator
* \brief This is the iterator for internal branch edge table.
* \note This is supposed to be a random-access iterator
*/
class EdgeTableIterator {
public:
EdgeTableIterator() = delete;
/*!
* \brief Constructor
* \param rd Pointer to its associated BT9 reader
* \param idx Edge record id
*/
EdgeTableIterator(const BT9Reader * rd, const uint32_t & idx) :
bt9_reader_(rd),
index_(idx)
{
assert(bt9_reader_ != nullptr);
}
/// Copy constructor
EdgeTableIterator(const EdgeTableIterator & rhs) :
bt9_reader_(rhs.bt9_reader_),
index_(rhs.index_)
{
assert(bt9_reader_ != nullptr);
}
/// Copy assignment operator
EdgeTableIterator & operator=(const EdgeTableIterator & rhs)
{
bt9_reader_ = rhs.bt9_reader_;
index_ = rhs.index_;
assert(bt9_reader_ != nullptr);
}
/// Pre-increment operator
EdgeTableIterator & operator++() {
++index_;
return *this;
}
/// Post-increment operator
EdgeTableIterator operator++(int) {
EdgeTableIterator it(*this);
++index_;
return it;
}
/// Equal operator
bool operator==(const EdgeTableIterator & rhs) const {
return ((bt9_reader_ == rhs.bt9_reader_) && (index_ == rhs.index_));
}
/// Not-equal operator
bool operator!=(const EdgeTableIterator & rhs) const {
return !(this->operator==(rhs));
}
/*!
* \brief Dereference operator
* \note Bound-checking is supported. If the itertor internal index is invalid,
* the std::invalid_argument exception is thrown.
*/
BT9ReaderEdgeRecord & operator*()
{
if (bt9_reader_->isValidEdgeIndex_(index_)) {
return *(bt9_reader_->edge_order_vector_[index_]);
}
else {
throw std::invalid_argument("Invalid Edge Index!\n");
}
}
/*!
* \brief Dereference operator
* \note Bound-checking is supported. If the itertor internal index is invalid,
* the std::invalid_argument exception is thrown.
*/
BT9ReaderEdgeRecord * operator->()
{
if (bt9_reader_->isValidEdgeIndex_(index_)) {
return bt9_reader_->edge_order_vector_[index_];
}
else {
throw std::invalid_argument("Invalid Edge Index!\n");
}
}
EdgeTableIterator operator+(uint32_t offset) const {
EdgeTableIterator it(bt9_reader_, index_+offset);
return it;
}
EdgeTableIterator operator-(uint32_t offset) const {
EdgeTableIterator it(bt9_reader_, index_-offset);
return it;
}
int64_t operator-(const EdgeTableIterator & rhs) const {
return (index_ - rhs.index_);
}
bool operator<(const EdgeTableIterator & rhs) const {
return (index_ < rhs.index_);
}
bool operator>(const EdgeTableIterator & rhs) const {
return (index_ > rhs.index_);
}
bool operator<=(const EdgeTableIterator & rhs) const {
return !this->operator>(rhs);
}
bool operator>=(const EdgeTableIterator & rhs) const {
return !this->operator<(rhs);
}
EdgeTableIterator operator+=(uint32_t offset) {
index_ += offset;
return *this;
}
EdgeTableIterator operator-=(uint32_t offset) {
index_ -= offset;
return *this;
}
BT9ReaderEdgeRecord & operator[](uint32_t idx) {
if (bt9_reader_->isValidEdgeIndex_(idx)) {
return *(bt9_reader_->edge_order_vector_[idx]);
}
else {
throw std::invalid_argument("Invalid Edge Index!\n");
}
}
const BT9ReaderEdgeRecord & operator[](uint32_t idx) const {
if (bt9_reader_->isValidEdgeIndex_(idx)) {
return *(bt9_reader_->edge_order_vector_[idx]);
}
else {
throw std::invalid_argument("Invalid Edge Index!\n");
}
}
private:
const BT9Reader * bt9_reader_ = nullptr;
uint32_t index_ = 0;
};
/*!
* \class EdgeTable
* \brief This is a wrapper class of internal edge table
* \note It provides the iterator interface that is accessible to the user
*/
class EdgeTable
{
public:
EdgeTableIterator begin() const { return bt9_reader_->edgeTableBegin_(); }
EdgeTableIterator end() const { return bt9_reader_->edgeTableEnd_(); }
friend std::ostream & operator<<(std::ostream &, const BT9Reader::EdgeTable &);
friend class BT9Reader;
private:
void print_(std::ostream & os) const
{
assert(bt9_reader_ != nullptr);
os << "BT9_EDGES\n";
os << "#EDGE "
<< std::setw(4) << " id"
<< std::setw(4) << " src_id "
<< std::setw(4) << " dest_id"
<< std::setw(8) << "taken "
<< std::setw(20) << " br_virt_target "
<< std::setw(20) << " br_phy_target "
<< std::setw(8) << " inst_cnt "
<< '\n';
for (const auto & edge : bt9_reader_->edge_table) {
os << "EDGE " << edge << '\n';
}
}
EdgeTable(const BT9Reader * rd) : bt9_reader_(rd) {};
EdgeTable(const EdgeTable &) = delete;
EdgeTable(EdgeTable &&) = delete;
EdgeTable & operator=(const EdgeTable &) = delete;
const BT9Reader * bt9_reader_ = nullptr;
};
class BranchInstanceIterator;
friend class BranchInstanceIterator;
/*!
* \class BranchInstanceIterator
* \brief This is the iterator for internal branch edge sequence list.
* \note This is supposed to be a forward(input) iterator
*/
class BranchInstanceIterator {
public:
/*!
* \brief Default constructor
*
* It's constructed to be an end iterator by default if no BT9Reader is provided
*/
BranchInstanceIterator() :
bt9_reader_(nullptr),
reach_end_(true)
{}
/*!
* \brief Constructor
* \param rd Pointer to its associated BT9 reader
* \param end Indicate if it points to the end of edge sequence list
*/
BranchInstanceIterator(BT9Reader * rd,
bool end = false) :
bt9_reader_(rd),
reach_end_(end)
{
}
/*!
* \brief Copy constructor
* \param rhs The other BranchInstanceIterator instance to copy from
*/
BranchInstanceIterator(const BranchInstanceIterator & rhs) :
bt9_reader_(rhs.bt9_reader_),
reach_end_(rhs.reach_end_),
index_(rhs.index_),
br_inst_(rhs.br_inst_)
{
}
/*!
* \brief Move constructor
* \param rhs The other BranchInstanceIterator instance to move from
*/
BranchInstanceIterator(BranchInstanceIterator && rhs) :
bt9_reader_(rhs.bt9_reader_),
reach_end_(rhs.reach_end_),
index_(rhs.index_),
br_inst_(rhs.br_inst_)
{
}
/*!
* \brief Copy assignment operator
* \param rhs The other BranchInstanceIterator instance to copy from
*/
BranchInstanceIterator & operator=(const BranchInstanceIterator & rhs)
{
bt9_reader_ = rhs.bt9_reader_;
reach_end_ = rhs.reach_end_;
index_ = rhs.index_;
br_inst_ = rhs.br_inst_;
return *this;
}
/// Destructor
~BranchInstanceIterator() {}
/*!
* \brief Pre-increment operator
* \note If the iterator access index goes beyond its current access window,
* the window will be automatically shifted forward (half-buffer size stride)
* by the BT9Reader library helper functions
*/
BranchInstanceIterator & operator++()
{
br_inst_.invalidate_();
if (!reach_end_) {
reach_end_ = !bt9_reader_->moveToNextEdgeSeqListEntry_(index_);
}
return *this;
}
/*!
* \brief Post-increment operator
* \note If the iterator access index goes beyond its current access window,
* the window will be automatically shifted forward (half-buffer size stride)
* by the BT9Reader library helper functions
*/
BranchInstanceIterator operator++(int)
{
auto temp = *this;
br_inst_.invalidate_();
if (!reach_end_) {
reach_end_ = !bt9_reader_->moveToNextEdgeSeqListEntry_(index_);
}
return *this;
}
/*!
* \brief Equal operator
* \note The end iterator comparison is treated separately because the iterator
* itself is not aware of its position in the file until the BT9Reader
* informs it about this
*/
bool operator==(const BranchInstanceIterator & rhs) const {
if (bt9_reader_ != rhs.bt9_reader_) {
return false;
}
else if (!reach_end_ && !rhs.reach_end_) {
return (index_ == rhs.index_);
}
else {
return (reach_end_ && rhs.reach_end_);
}
}
/// Not-equal operator
bool operator!=(const BranchInstanceIterator & rhs) const {
return !this->operator==(rhs);
}
/*!
* \brief Dereference operator
* \note Bound-checking is done under the scene by the BT9Reader helper function
*/
BT9BranchInstance & operator*()
{
if (!br_inst_.isValid()) {
bt9_reader_->loadBT9BranchInstance_(index_, br_inst_);
}
return br_inst_;
}
/*!
* \brief Dereference operator
* \note Bound-checking is done under the scene by the BT9Reader helper function
*/
BT9BranchInstance * operator->()
{
if (!br_inst_.isValid()) {
bt9_reader_->loadBT9BranchInstance_(index_, br_inst_);
}
return &br_inst_;
}
private:
BT9Reader * bt9_reader_ = nullptr;
bool reach_end_ = false;
uint64_t index_ = 0;
BT9BranchInstance br_inst_;
};
BranchInstanceIterator begin() { return BranchInstanceIterator(this); }
BranchInstanceIterator end() { return BranchInstanceIterator(this, true); }
public:
/// BT9 header
BT9ReaderHeader header;
/// Node table (wrapper class for the internal node table) that is visible to the user
NodeTable node_table;
/// Edge table (wrapper class for the internal edge table) that is visible to the user
EdgeTable edge_table;
private:
/*!
* \brief Open and read BT9 trace file via Linux pipe
* \param name BT9 trace file path
* \Return It returns the integer file descriptor that is required by the
* boost::iostreams::stream_buffer
*/
int openBT9TraceFile_(const std::string & name) {
std::string cmd = "/bin/cat " + name;
auto gzip_suffix_pos = name.find(".gz");
if (gzip_suffix_pos != std::string::npos) {
cmd = "gunzip -dc " + name;
}
FILE * pipe = popen(cmd.c_str(), "r");
if (!pipe) {
std::cerr << "Failed to open trace file \'"
<< name << "\' with pipe\n";
exit(-1);
}
return fileno(pipe);
}
/// Read BT9 tracefile header with linux pipe
void readBT9Header_()
{
std::string line;
std::string token;
// Check BT9 header title line
std::getline(pinfile_, line, '\n');
line_num_++;
std::stringstream ss(line);
ss >> token;
if (token != "BT9_SPA_TRACE_FORMAT") {
std::cerr << "line:" << line_num_ << " \'" << tracefile_name_ << "\' is not BT9 file\n";
exit(-1);
}
// Read BT9 header fields (each is a key-value string pair)
while (std::getline(pinfile_, line, '\n')) {
line_num_++;
// Skip any comments at the end of line
auto pos = line.find_first_of('#');
if (pos != std::string::npos) {
line.erase(pos, std::numeric_limits<std::string::size_type>::max());
}
std::stringstream ss(line);
std::string token;
ss >> token;
// Skip if the whole line contains only comments
if (token.empty()) {
continue;
}
if (token == "BT9_NODES") {
reach_node_table_ = true;
break;
}
else {
// Erase the key-string, get the following value-string
line.erase(0, token.size());
// Update header fields
parseHeader_(ss, token, line);
}
}
}
/// Parse BT9 tracefile header
void parseHeader_(std::stringstream & ss,
std::string & token,
std::string & line)
{
if (token == "bt9_minor_version:") {
ss >> token;
try {
header.version_num_ = static_cast<BasicHeader::BT9MinorVersionNum>(std::stoul(token, nullptr, 0));
}
catch (const std::invalid_argument & ex) {
std::cerr << ex.what();
std::cerr << "line:" << line_num_ << " bt9_minor_version: " << token << " is invalid!\n";
exit(-1);
}
}
else if (token == "has_physical_address:") {
ss >> token;
try {
header.has_phy_addr_ = std::stoul(token, nullptr, 0);
}
catch (const std::invalid_argument & ex) {
std::cerr << ex.what();
std::cerr << "line:" << line_num_ << " has_physical_address: " << token << " is invalid!\n";
exit(-1);
}
}
else if (token == "md5_checksum:") {
header.md5sum_ = line;
}
else if (token == "conversion_date:") {
header.date_ = line;
}
else if (token == "original_stf_input_file:") {
header.original_tracefile_path_ = line;
}
else {
header.unclassified_fields_[token] = line;
}
}
/// Read BT9 tracefile node table with linux pipe
void readBT9NodeTable_()
{
std::string line;