File: energy_impact_mac_unittest.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (256 lines) | stat: -rw-r--r-- 10,631 bytes parent folder | download | duplicates (9)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/power_metrics/energy_impact_mac.h"

#include "base/base_paths.h"
#include "base/path_service.h"
#include "components/power_metrics/resource_coalition_mac.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace power_metrics {

namespace {

constexpr mach_timebase_info_data_t kIntelTimebase = {1, 1};
constexpr mach_timebase_info_data_t kM1Timebase = {125, 3};

base::FilePath GetTestDataPath() {
  base::FilePath test_path;
  EXPECT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &test_path));
  test_path = test_path.Append(FILE_PATH_LITERAL("components"));
  test_path = test_path.Append(FILE_PATH_LITERAL("power_metrics"));
  test_path = test_path.Append(FILE_PATH_LITERAL("test"));
  test_path = test_path.Append(FILE_PATH_LITERAL("data"));
  return test_path;
}

coalition_resource_usage MakeResourceUsageWithQOS(int qos_level,
                                                  base::TimeDelta cpu_time) {
  coalition_resource_usage result{};
  result.cpu_time_eqos_len = COALITION_NUM_THREAD_QOS_TYPES;
  result.cpu_time_eqos[qos_level] = cpu_time.InNanoseconds();
  return result;
}

// Scales a time given in ns to mach_time in |timebase|.
uint64_t NsScaleToTimebase(const mach_timebase_info_data_t& timebase,
                           int64_t time_ns) {
  return time_ns * timebase.denom / timebase.numer;
}

}  // namespace

TEST(EnergyImpactTest, ReadCoefficientsFromPath) {
  base::FilePath test_path = GetTestDataPath();

  // Validate that attempting to read from a non-existent file fails.
  auto coefficients = internal::ReadCoefficientsFromPath(
      test_path.Append(FILE_PATH_LITERAL("does-not-exist.plist")));
  EXPECT_FALSE(coefficients.has_value());

  // Validate that a well-formed file returns the expected coefficients.
  coefficients = internal::ReadCoefficientsFromPath(
      test_path.Append(FILE_PATH_LITERAL("test.plist")));
  ASSERT_TRUE(coefficients.has_value());

  const EnergyImpactCoefficients& value = coefficients.value();
  EXPECT_FLOAT_EQ(value.kcpu_time, 1.23);
  EXPECT_FLOAT_EQ(value.kdiskio_bytesread, 7.89);
  EXPECT_FLOAT_EQ(value.kdiskio_byteswritten, 1.2345);
  EXPECT_FLOAT_EQ(value.kgpu_time, 6.789);
  EXPECT_FLOAT_EQ(value.knetwork_recv_bytes, 12.3);
  EXPECT_FLOAT_EQ(value.knetwork_recv_packets, 45.6);
  EXPECT_FLOAT_EQ(value.knetwork_sent_bytes, 67.8);
  EXPECT_FLOAT_EQ(value.knetwork_sent_packets, 89);
  EXPECT_FLOAT_EQ(value.kqos_background, 8.9);
  EXPECT_FLOAT_EQ(value.kqos_default, 6.78);
  EXPECT_FLOAT_EQ(value.kqos_legacy, 5.678);
  EXPECT_FLOAT_EQ(value.kqos_user_initiated, 9.012);
  EXPECT_FLOAT_EQ(value.kqos_user_interactive, 3.456);
  EXPECT_FLOAT_EQ(value.kqos_utility, 1.234);
  EXPECT_FLOAT_EQ(value.kcpu_wakeups, 3.45);
}

TEST(EnergyImpactTest, ReadCoefficientsForBoardIdOrDefault_Exists) {
  // This board-id should exist.
  auto coefficients = internal::ReadCoefficientsForBoardIdOrDefault(
      GetTestDataPath(), "Mac-7BA5B2DFE22DDD8C");
  ASSERT_TRUE(coefficients.has_value());

  // Validate that the default coefficients haven't been loaded.
  EXPECT_FLOAT_EQ(3.4, coefficients.value().kgpu_time);
  EXPECT_FLOAT_EQ(0.39, coefficients.value().kqos_background);
}

