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>(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 E : scoped_ptr<EventInterface> EventInterface::Load(core::InArchive* in_archive) {
77 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
78 :
79 E : uint16 type = 0;
80 E : if (!in_archive->Load(&type))
81 i : return false;
82 :
83 E : switch (static_cast<EventType>(type)) {
84 : case kLinkedEvent:
85 E : return events::LinkedEvent::Load(in_archive);
86 : case kHeapAllocEvent:
87 E : return events::HeapAllocEvent::Load(in_archive);
88 : case kHeapCreateEvent:
89 E : return events::HeapCreateEvent::Load(in_archive);
90 : case kHeapDestroyEvent:
91 E : return events::HeapDestroyEvent::Load(in_archive);
92 : case kHeapFreeEvent:
93 E : return events::HeapFreeEvent::Load(in_archive);
94 : case kHeapReAllocEvent:
95 E : return events::HeapReAllocEvent::Load(in_archive);
96 : case kHeapSetInformationEvent:
97 E : return events::HeapSetInformationEvent::Load(in_archive);
98 : case kHeapSizeEvent:
99 E : return events::HeapSizeEvent::Load(in_archive);
100 : case kMaxEventType:
101 : break;
102 : // No default case is specified so that the compiler will complain if a
103 : // new type is defined by not handled here.
104 : }
105 :
106 i : NOTREACHED();
107 i : return nullptr;
108 E : }
109 :
110 : } // namespace bard
|