| Index: third_party/tcmalloc/chromium/src/tests/profile-handler_unittest.cc
|
| ===================================================================
|
| --- third_party/tcmalloc/chromium/src/tests/profile-handler_unittest.cc (revision 41940)
|
| +++ third_party/tcmalloc/chromium/src/tests/profile-handler_unittest.cc (working copy)
|
| @@ -8,9 +8,8 @@
|
| #include "profile-handler.h"
|
|
|
| #include <assert.h>
|
| -#include <pthread.h>
|
| #include <sys/time.h>
|
| -#include <time.h>
|
| +#include <pthread.h>
|
| #include "base/logging.h"
|
| #include "base/simple_mutex.h"
|
|
|
| @@ -47,41 +46,19 @@
|
| bool joinable_;
|
| };
|
|
|
| -// Sleep interval in nano secs. ITIMER_PROF goes off only afer the specified CPU
|
| -// time is consumed. Under heavy load this process may no get scheduled in a
|
| -// timely fashion. Therefore, give enough time (20x of ProfileHandle timer
|
| -// interval 10ms (100Hz)) for this process to accumulate enought CPU time to get
|
| -// a profile tick.
|
| -int kSleepInterval = 200000000;
|
| +// Sleep interval in usecs. To ensure a SIGPROF timer interrupt under heavy
|
| +// load, this is set to a 20x of ProfileHandler timer interval (i.e 100Hz)
|
| +// TODO(nabeelmian) Under very heavy loads, the worker thread may not accumulate
|
| +// enough cpu usage to get a profile tick.
|
| +int kSleepInterval = 200000;
|
|
|
| -// Sleep interval in nano secs. To ensure that if the timer has expired it is
|
| -// reset.
|
| -int kTimerResetInterval = 5000000;
|
| -
|
| // Whether each thread has separate timers.
|
| static bool timer_separate_ = false;
|
| -static int timer_type_ = ITIMER_PROF;
|
| -static int signal_number_ = SIGPROF;
|
|
|
| -// Delays processing by the specified number of nano seconds. 'delay_ns'
|
| -// must be less than the number of nano seconds in a second (1000000000).
|
| -void Delay(int delay_ns) {
|
| - static const int kNumNSecInSecond = 1000000000;
|
| - EXPECT_LT(delay_ns, kNumNSecInSecond);
|
| - struct timespec delay = { 0, delay_ns };
|
| - nanosleep(&delay, 0);
|
| -}
|
| -
|
| // Checks whether the profile timer is enabled for the current thread.
|
| bool IsTimerEnabled() {
|
| itimerval current_timer;
|
| - EXPECT_EQ(0, getitimer(timer_type_, ¤t_timer));
|
| - if ((current_timer.it_value.tv_sec == 0) &&
|
| - (current_timer.it_value.tv_usec != 0)) {
|
| - // May be the timer has expired. Sleep for a bit and check again.
|
| - Delay(kTimerResetInterval);
|
| - EXPECT_EQ(0, getitimer(timer_type_, ¤t_timer));
|
| - }
|
| + EXPECT_EQ(0, getitimer(ITIMER_PROF, ¤t_timer));
|
| return (current_timer.it_value.tv_sec != 0 ||
|
| current_timer.it_value.tv_usec != 0);
|
| }
|
| @@ -183,15 +160,11 @@
|
|
|
| // Determines whether threads have separate timers.
|
| static void SetUpTestCase() {
|
| - timer_type_ = (getenv("CPUPROFILE_REALTIME") ? ITIMER_REAL : ITIMER_PROF);
|
| - signal_number_ = (getenv("CPUPROFILE_REALTIME") ? SIGALRM : SIGPROF);
|
| -
|
| timer_separate_ = threads_have_separate_timers();
|
| - Delay(kTimerResetInterval);
|
| }
|
|
|
| - // Sets up the profile timers and SIGPROF/SIGALRM handler in a known state.
|
| - // It does the following:
|
| + // Sets up the profile timers and SIGPROF handler in a known state. It does
|
| + // the following:
|
| // 1. Unregisters all the callbacks, stops the timer (if shared) and
|
| // clears out timer_sharing state in the ProfileHandler. This clears
|
| // out any state left behind by the previous test or during module
|
| @@ -203,7 +176,7 @@
|
| // Reset the state of ProfileHandler between each test. This unregisters
|
| // all callbacks, stops timer (if shared) and clears timer sharing state.
|
| ProfileHandlerReset();
|
| - EXPECT_EQ(0, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 0);
|
| VerifyDisabled();
|
| // ProfileHandler requires at least two threads to be registerd to determine
|
| // whether timers are shared.
|
| @@ -240,7 +213,7 @@
|
| busy_worker_->Start();
|
| // Wait for worker to start up and register with the ProfileHandler.
|
| // TODO(nabeelmian) This may not work under very heavy load.
|
| - Delay(kSleepInterval);
|
| + usleep(kSleepInterval);
|
| }
|
|
|
| // Stops the worker thread.
|
| @@ -250,10 +223,10 @@
|
| delete busy_worker_;
|
| }
|
|
|
| - // Checks whether SIGPROF/SIGALRM signal handler is enabled.
|
| + // Checks whether SIGPROF signal handler is enabled.
|
| bool IsSignalEnabled() {
|
| struct sigaction sa;
|
| - CHECK_EQ(sigaction(signal_number_, NULL, &sa), 0);
|
| + CHECK_EQ(sigaction(SIGPROF, NULL, &sa), 0);
|
| return ((sa.sa_handler == SIG_IGN) || (sa.sa_handler == SIG_DFL)) ?
|
| false : true;
|
| }
|
| @@ -284,7 +257,7 @@
|
| uint64 interrupts_before = GetInterruptCount();
|
| // Sleep for a bit and check that tick counter is making progress.
|
| int old_tick_count = tick_counter;
|
| - Delay(kSleepInterval);
|
| + usleep(kSleepInterval);
|
| int new_tick_count = tick_counter;
|
| EXPECT_GT(new_tick_count, old_tick_count);
|
| uint64 interrupts_after = GetInterruptCount();
|
| @@ -295,9 +268,9 @@
|
| void VerifyUnregistration(const int& tick_counter) {
|
| // Sleep for a bit and check that tick counter is not making progress.
|
| int old_tick_count = tick_counter;
|
| - Delay(kSleepInterval);
|
| + usleep(kSleepInterval);
|
| int new_tick_count = tick_counter;
|
| - EXPECT_EQ(old_tick_count, new_tick_count);
|
| + EXPECT_EQ(new_tick_count, old_tick_count);
|
| // If no callbacks, signal handler and shared timer should be disabled.
|
| if (GetCallbackCount() == 0) {
|
| EXPECT_FALSE(IsSignalEnabled());
|
| @@ -309,13 +282,13 @@
|
| }
|
| }
|
|
|
| - // Verifies that the SIGPROF/SIGALRM interrupt handler is disabled and the
|
| - // timer, if shared, is disabled. Expects the worker to be running.
|
| + // Verifies that the SIGPROF interrupt handler is disabled and the timer,
|
| + // if shared, is disabled. Expects the worker to be running.
|
| void VerifyDisabled() {
|
| // Check that the signal handler is disabled.
|
| EXPECT_FALSE(IsSignalEnabled());
|
| // Check that the callback count is 0.
|
| - EXPECT_EQ(0, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 0);
|
| // Check that the timer is disabled if shared, enabled otherwise.
|
| if (timer_separate_) {
|
| EXPECT_TRUE(IsTimerEnabled());
|
| @@ -324,27 +297,11 @@
|
| }
|
| // Verify that the ProfileHandler is not accumulating profile ticks.
|
| uint64 interrupts_before = GetInterruptCount();
|
| - Delay(kSleepInterval);
|
| + usleep(kSleepInterval);
|
| uint64 interrupts_after = GetInterruptCount();
|
| - EXPECT_EQ(interrupts_before, interrupts_after);
|
| + EXPECT_EQ(interrupts_after, interrupts_before);
|
| }
|
|
|
| - // Registers a callback and waits for kTimerResetInterval for timers to get
|
| - // reset.
|
| - ProfileHandlerToken* RegisterCallback(void* callback_arg) {
|
| - ProfileHandlerToken* token = ProfileHandlerRegisterCallback(
|
| - TickCounter, callback_arg);
|
| - Delay(kTimerResetInterval);
|
| - return token;
|
| - }
|
| -
|
| - // Unregisters a callback and waits for kTimerResetInterval for timers to get
|
| - // reset.
|
| - void UnregisterCallback(ProfileHandlerToken* token) {
|
| - ProfileHandlerUnregisterCallback(token);
|
| - Delay(kTimerResetInterval);
|
| - }
|
| -
|
| // Busy worker thread to accumulate cpu usage.
|
| BusyThread* busy_worker_;
|
|
|
| @@ -379,9 +336,10 @@
|
| // ProfileHandlerUnregisterCallback.
|
| TEST_F(ProfileHandlerTest, RegisterUnregisterCallback) {
|
| int tick_count = 0;
|
| - ProfileHandlerToken* token = RegisterCallback(&tick_count);
|
| + ProfileHandlerToken* token = ProfileHandlerRegisterCallback(
|
| + TickCounter, &tick_count);
|
| VerifyRegistration(tick_count);
|
| - UnregisterCallback(token);
|
| + ProfileHandlerUnregisterCallback(token);
|
| VerifyUnregistration(tick_count);
|
| }
|
|
|
| @@ -389,29 +347,31 @@
|
| TEST_F(ProfileHandlerTest, MultipleCallbacks) {
|
| // Register first callback.
|
| int first_tick_count;
|
| - ProfileHandlerToken* token1 = RegisterCallback(&first_tick_count);
|
| + ProfileHandlerToken* token1 = ProfileHandlerRegisterCallback(
|
| + TickCounter, &first_tick_count);
|
| // Check that callback was registered correctly.
|
| VerifyRegistration(first_tick_count);
|
| - EXPECT_EQ(1, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 1);
|
|
|
| // Register second callback.
|
| int second_tick_count;
|
| - ProfileHandlerToken* token2 = RegisterCallback(&second_tick_count);
|
| + ProfileHandlerToken* token2 = ProfileHandlerRegisterCallback(
|
| + TickCounter, &second_tick_count);
|
| // Check that callback was registered correctly.
|
| VerifyRegistration(second_tick_count);
|
| - EXPECT_EQ(2, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 2);
|
|
|
| // Unregister first callback.
|
| - UnregisterCallback(token1);
|
| + ProfileHandlerUnregisterCallback(token1);
|
| VerifyUnregistration(first_tick_count);
|
| - EXPECT_EQ(1, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 1);
|
| // Verify that second callback is still registered.
|
| VerifyRegistration(second_tick_count);
|
|
|
| // Unregister second callback.
|
| - UnregisterCallback(token2);
|
| + ProfileHandlerUnregisterCallback(token2);
|
| VerifyUnregistration(second_tick_count);
|
| - EXPECT_EQ(0, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 0);
|
|
|
| // Verify that the signal handler and timers are correctly disabled.
|
| VerifyDisabled();
|
| @@ -422,15 +382,15 @@
|
| // Verify that the profile timer interrupt is disabled.
|
| VerifyDisabled();
|
| int first_tick_count;
|
| - RegisterCallback(&first_tick_count);
|
| + ProfileHandlerRegisterCallback(TickCounter, &first_tick_count);
|
| VerifyRegistration(first_tick_count);
|
| - EXPECT_EQ(1, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 1);
|
|
|
| // Register second callback.
|
| int second_tick_count;
|
| - RegisterCallback(&second_tick_count);
|
| + ProfileHandlerRegisterCallback(TickCounter, &second_tick_count);
|
| VerifyRegistration(second_tick_count);
|
| - EXPECT_EQ(2, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 2);
|
|
|
| // Reset the profile handler and verify that callback were correctly
|
| // unregistered and timer/signal are disabled.
|
| @@ -449,7 +409,7 @@
|
| // the signal handler and reset the timer sharing state in the Profile
|
| // Handler.
|
| ProfileHandlerReset();
|
| - EXPECT_EQ(0, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 0);
|
| VerifyDisabled();
|
|
|
| // Start the worker. At this time ProfileHandler doesn't know if timers are
|
| @@ -457,14 +417,14 @@
|
| StartWorker();
|
| // Register a callback and check that profile ticks are being delivered.
|
| int tick_count;
|
| - RegisterCallback(&tick_count);
|
| - EXPECT_EQ(1, GetCallbackCount());
|
| + ProfileHandlerRegisterCallback(TickCounter, &tick_count);
|
| + EXPECT_EQ(GetCallbackCount(), 1);
|
| VerifyRegistration(tick_count);
|
|
|
| // Register a second thread and verify that timer and signal handler are
|
| // correctly enabled.
|
| RegisterThread();
|
| - EXPECT_EQ(1, GetCallbackCount());
|
| + EXPECT_EQ(GetCallbackCount(), 1);
|
| EXPECT_TRUE(IsTimerEnabled());
|
| EXPECT_TRUE(IsSignalEnabled());
|
| }
|
|
|