bin/runcpu: don't wrap programs under systemd, parse the result instead

This commit is contained in:
2025-04-18 09:42:46 +08:00
parent 76c230f554
commit 1766cbdd15

View File

@@ -1,10 +1,8 @@
import yargs from 'yargs'; import yargs from 'yargs';
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import { promisifySpawn } from '../cli.js';
import { RunCPUOptions, runcpuOptions } from '../commands/spec.js'; import { RunCPUOptions, runcpuOptions } from '../commands/spec.js';
import { systemdRunOptions } from '../commands/systemd.js';
import path from 'path'; import path from 'path';
import { ConfigOptions, mkSPEC, renderConfig } from '../spec/index.js'; import { ConfigOptions, mkSPEC, renderConfig, getBaseRatios } from '../spec/index.js';
import { sw64TargetOptions } from '../commands/compiler.js'; import { sw64TargetOptions } from '../commands/compiler.js';
import crypto from 'crypto'; import crypto from 'crypto';
import { hideBin } from 'yargs/helpers'; import { hideBin } from 'yargs/helpers';
@@ -85,31 +83,16 @@ console.log(`using sysroot: ${chalk.red(sysroot)}`);
const specPath = path.resolve(argv.spec); const specPath = path.resolve(argv.spec);
interface systemdRunSPECOptions {
specConfig: string;
id: string;
runcpuOverride?: (old: RunCPUOptions) => RunCPUOptions;
}
function systemdRunSPEC({ function runspec(specConfig: string, id: string) {
specConfig,
id,
runcpuOverride,
}: systemdRunSPECOptions) {
const config = `${id}.cfg`; const config = `${id}.cfg`;
const spec = mkSPEC(specPath); const spec = mkSPEC(specPath);
spec.newConfig(config, specConfig); spec.newConfig(config, specConfig);
return spawn('systemd-run', [ return spawn('runcpu', [
...systemdRunOptions({ ...runcpuOptions({
scope: false,
unit: id,
user: true,
}),
'runcpu',
...runcpuOptions((runcpuOverride || (x => x))({
outputRoot: path.resolve(path.resolve(argv.output), id), outputRoot: path.resolve(path.resolve(argv.output), id),
config, config,
setprocgroup: true, setprocgroup: true,
@@ -117,8 +100,8 @@ function systemdRunSPEC({
buildType: argv.buildType as RunCPUOptions["buildType"], buildType: argv.buildType as RunCPUOptions["buildType"],
benchmarks: argv.benchmarks as RunCPUOptions["benchmarks"], benchmarks: argv.benchmarks as RunCPUOptions["benchmarks"],
action: argv.action as RunCPUOptions["action"], action: argv.action as RunCPUOptions["action"],
})), }),
], { env: spec.getEnvironment(), stdio: "inherit" }); ], { env: spec.getEnvironment() });
} }
// Function to convert hash to prefix // Function to convert hash to prefix
@@ -128,8 +111,6 @@ const uuid = crypto.randomUUID();
const prefix = path.resolve(argv.compilerPrefix); const prefix = path.resolve(argv.compilerPrefix);
console.log(prefix);
const o3LTOFlags = [ const o3LTOFlags = [
"-O3", "-O3",
"-flto", "-flto",
@@ -143,29 +124,26 @@ const unalignedFlags = [
]; ];
// SPEC configuration
const specConfig = renderConfig({
prefix,
optimize: [
"-O2",
...sw64TargetOptions({ simd: argv.simd }),
...argv.compilerSuite == "llvm" ? [sunway.mcpu(sunwayGeneration)] : [], // Only add -mcpu for llvm
],
ldflags: [
'-static',
`-L${path.resolve(prefix, 'lib')}`,
],
sysroot,
compiler: argv.compilerSuite as ConfigOptions["compiler"],
});
console.log(`Running SPEC compiled from ${prefix} as ${uuid}`); console.log(`Running SPEC compiled from ${prefix} as ${uuid}`);
console.log(`View: ${path.resolve(argv.output, uuid)}`)
await promisifySpawn(systemdRunSPEC({ const specProc = runspec(
specConfig, renderConfig({
id: uuid, prefix,
})); optimize: [
"-O2",
...sw64TargetOptions({ simd: argv.simd }),
...argv.compilerSuite == "llvm" ? [sunway.mcpu(sunwayGeneration)] : [], // Only add -mcpu for llvm
],
ldflags: [
'-static',
`-L${path.resolve(prefix, 'lib')}`,
],
sysroot,
compiler: argv.compilerSuite as ConfigOptions["compiler"],
}),
uuid
);
console.log(`Command suggest:`); const baseRatios = await getBaseRatios(specProc);
console.log(chalk.green(`systemctl --user status ${uuid}`)); console.log(baseRatios);
console.log(chalk.green(`journalctl --user -xeu ${uuid}`));