Skip to content
Open
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
30 changes: 29 additions & 1 deletion linux-perf-listener.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
#include <sstream>
#include <thread>
#include <array>
#include <string>
#include <chrono>
#include <fstream>
#include <iostream>

#include "linux-perf.h"

namespace node {

void write_to_file(LinuxPerfHandler* handler) {

while(handler->IsAlive()) {
std::vector<std::string> arr = handler->GetBufferElements(100);
if (arr.size() > 0 ) {
for(uint i = 0; i < arr.size(); i++) {
*(handler->GetFile()) << arr[i];
}
}
std::chrono::milliseconds(1000);
}
}

LinuxPerfHandler::LinuxPerfHandler(v8::Isolate* isolate) : v8::CodeEventHandler(isolate) {
// TODO(mmarchini):: ideally this should be handled in another thread.
auto pid = static_cast<int>(uv_os_getpid());

mapFile.open("/tmp/perf-" + std::to_string(pid) + ".map");
t = std::thread(write_to_file, this);
}

LinuxPerfHandler::~LinuxPerfHandler() {
alive = false;
t.join();
mapFile.close();
}

Expand All @@ -36,10 +58,16 @@ std::string LinuxPerfHandler::FormatName(v8::CodeEvent* code_event) {
}

void LinuxPerfHandler::Handle(v8::CodeEvent* code_event) {
mapFile << std::hex << code_event->GetCodeStartAddress() << " "
// std::cout << "Handling event" << std::endl;
codeEventStream << std::hex << code_event->GetCodeStartAddress() << " "
<< std::hex << code_event->GetCodeSize() << " "
<< v8::CodeEvent::GetCodeEventTypeName(code_event->GetCodeType())
<< ":" << FormatName(code_event) << std::endl;

std::lock_guard<std::mutex> lock(mutex_);
buffer.push(codeEventStream.str());
codeEventStream.str("");
}


}
34 changes: 32 additions & 2 deletions linux-perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
#include <nan.h>
#include <node_object_wrap.h>
#include <fstream>

#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <thread>
#include <mutex>

namespace node {

Expand All @@ -14,11 +19,36 @@ class LinuxPerfHandler : public v8::CodeEventHandler {
explicit LinuxPerfHandler(v8::Isolate* isolate);
~LinuxPerfHandler() override;


void Handle(v8::CodeEvent* code_event) override;

std::vector<std::string> GetBufferElements(int n) {
int count = 0;
std::vector<std::string> values;
std::lock_guard<std::mutex> lock(mutex_);
while(count < n && !buffer.empty()) {
values.push_back(buffer.front());
buffer.pop();
count++;
}
return values;
}

bool IsAlive() {
return alive;
}

std::ofstream* GetFile() {
return &mapFile;
}

private:
bool alive = true;
std::thread t;
std::ofstream mapFile;
std::queue<std::string> buffer;
std::stringstream codeEventStream;
std::string FormatName(v8::CodeEvent* code_event);
std::mutex mutex_;
};

class LinuxPerf : public Nan::ObjectWrap {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.