forked from google/pprof-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode-map.cc
More file actions
126 lines (103 loc) · 3.59 KB
/
code-map.cc
File metadata and controls
126 lines (103 loc) · 3.59 KB
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
/*
* Copyright 2023 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unordered_map>
#include <node.h>
#include "code-map.hh"
#include "profilers/cpu.hh"
namespace dd {
static std::unordered_map<v8::Isolate*, std::shared_ptr<CodeMap>> code_maps_;
CodeMap::CodeMap(v8::Isolate* isolate, CodeEntries entries)
: CodeEventHandler(isolate), code_entries_(entries), isolate_(isolate) {}
CodeMap::~CodeMap() {
Disable();
}
std::shared_ptr<CodeMap> CodeMap::For(v8::Isolate* isolate) {
auto maybe = code_maps_.find(isolate);
if (maybe != code_maps_.end()) {
return maybe->second;
}
code_maps_[isolate] = std::make_shared<CodeMap>(isolate);
return code_maps_[isolate];
}
CodeEntries CodeMap::Entries() {
return code_entries_;
}
void CodeMap::Enable() {
if (++refs == 1) {
CodeEventHandler::Enable();
isolate_->SetJitCodeEventHandler(v8::kJitCodeEventDefault,
StaticHandleJitEvent);
}
}
void CodeMap::Disable() {
if (--refs == 0) {
CodeEventHandler::Disable();
isolate_->SetJitCodeEventHandler(v8::kJitCodeEventDefault, nullptr);
code_entries_.clear();
}
}
// TODO: unsure of ordering but might need bi-directional merging for script id
void CodeMap::HandleJitEvent(const v8::JitCodeEvent* event) {
if (event->type == v8::JitCodeEvent::CODE_REMOVED) {
Remove(reinterpret_cast<uintptr_t>(event->code_start));
return;
}
CodeEntries::iterator it =
code_entries_.find(reinterpret_cast<uintptr_t>(event->code_start));
if (it != code_entries_.end() && !event->script.IsEmpty()) {
it->second->SetScriptId(event->script->GetId());
}
}
void CodeMap::StaticHandleJitEvent(const v8::JitCodeEvent* event) {
auto code_map = CodeMap::For(event->isolate);
code_map->HandleJitEvent(event);
}
// TODO: Figure out if additional checks are needed to cleanup expired regions.
// If size of previous is greater than offset of new position, the old record
// must be invalid, clean it up.
void CodeMap::Handle(v8::CodeEvent* code_event) {
#if NODE_MODULE_VERSION > 79
if (code_event->GetCodeType() == v8::CodeEventType::kRelocationType) {
CodeEntries::iterator it =
code_entries_.find(code_event->GetPreviousCodeStartAddress());
if (it != code_entries_.end()) {
code_entries_.erase(it);
}
}
#endif
Add(code_event->GetCodeStartAddress(),
std::make_shared<CodeEventRecord>(isolate_, code_event));
}
void CodeMap::Add(uintptr_t address, std::shared_ptr<CodeEventRecord> record) {
code_entries_.insert(std::make_pair(address, std::move(record)));
}
void CodeMap::Remove(uintptr_t address) {
code_entries_.erase(address);
}
void CodeMap::Clear() {
code_entries_.clear();
}
std::shared_ptr<CodeEventRecord> CodeMap::Lookup(uintptr_t address) {
CodeEntries::iterator it = code_entries_.upper_bound(address);
if (it == code_entries_.begin()) return nullptr;
--it;
uintptr_t start_address = it->first;
std::shared_ptr<CodeEventRecord> entry = it->second;
uintptr_t code_end = start_address + entry->size;
if (address >= code_end) return nullptr;
return entry;
}
}; // namespace dd