Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / power_profiler / power_data_provider_ia_win.cc
blobe6509ea31f76832cb107aa43d2a49d3dccb0fccc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/power_profiler/power_data_provider_ia_win.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
10 namespace content {
12 // Default sampling period, as recommended by Intel Power Gadget.
13 // Section 3.1 of
14 // http://software.intel.com/en-us/blogs/2013/10/03/using-the-intel-power-gadget-api-on-windows
15 const int kDefaultSamplePeriodMs = 50;
17 scoped_ptr<PowerDataProvider> PowerDataProvider::Create() {
18 scoped_ptr<PowerDataProviderIA> provider(new PowerDataProviderIA());
19 if (provider->Initialize())
20 return make_scoped_ptr<PowerDataProvider>(provider.release());
22 return make_scoped_ptr<PowerDataProvider>(NULL);
25 PowerDataProviderIA::PowerDataProviderIA()
26 : sockets_number_(0),
27 is_open_(false) {
28 for (int i = 0; i < PowerEvent::ID_COUNT; i++ )
29 power_msr_ids_[i] = -1;
32 PowerDataProviderIA::~PowerDataProviderIA() {
35 PowerEventVector PowerDataProviderIA::GetData() {
36 PowerEventVector events;
38 if (!energy_lib_.ReadSample())
39 return events;
41 PowerEvent event;
42 double package_power = 0.0;
43 double power[3];
44 int data_count;
46 for (int i = 0; i < sockets_number_; i++) {
47 if (power_msr_ids_[PowerEvent::SOC_PACKAGE] == -1)
48 break;
50 energy_lib_.GetPowerData(i,
51 power_msr_ids_[PowerEvent::SOC_PACKAGE], power, &data_count);
52 package_power += power[0];
55 event.type = PowerEvent::SOC_PACKAGE;
56 event.value = package_power;
57 event.time = base::TimeTicks::Now();
58 events.push_back(event);
60 return events;
63 base::TimeDelta PowerDataProviderIA::GetSamplingRate() {
64 return base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs);
67 PowerDataProvider::AccuracyLevel PowerDataProviderIA::GetAccuracyLevel() {
68 return High;
71 bool PowerDataProviderIA::Initialize() {
72 if (is_open_)
73 return true;
75 if (!energy_lib_.IntelEnergyLibInitialize()) {
76 LOG(ERROR) << "Power Data Provider initialize failed!";
77 return false;
80 energy_lib_.GetNumNodes(&sockets_number_);
82 const std::wstring package_msr_name = L"Processor";
84 int msr_number;
85 energy_lib_.GetNumMsrs(&msr_number);
87 int func_id;
88 wchar_t name[32];
89 for(int i = 0; i < msr_number; i++) {
90 energy_lib_.GetMsrFunc(i, &func_id);
91 energy_lib_.GetMsrName(i, name);
93 if (func_id != 1)
94 continue;
96 if (package_msr_name.compare(name) == 0)
97 power_msr_ids_[PowerEvent::SOC_PACKAGE] = i;
100 is_open_ = true;
101 return true;
104 } // namespace content