40dc647b77978c527da99e7404ca80603b4a5876
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / linux_platform_backend.py
1 # Copyright (c) 2013 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.
4
5 import logging
6 import os
7 import subprocess
8 import sys
9
10 from telemetry import decorators
11 from telemetry.core import util
12 from telemetry.core.platform import platform_backend
13 from telemetry.core.platform import posix_platform_backend
14 from telemetry.core.platform import proc_supporting_platform_backend
15 from telemetry.page import cloud_storage
16
17
18 class LinuxPlatformBackend(
19     posix_platform_backend.PosixPlatformBackend,
20     proc_supporting_platform_backend.ProcSupportingPlatformBackend):
21
22   def StartRawDisplayFrameRateMeasurement(self):
23     raise NotImplementedError()
24
25   def StopRawDisplayFrameRateMeasurement(self):
26     raise NotImplementedError()
27
28   def GetRawDisplayFrameRateMeasurements(self):
29     raise NotImplementedError()
30
31   def IsThermallyThrottled(self):
32     raise NotImplementedError()
33
34   def HasBeenThermallyThrottled(self):
35     raise NotImplementedError()
36
37   def GetOSName(self):
38     return 'linux'
39
40   @decorators.Cache
41   def GetOSVersionName(self):
42     if not os.path.exists('/etc/lsb-release'):
43       raise NotImplementedError('Unknown Linux OS version')
44
45     codename = None
46     version = None
47     for line in open('/etc/lsb-release', 'r').readlines():
48       key, _, value = line.partition('=')
49       if key == 'DISTRIB_CODENAME':
50         codename = value.strip()
51       elif key == 'DISTRIB_RELEASE':
52         version = float(value)
53       if codename and version:
54         break
55     return platform_backend.OSVersion(codename, version)
56
57   def CanFlushIndividualFilesFromSystemCache(self):
58     return True
59
60   def FlushEntireSystemCache(self):
61     p = subprocess.Popen(['/sbin/sysctl', '-w', 'vm.drop_caches=3'])
62     p.wait()
63     assert p.returncode == 0, 'Failed to flush system cache'
64
65   def CanLaunchApplication(self, application):
66     if application == 'ipfw' and not self._IsIpfwKernelModuleInstalled():
67       return False
68     return super(LinuxPlatformBackend, self).CanLaunchApplication(application)
69
70   def InstallApplication(self, application):
71     if application == 'ipfw':
72       self._InstallIpfw()
73     elif application == 'avconv':
74       self._InstallAvconv()
75     else:
76       raise NotImplementedError(
77           'Please teach Telemetry how to install ' + application)
78
79   def _IsIpfwKernelModuleInstalled(self):
80     return 'ipfw_mod' in subprocess.Popen(
81         ['lsmod'], stdout=subprocess.PIPE).communicate()[0]
82
83   def _InstallIpfw(self):
84     ipfw_bin = os.path.join(util.GetTelemetryDir(), 'bin', 'ipfw')
85     ipfw_mod = os.path.join(util.GetTelemetryDir(), 'bin', 'ipfw_mod.ko')
86
87     try:
88       changed = cloud_storage.GetIfChanged(
89           ipfw_bin, cloud_storage.INTERNAL_BUCKET)
90       changed |= cloud_storage.GetIfChanged(
91           ipfw_mod, cloud_storage.INTERNAL_BUCKET)
92     except cloud_storage.CloudStorageError, e:
93       logging.error(e)
94       logging.error('You may proceed by manually installing dummynet. See: '
95                     'http://info.iet.unipi.it/~luigi/dummynet/')
96       sys.exit(1)
97
98     if changed or not self.CanLaunchApplication('ipfw'):
99       if not self._IsIpfwKernelModuleInstalled():
100         subprocess.check_call(['sudo', 'insmod', ipfw_mod])
101       os.chmod(ipfw_bin, 0755)
102       subprocess.check_call(['sudo', 'cp', ipfw_bin, '/usr/local/sbin'])
103
104     assert self.CanLaunchApplication('ipfw'), 'Failed to install ipfw'
105
106   def _InstallAvconv(self):
107     telemetry_bin_dir = os.path.join(util.GetTelemetryDir(), 'bin')
108     avconv_bin = os.path.join(telemetry_bin_dir, 'avconv')
109     os.environ['PATH'] += os.pathsep + telemetry_bin_dir
110
111     try:
112       cloud_storage.GetIfChanged(avconv_bin, cloud_storage.INTERNAL_BUCKET)
113     except cloud_storage.CloudStorageError, e:
114       logging.error(e)
115       logging.error('You may proceed by manually installing avconv via:\n'
116                     'sudo apt-get install libav-tools')
117       sys.exit(1)
118
119     assert self.CanLaunchApplication('avconv'), 'Failed to install avconv'