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