gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ExpectedMap.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __MEM_RUBY_COMMON_EXPECTEDMAP_HH__
39 #define __MEM_RUBY_COMMON_EXPECTEDMAP_HH__
40 
41 #include <cassert>
42 #include <iostream>
43 #include <unordered_map>
44 
45 // ExpectedMap helper class is used to facilitate tracking of pending
46 // response and data messages in the CHI protocol. It offers additional
47 // functionality when compared to plain counters:
48 // - tracks the expected type for received messages
49 // - tracks segmented data messages (i.e. when a line transfer is split in
50 // multiple messages)
51 
52 template<typename RespType, typename DataType>
54 {
55  private:
56 
57  template<typename Type>
59  {
61  {
62  std::size_t operator()(Type t) const
63  {
64  return static_cast<std::size_t>(t);
65  }
66  };
67 
68  private:
69  // chunks is the number segmented messages we expect to receive
70  // before incrementing numReceived. This is tipically always 1 for all
71  // non-data messages
72  int chunks;
73  int currChunk;
75  std::unordered_map<Type, bool, EnumClassHash> expectedTypes;
76 
77  public:
79  :chunks(1), currChunk(0), numReceived(0)
80  {}
81 
82  void
83  clear(int msg_chunks)
84  {
85  chunks = msg_chunks;
86  currChunk = 0;
87  numReceived = 0;
88  expectedTypes.clear();
89  }
90 
91  void
92  addExpectedType(const Type &val)
93  {
94  expectedTypes[val] = false;
95  }
96 
97  int received() const { return numReceived; }
98 
99  bool
100  increaseReceived(const Type &val)
101  {
102  if (expectedTypes.find(val) == expectedTypes.end())
103  return false;
104 
105  expectedTypes[val] = true;
106  ++currChunk;
107  if (currChunk == chunks) {
108  ++numReceived;
109  currChunk = 0;
110  }
111 
112  return true;
113  }
114 
115  bool
116  receivedType(const Type &val) const
117  {
118  auto i = expectedTypes.find(val);
119  if (i != expectedTypes.end())
120  return i->second;
121  else
122  return false;
123  }
124  };
125 
129 
130  public:
133  {}
134 
135  // Clear the tracking state and specified the number of chunks are required
136  // to receive a complete data message
137  void
138  clear(int dataChunks)
139  {
140  expectedData.clear(dataChunks);
141  expectedResp.clear(1);
142  totalExpected = 0;
143  }
144 
145  // Register an expected response message type
146  void
147  addExpectedRespType(const RespType &val)
148  {
150  }
151 
152  // Register an expected data message type
153  void
154  addExpectedDataType(const DataType &val)
155  {
157  }
158 
159  // Set the number of expected messages
161 
163 
164  // Returns the number of messages received.
165  // Notice that a data message counts as received only after all of
166  // its chunks are received.
167  int
168  received() const
169  {
171  }
172 
173  // Returns the remaining number of expected messages
174  int expected() const { return totalExpected - received(); }
175 
176  // Has any expected message ?
177  bool hasExpected() const { return expected() != 0; }
178 
179  // Has received any data ?
180  bool hasReceivedData() const { return expectedData.received() != 0; }
181 
182  // Has received any response ?
183  bool hasReceivedResp() const { return expectedResp.received() != 0; }
184 
185 
186  // Notifies that a response message was received
187  bool
188  receiveResp(const RespType &val)
189  {
190  assert(received() < totalExpected);
192  }
193 
194  // Notifies that a data message chunk was received
195  bool
196  receiveData(const DataType &val)
197  {
198  assert(received() <= totalExpected);
200  }
201 
202  // Has received any data of the given type ?
203  bool
204  receivedDataType(const DataType &val) const
205  {
206  return expectedData.receivedType(val);
207  }
208 
209  // Has received any response of the given type ?
210  bool
211  receivedRespType(const RespType &val) const
212  {
213  return expectedResp.receivedType(val);
214  }
215 
216  void
217  print(std::ostream& out) const
218  {
219  out << expected();
220  }
221 };
222 
223 template<typename RespType, typename DataType>
224 inline std::ostream&
225 operator<<(std::ostream& out, const ExpectedMap<RespType,DataType>& obj)
226 {
227  obj.print(out);
228  return out;
229 }
230 
231 
232 #endif // __MEM_RUBY_COMMON_EXPECTEDMAP_HH__
ExpectedMap::hasReceivedData
bool hasReceivedData() const
Definition: ExpectedMap.hh:180
ExpectedMap::receivedDataType
bool receivedDataType(const DataType &val) const
Definition: ExpectedMap.hh:204
ExpectedMap::receiveResp
bool receiveResp(const RespType &val)
Definition: ExpectedMap.hh:188
ExpectedMap::ExpectedState::clear
void clear(int msg_chunks)
Definition: ExpectedMap.hh:83
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
ExpectedMap::setExpectedCount
void setExpectedCount(int val)
Definition: ExpectedMap.hh:160
ExpectedMap::ExpectedState::chunks
int chunks
Definition: ExpectedMap.hh:72
ExpectedMap::hasReceivedResp
bool hasReceivedResp() const
Definition: ExpectedMap.hh:183
ExpectedMap::totalExpected
int totalExpected
Definition: ExpectedMap.hh:128
ExpectedMap::ExpectedState
Definition: ExpectedMap.hh:58
ExpectedMap::expectedData
ExpectedState< DataType > expectedData
Definition: ExpectedMap.hh:126
ExpectedMap::addExpectedRespType
void addExpectedRespType(const RespType &val)
Definition: ExpectedMap.hh:147
ExpectedMap::ExpectedState::addExpectedType
void addExpectedType(const Type &val)
Definition: ExpectedMap.hh:92
ExpectedMap::expected
int expected() const
Definition: ExpectedMap.hh:174
operator<<
std::ostream & operator<<(std::ostream &out, const ExpectedMap< RespType, DataType > &obj)
Definition: ExpectedMap.hh:225
ExpectedMap::ExpectedState::expectedTypes
std::unordered_map< Type, bool, EnumClassHash > expectedTypes
Definition: ExpectedMap.hh:75
ExpectedMap::receivedRespType
bool receivedRespType(const RespType &val) const
Definition: ExpectedMap.hh:211
ExpectedMap::expectedResp
ExpectedState< RespType > expectedResp
Definition: ExpectedMap.hh:127
ExpectedMap::ExpectedState::receivedType
bool receivedType(const Type &val) const
Definition: ExpectedMap.hh:116
ExpectedMap::print
void print(std::ostream &out) const
Definition: ExpectedMap.hh:217
ExpectedMap::received
int received() const
Definition: ExpectedMap.hh:168
ExpectedMap::ExpectedState::increaseReceived
bool increaseReceived(const Type &val)
Definition: ExpectedMap.hh:100
ExpectedMap::ExpectedState::EnumClassHash
Definition: ExpectedMap.hh:60
ExpectedMap::addExpectedDataType
void addExpectedDataType(const DataType &val)
Definition: ExpectedMap.hh:154
ExpectedMap::ExpectedState::EnumClassHash::operator()
std::size_t operator()(Type t) const
Definition: ExpectedMap.hh:62
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
ExpectedMap::addExpectedCount
void addExpectedCount(int val)
Definition: ExpectedMap.hh:162
ExpectedMap::ExpectedState::currChunk
int currChunk
Definition: ExpectedMap.hh:73
ExpectedMap
Definition: ExpectedMap.hh:53
ExpectedMap::ExpectedMap
ExpectedMap()
Definition: ExpectedMap.hh:131
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
ExpectedMap::receiveData
bool receiveData(const DataType &val)
Definition: ExpectedMap.hh:196
ExpectedMap::hasExpected
bool hasExpected() const
Definition: ExpectedMap.hh:177
ExpectedMap::clear
void clear(int dataChunks)
Definition: ExpectedMap.hh:138
ExpectedMap::ExpectedState::received
int received() const
Definition: ExpectedMap.hh:97
ExpectedMap::ExpectedState::ExpectedState
ExpectedState()
Definition: ExpectedMap.hh:78
ExpectedMap::ExpectedState::numReceived
int numReceived
Definition: ExpectedMap.hh:74

Generated on Tue Jun 22 2021 15:28:29 for gem5 by doxygen 1.8.17