Working getrusage and wall clock metrics

This commit is contained in:
flamingcow
2019-05-10 23:04:26 -07:00
parent 187585d02c
commit 13553e1a07
5 changed files with 95 additions and 6 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.a *.a
*.o *.o
*.so *.so
example_loop

View File

@@ -1,9 +1,9 @@
FIRE_CXX ?= clang++ FIRE_CXX ?= clang++
FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror -Wextra -fPIE -fPIC -fstack-protector-strong -fsanitize=safe-stack -fsanitize=safe-stack FIRE_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror -Wextra -fPIE -fPIC -fstack-protector-strong -fsanitize=safe-stack -fsanitize=safe-stack
FIRE_LDFLAGS ?= -fuse-ld=gold -flto -Wl,-z,relro -Wl,-z,now FIRE_LDFLAGS ?= -fuse-ld=gold -flto -Wl,-z,relro -Wl,-z,now
FIRE_LDLIBS ?= -lglog FIRE_LDLIBS ?= -lgflags -lglog
all: fireusage.a fireusage.o fireusage.so all: fireusage.a fireusage.o fireusage.so example_loop
objects = usage.o objects = usage.o
@@ -16,6 +16,9 @@ fireusage.o: $(objects)
fireusage.so: $(objects) fireusage.so: $(objects)
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS) $(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS)
example_loop: example_loop.o fireusage.o
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -pie -o $@ $+ $(FIRE_LDLIBS)
%.o: %.cc *.h Makefile %.o: %.cc *.h Makefile
$(FIRE_CXX) $(FIRE_CXXFLAGS) -c -o $@ $< $(FIRE_CXX) $(FIRE_CXXFLAGS) -c -o $@ $<

22
example_loop.cc Normal file
View File

@@ -0,0 +1,22 @@
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "usage.h"
int main(int argc, char *argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
fireusage::UsageTracker tracker;
tracker.Start();
for (int i = 0; i < 100000000; ++i) {
tracker.AddEvent();
for (int _ = 0; _ < 100000; ++_) {
}
}
tracker.Stop();
tracker.Log();
gflags::ShutDownCommandLineFlags();
google::ShutdownGoogleLogging();
}

View File

@@ -1,9 +1,58 @@
#include <glog/logging.h>
#include <iomanip>
#include "usage.h" #include "usage.h"
namespace fireusage { namespace fireusage {
UsageTracker::UsageTracker(const std::string_view& plural_resource_name) namespace {
: plural_resource_name_(plural_resource_name) {
std::chrono::nanoseconds TvToNs(const timeval& tv) {
return std::chrono::seconds(tv.tv_sec) + std::chrono::microseconds(tv.tv_usec);
}
} // namespace
void UsageTracker::AddEvents(uint64_t num) {
events_ += num;
}
void UsageTracker::AddEvent() {
events_ += 1;
}
void UsageTracker::Start() {
CHECK(!running_);
running_ = true;
PCHECK(getrusage(RUSAGE_THREAD, &start_usage_) == 0);
start_time_ = std::chrono::steady_clock::now();
}
void UsageTracker::Stop() {
CHECK(running_);
running_ = false;
const auto end_time_ = std::chrono::steady_clock::now();
rusage end_usage_;
PCHECK(getrusage(RUSAGE_THREAD, &end_usage_) == 0);
wall_time_ += end_time_ - start_time_;
user_time_ += TvToNs(end_usage_.ru_utime) - TvToNs(start_usage_.ru_utime);
sys_time_ += TvToNs(end_usage_.ru_stime) - TvToNs(start_usage_.ru_stime);
}
void UsageTracker::Log() {
LOG(INFO) << "usage:";
LOG(INFO) << "\tevents: " << std::setw(20) << std::setfill(' ') << events_;
LOG(INFO) << "\twall time: " << std::setw(20) << std::setfill(' ') << wall_time_.count() << "ns";
LOG(INFO) << "\tuser time: " << std::setw(20) << std::setfill(' ') << user_time_.count() << "ns";
LOG(INFO) << "\tsys time: " << std::setw(20) << std::setfill(' ') << sys_time_.count() << "ns";
LOG(INFO) << "\twall time / event: " << std::setw(20) << std::setfill(' ') << (wall_time_ / events_).count() << "ns";
LOG(INFO) << "\tuser time / event: " << std::setw(20) << std::setfill(' ') << (user_time_ / events_).count() << "ns";
LOG(INFO) << "\tsys time / event: " << std::setw(20) << std::setfill(' ') << (sys_time_ / events_).count() << "ns";
} }
} // namespace fireusage } // namespace fireusage

18
usage.h
View File

@@ -1,15 +1,29 @@
#pragma once #pragma once
#include <chrono>
#include <string> #include <string>
#include <sys/resource.h>
namespace fireusage { namespace fireusage {
class UsageTracker { class UsageTracker {
public: public:
UsageTracker(const std::string_view& plural_resource_name); void AddEvents(uint64_t num);
void AddEvent();
void Start();
void Stop();
void Log();
private: private:
const std::string plural_resource_name_; bool running_ = false;
uint64_t events_ = 0;
std::chrono::nanoseconds wall_time_;
std::chrono::nanoseconds user_time_;
std::chrono::nanoseconds sys_time_;
std::chrono::steady_clock::time_point start_time_;
rusage start_usage_;
}; };
} // namespace fireusage } // namespace fireusage