3 # Copyright 2015 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 # This is a script developers can use to set-up their workstation to let
8 # Telemetry read the CPU's Model Specific Registers in order to get power
9 # measurements. It can check if reading from MSRs is possible as any user, but
10 # must run as root to make changes. Not all changes are sticky, so one has to
11 # re-run this script after each reboot.
13 # This script is currently Debian/Ubuntu specific.
19 MSR_DEV_FILE_PATH
= '/dev/cpu/0/msr'
20 RDMSR_PATH
= '/usr/sbin/rdmsr'
22 def _Usage(prog_name
):
23 """Print a help message."""
24 print 'Run "%s" as a regular user to check if reading from the MSR ' \
25 'is possible.' % prog_name
26 print 'Run "%s enable" as root to automatically set up reading from ' \
27 'the MSR.' % prog_name
30 def _CheckMsrKernelModule():
31 """Return whether the 'msr' kernel module is loaded."""
32 proc
= subprocess
.Popen('/sbin/lsmod', stdout
=subprocess
.PIPE
)
33 stdout
= proc
.communicate()[0]
36 raise OSError('lsmod failed')
38 if not any([line
.startswith('msr ') for line
in stdout
.splitlines()]):
39 print 'Error: MSR module not loaded.'
45 def _CheckMsrDevNodes():
46 """Check whether the MSR /dev files have the right permissions."""
47 if not os
.path
.exists(MSR_DEV_FILE_PATH
):
48 print 'Error: %s does not exist.' % MSR_DEV_FILE_PATH
51 if not os
.access(MSR_DEV_FILE_PATH
, os
.R_OK
):
52 print 'Error: Cannot read from %s' % MSR_DEV_FILE_PATH
59 """Check and make sure /usr/sbin/rdmsr is set up correctly."""
60 if not os
.access(RDMSR_PATH
, os
.X_OK
):
61 print 'Error: %s missing or not executable.' % RDMSR_PATH
64 proc
= subprocess
.Popen(['/sbin/getcap', RDMSR_PATH
], stdout
=subprocess
.PIPE
)
65 stdout
= proc
.communicate()[0]
68 raise OSError('getcap failed')
70 if not 'cap_sys_rawio+ep' in stdout
:
71 print 'Error: /usr/sbin/rdmsr needs RAWIO capability.'
78 """Check to make sure it is possible to read from the MSRs."""
80 print 'WARNING: Running as root, msr permission check likely inaccurate.'
82 has_dev_node
= _CheckMsrDevNodes() if _CheckMsrKernelModule() else False
83 has_rdmsr
= _CheckRdmsr()
84 return has_dev_node
and has_rdmsr
87 def _EnableMsr(prog_name
):
88 """Do all the setup needed to pass _RunAllChecks().
90 Needs to run as root."""
92 print 'Error: Must run "%s enable" as root.' % prog_name
95 print 'Loading msr kernel module.'
96 ret
= subprocess
.call(['/sbin/modprobe', 'msr'])
98 print 'Error: Cannot load msr module.'
101 print 'Running chmod on %s.' % MSR_DEV_FILE_PATH
102 ret
= subprocess
.call(['/bin/chmod', 'a+r', MSR_DEV_FILE_PATH
])
104 print 'Error: Cannot chmod %s.' % MSR_DEV_FILE_PATH
107 if not os
.access(RDMSR_PATH
, os
.F_OK
):
108 print 'Need to install the msr-tools package.'
109 ret
= subprocess
.call(['/usr/bin/apt-get', 'install', '-y', 'msr-tools'])
111 print 'Error: Did not successfully install msr-tools.'
114 print 'Running setcap on %s.' % RDMSR_PATH
115 ret
= subprocess
.call(['/sbin/setcap', 'cap_sys_rawio+ep', RDMSR_PATH
])
117 print 'Error: Cannot give /usr/sbin/rdmsr RAWIO capability.'
123 def main(prog_name
, argv
):
126 print 'Check succeeded'
129 print 'Check failed, try running "%s enable" as root to fix.' % prog_name
133 if argv
[0] == 'enable':
134 return 0 if _EnableMsr(prog_name
) else 1
136 print 'Error: Unknown sub-command %s' % argv
[0]
140 print 'Error: Bad number of arguments'
145 if '__main__' == __name__
:
146 sys
.exit(main(os
.path
.basename(sys
.argv
[0]), sys
.argv
[1:]))