Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: base/trace_event/process_memory_maps_dump_provider_unittest.cc

Issue 1332943002: [tracing] Smaps provider uses fscanf instead of istream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using FILE*. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/process_memory_maps_dump_provider.h" 5 #include "base/trace_event/process_memory_maps_dump_provider.h"
6 6
7 #include <fstream> 7 #include <fcntl.h>
Primiano Tucci (use gerrit) 2015/09/30 15:36:35 this can only be included on linux/android. move
ssid 2015/10/02 17:07:20 Done.
8 #include <sstream>
9 8
9 #include "base/files/file_util.h"
10 #include "base/trace_event/process_memory_dump.h" 10 #include "base/trace_event/process_memory_dump.h"
11 #include "base/trace_event/process_memory_maps.h" 11 #include "base/trace_event/process_memory_maps.h"
12 #include "base/trace_event/trace_event_argument.h" 12 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace base { 15 namespace base {
16 namespace trace_event { 16 namespace trace_event {
17 17
18 #if defined(OS_LINUX) || defined(OS_ANDROID) 18 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 namespace { 19 namespace {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 "Private_Clean: 8 kB\n" 97 "Private_Clean: 8 kB\n"
98 "Private_Dirty: 4 kB\n" 98 "Private_Dirty: 4 kB\n"
99 "Referenced: 40 kB\n" 99 "Referenced: 40 kB\n"
100 "Anonymous: 16 kB\n" 100 "Anonymous: 16 kB\n"
101 "AnonHugePages: 0 kB\n" 101 "AnonHugePages: 0 kB\n"
102 "Swap: 0 kB\n" 102 "Swap: 0 kB\n"
103 "KernelPageSize: 4 kB\n" 103 "KernelPageSize: 4 kB\n"
104 "MMUPageSize: 4 kB\n" 104 "MMUPageSize: 4 kB\n"
105 "Locked: 0 kB\n" 105 "Locked: 0 kB\n"
106 "VmFlags: rd wr mr mw me ac sd\n"; 106 "VmFlags: rd wr mr mw me ac sd\n";
107
108 void CreateAndSetSmapsFileForTesting(const char* smaps_string,
109 ScopedFILE& file) {
110 FilePath temp_path;
111 FILE* temp_file = CreateAndOpenTemporaryFile(&temp_path);
112 file.reset(temp_file);
113 ASSERT_TRUE(temp_file);
114
115 ASSERT_TRUE(base::WriteFileDescriptor(fileno(temp_file), smaps_string,
116 strlen(smaps_string)));
117 }
118
107 } // namespace 119 } // namespace
108 120
109 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) { 121 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
110 const uint32 kProtR = ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; 122 const uint32 kProtR = ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
111 const uint32 kProtW = ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; 123 const uint32 kProtW = ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
112 const uint32 kProtX = ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; 124 const uint32 kProtX = ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
113 const MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED}; 125 const MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::DETAILED};
114 126
115 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance(); 127 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance();
116 128
117 // Emulate a non-existent /proc/self/smaps. 129 // Emulate an empty /proc/self/smaps.
118 ProcessMemoryDump pmd_invalid(nullptr /* session_state */); 130 ProcessMemoryDump pmd_invalid(nullptr /* session_state */);
119 std::ifstream non_existent_file("/tmp/does-not-exist"); 131 ScopedFILE empty_file(OpenFile(FilePath("/dev/null"), "r"));
120 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file; 132 ASSERT_TRUE(empty_file.get());
121 CHECK_EQ(false, non_existent_file.good()); 133 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = empty_file.get();
122 pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
123 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
124
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); 134 pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
130 ASSERT_FALSE(pmd_invalid.has_process_mmaps()); 135 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
131 136
132 // Parse the 1st smaps file. 137 // Parse the 1st smaps file.
133 ProcessMemoryDump pmd_1(nullptr /* session_state */); 138 ProcessMemoryDump pmd_1(nullptr /* session_state */);
134 std::istringstream test_smaps_1(kTestSmaps1); 139 ScopedFILE temp_file1;
135 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1; 140 CreateAndSetSmapsFileForTesting(kTestSmaps1, temp_file1);
141 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = temp_file1.get();
136 pmmdp->OnMemoryDump(dump_args, &pmd_1); 142 pmmdp->OnMemoryDump(dump_args, &pmd_1);
137 ASSERT_TRUE(pmd_1.has_process_mmaps()); 143 ASSERT_TRUE(pmd_1.has_process_mmaps());
138 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions(); 144 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
139 ASSERT_EQ(2UL, regions_1.size()); 145 ASSERT_EQ(2UL, regions_1.size());
140 146
141 EXPECT_EQ(0x00400000UL, regions_1[0].start_address); 147 EXPECT_EQ(0x00400000UL, regions_1[0].start_address);
142 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes); 148 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes);
143 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags); 149 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags);
144 EXPECT_EQ("/file/1", regions_1[0].mapped_file); 150 EXPECT_EQ("/file/1", regions_1[0].mapped_file);
145 EXPECT_EQ(162 * 1024UL, regions_1[0].byte_stats_proportional_resident); 151 EXPECT_EQ(162 * 1024UL, regions_1[0].byte_stats_proportional_resident);
146 EXPECT_EQ(228 * 1024UL, regions_1[0].byte_stats_shared_clean_resident); 152 EXPECT_EQ(228 * 1024UL, regions_1[0].byte_stats_shared_clean_resident);
147 EXPECT_EQ(0UL, regions_1[0].byte_stats_shared_dirty_resident); 153 EXPECT_EQ(0UL, regions_1[0].byte_stats_shared_dirty_resident);
148 EXPECT_EQ(0UL, regions_1[0].byte_stats_private_clean_resident); 154 EXPECT_EQ(0UL, regions_1[0].byte_stats_private_clean_resident);
149 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_private_dirty_resident); 155 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_private_dirty_resident);
150 EXPECT_EQ(4 * 1024UL, regions_1[0].byte_stats_swapped); 156 EXPECT_EQ(4 * 1024UL, regions_1[0].byte_stats_swapped);
151 157
152 EXPECT_EQ(0xff000000UL, regions_1[1].start_address); 158 EXPECT_EQ(0xff000000UL, regions_1[1].start_address);
153 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes); 159 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes);
154 EXPECT_EQ(kProtW, regions_1[1].protection_flags); 160 EXPECT_EQ(kProtW, regions_1[1].protection_flags);
155 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file); 161 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file);
156 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_proportional_resident); 162 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_proportional_resident);
157 EXPECT_EQ(120 * 1024UL, regions_1[1].byte_stats_shared_clean_resident); 163 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); 164 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); 165 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); 166 EXPECT_EQ(8 * 1024UL, regions_1[1].byte_stats_private_dirty_resident);
161 EXPECT_EQ(0 * 1024UL, regions_1[1].byte_stats_swapped); 167 EXPECT_EQ(0 * 1024UL, regions_1[1].byte_stats_swapped);
162 168
163 // Parse the 2nd smaps file. 169 // Parse the 2nd smaps file.
164 ProcessMemoryDump pmd_2(nullptr /* session_state */); 170 ProcessMemoryDump pmd_2(nullptr /* session_state */);
165 std::istringstream test_smaps_2(kTestSmaps2); 171 ScopedFILE temp_file2;
166 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2; 172 CreateAndSetSmapsFileForTesting(kTestSmaps2, temp_file2);
173 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = temp_file2.get();
167 pmmdp->OnMemoryDump(dump_args, &pmd_2); 174 pmmdp->OnMemoryDump(dump_args, &pmd_2);
168 ASSERT_TRUE(pmd_2.has_process_mmaps()); 175 ASSERT_TRUE(pmd_2.has_process_mmaps());
169 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions(); 176 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();
170 ASSERT_EQ(1UL, regions_2.size()); 177 ASSERT_EQ(1UL, regions_2.size());
171 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address); 178 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address);
172 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes); 179 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes);
173 EXPECT_EQ(0U, regions_2[0].protection_flags); 180 EXPECT_EQ(0U, regions_2[0].protection_flags);
174 EXPECT_EQ("", regions_2[0].mapped_file); 181 EXPECT_EQ("", regions_2[0].mapped_file);
175 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident); 182 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident);
176 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_shared_clean_resident); 183 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); 184 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); 185 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); 186 EXPECT_EQ(4 * 1024UL, regions_2[0].byte_stats_private_dirty_resident);
180 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped); 187 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped);
181 } 188 }
182 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 189 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
183 190
184 } // namespace trace_event 191 } // namespace trace_event
185 } // namespace base 192 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698