• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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  */
16 
17 #include "src/base/test/vm_test_utils.h"
18 
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/utils.h"
21 
22 #include <memory>
23 
24 #include <errno.h>
25 #include <string.h>
26 
27 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
28 #include <Windows.h>
29 #include <Psapi.h>
30 #else
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #endif
34 
35 #include "gtest/gtest.h"
36 #include "perfetto/base/build_config.h"
37 
38 namespace perfetto {
39 namespace base {
40 namespace vm_test_utils {
41 
IsMapped(void * start,size_t size)42 bool IsMapped(void* start, size_t size) {
43   EXPECT_EQ(0u, size % kPageSize);
44 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
45   int retries = 5;
46   int number_of_entries = 4000;  // Just a guess.
47   PSAPI_WORKING_SET_INFORMATION* ws_info = nullptr;
48 
49   std::vector<char> buffer;
50   for (;;) {
51     size_t buffer_size =
52       sizeof(PSAPI_WORKING_SET_INFORMATION) +
53       (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
54 
55     buffer.resize(buffer_size);
56     ws_info = reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>(&buffer[0]);
57 
58     // On success, |buffer_| is populated with info about the working set of
59     // |process|. On ERROR_BAD_LENGTH failure, increase the size of the
60     // buffer and try again.
61     if (QueryWorkingSet(GetCurrentProcess(), &buffer[0], buffer_size))
62       break;  // Success
63 
64     if (GetLastError() != ERROR_BAD_LENGTH) {
65       EXPECT_EQ(true, false);
66       return false;
67     }
68 
69     number_of_entries = ws_info->NumberOfEntries;
70 
71     // Maybe some entries are being added right now. Increase the buffer to
72     // take that into account. Increasing by 10% should generally be enough.
73     number_of_entries *= 1.1;
74 
75     if (--retries == 0) {
76       // If we're looping, eventually fail.
77       EXPECT_EQ(true, false);
78       return false;
79     }
80   }
81 
82   void* end = reinterpret_cast<char*>(start) + size;
83   // Now scan the working-set information looking for the addresses.
84   unsigned pages_found = 0;
85   for (unsigned i = 0; i < ws_info->NumberOfEntries; ++i) {
86     void* address =
87         reinterpret_cast<void*>(ws_info->WorkingSetInfo[i].VirtualPage *
88         kPageSize);
89     if (address >= start && address < end)
90       ++pages_found;
91   }
92 
93   if (pages_found * kPageSize == size)
94     return true;
95   return false;
96 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
97   // Fuchsia doesn't yet support paging (b/119503290).
98   return true;
99 #else
100 #if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
101   using PageState = char;
102   static constexpr PageState kIncoreMask = MINCORE_INCORE;
103 #else
104   using PageState = unsigned char;
105   static constexpr PageState kIncoreMask = 1;
106 #endif
107   const size_t num_pages = size / kPageSize;
108   std::unique_ptr<PageState[]> page_states(new PageState[num_pages]);
109   memset(page_states.get(), 0, num_pages * sizeof(PageState));
110   int res = mincore(start, size, page_states.get());
111   // Linux returns ENOMEM when an unmapped memory range is passed.
112   // MacOS instead returns 0 but leaves the page_states empty.
113   if (res == -1 && errno == ENOMEM)
114     return false;
115   EXPECT_EQ(0, res);
116   for (size_t i = 0; i < num_pages; i++) {
117     if (!(page_states[i] & kIncoreMask))
118       return false;
119   }
120   return true;
121 #endif
122 }
123 
124 }  // namespace vm_test_utils
125 }  // namespace base
126 }  // namespace perfetto
127