From 26a8d3908f203e6cce528b059f7277439dbfe25a Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Tue, 12 Aug 2025 22:59:25 +0800 Subject: [PATCH] cpubench: init --- src/cpubench.ts | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/cpubench.ts diff --git a/src/cpubench.ts b/src/cpubench.ts new file mode 100644 index 0000000..455a658 --- /dev/null +++ b/src/cpubench.ts @@ -0,0 +1,112 @@ +import { spawn } from 'child_process'; +import { randomUUID } from 'crypto'; +import * as fs from 'fs/promises'; +import path from 'path'; +import { promisifySpawn } from './cli.js'; +import { sw64TargetOptions } from './commands/compiler.js'; +import * as cpubench from './cpubench/index.js'; +import * as sunway from './sunway.js'; + +// 定义函数参数接口,所有参数都设为可选,以便支持默认值 +export interface RunCPUBenchOptions { + llvm: string; + cpubench: string; + simd?: boolean; + benchmarks: string[]; + optimizeProfile: 'O2' | 'base'; + rebuild?: boolean; + workdir: string; + uuid?: boolean; + platform: sunway.SunwayGeneration | "host"; + allowMisaligned?: boolean; + action?: string; +} + +/** + * 运行CPUBench的函数,带有默认选项 + * @param options 运行CPUBench所需的配置选项(可选) + */ +export async function runCPUBench(options: RunCPUBenchOptions): Promise { + const defaults: Partial = { + uuid: true, + platform: 'host', + action: 'standard', + }; + + const config = { ...defaults, ...options } as RunCPUBenchOptions; + + const sw64UnalignedFlag = "-sw64-allows-misaligned-memory-accesses"; + + const cpubenchDir = path.resolve(config.cpubench); + const llvmInstall = path.resolve(config.llvm); + + const ltoFlags = [ + "-flto", + "-fuse-ld=lld", + ]; + + const optimizeProfiles = { + base: [ + "-O3", + ...ltoFlags, + ], + O2: [ + "-O2", + ] + }; + + const optimizeFlags = optimizeProfiles[config.optimizeProfile]; + + const sunwayGeneration = config.platform === "host" + ? sunway.getHostGeneration() + : config.platform; + + const cpubenchConfig = cpubench.renderConfig({ + llvmInstall: llvmInstall, + optimize: [ + ...sw64TargetOptions({ simd: config.simd }), + ...optimizeFlags, + ...config.allowMisaligned ? ["-mllvm", sw64UnalignedFlag] : [], + sunway.mcpu(sunwayGeneration), + ], + benchmarks: config.benchmarks as cpubench.CPUBenchConfigOptions["benchmarks"], + ldflags: [ + ...optimizeFlags, + "-Wl,-z,notext", + ...config.optimizeProfile === "base" ? [ + ...config.simd ? ["-Wl,-plugin-opt,--mattr=simd"] : [], + ...config.allowMisaligned ? [`-Wl,-plugin-opt,${sw64UnalignedFlag}`] : [], + sunway.mcpu(sunwayGeneration), + ] : [], + ], + }); + + const uuid = randomUUID(); + + const configFile = path.resolve(cpubenchDir, 'config', `rendered-${uuid}.config`); + + await fs.writeFile(configFile, cpubenchConfig); + + const workdir = config.uuid ? path.join(config.workdir, uuid) : config.workdir; + console.log(`Using workdir = ${workdir}`); + await fs.mkdir(workdir, { recursive: true }); + + const proc = spawn( + path.resolve(cpubenchDir, 'cpubench.sh'), + [ + `--config=${configFile}`, + "--skip_verify=1", + (`--rebuild=${config.rebuild ? "1" : "0"}`), + "-i=1", + `--work_dir=${workdir}`, + `--perf=1`, + `-a=${config.action}`, + ], + { cwd: cpubenchDir } + ); + + proc.stdout.pipe(process.stdout); + proc.stderr.pipe(process.stderr); + + await promisifySpawn(proc); +}