a bit better call stack
This commit is contained in:
@@ -16,41 +16,40 @@ void MlPerfMon::turnOn() {
|
||||
start_time = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void MlPerfMon::add_method_call(const std::string &method) {
|
||||
std::thread::id this_id = std::this_thread::get_id();
|
||||
void MlPerfMon::add_method_call(const MlValue &function, const std::vector<MlValue> &args) {
|
||||
// only main thread is logged, to prevent mixing from others threads so lock not needed
|
||||
if (main_thread_id != this_id)
|
||||
return;
|
||||
if (main_thread_id != std::this_thread::get_id()) return;
|
||||
|
||||
std::string method = function.type == MlValue::LAMBDA ? "lambda" : function.str;
|
||||
|
||||
call_stack.push_back(method);
|
||||
|
||||
if (perfOn) {
|
||||
auto search = calls_counter.find(method);
|
||||
if (search != calls_counter.end()) {
|
||||
calls_counter[method] = search->second + 1;
|
||||
} else {
|
||||
calls_counter.insert({method, 1});
|
||||
}
|
||||
std::string args_string;
|
||||
for (size_t i = 0; i < args.size() && args_string.size() < 64; i++) {
|
||||
args_string.append(" ");
|
||||
args_string.append(args[i].display());
|
||||
}
|
||||
if (args_string.size() > 64)
|
||||
args_string = args_string.substr(0, 62) + ".."; // TODO introduce constant here
|
||||
|
||||
call_stack.push_back("(" + method + args_string + ")");
|
||||
|
||||
if (perfOn)
|
||||
calls_counter[method]++;
|
||||
}
|
||||
|
||||
void MlPerfMon::end_method_call() {
|
||||
std::thread::id this_id = std::this_thread::get_id();
|
||||
// only main thread is looged, to prevent mixing from others threads so lock not needed
|
||||
if (main_thread_id != this_id)
|
||||
return;
|
||||
if (main_thread_id != std::this_thread::get_id()) return;
|
||||
|
||||
if (!call_stack.empty()){
|
||||
if (!call_stack.empty())
|
||||
call_stack.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
std::string MlPerfMon::callstack() const {
|
||||
std::string cs {"call stack:\n"};
|
||||
std::string cs {"\ncall stack:\n"};
|
||||
|
||||
int cnt = 0;
|
||||
for (auto it = call_stack.rbegin(); it != call_stack.rend() && cnt < call_stack_max_methods; ++it, ++cnt)
|
||||
cs.append( std::string(*it) + "\n"); // << std::endl;
|
||||
cs.append(" " + std::string(*it) + "\n"); // << std::endl;
|
||||
|
||||
if (call_stack.size() > call_stack_max_methods)
|
||||
cs.append("next " + std::to_string(call_stack.size() - call_stack_max_methods) + " entries skipped..\n");
|
||||
|
||||
Reference in New Issue
Block a user