kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
| 11 | #include "system_wrappers/interface/cpu_wrapper.h" |
| 12 | |
| 13 | #include "gtest/gtest.h" |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 14 | #include "system_wrappers/interface/cpu_info.h" |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 15 | #include "system_wrappers/interface/event_wrapper.h" |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 16 | #include "system_wrappers/interface/trace.h" |
| 17 | |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 18 | using webrtc::CpuInfo; |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 19 | using webrtc::CpuWrapper; |
| 20 | using webrtc::Trace; |
| 21 | |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 22 | // Only utilizes some of the cpu_info.h and cpu_wrapper.h code. Does not verify |
| 23 | // anything except that it doesn't crash. |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 24 | // TODO(kjellander): Improve this test so it verifies the implementation |
| 25 | // executes as expected. |
| 26 | TEST(CpuWrapperTest, Usage) { |
| 27 | Trace::CreateTrace(); |
| 28 | Trace::SetTraceFile("cpu_wrapper_unittest.txt"); |
| 29 | Trace::SetLevelFilter(webrtc::kTraceAll); |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 30 | printf("Number of cores detected:%u\n", CpuInfo::DetectNumberOfCores()); |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 31 | CpuWrapper* cpu = CpuWrapper::CreateCpu(); |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 32 | ASSERT_TRUE(cpu != NULL); |
| 33 | webrtc::EventWrapper* sleep_event = webrtc::EventWrapper::Create(); |
| 34 | ASSERT_TRUE(sleep_event != NULL); |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 35 | |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 36 | int num_iterations = 0; |
| 37 | WebRtc_UWord32 num_cores = 0; |
| 38 | WebRtc_UWord32* cores = NULL; |
| 39 | bool cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1; |
| 40 | // Initializing the CPU measurements may take a couple of seconds on Windows. |
| 41 | // Since the initialization is lazy we need to wait until it is completed. |
| 42 | // Should not take more than 10000 ms. |
| 43 | while (cpu_usage_available && (++num_iterations < 10000)) { |
| 44 | if (cores != NULL) { |
henrike@webrtc.org | ce9d89d | 2011-11-19 00:14:37 +0000 | [diff] [blame^] | 45 | ASSERT_GT(num_cores, 0u); |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 46 | break; |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 47 | } |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 48 | sleep_event->Wait(1); |
| 49 | cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1; |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 50 | } |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 51 | ASSERT_TRUE(cpu_usage_available); |
| 52 | |
| 53 | const WebRtc_Word32 total = cpu->CpuUsageMultiCore(num_cores, cores); |
| 54 | ASSERT_TRUE(cores != NULL); |
henrike@webrtc.org | ce9d89d | 2011-11-19 00:14:37 +0000 | [diff] [blame^] | 55 | EXPECT_GT(num_cores, 0u); |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 56 | EXPECT_GE(total, 0); |
| 57 | |
| 58 | printf("\nNumCores:%d\n", num_cores); |
| 59 | printf("Total cpu:%d\n", total); |
| 60 | for (WebRtc_UWord32 i = 0; i < num_cores; i++) { |
| 61 | printf("Core:%u CPU:%u \n", i, cores[i]); |
henrike@webrtc.org | ce9d89d | 2011-11-19 00:14:37 +0000 | [diff] [blame^] | 62 | EXPECT_LE(cores[i], static_cast<WebRtc_UWord32> (total)); |
henrike@webrtc.org | ad98a3e | 2011-11-18 23:55:12 +0000 | [diff] [blame] | 63 | } |
| 64 | |
kjellander@webrtc.org | 177bb52 | 2011-10-31 17:10:01 +0000 | [diff] [blame] | 65 | delete cpu; |
| 66 | Trace::ReturnTrace(); |
| 67 | }; |