TEST(EnergyImpactTest, ReadCoefficientsForBoardIdOrDefault_Default) {
  // This board-id should not exist.
  auto coefficients = internal::ReadCoefficientsForBoardIdOrDefault(
      GetTestDataPath(), "Mac-031B6874CF7F642A");
  ASSERT_TRUE(coefficients.has_value());

  // Validate that the default coefficients were loaded.
  EXPECT_FLOAT_EQ(0, coefficients.value().kgpu_time);
  EXPECT_FLOAT_EQ(0.8, coefficients.value().kqos_background);
}

TEST(EnergyImpactTest,
     ReadCoefficientsForBoardIdOrDefault_NonExistentDirectory) {
  // This directory shouldn't exist, so nothing should be loaded.
  EXPECT_FALSE(
      internal::ReadCoefficientsForBoardIdOrDefault(
          GetTestDataPath().Append("nonexistent"), "Mac-7BA5B2DFE22DDD8C")
          .has_value());
}

TEST(EnergyImpactTest, GetBoardIdForThisMachine) {
  // This can't really be tested except that the contract holds one way
  // or the other.
  auto board_id = internal::GetBoardIdForThisMachine();
  if (board_id.has_value()) {
    EXPECT_FALSE(board_id.value().empty());
  }
}

// Verify the Energy Impact score when there is a single source of energy
// consumption (only one member set in `coalition_resource_usage`).
TEST(EnergyImpactTest, ComputeEnergyImpactForResourceUsage_Individual) {
  EXPECT_EQ(0.0, ComputeEnergyImpactForResourceUsage(coalition_resource_usage(),
                                                     EnergyImpactCoefficients{},
                                                     kIntelTimebase));

  // Test the coefficients and sample factors individually.
  EXPECT_DOUBLE_EQ(
      2.66, ComputeEnergyImpactForResourceUsage(
                coalition_resource_usage{.platform_idle_wakeups = 133},
                EnergyImpactCoefficients{
                    .kcpu_wakeups = base::Microseconds(200).InSecondsF()},
                kIntelTimebase));

  // Test 100 ms of CPU, which should come out to 8% of a CPU second with a
  // background QOS discount of rate of 0.8.
  EXPECT_DOUBLE_EQ(8.0, ComputeEnergyImpactForResourceUsage(
                            MakeResourceUsageWithQOS(THREAD_QOS_BACKGROUND,
                                                     base::Milliseconds(100)),
                            EnergyImpactCoefficients{.kqos_background = 0.8},
                            kIntelTimebase));
  EXPECT_DOUBLE_EQ(
      5.0,
      ComputeEnergyImpactForResourceUsage(
          MakeResourceUsageWithQOS(THREAD_QOS_DEFAULT, base::Milliseconds(50)),
          EnergyImpactCoefficients{.kqos_default = 1.0}, kIntelTimebase));
  EXPECT_DOUBLE_EQ(
      10.0,
      ComputeEnergyImpactForResourceUsage(
          MakeResourceUsageWithQOS(THREAD_QOS_UTILITY, base::Milliseconds(100)),
          EnergyImpactCoefficients{.kqos_utility = 1.0}, kIntelTimebase));
  EXPECT_DOUBLE_EQ(
      1.0,
      ComputeEnergyImpactForResourceUsage(
          MakeResourceUsageWithQOS(THREAD_QOS_LEGACY, base::Milliseconds(10)),
          EnergyImpactCoefficients{.kqos_legacy = 1.0}, kIntelTimebase));
  EXPECT_DOUBLE_EQ(1.0,
                   ComputeEnergyImpactForResourceUsage(
                       MakeResourceUsageWithQOS(THREAD_QOS_USER_INITIATED,
                                                base::Milliseconds(10)),
                       EnergyImpactCoefficients{.kqos_user_initiated = 1.0},
                       kIntelTimebase));
  EXPECT_DOUBLE_EQ(1.0,
                   ComputeEnergyImpactForResourceUsage(
                       MakeResourceUsageWithQOS(THREAD_QOS_USER_INTERACTIVE,
                                                base::Milliseconds(10)),
                       EnergyImpactCoefficients{.kqos_user_interactive = 1.0},
                       kIntelTimebase));
  EXPECT_DOUBLE_EQ(
      1.0, ComputeEnergyImpactForResourceUsage(
               coalition_resource_usage{
                   .gpu_time = base::Milliseconds(4).InNanoseconds()},
               EnergyImpactCoefficients{.kgpu_time = 2.5}, kIntelTimebase));
}

