Skip to content

Commit

Permalink
src: move CpuProfilerStor impl to cc file
Browse files Browse the repository at this point in the history
PR-URL: #18
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno authored and trevnorris committed Dec 7, 2023
1 parent ac7b0a0 commit 40931c4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 46 deletions.
44 changes: 42 additions & 2 deletions src/nsolid/nsolid_cpu_profiler.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
#include <nsolid/nsolid_output_stream.h>
#include "env-inl.h"
#include "nsolid_cpu_profiler.h"
#include "asserts-cpp/asserts.h"
#include "nsolid/nsolid_api.h"
#include "nsolid/nsolid_output_stream.h"
#include "v8-profiler.h"

namespace node {
namespace nsolid {

std::atomic<bool> NSolidCpuProfiler::is_running = { false };

CpuProfilerStor::CpuProfilerStor(uint64_t thread_id,
uint64_t timeout,
const std::string& title,
void* data,
CpuProfiler::cpu_profiler_proxy_sig proxy,
internal::deleter_sig deleter)
: thread_id_(thread_id),
timeout_(timeout),
title_(title),
profiler_(nullptr),
profile_(nullptr),
proxy_(proxy),
data_(data, deleter) {}

CpuProfilerStor::~CpuProfilerStor() {
// Don't try to access the profile if the Isolate it comes from is gone
SharedEnvInst envinst = GetEnvInst(thread_id_);
if (!envinst) {
return;
}

// Keep the Isolate alive while diposing the profiler and profile
EnvInst::Scope scp(envinst);
if (!scp.Success()) {
return;
}

if (profile_) {
profile_->Delete();
profile_ = nullptr;
}

if (profiler_) {
profiler_->Dispose();
profiler_ = nullptr;
}
}

NSolidCpuProfiler::NSolidCpuProfiler(): dummy_stub_(nullptr, nullptr) {
is_running = true;
ASSERT_EQ(0, blocked_cpu_profilers_.init(true));
Expand Down
73 changes: 29 additions & 44 deletions src/nsolid/nsolid_cpu_profiler.h
Original file line number Diff line number Diff line change
@@ -1,58 +1,44 @@
#ifndef SRC_NSOLID_NSOLID_CPU_PROFILER_H_
#define SRC_NSOLID_NSOLID_CPU_PROFILER_H_

#include <nsolid/nsolid_api.h>
#include <nsolid/nsolid_util.h>
#include <v8.h>
#include <v8-profiler.h>
#include <tuple>
#include <map>
#include "nsolid.h"
#include "nsolid/nsolid_util.h"
#include "nsuv-inl.h"

#include "asserts-cpp/asserts.h"
// pre-declarations.
namespace v8 {
class CpuProfiler;
class CpuProfile;
} // namespace v8

namespace node {
namespace nsolid {


class NSolidCpuProfiler {
struct CpuProfilerStor {
public:
struct CpuProfilerStor {
CpuProfilerStor(uint64_t timeout,
const std::string& title,
void* data,
CpuProfiler::cpu_profiler_proxy_sig proxy,
internal::deleter_sig deleter):
status_(0),
timeout_(timeout),
title_(title),
profiler_(nullptr),
profile_(nullptr),
proxy_(proxy),
data_(data, deleter) {
}

~CpuProfilerStor() {
if (profile_) {
profile_->Delete();
profile_ = nullptr;
}

if (profiler_) {
profiler_->Dispose();
profiler_ = nullptr;
}
}

int status_;
uint64_t timeout_;
std::string title_;
v8::CpuProfiler* profiler_;
v8::CpuProfile* profile_;
CpuProfiler::cpu_profiler_proxy_sig proxy_;
internal::user_data data_;
};
explicit CpuProfilerStor(uint64_t thread_id,
uint64_t timeout,
const std::string& title,
void* data,
CpuProfiler::cpu_profiler_proxy_sig proxy,
internal::deleter_sig deleter);
NSOLID_DELETE_DEFAULT_CONSTRUCTORS(CpuProfilerStor)
~CpuProfilerStor();

private:
friend class NSolidCpuProfiler;
uint64_t thread_id_;
uint64_t timeout_;
std::string title_;
v8::CpuProfiler* profiler_;
v8::CpuProfile* profile_;
CpuProfiler::cpu_profiler_proxy_sig proxy_;
internal::user_data data_;
};

class NSolidCpuProfiler {
public:
static NSolidCpuProfiler* Inst();

int TakeCpuProfile(SharedEnvInst envinst,
Expand Down Expand Up @@ -99,7 +85,6 @@ class NSolidCpuProfiler {
~NSolidCpuProfiler();
};


} // namespace nsolid
} // namespace node

Expand Down

0 comments on commit 40931c4

Please sign in to comment.