diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 18:49:55 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 18:49:55 +0000 |
commit | 07c19ac617bf16fc8559448fb43e696bcf43b2e9 (patch) | |
tree | 30f2a78649e2bf97d57e1586d2e27fbf0e4731ff /base | |
parent | 353132b98a665e7f93b110c36888c4eef2002a9b (diff) | |
download | chromium_src-07c19ac617bf16fc8559448fb43e696bcf43b2e9.zip chromium_src-07c19ac617bf16fc8559448fb43e696bcf43b2e9.tar.gz chromium_src-07c19ac617bf16fc8559448fb43e696bcf43b2e9.tar.bz2 |
Use the new ScopedMachVM class and the MACH_LOG family of logging macros
where it makes sense to do so in existing code.
This migration shook out a couple of bugs in mach_logging.h carried over
from mac_logging.h (fixed there too).
R=rsesek@chromium.org, sky@chromium.org
Review URL: https://codereview.chromium.org/278923002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269829 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/mac/mac_logging.h | 14 | ||||
-rw-r--r-- | base/mac/mach_logging.cc | 10 | ||||
-rw-r--r-- | base/mac/mach_logging.h | 64 | ||||
-rw-r--r-- | base/mac/scoped_mach_port.h | 2 | ||||
-rw-r--r-- | base/mac/scoped_mach_vm.cc | 16 | ||||
-rw-r--r-- | base/mac/scoped_mach_vm.h | 26 | ||||
-rw-r--r-- | base/memory/discardable_memory_mac.cc | 69 | ||||
-rw-r--r-- | base/process/memory_mac.mm | 15 | ||||
-rw-r--r-- | base/process/process_metrics_mac.cc | 69 | ||||
-rw-r--r-- | base/threading/platform_thread_mac.mm | 43 | ||||
-rw-r--r-- | base/time/time_mac.cc | 5 |
12 files changed, 187 insertions, 148 deletions
diff --git a/base/base.gypi b/base/base.gypi index 520f567..86354ae4 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -829,8 +829,10 @@ ['include', '^mac/bundle_locations\\.'], ['include', '^mac/foundation_util\\.'], ['include', '^mac/mac_logging\\.'], + ['include', '^mac/mach_logging\\.'], ['include', '^mac/objc_property_releaser\\.'], ['include', '^mac/scoped_mach_port\\.'], + ['include', '^mac/scoped_mach_vm\\.'], ['include', '^mac/scoped_nsautorelease_pool\\.'], ['include', '^mac/scoped_nsobject\\.'], ['include', '^memory/discardable_memory_mac\\.'], diff --git a/base/mac/mac_logging.h b/base/mac/mac_logging.h index 02f205d..1081490 100644 --- a/base/mac/mac_logging.h +++ b/base/mac/mac_logging.h @@ -5,6 +5,8 @@ #ifndef BASE_MAC_MAC_LOGGING_H_ #define BASE_MAC_MAC_LOGGING_H_ +#include "base/base_export.h" +#include "base/basictypes.h" #include "base/logging.h" #include "build/build_config.h" @@ -43,6 +45,12 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { } // namespace logging +#if defined(NDEBUG) +#define MAC_DVLOG_IS_ON(verbose_level) 0 +#else +#define MAC_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level) +#endif + #define OSSTATUS_LOG_STREAM(severity, status) \ COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream() #define OSSTATUS_VLOG_STREAM(verbose_level, status) \ @@ -74,10 +82,10 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { #define OSSTATUS_DVLOG(verbose_level, status) \ LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ - DVLOG_IS_ON(verbose_level)) + MAC_DVLOG_IS_ON(verbose_level)) #define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \ - LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status) \ - DVLOG_IS_ON(verbose_level) && (condition)) + LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \ + MAC_DVLOG_IS_ON(verbose_level) && (condition)) #define OSSTATUS_DCHECK(condition, status) \ LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \ diff --git a/base/mac/mach_logging.cc b/base/mac/mach_logging.cc index 0ab6416..c5ff85e 100644 --- a/base/mac/mach_logging.cc +++ b/base/mac/mach_logging.cc @@ -4,13 +4,15 @@ #include "base/mac/mach_logging.h" -#include <servers/bootstrap.h> - #include <iomanip> #include <string> #include "base/strings/stringprintf.h" +#if !defined(OS_IOS) +#include <servers/bootstrap.h> +#endif // !OS_IOS + namespace { std::string FormatMachErrorNumber(mach_error_t mach_err) { @@ -41,6 +43,8 @@ MachLogMessage::~MachLogMessage() { << FormatMachErrorNumber(mach_err_); } +#if !defined(OS_IOS) + BootstrapLogMessage::BootstrapLogMessage(const char* file_path, int line, LogSeverity severity, @@ -78,4 +82,6 @@ BootstrapLogMessage::~BootstrapLogMessage() { } } +#endif // !OS_IOS + } // namespace logging diff --git a/base/mac/mach_logging.h b/base/mac/mach_logging.h index 589dde2..a9b3b65 100644 --- a/base/mac/mach_logging.h +++ b/base/mac/mach_logging.h @@ -7,8 +7,10 @@ #include <mach/mach.h> +#include "base/base_export.h" #include "base/basictypes.h" #include "base/logging.h" +#include "build/build_config.h" // Use the MACH_LOG family of macros along with a mach_error_t (kern_return_t) // containing a Mach error. The error value will be decoded so that logged @@ -26,12 +28,12 @@ // MACH_LOG(ERROR, kr) << "mach_timebase_info"; // } // -// kr = mach_vm_deallocate(task, address, size); -// MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_vm_deallocate"; +// kr = vm_deallocate(task, address, size); +// MACH_DCHECK(kr == KERN_SUCCESS, kr) << "vm_deallocate"; namespace logging { -class MachLogMessage : public logging::LogMessage { +class BASE_EXPORT MachLogMessage : public logging::LogMessage { public: MachLogMessage(const char* file_path, int line, @@ -45,22 +47,14 @@ class MachLogMessage : public logging::LogMessage { DISALLOW_COPY_AND_ASSIGN(MachLogMessage); }; -class BootstrapLogMessage : public logging::LogMessage { - public: - BootstrapLogMessage(const char* file_path, - int line, - LogSeverity severity, - kern_return_t bootstrap_err); - ~BootstrapLogMessage(); - - private: - kern_return_t bootstrap_err_; - - DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage); -}; - } // namespace logging +#if defined(NDEBUG) +#define MACH_DVLOG_IS_ON(verbose_level) 0 +#else +#define MACH_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level) +#endif + #define MACH_LOG_STREAM(severity, mach_err) \ COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream() #define MACH_VLOG_STREAM(verbose_level, mach_err) \ @@ -92,16 +86,38 @@ class BootstrapLogMessage : public logging::LogMessage { #define MACH_DVLOG(verbose_level, mach_err) \ LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \ - DVLOG_IS_ON(verbose_level)) + MACH_DVLOG_IS_ON(verbose_level)) #define MACH_DVLOG_IF(verbose_level, condition, mach_err) \ - LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err) \ - DVLOG_IS_ON(verbose_level) && (condition)) + LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \ + MACH_DVLOG_IS_ON(verbose_level) && (condition)) #define MACH_DCHECK(condition, mach_err) \ LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \ DCHECK_IS_ON && !(condition)) \ << "Check failed: " # condition << ". " +#if !defined(OS_IOS) + +namespace logging { + +class BASE_EXPORT BootstrapLogMessage : public logging::LogMessage { + public: + BootstrapLogMessage(const char* file_path, + int line, + LogSeverity severity, + kern_return_t bootstrap_err); + ~BootstrapLogMessage(); + + private: + kern_return_t bootstrap_err_; + + DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage); +}; + +} // namespace logging + +#define BOOTSTRAP_DVLOG_IS_ON MACH_DVLOG_IS_ON + #define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \ COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \ bootstrap_err).stream() @@ -136,14 +152,16 @@ class BootstrapLogMessage : public logging::LogMessage { #define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \ LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \ - DVLOG_IS_ON(verbose_level)) + BOOTSTRAP_DVLOG_IS_ON(verbose_level)) #define BOOTSTRAP_DVLOG_IF(verbose_level, condition, bootstrap_err) \ - LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \ - DVLOG_IS_ON(verbose_level) && (condition)) + LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \ + BOOTSTRAP_DVLOG_IS_ON(verbose_level) && (condition)) #define BOOTSTRAP_DCHECK(condition, bootstrap_err) \ LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \ DCHECK_IS_ON && !(condition)) \ << "Check failed: " # condition << ". " +#endif // !OS_IOS + #endif // BASE_MAC_MACH_LOGGING_H_ diff --git a/base/mac/scoped_mach_port.h b/base/mac/scoped_mach_port.h index cc2ef20..0b2ac7f 100644 --- a/base/mac/scoped_mach_port.h +++ b/base/mac/scoped_mach_port.h @@ -7,8 +7,8 @@ #include <mach/mach.h> -#include "base/basictypes.h" #include "base/base_export.h" +#include "base/basictypes.h" namespace base { namespace mac { diff --git a/base/mac/scoped_mach_vm.cc b/base/mac/scoped_mach_vm.cc index a9055aa..1c28d9c 100644 --- a/base/mac/scoped_mach_vm.cc +++ b/base/mac/scoped_mach_vm.cc @@ -7,21 +7,21 @@ namespace base { namespace mac { -void ScopedMachVM::reset(mach_vm_address_t address, mach_vm_size_t size) { +void ScopedMachVM::reset(vm_address_t address, vm_size_t size) { DCHECK(address % PAGE_SIZE == 0); DCHECK(size % PAGE_SIZE == 0); if (size_) { if (address_ < address) { - mach_vm_deallocate(mach_task_self(), - address_, - std::min(size_, address - address_)); + vm_deallocate(mach_task_self(), + address_, + std::min(size_, address - address_)); } if (address_ + size_ > address + size) { - mach_vm_address_t deallocate_start = std::max(address_, address + size); - mach_vm_deallocate(mach_task_self(), - deallocate_start, - address_ + size_ - deallocate_start); + vm_address_t deallocate_start = std::max(address_, address + size); + vm_deallocate(mach_task_self(), + deallocate_start, + address_ + size_ - deallocate_start); } } diff --git a/base/mac/scoped_mach_vm.h b/base/mac/scoped_mach_vm.h index 065b968..b130a79 100644 --- a/base/mac/scoped_mach_vm.h +++ b/base/mac/scoped_mach_vm.h @@ -6,15 +6,15 @@ #define BASE_MAC_SCOPED_MACH_VM_H_ #include <mach/mach.h> -#include <mach/mach_vm.h> #include <algorithm> +#include "base/base_export.h" #include "base/basictypes.h" #include "base/logging.h" // Use ScopedMachVM to supervise ownership of pages in the current process -// through the Mach VM subsystem. Pages allocated with mach_vm_allocate can be +// through the Mach VM subsystem. Pages allocated with vm_allocate can be // released when exiting a scope with ScopedMachVM. // // The Mach VM subsystem operates on a page-by-page basis, and a single VM @@ -33,10 +33,10 @@ // // Example: // -// mach_vm_address_t address = 0; -// mach_vm_size_t size = 12345; // This requested size is not page-aligned. +// vm_address_t address = 0; +// vm_size_t size = 12345; // This requested size is not page-aligned. // kern_return_t kr = -// mach_vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE); +// vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE); // if (kr != KERN_SUCCESS) { // return false; // } @@ -45,9 +45,9 @@ namespace base { namespace mac { -class ScopedMachVM { +class BASE_EXPORT ScopedMachVM { public: - explicit ScopedMachVM(mach_vm_address_t address = 0, mach_vm_size_t size = 0) + explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0) : address_(address), size_(size) { DCHECK(address % PAGE_SIZE == 0); @@ -56,17 +56,17 @@ class ScopedMachVM { ~ScopedMachVM() { if (size_) { - mach_vm_deallocate(mach_task_self(), address_, size_); + vm_deallocate(mach_task_self(), address_, size_); } } - void reset(mach_vm_address_t address = 0, mach_vm_size_t size = 0); + void reset(vm_address_t address = 0, vm_size_t size = 0); - mach_vm_address_t address() const { + vm_address_t address() const { return address_; } - mach_vm_size_t size() const { + vm_size_t size() const { return size_; } @@ -81,8 +81,8 @@ class ScopedMachVM { } private: - mach_vm_address_t address_; - mach_vm_size_t size_; + vm_address_t address_; + vm_size_t size_; DISALLOW_COPY_AND_ASSIGN(ScopedMachVM); }; diff --git a/base/memory/discardable_memory_mac.cc b/base/memory/discardable_memory_mac.cc index d7133f5..d8c0ada 100644 --- a/base/memory/discardable_memory_mac.cc +++ b/base/memory/discardable_memory_mac.cc @@ -5,12 +5,13 @@ #include "base/memory/discardable_memory.h" #include <mach/mach.h> -#include <sys/mman.h> #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/lazy_instance.h" #include "base/logging.h" +#include "base/mac/mach_logging.h" +#include "base/mac/scoped_mach_vm.h" #include "base/memory/discardable_memory_emulated.h" #include "base/memory/discardable_memory_malloc.h" #include "base/memory/discardable_memory_manager.h" @@ -40,7 +41,9 @@ class DiscardableMemoryMac public internal::DiscardableMemoryManagerAllocation { public: explicit DiscardableMemoryMac(size_t bytes) - : buffer_(0), bytes_(bytes), is_locked_(false) { + : memory_(0, 0), + bytes_(mach_vm_round_page(bytes)), + is_locked_(false) { g_shared_state.Pointer()->manager.Register(this, bytes); } @@ -50,8 +53,6 @@ class DiscardableMemoryMac if (is_locked_) Unlock(); g_shared_state.Pointer()->manager.Unregister(this); - if (buffer_) - vm_deallocate(mach_task_self(), buffer_, bytes_); } // Overridden from DiscardableMemory: @@ -66,64 +67,78 @@ class DiscardableMemoryMac return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; } + virtual void Unlock() OVERRIDE { DCHECK(is_locked_); g_shared_state.Pointer()->manager.ReleaseLock(this); is_locked_ = false; } + virtual void* Memory() const OVERRIDE { DCHECK(is_locked_); - return reinterpret_cast<void*>(buffer_); + return reinterpret_cast<void*>(memory_.address()); } // Overridden from internal::DiscardableMemoryManagerAllocation: virtual bool AllocateAndAcquireLock() OVERRIDE { bool persistent = true; - if (!buffer_) { - kern_return_t ret = vm_allocate( + kern_return_t ret; + if (!memory_.size()) { + vm_address_t address = 0; + ret = vm_allocate( mach_task_self(), - &buffer_, + &address, bytes_, - VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE | kDiscardableMemoryTag); - CHECK_EQ(KERN_SUCCESS, ret) << "wm_allocate() failed."; + VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag); + MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate"; + memory_.reset(address, bytes_); persistent = false; } + #if !defined(NDEBUG) - int status = mprotect( - reinterpret_cast<void*>(buffer_), bytes_, PROT_READ | PROT_WRITE); - DCHECK_EQ(0, status); + ret = vm_protect(mach_task_self(), + memory_.address(), + memory_.size(), + FALSE, + VM_PROT_DEFAULT); + MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; #endif + int state = VM_PURGABLE_NONVOLATILE; - kern_return_t ret = vm_purgable_control(mach_task_self(), - buffer_, - VM_PURGABLE_SET_STATE, - &state); - CHECK_EQ(KERN_SUCCESS, ret) << "Failed to lock memory."; + ret = vm_purgable_control(mach_task_self(), + memory_.address(), + VM_PURGABLE_SET_STATE, + &state); + MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; if (state & VM_PURGABLE_EMPTY) persistent = false; return persistent; } + virtual void ReleaseLock() OVERRIDE { int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; kern_return_t ret = vm_purgable_control(mach_task_self(), - buffer_, + memory_.address(), VM_PURGABLE_SET_STATE, &state); - CHECK_EQ(KERN_SUCCESS, ret) << "Failed to unlock memory."; + MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; + #if !defined(NDEBUG) - int status = mprotect(reinterpret_cast<void*>(buffer_), bytes_, PROT_NONE); - DCHECK_EQ(0, status); + ret = vm_protect(mach_task_self(), + memory_.address(), + memory_.size(), + FALSE, + VM_PROT_NONE); + MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; #endif } + virtual void Purge() OVERRIDE { - if (buffer_) { - vm_deallocate(mach_task_self(), buffer_, bytes_); - buffer_ = 0; - } + memory_.reset(); } private: - vm_address_t buffer_; + mac::ScopedMachVM memory_; const size_t bytes_; bool is_locked_; diff --git a/base/process/memory_mac.mm b/base/process/memory_mac.mm index 6cf1297..8503bbb 100644 --- a/base/process/memory_mac.mm +++ b/base/process/memory_mac.mm @@ -16,6 +16,7 @@ #include "base/lazy_instance.h" #include "base/logging.h" #include "base/mac/mac_util.h" +#include "base/mac/mach_logging.h" #include "base/scoped_clear_errno.h" #include "third_party/apple_apsl/CFBase.h" #include "third_party/apple_apsl/malloc.h" @@ -222,10 +223,12 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone, reinterpret_cast<vm_region_info_t>(&info), &count, &unused); - CHECK(result == KERN_SUCCESS); + MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_region"; - result = mach_port_deallocate(mach_task_self(), unused); - CHECK(result == KERN_SUCCESS); + // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but + // balance it with a deallocate in case this ever changes. See 10.9.2 + // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. + mach_port_deallocate(mach_task_self(), unused); // Does the region fully enclose the zone pointers? Possibly unwarranted // simplification used: using the size of a full version 8 malloc zone rather @@ -248,7 +251,7 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone, *reprotection_length, false, info.protection | VM_PROT_WRITE); - CHECK(result == KERN_SUCCESS); + MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; } } @@ -646,7 +649,7 @@ void EnableTerminationOnOutOfMemory() { default_reprotection_length, false, default_reprotection_value); - CHECK(result == KERN_SUCCESS); + MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; } if (purgeable_reprotection_start) { @@ -655,7 +658,7 @@ void EnableTerminationOnOutOfMemory() { purgeable_reprotection_length, false, purgeable_reprotection_value); - CHECK(result == KERN_SUCCESS); + MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; } #endif diff --git a/base/process/process_metrics_mac.cc b/base/process/process_metrics_mac.cc index 1b2e61b..ee79583 100644 --- a/base/process/process_metrics_mac.cc +++ b/base/process/process_metrics_mac.cc @@ -11,6 +11,7 @@ #include "base/containers/hash_tables.h" #include "base/logging.h" +#include "base/mac/mach_logging.h" #include "base/mac/scoped_mach_port.h" #include "base/sys_info.h" @@ -116,7 +117,6 @@ size_t ProcessMetrics::GetPeakWorkingSetSize() const { // shared_bytes is the size of shared resident memory. bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, size_t* shared_bytes) { - kern_return_t kr; size_t private_pages_count = 0; size_t shared_pages_count = 0; @@ -153,22 +153,26 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, vm_region_top_info_data_t info; mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; mach_port_t object_name; - kr = mach_vm_region(task, - &address, - &size, - VM_REGION_TOP_INFO, - (vm_region_info_t)&info, - &info_count, - &object_name); + kern_return_t kr = mach_vm_region(task, + &address, + &size, + VM_REGION_TOP_INFO, + reinterpret_cast<vm_region_info_t>(&info), + &info_count, + &object_name); if (kr == KERN_INVALID_ADDRESS) { // We're at the end of the address space. break; } else if (kr != KERN_SUCCESS) { - DLOG(ERROR) << "Calling mach_vm_region failed with error: " - << mach_error_string(kr); + MACH_DLOG(ERROR, kr) << "mach_vm_region"; return false; } + // The kernel always returns a null object for VM_REGION_TOP_INFO, but + // balance it with a deallocate in case this ever changes. See 10.9.2 + // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. + mach_port_deallocate(mach_task_self(), object_name); + if (IsAddressInSharedRegion(address, cpu_type) && info.share_mode != SM_PRIVATE) continue; @@ -196,18 +200,10 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, } } - vm_size_t page_size; - kr = host_page_size(task, &page_size); - if (kr != KERN_SUCCESS) { - DLOG(ERROR) << "Failed to fetch host page size, error: " - << mach_error_string(kr); - return false; - } - if (private_bytes) - *private_bytes = private_pages_count * page_size; + *private_bytes = private_pages_count * PAGE_SIZE; if (shared_bytes) - *shared_bytes = shared_pages_count * page_size; + *shared_bytes = shared_pages_count * PAGE_SIZE; return true; } @@ -235,16 +231,14 @@ double ProcessMetrics::GetCPUUsage() { if (task == MACH_PORT_NULL) return 0; - kern_return_t kr; - // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() // in libtop.c), but this is more concise and gives the same results: task_thread_times_info thread_info_data; mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; - kr = task_info(task, - TASK_THREAD_TIMES_INFO, - reinterpret_cast<task_info_t>(&thread_info_data), - &thread_info_count); + kern_return_t kr = task_info(task, + TASK_THREAD_TIMES_INFO, + reinterpret_cast<task_info_t>(&thread_info_data), + &thread_info_count); if (kr != KERN_SUCCESS) { // Most likely cause: |task| is a zombie. return 0; @@ -294,14 +288,12 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() { if (task == MACH_PORT_NULL) return 0; - kern_return_t kr; - task_power_info power_info_data; mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT; - kr = task_info(task, - TASK_POWER_INFO, - reinterpret_cast<task_info_t>(&power_info_data), - &power_info_count); + kern_return_t kr = task_info(task, + TASK_POWER_INFO, + reinterpret_cast<task_info_t>(&power_info_data), + &power_info_count); if (kr != KERN_SUCCESS) { // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system // where TASK_POWER_INFO isn't supported yet. @@ -362,19 +354,12 @@ size_t GetSystemCommitCharge() { kern_return_t kr = host_statistics(host, HOST_VM_INFO, reinterpret_cast<host_info_t>(&data), &count); - if (kr) { - DLOG(WARNING) << "Failed to fetch host statistics."; - return 0; - } - - vm_size_t page_size; - kr = host_page_size(host, &page_size); - if (kr) { - DLOG(ERROR) << "Failed to fetch host page size."; + if (kr != KERN_SUCCESS) { + MACH_DLOG(WARNING, kr) << "host_statistics"; return 0; } - return (data.active_count * page_size) / 1024; + return (data.active_count * PAGE_SIZE) / 1024; } } // namespace base diff --git a/base/threading/platform_thread_mac.mm b/base/threading/platform_thread_mac.mm index 486181f..147e625 100644 --- a/base/threading/platform_thread_mac.mm +++ b/base/threading/platform_thread_mac.mm @@ -14,6 +14,7 @@ #include "base/lazy_instance.h" #include "base/logging.h" +#include "base/mac/mach_logging.h" #include "base/threading/thread_id_name_manager.h" #include "base/tracked_objects.h" @@ -61,20 +62,19 @@ void SetPriorityNormal(mach_port_t mach_thread_id) { // Please note that this call could fail in rare cases depending // on runtime conditions. thread_standard_policy policy; - kern_return_t result = thread_policy_set(mach_thread_id, - THREAD_STANDARD_POLICY, - (thread_policy_t)&policy, - THREAD_STANDARD_POLICY_COUNT); + kern_return_t result = + thread_policy_set(mach_thread_id, + THREAD_STANDARD_POLICY, + reinterpret_cast<thread_policy_t>(&policy), + THREAD_STANDARD_POLICY_COUNT); if (result != KERN_SUCCESS) - DVLOG(1) << "thread_policy_set() failure: " << result; + MACH_DVLOG(1, result) << "thread_policy_set"; } // Enables time-contraint policy and priority suitable for low-latency, // glitch-resistant audio. void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { - kern_return_t result; - // Increase thread priority to real-time. // Please note that the thread_policy_set() calls may fail in @@ -85,12 +85,13 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { // Make thread fixed priority. thread_extended_policy_data_t policy; policy.timeshare = 0; // Set to 1 for a non-fixed thread. - result = thread_policy_set(mach_thread_id, - THREAD_EXTENDED_POLICY, - (thread_policy_t)&policy, - THREAD_EXTENDED_POLICY_COUNT); + kern_return_t result = + thread_policy_set(mach_thread_id, + THREAD_EXTENDED_POLICY, + reinterpret_cast<thread_policy_t>(&policy), + THREAD_EXTENDED_POLICY_COUNT); if (result != KERN_SUCCESS) { - DVLOG(1) << "thread_policy_set() failure: " << result; + MACH_DVLOG(1, result) << "thread_policy_set"; return; } @@ -99,10 +100,10 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { precedence.importance = 63; result = thread_policy_set(mach_thread_id, THREAD_PRECEDENCE_POLICY, - (thread_policy_t)&precedence, + reinterpret_cast<thread_policy_t>(&precedence), THREAD_PRECEDENCE_POLICY_COUNT); if (result != KERN_SUCCESS) { - DVLOG(1) << "thread_policy_set() failure: " << result; + MACH_DVLOG(1, result) << "thread_policy_set"; return; } @@ -133,7 +134,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { mach_timebase_info_data_t tb_info; mach_timebase_info(&tb_info); double ms_to_abs_time = - ((double)tb_info.denom / (double)tb_info.numer) * 1000000; + (static_cast<double>(tb_info.denom) / tb_info.numer) * 1000000; thread_time_constraint_policy_data_t time_constraints; time_constraints.period = kTimeQuantum * ms_to_abs_time; @@ -141,12 +142,12 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; time_constraints.preemptible = 0; - result = thread_policy_set(mach_thread_id, - THREAD_TIME_CONSTRAINT_POLICY, - (thread_policy_t)&time_constraints, - THREAD_TIME_CONSTRAINT_POLICY_COUNT); - if (result != KERN_SUCCESS) - DVLOG(1) << "thread_policy_set() failure: " << result; + result = + thread_policy_set(mach_thread_id, + THREAD_TIME_CONSTRAINT_POLICY, + reinterpret_cast<thread_policy_t>(&time_constraints), + THREAD_TIME_CONSTRAINT_POLICY_COUNT); + MACH_DVLOG_IF(1, result != KERN_SUCCESS, result) << "thread_policy_set"; return; } diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc index 6089efb..515cafe 100644 --- a/base/time/time_mac.cc +++ b/base/time/time_mac.cc @@ -15,6 +15,7 @@ #include "base/basictypes.h" #include "base/logging.h" +#include "base/mac/mach_logging.h" #include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_mach_port.h" @@ -46,7 +47,7 @@ uint64_t ComputeCurrentTicks() { // whether mach_timebase_info has already been called. This is // recommended by Apple's QA1398. kern_return_t kr = mach_timebase_info(&timebase_info); - DCHECK_EQ(KERN_SUCCESS, kr); + MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info"; } // mach_absolute_time is it when it comes to ticks on the Mac. Other calls @@ -85,7 +86,7 @@ uint64_t ComputeThreadTicks() { THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&thread_info_data), &thread_info_count); - DCHECK_EQ(KERN_SUCCESS, kr); + MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; return (thread_info_data.user_time.seconds * base::Time::kMicrosecondsPerSecond) + |