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_size_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 : HeapSizeEvent::HeapSizeEvent(HANDLE trace_heap,
24 : DWORD flags,
25 : LPCVOID trace_alloc,
26 : SIZE_T trace_size)
27 : : trace_heap_(trace_heap),
28 : flags_(flags),
29 : trace_alloc_(trace_alloc),
30 E : trace_size_(trace_size) {
31 E : }
32 :
33 : bool HeapSizeEvent::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 HeapSizeEvent* derived_event =
39 E : reinterpret_cast<const HeapSizeEvent*>(event);
40 :
41 : return out_archive->Save(
42 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
43 : out_archive->Save(derived_event->flags_) &&
44 : out_archive->Save(
45 : reinterpret_cast<uintptr_t>(derived_event->trace_alloc_)) &&
46 E : out_archive->Save(derived_event->trace_size_);
47 E : }
48 :
49 : scoped_ptr<HeapSizeEvent> HeapSizeEvent::Load(
50 E : core::InArchive* in_archive) {
51 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
52 :
53 : uintptr_t trace_heap;
54 : DWORD flags;
55 : uintptr_t trace_alloc;
56 : SIZE_T trace_size;
57 : if (in_archive->Load(&trace_heap) &&
58 : in_archive->Load(&flags) &&
59 : in_archive->Load(&trace_alloc) &&
60 E : in_archive->Load(&trace_size)) {
61 : return scoped_ptr<HeapSizeEvent>(
62 : new HeapSizeEvent(reinterpret_cast<HANDLE>(trace_heap),
63 : flags,
64 : reinterpret_cast<LPVOID>(trace_alloc),
65 E : trace_size));
66 : }
67 i : return nullptr;
68 E : }
69 :
70 E : bool HeapSizeEvent::Play(void* backdrop) {
71 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
72 :
73 : using bard::backdrops::HeapBackdrop;
74 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
75 :
76 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
77 E : LPVOID live_alloc = nullptr;
78 :
79 : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap) ||
80 : !heap_backdrop->alloc_map().GetLiveFromTrace(
81 E : const_cast<LPVOID>(trace_alloc_), &live_alloc)) {
82 i : return false;
83 : }
84 :
85 E : uint64_t t0 = ::trace::common::GetTsc();
86 E : SIZE_T live_size = heap_backdrop->HeapSize(live_heap, flags_, live_alloc);
87 E : uint64_t t1 = ::trace::common::GetTsc();
88 :
89 E : if (live_size != trace_size_) {
90 E : LOG(ERROR) << "HeapSize returned an unexpected allocation size.";
91 E : return false;
92 : }
93 :
94 E : heap_backdrop->UpdateStats(type(), t1 - t0);
95 :
96 E : return true;
97 E : }
98 :
99 E : bool HeapSizeEvent::Equals(const EventInterface* rhs) const {
100 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
101 :
102 E : if (rhs->type() != kHeapSizeEvent)
103 i : return false;
104 :
105 E : const auto e = reinterpret_cast<const HeapSizeEvent*>(rhs);
106 : if (trace_heap_ != e->trace_heap_ || flags_ != e->flags_ ||
107 E : trace_alloc_ != e->trace_alloc_ || trace_size_ != e->trace_size_) {
108 E : return false;
109 : }
110 :
111 E : return true;
112 E : }
113 :
114 : } // namespace events
115 : } // namespace bard
|