1#![allow(dead_code)]
12
13use alloc::string::String;
14use alloc::vec::Vec;
15use spin::Mutex;
16
17const DRAW_BUDGET_MS: u64 = 2000;
21const OFFENSE_LIMIT: u32 = 3;
23
24struct DrawWatch {
25 active_app: Option<String>,
27 start_ms: u64,
29 hang_reported: bool,
31 offenders: Vec<(String, u32)>,
33 not_responding: Vec<String>,
35}
36
37static WATCH: Mutex<DrawWatch> = Mutex::new(DrawWatch {
38 active_app: None,
39 start_ms: 0,
40 hang_reported: false,
41 offenders: Vec::new(),
42 not_responding: Vec::new(),
43});
44
45pub fn enter(app_name: &str) {
47 let now = crate::kernel::timer::get_system_time_ms();
48 let mut w = WATCH.lock();
49 w.active_app = Some(String::from(app_name));
50 w.start_ms = now;
51 w.hang_reported = false;
52}
53
54pub fn exit() {
59 let now = crate::kernel::timer::get_system_time_ms();
60 let mut w = WATCH.lock();
61 let elapsed = now.wrapping_sub(w.start_ms);
62 let was_near_hang = w.hang_reported;
63 if let Some(name) = w.active_app.take() {
64 if was_near_hang {
65 let mut found = false;
67 let mut hit_limit = false;
68 for (n, c) in w.offenders.iter_mut() {
69 if *n == name {
70 *c += 1;
71 if *c >= OFFENSE_LIMIT {
72 hit_limit = true;
73 }
74 found = true;
75 break;
76 }
77 }
78 if !found {
79 w.offenders.push((name.clone(), 1));
80 }
81 crate::warn!(
82 "[WDOG] app '{}' near-hang draw took {} ms",
83 name,
84 elapsed
85 );
86 if hit_limit && !w.not_responding.iter().any(|n| n == &name) {
87 w.not_responding.push(name.clone());
88 crate::warn!(
89 "[WDOG] app '{}' marked NOT RESPONDING — its draw will be skipped",
90 name
91 );
92 }
93 } else if elapsed > DRAW_BUDGET_MS {
94 crate::info!(
96 "[WDOG] app '{}' slow draw: {} ms (budget {} ms)",
97 name,
98 elapsed,
99 DRAW_BUDGET_MS
100 );
101 } else {
102 w.offenders.retain(|(n, _)| n != &name);
104 }
105 }
106}
107
108pub fn check_from_irq() {
111 if let Some(mut w) = WATCH.try_lock() {
113 if w.hang_reported {
114 return;
115 }
116 if let Some(name) = w.active_app.clone() {
117 let now = crate::kernel::timer::get_system_time_ms();
118 let elapsed = now.wrapping_sub(w.start_ms);
119 if elapsed > DRAW_BUDGET_MS * 2 {
120 w.hang_reported = true;
121 crate::warn!(
122 "[WDOG] HANG suspected: app '{}' has been drawing for {} ms",
123 name,
124 elapsed
125 );
126 }
127 }
128 }
129}
130
131pub fn is_not_responding(app_name: &str) -> bool {
133 let w = WATCH.lock();
134 w.not_responding.iter().any(|n| n == app_name)
135}
136
137pub fn clear(app_name: &str) {
139 let mut w = WATCH.lock();
140 w.offenders.retain(|(n, _)| n != app_name);
141 w.not_responding.retain(|n| n != app_name);
142}
143
144const SLOW_US: u64 = 100_000;
146static SLOW_START: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
148static SLOW_ELR: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
149static SLOW_NAME: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
150static SLOW_REPORTED: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
152
153pub fn report_os_slow() {
158 use core::sync::atomic::Ordering;
159 let start = SLOW_START.load(Ordering::Relaxed);
160 if start == 0 || SLOW_REPORTED.load(Ordering::Relaxed) == start {
161 return;
162 }
163 let cur = crate::kernel::responsiveness::os_loop_last_us();
164 if cur == start {
165 return;
167 }
168 SLOW_REPORTED.store(start, Ordering::Relaxed);
169 let v = SLOW_NAME.load(Ordering::Relaxed);
170 let len = (v >> 48) as usize;
171 let ptr = (v & 0xFFFF_FFFF_FFFF) as *const u8;
172 let name = if ptr.is_null() {
173 "?"
174 } else {
175 unsafe { core::str::from_utf8(core::slice::from_raw_parts(ptr, len)).unwrap_or("?") }
177 };
178 crate::warn!(
179 "[WDOG] [OSSLOW] OS のメインループの 1 周が {}ms: 100ms の時点で core={} thread='{}' elr={:#x}",
180 cur.saturating_sub(start) / 1000,
181 crate::kernel::scheduler::OS_CORE,
182 name,
183 SLOW_ELR.load(Ordering::Relaxed)
184 );
185}
186
187pub fn check_os_loop_from_irq() {
194 use core::sync::atomic::{AtomicU64, Ordering};
195 const STALL_US: u64 = 3_000_000;
196 static REPORTED: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4];
198 let last = crate::kernel::responsiveness::os_loop_last_us();
199 if last == 0 {
200 return;
201 }
202 let now = crate::kernel::timer::get_system_time_us();
203 let cid = crate::kernel::scheduler::core_id();
204 if cid == crate::kernel::scheduler::OS_CORE
205 && now.saturating_sub(last) >= SLOW_US
206 && SLOW_START.load(Ordering::Relaxed) != last
207 {
208 let elr: u64;
210 unsafe { core::arch::asm!("mrs {}, elr_el1", out(reg) elr, options(nomem, nostack, preserves_flags)) };
212 let name = crate::kernel::scheduler::get_current_process_name().unwrap_or("?");
213 SLOW_ELR.store(elr, Ordering::Relaxed);
214 SLOW_NAME.store(name.as_ptr() as u64 | ((name.len() as u64) << 48), Ordering::Relaxed);
215 SLOW_START.store(last, Ordering::Relaxed);
216 }
217 if now.saturating_sub(last) < STALL_US {
218 return;
219 }
220 let Some(slot) = REPORTED.get(cid) else { return };
221 if slot.swap(last, Ordering::Relaxed) == last {
222 return;
223 }
224 let elr: u64;
225 unsafe { core::arch::asm!("mrs {}, elr_el1", out(reg) elr, options(nomem, nostack, preserves_flags)) };
227 let name = crate::kernel::scheduler::get_current_process_name().unwrap_or("?");
228 crate::kernel::uart::with_urgent_output(|| {
230 crate::warn!(
231 "[WDOG] [OSSTALL] OS のメインループが {}ms 進んでいない: core={} thread='{}' elr={:#x}",
232 now.saturating_sub(last) / 1000,
233 cid,
234 name,
235 elr
236 );
237 });
238}