mc2lib
eventsets.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Marco Elver
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the
15  * distribution.
16  *
17  * * Neither the name of the software nor the names of its contributors
18  * may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifndef MC2LIB_MEMCONSISTENCY_EVENTSETS_HPP_
35 #define MC2LIB_MEMCONSISTENCY_EVENTSETS_HPP_
36 
37 #include <iomanip>
38 #include <sstream>
39 #include <stdexcept>
40 #include <string>
41 
42 #include "../sets.hpp"
43 #include "../types.hpp"
44 
45 namespace mc2lib {
46 
51 namespace memconsistency {
52 
53 class Iiid {
54  public:
55  struct Hash {
56  typedef std::hash<types::Poi>::result_type result_type;
57  result_type operator()(const Iiid& k) const {
58  return std::hash<types::Poi>()(k.poi);
59  }
60  };
61 
62  Iiid() : pid(0), poi(0) {}
63 
64  Iiid(types::Pid pid_, types::Poi poi_) : pid(pid_), poi(poi_) {}
65 
66  operator std::string() const {
67  std::ostringstream oss;
68  oss << "P" << std::setfill('0') << std::setw(2) << pid << ": "
69  << std::setfill('0') << std::setw(sizeof(types::Poi) * 2) << std::hex
70  << poi;
71  return oss.str();
72  }
73 
74  bool operator==(const Iiid& rhs) const {
75  return pid == rhs.pid && poi == rhs.poi;
76  }
77 
78  bool operator!=(const Iiid& rhs) const {
79  return pid != rhs.pid || poi != rhs.poi;
80  }
81 
82  bool operator<(const Iiid& rhs) const {
83  return pid < rhs.pid || (pid == rhs.pid && poi < rhs.poi);
84  }
85 
87  ++poi;
88  return *this;
89  }
90 
91  Iiid Next() const { return Iiid(pid, poi + 1); }
92 
93  Iiid Prev() const {
94  assert(poi > 0);
95  return Iiid(pid, poi - 1);
96  }
97 
98  public:
101 };
102 
103 class Event {
104  public:
105  struct Hash {
107  return Iiid::Hash()(k.iiid);
108  }
109  };
110 
111  typedef std::uint32_t Type;
112 
113  // TYPE DEFINITIONS {{{
114 
115  static constexpr Type kNone = 0x00000000;
116 
117  // Memory operations
118  static constexpr Type kRead = 0x00000001;
119  static constexpr Type kWrite = 0x00000002;
120  static constexpr Type kAcquire = 0x00000004;
121  static constexpr Type kRelease = 0x00000008;
122  static constexpr Type kMemoryOperation = kRead | kWrite | kAcquire | kRelease;
123 
124  // Auxiliary attributes
125  static constexpr Type kRegInAddr = 0x00000010;
126  static constexpr Type kRegInData = 0x00000020;
127  static constexpr Type kRegOut = 0x00000040;
128  static constexpr Type kBranch = 0x00000080;
129 
130  // User declared attributes
131  static constexpr Type kNext = 0x00000100;
132 
133  // }}}
134 
135  Event() : addr(0), type(kNone) {}
136 
137  Event(Type type_, types::Addr addr_, const Iiid& iiid_)
138  : addr(addr_), type(type_), iiid(iiid_) {}
139 
140  operator std::string() const {
141  std::ostringstream oss;
142  oss << "[" << static_cast<std::string>(iiid) << "] ";
143 
144  std::ostringstream memtype;
145 
146  if (type == kNone) {
147  memtype << "None";
148  } else {
149  bool found_type = false;
150 
151  if (AllType(kRead)) {
152  memtype << "Read";
153  found_type = true;
154  }
155 
156  if (AllType(kWrite)) {
157  memtype << (found_type ? "|" : "") << "Write";
158  found_type = true;
159  }
160 
161  if (AllType(kAcquire)) {
162  memtype << (found_type ? "|" : "") << "Acquire";
163  found_type = true;
164  }
165 
166  if (AllType(kRelease)) {
167  memtype << (found_type ? "|" : "") << "Release";
168  found_type = true;
169  }
170 
171  if (AllType(kRegInAddr)) {
172  memtype << (found_type ? "|" : "") << "RegInAddr";
173  found_type = true;
174  }
175 
176  if (AllType(kRegInData)) {
177  memtype << (found_type ? "|" : "") << "RegInData";
178  found_type = true;
179  }
180 
181  if (AllType(kRegOut)) {
182  memtype << (found_type ? "|" : "") << "RegOut";
183  found_type = true;
184  }
185 
186  if (AllType(kBranch)) {
187  memtype << (found_type ? "|" : "") << "Branch";
188  // found_type = true;
189  }
190  }
191 
192  oss << std::setfill(' ') << std::setw(8) << memtype.str() << " @ "
193  << std::hex << addr;
194  return oss.str();
195  }
196 
197  bool operator==(const Event& rhs) const {
198  return type == rhs.type && addr == rhs.addr && iiid == rhs.iiid;
199  }
200 
201  bool operator!=(const Event& rhs) const {
202  return type != rhs.type || addr != rhs.addr || iiid != rhs.iiid;
203  }
204 
205  // This function in no way says anything about event ordering. Used for
206  // ordered map.
207  bool operator<(const Event& rhs) const { return iiid < rhs.iiid; }
208 
209  bool AllType(Type type_mask) const {
210  return sets::AllBitmask(type, type_mask);
211  }
212 
213  bool AnyType(Type type_mask) const {
214  return sets::AnyBitmask(type, type_mask);
215  }
216 
217  public:
219  Type type;
221 };
222 
226 
227 class Error : public std::logic_error {
228  public:
229 #if 0
230  // constructor inheritance not supported by gcc 4.7
231  using std::logic_error::logic_error;
232 #else
233  explicit Error(const std::string& what_arg) : std::logic_error(what_arg) {}
234 
235  explicit Error(const char* what_arg) : std::logic_error(what_arg) {}
236 #endif
237 };
238 
239 } // namespace memconsistency
240 } // namespace mc2lib
241 
242 #endif /* MEMCONSISTENCY_EVENTSETS_HPP_ */
243 
244 /* vim: set ts=2 sts=2 sw=2 et : */
Definition: eventsets.hpp:227
Iiid Prev() const
Definition: eventsets.hpp:93
bool AllType(Type type_mask) const
Definition: eventsets.hpp:209
bool AnyBitmask(T mask, T any)
Definition: sets.hpp:73
Iiid(types::Pid pid_, types::Poi poi_)
Definition: eventsets.hpp:64
bool AnyType(Type type_mask) const
Definition: eventsets.hpp:213
Types< true >::Poi Poi
Program order index type.
Definition: types.hpp:76
Event(Type type_, types::Addr addr_, const Iiid &iiid_)
Definition: eventsets.hpp:137
bool operator!=(const Event &rhs) const
Definition: eventsets.hpp:201
sets::Set< sets::Types< Event > > EventSet
Definition: eventsets.hpp:223
Iiid Next() const
Definition: eventsets.hpp:91
Error(const std::string &what_arg)
Definition: eventsets.hpp:233
Definition: eventsets.hpp:53
Definition: cats.hpp:47
bool operator==(const Iiid &rhs) const
Definition: eventsets.hpp:74
std::uint32_t Type
Definition: eventsets.hpp:111
Definition: eventsets.hpp:55
Iiid iiid
Definition: eventsets.hpp:220
Iiid()
Definition: eventsets.hpp:62
bool operator<(const Iiid &rhs) const
Definition: eventsets.hpp:82
bool AllBitmask(T mask, T all)
Definition: sets.hpp:60
Definition: eventsets.hpp:103
sets::RelationSeq< sets::Types< Event > > EventRelSeq
Definition: eventsets.hpp:225
bool operator==(const Event &rhs) const
Definition: eventsets.hpp:197
Iiid & operator++()
Definition: eventsets.hpp:86
Definition: sets.hpp:319
bool operator!=(const Iiid &rhs) const
Definition: eventsets.hpp:78
Abstracts over container library&#39;s set implementation.
Definition: sets.hpp:85
Iiid::Hash::result_type operator()(const Event &k) const
Definition: eventsets.hpp:106
Error(const char *what_arg)
Definition: eventsets.hpp:235
sets::Relation< sets::Types< Event > > EventRel
Definition: eventsets.hpp:224
bool operator<(const Event &rhs) const
Definition: eventsets.hpp:207
std::hash< types::Poi >::result_type result_type
Definition: eventsets.hpp:56
types::Addr addr
Definition: eventsets.hpp:218
Types< true >::Addr Addr
Address type.
Definition: types.hpp:66
Definition: eventsets.hpp:105
types::Pid pid
Definition: eventsets.hpp:99
Types< true >::Pid Pid
Processor/thread ID type.
Definition: types.hpp:71
Definition: sets.hpp:1310
Event()
Definition: eventsets.hpp:135
result_type operator()(const Iiid &k) const
Definition: eventsets.hpp:57
types::Poi poi
Definition: eventsets.hpp:100
Type type
Definition: eventsets.hpp:219