Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: recipe_modules/platform/api.py

Issue 2641253005: Add multiprocessing.cpu_count access to platform module. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | recipe_modules/platform/example.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The LUCI Authors. All rights reserved. 1 # Copyright 2013 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 import sys 5 import sys
6 import platform 6 import platform
7 import multiprocessing
7 8
8 from recipe_engine import recipe_api 9 from recipe_engine import recipe_api
9 10
10 11
11 def norm_plat(plat): 12 def norm_plat(plat):
12 if plat.startswith('linux'): 13 if plat.startswith('linux'):
13 return 'linux' 14 return 'linux'
14 elif plat.startswith(('win', 'cygwin')): 15 elif plat.startswith(('win', 'cygwin')):
15 return 'win' 16 return 'win'
16 elif plat.startswith(('darwin', 'mac')): 17 elif plat.startswith(('darwin', 'mac')):
(...skipping 20 matching lines...) Expand all
37 super(PlatformApi, self).__init__(**kwargs) 38 super(PlatformApi, self).__init__(**kwargs)
38 self._name = norm_plat(sys.platform) 39 self._name = norm_plat(sys.platform)
39 40
40 self._arch = 'intel' 41 self._arch = 'intel'
41 self._bits = norm_bits(platform.machine()) 42 self._bits = norm_bits(platform.machine())
42 43
43 if self._test_data.enabled: 44 if self._test_data.enabled:
44 # Default to linux/64, unless test case says otherwise. 45 # Default to linux/64, unless test case says otherwise.
45 self._name = norm_plat(self._test_data.get('name', 'linux')) 46 self._name = norm_plat(self._test_data.get('name', 'linux'))
46 self._bits = norm_bits(self._test_data.get('bits', 64)) 47 self._bits = norm_bits(self._test_data.get('bits', 64))
48 self._cpu_count = self._test_data.get('cpu_count', 2)
47 else: # pragma: no cover 49 else: # pragma: no cover
48 # platform.machine is based on running kernel. It's possible to use 64-bit 50 # platform.machine is based on running kernel. It's possible to use 64-bit
49 # kernel with 32-bit userland, e.g. to give linker slightly more memory. 51 # kernel with 32-bit userland, e.g. to give linker slightly more memory.
50 # Distinguish between different userland bitness by querying 52 # Distinguish between different userland bitness by querying
51 # the python binary. 53 # the python binary.
52 if (self._name == 'linux' and 54 if (self._name == 'linux' and
53 self._bits == 64 and 55 self._bits == 64 and
54 platform.architecture()[0] == '32bit'): 56 platform.architecture()[0] == '32bit'):
55 self._bits = 32 57 self._bits = 32
56 # On Mac, the inverse of the above is true: the kernel is 32-bit but the 58 # On Mac, the inverse of the above is true: the kernel is 32-bit but the
57 # CPU and userspace both are capable of running 64-bit programs. 59 # CPU and userspace both are capable of running 64-bit programs.
58 elif (self._name == 'mac' and 60 elif (self._name == 'mac' and
59 self._bits == 32 and 61 self._bits == 32 and
60 platform.architecture()[0] == '64bit'): 62 platform.architecture()[0] == '64bit'):
61 self._bits = 64 63 self._bits = 64
62 64
65 self._cpu_count = multiprocessing.cpu_count()
66
63 @property 67 @property
64 def is_win(self): 68 def is_win(self):
65 return self.name == 'win' 69 return self.name == 'win'
66 70
67 @property 71 @property
68 def is_mac(self): 72 def is_mac(self):
69 return self.name == 'mac' 73 return self.name == 'mac'
70 74
71 @property 75 @property
72 def is_linux(self): 76 def is_linux(self):
73 return self.name == 'linux' 77 return self.name == 'linux'
74 78
75 @property 79 @property
76 def name(self): 80 def name(self):
77 return self._name 81 return self._name
78 82
79 @property 83 @property
80 def bits(self): 84 def bits(self):
81 # The returned bitness corresponds to the userland. If anyone ever wants 85 # The returned bitness corresponds to the userland. If anyone ever wants
82 # to query for bitness of the kernel, another accessor should be added. 86 # to query for bitness of the kernel, another accessor should be added.
83 return self._bits 87 return self._bits
84 88
85 @property 89 @property
86 def arch(self): 90 def arch(self):
87 return self._arch 91 return self._arch
88 92
93 @property
94 def cpu_count(self):
95 """The number of CPU cores, according to multiprocessing.cpu_count()."""
96 return self._cpu_count
97
89 @staticmethod 98 @staticmethod
90 def normalize_platform_name(platform): 99 def normalize_platform_name(platform):
91 """One of python's sys.platform values -> 'win', 'linux' or 'mac'.""" 100 """One of python's sys.platform values -> 'win', 'linux' or 'mac'."""
92 return norm_plat(platform) # pragma: no cover 101 return norm_plat(platform) # pragma: no cover
OLDNEW
« no previous file with comments | « no previous file | recipe_modules/platform/example.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698