ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/cpu_time.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/platform_thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 14 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "system_wrappers/include/sleep.h" |
| 16 | #include "test/gtest.h" |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 17 | |
deadbeef | 7311b24 | 2017-05-01 10:43:39 -0700 | [diff] [blame] | 18 | // Only run these tests on non-instrumented builds, because timing on |
| 19 | // instrumented builds is unreliable, causing the test to be flaky. |
| 20 | #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
| 21 | defined(ADDRESS_SANITIZER) |
| 22 | #define MAYBE_TEST(test_name) DISABLED_##test_name |
| 23 | #else |
| 24 | #define MAYBE_TEST(test_name) test_name |
| 25 | #endif |
| 26 | |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 27 | namespace { |
| 28 | const int kAllowedErrorMillisecs = 30; |
| 29 | const int kProcessingTimeMillisecs = 300; |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 30 | const int kWorkingThreads = 2; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 31 | |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 32 | // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread. |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 33 | bool WorkingFunction(void* counter_pointer) { |
| 34 | int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); |
| 35 | *counter = 0; |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 36 | int64_t stop_cpu_time = |
| 37 | rtc::GetThreadCpuTimeNanos() + |
| 38 | kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; |
| 39 | while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) { |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 40 | (*counter)++; |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | } // namespace |
| 45 | |
| 46 | namespace rtc { |
| 47 | |
deadbeef | 7311b24 | 2017-05-01 10:43:39 -0700 | [diff] [blame] | 48 | // A minimal test which can be run on instrumented builds, so that they're at |
| 49 | // least exercising the code to check for memory leaks/etc. |
| 50 | TEST(CpuTimeTest, BasicTest) { |
| 51 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 52 | int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
| 53 | int64_t process_duration_nanos = |
| 54 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 55 | int64_t thread_duration_nanos = |
| 56 | GetThreadCpuTimeNanos() - thread_start_time_nanos; |
| 57 | EXPECT_GE(process_duration_nanos, 0); |
| 58 | EXPECT_GE(thread_duration_nanos, 0); |
| 59 | } |
| 60 | |
| 61 | TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) { |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 62 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 63 | int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 64 | int64_t counter1; |
| 65 | int64_t counter2; |
| 66 | PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), |
| 67 | "Thread1"); |
| 68 | PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), |
| 69 | "Thread2"); |
| 70 | thread1.Start(); |
| 71 | thread2.Start(); |
| 72 | thread1.Stop(); |
| 73 | thread2.Stop(); |
| 74 | |
| 75 | EXPECT_GE(counter1, 0); |
| 76 | EXPECT_GE(counter2, 0); |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 77 | int64_t process_duration_nanos = |
| 78 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 79 | int64_t thread_duration_nanos = |
| 80 | GetThreadCpuTimeNanos() - thread_start_time_nanos; |
| 81 | // This thread did almost nothing. |
| 82 | // Therefore GetThreadCpuTime is not a wall clock. |
| 83 | EXPECT_LE(thread_duration_nanos, |
| 84 | kAllowedErrorMillisecs * kNumNanosecsPerMillisec); |
ilnik | 3e530d2 | 2017-03-09 00:41:31 -0800 | [diff] [blame] | 85 | // Total process time is at least twice working threads' CPU time. |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 86 | // Therefore process and thread times are correctly related. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 87 | EXPECT_GE(process_duration_nanos, |
| 88 | kWorkingThreads * |
| 89 | (kProcessingTimeMillisecs - kAllowedErrorMillisecs) * |
| 90 | kNumNanosecsPerMillisec); |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 91 | } |
| 92 | |
deadbeef | 7311b24 | 2017-05-01 10:43:39 -0700 | [diff] [blame] | 93 | TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) { |
ilnik | 78f2d21 | 2017-02-28 02:24:10 -0800 | [diff] [blame] | 94 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 95 | webrtc::SleepMs(kProcessingTimeMillisecs); |
| 96 | int64_t process_duration_nanos = |
| 97 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 98 | // Sleeping should not introduce any additional CPU time. |
| 99 | // Therefore GetProcessCpuTime is not a wall clock. |
| 100 | EXPECT_LE(process_duration_nanos, |
| 101 | kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace rtc |