2 * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "debug/RubyCacheTrace.hh"
31 #include "mem/ruby/recorder/CacheRecorder.hh"
32 #include "mem/ruby/system/Sequencer.hh"
33 #include "mem/ruby/system/System.hh"
38 TraceRecord::print(ostream
& out
) const
40 out
<< "[TraceRecord: Node, " << m_cntrl_id
<< ", "
41 << m_data_address
<< ", " << m_pc_address
<< ", "
42 << m_type
<< ", Time: " << m_time
<< "]";
45 CacheRecorder::CacheRecorder()
46 : m_uncompressed_trace(NULL
),
47 m_uncompressed_trace_size(0)
51 CacheRecorder::CacheRecorder(uint8_t* uncompressed_trace
,
52 uint64_t uncompressed_trace_size
,
53 std::vector
<Sequencer
*>& seq_map
)
54 : m_uncompressed_trace(uncompressed_trace
),
55 m_uncompressed_trace_size(uncompressed_trace_size
),
56 m_seq_map(seq_map
), m_bytes_read(0), m_records_read(0),
61 CacheRecorder::~CacheRecorder()
63 if (m_uncompressed_trace
!= NULL
) {
64 delete m_uncompressed_trace
;
65 m_uncompressed_trace
= NULL
;
71 CacheRecorder::enqueueNextFlushRequest()
73 if (m_records_flushed
< m_records
.size()) {
74 TraceRecord
* rec
= m_records
[m_records_flushed
];
76 Request
* req
= new Request(rec
->m_data_address
,
77 RubySystem::getBlockSizeBytes(),0,
78 Request::funcMasterId
);
79 MemCmd::Command requestType
= MemCmd::FlushReq
;
80 Packet
*pkt
= new Packet(req
, requestType
, -1);
82 Sequencer
* m_sequencer_ptr
= m_seq_map
[rec
->m_cntrl_id
];
83 assert(m_sequencer_ptr
!= NULL
);
84 m_sequencer_ptr
->makeRequest(pkt
);
86 DPRINTF(RubyCacheTrace
, "Flushing %s\n", *rec
);
91 CacheRecorder::enqueueNextFetchRequest()
93 if (m_bytes_read
< m_uncompressed_trace_size
) {
94 TraceRecord
* traceRecord
= (TraceRecord
*) (m_uncompressed_trace
+
97 DPRINTF(RubyCacheTrace
, "Issuing %s\n", *traceRecord
);
98 Request
* req
= new Request();
99 MemCmd::Command requestType
;
101 if (traceRecord
->m_type
== RubyRequestType_LD
) {
102 requestType
= MemCmd::ReadReq
;
103 req
->setPhys(traceRecord
->m_data_address
,
104 RubySystem::getBlockSizeBytes(),0, Request::funcMasterId
);
105 } else if (traceRecord
->m_type
== RubyRequestType_IFETCH
) {
106 requestType
= MemCmd::ReadReq
;
107 req
->setPhys(traceRecord
->m_data_address
,
108 RubySystem::getBlockSizeBytes(),
109 Request::INST_FETCH
, Request::funcMasterId
);
111 requestType
= MemCmd::WriteReq
;
112 req
->setPhys(traceRecord
->m_data_address
,
113 RubySystem::getBlockSizeBytes(),0, Request::funcMasterId
);
116 Packet
*pkt
= new Packet(req
, requestType
, -1);
117 pkt
->dataStatic(traceRecord
->m_data
);
119 Sequencer
* m_sequencer_ptr
= m_seq_map
[traceRecord
->m_cntrl_id
];
120 assert(m_sequencer_ptr
!= NULL
);
121 m_sequencer_ptr
->makeRequest(pkt
);
123 m_bytes_read
+= (sizeof(TraceRecord
) +
124 RubySystem::getBlockSizeBytes());
130 CacheRecorder::addRecord(int cntrl
, const physical_address_t data_addr
,
131 const physical_address_t pc_addr
,
132 RubyRequestType type
, Time time
, DataBlock
& data
)
134 TraceRecord
* rec
= (TraceRecord
*)malloc(sizeof(TraceRecord
) +
135 RubySystem::getBlockSizeBytes());
136 rec
->m_cntrl_id
= cntrl
;
138 rec
->m_data_address
= data_addr
;
139 rec
->m_pc_address
= pc_addr
;
141 memcpy(rec
->m_data
, data
.getData(0, RubySystem::getBlockSizeBytes()),
142 RubySystem::getBlockSizeBytes());
144 m_records
.push_back(rec
);
148 CacheRecorder::aggregateRecords(uint8_t** buf
, uint64 total_size
)
150 std::sort(m_records
.begin(), m_records
.end(), compareTraceRecords
);
152 int size
= m_records
.size();
153 uint64 current_size
= 0;
154 int record_size
= sizeof(TraceRecord
) + RubySystem::getBlockSizeBytes();
156 for (int i
= 0; i
< size
; ++i
) {
157 // Determine if we need to expand the buffer size
158 if (current_size
+ record_size
> total_size
) {
159 uint8_t* new_buf
= new (nothrow
) uint8_t[total_size
* 2];
160 if (new_buf
== NULL
) {
161 fatal("Unable to allocate buffer of size %s\n",
164 total_size
= total_size
* 2;
165 uint8_t* old_buf
= *buf
;
166 memcpy(new_buf
, old_buf
, current_size
);
171 // Copy the current record into the buffer
172 memcpy(&((*buf
)[current_size
]), m_records
[i
], record_size
);
173 current_size
+= record_size
;