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" 14 #include "system_wrappers/interface/cpu_info.h" 15 #include "system_wrappers/interface/event_wrapper.h" 16 #include "system_wrappers/interface/scoped_ptr.h" 17 #include "system_wrappers/interface/trace.h" 18 #include "testsupport/fileutils.h" 19 20 using webrtc::CpuInfo; 21 using webrtc::CpuWrapper; 22 using webrtc::EventWrapper; 23 using webrtc::scoped_ptr; 24 using webrtc::Trace; 25 26 TEST(CpuWrapperTest, Usage) { 27 Trace::CreateTrace(); 28 std::string trace_file = webrtc::test::OutputPath() + 29 "cpu_wrapper_unittest.txt"; 30 Trace::SetTraceFile(trace_file.c_str()); 31 Trace::SetLevelFilter(webrtc::kTraceAll); 32 printf("Number of cores detected:%u\n", CpuInfo::DetectNumberOfCores()); 33 scoped_ptr<CpuWrapper> cpu(CpuWrapper::CreateCpu()); 34 ASSERT_TRUE(cpu.get() != NULL); 35 scoped_ptr<EventWrapper> sleep_event(EventWrapper::Create()); 36 ASSERT_TRUE(sleep_event.get() != NULL); 37 38 int num_iterations = 0; 39 WebRtc_UWord32 num_cores = 0; 40 WebRtc_UWord32* cores = NULL; 41 bool cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1; 42 // Initializing the CPU measurements may take a couple of seconds on Windows. 43 // Since the initialization is lazy we need to wait until it is completed. 44 // Should not take more than 10000 ms. 45 while (cpu_usage_available && (++num_iterations < 10000)) { 46 if (cores != NULL) { 47 ASSERT_GT(num_cores, 0u); 48 break; 49 } 50 sleep_event->Wait(1); 51 cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1; 52 } 53 ASSERT_TRUE(cpu_usage_available); 54 55 const WebRtc_Word32 average = cpu->CpuUsageMultiCore(num_cores, cores); 56 ASSERT_TRUE(cores != NULL); 57 EXPECT_GT(num_cores, 0u); 58 EXPECT_GE(average, 0); 59 EXPECT_LE(average, 100); 60 61 printf("\nNumber of cores:%d\n", num_cores); 62 printf("Average cpu:%d\n", average); 63 for (WebRtc_UWord32 i = 0; i < num_cores; i++) { 64 printf("Core:%u CPU:%u \n", i, cores[i]); 65 EXPECT_GE(cores[i], 0u); 66 EXPECT_LE(cores[i], 100u); 67 } 68 69 Trace::ReturnTrace(); 70 }; 71