Commit 8da2981f authored by thestig's avatar thestig Committed by Commit bot

Telemetry: Linux-based backends should return CPU results in seconds, not jiffies.

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

Cr-Commit-Position: refs/heads/master@{#302004}
parent 2c62c78d
......@@ -33,7 +33,7 @@ class AndroidPlatformBackendTest(unittest.TestCase):
backend = android_platform_backend.AndroidPlatformBackend(
android_device.AndroidDevice('12345'))
cpu_stats = backend.GetCpuStats('7702')
self.assertEquals(cpu_stats, {'CpuProcessTime': 5.0})
self.assertEquals(cpu_stats, {'CpuProcessTime': 0.05})
@benchmark.Disabled('chromeos')
def testGetCpuStatsInvalidPID(self):
......
......@@ -14,7 +14,9 @@ from telemetry.core.platform import platform_backend
class LinuxBasedPlatformBackend(platform_backend.PlatformBackend):
"""Abstract platform containing functionality shared by all linux based OSes.
"""Abstract platform containing functionality shared by all Linux based OSes.
This includes Android and ChromeOS.
Subclasses must implement RunCommand, GetFileContents, GetPsOutput, and
ParseCStateSample."""
......@@ -38,19 +40,23 @@ class LinuxBasedPlatformBackend(platform_backend.PlatformBackend):
return self._ConvertKbToByte(meminfo['MemTotal'])
def GetCpuStats(self, pid):
results = {}
stats = self._GetProcFileForPid(pid, 'stat')
if not stats:
return {}
return results
stats = stats.split()
utime = float(stats[13])
stime = float(stats[14])
cpu_process_jiffies = utime + stime
return {'CpuProcessTime': cpu_process_jiffies}
clock_ticks = self.GetClockTicks()
results.update({'CpuProcessTime': cpu_process_jiffies / clock_ticks})
return results
def GetCpuTimestamp(self):
timer_list = self.GetFileContents('/proc/timer_list')
total_jiffies = float(self._GetProcJiffies(timer_list))
return {'TotalTime': total_jiffies}
clock_ticks = self.GetClockTicks()
return {'TotalTime': total_jiffies / clock_ticks}
def GetMemoryStats(self, pid):
status_contents = self._GetProcFileForPid(pid, 'status')
......@@ -84,6 +90,16 @@ class LinuxBasedPlatformBackend(platform_backend.PlatformBackend):
'ReadTransferCount': int(io['rchar']),
'WriteTransferCount': int(io['wchar'])}
@decorators.Cache
def GetClockTicks(self):
"""Returns the number of clock ticks per second.
The proper way is to call os.sysconf('SC_CLK_TCK') but that is not easy to
do on Android/CrOS. In practice, nearly all Linux machines have a USER_HZ
of 100, so just return that.
"""
return 100
def GetFileContents(self, filename):
raise NotImplementedError()
......
......@@ -23,21 +23,45 @@ class TestBackend(linux_based_platform_backend.LinuxBasedPlatformBackend):
def GetFileContents(self, filename):
return self._mock_files[filename]
def GetClockTicks(self):
return 41
class LinuxBasedPlatformBackendTest(unittest.TestCase):
def SetMockFileInBackend(self, backend, real_file, mock_file):
with open(os.path.join(util.GetUnittestDataDir(), real_file)) as f:
backend.SetMockFile(mock_file, f.read())
def testGetCpuStatsBasic(self):
if not linux_based_platform_backend.resource:
logging.warning('Test not supported')
return
backend = TestBackend()
self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat')
result = backend.GetCpuStats(1)
self.assertEquals(result, {'CpuProcessTime': 22.0})
def testGetCpuTimestampBasic(self):
if not linux_based_platform_backend.resource:
logging.warning('Test not supported')
return
backend = TestBackend()
self.SetMockFileInBackend(backend, 'timer_list', '/proc/timer_list')
result = backend.GetCpuTimestamp()
self.assertEquals(result, {'TotalTime': 105054633.0})
def testGetMemoryStatsBasic(self):
if not linux_based_platform_backend.resource:
logging.warning('Test not supported')
return
backend = TestBackend()
with open(os.path.join(util.GetUnittestDataDir(), 'stat')) as f:
backend.SetMockFile('/proc/1/stat', f.read())
with open(os.path.join(util.GetUnittestDataDir(), 'status')) as f:
backend.SetMockFile('/proc/1/status', f.read())
with open(os.path.join(util.GetUnittestDataDir(), 'smaps')) as f:
backend.SetMockFile('/proc/1/smaps', f.read())
self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat')
self.SetMockFileInBackend(backend, 'status', '/proc/1/status')
self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps')
result = backend.GetMemoryStats(1)
self.assertEquals(result, {'PrivateDirty': 5324800,
'VM': 1025978368,
......@@ -51,12 +75,9 @@ class LinuxBasedPlatformBackendTest(unittest.TestCase):
return
backend = TestBackend()
with open(os.path.join(util.GetUnittestDataDir(), 'stat')) as f:
backend.SetMockFile('/proc/1/stat', f.read())
with open(os.path.join(util.GetUnittestDataDir(), 'status_nohwm')) as f:
backend.SetMockFile('/proc/1/status', f.read())
with open(os.path.join(util.GetUnittestDataDir(), 'smaps')) as f:
backend.SetMockFile('/proc/1/smaps', f.read())
self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat')
self.SetMockFileInBackend(backend, 'status_nohwm', '/proc/1/status')
self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps')
result = backend.GetMemoryStats(1)
self.assertEquals(result, {'PrivateDirty': 5324800,
'VM': 1025978368,
......
Timer List Version: v0.7
HRTIMER_MAX_CLOCK_BASES: 4
now at 49390649032656 nsecs
cpu: 0
clock 0:
.base: 0000000000000000
.index: 0
.resolution: 1 nsecs
.get_time: ktime_get
.offset: 0 nsecs
active timers:
#0: <0000000000000000>, hrtimer_wakeup, S:01, schedule_hrtimeout_range_clock, census_census_h/2343
# expires at 49390651724646-49390651774646 nsecs [in 2691990 to 2741990 nsecs]
#1: <0000000000000000>, hrtimer_wakeup, S:01, schedule_hrtimeout_range_clock, census_census_h/3680
# expires at 49390656983466-49390657033466 nsecs [in 7950810 to 8000810 nsecs]
clock 1:
.base: 0000000000000000
.index: 1
.resolution: 1 nsecs
.get_time: ktime_get_real
.offset: 1414432541083798144 nsecs
active timers:
#0: <0000000000000000>, hrtimer_wakeup, S:01, hrtimer_start_range_ns, mysqld/27245
# expires at 1414481934522781000-1414481934522831000 nsecs [in 1414432543873748344 to 1414432543873798344 nsecs]
#1: <0000000000000000>, hrtimer_wakeup, S:01, hrtimer_start_range_ns, DNS Res~ver #14/21520
# expires at 1414482222918140000-1414482222918190000 nsecs [in 1414432832269107344 to 1414432832269157344 nsecs]
clock 2:
.base: 0000000000000000
.index: 2
.resolution: 1 nsecs
.get_time: ktime_get_boottime
.offset: 0 nsecs
active timers:
clock 3:
.base: 0000000000000000
.index: 3
.resolution: 1 nsecs
.get_time: ktime_get_clocktai
.offset: 1414432541083798144 nsecs
active timers:
.expires_next : 49390651774646 nsecs
.hres_active : 1
.nr_events : 9793846
.nr_retries : 580
.nr_hangs : 0
.max_hang_time : 0 nsecs
.nohz_mode : 2
.last_tick : 49390648000000 nsecs
.tick_stopped : 1
.idle_jiffies : 4307239958
.idle_calls : 16879086
.idle_sleeps : 15799778
.idle_entrytime : 49390647721066 nsecs
.idle_waketime : 49390647714831 nsecs
.idle_exittime : 49390647489308 nsecs
.idle_sleeptime : 44326049938322 nsecs
.iowait_sleeptime: 1007311574977 nsecs
.last_jiffies : 4307239958
.next_jiffies : 4307239968
.idle_expires : 49390684000000 nsecs
jiffies: 4307239953
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