blob: 94f82f43065f7f2167d786ef40ced0721ed03667 [file] [log] [blame]
ilnik531100d2017-02-21 11:33:241/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/cpu_time.h"
Yves Gerey3e707812018-11-28 15:47:4912
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/platform_thread.h"
Steve Anton10542f22019-01-11 17:11:0014#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "system_wrappers/include/sleep.h"
16#include "test/gtest.h"
ilnik531100d2017-02-21 11:33:2417
deadbeef7311b242017-05-01 17:43:3918// 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
ilnik531100d2017-02-21 11:33:2427namespace {
28const int kAllowedErrorMillisecs = 30;
Ilya Nikolaevskiy7787ebc2020-01-14 09:25:3029const int kProcessingTimeMillisecs = 500;
ilnik78f2d212017-02-28 10:24:1030const int kWorkingThreads = 2;
ilnik531100d2017-02-21 11:33:2431
ilnik78f2d212017-02-28 10:24:1032// Consumes approximately kProcessingTimeMillisecs of CPU time in single thread.
Markus Handellad5037b2021-05-07 13:02:3633void WorkingFunction(int64_t* counter) {
ilnik531100d2017-02-21 11:33:2434 *counter = 0;
ilnik78f2d212017-02-28 10:24:1035 int64_t stop_cpu_time =
36 rtc::GetThreadCpuTimeNanos() +
37 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
38 while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) {
ilnik531100d2017-02-21 11:33:2439 (*counter)++;
40 }
ilnik531100d2017-02-21 11:33:2441}
42} // namespace
43
44namespace rtc {
45
deadbeef7311b242017-05-01 17:43:3946// 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.
48TEST(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
59TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) {
ilnik78f2d212017-02-28 10:24:1060 int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
61 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
ilnik531100d2017-02-21 11:33:2462 int64_t counter1;
63 int64_t counter2;
Markus Handellad5037b2021-05-07 13:02:3664 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();
ilnik531100d2017-02-21 11:33:2470
71 EXPECT_GE(counter1, 0);
72 EXPECT_GE(counter2, 0);
ilnik78f2d212017-02-28 10:24:1073 int64_t process_duration_nanos =
74 GetProcessCpuTimeNanos() - process_start_time_nanos;
75 int64_t thread_duration_nanos =
76 GetThreadCpuTimeNanos() - thread_start_time_nanos;
Ilya Nikolaevskiy7787ebc2020-01-14 09:25:3077 // This thread did almost nothing. Definetly less work than kProcessingTime.
ilnik78f2d212017-02-28 10:24:1078 // Therefore GetThreadCpuTime is not a wall clock.
79 EXPECT_LE(thread_duration_nanos,
Ilya Nikolaevskiy7787ebc2020-01-14 09:25:3080 (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
81 kNumNanosecsPerMillisec);
ilnik3e530d22017-03-09 08:41:3182 // Total process time is at least twice working threads' CPU time.
ilnik78f2d212017-02-28 10:24:1083 // Therefore process and thread times are correctly related.
Yves Gerey665174f2018-06-19 13:03:0584 EXPECT_GE(process_duration_nanos,
85 kWorkingThreads *
86 (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
87 kNumNanosecsPerMillisec);
ilnik531100d2017-02-21 11:33:2488}
89
deadbeef7311b242017-05-01 17:43:3990TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) {
ilnik78f2d212017-02-28 10:24:1091 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 Nikolaevskiy7787ebc2020-01-14 09:25:3098 (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
99 kNumNanosecsPerMillisec);
ilnik531100d2017-02-21 11:33:24100}
101
102} // namespace rtc