Commit 4ce59755 authored by fmeawad@chromium.org's avatar fmeawad@chromium.org

Implement thread-specific CPU time on Mac OS X

This is different from https://code.google.com/p/chromium/codesearch#chromium/src/base/process/process_metrics_mac.cc&l=216 as it gets the per-thread CPU time while process metrics gets all-thread CPU usage.

BUG=280744

Review URL: https://codereview.chromium.org/56973012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233450 0039d316-1c4b-4281-b951-d872f2087c98
parent 087edc74
...@@ -548,7 +548,8 @@ class BASE_EXPORT TimeTicks { ...@@ -548,7 +548,8 @@ class BASE_EXPORT TimeTicks {
// Returns true if ThreadNow() is supported on this system. // Returns true if ThreadNow() is supported on this system.
static bool IsThreadNowSupported() { static bool IsThreadNowSupported() {
#if defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0) #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
(defined(OS_MACOSX) && !defined(OS_IOS))
return true; return true;
#else #else
return false; return false;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <CoreFoundation/CFDate.h> #include <CoreFoundation/CFDate.h>
#include <CoreFoundation/CFTimeZone.h> #include <CoreFoundation/CFTimeZone.h>
#include <mach/mach.h>
#include <mach/mach_time.h> #include <mach/mach_time.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/time.h> #include <sys/time.h>
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_mach_port.h"
namespace { namespace {
...@@ -64,6 +66,33 @@ uint64_t ComputeCurrentTicks() { ...@@ -64,6 +66,33 @@ uint64_t ComputeCurrentTicks() {
#endif // defined(OS_IOS) #endif // defined(OS_IOS)
} }
uint64_t ComputeThreadTicks() {
#if defined(OS_IOS)
NOTREACHED();
return 0;
#else
base::mac::ScopedMachPort thread(mach_thread_self());
mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
thread_basic_info_data_t thread_info_data;
if (thread == MACH_PORT_NULL) {
DLOG(ERROR) << "Failed to get mach_thread_self()";
return 0;
}
kern_return_t kr = thread_info(
thread,
THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&thread_info_data),
&thread_info_count);
DCHECK_EQ(KERN_SUCCESS, kr);
return (thread_info_data.user_time.seconds *
base::Time::kMicrosecondsPerSecond) +
thread_info_data.user_time.microseconds;
#endif // defined(OS_IOS)
}
} // namespace } // namespace
namespace base { namespace base {
...@@ -196,8 +225,7 @@ bool TimeTicks::IsHighResNowFastAndReliable() { ...@@ -196,8 +225,7 @@ bool TimeTicks::IsHighResNowFastAndReliable() {
// static // static
TimeTicks TimeTicks::ThreadNow() { TimeTicks TimeTicks::ThreadNow() {
NOTREACHED(); return TimeTicks(ComputeThreadTicks());
return TimeTicks();
} }
// static // static
......
...@@ -639,12 +639,17 @@ TEST(TimeTicks, ThreadNow) { ...@@ -639,12 +639,17 @@ TEST(TimeTicks, ThreadNow) {
if (TimeTicks::IsThreadNowSupported()) { if (TimeTicks::IsThreadNowSupported()) {
TimeTicks begin = TimeTicks::Now(); TimeTicks begin = TimeTicks::Now();
TimeTicks begin_thread = TimeTicks::ThreadNow(); TimeTicks begin_thread = TimeTicks::ThreadNow();
// Sleep for 10 milliseconds to get the thread de-scheduled // Make sure that ThreadNow value is non-zero.
EXPECT_GT(begin_thread, TimeTicks());
// Sleep for 10 milliseconds to get the thread de-scheduled.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
TimeTicks end_thread = TimeTicks::ThreadNow(); TimeTicks end_thread = TimeTicks::ThreadNow();
TimeTicks end = TimeTicks::Now(); TimeTicks end = TimeTicks::Now();
TimeDelta delta = end - begin; TimeDelta delta = end - begin;
TimeDelta delta_thread = end_thread - begin_thread; TimeDelta delta_thread = end_thread - begin_thread;
// Make sure that some thread time have elapsed.
EXPECT_GT(delta_thread.InMicroseconds(), 0);
// But the thread time is at least 9ms less than clock time.
TimeDelta difference = delta - delta_thread; TimeDelta difference = delta - delta_thread;
EXPECT_GE(difference.InMicroseconds(), 9000); EXPECT_GE(difference.InMicroseconds(), 9000);
} }
......
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