LLVM OpenMP 22.0.0git
OmptAsserter.h
Go to the documentation of this file.
1//===- OmptAsserter.h - Asserter-related classes, enums, etc. ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Contains all asserter-related class declarations and important enums.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef OPENMP_TOOLS_OMPTEST_INCLUDE_OMPTASSERTER_H
15#define OPENMP_TOOLS_OMPTEST_INCLUDE_OMPTASSERTER_H
16
17#include "Logging.h"
18#include "OmptAssertEvent.h"
19
20#include <cassert>
21#include <iostream>
22#include <map>
23#include <mutex>
24#include <set>
25#include <vector>
26
27namespace omptest {
28
29// Forward declaration.
30class OmptEventGroupInterface;
31
32enum class AssertMode { Strict, Relaxed };
33enum class AssertState { Pass, Fail };
34
35/// General base class for the subscriber/notification pattern in
36/// OmptCallbackHandler. Derived classes need to implement the notify method.
38public:
39 virtual ~OmptListener() = default;
40
41 /// Called for each registered OMPT event of the OmptCallbackHandler
42 virtual void notify(omptest::OmptAssertEvent &&AE) = 0;
43
44 /// Control whether this asserter should be considered 'active'.
45 void setActive(bool Enabled);
46
47 /// Check if this asserter is considered 'active'.
48 bool isActive();
49
50 /// Check if the given event type is from the set of suppressed event types.
52
53 /// Remove the given event type to the set of suppressed events.
55
56 /// Add the given event type to the set of suppressed events.
58
59private:
60 bool Active{true};
61
62 // Add event types to the set of suppressed events by default.
63 std::set<omptest::internal::EventTy> SuppressedEvents{
82};
83
84/// Base class for asserting on OMPT events
85class OmptAsserter : public OmptListener {
86public:
88 virtual ~OmptAsserter() = default;
89
90 /// Add an event to the asserter's internal data structure.
91 virtual void insert(omptest::OmptAssertEvent &&AE);
92
93 /// Called from the CallbackHandler with a corresponding AssertEvent to which
94 /// callback was handled.
95 void notify(omptest::OmptAssertEvent &&AE) override;
96
97 /// Implemented in subclasses to implement what should actually be done with
98 /// the notification.
99 virtual void notifyImpl(omptest::OmptAssertEvent &&AE) = 0;
100
101 /// Get the number of currently remaining events, with: ObserveState::Always.
102 virtual size_t getRemainingEventCount() = 0;
103
104 /// Get the total number of received, effective notifications.
106
107 /// Get the total number of successful assertion checks.
109
110 /// Get the asserter's current operationmode: e.g.: Strict or Relaxed.
112
113 /// Return the asserter's current state.
115
116 /// Determine and return the asserter's state.
118
119 /// Accessor for the event group interface.
120 std::shared_ptr<OmptEventGroupInterface> getEventGroups() const {
121 return EventGroups;
122 }
123
124 /// Accessor for the event group interface.
125 std::shared_ptr<logging::Logger> getLog() const { return Log; }
126
127 /// Check the observed events' group association. If the event indicates the
128 /// begin/end of an OpenMP target region, we will create/deprecate the
129 /// expected event's group. Return true if the expected event group exists
130 /// (and is active), otherwise: false. Note: BufferRecords may also match with
131 /// deprecated groups as they may be delivered asynchronously.
132 bool verifyEventGroups(const omptest::OmptAssertEvent &ExpectedEvent,
133 const omptest::OmptAssertEvent &ObservedEvent);
134
135 /// Set the asserter's mode of operation w.r.t. assertion.
136 void setOperationMode(AssertMode Mode);
137
138protected:
139 /// The asserter's current state.
141
142 /// Mutex to avoid data races w.r.t. event notifications and/or insertions.
143 std::mutex AssertMutex;
144
145 /// Pointer to the OmptEventGroupInterface.
146 std::shared_ptr<OmptEventGroupInterface> EventGroups{nullptr};
147
148 /// Pointer to the logging instance.
149 std::shared_ptr<logging::Logger> Log{nullptr};
150
151 /// Operation mode during assertion / notification.
153
154 /// The total number of effective notifications. For example, if specific
155 /// notifications are to be ignored, they will not count towards this total.
157
158 /// The number of successful assertion checks.
160
161private:
162 /// Mutex for creating/accessing the singleton members
163 static std::mutex StaticMemberAccessMutex;
164
165 /// Static member to manage the singleton event group interface instance
166 static std::weak_ptr<OmptEventGroupInterface> EventGroupInterfaceInstance;
167
168 /// Static member to manage the singleton logging instance
169 static std::weak_ptr<logging::Logger> LoggingInstance;
170};
171
172/// Class that can assert in a sequenced fashion, i.e., events have to occur in
173/// the order they were registered
175public:
177
178 /// Add the event to the in-sequence set of events that the asserter should
179 /// check for.
180 void insert(omptest::OmptAssertEvent &&AE) override;
181
182 /// Implements the asserter's actual logic
183 virtual void notifyImpl(omptest::OmptAssertEvent &&AE) override;
184
185 size_t getRemainingEventCount() override;
186
188
190
191protected:
192 /// Notification helper function, implementing SyncPoint logic. Returns true
193 /// in case of consumed event, indicating early exit of notification.
195
196 /// Notification helper function, implementing excess event notification
197 /// logic. Returns true when no more events were expected, indicating early
198 /// exit of notification.
200
201 /// Notification helper function, implementing Suspend logic. Returns true
202 /// in case of consumed event, indicating early exit of notification.
203 bool consumeSuspend();
204
205 /// Notification helper function, implementing regular event notification
206 /// logic. Returns true when a matching event was encountered, indicating
207 /// early exit of notification.
209
210public:
211 /// Index of the next, expected event.
212 size_t NextEvent{0};
213 std::vector<omptest::OmptAssertEvent> Events{};
214};
215
216/// Class that asserts with set semantics, i.e., unordered
219
220 /// Add the event to the set of events that the asserter should check for.
221 void insert(omptest::OmptAssertEvent &&AE) override;
222
223 /// Implements the asserter's logic
224 virtual void notifyImpl(omptest::OmptAssertEvent &&AE) override;
225
226 size_t getRemainingEventCount() override;
227
229
230 size_t NumEvents{0};
231
232 /// For now use vector (but do set semantics)
233 // TODO std::unordered_set?
234 std::vector<omptest::OmptAssertEvent> Events{};
235};
236
237/// Class that reports the occurred events
239public:
240 OmptEventReporter(std::ostream &OutStream = std::cout)
241 : OutStream(OutStream) {}
242
243 /// Called from the CallbackHandler with a corresponding AssertEvent to which
244 /// callback was handled.
245 void notify(omptest::OmptAssertEvent &&AE) override;
246
247private:
248 std::ostream &OutStream;
249};
250
251/// This class provides the members and methods to manage event groups and
252/// SyncPoints in conjunction with asserters. Most importantly it maintains a
253/// coherent view of active and past events or SyncPoints.
255public:
258
259 /// Non-copyable and non-movable
264
265 /// Add given group to the set of active event groups. Effectively connecting
266 /// the given groupname (expected) with a target region id (observed).
267 bool addActiveEventGroup(const std::string &GroupName,
269
270 /// Move given group from the set of active event groups to the set of
271 /// previously active event groups.
272 bool deprecateActiveEventGroup(const std::string &GroupName);
273
274 /// Check if given group is currently part of the active event groups.
275 bool checkActiveEventGroups(const std::string &GroupName,
277
278 /// Check if given group is currently part of the deprecated event groups.
279 bool checkDeprecatedEventGroups(const std::string &GroupName,
281
282private:
283 mutable std::mutex GroupMutex;
284 std::map<std::string, omptest::AssertEventGroup> ActiveEventGroups{};
285 std::map<std::string, omptest::AssertEventGroup> DeprecatedEventGroups{};
286 std::set<std::string> EncounteredSyncPoints{};
287};
288
289} // namespace omptest
290
291#endif
Provides ompTest-tailored logging, with log-levels and formatting/coloring.
Contains assertion event constructors, for generally all observable events.
Base class for asserting on OMPT events.
Definition: OmptAsserter.h:85
virtual omptest::AssertState checkState()
Determine and return the asserter's state.
std::shared_ptr< OmptEventGroupInterface > EventGroups
Pointer to the OmptEventGroupInterface.
Definition: OmptAsserter.h:146
AssertMode OperationMode
Operation mode during assertion / notification.
Definition: OmptAsserter.h:152
std::mutex AssertMutex
Mutex to avoid data races w.r.t. event notifications and/or insertions.
Definition: OmptAsserter.h:143
AssertMode getOperationMode()
Get the asserter's current operationmode: e.g.: Strict or Relaxed.
Definition: OmptAsserter.h:111
int getNotificationCount()
Get the total number of received, effective notifications.
Definition: OmptAsserter.h:105
omptest::AssertState getState()
Return the asserter's current state.
Definition: OmptAsserter.h:114
int NumNotifications
The total number of effective notifications.
Definition: OmptAsserter.h:156
virtual ~OmptAsserter()=default
void notify(omptest::OmptAssertEvent &&AE) override
Called from the CallbackHandler with a corresponding AssertEvent to which callback was handled.
virtual void insert(omptest::OmptAssertEvent &&AE)
Add an event to the asserter's internal data structure.
virtual void notifyImpl(omptest::OmptAssertEvent &&AE)=0
Implemented in subclasses to implement what should actually be done with the notification.
int NumSuccessfulAsserts
The number of successful assertion checks.
Definition: OmptAsserter.h:159
std::shared_ptr< OmptEventGroupInterface > getEventGroups() const
Accessor for the event group interface.
Definition: OmptAsserter.h:120
virtual size_t getRemainingEventCount()=0
Get the number of currently remaining events, with: ObserveState::Always.
void setOperationMode(AssertMode Mode)
Set the asserter's mode of operation w.r.t. assertion.
int getSuccessfulAssertionCount()
Get the total number of successful assertion checks.
Definition: OmptAsserter.h:108
bool verifyEventGroups(const omptest::OmptAssertEvent &ExpectedEvent, const omptest::OmptAssertEvent &ObservedEvent)
Check the observed events' group association.
std::shared_ptr< logging::Logger > getLog() const
Accessor for the event group interface.
Definition: OmptAsserter.h:125
std::shared_ptr< logging::Logger > Log
Pointer to the logging instance.
Definition: OmptAsserter.h:149
omptest::AssertState State
The asserter's current state.
Definition: OmptAsserter.h:140
This class provides the members and methods to manage event groups and SyncPoints in conjunction with...
Definition: OmptAsserter.h:254
bool checkActiveEventGroups(const std::string &GroupName, omptest::AssertEventGroup Group)
Check if given group is currently part of the active event groups.
OmptEventGroupInterface & operator=(const OmptEventGroupInterface &)=delete
OmptEventGroupInterface(const OmptEventGroupInterface &)=delete
Non-copyable and non-movable.
bool checkDeprecatedEventGroups(const std::string &GroupName, omptest::AssertEventGroup Group)
Check if given group is currently part of the deprecated event groups.
OmptEventGroupInterface & operator=(OmptEventGroupInterface &&)=delete
bool addActiveEventGroup(const std::string &GroupName, omptest::AssertEventGroup Group)
Add given group to the set of active event groups.
OmptEventGroupInterface(OmptEventGroupInterface &&)=delete
bool deprecateActiveEventGroup(const std::string &GroupName)
Move given group from the set of active event groups to the set of previously active event groups.
Class that reports the occurred events.
Definition: OmptAsserter.h:238
OmptEventReporter(std::ostream &OutStream=std::cout)
Definition: OmptAsserter.h:240
void notify(omptest::OmptAssertEvent &&AE) override
Called from the CallbackHandler with a corresponding AssertEvent to which callback was handled.
General base class for the subscriber/notification pattern in OmptCallbackHandler.
Definition: OmptAsserter.h:37
virtual ~OmptListener()=default
void suppressEvent(omptest::internal::EventTy EvTy)
Add the given event type to the set of suppressed events.
bool isSuppressedEventType(omptest::internal::EventTy EvTy)
Check if the given event type is from the set of suppressed event types.
void permitEvent(omptest::internal::EventTy EvTy)
Remove the given event type to the set of suppressed events.
virtual void notify(omptest::OmptAssertEvent &&AE)=0
Called for each registered OMPT event of the OmptCallbackHandler.
void setActive(bool Enabled)
Control whether this asserter should be considered 'active'.
bool isActive()
Check if this asserter is considered 'active'.
Class that can assert in a sequenced fashion, i.e., events have to occur in the order they were regis...
Definition: OmptAsserter.h:174
size_t getRemainingEventCount() override
Get the number of currently remaining events, with: ObserveState::Always.
bool consumeSuspend()
Notification helper function, implementing Suspend logic.
bool consumeRegularEvent(const omptest::OmptAssertEvent &AE)
Notification helper function, implementing regular event notification logic.
void insert(omptest::OmptAssertEvent &&AE) override
Add the event to the in-sequence set of events that the asserter should check for.
bool consumeSyncPoint(const omptest::OmptAssertEvent &AE)
Notification helper function, implementing SyncPoint logic.
virtual void notifyImpl(omptest::OmptAssertEvent &&AE) override
Implements the asserter's actual logic.
omptest::AssertState checkState() override
Determine and return the asserter's state.
size_t NextEvent
Index of the next, expected event.
Definition: OmptAsserter.h:212
bool checkExcessNotify(const omptest::OmptAssertEvent &AE)
Notification helper function, implementing excess event notification logic.
std::vector< omptest::OmptAssertEvent > Events
Definition: OmptAsserter.h:213
EventTy
Enum values are used for comparison of observed and asserted events List is based on OpenMP 5....
POD type, which holds the target region id, corresponding to an event group.
Assertion event struct, provides statically callable CTORs.
Class that asserts with set semantics, i.e., unordered.
Definition: OmptAsserter.h:217
virtual void notifyImpl(omptest::OmptAssertEvent &&AE) override
Implements the asserter's logic.
size_t getRemainingEventCount() override
Get the number of currently remaining events, with: ObserveState::Always.
void insert(omptest::OmptAssertEvent &&AE) override
Add the event to the set of events that the asserter should check for.
omptest::AssertState checkState() override
Determine and return the asserter's state.
std::vector< omptest::OmptAssertEvent > Events
For now use vector (but do set semantics)
Definition: OmptAsserter.h:234