a886f32384884ff0dfbc5625993eaea9fbc8fc06
[gem5.git] / src / mem / ruby / recorder / CacheRecorder.cc
1 /*
2 * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
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.
16 *
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.
28 */
29
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"
34
35 using namespace std;
36
37 void
38 TraceRecord::print(ostream& out) const
39 {
40 out << "[TraceRecord: Node, " << m_cntrl_id << ", "
41 << m_data_address << ", " << m_pc_address << ", "
42 << m_type << ", Time: " << m_time << "]";
43 }
44
45 CacheRecorder::CacheRecorder()
46 : m_uncompressed_trace(NULL),
47 m_uncompressed_trace_size(0)
48 {
49 }
50
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),
57 m_records_flushed(0)
58 {
59 }
60
61 CacheRecorder::~CacheRecorder()
62 {
63 if (m_uncompressed_trace != NULL) {
64 delete m_uncompressed_trace;
65 m_uncompressed_trace = NULL;
66 }
67 m_seq_map.clear();
68 }
69
70 void
71 CacheRecorder::enqueueNextFlushRequest()
72 {
73 if (m_records_flushed < m_records.size()) {
74 TraceRecord* rec = m_records[m_records_flushed];
75 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);
81
82 Sequencer* m_sequencer_ptr = m_seq_map[rec->m_cntrl_id];
83 assert(m_sequencer_ptr != NULL);
84 m_sequencer_ptr->makeRequest(pkt);
85
86 DPRINTF(RubyCacheTrace, "Flushing %s\n", *rec);
87 }
88 }
89
90 void
91 CacheRecorder::enqueueNextFetchRequest()
92 {
93 if (m_bytes_read < m_uncompressed_trace_size) {
94 TraceRecord* traceRecord = (TraceRecord*) (m_uncompressed_trace +
95 m_bytes_read);
96
97 DPRINTF(RubyCacheTrace, "Issuing %s\n", *traceRecord);
98 Request* req = new Request();
99 MemCmd::Command requestType;
100
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);
110 } else {
111 requestType = MemCmd::WriteReq;
112 req->setPhys(traceRecord->m_data_address,
113 RubySystem::getBlockSizeBytes(),0, Request::funcMasterId);
114 }
115
116 Packet *pkt = new Packet(req, requestType, -1);
117 pkt->dataStatic(traceRecord->m_data);
118
119 Sequencer* m_sequencer_ptr = m_seq_map[traceRecord->m_cntrl_id];
120 assert(m_sequencer_ptr != NULL);
121 m_sequencer_ptr->makeRequest(pkt);
122
123 m_bytes_read += (sizeof(TraceRecord) +
124 RubySystem::getBlockSizeBytes());
125 m_records_read++;
126 }
127 }
128
129 void
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)
133 {
134 TraceRecord* rec = (TraceRecord*)malloc(sizeof(TraceRecord) +
135 RubySystem::getBlockSizeBytes());
136 rec->m_cntrl_id = cntrl;
137 rec->m_time = time;
138 rec->m_data_address = data_addr;
139 rec->m_pc_address = pc_addr;
140 rec->m_type = type;
141 memcpy(rec->m_data, data.getData(0, RubySystem::getBlockSizeBytes()),
142 RubySystem::getBlockSizeBytes());
143
144 m_records.push_back(rec);
145 }
146
147 uint64
148 CacheRecorder::aggregateRecords(uint8_t** buf, uint64 total_size)
149 {
150 std::sort(m_records.begin(), m_records.end(), compareTraceRecords);
151
152 int size = m_records.size();
153 uint64 current_size = 0;
154 int record_size = sizeof(TraceRecord) + RubySystem::getBlockSizeBytes();
155
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",
162 total_size * 2);
163 }
164 total_size = total_size * 2;
165 uint8_t* old_buf = *buf;
166 memcpy(new_buf, old_buf, current_size);
167 *buf = new_buf;
168 delete [] old_buf;
169 }
170
171 // Copy the current record into the buffer
172 memcpy(&((*buf)[current_size]), m_records[i], record_size);
173 current_size += record_size;
174
175 free(m_records[i]);
176 m_records[i] = NULL;
177 }
178
179 m_records.clear();
180 return current_size;
181 }