Chromium Code Reviews| 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 <mach/mach.h> | 7 #include <mach/mach.h> |
| 8 #include <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
| 9 #include <mach/shared_region.h> | 9 #include <mach/shared_region.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 } else if (type == CPU_TYPE_X86_64) { | 73 } else if (type == CPU_TYPE_X86_64) { |
| 74 return addr >= SHARED_REGION_BASE_X86_64 && | 74 return addr >= SHARED_REGION_BASE_X86_64 && |
| 75 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 75 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
| 76 } else { | 76 } else { |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 } // namespace | 81 } // namespace |
| 82 | 82 |
| 83 SystemMemoryInfoKB::SystemMemoryInfoKB() : total(0), free(0) {} | |
| 84 | |
| 85 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = | |
| 86 default; | |
| 87 | |
| 88 // Getting a mach task from a pid for another process requires permissions in | 83 // Getting a mach task from a pid for another process requires permissions in |
| 89 // general, so there doesn't really seem to be a way to do these (and spinning | 84 // general, so there doesn't really seem to be a way to do these (and spinning |
| 90 // up ps to fetch each stats seems dangerous to put in a base api for anyone to | 85 // up ps to fetch each stats seems dangerous to put in a base api for anyone to |
| 91 // call). Child processes ipc their port, so return something if available, | 86 // call). Child processes ipc their port, so return something if available, |
| 92 // otherwise return 0. | 87 // otherwise return 0. |
| 93 | 88 |
| 94 // static | 89 // static |
| 95 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( | 90 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 96 ProcessHandle process, | 91 ProcessHandle process, |
| 97 PortProvider* port_provider) { | 92 PortProvider* port_provider) { |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 reinterpret_cast<host_info_t>(&data), | 365 reinterpret_cast<host_info_t>(&data), |
| 371 &count); | 366 &count); |
| 372 if (kr != KERN_SUCCESS) { | 367 if (kr != KERN_SUCCESS) { |
| 373 MACH_DLOG(WARNING, kr) << "host_statistics"; | 368 MACH_DLOG(WARNING, kr) << "host_statistics"; |
| 374 return 0; | 369 return 0; |
| 375 } | 370 } |
| 376 | 371 |
| 377 return (data.active_count * PAGE_SIZE) / 1024; | 372 return (data.active_count * PAGE_SIZE) / 1024; |
| 378 } | 373 } |
| 379 | 374 |
| 380 // On Mac, We only get total memory and free memory from the system. | |
| 381 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { | 375 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { |
| 382 struct host_basic_info hostinfo; | 376 struct host_basic_info hostinfo; |
| 383 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; | 377 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 384 base::mac::ScopedMachSendRight host(mach_host_self()); | 378 base::mac::ScopedMachSendRight host(mach_host_self()); |
| 385 int result = host_info(host.get(), HOST_BASIC_INFO, | 379 int result = host_info(host.get(), HOST_BASIC_INFO, |
| 386 reinterpret_cast<host_info_t>(&hostinfo), &count); | 380 reinterpret_cast<host_info_t>(&hostinfo), &count); |
| 387 if (result != KERN_SUCCESS) | 381 if (result != KERN_SUCCESS) |
| 388 return false; | 382 return false; |
| 389 | 383 |
| 390 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); | 384 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 391 meminfo->total = static_cast<int>(hostinfo.max_mem / 1024); | 385 meminfo->total = static_cast<int>(hostinfo.max_mem / 1024); |
| 392 | 386 |
| 393 vm_statistics_data_t vm_info; | 387 vm_statistics64_data_t vm_info; |
| 394 count = HOST_VM_INFO_COUNT; | 388 count = HOST_VM_INFO64_COUNT; |
| 395 | 389 |
| 396 if (host_statistics(host.get(), HOST_VM_INFO, | 390 if (host_statistics64(host.get(), HOST_VM_INFO64, |
| 397 reinterpret_cast<host_info_t>(&vm_info), | 391 reinterpret_cast<host_info64_t>(&vm_info), |
| 398 &count) != KERN_SUCCESS) { | 392 &count) != KERN_SUCCESS) { |
| 399 return false; | 393 return false; |
| 400 } | 394 } |
| 395 DCHECK_EQ(HOST_VM_INFO64_COUNT, count); | |
| 401 | 396 |
| 397 static_assert(PAGE_SIZE % 1024 == 0, "Invalid page size"); | |
| 402 meminfo->free = static_cast<int>( | 398 meminfo->free = static_cast<int>( |
| 403 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); | 399 PAGE_SIZE / 1024 * (vm_info.free_count - vm_info.speculative_count)); |
|
danakj
2017/03/13 16:01:21
Thanks, this seems good, can you use saturated_cas
Michael K. (Yandex Team)
2017/03/14 03:49:09
I think you are worried too much about these casts
danakj
2017/03/14 14:41:46
You're probably right, but then saturated_cast won
| |
| 400 meminfo->speculative = | |
| 401 static_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); | |
| 402 meminfo->file_backed = | |
| 403 static_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); | |
| 404 meminfo->purgeable = | |
| 405 static_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); | |
| 404 | 406 |
| 405 return true; | 407 return true; |
| 406 } | 408 } |
| 407 | 409 |
| 408 } // namespace base | 410 } // namespace base |
| OLD | NEW |