更新了一下文档
This commit is contained in:
1 parent
56f4b3adb0
commit
72f407c110
7 files changed
+319
-13
No files matched your search
+75
-1
@@ -6,6 +6,7 @@ import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import shutil
|
||||
import signal
|
||||
import subprocess
|
||||
@@ -20,6 +21,12 @@ _KNOWN_BINARIES = ('self_filter', 'nav_client', 'patrol', 'odom_tf', 'cmd_vel_re
|
||||
_GZ_CANDIDATES = ('/usr/bin/gz', '/usr/local/bin/gz', '/opt/gz/bin/gz')
|
||||
_GZ_VERSION_TIMEOUT = 5
|
||||
_FIELD_PACKAGE = Path('src/guodong_sentry_sim')
|
||||
# 无界面 Gazebo 的 OGRE2 走 EGL。混合显卡笔记本上 glvnd 默认挑 Mesa,
|
||||
# gpu_lidar 于是退化成 llvmpipe 软件光栅(实测单核 84%,CPU 射线只要 30%)。
|
||||
# 把 EGL vendor 指向 NVIDIA 的 json,OGRE2 才能真正用上显卡。
|
||||
_EGL_NVIDIA_JSON = Path('/usr/share/glvnd/egl_vendor.d/10_nvidia.json')
|
||||
_NVIDIA_DEVICE_NODE = Path('/dev/nvidiactl')
|
||||
_GPU_EGL_MODES = ('auto', 'nvidia', 'off')
|
||||
|
||||
|
||||
class ProbeError(RuntimeError):
|
||||
@@ -197,6 +204,66 @@ def wait_gz_clock(gz, gazebo_pid=None):
|
||||
f'{os.environ.get("GZ_PARTITION") or "sentry"}')
|
||||
|
||||
|
||||
def egl_vendor_json():
|
||||
"""NVIDIA 的 EGL vendor 描述文件。
|
||||
|
||||
`SENTRY_EGL_VENDOR_JSON` 可覆盖 —— 发行版把 glvnd 配置放别处时用。
|
||||
"""
|
||||
override = os.environ.get('SENTRY_EGL_VENDOR_JSON')
|
||||
return Path(override) if override else _EGL_NVIDIA_JSON
|
||||
|
||||
|
||||
def gpu_env():
|
||||
"""Return the EGL environment that makes OGRE2 (and gpu_lidar) use the dGPU.
|
||||
|
||||
`SENTRY_GPU_EGL` selects the behaviour:
|
||||
auto enable only when an NVIDIA device node and the vendor json exist
|
||||
nvidia always enable; missing vendor json is an error
|
||||
off never touch the environment
|
||||
`SENTRY_EGL_PLATFORM` additionally exports `EGL_PLATFORM` when set (some
|
||||
hosts only reach the surfaceless platform that way).
|
||||
|
||||
An empty dict means "leave the environment alone" so callers can splat it
|
||||
unconditionally.
|
||||
"""
|
||||
mode = (os.environ.get('SENTRY_GPU_EGL') or 'auto').strip().lower()
|
||||
if mode not in _GPU_EGL_MODES:
|
||||
raise ProbeError(
|
||||
f'SENTRY_GPU_EGL 只能是 {"|".join(_GPU_EGL_MODES)},当前为 {mode!r}')
|
||||
if mode == 'off':
|
||||
return {}
|
||||
vendor = egl_vendor_json()
|
||||
if mode == 'nvidia' and not vendor.is_file():
|
||||
raise ProbeError(f'SENTRY_GPU_EGL=nvidia 但找不到 {vendor}')
|
||||
if mode == 'auto' and not (_NVIDIA_DEVICE_NODE.exists() and vendor.is_file()):
|
||||
return {}
|
||||
env = {
|
||||
'__EGL_VENDOR_LIBRARY_FILENAMES': str(vendor),
|
||||
'NVIDIA_DRIVER_CAPABILITIES':
|
||||
os.environ.get('NVIDIA_DRIVER_CAPABILITIES') or 'all',
|
||||
}
|
||||
if platform := os.environ.get('SENTRY_EGL_PLATFORM'):
|
||||
env['EGL_PLATFORM'] = platform
|
||||
return env
|
||||
|
||||
|
||||
def describe_gpu_env():
|
||||
"""One-line human summary of what `gpu_env()` decided and why."""
|
||||
mode = (os.environ.get('SENTRY_GPU_EGL') or 'auto').strip().lower()
|
||||
env = gpu_env()
|
||||
if env:
|
||||
return 'nvidia → ' + ' '.join(f'{k}={v}' for k, v in env.items())
|
||||
if mode == 'off':
|
||||
return 'off(SENTRY_GPU_EGL=off,不改环境)'
|
||||
reasons = []
|
||||
if not _NVIDIA_DEVICE_NODE.exists():
|
||||
reasons.append(f'{_NVIDIA_DEVICE_NODE} 不存在')
|
||||
vendor = egl_vendor_json()
|
||||
if not vendor.is_file():
|
||||
reasons.append(f'{vendor} 不存在')
|
||||
return 'off(' + ';'.join(reasons or ['未找到可用显卡']) + ')'
|
||||
|
||||
|
||||
def find_field_root(ws=WORKSPACE, *, explicit=None):
|
||||
if explicit:
|
||||
return require_directory(explicit, 'field_root')
|
||||
@@ -244,13 +311,15 @@ def report(ws):
|
||||
print(f' gz 版本参数 : {versions or "<无>"}')
|
||||
print(f' FIELD_ROOT : {field_root}')
|
||||
print(f' MAP_YAML : {map_yaml}')
|
||||
print(f' GPU EGL : {describe_gpu_env()}')
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('workspace', type=Path, nargs='?', default=WORKSPACE)
|
||||
queries = parser.add_mutually_exclusive_group()
|
||||
for query in ('bin-dir', 'field', 'map', 'gz', 'gz-version-args', 'ros-setup'):
|
||||
for query in ('bin-dir', 'field', 'map', 'gz', 'gz-version-args', 'ros-setup',
|
||||
'gpu', 'gpu-shell'):
|
||||
queries.add_argument(f'--{query}', dest='query', action='store_const', const=query)
|
||||
for query in ('gz-version-args-lines', 'wait-gz-clock'):
|
||||
queries.add_argument(f'--{query}', dest='query', action='store_const', const=query,
|
||||
@@ -283,6 +352,11 @@ def main(argv=None):
|
||||
print()
|
||||
case 'ros-setup':
|
||||
print(find_ros_setup() or '')
|
||||
case 'gpu':
|
||||
print(describe_gpu_env())
|
||||
case 'gpu-shell':
|
||||
for name, value in gpu_env().items():
|
||||
print(f'export {name}={shlex.quote(value)}')
|
||||
case 'wait-gz-clock':
|
||||
wait_gz_clock(find_gz_bin(), args.gazebo_pid)
|
||||
case None:
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -27,7 +28,9 @@ class EnvironmentTest(unittest.TestCase):
|
||||
self.env['PYTHONDONTWRITEBYTECODE'] = '1'
|
||||
for name in ('ROS_SETUP', 'ROS_INSTALL_ROOT', 'ROS_DISTRO', 'AMENT_PREFIX_PATH',
|
||||
'SENTRY_BIN_DIR', 'NAV_BIN_DIR', 'SENTRY_FIELD_ROOT', 'SENTRY_MAP_YAML',
|
||||
'GZ_BIN', 'GZ_FORCE_VERSION', 'BRIDGE_WAIT_TIMEOUT', 'GZ_PARTITION'):
|
||||
'GZ_BIN', 'GZ_FORCE_VERSION', 'BRIDGE_WAIT_TIMEOUT', 'GZ_PARTITION',
|
||||
'SENTRY_GPU_EGL', 'SENTRY_EGL_VENDOR_JSON', 'SENTRY_EGL_PLATFORM',
|
||||
'NVIDIA_DRIVER_CAPABILITIES', 'EGL_PLATFORM'):
|
||||
self.env.pop(name, None)
|
||||
|
||||
def run_command(self, command, **overrides):
|
||||
@@ -139,6 +142,61 @@ class EnvironmentTest(unittest.TestCase):
|
||||
with self.assertRaisesRegex(project_env.ProbeError, '找不到 Gazebo'):
|
||||
project_env.find_gz_bin()
|
||||
|
||||
def test_gpu_egl_matches_shell(self):
|
||||
"""`gpu_env()` 的判定必须在 CLI 和 Shell 两个入口给出同样结果。"""
|
||||
vendor = self.root/'glvnd/nvidia egl.json'
|
||||
vendor.parent.mkdir(parents=True)
|
||||
vendor.write_text('{"ICD":{"library_path":"libEGL_nvidia.so.0"}}\n')
|
||||
missing = self.root/'nope.json'
|
||||
|
||||
# nvidia:只校验 vendor json,不依赖设备节点 —— 两边都该导出两条。
|
||||
env = dict(SENTRY_GPU_EGL='nvidia', SENTRY_EGL_VENDOR_JSON=str(vendor))
|
||||
exports = (f'export __EGL_VENDOR_LIBRARY_FILENAMES={shlex.quote(str(vendor))}\n'
|
||||
'export NVIDIA_DRIVER_CAPABILITIES=all\n')
|
||||
self.assertEqual(self.cli('--gpu-shell', **env).stdout, exports)
|
||||
result = self.run_command(
|
||||
['bash', '-c', 'source "$1"; env_gpu_egl 2>/dev/null; '
|
||||
'printf "%s|%s\\n" "$__EGL_VENDOR_LIBRARY_FILENAMES" '
|
||||
'"$NVIDIA_DRIVER_CAPABILITIES"',
|
||||
'env-test', str(SCRIPTS/'env_common.sh')], **env)
|
||||
self.expect_value(result, f'{vendor}|all')
|
||||
|
||||
# off:两边都静默(空输出,连换行都没有)。
|
||||
off_cli = self.cli('--gpu-shell', SENTRY_GPU_EGL='off')
|
||||
self.assertEqual(off_cli.returncode, 0, off_cli.stderr)
|
||||
self.assertEqual(off_cli.stdout, '')
|
||||
off = self.shell('env_gpu_egl')
|
||||
self.assertEqual(off.returncode, 0, off.stderr)
|
||||
self.assertEqual(off.stdout, '')
|
||||
|
||||
# EGL_PLATFORM 只在显式设置时才追加。
|
||||
with patch.dict(os.environ, {**env, 'SENTRY_EGL_PLATFORM': 'surfaceless'}):
|
||||
self.assertEqual(project_env.gpu_env()['EGL_PLATFORM'], 'surfaceless')
|
||||
|
||||
# auto:设备节点和 vendor json 缺一不可。
|
||||
auto = {**env, 'SENTRY_GPU_EGL': 'auto'}
|
||||
with patch.dict(os.environ, auto):
|
||||
with patch.object(project_env, '_NVIDIA_DEVICE_NODE', self.root/'missing-device'):
|
||||
self.assertEqual(project_env.gpu_env(), {})
|
||||
self.assertIn('不存在', project_env.describe_gpu_env())
|
||||
with patch.object(project_env, '_NVIDIA_DEVICE_NODE', vendor):
|
||||
self.assertEqual(project_env.gpu_env()['__EGL_VENDOR_LIBRARY_FILENAMES'],
|
||||
str(vendor))
|
||||
|
||||
# 缺 vendor json:nvidia 报错,auto 静默。
|
||||
self.expect_error(self.cli('--gpu-shell', SENTRY_GPU_EGL='nvidia',
|
||||
SENTRY_EGL_VENDOR_JSON=str(missing)), '找不到')
|
||||
with patch.dict(os.environ, {'SENTRY_GPU_EGL': 'auto',
|
||||
'SENTRY_EGL_VENDOR_JSON': str(missing)}):
|
||||
with patch.object(project_env, '_NVIDIA_DEVICE_NODE', vendor):
|
||||
self.assertEqual(project_env.gpu_env(), {})
|
||||
|
||||
# 非法取值必须被拒绝,而不是静默当成 auto。
|
||||
self.expect_error(self.cli('--gpu', SENTRY_GPU_EGL='bogus'), 'SENTRY_GPU_EGL')
|
||||
with patch.dict(os.environ, {'SENTRY_GPU_EGL': 'bogus'}):
|
||||
with self.assertRaisesRegex(project_env.ProbeError, 'SENTRY_GPU_EGL'):
|
||||
project_env.gpu_env()
|
||||
|
||||
def test_build_directory_priority_matches_shell(self):
|
||||
build = self.root/'build'
|
||||
unified = build/'bin'
|
||||
|
||||
Reference in new issue
Block a user