Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / trace_event / process_memory_maps_dump_provider_unittest.cc
blob5416e119960f790858386de5bafb167d549da6fe
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;
114 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance();
116 // Emulate a non-existent /proc/self/smaps.
117 ProcessMemoryDump pmd_invalid(nullptr /* session_state */);
118 std::ifstream non_existent_file("/tmp/does-not-exist");
119 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file;
120 CHECK_EQ(false, non_existent_file.good());
121 pmmdp->OnMemoryDump(&pmd_invalid);
122 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
124 // Emulate an empty /proc/self/smaps.
125 std::ifstream empty_file("/dev/null");
126 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file;
127 CHECK_EQ(true, empty_file.good());
128 pmmdp->OnMemoryDump(&pmd_invalid);
129 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
131 // Parse the 1st smaps file.
132 ProcessMemoryDump pmd_1(nullptr /* session_state */);
133 std::istringstream test_smaps_1(kTestSmaps1);
134 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1;
135 pmmdp->OnMemoryDump(&pmd_1);
136 ASSERT_TRUE(pmd_1.has_process_mmaps());
137 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
138 ASSERT_EQ(2UL, regions_1.size());
140 EXPECT_EQ(0x00400000UL, regions_1[0].start_address);
141 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes);
142 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags);
143 EXPECT_EQ("/file/1", regions_1[0].mapped_file);
144 EXPECT_EQ(162 * 1024UL, regions_1[0].byte_stats_proportional_resident);
145 EXPECT_EQ(228 * 1024UL, regions_1[0].byte_stats_shared_clean_resident);
146 EXPECT_EQ(0UL, regions_1[0].byte_stats_shared_dirty_resident);
147 EXPECT_EQ(0UL, regions_1[0].byte_stats_private_clean_resident);
148 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_private_dirty_resident);
149 EXPECT_EQ(4 * 1024UL, regions_1[0].byte_stats_swapped);
151 EXPECT_EQ(0xff000000UL, regions_1[1].start_address);
152 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes);
153 EXPECT_EQ(kProtW, regions_1[1].protection_flags);
154 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file);
155 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_proportional_resident);
156 EXPECT_EQ(120 * 1024UL, regions_1[1].byte_stats_shared_clean_resident);
157 EXPECT_EQ(4 * 1024UL, regions_1[1].byte_stats_shared_dirty_resident);
158 EXPECT_EQ(60 * 1024UL, regions_1[1].byte_stats_private_clean_resident);
159 EXPECT_EQ(8 * 1024UL, regions_1[1].byte_stats_private_dirty_resident);
160 EXPECT_EQ(0 * 1024UL, regions_1[1].byte_stats_swapped);
162 // Parse the 2nd smaps file.
163 ProcessMemoryDump pmd_2(nullptr /* session_state */);
164 std::istringstream test_smaps_2(kTestSmaps2);
165 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2;
166 pmmdp->OnMemoryDump(&pmd_2);
167 ASSERT_TRUE(pmd_2.has_process_mmaps());
168 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();
169 ASSERT_EQ(1UL, regions_2.size());
170 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address);
171 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes);
172 EXPECT_EQ(0U, regions_2[0].protection_flags);
173 EXPECT_EQ("", regions_2[0].mapped_file);
174 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident);
175 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_shared_clean_resident);
176 EXPECT_EQ(12 * 1024UL, regions_2[0].byte_stats_shared_dirty_resident);
177 EXPECT_EQ(8 * 1024UL, regions_2[0].byte_stats_private_clean_resident);
178 EXPECT_EQ(4 * 1024UL, regions_2[0].byte_stats_private_dirty_resident);
179 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped);
181 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
183 } // namespace trace_event
184 } // namespace base