From 596033ec83d1365d7a5df31d28f91347320fe155 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Sun, 19 May 2013 10:02:45 -0400 Subject: KenLM 10ddf7d92335 Comment some asserts, update uusage --- klm/util/usage.cc | 65 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) (limited to 'klm/util/usage.cc') diff --git a/klm/util/usage.cc b/klm/util/usage.cc index ad4dc7b4..5fa3cc9a 100644 --- a/klm/util/usage.cc +++ b/klm/util/usage.cc @@ -5,51 +5,80 @@ #include #include #include +#include +#include #include #include #if !defined(_WIN32) && !defined(_WIN64) #include #include +#include #include #endif namespace util { -namespace { #if !defined(_WIN32) && !defined(_WIN64) +namespace { float FloatSec(const struct timeval &tv) { return static_cast(tv.tv_sec) + (static_cast(tv.tv_usec) / 1000000.0); } -#endif +float FloatSec(const struct timespec &tv) { + return static_cast(tv.tv_sec) + (static_cast(tv.tv_nsec) / 1000000000.0); +} const char *SkipSpaces(const char *at) { - for (; *at == ' '; ++at) {} + for (; *at == ' ' || *at == '\t'; ++at) {} return at; } + +class RecordStart { + public: + RecordStart() { + clock_gettime(CLOCK_MONOTONIC, &started_); + } + + const struct timespec &Started() const { + return started_; + } + + private: + struct timespec started_; +}; + +const RecordStart kRecordStart; } // namespace +#endif void PrintUsage(std::ostream &out) { #if !defined(_WIN32) && !defined(_WIN64) + // Linux doesn't set memory usage in getrusage :-( + std::set headers; + headers.insert("VmPeak:"); + headers.insert("VmRSS:"); + headers.insert("Name:"); + + std::ifstream status("/proc/self/status", std::ios::in); + std::string header, value; + while ((status >> header) && getline(status, value)) { + if (headers.find(header) != headers.end()) { + out << header << SkipSpaces(value.c_str()) << '\t'; + } + } + struct rusage usage; - if (getrusage(RUSAGE_SELF, &usage)) { + if (getrusage(RUSAGE_CHILDREN, &usage)) { perror("getrusage"); return; } - out << "user\t" << FloatSec(usage.ru_utime) << "\nsys\t" << FloatSec(usage.ru_stime) << '\n'; - out << "CPU\t" << (FloatSec(usage.ru_utime) + FloatSec(usage.ru_stime)) << '\n'; - // Linux doesn't set memory usage :-(. - std::ifstream status("/proc/self/status", std::ios::in); - std::string line; - while (getline(status, line)) { - if (!strncmp(line.c_str(), "VmRSS:\t", 7)) { - out << "RSSCur\t" << SkipSpaces(line.c_str() + 7) << '\n'; - break; - } else if (!strncmp(line.c_str(), "VmPeak:\t", 8)) { - out << "VmPeak\t" << SkipSpaces(line.c_str() + 8) << '\n'; - } - } - out << "RSSMax\t" << usage.ru_maxrss << " kB" << '\n'; + out << "RSSMax:" << usage.ru_maxrss << " kB" << '\t'; + out << "user:" << FloatSec(usage.ru_utime) << "\tsys:" << FloatSec(usage.ru_stime) << '\t'; + out << "CPU:" << (FloatSec(usage.ru_utime) + FloatSec(usage.ru_stime)); + + struct timespec current; + clock_gettime(CLOCK_MONOTONIC, ¤t); + out << "\treal:" << (FloatSec(current) - FloatSec(kRecordStart.Started())) << '\n'; #endif } -- cgit v1.2.3