env_detect API

目的

Luna-Flow/mare_mark/env_detect はコンパイル先、runtime、メモリー管理、OS、ホスト名、CPU と論理コア数、現在時刻、ソース revision、新しい run id を調べ、model.EnvironmentSnapshot を作ります。環境を手書きする必要はありません。検出に失敗した欄には文書化された代替値を入れ、undetected に列挙します。全欄を上書きできます。env_detect の設計に理由を説明します。

ソース:src/env_detect/env_detect.mbt、src/env_detect/target.mbt、target ごとの 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 を使います。detect は async なので moonbitlang/async が必要です。runner.run と同様に async fn main または async test から呼びます。全 target でビルドでき、detect は async runtime のある native、JS、wasm で実行できます。通常関数 new_run_id はすべての target で実行できます。

検出

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 にも含めません。それ以外の欄は次の規則に従います。

フィールド部分上書きしない場合の値
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性能環境target のメモリー管理: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来歴OS と release(例:"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 の一部だけ分かる場合、モデルだけ、または "unknown (10 logical cores)" のようなコア数だけを使い、両方不明な場合だけ未検出扱いにします。環境変数は前後の空白を除き、空白だけの値は未設定扱いです。target ごとの検出方法は env_detect の設計を参照してください。

concurrency はベンチマーク自体の並列度です。操作を 1 個ずつ実行する場合は 1 であり、プロセスから推定できません。論理コア数は cpu の一部です。

native では最大 5 個の短い子プロセス(uname を 2 回、getconf、/proc/cpuinfo にモデルがなければ sysctl、git)を各 5 秒まで実行します。コマンドが存在しない、失敗する、時間切れになる場合、その欄だけ未検出になります。他の target では 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、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")
}

Run id

new_run_id

new_run_id は指定時刻に始まる run の id を生成します。

pub fn new_run_id(UInt64) -> String

引数は @env.now() と同じ Unix epoch からのミリ秒です。id はミリ秒付き UTC の基本 ISO 8601 形式、ハイフン、platform のエントロピー源の 8 bytes を表す小文字 16 桁の 16 進文字列から成ります。

YYYYMMDDTHHMMSSmmmZ-<16 hex digits>      20261009T071142123Z-9f2c4e01a7b3d856

エントロピー源がない場合(wasm-gc、または host が要求を拒否した場合)、接尾辞は 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

並列度 00 でベンチマークは動かないので、実際の値と混同しません。

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 は runner が全 target で使う moonbitlang/core/bench の単調時計です。DEFAULT_DEVICE と DEFAULT_FREQUENCY_POLICY は @model.PerformanceEnvironment::new の既定値です。既定値はマシンの観測結果ではないので、この 3 欄も 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")
}