Commit 07c19ac6 authored by mark@chromium.org's avatar mark@chromium.org

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
parent 353132b9
......@@ -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\\.'],
......
......@@ -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), \
......
......@@ -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
......@@ -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_
......@@ -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 {
......
......@@ -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);
}
}
......
......@@ -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);
};
......
......@@ -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_;
......
......@@ -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
......
......@@ -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
......@@ -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;
}
......
......@@ -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) +
......
......@@ -24,6 +24,7 @@
#include "base/mac/authorization_util.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/mac_logging.h"
#include "base/mac/mach_logging.h"
#import "base/mac/mac_util.h"
#include "base/mac/scoped_authorizationref.h"
#include "base/mac/scoped_cftyperef.h"
......@@ -81,7 +82,7 @@ io_service_t CopyHDIXDriveServiceForMedia(io_service_t media) {
kIORegistryIterateParents,
&iterator_ref);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IORegistryEntryCreateIterator: " << kr;
MACH_LOG(ERROR, kr) << "IORegistryEntryCreateIterator";
return IO_OBJECT_NULL;
}
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
......@@ -207,7 +208,7 @@ bool IsPathOnReadOnlyDiskImage(const char path[],
match_dict,
&iterator_ref);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IOServiceGetMatchingServices: " << kr;
MACH_LOG(ERROR, kr) << "IOServiceGetMatchingServices";
return false;
}
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
......
......@@ -12,6 +12,7 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
......@@ -28,11 +29,6 @@ namespace content {
namespace {
// Prints a string representation of a Mach error code.
std::string MachErrorCode(kern_return_t err) {
return base::StringPrintf("0x%x %s", err, mach_error_string(err));
}
// Mach message structure used in the child as a sending message.
struct MachBroker_ChildSendMsg {
mach_msg_header_t header;
......@@ -64,8 +60,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
MACH_PORT_RIGHT_RECEIVE,
&port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to allocate MachBroker server port: "
<< MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_port_allocate";
return false;
}
......@@ -73,8 +68,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
kr = mach_port_insert_right(
mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to insert send right for MachBroker server port: "
<< MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_port_insert_right";
return false;
}
......@@ -96,33 +90,35 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
msg.header.msgh_size = sizeof(msg);
msg.header.msgh_local_port = server_port_.get();
const mach_msg_option_t options = MACH_RCV_MSG |
MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
kern_return_t kr;
do {
while ((kr = mach_msg(&msg.header,
options,
0,
sizeof(msg),
server_port_,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL)) == KERN_SUCCESS) {
// Use the kernel audit information to make sure this message is from
// a task that this process spawned. The kernel audit token contains the
// unspoofable pid of the task that sent the message.
mach_msg_option_t options = MACH_RCV_MSG |
MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
kr = mach_msg(&msg.header, options, 0, sizeof(msg), server_port_,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (kr == KERN_SUCCESS) {
// TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
pid_t child_pid;
audit_token_to_au32(msg.trailer.msgh_audit,
NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
mach_port_t child_task_port = msg.child_task_port.name;
// Take the lock and update the broker information.
base::AutoLock lock(broker_->GetLock());
broker_->FinalizePid(child_pid, child_task_port);
}
} while (kr == KERN_SUCCESS);
LOG(ERROR) << "MachBroker thread exiting; mach_msg() likely failed: "
<< MachErrorCode(kr);
//
// TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
pid_t child_pid;
audit_token_to_au32(msg.trailer.msgh_audit,
NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
mach_port_t child_task_port = msg.child_task_port.name;
// Take the lock and update the broker information.
base::AutoLock lock(broker_->GetLock());
broker_->FinalizePid(child_pid, child_task_port);
}
MACH_LOG(ERROR, kr) << "mach_msg";
}
private:
......@@ -141,7 +137,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
mach_port_t bootstrap_port;
kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up bootstrap port: " << MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "task_get_bootstrap_port";
return false;
}
......@@ -149,7 +145,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = bootstrap_look_up(bootstrap_port,
const_cast<char*>(GetMachPortName().c_str()), &parent_port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up named parent port: " << MachErrorCode(kr);
BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
return false;
}
......@@ -169,7 +165,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to send task port to parent: " << MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_msg";
return false;
}
......@@ -279,9 +275,7 @@ void MachBroker::InvalidatePid(base::ProcessHandle pid) {
kern_return_t kr = mach_port_deallocate(mach_task_self(),
it->second);
LOG_IF(WARNING, kr != KERN_SUCCESS)
<< "Failed to mach_port_deallocate mach task " << it->second
<< ", error " << MachErrorCode(kr);
MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
mach_map_.erase(it);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment