Debian Codegraph @

plyer/platforms/linux/cpu.py

raw symbols SCIP
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 # physical CPU sockets (or slots) on motherboard
19 sockets = [] # list of CPU ids from kernel
20
21 # open Linux kernel data file for CPU
22 with open('/proc/cpuinfo', 'rb') as fle:
23 lines = fle.readlines()
24
25 # go through the lines and obtain physical CPU ids
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 # total sockets is the length of unique CPU ids from kernel
34 sockets = len(set(sockets))
35 return sockets
36
37 def _physical(self):
38 # cores
39 physical = [] # list of CPU ids from kernel
40
41 # open Linux kernel data file for CPU
42 with open('/proc/cpuinfo', 'rb') as fle:
43 lines = fle.readlines()
44
45 # go through the lines and obtain CPU core ids
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 # total cores (socket * core per socket)
54 # is the length of unique CPU core ids from kernel
55 physical = len(set(physical))
56 return physical
57
58 def _logical(self):
59 # cores * threads
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 # get present cores from kernel device
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 # get 'indexN' files from 'cache' folder assuming
90 # the filename is in range index0 to index99
91 # in case a wild 'index_whatevercontent' file appears
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()