Skip to content

src: move CpuProfilerStor impl to cc file #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
88 changes: 29 additions & 59 deletions src/nsolid/nsolid_cpu_profiler.h
Original file line number Diff line number Diff line change
@@ -1,73 +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 thread_id,
uint64_t timeout,
const std::string& title,
void* data,
CpuProfiler::cpu_profiler_proxy_sig proxy,
internal::deleter_sig deleter):
status_(0),
thread_id_(thread_id),
timeout_(timeout),
title_(title),
profiler_(nullptr),
profile_(nullptr),
proxy_(proxy),
data_(data, deleter) {
}

~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;
}
}

int status_;
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_;
};
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 @@ -114,7 +85,6 @@ class NSolidCpuProfiler {
~NSolidCpuProfiler();
};


} // namespace nsolid
} // namespace node

Expand Down