-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmlog_writer_test.cc
256 lines (213 loc) · 7.9 KB
/
vmlog_writer_test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2010 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <inttypes.h>
#include <utime.h>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <base/at_exit.h>
#include <base/files/file_util.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/stringprintf.h>
#include <chromeos/dbus/service_constants.h>
#include <gtest/gtest.h>
#include "metrics/metrics_daemon.h"
#include "metrics/metrics_library_mock.h"
#include "metrics/persistent_integer_mock.h"
#include "metrics/vmlog_writer.h"
using base::FilePath;
using base::StringPrintf;
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
using std::string;
using std::vector;
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::StrictMock;
namespace chromeos_metrics {
TEST(VmlogWriterTest, ParseVmStats) {
const char kVmStats[] =
"pswpin 1345\n"
"pswpout 8896\n"
"foo 100\n"
"bar 200\n"
"pgmajfault 42\n"
"pgmajfault_a 3838\n"
"pgmajfault_f 66\n"
"etcetc 300\n";
std::istringstream input_stream(kVmStats);
struct VmstatRecord stats;
EXPECT_TRUE(VmStatsParseStats(&input_stream, &stats));
EXPECT_EQ(stats.page_faults_, 42);
EXPECT_EQ(stats.anon_page_faults_, 3838);
EXPECT_EQ(stats.file_page_faults_, 66);
EXPECT_EQ(stats.swap_in_, 1345);
EXPECT_EQ(stats.swap_out_, 8896);
}
TEST(VmlogWriterTest, ParseVmStatsOptionalMissing) {
const char kVmStats[] =
"pswpin 1345\n"
"pswpout 8896\n"
"foo 100\n"
"bar 200\n"
"pgmajfault 42\n"
// pgmajfault_a and pgmajfault_f are optional.
// The default value when missing is 0.
// "pgmajfault_a 3838\n"
// "pgmajfault_f 66\n"
"etcetc 300\n";
std::istringstream input_stream(kVmStats);
struct VmstatRecord stats;
EXPECT_TRUE(VmStatsParseStats(&input_stream, &stats));
EXPECT_EQ(stats.anon_page_faults_, 0);
EXPECT_EQ(stats.file_page_faults_, 0);
}
// For mocking sysfs info.
class TestGpuInfo : public GpuInfo {
public:
TestGpuInfo(std::unique_ptr<std::istream> gpu_info_stream,
GpuInfo::GpuType gpu_type)
: GpuInfo(std::move(gpu_info_stream), gpu_type) {}
};
TEST(VmlogWriterTest, ParseAmdgpuFrequency) {
const char kAmdgpuSclkFrequency[] =
"0: 200Mhz\n"
"1: 300Mhz\n"
"2: 400Mhz *\n"
"3: 480Mhz\n"
"4: 553Mhz\n"
"5: 626Mhz\n"
"6: 685Mhz\n"
"7: 720Mhz\n";
auto input_stream =
std::make_unique<std::istringstream>(kAmdgpuSclkFrequency);
std::stringstream selected_frequency;
TestGpuInfo gpu_info(std::move(input_stream), GpuInfo::GpuType::kAmd);
EXPECT_TRUE(gpu_info.GetCurrentFrequency(selected_frequency));
EXPECT_EQ(selected_frequency.str(), " 400");
}
TEST(VmlogWriterTest, ParseAmdgpuFrequencyMissing) {
const char kAmdgpuSclkFrequency[] =
"0: 200Mhz\n"
"1: 300Mhz\n"
"2: 400Mhz\n"
"3: 480Mhz\n"
"4: 553Mhz\n"
"5: 626Mhz\n"
"6: 685Mhz\n"
"7: 720Mhz\n";
auto input_stream =
std::make_unique<std::istringstream>(kAmdgpuSclkFrequency);
std::stringstream selected_frequency;
TestGpuInfo gpu_info(std::move(input_stream), GpuInfo::GpuType::kAmd);
EXPECT_FALSE(gpu_info.GetCurrentFrequency(selected_frequency));
EXPECT_EQ(selected_frequency.str(), "");
}
TEST(VmlogWriterTest, ParseIntelgpuFrequency) {
const char kIntelI915FrequencyInfo[] = R"(
PM IER=0x00000070 IMR=0xffffff8f ISR=0x00000000 IIR=0x00000000, MASK=0x00003fde
GT_PERF_STATUS: 0x00000000
Current freq: 100 MHz
Actual freq: 100 MHz
Idle freq: 100 MHz
Min freq: 100 MHz
)";
auto input_stream =
std::make_unique<std::istringstream>(kIntelI915FrequencyInfo);
std::stringstream selected_frequency;
TestGpuInfo gpu_info(std::move(input_stream), GpuInfo::GpuType::kIntel);
EXPECT_TRUE(gpu_info.GetCurrentFrequency(selected_frequency));
EXPECT_EQ(selected_frequency.str(), " 100");
}
TEST(VmlogWriterTest, ParseIntelgpuFrequencyMissing) {
const char kIntelI915FrequencyInfo[] = R"()";
auto input_stream =
std::make_unique<std::istringstream>(kIntelI915FrequencyInfo);
std::stringstream selected_frequency;
TestGpuInfo gpu_info(std::move(input_stream), GpuInfo::GpuType::kIntel);
EXPECT_FALSE(gpu_info.GetCurrentFrequency(selected_frequency));
EXPECT_EQ(selected_frequency.str(), "");
}
TEST(VmlogWriterTest, ParseCpuTime) {
const char kProcStat[] =
"cpu 9440559 4101628 4207468 764635735 5162045 0 132368 0 0 0";
std::istringstream input_stream(kProcStat);
struct CpuTimeRecord record;
EXPECT_TRUE(ParseCpuTime(&input_stream, &record));
EXPECT_EQ(record.non_idle_time_, 17882023);
EXPECT_EQ(record.total_time_, 787679803);
}
TEST(VmlogWriterTest, VmlogRotation) {
base::FilePath temp_directory;
EXPECT_TRUE(base::CreateNewTempDirectory("", &temp_directory));
base::FilePath log_path = temp_directory.Append("log");
base::FilePath rotated_path = temp_directory.Append("rotated");
base::FilePath latest_symlink_path = temp_directory.Append("vmlog.1.LATEST");
base::FilePath previous_symlink_path =
temp_directory.Append("vmlog.1.PREVIOUS");
// VmlogFile expects to create its output files.
base::DeleteFile(log_path);
base::DeleteFile(rotated_path);
std::string header_string("header\n");
VmlogFile l(log_path, rotated_path, 500, header_string);
EXPECT_FALSE(base::PathExists(latest_symlink_path));
std::string x_400(400, 'x');
EXPECT_TRUE(l.Write(x_400));
std::string buf;
EXPECT_TRUE(base::ReadFileToString(log_path, &buf));
EXPECT_EQ(header_string.size() + x_400.size(), buf.size());
EXPECT_FALSE(base::ReadFileToString(rotated_path, &buf));
std::string y_200(200, 'y');
EXPECT_TRUE(l.Write(y_200));
EXPECT_TRUE(base::ReadFileToString(log_path, &buf));
EXPECT_EQ(header_string.size() + y_200.size(), buf.size());
EXPECT_TRUE(base::ReadFileToString(rotated_path, &buf));
EXPECT_EQ(header_string.size() + x_400.size(), buf.size());
EXPECT_TRUE(base::PathExists(latest_symlink_path));
base::FilePath symlink_target;
EXPECT_TRUE(base::ReadSymbolicLink(latest_symlink_path, &symlink_target));
EXPECT_EQ(rotated_path.value(), symlink_target.value());
// Test log rotation for vmlog.1 files when a writer is created.
// We use a zero log_interval to prevent writes from happening.
EXPECT_TRUE(base::PathExists(latest_symlink_path));
EXPECT_FALSE(base::PathExists(previous_symlink_path));
VmlogWriter writer(temp_directory, base::TimeDelta());
EXPECT_FALSE(base::PathExists(latest_symlink_path));
EXPECT_TRUE(base::PathExists(previous_symlink_path));
EXPECT_TRUE(base::ReadSymbolicLink(previous_symlink_path, &symlink_target));
EXPECT_EQ(rotated_path.value(), symlink_target.value());
}
TEST(VmlogWriterTest, WriteCallbackSuccess) {
base::FilePath tempdir;
EXPECT_TRUE(base::CreateNewTempDirectory("", &tempdir));
// Create a VmlogWriter with a zero log_interval to avoid scheduling write
// callbacks.
VmlogWriter writer(tempdir, base::TimeDelta());
writer.WriteCallback();
EXPECT_TRUE(base::PathExists(writer.vmlog_->live_path_));
EXPECT_FALSE(base::PathExists(writer.vmlog_->rotated_path_));
}
TEST(VmlogWriterTest, GetOnlineCpus) {
const char kProcCpuInfo[] =
R"(processor : 0
vendor_id : GenuineIntel
model name : Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass
processor : 2
vendor_id : GenuineIntel
model name : Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass)";
std::optional<brillo::CpuInfo> c =
brillo::CpuInfo::CreateFromString(kProcCpuInfo);
EXPECT_TRUE(c.has_value());
auto cpus = GetOnlineCpus(c.value());
EXPECT_TRUE(cpus.has_value());
std::vector<int> expected_cpus = {0, 2};
EXPECT_EQ(*cpus, expected_cpus);
}
} // namespace chromeos_metrics