-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmetrics.cc
48 lines (36 loc) · 1.15 KB
/
sysmetrics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ml_benchmark/sysmetrics.h"
#include <base/check.h>
#include <base/files/file.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <re2/re2.h>
#include <string>
namespace {
static const char* kProcFile = "/proc/self/status";
int64_t GetStatusField(const std::string field_name) {
const std::string field_matcher = field_name + ":\\s+(\\d+)\\s+kB";
std::string status;
int64_t value;
CHECK(base::ReadFileToString(base::FilePath(kProcFile), &status))
<< "Could not read " << kProcFile;
if (!RE2::PartialMatch(status, field_matcher, &value)) {
LOG(ERROR) << "Couldn't parse " << field_name << " from " << kProcFile;
return -1;
}
return value;
}
} // namespace
namespace ml_benchmark {
int64_t GetVMSizeBytes() {
return GetStatusField("VmSize") * 1024;
}
int64_t GetVMPeakBytes() {
return GetStatusField("VmPeak") * 1024;
}
int64_t GetSwapAndRSSBytes() {
return GetStatusField("VmRSS") * 1024 + GetStatusField("VmSwap") * 1024;
}
} // namespace ml_benchmark