Skip to content

Commit

Permalink
feat: add process.getHeapSnapshot()
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 4, 2018
1 parent 357576a commit ab6a9fe
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions atom/common/api/atom_bindings.cc
Expand Up @@ -8,16 +8,19 @@
#include <iostream>
#include <string>

#include "atom/common/api/file_output_stream.h"
#include "atom/common/api/locker.h"
#include "atom/common/atom_version.h"
#include "atom/common/chrome_version.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/node_includes.h"
#include "base/logging.h"
#include "base/process/process_info.h"
#include "base/process/process_metrics_iocounters.h"
#include "base/sys_info.h"
#include "native_mate/dictionary.h"
#include "v8/include/v8-profiler.h"

namespace atom {

Expand Down Expand Up @@ -54,6 +57,7 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local<v8::Object> process) {
dict.SetMethod("crash", &AtomBindings::Crash);
dict.SetMethod("hang", &Hang);
dict.SetMethod("log", &Log);
dict.SetMethod("getHeapSnapshot", &GetHeapSnapshot);
dict.SetMethod("getHeapStatistics", &GetHeapStatistics);
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
dict.SetMethod("getCreationTime", &GetCreationTime);
Expand Down Expand Up @@ -129,6 +133,21 @@ void AtomBindings::Hang() {
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
}

// static
bool AtomBindings::GetHeapSnapshot(v8::Isolate* isolate,
const base::FilePath& file_path) {
FileOutputStream stream(file_path);
if (!stream.IsValid())
return false;

auto* snap = isolate->GetHeapProfiler()->TakeHeapSnapshot();
snap->Serialize(&stream, v8::HeapSnapshot::kJSON);

const_cast<v8::HeapSnapshot*>(snap)->Delete();

return stream.IsComplete();
}

// static
v8::Local<v8::Value> AtomBindings::GetHeapStatistics(v8::Isolate* isolate) {
v8::HeapStatistics v8_heap_stats;
Expand Down
3 changes: 3 additions & 0 deletions atom/common/api/atom_bindings.h
Expand Up @@ -7,6 +7,7 @@

#include <list>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/process/process_metrics.h"
#include "base/strings/string16.h"
Expand Down Expand Up @@ -35,6 +36,8 @@ class AtomBindings {
static void Log(const base::string16& message);
static void Crash();
static void Hang();
static bool GetHeapSnapshot(v8::Isolate* isolate,
const base::FilePath& file_path);
static v8::Local<v8::Value> GetHeapStatistics(v8::Isolate* isolate);
static v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate);
static v8::Local<v8::Value> GetCreationTime(v8::Isolate* isolate);
Expand Down
35 changes: 35 additions & 0 deletions atom/common/api/file_output_stream.cc
@@ -0,0 +1,35 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/common/api/file_output_stream.h"

namespace atom {

FileOutputStream::FileOutputStream(const base::FilePath& file_path)
: file_(file_path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE) {}

bool FileOutputStream::IsValid() const {
return file_.IsValid();
}

bool FileOutputStream::IsComplete() const {
return is_complete_;
}

int FileOutputStream::GetChunkSize() {
return 65536;
}

void FileOutputStream::EndOfStream() {
is_complete_ = true;
}

v8::OutputStream::WriteResult FileOutputStream::WriteAsciiChunk(char* data,
int size) {
auto bytes_written = file_.WriteAtCurrentPos(data, size);
return bytes_written == size ? kContinue : kAbort;
}

} // namespace atom
33 changes: 33 additions & 0 deletions atom/common/api/file_output_stream.h
@@ -0,0 +1,33 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_COMMON_API_FILE_OUTPUT_STREAM_H_
#define ATOM_COMMON_API_FILE_OUTPUT_STREAM_H_

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "v8/include/v8-profiler.h"

namespace atom {

class FileOutputStream : public v8::OutputStream {
public:
explicit FileOutputStream(const base::FilePath& file_path);

bool IsValid() const;
bool IsComplete() const;

// v8::OutputStream
virtual int GetChunkSize();
virtual void EndOfStream();
virtual v8::OutputStream::WriteResult WriteAsciiChunk(char* data, int size);

private:
base::File file_;
bool is_complete_ = false;
};

} // namespace atom

#endif // ATOM_COMMON_API_FILE_OUTPUT_STREAM_H_
2 changes: 2 additions & 0 deletions atom/renderer/atom_sandboxed_renderer_client.cc
Expand Up @@ -6,6 +6,7 @@

#include "atom/common/api/api_messages.h"
#include "atom/common/api/atom_bindings.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_bindings.h"
Expand Down Expand Up @@ -152,6 +153,7 @@ void AtomSandboxedRendererClient::InitializeBindings(
b.SetMethod("getExecPath", GetExecPath);
b.SetMethod("getPid", &base::GetCurrentProcId);
b.SetMethod("getResourcesPath", &NodeBindings::GetHelperResourcesPath);
b.SetMethod("getHeapSnapshot", &AtomBindings::GetHeapSnapshot);
b.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics);
b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo);
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
Expand Down
2 changes: 2 additions & 0 deletions filenames.gypi
Expand Up @@ -449,6 +449,8 @@
'atom/common/api/event_emitter_caller.cc',
'atom/common/api/event_emitter_caller.h',
'atom/common/api/features.cc',
'atom/common/api/file_output_stream.cc',
'atom/common/api/file_output_stream.h',
'atom/common/api/locker.cc',
'atom/common/api/locker.h',
'atom/common/api/object_life_monitor.cc',
Expand Down
1 change: 1 addition & 0 deletions lib/sandboxed_renderer/init.js
Expand Up @@ -48,6 +48,7 @@ require('../renderer/web-frame-init')()
const preloadProcess = new events.EventEmitter()
preloadProcess.crash = () => binding.crash()
preloadProcess.hang = () => binding.hang()
preloadProcess.getHeapSnapshot = (filePath) => binding.getHeapSnapshot(filePath)
preloadProcess.getHeapStatistics = () => binding.getHeapStatistics()
preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
Expand Down

0 comments on commit ab6a9fe

Please sign in to comment.