| 1 |
''' |
| 2 |
Module of Linux API for plyer.cpu. |
| 3 |
''' |
| 4 |
|
| 5 |
from os.path import join |
| 6 |
from os import environ, listdir |
| 7 |
from subprocess import Popen, PIPE |
| 8 |
from plyer.facades import CPU |
| 9 |
from plyer.utils import whereis_exe |
| 10 |
|
| 11 |
|
| 12 |
class LinuxCPU(CPU): |
| 13 |
''' |
| 14 |
Implementation of Linux CPU API. |
| 15 |
''' |
| 16 |
|
| 17 |
def _sockets(self): |
| 18 |
|
| 19 |
sockets = [] |
| 20 |
|
| 21 |
|
| 22 |
with open('/proc/cpuinfo', 'rb') as fle: |
| 23 |
lines = fle.readlines() |
| 24 |
|
| 25 |
|
| 26 |
for line in lines: |
| 27 |
line = line.decode('utf-8') |
| 28 |
if 'physical id' not in line: |
| 29 |
continue |
| 30 |
cpuid = line.split(':')[1].strip() |
| 31 |
sockets.append(cpuid) |
| 32 |
|
| 33 |
|
| 34 |
sockets = len(set(sockets)) |
| 35 |
return sockets |
| 36 |
|
| 37 |
def _physical(self): |
| 38 |
|
| 39 |
physical = [] |
| 40 |
|
| 41 |
|
| 42 |
with open('/proc/cpuinfo', 'rb') as fle: |
| 43 |
lines = fle.readlines() |
| 44 |
|
| 45 |
|
| 46 |
for line in lines: |
| 47 |
line = line.decode('utf-8') |
| 48 |
if 'core id' not in line: |
| 49 |
continue |
| 50 |
cpuid = line.split(':')[1].strip() |
| 51 |
physical.append(cpuid) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
physical = len(set(physical)) |
| 56 |
return physical |
| 57 |
|
| 58 |
def _logical(self): |
| 59 |
|
| 60 |
logical = None |
| 61 |
old_lang = environ.get('LANG', '') |
| 62 |
environ['LANG'] = 'C' |
| 63 |
|
| 64 |
_logical = Popen(['nproc', '--all'], stdout=PIPE) |
| 65 |
output = _logical.communicate()[0].decode('utf-8').strip() |
| 66 |
if output: |
| 67 |
logical = int(output) |
| 68 |
|
| 69 |
environ['LANG'] = old_lang |
| 70 |
return logical |
| 71 |
|
| 72 |
def _cache(self): |
| 73 |
values = {key: 0 for key in ('L1', 'L2', 'L3')} |
| 74 |
cpu_path = join('/sys', 'devices', 'system', 'cpu') |
| 75 |
|
| 76 |
|
| 77 |
with open(join(cpu_path, 'present')) as fle: |
| 78 |
present = fle.read().decode('utf-8') |
| 79 |
present = present.strip().split('-') |
| 80 |
|
| 81 |
if len(present) == 2: |
| 82 |
present = range(int(present[1]) + 1) |
| 83 |
else: |
| 84 |
present = [present[0]] |
| 85 |
|
| 86 |
cores = ['cpu{}'.format(i) for i in present] |
| 87 |
for core in cores: |
| 88 |
indicies = [ |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
fle for fle in listdir(join(cpu_path, core, 'cache')) |
| 93 |
if fle.startswith('index') and len(fle) <= len('index') + 2 |
| 94 |
] |
| 95 |
|
| 96 |
for index in indicies: |
| 97 |
index_type = join(cpu_path, core, 'cache', index, 'level') |
| 98 |
with open(index_type, 'rb') as fle: |
| 99 |
cache_level = fle.read().decode('utf-8').strip() |
| 100 |
values['L{}'.format(cache_level)] += 1 |
| 101 |
return values |
| 102 |
|
| 103 |
@staticmethod |
| 104 |
def _numa(): |
| 105 |
return |
| 106 |
|
| 107 |
|
| 108 |
def instance(): |
| 109 |
''' |
| 110 |
Instance for facade proxy. |
| 111 |
''' |
| 112 |
import sys |
| 113 |
if whereis_exe('nproc'): |
| 114 |
return LinuxCPU() |
| 115 |
sys.stderr.write("nproc not found.") |
| 116 |
return CPU() |