PassengerStatistics/Tools/LeakTracer/LeakTracer.cpp
2024-03-23 11:57:23 +08:00

39 lines
1.2 KiB
C++

#include "LeakTracer.h"
#include "DateTime.h"
#include <leaktracer.h>
#include <sstream>
LeakTracer::LeakTracer(const std::string &path, const std::string &applicationName)
: m_path(path), m_applicationName(applicationName) {
}
LeakTracer::~LeakTracer() {
stop();
}
void LeakTracer::run() {
using namespace std::chrono;
using namespace std::chrono_literals;
while (!m_exit) {
auto start = system_clock::now();
leaktracer_startMonitoringAllThreads();
while (!m_exit) {
std::this_thread::sleep_for(50ms);
auto now = system_clock::now();
auto elapsed = duration_cast<milliseconds>(now - start);
if (elapsed >= m_intervals) break;
}
std::ostringstream oss;
oss << m_path << "/" << m_applicationName << "_" << DateTime(start).toString("%Y%m%d%H%M%S") << "-"
<< DateTime::currentDateTime().toString("%Y%m%d%H%M%S") << ".dump";
leaktracer_writeLeaksToFile(oss.str().c_str());
leaktracer_stopAllMonitoring();
}
}
void LeakTracer::stop() {
if (!m_exit) {
m_exit = true;
if (m_thread.joinable()) m_thread.join();
}
}