OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include "webrtc/base/cpu_time.h" | 13 #include "webrtc/base/cpu_time.h" |
14 #include "webrtc/base/platform_thread.h" | 14 #include "webrtc/base/platform_thread.h" |
15 #include "webrtc/base/timeutils.h" | 15 #include "webrtc/base/timeutils.h" |
16 #include "webrtc/test/gtest.h" | 16 #include "webrtc/test/gtest.h" |
17 #include "webrtc/system_wrappers/include/cpu_info.h" | 17 #include "webrtc/system_wrappers/include/cpu_info.h" |
18 #include "webrtc/system_wrappers/include/sleep.h" | |
18 | 19 |
19 namespace { | 20 namespace { |
20 const int kAllowedErrorMillisecs = 30; | 21 const int kAllowedErrorMillisecs = 30; |
21 const int kProcessingTimeMillisecs = 300; | 22 const int kProcessingTimeMillisecs = 300; |
23 const int kWorkingThreads = 2; | |
22 | 24 |
23 // Consumes approximately kProcessingTimeMillisecs of CPU time. | 25 // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread. |
24 bool WorkingFunction(void* counter_pointer) { | 26 bool WorkingFunction(void* counter_pointer) { |
25 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); | 27 int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer); |
26 *counter = 0; | 28 *counter = 0; |
27 int64_t stop_time = rtc::SystemTimeNanos() + | 29 int64_t stop_cpu_time = |
28 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; | 30 rtc::GetThreadCpuTimeNanos() + |
29 while (rtc::SystemTimeNanos() < stop_time) { | 31 kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec; |
32 while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) { | |
30 (*counter)++; | 33 (*counter)++; |
31 } | 34 } |
32 return false; | 35 return false; |
33 } | 36 } |
34 } // namespace | 37 } // namespace |
35 | 38 |
36 namespace rtc { | 39 namespace rtc { |
37 | 40 |
38 TEST(GetProcessCpuTimeTest, SingleThread) { | 41 TEST(CpuTimeTest, TwoThreads) { |
39 int64_t start_time_nanos = GetProcessCpuTimeNanos(); | 42 int64_t process_start_time_nanos = GetProcessCpuTimeNanos(); |
40 int64_t counter; | 43 int64_t thread_start_time_nanos = GetThreadCpuTimeNanos(); |
41 WorkingFunction(reinterpret_cast<void*>(&counter)); | |
42 EXPECT_GT(counter, 0); | |
43 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos; | |
44 // Should be about kProcessingTimeMillisecs. | |
45 EXPECT_NEAR(duration_nanos, | |
46 kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
47 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
48 } | |
49 | |
50 TEST(GetProcessCpuTimeTest, TwoThreads) { | |
51 int64_t start_time_nanos = GetProcessCpuTimeNanos(); | |
52 int64_t counter1; | 44 int64_t counter1; |
53 int64_t counter2; | 45 int64_t counter2; |
54 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), | 46 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), |
55 "Thread1"); | |
56 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), | |
57 "Thread2"); | |
58 thread1.Start(); | |
59 thread2.Start(); | |
60 thread1.Stop(); | |
61 thread2.Stop(); | |
62 | |
63 EXPECT_GE(counter1, 0); | |
64 EXPECT_GE(counter2, 0); | |
65 int64_t duration_nanos = GetProcessCpuTimeNanos() - start_time_nanos; | |
66 const uint32_t kWorkingThreads = 2; | |
67 uint32_t used_cores = | |
68 std::min(webrtc::CpuInfo::DetectNumberOfCores(), kWorkingThreads); | |
69 // Two working threads for kProcessingTimeMillisecs consume double CPU time | |
70 // if there are at least 2 cores. | |
71 EXPECT_NEAR(duration_nanos, | |
72 used_cores * kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
73 used_cores * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
74 } | |
75 | |
76 TEST(GetThreadCpuTimeTest, SingleThread) { | |
77 int64_t start_times_nanos = GetThreadCpuTimeNanos(); | |
78 int64_t counter; | |
79 WorkingFunction(reinterpret_cast<void*>(&counter)); | |
80 EXPECT_GT(counter, 0); | |
81 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_times_nanos; | |
82 EXPECT_NEAR(duration_nanos, | |
83 kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
84 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
85 } | |
86 | |
87 TEST(GetThreadCpuTimeTest, TwoThreads) { | |
88 int64_t start_time_nanos = GetThreadCpuTimeNanos(); | |
89 int64_t counter1; | |
90 int64_t counter2; | |
91 PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1), | |
92 "Thread1"); | 47 "Thread1"); |
93 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), | 48 PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2), |
94 "Thread2"); | 49 "Thread2"); |
95 thread1.Start(); | 50 thread1.Start(); |
96 thread2.Start(); | 51 thread2.Start(); |
97 thread1.Stop(); | 52 thread1.Stop(); |
98 thread2.Stop(); | 53 thread2.Stop(); |
99 | 54 |
100 EXPECT_GE(counter1, 0); | 55 EXPECT_GE(counter1, 0); |
101 EXPECT_GE(counter2, 0); | 56 EXPECT_GE(counter2, 0); |
102 int64_t duration_nanos = GetThreadCpuTimeNanos() - start_time_nanos; | 57 int64_t process_duration_nanos = |
103 // This thread didn't do any work. | 58 GetProcessCpuTimeNanos() - process_start_time_nanos; |
104 EXPECT_NEAR(duration_nanos, 0, | 59 int64_t thread_duration_nanos = |
105 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | 60 GetThreadCpuTimeNanos() - thread_start_time_nanos; |
61 // This thread did almost nothing. | |
62 // Therefore GetThreadCpuTime is not a wall clock. | |
63 EXPECT_LE(thread_duration_nanos, | |
64 kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
65 // Total process time is twice working threads' CPU time. | |
66 // Therefore process and thread times are correctly related. | |
67 EXPECT_NEAR( | |
68 process_duration_nanos, | |
69 kWorkingThreads * kProcessingTimeMillisecs * kNumNanosecsPerMillisec, | |
70 kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
71 webrtc::SleepMs(kProcessingTimeMillisecs); | |
Taylor Brandstetter
2017/02/28 09:46:39
nit: Checking the time before and after sleeping c
ilnik
2017/02/28 09:53:47
Done.
| |
72 int64_t process_duration_after_wait_nanos = | |
73 GetProcessCpuTimeNanos() - process_start_time_nanos; | |
74 // Sleeping should not introduce any additional CPU time. | |
75 // Therefore GetProcessCpuTime is not a wall clock. | |
76 EXPECT_NEAR( | |
77 process_duration_after_wait_nanos, process_duration_nanos, | |
78 kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec); | |
106 } | 79 } |
107 | 80 |
108 } // namespace rtc | 81 } // namespace rtc |
OLD | NEW |