1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 17:17:40 +03:00
mold/perf.cc

85 lines
1.9 KiB
C++
Raw Normal View History

2020-11-03 11:26:42 +03:00
#include "mold.h"
2020-12-10 16:32:47 +03:00
#include <iomanip>
#include <ios>
2020-12-11 10:51:20 +03:00
#include <sys/resource.h>
#include <sys/time.h>
2020-12-10 16:32:47 +03:00
2020-11-03 11:26:42 +03:00
std::vector<Counter *> Counter::instances;
bool Counter::enabled = true;
2020-12-11 10:51:20 +03:00
std::vector<Timer *> Timer::instances;
2020-11-03 11:26:42 +03:00
void Counter::print() {
if (!enabled)
return;
std::vector<Counter *> vec = instances;
2020-11-30 12:43:04 +03:00
std::stable_sort(vec.begin(), vec.end(), [](Counter *a, Counter *b) {
return a->value > b->value;
});
2020-11-03 11:26:42 +03:00
for (Counter *c : vec)
2020-12-10 16:32:47 +03:00
std::cout << std::setw(20) << std::right << c->name << "=" << c->value << "\n";
2020-11-03 11:26:42 +03:00
}
2020-12-11 10:51:20 +03:00
static u64 now_nsec() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
}
2020-12-11 10:52:19 +03:00
static u64 to_nsec(struct timeval t) {
return t.tv_sec * 1000000000 + t.tv_usec * 1000;
2020-12-11 10:51:20 +03:00
}
Timer::Timer(std::string name) : name(name) {
instances.push_back(this);
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
start = now_nsec();
2020-12-11 10:52:19 +03:00
user = to_nsec(usage.ru_utime);
sys = to_nsec(usage.ru_stime);
2020-12-11 10:51:20 +03:00
}
void Timer::stop() {
if (stopped)
return;
stopped = true;
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
end = now_nsec();
2020-12-11 10:52:19 +03:00
user = to_nsec(usage.ru_utime) - user;
sys = to_nsec(usage.ru_stime) - sys;
2020-12-11 10:51:20 +03:00
}
void Timer::print() {
2020-12-11 10:52:19 +03:00
for (int i = instances.size() - 1; i >= 0; i--)
instances[i]->stop();
2020-12-11 10:51:20 +03:00
std::vector<int> depth(instances.size());
2020-12-11 11:04:19 +03:00
for (int i = 0; i < instances.size(); i++)
for (int j = 0; j < i; j++)
if (instances[i]->end < instances[j]->end)
depth[i]++;
2020-12-11 10:51:20 +03:00
std::cout << " User System Real Name\n";
for (int i = 0; i < instances.size(); i++) {
2020-12-11 10:52:19 +03:00
Timer &t = *instances[i];
2020-12-11 10:51:20 +03:00
printf(" % 8.3f % 8.3f % 8.3f %s%s\n",
2020-12-11 10:52:19 +03:00
((double)t.user / 1000000000),
((double)t.sys / 1000000000),
2020-12-11 10:51:20 +03:00
(((double)t.end - t.start) / 1000000000),
2020-12-12 07:11:55 +03:00
std::string(depth[i] * 2, ' ').c_str(),
2020-12-11 10:51:20 +03:00
t.name.c_str());
}
2020-12-11 10:52:19 +03:00
2020-12-11 10:51:20 +03:00
std::cout << std::flush;
}