-
Notifications
You must be signed in to change notification settings - Fork 130
/
heapdump.cc
138 lines (112 loc) · 4.24 KB
/
heapdump.cc
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
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2012, Ben Noordhuis <info@bnoordhuis.nl>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "node.h" // Picks up BUILDING_NODE_EXTENSION on Windows, see #30.
#include "nan.h"
#include "uv.h"
#include "v8-profiler.h"
#include "v8.h"
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
namespace {
static const int kMaxPath = 4096;
static const int kSignalFlag = 2;
inline int WriteSnapshot(v8::Isolate* isolate, const char* filename);
inline void PlatformInit(v8::Isolate* isolate, int flags);
inline void RandomSnapshotFilename(char* buffer, size_t size);
} // namespace anonymous
#ifdef _WIN32
#include "heapdump-win32.h"
#else
#include "heapdump-posix.h"
#endif
namespace {
class FileOutputStream : public v8::OutputStream {
public:
explicit FileOutputStream(FILE* stream) : stream_(stream) {}
virtual int GetChunkSize() {
return 65536; // big chunks == faster
}
virtual void EndOfStream() {}
virtual WriteResult WriteAsciiChunk(char* data, int size) {
const size_t len = static_cast<size_t>(size);
size_t off = 0;
while (off < len && !feof(stream_) && !ferror(stream_))
off += fwrite(data + off, 1, len - off, stream_);
return off == len ? kContinue : kAbort;
}
private:
FILE* stream_;
};
const v8::HeapSnapshot* TakeHeapSnapshot(v8::Isolate* isolate) {
(void) &isolate;
#if NODE_VERSION_AT_LEAST(3, 0, 0)
return isolate->GetHeapProfiler()->TakeHeapSnapshot();
#elif NODE_VERSION_AT_LEAST(0, 11, 0)
return isolate->GetHeapProfiler()->TakeHeapSnapshot(Nan::EmptyString());
#else
return v8::HeapProfiler::TakeSnapshot(Nan::EmptyString());
#endif
}
NAN_METHOD(WriteSnapshot) {
v8::Isolate* const isolate = info.GetIsolate();
char filename[kMaxPath];
v8::Local<v8::Value> filename_v = info[0];
if (filename_v->IsString()) {
Nan::Utf8String filename_string(filename_v);
snprintf(filename, sizeof(filename), "%s", *filename_string);
} else {
RandomSnapshotFilename(filename, sizeof(filename));
}
if (const int err = WriteSnapshot(isolate, filename)) {
info.GetReturnValue().Set(err);
return; // Write error.
}
if (!filename_v->IsString()) filename_v = Nan::New(filename).ToLocalChecked();
info.GetReturnValue().Set(filename_v);
}
inline int WriteSnapshot(v8::Isolate* isolate, const char* filename) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) return errno;
const v8::HeapSnapshot* const snap = TakeHeapSnapshot(isolate);
FileOutputStream stream(fp);
snap->Serialize(&stream, v8::HeapSnapshot::kJSON);
int err = 0;
if (fclose(fp)) err = errno;
// Work around a deficiency in the API. The HeapSnapshot object is const
// but we cannot call HeapProfiler::DeleteAllHeapSnapshots() because that
// invalidates _all_ snapshots, including those created by other tools.
const_cast<v8::HeapSnapshot*>(snap)->Delete();
return err;
}
inline void RandomSnapshotFilename(char* buffer, size_t size) {
const uint64_t now = uv_hrtime();
const unsigned long sec = static_cast<unsigned long>(now / 1000000);
const unsigned long usec = static_cast<unsigned long>(now % 1000000);
snprintf(buffer, size, "heapdump-%lu.%lu.heapsnapshot", sec, usec);
}
NAN_METHOD(Configure) {
PlatformInit(info.GetIsolate(), Nan::To<int32_t>(info[0]).FromJust());
}
NAN_MODULE_INIT(Initialize) {
Nan::Set(target,
Nan::New("kSignalFlag").ToLocalChecked(),
Nan::New(kSignalFlag));
Nan::SetMethod(target, "configure", Configure);
Nan::SetMethod(target, "writeSnapshot", WriteSnapshot);
}
NODE_MODULE(addon, Initialize)
} // namespace anonymous