【已解决】IDF如何限制ninja使用的CPU数量?

Luxni_
Posts: 28
Joined: Mon Aug 16, 2021 6:20 am

【已解决】IDF如何限制ninja使用的CPU数量?

Postby Luxni_ » Mon Jul 17, 2023 3:09 am

当前IDF在编译时,ninja默认使用全部的CPU来编译,导致电脑发热严重,能否显示它使用的CPU数量?

当前我的解决方案是:
在编译时,手动中断编译,然后 cd build:ninja -j2 all .

ESP_Penguin_Helper
Posts: 156
Joined: Tue Jul 11, 2023 6:55 am

Re: IDF如何限制ninja使用的CPU数量?

Postby ESP_Penguin_Helper » Tue Jul 18, 2023 10:56 am

在之前版本中可以使用 make 限制 CPU 的数量,现在使用 CMake 就一直默认使用所有 CPU 了。

Luxni_
Posts: 28
Joined: Mon Aug 16, 2021 6:20 am

Re: IDF如何限制ninja使用的CPU数量?

Postby Luxni_ » Wed Jul 19, 2023 8:14 am

我在idf的github找到一个example,自己尝试修改了下,发现可行。

Code: Select all


# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import collections
import multiprocessing
import os
import platform
from typing import Dict, Union

GENERATORS: Dict[str, Union[str, Dict, list]] = collections.OrderedDict([
    # - command: build command line
    # - version: version command line
    # - dry_run: command to run in dry run mode
    # - verbose_flag: verbose flag
    # - force_progression: one liner status of the progress
    ('Ninja', {
        'command': ['ninja'],
        'version': ['ninja', '--version'],
        'dry_run': ['ninja', '-n'],
        'verbose_flag': '-v',
        # as opposed to printing the status updates each in a in new line
        'force_progression': True,
    }),
])

# 添加了下面这两句
if os.getenv("IDF_JOB_POOL_COMPILE"):
    GENERATORS['Ninja']['command'] = ['ninja' , "-j" + os.getenv('IDF_JOB_POOL_COMPILE')]

if os.name != 'nt':
    MAKE_CMD = 'gmake' if platform.system() == 'FreeBSD' else 'make'
    GENERATORS['Unix Makefiles'] = {'command': [MAKE_CMD, '-j', str(multiprocessing.cpu_count() + 2)],
                                    'version': [MAKE_CMD, '--version'],
                                    'dry_run': [MAKE_CMD, '-n'],
                                    'verbose_flag': 'VERBOSE=1',
                                    'force_progression': False}

URL_TO_DOC = 'https://docs.espressif.com/projects/esp-idf'

SUPPORTED_TARGETS = ['esp32', 'esp32s2', 'esp32c3', 'esp32s3', 'esp32c2']
PREVIEW_TARGETS = ['linux', 'esp32h2']
使用了一下系统环境变量来设置ninja的并发编译,

Code: Select all

# 默认
$ export IDF_JOB_POOL_COMPILE=

#等同于ninja -j2
$ export IDF_JOB_POOL_COMPILE=2

Who is online

Users browsing this forum: No registered users and 84 guests