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 : #include "syzygy/bard/events/heap_create_event.h"
16 :
17 : #include "syzygy/bard/backdrops/heap_backdrop.h"
18 : #include "syzygy/trace/common/clock.h"
19 :
20 : namespace bard {
21 : namespace events {
22 :
23 : HeapCreateEvent::HeapCreateEvent(DWORD options,
24 : SIZE_T initial_size,
25 : SIZE_T maximum_size,
26 : HANDLE trace_heap)
27 : : options_(options),
28 : initial_size_(initial_size),
29 : maximum_size_(maximum_size),
30 E : trace_heap_(trace_heap) {
31 E : }
32 :
33 : bool HeapCreateEvent::Save(const EventInterface* const event,
34 E : core::OutArchive* out_archive) {
35 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
36 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
37 :
38 : const HeapCreateEvent* derived_event =
39 E : reinterpret_cast<const HeapCreateEvent*>(event);
40 :
41 : return out_archive->Save(derived_event->options_) &&
42 : out_archive->Save(derived_event->initial_size_) &&
43 : out_archive->Save(derived_event->maximum_size_) &&
44 : out_archive->Save(
45 E : reinterpret_cast<uintptr_t>(derived_event->trace_heap_));
46 E : }
47 :
48 : scoped_ptr<HeapCreateEvent> HeapCreateEvent::Load(
49 E : core::InArchive* in_archive) {
50 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
51 :
52 : DWORD options;
53 : SIZE_T initial_size;
54 : SIZE_T maximum_size;
55 : uintptr_t trace_heap;
56 : if (in_archive->Load(&options) &&
57 : in_archive->Load(&initial_size) &&
58 : in_archive->Load(&maximum_size) &&
59 E : in_archive->Load(&trace_heap)) {
60 : return scoped_ptr<HeapCreateEvent>(
61 : new HeapCreateEvent(options,
62 : initial_size,
63 : maximum_size,
64 E : reinterpret_cast<HANDLE>(trace_heap)));
65 : }
66 i : return nullptr;
67 E : }
68 :
69 E : bool HeapCreateEvent::Play(void* backdrop) {
70 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
71 :
72 : using bard::backdrops::HeapBackdrop;
73 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
74 :
75 E : uint64_t t0 = ::trace::common::GetTsc();
76 : HANDLE live_heap =
77 E : heap_backdrop->HeapCreate(options_, initial_size_, maximum_size_);
78 E : uint64_t t1 = ::trace::common::GetTsc();
79 :
80 E : if (!live_heap && trace_heap_) {
81 E : LOG(ERROR) << "HeapCreate failed to create a new heap.";
82 E : return false;
83 : }
84 :
85 E : if (live_heap) {
86 E : if (!trace_heap_) {
87 : // No need to keep this heap.
88 i : heap_backdrop->HeapDestroy(live_heap);
89 E : } else if (!heap_backdrop->heap_map().AddMapping(trace_heap_, live_heap)) {
90 i : return false;
91 : }
92 : }
93 :
94 E : heap_backdrop->UpdateStats(type(), t1 - t0);
95 :
96 E : return true;
97 E : }
98 :
99 E : bool HeapCreateEvent::Equals(const EventInterface* rhs) const {
100 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
101 :
102 E : if (rhs->type() != kHeapCreateEvent)
103 i : return false;
104 :
105 E : const auto e = reinterpret_cast<const HeapCreateEvent*>(rhs);
106 : if (options_ != e->options_ || initial_size_ != e->initial_size_ ||
107 E : maximum_size_ != e->maximum_size_ || trace_heap_ != e->trace_heap_) {
108 E : return false;
109 : }
110 :
111 E : return true;
112 E : }
113 :
114 : } // namespace events
115 : } // namespace bard
|