1 /*
2 * Copyright (c) 2012 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 // This test is flaky on Windows/Release.
27 // http://code.google.com/p/webrtc/issues/detail?id=290
28 #ifdef _WIN32
29 #define MAYBE_Usage DISABLED_Usage
30 #else
31 #define MAYBE_Usage Usage
32 #endif
TEST(CpuWrapperTest,MAYBE_Usage)33 TEST(CpuWrapperTest, MAYBE_Usage) {
34 Trace::CreateTrace();
35 std::string trace_file = webrtc::test::OutputPath() +
36 "cpu_wrapper_unittest.txt";
37 Trace::SetTraceFile(trace_file.c_str());
38 Trace::SetLevelFilter(webrtc::kTraceAll);
39 printf("Number of cores detected:%u\n", CpuInfo::DetectNumberOfCores());
40 scoped_ptr<CpuWrapper> cpu(CpuWrapper::CreateCpu());
41 ASSERT_TRUE(cpu.get() != NULL);
42 scoped_ptr<EventWrapper> sleep_event(EventWrapper::Create());
43 ASSERT_TRUE(sleep_event.get() != NULL);
44
45 int num_iterations = 0;
46 WebRtc_UWord32 num_cores = 0;
47 WebRtc_UWord32* cores = NULL;
48 bool cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1;
49 // Initializing the CPU measurements may take a couple of seconds on Windows.
50 // Since the initialization is lazy we need to wait until it is completed.
51 // Should not take more than 10000 ms.
52 while (!cpu_usage_available && (++num_iterations < 10000)) {
53 if (cores != NULL) {
54 ASSERT_GT(num_cores, 0u);
55 break;
56 }
57 sleep_event->Wait(1);
58 cpu_usage_available = cpu->CpuUsageMultiCore(num_cores, cores) != -1;
59 }
60 ASSERT_TRUE(cpu_usage_available);
61
62 const WebRtc_Word32 average = cpu->CpuUsageMultiCore(num_cores, cores);
63 ASSERT_TRUE(cores != NULL);
64 EXPECT_GT(num_cores, 0u);
65 EXPECT_GE(average, 0);
66 EXPECT_LE(average, 100);
67
68 printf("\nNumber of cores:%d\n", num_cores);
69 printf("Average cpu:%d\n", average);
70 for (WebRtc_UWord32 i = 0; i < num_cores; i++) {
71 printf("Core:%u CPU:%u \n", i, cores[i]);
72 EXPECT_GE(cores[i], 0u);
73 EXPECT_LE(cores[i], 100u);
74 }
75
76 Trace::ReturnTrace();
77 };
78