// Verify the Energy Impact score when there are multiple sources of energy
// consumption (multiple members set in `coalition_resource_usage`).
TEST(EnergyImpactTest, ComputeEnergyImpactForResourceUsage_Combined) {
  EnergyImpactCoefficients coefficients{
      .kcpu_wakeups = base::Microseconds(200).InSecondsF(),
      .kqos_default = 1.0,
      .kqos_background = 0.8,
      .kqos_utility = 1.0,
      .kqos_legacy = 1.0,
      .kqos_user_initiated = 1.0,
      .kqos_user_interactive = 1.0,
      .kgpu_time = 2.5,
  };
  coalition_resource_usage sample{
      .platform_idle_wakeups = 133,
      .gpu_time =
          NsScaleToTimebase(kM1Timebase, base::Milliseconds(4).InNanoseconds()),
      .cpu_time_eqos_len = COALITION_NUM_THREAD_QOS_TYPES,
  };
  sample.cpu_time_eqos[THREAD_QOS_BACKGROUND] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(100).InNanoseconds());
  sample.cpu_time_eqos[THREAD_QOS_DEFAULT] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(50).InNanoseconds());
  sample.cpu_time_eqos[THREAD_QOS_UTILITY] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(100).InNanoseconds());
  sample.cpu_time_eqos[THREAD_QOS_LEGACY] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(10).InNanoseconds());
  sample.cpu_time_eqos[THREAD_QOS_USER_INITIATED] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(10).InNanoseconds());
  sample.cpu_time_eqos[THREAD_QOS_USER_INTERACTIVE] =
      NsScaleToTimebase(kM1Timebase, base::Milliseconds(10).InNanoseconds());

  EXPECT_DOUBLE_EQ(29.66, ComputeEnergyImpactForResourceUsage(
                              sample, coefficients, kM1Timebase));
}

// Verify the Energy Impact score when fields of `coalition_resource_usage` that
// don't contribute to the score are set.
TEST(EnergyImpactTest, ComputeEnergyImpactForResourceUsage_Unused) {
  EnergyImpactCoefficients coefficients{
      .kdiskio_bytesread = 1000,
      .kdiskio_byteswritten = 1000,
      .knetwork_recv_bytes = 1000,
      .knetwork_recv_packets = 1000,
      .knetwork_sent_bytes = 1000,
      .knetwork_sent_packets = 1000,
  };
  coalition_resource_usage sample{
      .tasks_started = 1000,
      .tasks_exited = 1000,
      .time_nonempty = 1000,
      .cpu_time = 1000,
      .interrupt_wakeups = 1000,
      .bytesread = 1000,
      .byteswritten = 1000,
      .cpu_time_billed_to_me = 1000,
      .cpu_time_billed_to_others = 1000,
      .energy = 1000,
      .logical_immediate_writes = 1000,
      .logical_deferred_writes = 1000,
      .logical_invalidated_writes = 1000,
      .logical_metadata_writes = 1000,
      .logical_immediate_writes_to_external = 1000,
      .logical_deferred_writes_to_external = 1000,
      .logical_invalidated_writes_to_external = 1000,
      .logical_metadata_writes_to_external = 1000,
      .energy_billed_to_me = 1000,
      .energy_billed_to_others = 1000,
      .cpu_ptime = 1000,
      .cpu_instructions = 1000,
      .cpu_cycles = 1000,
      .fs_metadata_writes = 1000,
      .pm_writes = 1000,
  };

  EXPECT_EQ(0, ComputeEnergyImpactForResourceUsage(sample, coefficients,
                                                   kM1Timebase));
}

}  // namespace power_metrics