Commit 096c1e44 authored by nednguyen's avatar nednguyen Committed by Commit bot

Workaround telemetry CPU problem on Android.

Some devices remove cpu related files from /proc when the cores are
turned off. We assume the frequency is 0 if it's missing.

BUG=417259

Review URL: https://codereview.chromium.org/598093003

Cr-Commit-Position: refs/heads/master@{#296492}
parent 0b0be35d
...@@ -104,7 +104,10 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor): ...@@ -104,7 +104,10 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor):
for cpu in self._cpus: for cpu in self._cpus:
cpu_freq_path = os.path.join( cpu_freq_path = os.path.join(
CPU_PATH, cpu, 'cpufreq/stats/time_in_state') CPU_PATH, cpu, 'cpufreq/stats/time_in_state')
stats[cpu] = self._platform.RunCommand('cat %s' % cpu_freq_path) try:
stats[cpu] = self._platform.GetFileContents(cpu_freq_path)
except Exception:
stats[cpu] = None
return stats return stats
@staticmethod @staticmethod
...@@ -120,6 +123,9 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor): ...@@ -120,6 +123,9 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor):
sample_stats = {} sample_stats = {}
for cpu in sample: for cpu in sample:
frequencies = {} frequencies = {}
if sample[cpu] is None:
sample_stats[cpu] = None
continue
for line in sample[cpu].splitlines(): for line in sample[cpu].splitlines():
pair = line.split() pair = line.split()
freq = int(pair[0]) * 10 ** 3 freq = int(pair[0]) * 10 ** 3
...@@ -152,6 +158,9 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor): ...@@ -152,6 +158,9 @@ class SysfsPowerMonitor(power_monitor.PowerMonitor):
for cpu in initial: for cpu in initial:
current_cpu = {} current_cpu = {}
total = 0 total = 0
if initial[cpu] is None or final[cpu] is None:
cpu_stats[cpu] = collections.defaultdict(int)
continue
for state in initial[cpu]: for state in initial[cpu]:
current_cpu[state] = final[cpu][state] - initial[cpu][state] current_cpu[state] = final[cpu][state] - initial[cpu][state]
total += current_cpu[state] total += current_cpu[state]
......
...@@ -15,7 +15,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -15,7 +15,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
'cpu1': '1700000 11491\n1600000 0\n1500000 0\n1400000 248\n1300000 1166\n' 'cpu1': '1700000 11491\n1600000 0\n1500000 0\n1400000 248\n1300000 1166\n'
'1200000 2082\n1100000 2943\n1000000 6560\n900000 12517\n' '1200000 2082\n1100000 2943\n1000000 6560\n900000 12517\n'
'800000 8690\n700000 5105\n600000 3800\n500000 5131\n400000 5479\n' '800000 8690\n700000 5105\n600000 3800\n500000 5131\n400000 5479\n'
'300000 7571\n200000 133618' '300000 7571\n200000 133618',
'cpu2': '1700000 1131'
} }
final_freq = { final_freq = {
'cpu0': '1700000 7159\n1600000 0\n1500000 0\n1400000 68\n1300000 134\n' 'cpu0': '1700000 7159\n1600000 0\n1500000 0\n1400000 68\n1300000 134\n'
...@@ -25,7 +26,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -25,7 +26,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
'cpu1': '1700000 12048\n1600000 0\n1500000 0\n1400000 280\n1300000 1267\n' 'cpu1': '1700000 12048\n1600000 0\n1500000 0\n1400000 280\n1300000 1267\n'
'1200000 2272\n1100000 3163\n1000000 7039\n900000 13800\n' '1200000 2272\n1100000 3163\n1000000 7039\n900000 13800\n'
'800000 9599\n700000 5655\n600000 4144\n500000 5655\n400000 6005\n' '800000 9599\n700000 5655\n600000 4144\n500000 5655\n400000 6005\n'
'300000 8288\n200000 149724' '300000 8288\n200000 149724',
'cpu2': None
} }
expected_initial_freq = { expected_initial_freq = {
'cpu0': { 'cpu0': {
...@@ -63,6 +65,9 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -63,6 +65,9 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
400000000: 5479, 400000000: 5479,
300000000: 7571, 300000000: 7571,
200000000: 133618 200000000: 133618
},
'cpu2': {
1700000000: 1131
} }
} }
expected_final_freq = { expected_final_freq = {
...@@ -101,7 +106,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -101,7 +106,8 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
400000000: 6005, 400000000: 6005,
300000000: 8288, 300000000: 8288,
200000000: 149724 200000000: 149724
} },
'cpu2': None
} }
expected_freq_percents = { expected_freq_percents = {
'whole_package': { 'whole_package': {
...@@ -157,8 +163,12 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -157,8 +163,12 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
400000000: 2.3338361877717633, 400000000: 2.3338361877717633,
300000000: 3.1812938148904073, 300000000: 3.1812938148904073,
200000000: 71.46153163546012 200000000: 71.46153163546012
},
'cpu2': {
1700000000: 0.0,
} }
} }
def testParseCpuFreq(self): def testParseCpuFreq(self):
initial = sysfs_power_monitor.SysfsPowerMonitor.ParseFreqSample( initial = sysfs_power_monitor.SysfsPowerMonitor.ParseFreqSample(
self.initial_freq) self.initial_freq)
...@@ -170,7 +180,7 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase): ...@@ -170,7 +180,7 @@ class SysfsPowerMonitorMonitorTest(unittest.TestCase):
def testComputeCpuStats(self): def testComputeCpuStats(self):
results = sysfs_power_monitor.SysfsPowerMonitor.ComputeCpuStats( results = sysfs_power_monitor.SysfsPowerMonitor.ComputeCpuStats(
self.expected_initial_freq, self.expected_final_freq) self.expected_initial_freq, self.expected_final_freq)
for cpu in results: for cpu in self.expected_freq_percents:
for freq in results[cpu]: for freq in results[cpu]:
self.assertAlmostEqual(results[cpu][freq], self.assertAlmostEqual(results[cpu][freq],
self.expected_freq_percents[cpu][freq]) self.expected_freq_percents[cpu][freq])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment