base_utils: fix count_cpus to use multiprocess.cpu_count()
So in base_utils.count_cpu, try to use multiprocess.cpu_count() first.
If it is not availabe, then try to match line like
processor :0
in /proc/cpuinfo to find cpu number.
At last, return at least one.
BUG=chrome-os-partner:9582
TEST=try hardware_SAT test on dut, will see -m parameter with correct cpu number
Change-Id: I1652eca0ba63cf650caa3ca79c07888d5c8920bb
Reviewed-on: https://gerrit.chromium.org/gerrit/34494
Commit-Ready: Cheng-Yi Chiang <[email protected]>
Reviewed-by: Cheng-Yi Chiang <[email protected]>
Tested-by: Cheng-Yi Chiang <[email protected]>
diff --git a/client/bin/base_utils.py b/client/bin/base_utils.py
index 5d72566..620d655 100644
--- a/client/bin/base_utils.py
+++ b/client/bin/base_utils.py
@@ -8,7 +8,7 @@
precedence order defined there
"""
import os, shutil, sys, signal, commands, pickle, glob, statvfs
-import math, re, string, fnmatch, logging
+import math, re, string, fnmatch, logging, multiprocessing
from autotest_lib.client.common_lib import error, utils, magic
@@ -326,12 +326,20 @@
def count_cpus():
"""number of CPUs in the local machine according to /proc/cpuinfo"""
+ try:
+ return multiprocessing.cpu_count()
+ except Exception as e:
+ logging.exception('can not get cpu count from'
+ ' multiprocessing.cpu_count()')
+
f = file('/proc/cpuinfo', 'r')
cpus = 0
for line in f.readlines():
- if line.lower().startswith('processor'):
+ # Matches lines like "processor : 0"
+ if re.search(r'^processor\s*:\s*[0-9]+$', line):
cpus += 1
- return cpus
+ # Returns at least one cpu. Check comment #1 in crosbug.com/p/9582.
+ return cpus if cpus > 0 else 1
# Returns total memory in kb