1use core::sync::atomic::{AtomicU32, Ordering};
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum Slot {
37 HttpsGet = 0,
39 TlsWriteWait = 1,
41 TlsReadWait = 2,
43 CacheRead = 3,
45 CacheWrite = 4,
47 FetchScript = 5,
49 FontFace = 6,
51 FontParse = 7,
53 ImageDecode = 8,
55 SleepActual = 9,
57 PeekCheck = 10,
59 EthPoll = 11,
61 FirstByte = 12,
64 ReadSleep = 13,
66 ReadRecv = 14,
68 DnsQuery = 15,
70 TcpConnect = 16,
72 TlsHandshake = 17,
74 CssParse = 18,
76 CssKeyCmp = 19,
78 PaintBgImage = 20,
80 PaintGradient = 21,
82 PaintImg = 22,
84 PaintText = 23,
86 DrawCollect = 24,
88 DrawElements = 25,
90}
91
92pub const SLOT_COUNT: usize = 26;
94
95const NAMES: [&str; SLOT_COUNT] = [
97 "https取得",
98 "送信待ち",
99 "受信待ち",
100 "cache読",
101 "cache書",
102 "css/js取得",
103 "fontface",
104 "font解析",
105 "img復号",
106 "sleep実測",
107 "応答確認",
108 "eth poll",
109 "初回応答待ち",
110 "受信sleep",
111 "recv呼出",
112 "DNS",
113 "TCP接続",
114 "TLS握手",
115 "CSS解析",
116 "鍵比較",
117 "背景画像",
118 "グラデ",
119 "img描画",
120 "文字描画",
121 "画像収集",
122 "要素ループ",
123];
124
125#[allow(clippy::declare_interior_mutable_const)] const ZERO: AtomicU32 = AtomicU32::new(0);
127static TICKS: [AtomicU32; SLOT_COUNT] = [ZERO; SLOT_COUNT];
128static CALLS: [AtomicU32; SLOT_COUNT] = [ZERO; SLOT_COUNT];
129
130#[inline]
132pub fn start() -> usize {
133 crate::kernel::timer::get_ticks()
134}
135
136#[inline]
138pub fn add(slot: Slot, started: usize) {
139 let d = crate::kernel::timer::get_ticks().wrapping_sub(started);
140 let i = slot as usize;
141 if i < SLOT_COUNT {
142 TICKS[i].fetch_add(d as u32, Ordering::Relaxed);
143 CALLS[i].fetch_add(1, Ordering::Relaxed);
144 }
145 if slot == Slot::TlsHandshake {
147 crate::kernel::responsiveness::add(crate::kernel::responsiveness::Qty::TlsHandshakes, 1);
148 }
149}
150
151pub struct Guard {
163 slot: Slot,
164 started: usize,
165}
166
167impl Guard {
168 pub fn new(slot: Slot) -> Self {
169 Self {
170 slot,
171 started: start(),
172 }
173 }
174}
175
176impl Drop for Guard {
177 fn drop(&mut self) {
178 add(self.slot, self.started);
179 }
180}
181
182pub fn snapshot() -> [(&'static str, u32, u32); SLOT_COUNT] {
184 let mut out = [("", 0u32, 0u32); SLOT_COUNT];
185 for i in 0..SLOT_COUNT {
186 out[i] = (
187 NAMES[i],
188 TICKS[i].load(Ordering::Relaxed),
189 CALLS[i].load(Ordering::Relaxed),
190 );
191 }
192 out
193}
194
195pub fn dump(tag: &str) {
199 let s = snapshot();
200 crate::warn!(
201 "[PERF][ALL] {} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{}",
202 tag,
203 s[0].0, s[0].1, s[0].2,
204 s[1].0, s[1].1, s[1].2,
205 s[2].0, s[2].1, s[2].2,
206 s[3].0, s[3].1, s[3].2,
207 s[4].0, s[4].1, s[4].2,
208 s[5].0, s[5].1, s[5].2,
209 s[6].0, s[6].1, s[6].2,
210 s[7].0, s[7].1, s[7].2,
211 s[8].0, s[8].1, s[8].2,
212 );
213 crate::warn!(
214 "[PERF][ALL2] {} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{}",
215 tag,
216 s[9].0, s[9].1, s[9].2,
217 s[10].0, s[10].1, s[10].2,
218 s[11].0, s[11].1, s[11].2,
219 s[12].0, s[12].1, s[12].2,
220 s[13].0, s[13].1, s[13].2,
221 s[14].0, s[14].1, s[14].2,
222 );
223 crate::warn!(
224 "[PERF][ALL3] {} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{}",
225 tag,
226 s[15].0, s[15].1, s[15].2,
227 s[16].0, s[16].1, s[16].2,
228 s[17].0, s[17].1, s[17].2,
229 s[18].0, s[18].1, s[18].2,
230 s[19].0, s[19].1, s[19].2,
231 );
232 crate::warn!(
241 "[PERF][ALL4] {} 送信再送={} seg採用={} seg破棄={} RXフレーム={}",
242 tag,
243 crate::kernel::tls::WRITE_RETX_COUNT.load(core::sync::atomic::Ordering::Relaxed),
244 crate::kernel::net::tcp::SEG_ACCEPTED.load(core::sync::atomic::Ordering::Relaxed),
245 crate::kernel::net::tcp::SEG_DROPPED.load(core::sync::atomic::Ordering::Relaxed),
246 crate::kernel::usb::RX_FRAME_COUNT.load(core::sync::atomic::Ordering::Relaxed),
247 );
248 crate::warn!(
249 "[PERF][PAINT] {} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{} {}={}/{}",
250 tag,
251 s[20].0, s[20].1, s[20].2,
252 s[21].0, s[21].1, s[21].2,
253 s[22].0, s[22].1, s[22].2,
254 s[23].0, s[23].1, s[23].2,
255 s[24].0, s[24].1, s[24].2,
256 s[25].0, s[25].1, s[25].2,
257 );
258 }