Skip to content

Commit 764ad6a

Browse files
committed
Merge branch 'mr/force_cpu_number' into 'master'
Add ability to force the number of cores See merge request it/e3-core!109
2 parents 2764bd4 + beeae3e commit 764ad6a

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

src/e3/env.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def set_build(
123123
version: str | None = None,
124124
machine: str | None = None,
125125
mode: str | None = None,
126+
cores: int | None = None,
126127
) -> None:
127128
"""Set build platform.
128129
@@ -138,14 +139,16 @@ def set_build(
138139
:param mode: a string containing the name of the mode. This
139140
notion is needed on some targets such as VxWorks to switch between
140141
kernel mode and other modes such as rtp
142+
:param cores: force a number of apparent cores. If None that number
143+
is either computed (when possible) or set to 1.
141144
142145
When calling set_build, the target and host systems are reset to the
143146
build one. Thus you should call set_build before calling either
144147
set_host or set_target.
145148
"""
146149
e3.log.debug("set_build (build_name=%s, build_version=%s)", name, version)
147150
self.build = Platform.get(
148-
platform_name=name, version=version, machine=machine, mode=mode
151+
platform_name=name, version=version, machine=machine, mode=mode, cores=cores
149152
)
150153
self.host = self.build
151154
self.target = self.build
@@ -156,6 +159,7 @@ def set_host(
156159
version: str | None = None,
157160
machine: str | None = None,
158161
mode: str | None = None,
162+
cores: int | None = None,
159163
) -> None:
160164
"""Set host platform.
161165
@@ -170,6 +174,8 @@ def set_host(
170174
:param mode: a string containing the name of the mode. This
171175
notion is needed on some targets such as VxWorks to switch between
172176
kernel mode and other modes such as rtp
177+
:param cores: force a number of apparent cores. If None that number
178+
is either computed (when possible) or set to 1.
173179
174180
When calling set_host, the target system is reset to the host one.
175181
Thus you should call set_host before set_target otherwise your call
@@ -185,7 +191,11 @@ def set_host(
185191
self.host = self.build
186192
else:
187193
self.host = Platform.get(
188-
platform_name=name, version=version, machine=machine, mode=mode
194+
platform_name=name,
195+
version=version,
196+
machine=machine,
197+
mode=mode,
198+
cores=cores,
189199
)
190200
self.target = self.host
191201

@@ -195,6 +205,7 @@ def set_target(
195205
version: str | None = None,
196206
machine: str | None = None,
197207
mode: str | None = None,
208+
cores: int | None = None,
198209
) -> None:
199210
"""Set target platform.
200211
@@ -211,6 +222,8 @@ def set_target(
211222
:param mode: a string containing the name of the mode. This
212223
notion is needed on some targets such as VxWorks to switch between
213224
kernel mode and other modes such as rtp
225+
:param cores: force a number of apparent cores. If None that number
226+
is either computed (when possible) or set to 1.
214227
215228
The target parameters are ignored if the target_name is equal to the
216229
host platform.
@@ -224,7 +237,11 @@ def set_target(
224237
self.target = self.build
225238
else:
226239
self.target = Platform.get(
227-
platform_name=name, version=version, machine=machine, mode=mode
240+
platform_name=name,
241+
version=version,
242+
machine=machine,
243+
mode=mode,
244+
cores=cores,
228245
)
229246

230247
def set_env(

src/e3/os/platform.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,31 @@ def as_dict(self) -> dict[str, Any]:
329329

330330
@classmethod
331331
def get(
332-
cls, name: str, endian: str | None = None, compute_cores: bool = False
332+
cls,
333+
name: str,
334+
endian: str | None = None,
335+
compute_cores: bool = False,
336+
cores: int | None = None,
333337
) -> CPU:
334338
"""Initialize CPU instance.
335339
336340
:param name: cpu name
337341
:param endian: if not None override endianness default settings
338342
:param compute_cores: if True compute the number of cores
343+
:param cores: if not None force the apparent number of
344+
cpu to that value.
339345
"""
340346
assert name in KNOWLEDGE_BASE.cpu_info, "invalid cpu name"
341347
bits = KNOWLEDGE_BASE.cpu_info[name]["bits"]
342-
cores = 1
343348

344349
if endian is None:
345350
endian = KNOWLEDGE_BASE.cpu_info[name]["endian"]
346-
if compute_cores:
347-
cores = SystemInfo.core_number
351+
352+
if cores is None:
353+
if compute_cores:
354+
cores = SystemInfo.core_number
355+
else:
356+
cores = 1
348357

349358
return CPU(name, bits, endian, cores)
350359

src/e3/platform.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def get(
5959
machine: str | None = None,
6060
mode: str | None = None,
6161
compute_default: bool = False,
62+
cores: int | None = None,
6263
) -> Platform:
6364
"""Return a Platform object.
6465
@@ -72,6 +73,7 @@ def get(
7273
:param mode: an os mode (ex: rtp for vxworks)
7374
:param compute_default: if True compute the default Arch for the
7475
current machine (this parameter is for internal purpose only).
76+
:param cores: if not None force a number of apparent cores
7577
"""
7678
# normalize arguments
7779
if not version:
@@ -112,7 +114,9 @@ def get(
112114
# Fill other attributes
113115
assert platform_name is not None
114116
pi = KNOWLEDGE_BASE.platform_info[platform_name]
115-
cpu = e3.os.platform.CPU.get(pi["cpu"], pi.get("endian", None), is_host)
117+
cpu = e3.os.platform.CPU.get(
118+
pi["cpu"], pi.get("endian", None), is_host, cores=cores
119+
)
116120
os = e3.os.platform.OS.get(pi["os"], is_host, version=version, mode=mode)
117121
is_hie = pi["is_hie"]
118122

tests/tests_e3/env/main_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ def test_copy():
260260
assert new_e.target.platform == "arm-elf"
261261

262262

263+
def test_force_cores():
264+
"""Test that forcing the number of cores works in all cases."""
265+
e = e3.env.BaseEnv()
266+
e.set_build(cores=23)
267+
assert e.build.cpu.cores == 23
268+
269+
e.set_build(name="x86_64-linux", cores=23)
270+
assert e.build.cpu.cores == 23
271+
272+
e.set_build(name="x86_64-windows64", cores=23)
273+
assert e.build.cpu.cores == 23
274+
275+
263276
def test_build_os_propagation():
264277
"""Ensure that OS version is preserved when build platform is changed."""
265278
winenv = e3.env.BaseEnv(

0 commit comments

Comments
 (0)