env_detect API
用途
Luna-Flow/mare_mark/env_detect 根据运行中的进程能查明的关于自身的信息来填充 model.EnvironmentSnapshot:编译目标、运行时与内存管理、操作系统、主机名、CPU 型号及其逻辑核心数、当前时间、源码修订版本以及一个新的运行 id。这样,一次运行就不再需要手写环境。检测从不失败,也从不猜测:无法查明的字段会取一个有文档说明的回退值,并列在 undetected 中;每个字段都可以被覆盖。采用这种形式的原因见 env_detect 设计。
源码:src/env_detect/env_detect.mbt、src/env_detect/target.mbt,以及各目标的探测代码 probe_native.mbt、probe_js.mbt 和 probe_none.mbt。
导入
把这些包加入使用它们的那个包的 moon.pkg:
import {
"Luna-Flow/mare_mark/model",
"Luna-Flow/mare_mark/env_detect",
"moonbitlang/async",
}
本页的示例通过它们的默认别名(@model、@env_detect)调用它们。之所以需要 moonbitlang/async,是因为 detect 是 async 的:要像 runner.run 一样,在 async fn main 或 async test 中调用它。该包在所有目标上都能构建;detect 在 moonbitlang/async 有运行时的目标(native、JS、wasm)上运行,而 new_run_id 是一个普通函数,在任何地方都能运行。
检测
detect
detect 构建运行中进程的环境快照。
pub async fn detect(target? : @model.ExecutionTarget, toolchain? : String, compiler_flags? : String, dtype_abi? : String, runtime? : String, cpu? : String, gc? : String, concurrency? : Int, clock? : String, device? : String, frequency_policy? : String, os? : String, hostname? : String, timestamp? : String, revision? : String, run_id? : String) -> DetectedEnvironment
每个参数都是可选的,并以它所设置的快照字段命名。作为参数传入的字段按原样使用:它不会被探测,也永远不会出现在 undetected 中。对其他每个字段,detect 的做法如下。
| 字段 | 部分 | 未被覆盖时的值 |
|---|---|---|
target | 语义 | 代码编译所针对的目标(Native、Js、Wasm、WasmGc 或 Llvm) |
toolchain | 语义 | 环境变量 MARE_MARK_TOOLCHAIN,否则为 UNKNOWN |
compiler_flags | 语义 | 从不检测:UNKNOWN |
dtype_abi | 语义 | 从不检测:UNKNOWN |
runtime | 性能 | 在 native 和 LLVM 上为 "native",在 Node.js 上为 "node <version>",否则为 UNKNOWN |
cpu | 性能 | CPU 型号后接逻辑核心数,例如 "Apple M4 (10 logical cores)",否则为 UNKNOWN |
gc | 性能 | 目标的内存管理方式:在 native、LLVM 和 wasm 上为 "rc",在 wasm-gc 上为 "wasm-gc",在 JS 上为 "js-engine" |
concurrency | 性能 | 从不检测:UNKNOWN_CONCURRENCY |
clock | 性能 | 从不检测:DEFAULT_CLOCK |
device | 性能 | 从不检测:DEFAULT_DEVICE |
frequency_policy | 性能 | 从不检测:DEFAULT_FREQUENCY_POLICY |
os | 来源信息 | 操作系统及其发行版本,例如 "Darwin 24.1.0",否则为 UNKNOWN |
hostname | 来源信息 | 主机名,否则为 UNKNOWN |
timestamp | 来源信息 | 当前 UTC 时间,格式为 YYYY-MM-DDTHH:MM:SSZ |
revision | 来源信息 | GITHUB_SHA、CI_COMMIT_SHA 或 GIT_COMMIT,否则在 native 上为 git rev-parse HEAD,再否则为 UNKNOWN |
run_id | 来源信息 | 与 timestamp 同一时刻的 new_run_id |
target、gc、timestamp 和 run_id 总是有值,从不报告为未检测到。当 CPU 文本只知道一半时,cpu 只有型号,或者只知道核心数时为 "unknown (10 logical cores)";只有两者都不知道时它才算未检测到。环境变量在读取时会去除首尾空白,被设为空白的变量视为未设置。env_detect 设计列出了每个字段在每个目标上的探测方式。
concurrency 是基准测试自身的并行度(一次运行一个操作的基准测试为 1),进程无从知道。机器的逻辑核心数属于 cpu,而不是 concurrency。
在 native 上,检测最多运行五个短时子进程(两次 uname、getconf、当 /proc/cpuinfo 中没有型号时的 sysctl、git);每个最多给 5 秒,命令缺失、失败或超时只会让对应字段保持未检测状态。在其他目标上,detect 不会挂起。
async test "overrides are used as given" {
let detected = @env_detect.detect(
toolchain="moonc 0.10",
compiler_flags="release",
dtype_abi="f64",
concurrency=1,
hostname="bench-1",
)
let snapshot = detected.snapshot
inspect(snapshot.semantic.toolchain, content="moonc 0.10")
inspect(snapshot.performance.concurrency, content="1")
inspect(snapshot.provenance.hostname, content="bench-1")
inspect(detected.undetected.contains("compiler_flags"), content="false")
inspect(detected.undetected.contains("clock"), content="true")
inspect(snapshot.performance.clock, content="monotonic")
}
DetectedEnvironment
DetectedEnvironment 是 detect 的结果:快照,以及取了回退值的字段。
pub struct DetectedEnvironment {
snapshot : @model.EnvironmentSnapshot
undetected : Array[String]
}
snapshot 是一个普通的 EnvironmentSnapshot;把它传给 @runner.RunContext::new。undetected 列出既未检测到也未被覆盖的字段,名称与 JSONL 记录中的拼写一致("toolchain"、"cpu"、"concurrency" 等),每个只出现一次,并按快照顺序排列:toolchain、compiler_flags、dtype_abi、runtime、cpu、concurrency、clock、device、frequency_policy、os、hostname、revision。一个字段出现在列表中,当且仅当它因为没有更好的信息而取了 UNKNOWN、UNKNOWN_CONCURRENCY 或其 DEFAULT_* 常量。
async test "nothing is undetected when everything is given" {
let detected = @env_detect.detect(
target=@model.ExecutionTarget::Native,
toolchain="moonc 0.10",
compiler_flags="release",
dtype_abi="f64",
runtime="native",
cpu="Apple M4 (10 logical cores)",
gc="rc",
concurrency=1,
clock="monotonic",
device="host",
frequency_policy="uncontrolled",
os="Darwin 24.1.0",
hostname="bench-1",
timestamp="2026-10-09T08:00:00Z",
revision="628d39e",
run_id="nightly-17",
)
inspect(detected.undetected.length(), content="0")
inspect(detected.snapshot.provenance.run_id, content="nightly-17")
}
运行 id
new_run_id
new_run_id 为一次在给定时间开始的运行生成运行 id。
pub fn new_run_id(UInt64) -> String
参数是自 Unix 纪元以来的毫秒数,即 @env.now() 的返回值。id 由三部分组成:带毫秒的 ISO 8601 基本格式 UTC 时间、一个连字符,以及来自平台熵源 8 个字节的 16 位小写十六进制数字:
YYYYMMDDTHHMMSSmmmZ-<16 hex digits> 20261009T071142123Z-9f2c4e01a7b3d856
当平台没有熵源时(wasm-gc,或拒绝请求的宿主),后缀为 seq<n>,其中 n 统计当前进程中的调用次数;这样的 id 只在该进程内唯一。由于每个字段都是定宽的,id 按时间排序。
test "run ids start with the UTC time" {
let first = @env_detect.new_run_id(0UL)
let second = @env_detect.new_run_id(0UL)
inspect(first.has_prefix("19700101T000000000Z-"), content="true")
inspect(first != second, content="true")
inspect(@env_detect.new_run_id(1791529902123UL).has_prefix("20261009T071142123Z-"), content="true")
}
回退值
UNKNOWN
UNKNOWN 是既未检测到也未被覆盖的文本字段的值。
pub const UNKNOWN : String = "unknown"
UNKNOWN_CONCURRENCY
UNKNOWN_CONCURRENCY 是未被覆盖时 concurrency 的值。
pub const UNKNOWN_CONCURRENCY : Int = 0
没有基准测试会以 的并行度运行,因此这个值不会被误认为真实值。
DEFAULT_CLOCK, DEFAULT_DEVICE, DEFAULT_FREQUENCY_POLICY
这些常量是 clock、device 和 frequency_policy 未被覆盖时的值。
pub const DEFAULT_CLOCK : String = "monotonic"
pub const DEFAULT_DEVICE : String = "host"
pub const DEFAULT_FREQUENCY_POLICY : String = "uncontrolled"
DEFAULT_CLOCK 指运行器在所有目标上使用的 moonbitlang/core/bench 单调时钟;DEFAULT_DEVICE 和 DEFAULT_FREQUENCY_POLICY 是 @model.PerformanceEnvironment::new 的默认值。这三个字段仍然列在 undetected 中,因为默认值只是一种约定,而不是对机器的观察。
test "fallback constants" {
inspect(@env_detect.UNKNOWN, content="unknown")
inspect(@env_detect.UNKNOWN_CONCURRENCY, content="0")
inspect(@env_detect.DEFAULT_CLOCK, content="monotonic")
inspect(@env_detect.DEFAULT_DEVICE, content="host")
inspect(@env_detect.DEFAULT_FREQUENCY_POLICY, content="uncontrolled")
}