4 #include "util/proc_stats.h" 7 #include "base/walltime.h" 8 #include "strings/stringpiece.h" 9 #include "strings/numbers.h" 10 #include "strings/strip.h" 17 size_t find_nth(StringPiece str,
char c, uint32 index) {
18 for (
size_t i = 0; i < str.size(); ++i) {
24 return StringPiece::npos;
28 static std::once_flag cpu_once;
30 static int CPU_NUM = 0;
32 static void InitCpuInfo() {
33 FILE* f = fopen(
"/proc/cpuinfo",
"r");
34 const char kModelNameLine[] =
"model name";
39 while (fgets(line,
sizeof(line), f) != NULL) {
40 const char* sep = strchr(line,
':');
44 StringPiece str(line, sep - line);
45 str = absl::StripAsciiWhitespace(str);
46 if (str == kModelNameLine)
52 ProcessStats ProcessStats::Read() {
54 FILE* f = fopen(
"/proc/self/status",
"r");
59 while (getline(&line, &len, f) != -1) {
60 if (!strncmp(line,
"VmPeak:", 7)) stats.vm_peak = ParseLeadingUDec32Value(line + 8, 0);
61 else if (!strncmp(line,
"VmSize:", 7)) stats.vm_size = ParseLeadingUDec32Value(line + 8, 0);
62 else if (!strncmp(line,
"VmRSS:", 6)) stats.vm_size = ParseLeadingUDec32Value(line + 7, 0);
65 f = fopen(
"/proc/self/stat",
"r");
67 long jiffies_per_second = sysconf(_SC_CLK_TCK);
68 uint64 start_since_boot = 0;
70 size_t bytes_read = fread(buf, 1,
sizeof buf, f);
71 if (bytes_read ==
sizeof buf) {
72 fprintf(stderr,
"Buffer is too small %lu\n",
sizeof buf);
74 StringPiece str(buf, bytes_read);
75 size_t pos = find_nth(str,
' ', 20);
76 if (pos != StringPiece::npos) {
77 start_since_boot = ParseLeadingUDec64Value(str.data() + pos + 1, 0);
78 start_since_boot /= jiffies_per_second;
82 if (start_since_boot > 0) {
83 f = fopen(
"/proc/stat",
"r");
85 while (getline(&line, &len, f) != -1) {
86 if (!strncmp(line,
"btime ", 6)) {
87 uint64 boot_time = ParseLeadingUDec64Value(line + 6, 0);
89 stats.start_time_seconds = boot_time + start_since_boot;
102 unsigned int NumCPUs() {
103 std::call_once(cpu_once, InitCpuInfo);
112 os <<
"VmPeak: " << stats.vm_peak <<
"kb, VmSize: " << stats.vm_size
113 <<
"kb, VmRSS: " << stats.vm_rss <<
"kb, Start Time: " 114 << base::PrintLocalTime(stats.start_time_seconds);