blob: 16ad8648d4e600872f57ba7a6312fd12c4089c14 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001"""
mbligh2da4d882009-01-21 19:05:40 +00002DO NOT import this file directly - import client/bin/utils.py,
3which will mix this in
4
mbligh53da18e2009-01-05 21:13:26 +00005Convenience functions for use by tests or whomever.
mblighc86b0b42006-07-28 17:35:28 +00006
mbligh2da4d882009-01-21 19:05:40 +00007Note that this file is mixed in by utils.py - note very carefully the
8precedence order defined there
mbligh53da18e2009-01-05 21:13:26 +00009"""
Daniel Kurtzf278e852014-03-04 21:57:30 -080010import os, shutil, commands, pickle, glob
11import math, re, fnmatch, logging, multiprocessing
lmr959e8162010-04-16 06:15:34 +000012from autotest_lib.client.common_lib import error, utils, magic
mblighea397bb2008-02-02 19:17:51 +000013
14
mblighf4c35322006-03-13 01:01:10 +000015def grep(pattern, file):
jadmanski0afbb632008-06-06 21:10:57 +000016 """
17 This is mainly to fix the return code inversion from grep
18 Also handles compressed files.
mbligh7bdbfbd2006-09-30 16:47:01 +000019
jadmanski0afbb632008-06-06 21:10:57 +000020 returns 1 if the pattern is present in the file, 0 if not.
21 """
22 command = 'grep "%s" > /dev/null' % pattern
23 ret = cat_file_to_cmd(file, command, ignore_status=True)
24 return not ret
mblighaf4efc22008-01-25 16:33:59 +000025
26
mblighc86b0b42006-07-28 17:35:28 +000027def difflist(list1, list2):
jadmanski0afbb632008-06-06 21:10:57 +000028 """returns items in list2 that are not in list1"""
29 diff = [];
30 for x in list2:
31 if x not in list1:
32 diff.append(x)
33 return diff
mblighf4c35322006-03-13 01:01:10 +000034
mblighc86b0b42006-07-28 17:35:28 +000035
mbligh8ea61e22008-05-09 18:09:37 +000036def cat_file_to_cmd(file, command, ignore_status=0, return_output=False):
jadmanski0afbb632008-06-06 21:10:57 +000037 """
38 equivalent to 'cat file | command' but knows to use
39 zcat or bzcat if appropriate
40 """
mblighf2fa4712008-09-10 20:36:29 +000041 if not os.path.isfile(file):
42 raise NameError('invalid file %s to cat to command %s'
43 % (file, command))
44
jadmanski0afbb632008-06-06 21:10:57 +000045 if return_output:
mbligh2da4d882009-01-21 19:05:40 +000046 run_cmd = utils.system_output
jadmanski0afbb632008-06-06 21:10:57 +000047 else:
mbligh2da4d882009-01-21 19:05:40 +000048 run_cmd = utils.system
mbligh96ffb9b2008-03-28 14:35:42 +000049
lmr959e8162010-04-16 06:15:34 +000050 if magic.guess_type(file) == 'application/x-bzip2':
mblighf2fa4712008-09-10 20:36:29 +000051 cat = 'bzcat'
lmr959e8162010-04-16 06:15:34 +000052 elif magic.guess_type(file) == 'application/x-gzip':
mblighf2fa4712008-09-10 20:36:29 +000053 cat = 'zcat'
jadmanski0afbb632008-06-06 21:10:57 +000054 else:
mblighf2fa4712008-09-10 20:36:29 +000055 cat = 'cat'
56 return run_cmd('%s %s | %s' % (cat, file, command),
57 ignore_status=ignore_status)
mblighf4c35322006-03-13 01:01:10 +000058
mblighc86b0b42006-07-28 17:35:28 +000059
mbligh712cd142006-04-22 18:57:50 +000060def extract_tarball_to_dir(tarball, dir):
jadmanski0afbb632008-06-06 21:10:57 +000061 """
62 Extract a tarball to a specified directory name instead of whatever
63 the top level of a tarball is - useful for versioned directory names, etc
64 """
65 if os.path.exists(dir):
lmr959e8162010-04-16 06:15:34 +000066 if os.path.isdir(dir):
67 shutil.rmtree(dir)
68 else:
69 os.remove(dir)
jadmanski0afbb632008-06-06 21:10:57 +000070 pwd = os.getcwd()
71 os.chdir(os.path.dirname(os.path.abspath(dir)))
72 newdir = extract_tarball(tarball)
73 os.rename(newdir, dir)
74 os.chdir(pwd)
mbligh712cd142006-04-22 18:57:50 +000075
76
mbligh712cd142006-04-22 18:57:50 +000077def extract_tarball(tarball):
jadmanski0afbb632008-06-06 21:10:57 +000078 """Returns the directory extracted by the tarball."""
79 extracted = cat_file_to_cmd(tarball, 'tar xvf - 2>/dev/null',
80 return_output=True).splitlines()
mbligh5d423ca2008-04-16 23:06:12 +000081
jadmanski0afbb632008-06-06 21:10:57 +000082 dir = None
mbligh5d423ca2008-04-16 23:06:12 +000083
jadmanski0afbb632008-06-06 21:10:57 +000084 for line in extracted:
David Sharp77e01212015-03-06 15:01:06 -080085 if line.startswith('./'):
86 line = line[2:]
jadmanski0afbb632008-06-06 21:10:57 +000087 if not line or line == '.':
88 continue
89 topdir = line.split('/')[0]
90 if os.path.isdir(topdir):
91 if dir:
92 assert(dir == topdir)
93 else:
94 dir = topdir
95 if dir:
96 return dir
97 else:
98 raise NameError('extracting tarball produced no dir')
mbligh712cd142006-04-22 18:57:50 +000099
mblighcdf02a42006-04-23 02:22:49 +0000100
lmrd60882f2010-02-04 03:26:36 +0000101def unmap_url_cache(cachedir, url, expected_hash, method="md5"):
mbligh53da18e2009-01-05 21:13:26 +0000102 """
jadmanski0afbb632008-06-06 21:10:57 +0000103 Downloads a file from a URL to a cache directory. If the file is already
lmrd60882f2010-02-04 03:26:36 +0000104 at the expected position and has the expected hash, let's not download it
105 again.
106
107 @param cachedir: Directory that might hold a copy of the file we want to
108 download.
109 @param url: URL for the file we want to download.
110 @param expected_hash: Hash string that we expect the file downloaded to
111 have.
112 @param method: Method used to calculate the hash string (md5, sha1).
jadmanski0afbb632008-06-06 21:10:57 +0000113 """
114 # Let's convert cachedir to a canonical path, if it's not already
115 cachedir = os.path.realpath(cachedir)
116 if not os.path.isdir(cachedir):
117 try:
lmrd60882f2010-02-04 03:26:36 +0000118 os.makedirs(cachedir)
jadmanski0afbb632008-06-06 21:10:57 +0000119 except:
120 raise ValueError('Could not create cache directory %s' % cachedir)
121 file_from_url = os.path.basename(url)
122 file_local_path = os.path.join(cachedir, file_from_url)
lmrd60882f2010-02-04 03:26:36 +0000123
124 file_hash = None
125 failure_counter = 0
126 while not file_hash == expected_hash:
127 if os.path.isfile(file_local_path):
128 file_hash = hash_file(file_local_path, method)
129 if file_hash == expected_hash:
130 # File is already at the expected position and ready to go
131 src = file_from_url
132 else:
133 # Let's download the package again, it's corrupted...
134 logging.error("Seems that file %s is corrupted, trying to "
Daniel Kurtzf278e852014-03-04 21:57:30 -0800135 "download it again", file_from_url)
lmrd60882f2010-02-04 03:26:36 +0000136 src = url
137 failure_counter += 1
jadmanski0afbb632008-06-06 21:10:57 +0000138 else:
lmrd60882f2010-02-04 03:26:36 +0000139 # File is not there, let's download it
jadmanski0afbb632008-06-06 21:10:57 +0000140 src = url
lmrd60882f2010-02-04 03:26:36 +0000141 if failure_counter > 1:
142 raise EnvironmentError("Consistently failed to download the "
143 "package %s. Aborting further download "
144 "attempts. This might mean either the "
145 "network connection has problems or the "
146 "expected hash string that was determined "
Daniel Kurtzf278e852014-03-04 21:57:30 -0800147 "for this file is wrong", file_from_url)
lmrd60882f2010-02-04 03:26:36 +0000148 file_path = utils.unmap_url(cachedir, src, cachedir)
149
150 return file_path
mblighea30c8a2006-04-22 22:24:25 +0000151
152
mblighf4c35322006-03-13 01:01:10 +0000153def force_copy(src, dest):
jadmanski0afbb632008-06-06 21:10:57 +0000154 """Replace dest with a new copy of src, even if it exists"""
155 if os.path.isfile(dest):
156 os.remove(dest)
157 if os.path.isdir(dest):
158 dest = os.path.join(dest, os.path.basename(src))
159 shutil.copyfile(src, dest)
160 return dest
mblighf4c35322006-03-13 01:01:10 +0000161
162
mblighfdbcaec2006-10-01 23:28:57 +0000163def force_link(src, dest):
jadmanski0afbb632008-06-06 21:10:57 +0000164 """Link src to dest, overwriting it if it exists"""
mbligh2da4d882009-01-21 19:05:40 +0000165 return utils.system("ln -sf %s %s" % (src, dest))
mblighfdbcaec2006-10-01 23:28:57 +0000166
167
mblighcdf02a42006-04-23 02:22:49 +0000168def file_contains_pattern(file, pattern):
jadmanski0afbb632008-06-06 21:10:57 +0000169 """Return true if file contains the specified egrep pattern"""
170 if not os.path.isfile(file):
171 raise NameError('file %s does not exist' % file)
mbligh2da4d882009-01-21 19:05:40 +0000172 return not utils.system('egrep -q "' + pattern + '" ' + file, ignore_status=True)
mblighcdf02a42006-04-23 02:22:49 +0000173
174
175def list_grep(list, pattern):
jadmanski0afbb632008-06-06 21:10:57 +0000176 """True if any item in list matches the specified pattern."""
177 compiled = re.compile(pattern)
178 for line in list:
179 match = compiled.search(line)
180 if (match):
181 return 1
182 return 0
mblighcdf02a42006-04-23 02:22:49 +0000183
mbligh987071e2008-06-13 16:39:12 +0000184
mbligh42b81ca2006-09-30 22:10:01 +0000185def get_os_vendor():
jadmanski0afbb632008-06-06 21:10:57 +0000186 """Try to guess what's the os vendor
187 """
mbligh90b97ff2009-03-10 20:29:01 +0000188 if os.path.isfile('/etc/SuSE-release'):
189 return 'SUSE'
190
jadmanski0afbb632008-06-06 21:10:57 +0000191 issue = '/etc/issue'
mblighaf4efc22008-01-25 16:33:59 +0000192
jadmanski0afbb632008-06-06 21:10:57 +0000193 if not os.path.isfile(issue):
194 return 'Unknown'
mblighaf4efc22008-01-25 16:33:59 +0000195
jadmanski0afbb632008-06-06 21:10:57 +0000196 if file_contains_pattern(issue, 'Red Hat'):
197 return 'Red Hat'
mbligh2f29c192009-01-12 20:39:17 +0000198 elif file_contains_pattern(issue, 'Fedora'):
jadmanski0afbb632008-06-06 21:10:57 +0000199 return 'Fedora Core'
200 elif file_contains_pattern(issue, 'SUSE'):
201 return 'SUSE'
202 elif file_contains_pattern(issue, 'Ubuntu'):
203 return 'Ubuntu'
204 elif file_contains_pattern(issue, 'Debian'):
205 return 'Debian'
206 else:
207 return 'Unknown'
mblighaf4efc22008-01-25 16:33:59 +0000208
mblighcdf02a42006-04-23 02:22:49 +0000209
Eric Li8a12e802011-02-17 14:24:13 -0800210def get_cc():
211 try:
212 return os.environ['CC']
213 except KeyError:
214 return 'gcc'
215
216
mblighf49d5cf2006-04-23 02:24:42 +0000217def get_vmlinux():
jadmanski0afbb632008-06-06 21:10:57 +0000218 """Return the full path to vmlinux
mblighc86b0b42006-07-28 17:35:28 +0000219
jadmanski0afbb632008-06-06 21:10:57 +0000220 Ahem. This is crap. Pray harder. Bad Martin.
221 """
mbligh2da4d882009-01-21 19:05:40 +0000222 vmlinux = '/boot/vmlinux-%s' % utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000223 if os.path.isfile(vmlinux):
224 return vmlinux
mbligh2da4d882009-01-21 19:05:40 +0000225 vmlinux = '/lib/modules/%s/build/vmlinux' % utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000226 if os.path.isfile(vmlinux):
227 return vmlinux
228 return None
mblighf49d5cf2006-04-23 02:24:42 +0000229
230
231def get_systemmap():
jadmanski0afbb632008-06-06 21:10:57 +0000232 """Return the full path to System.map
mblighc86b0b42006-07-28 17:35:28 +0000233
jadmanski0afbb632008-06-06 21:10:57 +0000234 Ahem. This is crap. Pray harder. Bad Martin.
235 """
mbligh2da4d882009-01-21 19:05:40 +0000236 map = '/boot/System.map-%s' % utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000237 if os.path.isfile(map):
238 return map
mbligh2da4d882009-01-21 19:05:40 +0000239 map = '/lib/modules/%s/build/System.map' % utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000240 if os.path.isfile(map):
241 return map
242 return None
mbligh67b5ece2006-04-23 02:53:12 +0000243
244
245def get_modules_dir():
jadmanski0afbb632008-06-06 21:10:57 +0000246 """Return the modules dir for the running kernel version"""
mbligh2da4d882009-01-21 19:05:40 +0000247 kernel_version = utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000248 return '/lib/modules/%s/kernel' % kernel_version
mblighf49d5cf2006-04-23 02:24:42 +0000249
250
David Sharpeb5e7a32014-10-29 15:44:39 -0700251_CPUINFO_RE = re.compile(r'^(?P<key>[^\t]*)\t*: ?(?P<value>.*)$')
252
David Sharpf9483792015-03-06 13:56:39 -0800253
David Sharpeb5e7a32014-10-29 15:44:39 -0700254def get_cpuinfo():
255 """Read /proc/cpuinfo and convert to a list of dicts."""
256 cpuinfo = []
257 with open('/proc/cpuinfo', 'r') as f:
258 cpu = {}
259 for line in f:
260 line = line.strip()
261 if not line:
262 cpuinfo.append(cpu)
263 cpu = {}
264 continue
265 match = _CPUINFO_RE.match(line)
266 cpu[match.group('key')] = match.group('value')
267 if cpu:
268 # cpuinfo usually ends in a blank line, so this shouldn't happen.
269 cpuinfo.append(cpu)
270 return cpuinfo
271
David Sharpf9483792015-03-06 13:56:39 -0800272
mbligh5970cf02006-08-06 15:39:22 +0000273def get_cpu_arch():
jadmanski0afbb632008-06-06 21:10:57 +0000274 """Work out which CPU architecture we're running on"""
275 f = open('/proc/cpuinfo', 'r')
276 cpuinfo = f.readlines()
277 f.close()
278 if list_grep(cpuinfo, '^cpu.*(RS64|POWER3|Broadband Engine)'):
279 return 'power'
280 elif list_grep(cpuinfo, '^cpu.*POWER4'):
281 return 'power4'
282 elif list_grep(cpuinfo, '^cpu.*POWER5'):
283 return 'power5'
284 elif list_grep(cpuinfo, '^cpu.*POWER6'):
285 return 'power6'
mbligh0758c822009-09-18 19:37:35 +0000286 elif list_grep(cpuinfo, '^cpu.*POWER7'):
287 return 'power7'
jadmanski0afbb632008-06-06 21:10:57 +0000288 elif list_grep(cpuinfo, '^cpu.*PPC970'):
289 return 'power970'
mblighf23fd922010-02-25 18:45:45 +0000290 elif list_grep(cpuinfo, 'ARM'):
291 return 'arm'
mbligh12e8b892008-09-08 21:39:41 +0000292 elif list_grep(cpuinfo, '^flags.*:.* lm .*'):
jadmanski0afbb632008-06-06 21:10:57 +0000293 return 'x86_64'
HS Liaod24bc852015-07-15 18:59:06 +0800294 elif list_grep(cpuinfo, 'CPU.*implementer.*0x41'):
295 return 'arm'
jadmanski0afbb632008-06-06 21:10:57 +0000296 else:
297 return 'i386'
mblighf4c35322006-03-13 01:01:10 +0000298
David Sharpf9483792015-03-06 13:56:39 -0800299
Haixia Shi82dd9042016-09-27 18:10:53 -0700300def get_arm_soc_family_from_devicetree():
301 """
302 Work out which ARM SoC we're running on based on the 'compatible' property
303 of the base node of devicetree, if it exists.
304 """
305 devicetree_compatible = '/sys/firmware/devicetree/base/compatible'
306 if not os.path.isfile(devicetree_compatible):
307 return None
308 f = open(devicetree_compatible, 'r')
309 compatible = f.readlines()
310 f.close()
311 if list_grep(compatible, 'rk3399'):
312 return 'rockchip'
313 elif list_grep(compatible, 'mt8173'):
314 return 'mediatek'
315 return None
316
317
Daniel Kurtz7c6ee782014-03-03 18:14:33 -0800318def get_arm_soc_family():
319 """Work out which ARM SoC we're running on"""
Haixia Shi82dd9042016-09-27 18:10:53 -0700320 family = get_arm_soc_family_from_devicetree()
321 if family is not None:
322 return family
323
Daniel Kurtz7c6ee782014-03-03 18:14:33 -0800324 f = open('/proc/cpuinfo', 'r')
325 cpuinfo = f.readlines()
326 f.close()
327 if list_grep(cpuinfo, 'EXYNOS5'):
328 return 'exynos5'
329 elif list_grep(cpuinfo, 'Tegra'):
330 return 'tegra'
ZhengShunQianaec3bf52014-09-24 15:14:41 +0800331 elif list_grep(cpuinfo, 'Rockchip'):
332 return 'rockchip'
Daniel Kurtz7c6ee782014-03-03 18:14:33 -0800333 return 'arm'
334
David Sharpf9483792015-03-06 13:56:39 -0800335
Daniel Kurtz17113742014-03-05 14:10:03 -0800336def get_cpu_soc_family():
Daniel Kurtz7c6ee782014-03-03 18:14:33 -0800337 """Like get_cpu_arch, but for ARM, returns the SoC family name"""
338 family = get_cpu_arch()
339 if family == 'arm':
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700340 family = get_arm_soc_family()
Daniel Kurtz7c6ee782014-03-03 18:14:33 -0800341 return family
mblighf4c35322006-03-13 01:01:10 +0000342
David Sharpf9483792015-03-06 13:56:39 -0800343
David Sharpeb5e7a32014-10-29 15:44:39 -0700344INTEL_UARCH_TABLE = {
David Sharpeb5e7a32014-10-29 15:44:39 -0700345 '06_1C': 'Atom',
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700346 '06_26': 'Atom',
347 '06_36': 'Atom',
348 '06_4C': 'Braswell',
David Sharpeb5e7a32014-10-29 15:44:39 -0700349 '06_3D': 'Broadwell',
David Sharpeb5e7a32014-10-29 15:44:39 -0700350 '06_0D': 'Dothan',
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700351 '06_3A': 'IvyBridge',
352 '06_3E': 'IvyBridge',
353 '06_3C': 'Haswell',
354 '06_3F': 'Haswell',
355 '06_45': 'Haswell',
356 '06_46': 'Haswell',
357 '06_0F': 'Merom',
358 '06_16': 'Merom',
359 '06_17': 'Nehalem',
360 '06_1A': 'Nehalem',
361 '06_1D': 'Nehalem',
362 '06_1E': 'Nehalem',
363 '06_1F': 'Nehalem',
364 '06_2E': 'Nehalem',
365 '06_2A': 'SandyBridge',
366 '06_2D': 'SandyBridge',
367 '06_4E': 'Skylake',
368 '0F_03': 'Prescott',
369 '0F_04': 'Prescott',
370 '0F_06': 'Presler',
371 '06_25': 'Westmere',
372 '06_2C': 'Westmere',
373 '06_2F': 'Westmere',
David Sharpeb5e7a32014-10-29 15:44:39 -0700374}
375
David Sharpf9483792015-03-06 13:56:39 -0800376
David Sharpeb5e7a32014-10-29 15:44:39 -0700377def get_intel_cpu_uarch(numeric=False):
378 """Return the Intel microarchitecture we're running on, or None.
379
380 Returns None if this is not an Intel CPU. Returns the family and model as
381 underscore-separated hex (per Intel manual convention) if the uarch is not
382 known, or if numeric is True.
383 """
384 if not get_current_kernel_arch().startswith('x86'):
385 return None
386 cpuinfo = get_cpuinfo()[0]
387 if cpuinfo['vendor_id'] != 'GenuineIntel':
388 return None
389 family_model = '%02X_%02X' % (int(cpuinfo['cpu family']),
390 int(cpuinfo['model']))
391 if numeric:
392 return family_model
393 return INTEL_UARCH_TABLE.get(family_model, family_model)
394
395
mbligh548f29a2006-10-17 04:55:12 +0000396def get_current_kernel_arch():
jadmanski0afbb632008-06-06 21:10:57 +0000397 """Get the machine architecture, now just a wrap of 'uname -m'."""
398 return os.popen('uname -m').read().rstrip()
mblighcdf02a42006-04-23 02:22:49 +0000399
400
mblighfdbcaec2006-10-01 23:28:57 +0000401def get_file_arch(filename):
jadmanski0afbb632008-06-06 21:10:57 +0000402 # -L means follow symlinks
mbligh2da4d882009-01-21 19:05:40 +0000403 file_data = utils.system_output('file -L ' + filename)
jadmanski0afbb632008-06-06 21:10:57 +0000404 if file_data.count('80386'):
405 return 'i386'
406 return None
mblighfdbcaec2006-10-01 23:28:57 +0000407
408
mblighf4c35322006-03-13 01:01:10 +0000409def count_cpus():
jadmanski0afbb632008-06-06 21:10:57 +0000410 """number of CPUs in the local machine according to /proc/cpuinfo"""
cychiang46589b12012-10-03 11:19:33 +0800411 try:
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700412 return multiprocessing.cpu_count()
cychiang46589b12012-10-03 11:19:33 +0800413 except Exception as e:
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700414 logging.exception('can not get cpu count from'
cychiang46589b12012-10-03 11:19:33 +0800415 ' multiprocessing.cpu_count()')
David Sharpf9483792015-03-06 13:56:39 -0800416 cpuinfo = get_cpuinfo()
cychiang46589b12012-10-03 11:19:33 +0800417 # Returns at least one cpu. Check comment #1 in crosbug.com/p/9582.
David Sharpf9483792015-03-06 13:56:39 -0800418 return len(cpuinfo) or 1
419
420
421def cpu_online_map():
422 """
423 Check out the available cpu online map
424 """
425 cpuinfo = get_cpuinfo()
426 cpus = []
427 for cpu in cpuinfo:
Ilja H. Friedel59b3bc72015-05-18 13:53:28 -0700428 cpus.append(cpu['processor']) # grab cpu number
David Sharpf9483792015-03-06 13:56:39 -0800429 return cpus
430
431
432def get_cpu_family():
433 cpuinfo = get_cpuinfo()[0]
434 return int(cpuinfo['cpu_family'])
435
436
437def get_cpu_vendor():
438 cpuinfo = get_cpuinfo()
439 vendors = [cpu['vendor_id'] for cpu in cpuinfo]
440 for v in vendors[1:]:
441 if v != vendors[0]:
442 raise error.TestError('multiple cpu vendors found: ' + str(vendors))
443 return vendors[0]
444
445
446def probe_cpus():
447 """
448 This routine returns a list of cpu devices found under
449 /sys/devices/system/cpu.
450 """
451 cmd = 'find /sys/devices/system/cpu/ -maxdepth 1 -type d -name cpu*'
452 return utils.system_output(cmd).splitlines()
mblighf4c35322006-03-13 01:01:10 +0000453
mblighe7a170f2006-12-05 07:48:18 +0000454
455# Returns total memory in kb
mbligh558885e2008-04-01 20:41:38 +0000456def read_from_meminfo(key):
mbligh2da4d882009-01-21 19:05:40 +0000457 meminfo = utils.system_output('grep %s /proc/meminfo' % key)
jadmanski0afbb632008-06-06 21:10:57 +0000458 return int(re.search(r'\d+', meminfo).group(0))
mbligh558885e2008-04-01 20:41:38 +0000459
460
mblighe7a170f2006-12-05 07:48:18 +0000461def memtotal():
jadmanski0afbb632008-06-06 21:10:57 +0000462 return read_from_meminfo('MemTotal')
mbligh558885e2008-04-01 20:41:38 +0000463
464
465def freememtotal():
jadmanski0afbb632008-06-06 21:10:57 +0000466 return read_from_meminfo('MemFree')
mbligh558885e2008-04-01 20:41:38 +0000467
Puthikorn Voravootivatb7f4c2e2014-04-03 11:03:39 -0700468def usable_memtotal():
Puthikorn Voravootivatdca8dc62014-08-11 10:44:57 -0700469 # Reserved 5% for OS use
470 return int(read_from_meminfo('MemFree') * 0.95)
Puthikorn Voravootivatb7f4c2e2014-04-03 11:03:39 -0700471
mbligh558885e2008-04-01 20:41:38 +0000472
mbligh8493be42008-11-20 00:55:11 +0000473def rounded_memtotal():
mbligh53da18e2009-01-05 21:13:26 +0000474 # Get total of all physical mem, in kbytes
475 usable_kbytes = memtotal()
476 # usable_kbytes is system's usable DRAM in kbytes,
mbligh8493be42008-11-20 00:55:11 +0000477 # as reported by memtotal() from device /proc/meminfo memtotal
478 # after Linux deducts 1.5% to 5.1% for system table overhead
479 # Undo the unknown actual deduction by rounding up
480 # to next small multiple of a big power-of-two
481 # eg 12GB - 5.1% gets rounded back up to 12GB
482 mindeduct = 0.015 # 1.5 percent
483 maxdeduct = 0.055 # 5.5 percent
484 # deduction range 1.5% .. 5.5% supports physical mem sizes
485 # 6GB .. 12GB in steps of .5GB
486 # 12GB .. 24GB in steps of 1 GB
487 # 24GB .. 48GB in steps of 2 GB ...
488 # Finer granularity in physical mem sizes would require
489 # tighter spread between min and max possible deductions
490
491 # increase mem size by at least min deduction, without rounding
Puthikorn Voravootivatb7f4c2e2014-04-03 11:03:39 -0700492 min_kbytes = int(usable_kbytes / (1.0 - mindeduct))
mbligh8493be42008-11-20 00:55:11 +0000493 # increase mem size further by 2**n rounding, by 0..roundKb or more
mbligh53da18e2009-01-05 21:13:26 +0000494 round_kbytes = int(usable_kbytes / (1.0 - maxdeduct)) - min_kbytes
mbligh8493be42008-11-20 00:55:11 +0000495 # find least binary roundup 2**n that covers worst-cast roundKb
mbligh53da18e2009-01-05 21:13:26 +0000496 mod2n = 1 << int(math.ceil(math.log(round_kbytes, 2)))
497 # have round_kbytes <= mod2n < round_kbytes*2
498 # round min_kbytes up to next multiple of mod2n
499 phys_kbytes = min_kbytes + mod2n - 1
500 phys_kbytes = phys_kbytes - (phys_kbytes % mod2n) # clear low bits
501 return phys_kbytes
mbligh8493be42008-11-20 00:55:11 +0000502
503
jadmanskif4325162010-04-09 16:04:54 +0000504def sysctl(key, value=None):
505 """Generic implementation of sysctl, to read and write.
506
507 @param key: A location under /proc/sys
508 @param value: If not None, a value to write into the sysctl.
509
510 @return The single-line sysctl value as a string.
511 """
512 path = '/proc/sys/%s' % key
513 if value is not None:
514 utils.write_one_line(path, str(value))
515 return utils.read_one_line(path)
516
517
mbligh558885e2008-04-01 20:41:38 +0000518def sysctl_kernel(key, value=None):
jadmanski0afbb632008-06-06 21:10:57 +0000519 """(Very) partial implementation of sysctl, for kernel params"""
jadmanskif4325162010-04-09 16:04:54 +0000520 if value is not None:
jadmanski0afbb632008-06-06 21:10:57 +0000521 # write
mbligh2da4d882009-01-21 19:05:40 +0000522 utils.write_one_line('/proc/sys/kernel/%s' % key, str(value))
jadmanski0afbb632008-06-06 21:10:57 +0000523 else:
524 # read
mbligh2da4d882009-01-21 19:05:40 +0000525 out = utils.read_one_line('/proc/sys/kernel/%s' % key)
jadmanski0afbb632008-06-06 21:10:57 +0000526 return int(re.search(r'\d+', out).group(0))
mblighe7a170f2006-12-05 07:48:18 +0000527
528
mbligh5285a2d2008-03-10 20:28:08 +0000529def _convert_exit_status(sts):
jadmanski0afbb632008-06-06 21:10:57 +0000530 if os.WIFSIGNALED(sts):
531 return -os.WTERMSIG(sts)
532 elif os.WIFEXITED(sts):
533 return os.WEXITSTATUS(sts)
534 else:
535 # impossible?
536 raise RuntimeError("Unknown exit status %d!" % sts)
mbligh5285a2d2008-03-10 20:28:08 +0000537
538
mblighf4c35322006-03-13 01:01:10 +0000539def where_art_thy_filehandles():
jadmanski0afbb632008-06-06 21:10:57 +0000540 """Dump the current list of filehandles"""
541 os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid())
mblighf4c35322006-03-13 01:01:10 +0000542
543
544def print_to_tty(string):
jadmanski0afbb632008-06-06 21:10:57 +0000545 """Output string straight to the tty"""
546 open('/dev/tty', 'w').write(string + '\n')
mblighf4c35322006-03-13 01:01:10 +0000547
548
mblighb8a14e32006-05-06 00:17:35 +0000549def dump_object(object):
jadmanski0afbb632008-06-06 21:10:57 +0000550 """Dump an object's attributes and methods
mblighc86b0b42006-07-28 17:35:28 +0000551
jadmanski0afbb632008-06-06 21:10:57 +0000552 kind of like dir()
553 """
554 for item in object.__dict__.iteritems():
555 print item
556 try:
Puthikorn Voravootivatb7f4c2e2014-04-03 11:03:39 -0700557 (key, value) = item
jadmanski0afbb632008-06-06 21:10:57 +0000558 dump_object(value)
559 except:
560 continue
mblighb8a14e32006-05-06 00:17:35 +0000561
562
mbligh4b089662006-06-14 22:34:58 +0000563def environ(env_key):
jadmanski0afbb632008-06-06 21:10:57 +0000564 """return the requested environment variable, or '' if unset"""
565 if (os.environ.has_key(env_key)):
566 return os.environ[env_key]
567 else:
568 return ''
mbligh4b089662006-06-14 22:34:58 +0000569
570
571def prepend_path(newpath, oldpath):
jadmanski0afbb632008-06-06 21:10:57 +0000572 """prepend newpath to oldpath"""
573 if (oldpath):
574 return newpath + ':' + oldpath
575 else:
576 return newpath
mbligh4b089662006-06-14 22:34:58 +0000577
578
579def append_path(oldpath, newpath):
jadmanski0afbb632008-06-06 21:10:57 +0000580 """append newpath to oldpath"""
581 if (oldpath):
582 return oldpath + ':' + newpath
583 else:
584 return newpath
mbligh4b089662006-06-14 22:34:58 +0000585
586
David Sharp77e01212015-03-06 15:01:06 -0800587_TIME_OUTPUT_RE = re.compile(
588 r'([\d\.]*)user ([\d\.]*)system '
589 r'(\d*):([\d\.]*)elapsed (\d*)%CPU')
590
591
mbligh4e75b0d2006-08-29 15:22:44 +0000592def avgtime_print(dir):
jadmanski0afbb632008-06-06 21:10:57 +0000593 """ Calculate some benchmarking statistics.
594 Input is a directory containing a file called 'time'.
595 File contains one-per-line results of /usr/bin/time.
596 Output is average Elapsed, User, and System time in seconds,
597 and average CPU percentage.
598 """
jadmanski0afbb632008-06-06 21:10:57 +0000599 user = system = elapsed = cpu = count = 0
David Sharp77e01212015-03-06 15:01:06 -0800600 with open(dir + "/time") as f:
601 for line in f:
602 try:
603 m = _TIME_OUTPUT_RE.match(line);
604 user += float(m.group(1))
605 system += float(m.group(2))
606 elapsed += (float(m.group(3)) * 60) + float(m.group(4))
607 cpu += float(m.group(5))
608 count += 1
609 except:
610 raise ValueError("badly formatted times")
mblighaf4efc22008-01-25 16:33:59 +0000611
jadmanski0afbb632008-06-06 21:10:57 +0000612 return "Elapsed: %0.2fs User: %0.2fs System: %0.2fs CPU: %0.0f%%" % \
Puthikorn Voravootivatb7f4c2e2014-04-03 11:03:39 -0700613 (elapsed / count, user / count, system / count, cpu / count)
mbligh4e75b0d2006-08-29 15:22:44 +0000614
615
David Sharp77e01212015-03-06 15:01:06 -0800616def to_seconds(time_string):
617 """Converts a string in M+:SS.SS format to S+.SS"""
618 elts = time_string.split(':')
619 if len(elts) == 1:
620 return time_string
621 return str(int(elts[0]) * 60 + float(elts[1]))
622
623
624_TIME_OUTPUT_RE_2 = re.compile(r'(.*?)user (.*?)system (.*?)elapsed')
625
626
627def extract_all_time_results(results_string):
628 """Extract user, system, and elapsed times into a list of tuples"""
629 results = []
630 for result in _TIME_OUTPUT_RE_2.findall(results_string):
631 results.append(tuple([to_seconds(elt) for elt in result]))
632 return results
633
634
mblighf06db0f2006-09-30 17:08:43 +0000635def running_config():
jadmanski0afbb632008-06-06 21:10:57 +0000636 """
637 Return path of config file of the currently running kernel
638 """
mbligh2da4d882009-01-21 19:05:40 +0000639 version = utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000640 for config in ('/proc/config.gz', \
641 '/boot/config-%s' % version,
642 '/lib/modules/%s/build/.config' % version):
643 if os.path.isfile(config):
644 return config
645 return None
mbligh9ec8acc2006-10-05 06:52:33 +0000646
647
mbligha1bef1f2007-04-03 17:18:07 +0000648def check_for_kernel_feature(feature):
jadmanski0afbb632008-06-06 21:10:57 +0000649 config = running_config()
mbligha1bef1f2007-04-03 17:18:07 +0000650
jadmanski0afbb632008-06-06 21:10:57 +0000651 if not config:
652 raise TypeError("Can't find kernel config file")
mbligha1bef1f2007-04-03 17:18:07 +0000653
lmr959e8162010-04-16 06:15:34 +0000654 if magic.guess_type(config) == 'application/x-gzip':
jadmanski0afbb632008-06-06 21:10:57 +0000655 grep = 'zgrep'
656 else:
657 grep = 'grep'
658 grep += ' ^CONFIG_%s= %s' % (feature, config)
mbligha1bef1f2007-04-03 17:18:07 +0000659
mbligh2da4d882009-01-21 19:05:40 +0000660 if not utils.system_output(grep, ignore_status=True):
jadmanski0afbb632008-06-06 21:10:57 +0000661 raise ValueError("Kernel doesn't have a %s feature" % (feature))
mbligha1bef1f2007-04-03 17:18:07 +0000662
663
mbligh663e4f62006-10-11 05:03:40 +0000664def check_glibc_ver(ver):
jadmanski0afbb632008-06-06 21:10:57 +0000665 glibc_ver = commands.getoutput('ldd --version').splitlines()[0]
666 glibc_ver = re.search(r'(\d+\.\d+(\.\d+)?)', glibc_ver).group()
lmr0fab1e92010-05-21 18:25:36 +0000667 if utils.compare_versions(glibc_ver, ver) == -1:
668 raise error.TestError("Glibc too old (%s). Glibc >= %s is needed." %
669 (glibc_ver, ver))
mbligh07635222007-07-09 21:29:00 +0000670
671def check_kernel_ver(ver):
mbligh2da4d882009-01-21 19:05:40 +0000672 kernel_ver = utils.system_output('uname -r')
jadmanski0afbb632008-06-06 21:10:57 +0000673 kv_tmp = re.split(r'[-]', kernel_ver)[0:3]
lmr42b7a5d2010-05-21 12:52:08 +0000674 # In compare_versions, if v1 < v2, return value == -1
675 if utils.compare_versions(kv_tmp[0], ver) == -1:
676 raise error.TestError("Kernel too old (%s). Kernel > %s is needed." %
677 (kernel_ver, ver))
mbligh60418bb2008-01-03 01:55:09 +0000678
mbligh9061a272006-12-28 21:20:51 +0000679
mbligh264cd8f2007-02-02 23:57:43 +0000680def human_format(number):
jadmanski0afbb632008-06-06 21:10:57 +0000681 # Convert number to kilo / mega / giga format.
682 if number < 1024:
683 return "%d" % number
684 kilo = float(number) / 1024.0
685 if kilo < 1024:
686 return "%.2fk" % kilo
687 meg = kilo / 1024.0
688 if meg < 1024:
689 return "%.2fM" % meg
690 gig = meg / 1024.0
691 return "%.2fG" % gig
mbligh264cd8f2007-02-02 23:57:43 +0000692
mbligh8eca3a92007-02-03 20:59:39 +0000693
694def numa_nodes():
jadmanski0afbb632008-06-06 21:10:57 +0000695 node_paths = glob.glob('/sys/devices/system/node/node*')
696 nodes = [int(re.sub(r'.*node(\d+)', r'\1', x)) for x in node_paths]
697 return (sorted(nodes))
mbligh8eca3a92007-02-03 20:59:39 +0000698
699
700def node_size():
jadmanski0afbb632008-06-06 21:10:57 +0000701 nodes = max(len(numa_nodes()), 1)
702 return ((memtotal() * 1024) / nodes)
mbligh8eca3a92007-02-03 20:59:39 +0000703
mbligh32bcff32007-07-25 16:37:32 +0000704
mblighc4211642007-08-02 21:00:51 +0000705def pickle_load(filename):
jadmanski0afbb632008-06-06 21:10:57 +0000706 return pickle.load(open(filename, 'r'))
mblighc4211642007-08-02 21:00:51 +0000707
mbligh237bed32007-09-05 13:05:57 +0000708
709# Return the kernel version and build timestamp.
710def running_os_release():
jadmanski0afbb632008-06-06 21:10:57 +0000711 return os.uname()[2:4]
mbligh237bed32007-09-05 13:05:57 +0000712
713
714def running_os_ident():
jadmanski0afbb632008-06-06 21:10:57 +0000715 (version, timestamp) = running_os_release()
716 return version + '::' + timestamp
mblighb830e282007-10-02 16:35:03 +0000717
718
mbligh3bf79ca2009-01-21 19:02:40 +0000719def running_os_full_version():
720 (version, timestamp) = running_os_release()
721 return version
722
723
mbligh523a19b2007-12-04 22:50:14 +0000724# much like find . -name 'pattern'
725def locate(pattern, root=os.getcwd()):
jadmanski0afbb632008-06-06 21:10:57 +0000726 for path, dirs, files in os.walk(root):
mbligh987071e2008-06-13 16:39:12 +0000727 for f in files:
728 if fnmatch.fnmatch(f, pattern):
mbligh7076b192008-06-13 17:53:49 +0000729 yield os.path.abspath(os.path.join(path, f))
mbligh523a19b2007-12-04 22:50:14 +0000730
731
mbligh25bb1e12007-10-12 23:57:47 +0000732def freespace(path):
jadmanski0afbb632008-06-06 21:10:57 +0000733 """Return the disk free space, in bytes"""
734 s = os.statvfs(path)
735 return s.f_bavail * s.f_bsize
jadmanski8415f962008-05-06 20:38:53 +0000736
737
738def disk_block_size(path):
jadmanski0afbb632008-06-06 21:10:57 +0000739 """Return the disk block size, in bytes"""
740 return os.statvfs(path).f_bsize
mbligh6de9cdf2007-11-24 19:35:28 +0000741
742
David Sharp77e01212015-03-06 15:01:06 -0800743_DISK_PARTITION_3_RE = re.compile(r'^(/dev/hd[a-z]+)3', re.M)
744
mbligh2316e522007-11-24 19:39:52 +0000745def get_disks():
mbligh2da4d882009-01-21 19:05:40 +0000746 df_output = utils.system_output('df')
David Sharp77e01212015-03-06 15:01:06 -0800747 return _DISK_PARTITION_3_RE.findall(df_output)
mbligh6de9cdf2007-11-24 19:35:28 +0000748
mbligh3e9062e2007-11-29 15:57:02 +0000749
Puthikorn Voravootivat8b811e02014-06-02 14:13:45 -0700750def get_disk_size(disk_name):
751 """
752 Return size of disk in byte. Return 0 in Error Case
753
754 @param disk_name: disk name to find size
755 """
756 device = os.path.basename(disk_name)
757 for line in file('/proc/partitions'):
758 try:
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700759 _, _, blocks, name = re.split(r' +', line.strip())
Puthikorn Voravootivat8b811e02014-06-02 14:13:45 -0700760 except ValueError:
761 continue
762 if name == device:
763 return 1024 * int(blocks)
764 return 0
765
766
767def get_disk_size_gb(disk_name):
768 """
769 Return size of disk in GB (10^9). Return 0 in Error Case
770
771 @param disk_name: disk name to find size
772 """
773 return int(get_disk_size(disk_name) / (10.0 ** 9) + 0.5)
774
775
776def get_disk_model(disk_name):
777 """
778 Return model name for internal storage device
779
780 @param disk_name: disk name to find model
781 """
782 cmd1 = 'udevadm info --query=property --name=%s' % disk_name
783 cmd2 = 'grep -E "ID_(NAME|MODEL)="'
784 cmd3 = 'cut -f 2 -d"="'
785 cmd = ' | '.join([cmd1, cmd2, cmd3])
786 return utils.system_output(cmd)
787
788
Gwendal Grignoua409c802016-10-13 09:41:08 -0700789_DISK_DEV_RE = re.compile(r'/dev/sd[a-z]|/dev/mmcblk[0-9]*|/dev/nvme[0-9]*')
David Sharp77e01212015-03-06 15:01:06 -0800790
791
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700792def get_disk_from_filename(filename):
793 """
794 Return the disk device the filename is on.
795 If the file is on tmpfs or other special file systems,
796 return None.
797
798 @param filename: name of file, full path.
799 """
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700800
Gwendal Grignou30dcb3b2014-06-20 10:48:21 -0700801 if not os.path.exists(filename):
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700802 raise error.TestError('file %s missing' % filename)
803
804 if filename[0] != '/':
805 raise error.TestError('This code works only with full path')
806
David Sharp77e01212015-03-06 15:01:06 -0800807 m = _DISK_DEV_RE.match(filename)
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700808 while not m:
809 if filename[0] != '/':
810 return None
811 if filename == '/dev/root':
812 cmd = 'rootdev -d -s'
813 elif filename.startswith('/dev/mapper'):
814 cmd = 'dmsetup table "%s"' % os.path.basename(filename)
815 dmsetup_output = utils.system_output(cmd).split(' ')
816 if dmsetup_output[2] == 'verity':
817 maj_min = dmsetup_output[4]
818 elif dmsetup_output[2] == 'crypt':
819 maj_min = dmsetup_output[6]
820 cmd = 'realpath "/dev/block/%s"' % maj_min
821 elif filename.startswith('/dev/loop'):
822 cmd = 'losetup -O BACK-FILE "%s" | tail -1' % filename
823 else:
824 cmd = 'df "%s" | tail -1 | cut -f 1 -d" "' % filename
825 filename = utils.system_output(cmd)
David Sharp77e01212015-03-06 15:01:06 -0800826 m = _DISK_DEV_RE.match(filename)
Gwendal Grignou10e71e62014-06-12 11:25:19 -0700827 return m.group(0)
828
829
Puthikorn Voravootivat8ba770d2014-06-10 14:44:27 -0700830def get_disk_firmware_version(disk_name):
831 """
832 Return firmware version for internal storage device. (empty string for eMMC)
833
834 @param disk_name: disk name to find model
835 """
836 cmd1 = 'udevadm info --query=property --name=%s' % disk_name
837 cmd2 = 'grep -E "ID_REVISION="'
838 cmd3 = 'cut -f 2 -d"="'
839 cmd = ' | '.join([cmd1, cmd2, cmd3])
840 return utils.system_output(cmd)
841
842
843def is_disk_scsi(disk_name):
844 """
845 Return true if disk is a scsi device, return false otherwise
846
847 @param disk_name: disk name check
848 """
849 return re.match('/dev/sd[a-z]+', disk_name)
850
851
852def is_disk_harddisk(disk_name):
853 """
854 Return true if disk is a harddisk, return false otherwise
855
856 @param disk_name: disk name check
857 """
858 cmd1 = 'udevadm info --query=property --name=%s' % disk_name
859 cmd2 = 'grep -E "ID_ATA_ROTATION_RATE_RPM="'
860 cmd3 = 'cut -f 2 -d"="'
861 cmd = ' | '.join([cmd1, cmd2, cmd3])
862
863 rtt = utils.system_output(cmd)
864
865 # eMMC will not have this field; rtt == ''
866 # SSD will have zero rotation rate; rtt == '0'
867 # For harddisk rtt > 0
868 return rtt and int(rtt) > 0
869
870
871def verify_hdparm_feature(disk_name, feature):
872 """
873 Check for feature support for SCSI disk using hdparm
874
875 @param disk_name: target disk
876 @param feature: hdparm output string of the feature
877 """
878 cmd = 'hdparm -I %s | grep -q "%s"' % (disk_name, feature)
879 ret = utils.system(cmd, ignore_status=True)
880 if ret == 0:
881 return True
882 elif ret == 1:
883 return False
884 else:
885 raise error.TestFail('Error running command %s' % cmd)
886
887
888def get_storage_error_msg(disk_name, reason):
889 """
890 Get Error message for storage test which include disk model.
891 and also include the firmware version for the SCSI disk
892
893 @param disk_name: target disk
894 @param reason: Reason of the error.
895 """
896
897 msg = reason
898
899 model = get_disk_model(disk_name)
900 msg += ' Disk model: %s' % model
901
902 if is_disk_scsi(disk_name):
903 fw = get_disk_firmware_version(disk_name)
904 msg += ' firmware: %s' % fw
905
906 return msg
907
908
Brian Norris570152b2015-12-07 16:52:19 -0800909def load_module(module_name, params=None):
jadmanski0afbb632008-06-06 21:10:57 +0000910 # Checks if a module has already been loaded
911 if module_is_loaded(module_name):
912 return False
mblighaf4efc22008-01-25 16:33:59 +0000913
Brian Norris570152b2015-12-07 16:52:19 -0800914 cmd = '/sbin/modprobe ' + module_name
915 if params:
916 cmd += ' ' + params
917 utils.system(cmd)
jadmanski0afbb632008-06-06 21:10:57 +0000918 return True
mbligh3e9062e2007-11-29 15:57:02 +0000919
920
921def unload_module(module_name):
lmrb923fc22009-06-08 17:27:59 +0000922 """
923 Removes a module. Handles dependencies. If even then it's not possible
924 to remove one of the modules, it will trhow an error.CmdError exception.
925
926 @param module_name: Name of the module we want to remove.
927 """
Henrik Kjellanderabbd2372013-04-04 14:06:57 +0200928 l_raw = utils.system_output("/bin/lsmod").splitlines()
lmrb923fc22009-06-08 17:27:59 +0000929 lsmod = [x for x in l_raw if x.split()[0] == module_name]
930 if len(lsmod) > 0:
931 line_parts = lsmod[0].split()
932 if len(line_parts) == 4:
933 submodules = line_parts[3].split(",")
934 for submodule in submodules:
935 unload_module(submodule)
936 utils.system("/sbin/modprobe -r %s" % module_name)
Daniel Kurtzf278e852014-03-04 21:57:30 -0800937 logging.info("Module %s unloaded", module_name)
lmrb923fc22009-06-08 17:27:59 +0000938 else:
Daniel Kurtzf278e852014-03-04 21:57:30 -0800939 logging.info("Module %s is already unloaded", module_name)
mbligh3e9062e2007-11-29 15:57:02 +0000940
941
942def module_is_loaded(module_name):
jadmanski0afbb632008-06-06 21:10:57 +0000943 module_name = module_name.replace('-', '_')
Henrik Kjellanderabbd2372013-04-04 14:06:57 +0200944 modules = utils.system_output('/bin/lsmod').splitlines()
jadmanski0afbb632008-06-06 21:10:57 +0000945 for module in modules:
946 if module.startswith(module_name) and module[len(module_name)] == ' ':
947 return True
948 return False
mbligh3e9062e2007-11-29 15:57:02 +0000949
950
mbligh6b34c4c2008-01-10 16:32:04 +0000951def get_loaded_modules():
Henrik Kjellanderabbd2372013-04-04 14:06:57 +0200952 lsmod_output = utils.system_output('/bin/lsmod').splitlines()[1:]
jadmanski0afbb632008-06-06 21:10:57 +0000953 return [line.split(None, 1)[0] for line in lsmod_output]
mbligh6b34c4c2008-01-10 16:32:04 +0000954
955
mbligh3e9062e2007-11-29 15:57:02 +0000956def get_huge_page_size():
mbligh2da4d882009-01-21 19:05:40 +0000957 output = utils.system_output('grep Hugepagesize /proc/meminfo')
jadmanski0afbb632008-06-06 21:10:57 +0000958 return int(output.split()[1]) # Assumes units always in kB. :(
mbligh3e9062e2007-11-29 15:57:02 +0000959
960
961def get_num_huge_pages():
mbligh2da4d882009-01-21 19:05:40 +0000962 raw_hugepages = utils.system_output('/sbin/sysctl vm.nr_hugepages')
jadmanski0afbb632008-06-06 21:10:57 +0000963 return int(raw_hugepages.split()[2])
mbligh3e9062e2007-11-29 15:57:02 +0000964
965
966def set_num_huge_pages(num):
mbligh2da4d882009-01-21 19:05:40 +0000967 utils.system('/sbin/sysctl vm.nr_hugepages=%d' % num)
mbligh3e9062e2007-11-29 15:57:02 +0000968
969
mbligh70c50ad2008-02-12 20:56:13 +0000970def ping_default_gateway():
jadmanski0afbb632008-06-06 21:10:57 +0000971 """Ping the default gateway."""
mbligh70c50ad2008-02-12 20:56:13 +0000972
jadmanski0afbb632008-06-06 21:10:57 +0000973 network = open('/etc/sysconfig/network')
974 m = re.search('GATEWAY=(\S+)', network.read())
975
976 if m:
977 gw = m.group(1)
978 cmd = 'ping %s -c 5 > /dev/null' % gw
mbligh2da4d882009-01-21 19:05:40 +0000979 return utils.system(cmd, ignore_status=True)
jadmanski0afbb632008-06-06 21:10:57 +0000980
981 raise error.TestError('Unable to find default gateway')
mbligh70c50ad2008-02-12 20:56:13 +0000982
983
jadmanski115feb22008-07-23 21:36:22 +0000984def drop_caches():
985 """Writes back all dirty pages to disk and clears all the caches."""
mbligh2da4d882009-01-21 19:05:40 +0000986 utils.system("sync")
jadmanski115feb22008-07-23 21:36:22 +0000987 # We ignore failures here as this will fail on 2.6.11 kernels.
mbligh2da4d882009-01-21 19:05:40 +0000988 utils.system("echo 3 > /proc/sys/vm/drop_caches", ignore_status=True)
mbligha3c17fc2009-08-24 22:34:41 +0000989
990
mblighe6bc5b92009-11-06 03:12:11 +0000991def process_is_alive(name_pattern):
mbligha3c17fc2009-08-24 22:34:41 +0000992 """
993 'pgrep name' misses all python processes and also long process names.
994 'pgrep -f name' gets all shell commands with name in args.
mblighe6bc5b92009-11-06 03:12:11 +0000995 So look only for command whose initial pathname ends with name.
996 Name itself is an egrep pattern, so it can use | etc for variations.
mbligha3c17fc2009-08-24 22:34:41 +0000997 """
mblighe6bc5b92009-11-06 03:12:11 +0000998 return utils.system("pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern,
mbligha3c17fc2009-08-24 22:34:41 +0000999 ignore_status=True) == 0
mbligh88f602c2010-02-02 18:17:04 +00001000
1001
Eric Lia82dc352011-02-23 13:15:52 -08001002def get_hwclock_seconds(utc=True):
1003 """
1004 Return the hardware clock in seconds as a floating point value.
1005 Use Coordinated Universal Time if utc is True, local time otherwise.
1006 Raise a ValueError if unable to read the hardware clock.
1007 """
1008 cmd = '/sbin/hwclock --debug'
1009 if utc:
1010 cmd += ' --utc'
1011 hwclock_output = utils.system_output(cmd, ignore_status=True)
1012 match = re.search(r'= ([0-9]+) seconds since .+ (-?[0-9.]+) seconds$',
1013 hwclock_output, re.DOTALL)
1014 if match:
1015 seconds = int(match.group(1)) + float(match.group(2))
Daniel Kurtzf278e852014-03-04 21:57:30 -08001016 logging.debug('hwclock seconds = %f', seconds)
Eric Lia82dc352011-02-23 13:15:52 -08001017 return seconds
1018
1019 raise ValueError('Unable to read the hardware clock -- ' +
1020 hwclock_output)
1021
1022
1023def set_wake_alarm(alarm_time):
1024 """
1025 Set the hardware RTC-based wake alarm to 'alarm_time'.
1026 """
1027 utils.write_one_line('/sys/class/rtc/rtc0/wakealarm', str(alarm_time))
1028
1029
1030def set_power_state(state):
1031 """
1032 Set the system power state to 'state'.
1033 """
1034 utils.write_one_line('/sys/power/state', state)
1035
1036
1037def standby():
1038 """
1039 Power-on suspend (S1)
1040 """
1041 set_power_state('standby')
1042
1043
1044def suspend_to_ram():
1045 """
1046 Suspend the system to RAM (S3)
1047 """
1048 set_power_state('mem')
1049
1050
1051def suspend_to_disk():
1052 """
1053 Suspend the system to disk (S4)
1054 """
1055 set_power_state('disk')