summaryrefslogtreecommitdiffstats
path: root/content/browser/power_profiler/power_data_provider_ia_win.cc
blob: e6509ea31f76832cb107aa43d2a49d3dccb0fccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/power_profiler/power_data_provider_ia_win.h"

#include "base/basictypes.h"
#include "base/logging.h"

namespace content {

// Default sampling period, as recommended by Intel Power Gadget.
// Section 3.1 of
// http://software.intel.com/en-us/blogs/2013/10/03/using-the-intel-power-gadget-api-on-windows
const int kDefaultSamplePeriodMs = 50;

scoped_ptr<PowerDataProvider> PowerDataProvider::Create() {
  scoped_ptr<PowerDataProviderIA> provider(new PowerDataProviderIA());
  if (provider->Initialize())
    return make_scoped_ptr<PowerDataProvider>(provider.release());

  return make_scoped_ptr<PowerDataProvider>(NULL);
}

PowerDataProviderIA::PowerDataProviderIA()
    : sockets_number_(0),
      is_open_(false) {
  for (int i = 0; i < PowerEvent::ID_COUNT; i++ )
      power_msr_ids_[i] = -1;
}

PowerDataProviderIA::~PowerDataProviderIA() {
}

PowerEventVector PowerDataProviderIA::GetData() {
  PowerEventVector events;

  if (!energy_lib_.ReadSample())
    return events;

  PowerEvent event;
  double package_power = 0.0;
  double power[3];
  int data_count;

  for (int i = 0; i < sockets_number_; i++) {
    if (power_msr_ids_[PowerEvent::SOC_PACKAGE] == -1)
      break;

    energy_lib_.GetPowerData(i,
        power_msr_ids_[PowerEvent::SOC_PACKAGE], power, &data_count);
    package_power += power[0];
  }

  event.type = PowerEvent::SOC_PACKAGE;
  event.value = package_power;
  event.time = base::TimeTicks::Now();
  events.push_back(event);

  return events;
}

base::TimeDelta PowerDataProviderIA::GetSamplingRate() {
  return base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs);
}

PowerDataProvider::AccuracyLevel PowerDataProviderIA::GetAccuracyLevel() {
  return High;
}

bool PowerDataProviderIA::Initialize() {
  if (is_open_)
    return true;

  if (!energy_lib_.IntelEnergyLibInitialize()) {
    LOG(ERROR) << "Power Data Provider initialize failed!";
    return false;
  }

  energy_lib_.GetNumNodes(&sockets_number_);

  const std::wstring package_msr_name = L"Processor";

  int msr_number;
  energy_lib_.GetNumMsrs(&msr_number);

  int func_id;
  wchar_t name[32];
  for(int i = 0; i < msr_number; i++) {
    energy_lib_.GetMsrFunc(i, &func_id);
    energy_lib_.GetMsrName(i, name);

    if (func_id != 1)
      continue;

    if (package_msr_name.compare(name) == 0)
      power_msr_ids_[PowerEvent::SOC_PACKAGE] = i;
  }

  is_open_ = true;
  return true;
}

}  // namespace content