| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/process/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <winternl.h> | 11 #include <winternl.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/process/memory.h" |
| 17 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // System pagesize. This value remains constant on x86/64 architectures. | 23 // System pagesize. This value remains constant on x86/64 architectures. |
| 23 const int PAGESIZE_KB = 4; | 24 const int PAGESIZE_KB = 4; |
| 24 | 25 |
| 25 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)( | 26 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)( |
| 26 SYSTEM_INFORMATION_CLASS SystemInformationClass, | 27 SYSTEM_INFORMATION_CLASS SystemInformationClass, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 usage->priv = 0; | 134 usage->priv = 0; |
| 134 return; | 135 return; |
| 135 } | 136 } |
| 136 base_address = new_base; | 137 base_address = new_base; |
| 137 } | 138 } |
| 138 usage->image = committed_image / 1024; | 139 usage->image = committed_image / 1024; |
| 139 usage->mapped = committed_mapped / 1024; | 140 usage->mapped = committed_mapped / 1024; |
| 140 usage->priv = committed_private / 1024; | 141 usage->priv = committed_private / 1024; |
| 141 } | 142 } |
| 142 | 143 |
| 144 namespace { |
| 145 |
| 146 class WorkingSetInformationBuffer { |
| 147 public: |
| 148 WorkingSetInformationBuffer() {} |
| 149 ~WorkingSetInformationBuffer() { Clear(); } |
| 150 |
| 151 bool Reserve(size_t size) { |
| 152 Clear(); |
| 153 // Use UncheckedMalloc here because this can be called from the code |
| 154 // that handles low memory condition. |
| 155 return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_)); |
| 156 } |
| 157 |
| 158 PSAPI_WORKING_SET_INFORMATION* get() { return buffer_; } |
| 159 const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; } |
| 160 |
| 161 private: |
| 162 void Clear() { |
| 163 free(buffer_); |
| 164 buffer_ = nullptr; |
| 165 } |
| 166 |
| 167 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; |
| 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); |
| 170 }; |
| 171 |
| 172 } // namespace |
| 173 |
| 143 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 174 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 144 size_t ws_private = 0; | 175 size_t ws_private = 0; |
| 145 size_t ws_shareable = 0; | 176 size_t ws_shareable = 0; |
| 146 size_t ws_shared = 0; | 177 size_t ws_shared = 0; |
| 147 | 178 |
| 148 DCHECK(ws_usage); | 179 DCHECK(ws_usage); |
| 149 memset(ws_usage, 0, sizeof(*ws_usage)); | 180 memset(ws_usage, 0, sizeof(*ws_usage)); |
| 150 | 181 |
| 151 DWORD number_of_entries = 4096; // Just a guess. | 182 DWORD number_of_entries = 4096; // Just a guess. |
| 152 PSAPI_WORKING_SET_INFORMATION* buffer = NULL; | 183 WorkingSetInformationBuffer buffer; |
| 153 int retries = 5; | 184 int retries = 5; |
| 154 for (;;) { | 185 for (;;) { |
| 155 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | 186 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + |
| 156 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | 187 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); |
| 157 | 188 |
| 158 // if we can't expand the buffer, don't leak the previous | 189 if (!buffer.Reserve(buffer_size)) |
| 159 // contents or pass a NULL pointer to QueryWorkingSet | |
| 160 PSAPI_WORKING_SET_INFORMATION* new_buffer = | |
| 161 reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>( | |
| 162 realloc(buffer, buffer_size)); | |
| 163 if (!new_buffer) { | |
| 164 free(buffer); | |
| 165 return false; | 190 return false; |
| 166 } | |
| 167 buffer = new_buffer; | |
| 168 | 191 |
| 169 // Call the function once to get number of items | 192 // Call the function once to get number of items |
| 170 if (QueryWorkingSet(process_, buffer, buffer_size)) | 193 if (QueryWorkingSet(process_, buffer.get(), buffer_size)) |
| 171 break; // Success | 194 break; // Success |
| 172 | 195 |
| 173 if (GetLastError() != ERROR_BAD_LENGTH) { | 196 if (GetLastError() != ERROR_BAD_LENGTH) |
| 174 free(buffer); | |
| 175 return false; | 197 return false; |
| 176 } | |
| 177 | 198 |
| 178 number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries); | 199 number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries); |
| 179 | 200 |
| 180 // Maybe some entries are being added right now. Increase the buffer to | 201 // Maybe some entries are being added right now. Increase the buffer to |
| 181 // take that into account. | 202 // take that into account. Increasing by 10% should generally be enough, |
| 182 number_of_entries = static_cast<DWORD>(number_of_entries * 1.25); | 203 // especially considering the potentially low memory condition during the |
| 204 // call (when called from OomMemoryDetails) and the potentially high |
| 205 // number of entries (300K was observed in crash dumps). |
| 206 number_of_entries = static_cast<DWORD>(number_of_entries * 1.1); |
| 183 | 207 |
| 184 if (--retries == 0) { | 208 if (--retries == 0) { |
| 185 free(buffer); // If we're looping, eventually fail. | 209 // If we're looping, eventually fail. |
| 186 return false; | 210 return false; |
| 187 } | 211 } |
| 188 } | 212 } |
| 189 | 213 |
| 190 // On windows 2000 the function returns 1 even when the buffer is too small. | 214 // On windows 2000 the function returns 1 even when the buffer is too small. |
| 191 // The number of entries that we are going to parse is the minimum between the | 215 // The number of entries that we are going to parse is the minimum between the |
| 192 // size we allocated and the real number of entries. | 216 // size we allocated and the real number of entries. |
| 193 number_of_entries = | 217 number_of_entries = |
| 194 std::min(number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries)); | 218 std::min(number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries)); |
| 195 for (unsigned int i = 0; i < number_of_entries; i++) { | 219 for (unsigned int i = 0; i < number_of_entries; i++) { |
| 196 if (buffer->WorkingSetInfo[i].Shared) { | 220 if (buffer->WorkingSetInfo[i].Shared) { |
| 197 ws_shareable++; | 221 ws_shareable++; |
| 198 if (buffer->WorkingSetInfo[i].ShareCount > 1) | 222 if (buffer->WorkingSetInfo[i].ShareCount > 1) |
| 199 ws_shared++; | 223 ws_shared++; |
| 200 } else { | 224 } else { |
| 201 ws_private++; | 225 ws_private++; |
| 202 } | 226 } |
| 203 } | 227 } |
| 204 | 228 |
| 205 ws_usage->priv = ws_private * PAGESIZE_KB; | 229 ws_usage->priv = ws_private * PAGESIZE_KB; |
| 206 ws_usage->shareable = ws_shareable * PAGESIZE_KB; | 230 ws_usage->shareable = ws_shareable * PAGESIZE_KB; |
| 207 ws_usage->shared = ws_shared * PAGESIZE_KB; | 231 ws_usage->shared = ws_shared * PAGESIZE_KB; |
| 208 free(buffer); | |
| 209 return true; | 232 return true; |
| 210 } | 233 } |
| 211 | 234 |
| 212 static uint64_t FileTimeToUTC(const FILETIME& ftime) { | 235 static uint64_t FileTimeToUTC(const FILETIME& ftime) { |
| 213 LARGE_INTEGER li; | 236 LARGE_INTEGER li; |
| 214 li.LowPart = ftime.dwLowDateTime; | 237 li.LowPart = ftime.dwLowDateTime; |
| 215 li.HighPart = ftime.dwHighDateTime; | 238 li.HighPart = ftime.dwHighDateTime; |
| 216 return li.QuadPart; | 239 return li.QuadPart; |
| 217 } | 240 } |
| 218 | 241 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 | 318 |
| 296 meminfo->total = mem_status.ullTotalPhys / 1024; | 319 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 297 meminfo->free = mem_status.ullAvailPhys / 1024; | 320 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 298 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 321 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 299 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 322 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 300 | 323 |
| 301 return true; | 324 return true; |
| 302 } | 325 } |
| 303 | 326 |
| 304 } // namespace base | 327 } // namespace base |
| OLD | NEW |