ilnik | 531100d | 2017-02-21 11:33:24 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/cpu_time.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/platform_thread.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 14 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "system_wrappers/include/sleep.h" |
| 16 | #include "test/gtest.h" |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 17 | |
deadbeef | 7311b24 | 2017-05-01 17:43:39 | [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 11:33:24 | [diff] [blame] | 27 | namespace { |
| 28 | const int kAllowedErrorMillisecs = 30; |
Ilya Nikolaevskiy | 7787ebc | 2020-01-14 09:25:30 | [diff] [blame] | 29 | const int kProcessingTimeMillisecs = 500; |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 30 | const int kWorkingThreads = 2; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 31 | |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 32 | // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread. |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 33 | void WorkingFunction(int64_t* counter) { |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 34 | *counter = 0; |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 35 | int64_t stop_cpu_time = |
| 36 | rtc::GetThreadCpuTimeNanos() + |
| 37 | kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; |
| 38 | while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) { |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 39 | (*counter)++; |
| 40 | } |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 41 | } |
| 42 | } // namespace |
| 43 | |
| 44 | namespace rtc { |
| 45 | |
deadbeef | 7311b24 | 2017-05-01 17:43:39 | [diff] [blame] | 46 | // A minimal test which can be run on instrumented builds, so that they're at |
| 47 | // least exercising the code to check for memory leaks/etc. |
| 48 | TEST(CpuTimeTest, BasicTest) { |
| 49 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 50 | int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
| 51 | int64_t process_duration_nanos = |
| 52 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 53 | int64_t thread_duration_nanos = |
| 54 | GetThreadCpuTimeNanos() - thread_start_time_nanos; |
| 55 | EXPECT_GE(process_duration_nanos, 0); |
| 56 | EXPECT_GE(thread_duration_nanos, 0); |
| 57 | } |
| 58 | |
| 59 | TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) { |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 60 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 61 | int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 62 | int64_t counter1; |
| 63 | int64_t counter2; |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 64 | auto thread1 = PlatformThread::SpawnJoinable( |
| 65 | [&counter1] { WorkingFunction(&counter1); }, "Thread1"); |
| 66 | auto thread2 = PlatformThread::SpawnJoinable( |
| 67 | [&counter2] { WorkingFunction(&counter2); }, "Thread2"); |
| 68 | thread1.Finalize(); |
| 69 | thread2.Finalize(); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 70 | |
| 71 | EXPECT_GE(counter1, 0); |
| 72 | EXPECT_GE(counter2, 0); |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 73 | int64_t process_duration_nanos = |
| 74 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 75 | int64_t thread_duration_nanos = |
| 76 | GetThreadCpuTimeNanos() - thread_start_time_nanos; |
Ilya Nikolaevskiy | 7787ebc | 2020-01-14 09:25:30 | [diff] [blame] | 77 | // This thread did almost nothing. Definetly less work than kProcessingTime. |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 78 | // Therefore GetThreadCpuTime is not a wall clock. |
| 79 | EXPECT_LE(thread_duration_nanos, |
Ilya Nikolaevskiy | 7787ebc | 2020-01-14 09:25:30 | [diff] [blame] | 80 | (kProcessingTimeMillisecs - kAllowedErrorMillisecs) * |
| 81 | kNumNanosecsPerMillisec); |
ilnik | 3e530d2 | 2017-03-09 08:41:31 | [diff] [blame] | 82 | // Total process time is at least twice working threads' CPU time. |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 83 | // Therefore process and thread times are correctly related. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 84 | EXPECT_GE(process_duration_nanos, |
| 85 | kWorkingThreads * |
| 86 | (kProcessingTimeMillisecs - kAllowedErrorMillisecs) * |
| 87 | kNumNanosecsPerMillisec); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 88 | } |
| 89 | |
deadbeef | 7311b24 | 2017-05-01 17:43:39 | [diff] [blame] | 90 | TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) { |
ilnik | 78f2d21 | 2017-02-28 10:24:10 | [diff] [blame] | 91 | int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
| 92 | webrtc::SleepMs(kProcessingTimeMillisecs); |
| 93 | int64_t process_duration_nanos = |
| 94 | GetProcessCpuTimeNanos() - process_start_time_nanos; |
| 95 | // Sleeping should not introduce any additional CPU time. |
| 96 | // Therefore GetProcessCpuTime is not a wall clock. |
| 97 | EXPECT_LE(process_duration_nanos, |
Ilya Nikolaevskiy | 7787ebc | 2020-01-14 09:25:30 | [diff] [blame] | 98 | (kProcessingTimeMillisecs - kAllowedErrorMillisecs) * |
| 99 | kNumNanosecsPerMillisec); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace rtc |