Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 672f7bb227
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 109 lines (96 sloc) 3.827 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
#include <v8.h>
#include <v8-profiler.h>
#include <node.h>
#include "snapshot.h"
#include "profile.h"

using namespace v8;
using namespace node;
using namespace nodex;

static Handle<Value> TakeSnapshot(const Arguments& args) {
  HandleScope scope;
  Local<String> title = String::New("");
  int32_t len = args.Length();
  HeapSnapshot::Type mode = HeapSnapshot::kAggregated;
  if (len > 0) {
    title = args[0]->ToString();
  }
  if (len > 1 && args[1]->IsInt32()) {
    mode = static_cast<HeapSnapshot::Type>(args[1]->Int32Value());
  }
  const HeapSnapshot* snapshot = HeapProfiler::TakeSnapshot(title, mode);
  return scope.Close(Snapshot::New(snapshot));
}

static Handle<Value> GetSnapshot(const Arguments& args) {
  HandleScope scope;
  if (args.Length() < 1) {
    return ThrowException(Exception::Error(String::New("No index specified")));
  } else if (!args[0]->IsInt32()) {
    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  }
  int32_t index = args[0]->Int32Value();
  const HeapSnapshot* snapshot = HeapProfiler::GetSnapshot(index);
  return scope.Close(Snapshot::New(snapshot));
}

static Handle<Value> FindSnapshot(const Arguments& args) {
  HandleScope scope;
  if (args.Length() < 1) {
    return ThrowException(Exception::Error(String::New("No uid specified")));
  }
  uint32_t uid = args[0]->Uint32Value();
  const HeapSnapshot* snapshot = HeapProfiler::FindSnapshot(uid);
  return scope.Close(Snapshot::New(snapshot));
}

static Handle<Value> GetSnapshotsCount(const Arguments& args) {
  HandleScope scope;
  return scope.Close(Integer::New(HeapProfiler::GetSnapshotsCount()));
}

Handle<Value> StartProfiling(const Arguments& args) {
  HandleScope scope;
  Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
  v8::CpuProfiler::StartProfiling(title);
  return Undefined();
}

Handle<Value> StopProfiling(const Arguments& args) {
  HandleScope scope;
  Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
  const CpuProfile* profile = v8::CpuProfiler::StopProfiling(title);
  return scope.Close(Profile::New(profile));
}

Handle<Value> GetProfile(const Arguments& args) {
  HandleScope scope;
  if (args.Length() < 1) {
    return ThrowException(Exception::Error(String::New("No index specified")));
  } else if (!args[0]->IsInt32()) {
    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  }
  int32_t index = args[0]->Int32Value();
  const CpuProfile* profile = v8::CpuProfiler::GetProfile(index);
  return scope.Close(Profile::New(profile));
}

Handle<Value> FindProfile(const Arguments& args) {
  HandleScope scope;
  if (args.Length() < 1) {
    return ThrowException(Exception::Error(String::New("No index specified")));
  } else if (!args[0]->IsInt32()) {
    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  }
  uint32_t uid = args[0]->Uint32Value();
  const CpuProfile* profile = v8::CpuProfiler::FindProfile(uid);
  return scope.Close(Profile::New(profile));
}

Handle<Value> GetProfilesCount(const Arguments& args) {
  HandleScope scope;
  return scope.Close(Integer::New(v8::CpuProfiler::GetProfilesCount()));
}

extern "C" void init(Handle<Object> target) {
  HandleScope scope;

  NODE_SET_METHOD(target, "takeSnapshot", TakeSnapshot);
  NODE_SET_METHOD(target, "getSnapshot", GetSnapshot);
  NODE_SET_METHOD(target, "findSnapshot", FindSnapshot);
  NODE_SET_METHOD(target, "snapshotCount", GetSnapshotsCount);
  
  NODE_SET_METHOD(target, "startProfiling", StartProfiling);
  NODE_SET_METHOD(target, "stopProfiling", StopProfiling);
  NODE_SET_METHOD(target, "getProfile", GetProfile);
  NODE_SET_METHOD(target, "findProfile", FindProfile);
  NODE_SET_METHOD(target, "getProfilesCount", GetProfilesCount);
}
Something went wrong with that request. Please try again.