feat(hotspot)!: 下线 --func-hotspot 并为 --hotspot-sites 增加每函数计时器

移除 --func-hotspot 全链路(CLI 解析与绑定、Config argparse 与注入、Translator 属性、LlvmGenerator 收口与透传、BaseGen 专属实现、Phase2Translator 透传、帮助文本);全仓已无 FuncHotspot/func-hotspot 残留引用。

通用截断逻辑保留并改名为 _InstallHotspotTruncate,供 --hotspot-sites 使用(分别清空 hotspot_sites.txt 与 hotspot_time.txt);ctor 注册改为直接新建并接收处理器列表(sites 与 time 两个处理器在同一 ctor 内注册)。

--hotspot-sites 现附带每函数计时:入口读一次 @llvm.readcyclecounter() 存起始 tick,在函数所有 ret 之前再读一次并累加差值到 internal i64 __fhs_ftime_N,同时 __fhs_ftime_enter_N 记录进入次数;单位为 tick(x86_64 即 CPU 周期),语义为 inclusive(包含内层调用)。

计时结果按耗时降序写入同目录 hotspot_time.txt(# time <module_sha1> 分段 + 函数名/累计 tick/进入次数),不改变 hotspot_sites.txt 的既有格式与字段。不传 --hotspot-sites 时零影响。
This commit is contained in:
TermiNexus committed 2026-09-14 13:10:06 +08:00
1 parent 2021c0edb7
commit fc5a6134ca
7 files changed
+359 -356

No files matched your search

+14 -32
View File
@@ -162,7 +162,6 @@ def _parallel_translate_worker(src_path: str, out_path: str, src_root: str, temp
stub_files: dict[str, str], include_py_map: dict[str, str], slice_level: int,
shared_sym_pickle_path: str, struct_sha1_map: dict[str, str],
prebuilt_ModuleSha1Map: dict[str, str] | None = None,
func_hotspot_file: str | None = None,
hotspot_sites_file: str | None = None,
hotspot_sites_all: bool = False,
hotspot_callees: list[str] | None = None) -> tuple[str, str, str]:
@@ -183,10 +182,8 @@ def _parallel_translate_worker(src_path: str, out_path: str, src_root: str, temp
trans.include_py_map = include_py_map
trans.slice_level = slice_level
trans.struct_sha1_map = struct_sha1_map
# 函数热点统计(--func-hotspot):worker 子进程无父进程属性,须显式透传
# (None=未启用,零影响);生成 IR 时各模块处理器统一追加写最终 <path>
trans.func_hotspot_file = func_hotspot_file
# 调用点热点统计(--hotspot-sites):同样须显式透传(None=未启用,零影响)
# 调用点热点统计与每函数计时(--hotspot-sites):worker 子进程无父进程属性,
# 须显式透传(None=未启用,零影响);生成 IR 时各模块处理器统一追加写最终 <path>
trans.hotspot_sites_file = hotspot_sites_file
trans.hotspot_sites_all = hotspot_sites_all
trans.hotspot_callees = hotspot_callees
@@ -234,7 +231,6 @@ def _parallel_translate_batch(batch: list, src_root: str, temp_dir: str, output_
sha1_map: dict[str, str], sig_files: dict[str, str],
stub_files: dict[str, str], include_py_map: dict[str, str], slice_level: int,
shared_sym_pickle_path: str, struct_sha1_map: dict[str, str],
func_hotspot_file: str | None = None,
hotspot_sites_file: str | None = None,
hotspot_sites_all: bool = False,
hotspot_callees: list[str] | None = None) -> tuple[list, list]:
@@ -258,9 +254,7 @@ def _parallel_translate_batch(batch: list, src_root: str, temp_dir: str, output_
trans.include_py_map = include_py_map
trans.slice_level = slice_level
trans.struct_sha1_map = struct_sha1_map
# 函数热点统计(--func-hotspot):批内每文件的处理器统一追加写最终 <path>
trans.func_hotspot_file = func_hotspot_file
# 调用点热点统计(--hotspot-sites):批内每文件同样透传
# 调用点热点统计与每函数计时(--hotspot-sites):批内每文件同样透传
trans.hotspot_sites_file = hotspot_sites_file
trans.hotspot_sites_all = hotspot_sites_all
trans.hotspot_callees = hotspot_callees
@@ -662,13 +656,10 @@ class Phase2Translator:
os.environ['PATH'] = _clang64_bin + os.pathsep + os.environ.get('PATH', '')
self.asan_rt_dir = _FindAsanRuntimeDir()
self.slice_level = 3
# 函数热点统计(--func-hotspot):None=未启用(零影响);str=计数器输出
# 文件路径。各模块运行时自带 Ctrl+C 处理器、统一以追加方式写该路径;
# 含 main 的模块在 main 入口清空一次 → 每次运行一份结果(无构建后合并步骤)。
self.func_hotspot_file: str | None = None
# 调用点热点统计(--hotspot-sites):None=未启用(零影响);str=调用点计数
# 落盘文件路径。各模块运行时自带 Ctrl+C 处理器、统一以追加方式写该路径;
# 含 main 的模块在 main 入口清空一次 → 每次运行一份结果。
# 落盘文件路径(每函数计时落同目录 hotspot_time.txt)。各模块运行时自带
# Ctrl+C 处理器、统一以追加方式写该路径;含 main 的模块在 main 入口清空一次
# → 每次运行一份结果。
self.hotspot_sites_file: str | None = None
# 是否对全部直接 call 点插桩(忽略白名单);白名单被调函数基名列表(None=默认)
self.hotspot_sites_all: bool = False
@@ -876,11 +867,11 @@ class Phase2Translator:
self.struct_sha1_map = self._build_struct_sha1_map()
need_translate = []
# --func-hotspot / --hotspot-sites:缓存 .ll / .stub.ll+.text.ll 可能不含
# 热点计数 IR,启用任一功能时一律重译,保证每个模块都嵌入 internal 计数器
# 与 @llvm.global_ctors 自注册。未启用时 hotspot_on=False,缓存与重组分支
# --hotspot-sites:缓存 .ll / .stub.ll+.text.ll 可能不含热点计数/计时 IR,
# 启用时一律重译,保证每个模块都嵌入 internal 计数器、计时器与
# @llvm.global_ctors 自注册。未启用时 hotspot_on=False,缓存与重组分支
# 行为与改动前完全一致(零影响)。
hotspot_on: bool = bool(self.func_hotspot_file or self.hotspot_sites_file)
hotspot_on: bool = bool(self.hotspot_sites_file)
for i, src_path in enumerate(py_files, 1):
rel = os.path.relpath(src_path, self.src_root)
# 复用 parse_python_file 的 AST 缓存(get_file_dependencies 已 parse 过)
@@ -1039,7 +1030,6 @@ class Phase2Translator:
self.sha1_map, self.sig_files, self.stub_files,
self.include_py_map, self.slice_level,
shared_pickle_path, self.struct_sha1_map,
func_hotspot_file=self.func_hotspot_file,
hotspot_sites_file=self.hotspot_sites_file,
hotspot_sites_all=self.hotspot_sites_all,
hotspot_callees=self.hotspot_callees,
@@ -1317,7 +1307,7 @@ class Phase2Translator:
if stripped.startswith('@') and ' global ' in stripped and 'external global' not in stripped:
text_lines.append(line)
# LLVM 保留全局(如 --func-hotspot 的 @llvm.global_ctors;名字含点故
# LLVM 保留全局(如 --hotspot-sites 的 @llvm.global_ctors;名字含点故
# 最终 IR 保留引号)是 appending 定义,禁止改写为 "external global"
# 存根(否则 .ll 重组后 llc 报错)
if re.match(r'@("?)llvm\.', stripped):
@@ -1583,15 +1573,9 @@ class Phase2Translator:
trans.translator._shared_class_methods = self._shared_class_methods
trans.translator._shared_cross_module_novtable = self._shared_cross_module_novtable
# 函数热点统计(--func-hotspot,Projectrans 多模块):
# 各模块处理器统一以追加方式写最终 <path>;注册策略为本模块
# @llvm.global_ctors 自注册(见 BaseGen._InstallFuncHotspotModuleLocal)。
# self.func_hotspot_file 为 None 时不设置任何属性 → 与未启用时逐字节一致(零影响)。
if self.func_hotspot_file:
trans.translator.FuncHotspotFile = self.func_hotspot_file
trans.translator.FuncHotspotModuleLocal = True
# 调用点热点统计(--hotspot-sites,Projectrans 多模块):同上,注册策略为
# 本模块 ctor 自注册(见 BaseGen._InstallHotspotSitesModuleLocal);
# 调用点热点统计与每函数计时(--hotspot-sites,Projectrans 多模块):
# 各模块处理器统一以追加方式写最终 <path>,注册策略为本模块
# @llvm.global_ctors 自注册(见 BaseGen._InstallHotspotSitesModuleLocal);
# self.hotspot_sites_file 为 None 时不设置任何属性 → 与未启用时逐字节一致。
if self.hotspot_sites_file:
trans.translator.HotspotSitesFile = self.hotspot_sites_file
@@ -2878,7 +2862,6 @@ class Phase2Translator:
self.include_py_map, self.slice_level,
shared_pickle_path, self.struct_sha1_map,
include_ModuleSha1Map,
func_hotspot_file=self.func_hotspot_file,
hotspot_sites_file=self.hotspot_sites_file,
hotspot_sites_all=self.hotspot_sites_all,
hotspot_callees=self.hotspot_callees
@@ -2898,7 +2881,6 @@ class Phase2Translator:
self.include_py_map, self.slice_level,
shared_pickle_path, self.struct_sha1_map,
include_ModuleSha1Map,
func_hotspot_file=self.func_hotspot_file,
hotspot_sites_file=self.hotspot_sites_file,
hotspot_sites_all=self.hotspot_sites_all,
hotspot_callees=self.hotspot_callees