1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares an interface for recording events, which can be played by a
16 : // story teller in an arbitrary order, and during which stats can be
17 : // collected for user analysis.
18 :
19 : #include "syzygy/bard/event.h"
20 :
21 : #include "syzygy/bard/events/heap_alloc_event.h"
22 : #include "syzygy/bard/events/heap_create_event.h"
23 : #include "syzygy/bard/events/heap_destroy_event.h"
24 : #include "syzygy/bard/events/heap_free_event.h"
25 : #include "syzygy/bard/events/heap_realloc_event.h"
26 : #include "syzygy/bard/events/heap_set_information_event.h"
27 : #include "syzygy/bard/events/heap_size_event.h"
28 : #include "syzygy/bard/events/linked_event.h"
29 :
30 : namespace bard {
31 :
32 : // This ensures that Save and Load are kept up to date with the enumeration.
33 : static_assert(static_cast<int>(EventInterface::kHeapSizeEvent + 1) ==
34 : static_cast<int>(EventInterface::kMaxEventType),
35 : "all event types must be implemented");
36 :
37 : // static
38 : bool EventInterface::Save(const EventInterface* event,
39 E : core::OutArchive* out_archive) {
40 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
41 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
42 :
43 : static_assert((kMaxEventType & 0xFFFF) == kMaxEventType,
44 : "event type counts must fit in 16-bits");
45 E : if (!out_archive->Save(static_cast<uint16_t>(event->type())))
46 i : return false;
47 :
48 E : switch (event->type()) {
49 : case kLinkedEvent:
50 E : return events::LinkedEvent::Save(event, out_archive);
51 : case kHeapAllocEvent:
52 E : return events::HeapAllocEvent::Save(event, out_archive);
53 : case kHeapCreateEvent:
54 E : return events::HeapCreateEvent::Save(event, out_archive);
55 : case kHeapDestroyEvent:
56 E : return events::HeapDestroyEvent::Save(event, out_archive);
57 : case kHeapFreeEvent:
58 E : return events::HeapFreeEvent::Save(event, out_archive);
59 : case kHeapReAllocEvent:
60 E : return events::HeapReAllocEvent::Save(event, out_archive);
61 : case kHeapSetInformationEvent:
62 E : return events::HeapSetInformationEvent::Save(event, out_archive);
63 : case kHeapSizeEvent:
64 E : return events::HeapSizeEvent::Save(event, out_archive);
65 : case kMaxEventType:
66 : break;
67 : // No default case is specified so that the compiler will complain if a
68 : // new type is defined by not handled here.
69 : }
70 :
71 i : NOTREACHED();
72 i : return false;
73 E : }
74 :
75 : // static
76 : std::unique_ptr<EventInterface> EventInterface::Load(
77 E : core::InArchive* in_archive) {
78 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
79 :
80 E : uint16_t type = 0;
81 E : if (!in_archive->Load(&type))
82 i : return false;
83 :
84 E : switch (static_cast<EventType>(type)) {
85 : case kLinkedEvent:
86 E : return events::LinkedEvent::Load(in_archive);
87 : case kHeapAllocEvent:
88 E : return events::HeapAllocEvent::Load(in_archive);
89 : case kHeapCreateEvent:
90 E : return events::HeapCreateEvent::Load(in_archive);
91 : case kHeapDestroyEvent:
92 E : return events::HeapDestroyEvent::Load(in_archive);
93 : case kHeapFreeEvent:
94 E : return events::HeapFreeEvent::Load(in_archive);
95 : case kHeapReAllocEvent:
96 E : return events::HeapReAllocEvent::Load(in_archive);
97 : case kHeapSetInformationEvent:
98 E : return events::HeapSetInformationEvent::Load(in_archive);
99 : case kHeapSizeEvent:
100 E : return events::HeapSizeEvent::Load(in_archive);
101 : case kMaxEventType:
102 : break;
103 : // No default case is specified so that the compiler will complain if a
104 : // new type is defined by not handled here.
105 : }
106 :
107 i : NOTREACHED();
108 i : return nullptr;
109 E : }
110 :
111 : } // namespace bard
|