Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / trace_event / process_memory_maps_dump_provider_unittest.cc
blobf951a09fb8a2aaf84a290a96ece8bf0db26a3518
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/trace_event/process_memory_maps_dump_provider.h"
7 #include <fstream>
8 #include <sstream>
10 #include "base/trace_event/process_memory_dump.h"
11 #include "base/trace_event/process_memory_maps.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
16 namespace trace_event {
18 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 namespace {
20 const char kTestSmaps1[] =
21 "00400000-004be000 r-xp 00000000 fc:01 1234 /file/1\n"
22 "Size: 760 kB\n"
23 "Rss: 296 kB\n"
24 "Pss: 162 kB\n"
25 "Shared_Clean: 228 kB\n"
26 "Shared_Dirty: 0 kB\n"
27 "Private_Clean: 0 kB\n"
28 "Private_Dirty: 68 kB\n"
29 "Referenced: 296 kB\n"
30 "Anonymous: 68 kB\n"
31 "AnonHugePages: 0 kB\n"
32 "Swap: 4 kB\n"
33 "KernelPageSize: 4 kB\n"
34 "MMUPageSize: 4 kB\n"
35 "Locked: 0 kB\n"
36 "VmFlags: rd ex mr mw me dw sd\n"
37 "ff000000-ff800000 -w-p 00001080 fc:01 0 /file/name with space\n"
38 "Size: 0 kB\n"
39 "Rss: 192 kB\n"
40 "Pss: 128 kB\n"
41 "Shared_Clean: 120 kB\n"
42 "Shared_Dirty: 4 kB\n"
43 "Private_Clean: 60 kB\n"
44 "Private_Dirty: 8 kB\n"
45 "Referenced: 296 kB\n"
46 "Anonymous: 0 kB\n"
47 "AnonHugePages: 0 kB\n"
48 "Swap: 0 kB\n"
49 "KernelPageSize: 4 kB\n"
50 "MMUPageSize: 4 kB\n"
51 "Locked: 0 kB\n"
52 "VmFlags: rd ex mr mw me dw sd";
54 const char kTestSmaps2[] =
55 // An invalid region, with zero size and overlapping with the last one
56 // (See crbug.com/461237).
57 "7fe7ce79c000-7fe7ce79c000 ---p 00000000 00:00 0 \n"
58 "Size: 4 kB\n"
59 "Rss: 0 kB\n"
60 "Pss: 0 kB\n"
61 "Shared_Clean: 0 kB\n"
62 "Shared_Dirty: 0 kB\n"
63 "Private_Clean: 0 kB\n"
64 "Private_Dirty: 0 kB\n"
65 "Referenced: 0 kB\n"
66 "Anonymous: 0 kB\n"
67 "AnonHugePages: 0 kB\n"
68 "Swap: 0 kB\n"
69 "KernelPageSize: 4 kB\n"
70 "MMUPageSize: 4 kB\n"
71 "Locked: 0 kB\n"
72 "VmFlags: rd ex mr mw me dw sd\n"
73 // A invalid region with its range going backwards.
74 "00400000-00200000 ---p 00000000 00:00 0 \n"
75 "Size: 4 kB\n"
76 "Rss: 0 kB\n"
77 "Pss: 0 kB\n"
78 "Shared_Clean: 0 kB\n"
79 "Shared_Dirty: 0 kB\n"
80 "Private_Clean: 0 kB\n"
81 "Private_Dirty: 0 kB\n"
82 "Referenced: 0 kB\n"
83 "Anonymous: 0 kB\n"
84 "AnonHugePages: 0 kB\n"
85 "Swap: 0 kB\n"
86 "KernelPageSize: 4 kB\n"
87 "MMUPageSize: 4 kB\n"
88 "Locked: 0 kB\n"
89 "VmFlags: rd ex mr mw me dw sd\n"
90 // A good anonymous region at the end.
91 "7fe7ce79c000-7fe7ce7a8000 ---p 00000000 00:00 0 \n"
92 "Size: 48 kB\n"
93 "Rss: 40 kB\n"
94 "Pss: 32 kB\n"
95 "Shared_Clean: 16 kB\n"
96 "Shared_Dirty: 12 kB\n"
97 "Private_Clean: 8 kB\n"
98 "Private_Dirty: 4 kB\n"
99 "Referenced: 40 kB\n"
100 "Anonymous: 16 kB\n"
101 "AnonHugePages: 0 kB\n"
102 "Swap: 0 kB\n"
103 "KernelPageSize: 4 kB\n"
104 "MMUPageSize: 4 kB\n"
105 "Locked: 0 kB\n"
106 "VmFlags: rd wr mr mw me ac sd\n";
107 } // namespace
109 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
110 const uint32 kProtR = ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
111 const uint32 kProtW = ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
112 const uint32 kProtX = ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
113 const MemoryDumpArgs dump_args = {MemoryDumpArgs::LevelOfDetail::HIGH};
115 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance();
117 // Emulate a non-existent /proc/self/smaps.
118 ProcessMemoryDump pmd_invalid(nullptr /* session_state */);
119 std::ifstream non_existent_file("/tmp/does-not-exist");
120 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file;
121 CHECK_EQ(false, non_existent_file.good());
122 pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
123 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
125 // Emulate an empty /proc/self/smaps.
126 std::ifstream empty_file("/dev/null");
127 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file;
128 CHECK_EQ(true, empty_file.good());
129 pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
130 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
132 // Parse the 1st smaps file.
133 ProcessMemoryDump pmd_1(nullptr /* session_state */);
134 std::istringstream test_smaps_1(kTestSmaps1);
135 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1;
136 pmmdp->OnMemoryDump(dump_args, &pmd_1);
137 ASSERT_TRUE(pmd_1.has_process_mmaps());
138 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
139 ASSERT_EQ(2UL, regions_1.size());
141 EXPECT_EQ(0x00400000UL, regions_1[0].start_address);
142 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes);
143 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags);
144 EXPECT_EQ("/file/1", regions_1[0].mapped_file);
145 EXPECT_EQ(162 * 1024UL, regions_1[0].byte_stats_proportional_resident);
146 EXPECT_EQ(228 * 1024UL, regions_1[0].byte_stats_shared_clean_resident);
147 EXPECT_EQ(0UL, regions_1[0].byte_stats_shared_dirty_resident);
148 EXPECT_EQ(0UL, regions_1[0].byte_stats_private_clean_resident);
149 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_private_dirty_resident);
150 EXPECT_EQ(4 * 1024UL, regions_1[0].byte_stats_swapped);
152 EXPECT_EQ(0xff000000UL, regions_1[1].start_address);
153 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes);
154 EXPECT_EQ(kProtW, regions_1[1].protection_flags);
155 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file);
156 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_proportional_resident);
157 EXPECT_EQ(120 * 1024UL, regions_1[1].byte_stats_shared_clean_resident);
158 EXPECT_EQ(4 * 1024UL, regions_1[1].byte_stats_shared_dirty_resident);
159 EXPECT_EQ(60 * 1024UL, regions_1[1].byte_stats_private_clean_resident);
160 EXPECT_EQ(8 * 1024UL, regions_1[1].byte_stats_private_dirty_resident);
161 EXPECT_EQ(0 * 1024UL, regions_1[1].byte_stats_swapped);
163 // Parse the 2nd smaps file.
164 ProcessMemoryDump pmd_2(nullptr /* session_state */);
165 std::istringstream test_smaps_2(kTestSmaps2);
166 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2;
167 pmmdp->OnMemoryDump(dump_args, &pmd_2);
168 ASSERT_TRUE(pmd_2.has_process_mmaps());
169 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();
170 ASSERT_EQ(1UL, regions_2.size());
171 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address);
172 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes);
173 EXPECT_EQ(0U, regions_2[0].protection_flags);
174 EXPECT_EQ("", regions_2[0].mapped_file);
175 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident);
176 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_shared_clean_resident);
177 EXPECT_EQ(12 * 1024UL, regions_2[0].byte_stats_shared_dirty_resident);
178 EXPECT_EQ(8 * 1024UL, regions_2[0].byte_stats_private_clean_resident);
179 EXPECT_EQ(4 * 1024UL, regions_2[0].byte_stats_private_dirty_resident);
180 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped);
182 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
184 } // namespace trace_event
185 } // namespace base