|
| 1 | +#include "node_internals.h" |
| 2 | +#include "env.h" |
| 3 | + |
| 4 | +using v8::Array; |
| 5 | +using v8::Boolean; |
| 6 | +using v8::Context; |
| 7 | +using v8::EmbedderGraph; |
| 8 | +using v8::EscapableHandleScope; |
| 9 | +using v8::FunctionCallbackInfo; |
| 10 | +using v8::HandleScope; |
| 11 | +using v8::HeapSnapshot; |
| 12 | +using v8::Isolate; |
| 13 | +using v8::JSON; |
| 14 | +using v8::Local; |
| 15 | +using v8::MaybeLocal; |
| 16 | +using v8::Number; |
| 17 | +using v8::Object; |
| 18 | +using v8::String; |
| 19 | +using v8::Value; |
| 20 | + |
| 21 | +namespace node { |
| 22 | +namespace heap { |
| 23 | + |
| 24 | +class JSGraphJSNode : public EmbedderGraph::Node { |
| 25 | + public: |
| 26 | + const char* Name() override { return "<JS Node>"; } |
| 27 | + size_t SizeInBytes() override { return 0; } |
| 28 | + bool IsEmbedderNode() override { return false; } |
| 29 | + Local<Value> JSValue() { return StrongPersistentToLocal(persistent_); } |
| 30 | + |
| 31 | + int IdentityHash() { |
| 32 | + Local<Value> v = JSValue(); |
| 33 | + if (v->IsObject()) return v.As<Object>()->GetIdentityHash(); |
| 34 | + if (v->IsName()) return v.As<v8::Name>()->GetIdentityHash(); |
| 35 | + if (v->IsInt32()) return v.As<v8::Int32>()->Value(); |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + JSGraphJSNode(Isolate* isolate, Local<Value> val) |
| 40 | + : persistent_(isolate, val) { |
| 41 | + CHECK(!val.IsEmpty()); |
| 42 | + } |
| 43 | + |
| 44 | + struct Hash { |
| 45 | + inline size_t operator()(JSGraphJSNode* n) const { |
| 46 | + return n->IdentityHash(); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + struct Equal { |
| 51 | + inline bool operator()(JSGraphJSNode* a, JSGraphJSNode* b) const { |
| 52 | + return a->JSValue()->SameValue(b->JSValue()); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + private: |
| 57 | + Persistent<Value> persistent_; |
| 58 | +}; |
| 59 | + |
| 60 | +class JSGraph : public EmbedderGraph { |
| 61 | + public: |
| 62 | + explicit JSGraph(Isolate* isolate) : isolate_(isolate) {} |
| 63 | + |
| 64 | + Node* V8Node(const Local<Value>& value) override { |
| 65 | + std::unique_ptr<JSGraphJSNode> n { new JSGraphJSNode(isolate_, value) }; |
| 66 | + auto it = engine_nodes_.find(n.get()); |
| 67 | + if (it != engine_nodes_.end()) |
| 68 | + return *it; |
| 69 | + engine_nodes_.insert(n.get()); |
| 70 | + return AddNode(std::unique_ptr<Node>(n.release())); |
| 71 | + } |
| 72 | + |
| 73 | + Node* AddNode(std::unique_ptr<Node> node) override { |
| 74 | + Node* n = node.get(); |
| 75 | + nodes_.emplace(std::move(node)); |
| 76 | + return n; |
| 77 | + } |
| 78 | + |
| 79 | + void AddEdge(Node* from, Node* to) override { |
| 80 | + edges_[from].insert(to); |
| 81 | + } |
| 82 | + |
| 83 | + MaybeLocal<Array> CreateObject() const { |
| 84 | + EscapableHandleScope handle_scope(isolate_); |
| 85 | + Local<Context> context = isolate_->GetCurrentContext(); |
| 86 | + |
| 87 | + std::unordered_map<Node*, Local<Object>> info_objects; |
| 88 | + Local<Array> nodes = Array::New(isolate_, nodes_.size()); |
| 89 | + Local<String> edges_string = FIXED_ONE_BYTE_STRING(isolate_, "edges"); |
| 90 | + Local<String> is_root_string = FIXED_ONE_BYTE_STRING(isolate_, "isRoot"); |
| 91 | + Local<String> name_string = FIXED_ONE_BYTE_STRING(isolate_, "name"); |
| 92 | + Local<String> size_string = FIXED_ONE_BYTE_STRING(isolate_, "size"); |
| 93 | + Local<String> value_string = FIXED_ONE_BYTE_STRING(isolate_, "value"); |
| 94 | + Local<String> wraps_string = FIXED_ONE_BYTE_STRING(isolate_, "wraps"); |
| 95 | + |
| 96 | + for (const std::unique_ptr<Node>& n : nodes_) |
| 97 | + info_objects[n.get()] = Object::New(isolate_); |
| 98 | + |
| 99 | + { |
| 100 | + HandleScope handle_scope(isolate_); |
| 101 | + size_t i = 0; |
| 102 | + for (const std::unique_ptr<Node>& n : nodes_) { |
| 103 | + Local<Object> obj = info_objects[n.get()]; |
| 104 | + Local<Value> value; |
| 105 | + if (!String::NewFromUtf8(isolate_, n->Name(), |
| 106 | + v8::NewStringType::kNormal).ToLocal(&value) || |
| 107 | + obj->Set(context, name_string, value).IsNothing() || |
| 108 | + obj->Set(context, is_root_string, |
| 109 | + Boolean::New(isolate_, n->IsRootNode())).IsNothing() || |
| 110 | + obj->Set(context, size_string, |
| 111 | + Number::New(isolate_, n->SizeInBytes())).IsNothing() || |
| 112 | + obj->Set(context, edges_string, |
| 113 | + Array::New(isolate_)).IsNothing()) { |
| 114 | + return MaybeLocal<Array>(); |
| 115 | + } |
| 116 | + if (nodes->Set(context, i++, obj).IsNothing()) |
| 117 | + return MaybeLocal<Array>(); |
| 118 | + if (!n->IsEmbedderNode()) { |
| 119 | + value = static_cast<JSGraphJSNode*>(n.get())->JSValue(); |
| 120 | + if (obj->Set(context, value_string, value).IsNothing()) |
| 121 | + return MaybeLocal<Array>(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for (const std::unique_ptr<Node>& n : nodes_) { |
| 127 | + Node* wraps = n->WrapperNode(); |
| 128 | + if (wraps == nullptr) continue; |
| 129 | + Local<Object> from = info_objects[n.get()]; |
| 130 | + Local<Object> to = info_objects[wraps]; |
| 131 | + if (from->Set(context, wraps_string, to).IsNothing()) |
| 132 | + return MaybeLocal<Array>(); |
| 133 | + } |
| 134 | + |
| 135 | + for (const auto& edge_info : edges_) { |
| 136 | + Node* source = edge_info.first; |
| 137 | + Local<Value> edges; |
| 138 | + if (!info_objects[source]->Get(context, edges_string).ToLocal(&edges) || |
| 139 | + !edges->IsArray()) { |
| 140 | + return MaybeLocal<Array>(); |
| 141 | + } |
| 142 | + |
| 143 | + size_t i = 0; |
| 144 | + for (Node* target : edge_info.second) { |
| 145 | + if (edges.As<Array>()->Set(context, |
| 146 | + i++, |
| 147 | + info_objects[target]).IsNothing()) { |
| 148 | + return MaybeLocal<Array>(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return handle_scope.Escape(nodes); |
| 154 | + } |
| 155 | + |
| 156 | + private: |
| 157 | + Isolate* isolate_; |
| 158 | + std::unordered_set<std::unique_ptr<Node>> nodes_; |
| 159 | + std::unordered_set<JSGraphJSNode*, JSGraphJSNode::Hash, JSGraphJSNode::Equal> |
| 160 | + engine_nodes_; |
| 161 | + std::unordered_map<Node*, std::unordered_set<Node*>> edges_; |
| 162 | +}; |
| 163 | + |
| 164 | +void BuildEmbedderGraph(const FunctionCallbackInfo<Value>& args) { |
| 165 | + Environment* env = Environment::GetCurrent(args); |
| 166 | + JSGraph graph(env->isolate()); |
| 167 | + Environment::BuildEmbedderGraph(env->isolate(), &graph, env); |
| 168 | + Local<Array> ret; |
| 169 | + if (graph.CreateObject().ToLocal(&ret)) |
| 170 | + args.GetReturnValue().Set(ret); |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +class BufferOutputStream : public v8::OutputStream { |
| 175 | + public: |
| 176 | + BufferOutputStream() : buffer_(new JSString()) {} |
| 177 | + |
| 178 | + void EndOfStream() override {} |
| 179 | + int GetChunkSize() override { return 1024 * 1024; } |
| 180 | + WriteResult WriteAsciiChunk(char* data, int size) override { |
| 181 | + buffer_->Append(data, size); |
| 182 | + return kContinue; |
| 183 | + } |
| 184 | + |
| 185 | + Local<String> ToString(Isolate* isolate) { |
| 186 | + return String::NewExternalOneByte(isolate, |
| 187 | + buffer_.release()).ToLocalChecked(); |
| 188 | + } |
| 189 | + |
| 190 | + private: |
| 191 | + class JSString : public String::ExternalOneByteStringResource { |
| 192 | + public: |
| 193 | + void Append(char* data, size_t count) { |
| 194 | + store_.append(data, count); |
| 195 | + } |
| 196 | + |
| 197 | + const char* data() const override { return store_.data(); } |
| 198 | + size_t length() const override { return store_.size(); } |
| 199 | + |
| 200 | + private: |
| 201 | + std::string store_; |
| 202 | + }; |
| 203 | + |
| 204 | + std::unique_ptr<JSString> buffer_; |
| 205 | +}; |
| 206 | + |
| 207 | +void CreateHeapDump(const FunctionCallbackInfo<Value>& args) { |
| 208 | + Isolate* isolate = args.GetIsolate(); |
| 209 | + const HeapSnapshot* snapshot = isolate->GetHeapProfiler()->TakeHeapSnapshot(); |
| 210 | + BufferOutputStream out; |
| 211 | + snapshot->Serialize(&out, HeapSnapshot::kJSON); |
| 212 | + const_cast<HeapSnapshot*>(snapshot)->Delete(); |
| 213 | + Local<Value> ret; |
| 214 | + if (JSON::Parse(isolate->GetCurrentContext(), |
| 215 | + out.ToString(isolate)).ToLocal(&ret)) { |
| 216 | + args.GetReturnValue().Set(ret); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +void Initialize(Local<Object> target, |
| 221 | + Local<Value> unused, |
| 222 | + Local<Context> context) { |
| 223 | + Environment* env = Environment::GetCurrent(context); |
| 224 | + |
| 225 | + env->SetMethodNoSideEffect(target, "buildEmbedderGraph", BuildEmbedderGraph); |
| 226 | + env->SetMethodNoSideEffect(target, "createHeapDump", CreateHeapDump); |
| 227 | +} |
| 228 | + |
| 229 | +} // namespace heap |
| 230 | +} // namespace node |
| 231 | + |
| 232 | +NODE_MODULE_CONTEXT_AWARE_INTERNAL(heap_utils, node::heap::Initialize) |
0 commit comments