cpubench: init
This commit is contained in:
112
src/cpubench.ts
Normal file
112
src/cpubench.ts
Normal file
@@ -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<void> {
|
||||||
|
const defaults: Partial<RunCPUBenchOptions> = {
|
||||||
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user