|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +#include "node_profiling.h" |
| 23 | + |
| 24 | +#include "json_utils.h" |
| 25 | +#include "util.h" |
| 26 | + |
| 27 | +#include <memory> |
| 28 | + |
| 29 | +namespace node { |
| 30 | + |
| 31 | +using v8::AllocationProfile; |
| 32 | +using v8::HandleScope; |
| 33 | +using v8::HeapProfiler; |
| 34 | +using v8::Isolate; |
| 35 | +using v8::Value; |
| 36 | + |
| 37 | +static void BuildHeapProfileNode(Isolate* isolate, |
| 38 | + const AllocationProfile::Node* node, |
| 39 | + JSONWriter* writer) { |
| 40 | + size_t selfSize = 0; |
| 41 | + for (const auto& allocation : node->allocations) |
| 42 | + selfSize += allocation.size * allocation.count; |
| 43 | + |
| 44 | + writer->json_keyvalue("selfSize", selfSize); |
| 45 | + writer->json_keyvalue("id", node->node_id); |
| 46 | + writer->json_objectstart("callFrame"); |
| 47 | + writer->json_keyvalue("scriptId", node->script_id); |
| 48 | + writer->json_keyvalue("lineNumber", node->line_number - 1); |
| 49 | + writer->json_keyvalue("columnNumber", node->column_number - 1); |
| 50 | + Utf8Value name(isolate, node->name); |
| 51 | + Utf8Value script_name(isolate, node->script_name); |
| 52 | + writer->json_keyvalue("functionName", *name); |
| 53 | + writer->json_keyvalue("url", *script_name); |
| 54 | + writer->json_objectend(); |
| 55 | + |
| 56 | + writer->json_arraystart("children"); |
| 57 | + for (const auto* child : node->children) { |
| 58 | + writer->json_start(); |
| 59 | + BuildHeapProfileNode(isolate, child, writer); |
| 60 | + writer->json_end(); |
| 61 | + } |
| 62 | + writer->json_arrayend(); |
| 63 | +} |
| 64 | + |
| 65 | +bool SerializeHeapProfile(Isolate* isolate, std::ostringstream& out_stream) { |
| 66 | + HandleScope scope(isolate); |
| 67 | + HeapProfiler* profiler = isolate->GetHeapProfiler(); |
| 68 | + std::unique_ptr<AllocationProfile> profile(profiler->GetAllocationProfile()); |
| 69 | + if (!profile) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + profiler->StopSamplingHeapProfiler(); |
| 73 | + JSONWriter writer(out_stream, true); |
| 74 | + writer.json_start(); |
| 75 | + |
| 76 | + writer.json_arraystart("samples"); |
| 77 | + for (const auto& sample : profile->GetSamples()) { |
| 78 | + writer.json_start(); |
| 79 | + writer.json_keyvalue("size", sample.size * sample.count); |
| 80 | + writer.json_keyvalue("nodeId", sample.node_id); |
| 81 | + writer.json_keyvalue("ordinal", static_cast<double>(sample.sample_id)); |
| 82 | + writer.json_end(); |
| 83 | + } |
| 84 | + writer.json_arrayend(); |
| 85 | + |
| 86 | + writer.json_objectstart("head"); |
| 87 | + BuildHeapProfileNode(isolate, profile->GetRootNode(), &writer); |
| 88 | + writer.json_objectend(); |
| 89 | + |
| 90 | + writer.json_end(); |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +HeapProfileOptions ParseHeapProfileOptions( |
| 95 | + const v8::FunctionCallbackInfo<Value>& args) { |
| 96 | + HeapProfileOptions options; |
| 97 | + CHECK_LE(args.Length(), 3); |
| 98 | + if (args.Length() > 0) { |
| 99 | + CHECK(args[0]->IsNumber()); |
| 100 | + options.sample_interval = |
| 101 | + static_cast<uint64_t>(args[0].As<v8::Number>()->Value()); |
| 102 | + } |
| 103 | + if (args.Length() > 1) { |
| 104 | + CHECK(args[1]->IsInt32()); |
| 105 | + options.stack_depth = args[1].As<v8::Int32>()->Value(); |
| 106 | + } |
| 107 | + if (args.Length() > 2) { |
| 108 | + CHECK(args[2]->IsUint32()); |
| 109 | + options.flags = static_cast<v8::HeapProfiler::SamplingFlags>( |
| 110 | + args[2].As<v8::Uint32>()->Value()); |
| 111 | + } |
| 112 | + return options; |
| 113 | +} |
| 114 | + |
| 115 | +CpuProfileOptions ParseCpuProfileOptions( |
| 116 | + const v8::FunctionCallbackInfo<Value>& args) { |
| 117 | + CpuProfileOptions options; |
| 118 | + CHECK_LE(args.Length(), 2); |
| 119 | + if (args.Length() > 0) { |
| 120 | + CHECK(args[0]->IsInt32()); |
| 121 | + options.sampling_interval_us = args[0].As<v8::Int32>()->Value(); |
| 122 | + } |
| 123 | + if (args.Length() > 1) { |
| 124 | + CHECK(args[1]->IsUint32()); |
| 125 | + options.max_samples = args[1].As<v8::Uint32>()->Value(); |
| 126 | + } |
| 127 | + return options; |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace node |
0 commit comments