Home | History | Annotate | Download | only in detectors
      1 # Copyright 2018 The Chromium OS 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 
      6 import logging
      7 import subprocess
      8 
      9 from autotest_lib.client.common_lib import error
     10 
     11 cpu_list = {
     12     'Intel(R) Celeron(R) 2955U @ 1.40GHz'     : 'intel_celeron_2955U',
     13     'Intel(R) Celeron(R) 2957U @ 1.40GHz'     : 'intel_celeron_2957U',
     14     'Intel(R) Celeron(R) CPU 1007U @ 1.50GHz' : 'intel_celeron_1007U',
     15     'Intel(R) Celeron(R) CPU 847 @ 1.10GHz'   : 'intel_celeron_847',
     16     'Intel(R) Celeron(R) CPU 867 @ 1.30GHz'   : 'intel_celeron_867',
     17     'Intel(R) Celeron(R) CPU 877 @ 1.40GHz'   : 'intel_celeron_877',
     18     'Intel(R) Celeron(R) CPU B840 @ 1.90GHz'  : 'intel_celeron_B840',
     19     'Intel(R) Core(TM) i3-4005U CPU @ 1.70GHz': 'intel_i3_4005U',
     20     'Intel(R) Core(TM) i3-4010U CPU @ 1.70GHz': 'intel_i3_4010U',
     21     'Intel(R) Core(TM) i3-4030U CPU @ 1.90GHz': 'intel_i3_4030U',
     22     'Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz': 'intel_i5_2450M',
     23     'Intel(R) Core(TM) i5-2467M CPU @ 1.60GHz': 'intel_i5_2467M',
     24     'Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz': 'intel_i5_2520M',
     25     'Intel(R) Core(TM) i5-3427U CPU @ 1.80GHz': 'intel_i7_3427U',
     26     'Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz': 'intel_i7_4600U',
     27 }
     28 
     29 
     30 def detect():
     31     """
     32     Returns CPU type. That is 'Model name' column of 'lscpu'.
     33     For example, 'Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz'
     34     Raise exception if a processor isn't a known one.
     35 
     36     @returns string: CPU type.
     37     """
     38 
     39     def get_cpu_name(cpu_str):
     40         # Check if cpu_str is an expected cpu and return an original name
     41         # describing it. Otherwise an exception is thrown.
     42         logging.debug('Parse CPU type: %s', cpu_str)
     43         return cpu_list[cpu_str]
     44 
     45     try:
     46         lscpu_result = subprocess.check_output(['lscpu'])
     47     except subprocess.CalledProcessError:
     48         logging.exception('lscpu failed.')
     49         raise error.TestFail('Fail to execute "lscpu"')
     50 
     51     logging.debug('The result of lscpu.')
     52     for cpu_info in lscpu_result.splitlines():
     53         logging.debug(cpu_info)
     54         if not ':' in cpu_info:
     55             continue
     56         key, value = cpu_info.split(':', 1)
     57         if key == 'Model name':
     58             return get_cpu_name(value.strip())
     59 
     60     raise error.TestFail("Couldn't get CPU type.")
     61