Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions tools/launcher/core/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,17 @@ fn app_pids(app: &Value, pids: &[u32]) -> Vec<u32> {
.collect()
}

/// The app's state from its own simax reader (foreground / background), or what its process says (suspended)
fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap<u32, Row>) -> String {
if procs.get(&host_pid).is_some_and(|r| r.state == 'T' || r.state == 't') {
return "suspended".into();
}
let has_reader = crate::ax::endpoints(inst).iter().any(|e| crate::ax::pid_of_sock(&e.sock) == pid);
if !has_reader {
return "running".into();
}
match crate::ax::ask_pid(inst, pid, &json!({"op": "info"}), Duration::from_millis(1200)) {
const INFO_QUERY_TIMEOUT_MS: u64 = 1200;
const INFO_SIMAX_TIMEOUT_SECS: f64 = 0.9;

fn info_query() -> Value {
// Keep simax's main-thread wait strictly below INFO_QUERY_TIMEOUT_MS so a hung main thread
// returns {"error": "busy: ..."} instead of timing out the socket and being misclassified as "suspended".
json!({"op": "info", "timeout": INFO_SIMAX_TIMEOUT_SECS})
}

fn classify_info_reply(reply: Result<Value>) -> String {
match reply {
Ok(v) if v["error"].as_str().is_some_and(|e| e.starts_with("busy")) => "busy (main thread not answering)".into(),
Ok(v) => match v["state"].as_str().unwrap_or("") {
"active" => "foreground".into(),
Expand All @@ -196,6 +197,23 @@ fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap<u32, Row>)
}
}

/// The app's state from its own simax reader (foreground / background), or what its process says (suspended)
fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap<u32, Row>) -> String {
if procs.get(&host_pid).is_some_and(|r| r.state == 'T' || r.state == 't') {
return "suspended".into();
}
let has_reader = crate::ax::endpoints(inst).iter().any(|e| crate::ax::pid_of_sock(&e.sock) == pid);
if !has_reader {
return "running".into();
}
classify_info_reply(crate::ax::ask_pid(
inst,
pid,
&info_query(),
Duration::from_millis(INFO_QUERY_TIMEOUT_MS),
))
}

/// The state dir and Darling prefix this boot of the phone uses
fn dirs(inst: &Instance) -> (PathBuf, PathBuf) {
match crate::instance::Monitor::default().poll(std::slice::from_ref(inst)).into_iter().next() {
Expand Down Expand Up @@ -516,4 +534,23 @@ mod tests {
Some("translator: dynarmic assertion failed: !conf.shared_translations: dynarmic: shared code cache full (67108864 bytes)")
);
}

#[test]
fn info_query_distinguishes_busy_from_suspended() {
let q = info_query();
assert_eq!(q["op"].as_str(), Some("info"));
let simax_timeout = q["timeout"].as_f64().expect("timeout must be present");
assert!(
simax_timeout > 0.05 && (simax_timeout * 1000.0) < INFO_QUERY_TIMEOUT_MS as f64,
"simax timeout {simax_timeout}s must be strictly below socket timeout {INFO_QUERY_TIMEOUT_MS}ms"
);
assert_eq!(
classify_info_reply(Ok(json!({"error": "busy: the main thread did not answer in time"}))),
"busy (main thread not answering)"
);
assert_eq!(classify_info_reply(Ok(json!({"state": "active"}))), "foreground");
assert_eq!(classify_info_reply(Ok(json!({"state": "inactive"}))), "foreground (inactive)");
assert_eq!(classify_info_reply(Ok(json!({"state": "background"}))), "background");
assert_eq!(classify_info_reply(Err(anyhow::anyhow!("timeout"))), "suspended");
}
}
Loading