Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 16 | |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 19 | #include <base/at_exit.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 20 | #include <base/files/file_util.h> |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 21 | #include <base/files/scoped_temp_dir.h> |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 22 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 23 | #include <brillo/flag_helper.h> |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 26 | #include "constants.h" |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 27 | #include "metrics_collector.h" |
Samuel Tan | 28a78b7 | 2015-09-23 14:39:35 -0700 | [diff] [blame] | 28 | #include "metrics/metrics_library_mock.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 29 | #include "persistent_integer_mock.h" |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 30 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 31 | using base::FilePath; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 32 | using base::TimeDelta; |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 33 | using std::string; |
| 34 | using std::vector; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 35 | using ::testing::_; |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 36 | using ::testing::AnyNumber; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 37 | using ::testing::AtLeast; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 38 | using ::testing::Return; |
| 39 | using ::testing::StrictMock; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 40 | using chromeos_metrics::PersistentIntegerMock; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 41 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 42 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 43 | class MetricsCollectorTest : public testing::Test { |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 44 | protected: |
| 45 | virtual void SetUp() { |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 46 | brillo::FlagHelper::Init(0, nullptr, ""); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 48 | daemon_.Init(true, &metrics_lib_, "", temp_dir_.path()); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 51 | // Adds a metrics library mock expectation that the specified metric |
| 52 | // will be generated. |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 53 | void ExpectSample(const std::string& name, int sample) { |
| 54 | EXPECT_CALL(metrics_lib_, SendToUMA(name, sample, _, _, _)) |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 55 | .Times(1) |
| 56 | .WillOnce(Return(true)) |
| 57 | .RetiresOnSaturation(); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 60 | // Creates a new DBus signal message with zero or more string arguments. |
| 61 | // The message can be deallocated through DeleteDBusMessage. |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 62 | // |
| 63 | // |path| is the object emitting the signal. |
| 64 | // |interface| is the interface the signal is emitted from. |
| 65 | // |name| is the name of the signal. |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 66 | // |arg_values| contains the values of the string arguments. |
| 67 | DBusMessage* NewDBusSignalString(const string& path, |
| 68 | const string& interface, |
| 69 | const string& name, |
| 70 | const vector<string>& arg_values) { |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 71 | DBusMessage* msg = dbus_message_new_signal(path.c_str(), |
| 72 | interface.c_str(), |
| 73 | name.c_str()); |
| 74 | DBusMessageIter iter; |
| 75 | dbus_message_iter_init_append(msg, &iter); |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 76 | for (vector<string>::const_iterator it = arg_values.begin(); |
| 77 | it != arg_values.end(); ++it) { |
| 78 | const char* str_value = it->c_str(); |
| 79 | dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value); |
| 80 | } |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 81 | return msg; |
| 82 | } |
| 83 | |
| 84 | // Deallocates the DBus message |msg| previously allocated through |
| 85 | // dbus_message_new*. |
| 86 | void DeleteDBusMessage(DBusMessage* msg) { |
| 87 | dbus_message_unref(msg); |
| 88 | } |
| 89 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 90 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 91 | // Creates or overwrites the file in |path| so that it contains the printable |
| 92 | // representation of |value|. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 93 | void CreateUint64ValueFile(const base::FilePath& path, uint64_t value) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 94 | std::string value_string = base::Uint64ToString(value); |
| 95 | ASSERT_EQ(value_string.length(), |
| 96 | base::WriteFile(path, value_string.c_str(), |
| 97 | value_string.length())); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 100 | // The MetricsCollector under test. |
| 101 | MetricsCollector daemon_; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 102 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 103 | // Temporary directory used for tests. |
| 104 | base::ScopedTempDir temp_dir_; |
| 105 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 106 | // Mocks. They are strict mock so that all unexpected |
| 107 | // calls are marked as failures. |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 108 | StrictMock<MetricsLibraryMock> metrics_lib_; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 111 | TEST_F(MetricsCollectorTest, MessageFilter) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 112 | // Ignore calls to SendToUMA. |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 113 | EXPECT_CALL(metrics_lib_, SendToUMA(_, _, _, _, _)).Times(AnyNumber()); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 114 | |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 115 | DBusMessage* msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL); |
| 116 | DBusHandlerResult res = |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 117 | MetricsCollector::MessageFilter(/* connection */ nullptr, msg, &daemon_); |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 118 | EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); |
| 119 | DeleteDBusMessage(msg); |
| 120 | |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 121 | vector<string> signal_args; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 122 | msg = NewDBusSignalString("/", |
| 123 | "org.chromium.CrashReporter", |
| 124 | "UserCrash", |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 125 | signal_args); |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 126 | res = MetricsCollector::MessageFilter(/* connection */ nullptr, msg, &daemon_); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 127 | EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res); |
| 128 | DeleteDBusMessage(msg); |
| 129 | |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 130 | signal_args.clear(); |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 131 | signal_args.push_back("randomstate"); |
| 132 | signal_args.push_back("bob"); // arbitrary username |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 133 | msg = NewDBusSignalString("/", |
| 134 | "org.chromium.UnknownService.Manager", |
| 135 | "StateChanged", |
Daniel Erat | b9b05e6 | 2010-11-09 12:18:51 -0800 | [diff] [blame] | 136 | signal_args); |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 137 | res = MetricsCollector::MessageFilter(/* connection */ nullptr, msg, &daemon_); |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 138 | EXPECT_EQ(DBUS_HANDLER_RESULT_NOT_YET_HANDLED, res); |
| 139 | DeleteDBusMessage(msg); |
| 140 | } |
| 141 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 142 | TEST_F(MetricsCollectorTest, SendSample) { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 143 | ExpectSample("Dummy.Metric", 3); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 144 | daemon_.SendSample("Dummy.Metric", /* sample */ 3, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 145 | /* min */ 1, /* max */ 100, /* buckets */ 50); |
| 146 | } |
| 147 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 148 | TEST_F(MetricsCollectorTest, ProcessMeminfo) { |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 149 | string meminfo = |
| 150 | "MemTotal: 2000000 kB\nMemFree: 500000 kB\n" |
| 151 | "Buffers: 1000000 kB\nCached: 213652 kB\n" |
| 152 | "SwapCached: 0 kB\nActive: 133400 kB\n" |
| 153 | "Inactive: 183396 kB\nActive(anon): 92984 kB\n" |
| 154 | "Inactive(anon): 58860 kB\nActive(file): 40416 kB\n" |
| 155 | "Inactive(file): 124536 kB\nUnevictable: 0 kB\n" |
| 156 | "Mlocked: 0 kB\nSwapTotal: 0 kB\n" |
| 157 | "SwapFree: 0 kB\nDirty: 40 kB\n" |
| 158 | "Writeback: 0 kB\nAnonPages: 92652 kB\n" |
| 159 | "Mapped: 59716 kB\nShmem: 59196 kB\n" |
| 160 | "Slab: 16656 kB\nSReclaimable: 6132 kB\n" |
| 161 | "SUnreclaim: 10524 kB\nKernelStack: 1648 kB\n" |
| 162 | "PageTables: 2780 kB\nNFS_Unstable: 0 kB\n" |
| 163 | "Bounce: 0 kB\nWritebackTmp: 0 kB\n" |
| 164 | "CommitLimit: 970656 kB\nCommitted_AS: 1260528 kB\n" |
| 165 | "VmallocTotal: 122880 kB\nVmallocUsed: 12144 kB\n" |
| 166 | "VmallocChunk: 103824 kB\nDirectMap4k: 9636 kB\n" |
| 167 | "DirectMap2M: 1955840 kB\n"; |
| 168 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 169 | // All enum calls must report percents. |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 170 | EXPECT_CALL(metrics_lib_, SendEnumToUMA(_, _, 100)).Times(AtLeast(1)); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 171 | // Check that MemFree is correctly computed at 25%. |
| 172 | EXPECT_CALL(metrics_lib_, SendEnumToUMA("Platform.MeminfoMemFree", 25, 100)) |
| 173 | .Times(AtLeast(1)); |
| 174 | // Check that we call SendToUma at least once (log histogram). |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 175 | EXPECT_CALL(metrics_lib_, SendToUMA(_, _, _, _, _)) |
| 176 | .Times(AtLeast(1)); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 177 | // Make sure we don't report fields not in the list. |
| 178 | EXPECT_CALL(metrics_lib_, SendToUMA("Platform.MeminfoMlocked", _, _, _, _)) |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 179 | .Times(0); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 180 | EXPECT_CALL(metrics_lib_, SendEnumToUMA("Platform.MeminfoMlocked", _, _)) |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 181 | .Times(0); |
| 182 | EXPECT_TRUE(daemon_.ProcessMeminfo(meminfo)); |
| 183 | } |
| 184 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 185 | TEST_F(MetricsCollectorTest, ProcessMeminfo2) { |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 186 | string meminfo = "MemTotal: 2000000 kB\nMemFree: 1000000 kB\n"; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 187 | // Not enough fields. |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 188 | EXPECT_FALSE(daemon_.ProcessMeminfo(meminfo)); |
| 189 | } |
| 190 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 191 | TEST_F(MetricsCollectorTest, SendZramMetrics) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(daemon_.testing_); |
| 193 | |
| 194 | // |compr_data_size| is the size in bytes of compressed data. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 195 | const uint64_t compr_data_size = 50 * 1000 * 1000; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 196 | // The constant '3' is a realistic but random choice. |
| 197 | // |orig_data_size| does not include zero pages. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 198 | const uint64_t orig_data_size = compr_data_size * 3; |
| 199 | const uint64_t page_size = 4096; |
| 200 | const uint64_t zero_pages = 10 * 1000 * 1000 / page_size; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 201 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 202 | CreateUint64ValueFile( |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 203 | temp_dir_.path().Append(MetricsCollector::kComprDataSizeName), |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 204 | compr_data_size); |
| 205 | CreateUint64ValueFile( |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 206 | temp_dir_.path().Append(MetricsCollector::kOrigDataSizeName), |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 207 | orig_data_size); |
| 208 | CreateUint64ValueFile( |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 209 | temp_dir_.path().Append(MetricsCollector::kZeroPagesName), zero_pages); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 210 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 211 | const uint64_t real_orig_size = orig_data_size + zero_pages * page_size; |
| 212 | const uint64_t zero_ratio_percent = |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 213 | zero_pages * page_size * 100 / real_orig_size; |
| 214 | // Ratio samples are in percents. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 215 | const uint64_t actual_ratio_sample = real_orig_size * 100 / compr_data_size; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 216 | |
| 217 | EXPECT_CALL(metrics_lib_, SendToUMA(_, compr_data_size >> 20, _, _, _)); |
| 218 | EXPECT_CALL(metrics_lib_, |
| 219 | SendToUMA(_, (real_orig_size - compr_data_size) >> 20, _, _, _)); |
| 220 | EXPECT_CALL(metrics_lib_, SendToUMA(_, actual_ratio_sample, _, _, _)); |
| 221 | EXPECT_CALL(metrics_lib_, SendToUMA(_, zero_pages, _, _, _)); |
| 222 | EXPECT_CALL(metrics_lib_, SendToUMA(_, zero_ratio_percent, _, _, _)); |
| 223 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(daemon_.ReportZram(temp_dir_.path())); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 225 | } |