1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
//===- OmptAsserter.cpp - Asserter-related implementations ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Implements all asserter-related class methods, like: notifications, handling
/// of groups or determination of the testcase state.
///
//===----------------------------------------------------------------------===//
#include "OmptAsserter.h"
#include "Logging.h"
#include <algorithm>
using namespace omptest;
using namespace internal;
// Initialize static members
std::mutex OmptAsserter::StaticMemberAccessMutex;
std::weak_ptr<OmptEventGroupInterface>
OmptAsserter::EventGroupInterfaceInstance;
std::weak_ptr<logging::Logger> OmptAsserter::LoggingInstance;
OmptAsserter::OmptAsserter() {
// Protect static members access
std::lock_guard<std::mutex> Lock(StaticMemberAccessMutex);
// Upgrade OmptEventGroupInterface weak_ptr to shared_ptr
{
EventGroups = EventGroupInterfaceInstance.lock();
if (!EventGroups) {
// Coordinator doesn't exist or was previously destroyed, create a new
// one.
EventGroups = std::make_shared<OmptEventGroupInterface>();
// Store a weak reference to it
EventGroupInterfaceInstance = EventGroups;
}
// EventGroups is now a valid shared_ptr, either to a new or existing
// instance.
}
// Upgrade logging::Logger weak_ptr to shared_ptr
{
Log = LoggingInstance.lock();
if (!Log) {
// Coordinator doesn't exist or was previously destroyed, create a new
// one.
Log = std::make_shared<logging::Logger>();
// Store a weak reference to it
LoggingInstance = Log;
}
// Log is now a valid shared_ptr, either to a new or existing instance.
}
}
void OmptListener::setActive(bool Enabled) { Active = Enabled; }
bool OmptListener::isActive() { return Active; }
bool OmptListener::isSuppressedEventType(EventTy EvTy) {
return SuppressedEvents.find(EvTy) != SuppressedEvents.end();
}
void OmptListener::permitEvent(EventTy EvTy) { SuppressedEvents.erase(EvTy); }
void OmptListener::suppressEvent(EventTy EvTy) {
SuppressedEvents.insert(EvTy);
}
void OmptAsserter::insert(OmptAssertEvent &&AE) {
assert(false && "Base class 'insert' has undefined semantics.");
}
void OmptAsserter::notify(OmptAssertEvent &&AE) {
// Ignore notifications while inactive
if (!isActive() || isSuppressedEventType(AE.getEventType()))
return;
this->notifyImpl(std::move(AE));
}
AssertState OmptAsserter::checkState() { return State; }
bool OmptAsserter::verifyEventGroups(const OmptAssertEvent &ExpectedEvent,
const OmptAssertEvent &ObservedEvent) {
assert(ExpectedEvent.getEventType() == ObservedEvent.getEventType() &&
"Type mismatch: Expected != Observed event type");
assert(EventGroups && "Missing EventGroups interface");
// Ignore all events within "default" group
auto GroupName = ExpectedEvent.getEventGroup();
if (GroupName == "default")
return true;
// Get a pointer to the observed internal event
auto Event = ObservedEvent.getEvent();
switch (Event->Type) {
case EventTy::Target:
if (auto E = static_cast<const internal::Target *>(Event)) {
if (E->Endpoint == ompt_scope_begin) {
// Add new group since we entered a Target Region
EventGroups->addActiveEventGroup(GroupName,
AssertEventGroup{E->TargetId});
} else if (E->Endpoint == ompt_scope_end) {
// Deprecate group since we return from a Target Region
EventGroups->deprecateActiveEventGroup(GroupName);
}
return true;
}
return false;
case EventTy::TargetEmi:
if (auto E = static_cast<const internal::TargetEmi *>(Event)) {
if (E->Endpoint == ompt_scope_begin) {
// Add new group since we entered a Target Region
EventGroups->addActiveEventGroup(
GroupName, AssertEventGroup{E->TargetData->value});
} else if (E->Endpoint == ompt_scope_end) {
// Deprecate group since we return from a Target Region
EventGroups->deprecateActiveEventGroup(GroupName);
}
return true;
}
return false;
case EventTy::TargetDataOp:
if (auto E = static_cast<const internal::TargetDataOp *>(Event))
return EventGroups->checkActiveEventGroups(GroupName,
AssertEventGroup{E->TargetId});
return false;
case EventTy::TargetDataOpEmi:
if (auto E = static_cast<const internal::TargetDataOpEmi *>(Event))
return EventGroups->checkActiveEventGroups(
GroupName, AssertEventGroup{E->TargetData->value});
return false;
case EventTy::TargetSubmit:
if (auto E = static_cast<const internal::TargetSubmit *>(Event))
return EventGroups->checkActiveEventGroups(GroupName,
AssertEventGroup{E->TargetId});
return false;
case EventTy::TargetSubmitEmi:
if (auto E = static_cast<const internal::TargetSubmitEmi *>(Event))
return EventGroups->checkActiveEventGroups(
GroupName, AssertEventGroup{E->TargetData->value});
return false;
case EventTy::BufferRecord:
// BufferRecords are delivered asynchronously: also check deprecated groups.
if (auto E = static_cast<const internal::BufferRecord *>(Event))
return (EventGroups->checkActiveEventGroups(
GroupName, AssertEventGroup{E->Record.target_id}) ||
EventGroups->checkDeprecatedEventGroups(
GroupName, AssertEventGroup{E->Record.target_id}));
return false;
// Some event types do not need any handling
case EventTy::ThreadBegin:
case EventTy::ThreadEnd:
case EventTy::ParallelBegin:
case EventTy::ParallelEnd:
case EventTy::Work:
case EventTy::Dispatch:
case EventTy::TaskCreate:
case EventTy::Dependences:
case EventTy::TaskDependence:
case EventTy::TaskSchedule:
case EventTy::ImplicitTask:
case EventTy::Masked:
case EventTy::SyncRegion:
case EventTy::MutexAcquire:
case EventTy::Mutex:
case EventTy::NestLock:
case EventTy::Flush:
case EventTy::Cancel:
case EventTy::DeviceInitialize:
case EventTy::DeviceFinalize:
case EventTy::DeviceLoad:
case EventTy::DeviceUnload:
case EventTy::BufferRequest:
case EventTy::BufferComplete:
case EventTy::BufferRecordDeallocation:
return true;
// Some event types must not be encountered
case EventTy::None:
case EventTy::AssertionSyncPoint:
case EventTy::AssertionSuspend:
default:
Log->log("Observed invalid event type: " + Event->toString(),
logging::Level::Critical);
__builtin_unreachable();
}
return true;
}
void OmptAsserter::setOperationMode(AssertMode Mode) { OperationMode = Mode; }
void OmptSequencedAsserter::insert(OmptAssertEvent &&AE) {
std::lock_guard<std::mutex> Lock(AssertMutex);
Events.emplace_back(std::move(AE));
}
void OmptSequencedAsserter::notifyImpl(OmptAssertEvent &&AE) {
std::lock_guard<std::mutex> Lock(AssertMutex);
// Ignore notifications while inactive, or for suppressed events
if (Events.empty() || !isActive() || isSuppressedEventType(AE.getEventType()))
return;
++NumNotifications;
// Note: Order of these checks has semantic meaning.
// (1) Synchronization points should fail if there are remaining events,
// otherwise pass. (2) Regular notification while no further events are
// expected: fail. (3) Assertion suspension relies on a next expected event
// being available. (4) All other cases are considered 'regular' and match the
// next expected against the observed event. (5+6) Depending on the state /
// mode we signal failure if no other check has done already, or signaled pass
// by early-exit.
if (consumeSyncPoint(AE) || // Handle observed SyncPoint event
checkExcessNotify(AE) || // Check for remaining expected
consumeSuspend() || // Handle requested suspend
consumeRegularEvent(AE) || // Handle regular event
AssertionSuspended || // Ignore fail, if suspended
OperationMode == AssertMode::Relaxed) // Ignore fail, if Relaxed op-mode
return;
Log->logEventMismatch("[OmptSequencedAsserter] The events are not equal",
Events[NextEvent], AE);
State = AssertState::Fail;
}
bool OmptSequencedAsserter::consumeSyncPoint(
const omptest::OmptAssertEvent &AE) {
if (AE.getEventType() == EventTy::AssertionSyncPoint) {
auto NumRemainingEvents = getRemainingEventCount();
// Upon encountering a SyncPoint, all events should have been processed
if (NumRemainingEvents == 0)
return true;
Log->logEventMismatch(
"[OmptSequencedAsserter] Encountered SyncPoint while still awaiting " +
std::to_string(NumRemainingEvents) + " events. Asserted " +
std::to_string(NumSuccessfulAsserts) + "/" +
std::to_string(Events.size()) + " events successfully.",
AE);
State = AssertState::Fail;
return true;
}
// Nothing to process: continue.
return false;
}
bool OmptSequencedAsserter::checkExcessNotify(
const omptest::OmptAssertEvent &AE) {
if (NextEvent >= Events.size()) {
// If we are not expecting any more events and passively asserting: return
if (AssertionSuspended)
return true;
Log->logEventMismatch(
"[OmptSequencedAsserter] Too many events to check (" +
std::to_string(NumNotifications) + "). Asserted " +
std::to_string(NumSuccessfulAsserts) + "/" +
std::to_string(Events.size()) + " events successfully.",
AE);
State = AssertState::Fail;
return true;
}
// Remaining expected events present: continue.
return false;
}
bool OmptSequencedAsserter::consumeSuspend() {
// On AssertionSuspend -- enter 'passive' assertion.
// Since we may encounter multiple, successive AssertionSuspend events, loop
// until we hit the next non-AssertionSuspend event.
while (Events[NextEvent].getEventType() == EventTy::AssertionSuspend) {
AssertionSuspended = true;
// We just hit the very last event: indicate early exit.
if (++NextEvent >= Events.size())
return true;
}
// Continue with remaining notification logic.
return false;
}
bool OmptSequencedAsserter::consumeRegularEvent(
const omptest::OmptAssertEvent &AE) {
// If we are actively asserting, increment the event counter.
// Otherwise: If passively asserting, we will keep waiting for a match.
auto &E = Events[NextEvent];
if (E == AE && verifyEventGroups(E, AE)) {
if (E.getEventExpectedState() == ObserveState::Always) {
++NumSuccessfulAsserts;
} else if (E.getEventExpectedState() == ObserveState::Never) {
Log->logEventMismatch(
"[OmptSequencedAsserter] Encountered forbidden event", E, AE);
State = AssertState::Fail;
}
// Return to active assertion
if (AssertionSuspended)
AssertionSuspended = false;
// Match found, increment index and indicate early exit (success).
++NextEvent;
return true;
}
// Continue with remaining notification logic.
return false;
}
size_t OmptSequencedAsserter::getRemainingEventCount() {
return std::count_if(Events.begin(), Events.end(),
[](const omptest::OmptAssertEvent &E) {
return E.getEventExpectedState() ==
ObserveState::Always;
}) -
NumSuccessfulAsserts;
}
AssertState OmptSequencedAsserter::checkState() {
// This is called after the testcase executed.
// Once reached the number of successful notifications should be equal to the
// number of expected events. However, there may still be excluded as well as
// special asserter events remaining in the sequence.
for (size_t i = NextEvent; i < Events.size(); ++i) {
auto &E = Events[i];
if (E.getEventExpectedState() == ObserveState::Always) {
State = AssertState::Fail;
Log->logEventMismatch("[OmptSequencedAsserter] Expected event was not "
"encountered (Remaining events: " +
std::to_string(getRemainingEventCount()) + ")",
E);
break;
}
}
return State;
}
void OmptEventAsserter::insert(OmptAssertEvent &&AE) {
std::lock_guard<std::mutex> Lock(AssertMutex);
Events.emplace_back(std::move(AE));
}
void OmptEventAsserter::notifyImpl(OmptAssertEvent &&AE) {
std::lock_guard<std::mutex> Lock(AssertMutex);
if (Events.empty() || !isActive() || isSuppressedEventType(AE.getEventType()))
return;
if (NumEvents == 0)
NumEvents = Events.size();
++NumNotifications;
if (AE.getEventType() == EventTy::AssertionSyncPoint) {
auto NumRemainingEvents = getRemainingEventCount();
// Upon encountering a SyncPoint, all events should have been processed
if (NumRemainingEvents == 0)
return;
Log->logEventMismatch(
"[OmptEventAsserter] Encountered SyncPoint while still awaiting " +
std::to_string(NumRemainingEvents) + " events. Asserted " +
std::to_string(NumSuccessfulAsserts) + " events successfully.",
AE);
State = AssertState::Fail;
return;
}
for (size_t i = 0; i < Events.size(); ++i) {
auto &E = Events[i];
if (E == AE && verifyEventGroups(E, AE)) {
if (E.getEventExpectedState() == ObserveState::Always) {
Events.erase(Events.begin() + i);
++NumSuccessfulAsserts;
} else if (E.getEventExpectedState() == ObserveState::Never) {
Log->logEventMismatch("[OmptEventAsserter] Encountered forbidden event",
E, AE);
State = AssertState::Fail;
}
return;
}
}
if (OperationMode == AssertMode::Strict) {
Log->logEventMismatch("[OmptEventAsserter] Too many events to check (" +
std::to_string(NumNotifications) +
"). Asserted " +
std::to_string(NumSuccessfulAsserts) +
" events successfully. (Remaining events: " +
std::to_string(getRemainingEventCount()) + ")",
AE);
State = AssertState::Fail;
return;
}
}
size_t OmptEventAsserter::getRemainingEventCount() {
return std::count_if(
Events.begin(), Events.end(), [](const omptest::OmptAssertEvent &E) {
return E.getEventExpectedState() == ObserveState::Always;
});
}
AssertState OmptEventAsserter::checkState() {
// This is called after the testcase executed.
// Once reached no more expected events should be in the queue
for (const auto &E : Events) {
// Check if any of the remaining events were expected to be observed
if (E.getEventExpectedState() == ObserveState::Always) {
State = AssertState::Fail;
Log->logEventMismatch("[OmptEventAsserter] Expected event was not "
"encountered (Remaining events: " +
std::to_string(getRemainingEventCount()) + ")",
E);
break;
}
}
return State;
}
void OmptEventReporter::notify(OmptAssertEvent &&AE) {
if (!isActive() || isSuppressedEventType(AE.getEventType()))
return;
// Prepare notification, containing the newline to avoid stream interleaving.
auto Notification{AE.toString()};
Notification.push_back('\n');
OutStream << Notification;
}
bool OmptEventGroupInterface::addActiveEventGroup(
const std::string &GroupName, omptest::AssertEventGroup Group) {
std::lock_guard<std::mutex> Lock(GroupMutex);
auto EventGroup = ActiveEventGroups.find(GroupName);
if (EventGroup != ActiveEventGroups.end() &&
EventGroup->second.TargetRegion == Group.TargetRegion)
return false;
ActiveEventGroups.emplace(GroupName, Group);
return true;
}
bool OmptEventGroupInterface::deprecateActiveEventGroup(
const std::string &GroupName) {
std::lock_guard<std::mutex> Lock(GroupMutex);
auto EventGroup = ActiveEventGroups.find(GroupName);
auto DeprecatedEventGroup = DeprecatedEventGroups.find(GroupName);
if (EventGroup == ActiveEventGroups.end() &&
DeprecatedEventGroup != DeprecatedEventGroups.end())
return false;
DeprecatedEventGroups.emplace(GroupName, EventGroup->second);
ActiveEventGroups.erase(GroupName);
return true;
}
bool OmptEventGroupInterface::checkActiveEventGroups(
const std::string &GroupName, omptest::AssertEventGroup Group) {
std::lock_guard<std::mutex> Lock(GroupMutex);
auto EventGroup = ActiveEventGroups.find(GroupName);
return (EventGroup != ActiveEventGroups.end() &&
EventGroup->second.TargetRegion == Group.TargetRegion);
}
bool OmptEventGroupInterface::checkDeprecatedEventGroups(
const std::string &GroupName, omptest::AssertEventGroup Group) {
std::lock_guard<std::mutex> Lock(GroupMutex);
auto EventGroup = DeprecatedEventGroups.find(GroupName);
return (EventGroup != DeprecatedEventGroups.end() &&
EventGroup->second.TargetRegion == Group.TargetRegion);
}
|