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

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

Issue 951463002: [tracing] Add memory maps dumper impl for Linux/Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mmaps_skeleton
Patch Set: dsinclair nits Created 5 years, 10 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 <fstream>
8 #include <sstream> 8 #include <sstream>
9 9
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 {
16 const char kTestSmaps1[] =
17 "00400000-004be000 r-xp 00000000 fc:01 1234 /file/1\n"
18 "Size: 760 kB\n"
19 "Rss: 296 kB\n"
20 "Pss: 162 kB\n"
21 "Shared_Clean: 228 kB\n"
22 "Shared_Dirty: 0 kB\n"
23 "Private_Clean: 0 kB\n"
24 "Private_Dirty: 68 kB\n"
25 "Referenced: 296 kB\n"
26 "Anonymous: 68 kB\n"
27 "AnonHugePages: 0 kB\n"
28 "Swap: 0 kB\n"
29 "KernelPageSize: 4 kB\n"
30 "MMUPageSize: 4 kB\n"
31 "Locked: 0 kB\n"
32 "VmFlags: rd ex mr mw me dw sd\n"
33 "ff000000-ff800000 -w-p 00001080 fc:01 0 /file/name with space\n"
34 "Size: 0 kB\n"
35 "Rss: 128 kB\n"
36 "Pss: 128 kB\n"
37 "Shared_Clean: 124 kB\n"
38 "Shared_Dirty: 0 kB\n"
39 "Private_Clean: 68 kB\n"
40 "Private_Dirty: 0 kB\n"
41 "Referenced: 296 kB\n"
42 "Anonymous: 0 kB\n"
43 "AnonHugePages: 0 kB\n"
44 "Swap: 0 kB\n"
45 "KernelPageSize: 4 kB\n"
46 "MMUPageSize: 4 kB\n"
47 "Locked: 0 kB\n"
48 "VmFlags: rd ex mr mw me dw sd";
49
50 const char kTestSmaps2[] =
51 "7fe7ce79c000-7fe7ce7a8000 ---p 00000000 00:00 0 \n" // An anonymous vma.
52 "Size: 48 kB\n"
53 "Rss: 40 kB\n"
54 "Pss: 0 kB\n"
55 "Shared_Clean: 16 kB\n"
56 "Shared_Dirty: 12 kB\n"
57 "Private_Clean: 8 kB\n"
58 "Private_Dirty: 4 kB\n"
59 "Referenced: 40 kB\n"
60 "Anonymous: 16 kB\n"
61 "AnonHugePages: 0 kB\n"
62 "Swap: 0 kB\n"
63 "KernelPageSize: 4 kB\n"
64 "MMUPageSize: 4 kB\n"
65 "Locked: 0 kB\n"
66 "VmFlags: rd wr mr mw me ac sd";
67 } // namespace
Sami 2015/02/24 11:11:25 Could you add a couple of malformed examples (empt
Primiano Tucci (use gerrit) 2015/02/24 13:17:07 Done for negative zero. Empty file, instead, is a
68
15 namespace base { 69 namespace base {
16 namespace trace_event { 70 namespace trace_event {
17 71
18 #if defined(OS_LINUX) || defined(OS_ANDROID) 72 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) { 73 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
74 const uint32 kProtR = ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
75 const uint32 kProtW = ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
76 const uint32 kProtX = ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
77
20 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance(); 78 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance();
21 79
22 // Emulate a non-existent /proc/self/smaps. 80 // Emulate a non-existent /proc/self/smaps.
23 ProcessMemoryDump pmd_invalid; 81 ProcessMemoryDump pmd_invalid;
24 std::ifstream non_existent_file("/tmp/does-not-exist"); 82 std::ifstream non_existent_file("/tmp/does-not-exist");
25 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file; 83 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file;
26 CHECK_EQ(false, non_existent_file.good()); 84 CHECK_EQ(false, non_existent_file.good());
27 pmmdp->DumpInto(&pmd_invalid); 85 pmmdp->DumpInto(&pmd_invalid);
28 ASSERT_FALSE(pmd_invalid.has_process_mmaps()); 86 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
29 87
30 // Emulate an empty /proc/self/smaps. 88 // Emulate an empty /proc/self/smaps.
31 std::ifstream empty_file("/dev/null"); 89 std::ifstream empty_file("/dev/null");
32 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file; 90 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file;
33 CHECK_EQ(true, empty_file.good()); 91 CHECK_EQ(true, empty_file.good());
34 pmmdp->DumpInto(&pmd_invalid); 92 pmmdp->DumpInto(&pmd_invalid);
35 ASSERT_FALSE(pmd_invalid.has_process_mmaps()); 93 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
36 94
37 // TODO(primiano): in the next CLs add the code to parse some actual smaps 95 // Parse the 1st smaps file.
38 // files and check that the parsing logic of the dump provider holds. 96 ProcessMemoryDump pmd_1;
97 std::istringstream test_smaps_1(kTestSmaps1);
98 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1;
99 pmmdp->DumpInto(&pmd_1);
100 ASSERT_TRUE(pmd_1.has_process_mmaps());
101 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
102 ASSERT_EQ(2UL, regions_1.size());
103
104 EXPECT_EQ(0x00400000UL, regions_1[0].start_address);
105 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes);
106 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags);
107 EXPECT_EQ("/file/1", regions_1[0].mapped_file);
108 EXPECT_EQ(0UL, regions_1[0].mapped_file_offset);
109 EXPECT_EQ(296 * 1024UL, regions_1[0].byte_stats_resident);
110 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_anonymous);
111
112 EXPECT_EQ(0xff000000UL, regions_1[1].start_address);
113 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes);
114 EXPECT_EQ(kProtW, regions_1[1].protection_flags);
115 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file);
116 EXPECT_EQ(0x00001080UL, regions_1[1].mapped_file_offset);
117 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_resident);
118 EXPECT_EQ(0UL, regions_1[1].byte_stats_anonymous);
119
120 // Parse the 2nd smaps file.
121 ProcessMemoryDump pmd_2;
122 std::istringstream test_smaps_2(kTestSmaps2);
123 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2;
124 pmmdp->DumpInto(&pmd_2);
125 ASSERT_TRUE(pmd_2.has_process_mmaps());
126 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();
127 ASSERT_EQ(1UL, regions_2.size());
128 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address);
129 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes);
130 EXPECT_EQ(0U, regions_2[0].protection_flags);
131 EXPECT_EQ("", regions_2[0].mapped_file);
132 EXPECT_EQ(0UL, regions_2[0].mapped_file_offset);
133 EXPECT_EQ(40 * 1024UL, regions_2[0].byte_stats_resident);
134 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_anonymous);
39 } 135 }
40 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 136 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
41 137
42 } // namespace trace_event 138 } // namespace trace_event
43 } // namespace base 139 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698