跳到主要內容

發表文章

Android ANR 與原生崩潰拆解:InputDispatcher、Watchdog 到 Tombstone / Android ANR & Native Crash Debugging: From InputDispatcher and Watchdog to Linux Signals and Tombstones

daily-tech:2026-12-05 現象: 上一篇我們把 Android 從 Linux 核心、 init 、 Zygote 到 SystemServer 的開機啟動鏈一路攤平;今天接著看系統進入日常運作後,工程師最常面對的兩種「現場凍住或瞬間退場」情境。你可能遇過這樣的畫面:手指在 App 上連點了幾下毫無反應,過幾秒跳出「應用程式沒有回應」(ANR, Application Not Responding);更棘手一點,整個畫面突然黑屏並直接退回開機動畫,卻沒有重跑硬體開機商標;又或者呼叫 C/C++ 原生函式庫(例如影像編解碼或端側推論引擎)的瞬間,App 連個 Java 例外都沒留下就直接閃退。 Symptom: In our previous post, we traced the Android boot chain from the Linux kernel through init , Zygote , and SystemServer . Today we examine what happens once the system is up and something freezes or vanishes. You have likely seen an app ignore taps for several seconds before popping up an "Application Not Responding" (ANR) dialog, or watched the entire UI cut back to the boot animation without a full hardware reboot, or seen an app vanish instantly while running C/C++ native code (such as media codecs or an edge inference engine) without throwing a Java exception. 這三種表面現象背後其實對應著三條截然不同的監控與崩潰機制:前景應用程式卡住走的是 InputDispatcher 逾時與 ANR 轉儲路徑 ;整機退回開機動畫通常是 SystemSe...
最近的文章

Android 開機啟動鏈拆解:從 init、Zygote 到 SystemServer 的行程孵化與服務註冊時序 / Android Boot Chain Deconstructed: Process Forking, Preloading, and Service Startup from init and Zygote to SystemServer

daily-tech:2026-12-04 現象: 上一篇我們拆解了系統跑起來之後,如何用 ftrace 與 perf 揪出核心延遲尖峰與鎖競爭熱點;今天我們把時間軸往回拉,對準按下電源鍵到桌面出現的那幾十秒。你可能遇過這樣的畫面:Android 手機做完系統更新重新開機,或是開發板剛燒錄好新映像檔,螢幕一直停在開機動畫打轉,既沒有直接當機重啟,也看不到哪個環節卡住了。究竟是 Linux 核心還沒交棒、磁碟分區掛載慢了、Zygote 預載類別太重,還是 SystemServer 裡某個系統服務在主執行緒卡死? Symptom: In our previous post, we used ftrace and perf to hunt down kernel latency spikes and lock contention on a running system. Today we rewind the clock to the tens of seconds between pressing the power button and seeing the home screen. You have likely seen an Android phone after an OS update—or a freshly flashed dev board—spin endlessly on the boot animation without crashing or revealing where time is going. Is the Linux kernel still handing off control, is partition mounting stalled, is Zygote preloading too much, or is a service inside SystemServer blocking the main thread? 想看懂開機時間花去哪裡,不能只把開機動畫當成一個黑盒子。整個 Android 使用者空間(Userspace)的誕生其實是一條高度有序的接力鏈:從 Linux 核心交出第一個使用者行程 init (PID 1) ,再由 init 喚起行程母體 Zygote 預先暖機 AR...

Linux 效能除錯實戰:用 ftrace 與 perf 抓出核心延遲尖峰與鎖競爭熱點 / Linux Performance Debugging with ftrace and perf: Catching Kernel Latency Spikes and Lock Contention

daily-tech:2026-12-03 現象: 延續上一篇我們用 XDP 把異常網路封包洪水擋在核心大門之外的思路,許多系統工程師在真實線上環境常遇到另一種更棘手的畫面。假設你的 Linux 主機網路流量完全正常, top 顯示整體 CPU 使用率甚至不到一半,記憶體也沒有逼近 OOM 紅線,但 API 尾端延遲(例如 P99)或是端側推論節點的請求回應時間,卻每隔幾十秒就無預警飆出數十毫秒的尖峰。既然不是單一程式吃滿 CPU,問題究竟藏在核心排程等待、中斷延遲,還是多核心爭搶同一把核心鎖? Symptom: Continuing from our previous post on shedding packet floods at the door with XDP, systems engineers often face a sneakier failure mode in production. Suppose your Linux host has clean network traffic, top shows total CPU utilization well under half, and memory is nowhere near OOM, yet tail latency (such as P99) or edge inference response time spikes by tens of milliseconds every few dozen seconds. When no single process is pegging CPU, is the stall hiding in scheduler wakeup latency, interrupt delays, or multi-core contention over a kernel lock? 在這種「平均值看起來很健康、尖峰卻很致命」的情境下,很多新手一緊張就直接把所有追蹤工具全開,結果不是因為採樣頻率太低而漏掉短短幾毫秒的尖峰,就是因為開啟全核心函式追蹤,反而把主機效能拖得更慢。要精準定位核心延遲,關鍵在於先看懂 Linux 核心內建的兩套主力觀測引擎—— ftrace (事件與函式追蹤框架) 與 perf (硬體效能計數器與統計採樣框架...

從 eBPF 看 Linux 核心網路封包路徑:XDP 零拷貝過濾與安全防禦實戰 / Linux Packet Path via eBPF & XDP Zero-Copy Filtering

daily-tech:2026-12-02 現象: 延續上一篇我們討論 Linux OOM Killer 與記憶體壓力時看到的系統雪崩問題,其實在網路邊緣閘道或高併發 API 主機上,還有一種非常常見的「還沒進應用程式就先卡死」情境。假設你的 Linux 伺服器突然遭遇大量異常 UDP 或 TCP SYN 封包洪水,你立刻在 nftables 或 iptables 的 PREROUTING 鏈加上丟棄規則,卻發現 top 畫面裡的軟中斷(SoftIRQ, %si )與 ksoftirqd 核心執行緒依然飆高,正常的 SSH 連線和 API 請求開始出現明顯延遲。明明防火牆已經把惡意封包全數 DROP 了,為什麼 CPU 和核心記憶體仍然喘不過氣? Symptom: Continuing from our previous breakdown of Linux OOM Killer and memory pressure, edge gateways and high-concurrency API servers often hit another frustrating wall where the machine stalls before user-space code even runs. Suppose your Linux host is hit by a flood of malformed UDP or TCP SYN packets: you add a DROP rule in the nftables or iptables PREROUTING chain, yet top still shows SoftIRQ ( %si ) and ksoftirqd spiking while legitimate SSH and API traffic slows to a crawl. If the firewall is already dropping those packets, why are CPU cycles and kernel memory still getting crushed? 答案不在防火牆規則寫得不夠早,而在於 Linux 傳統網路協定堆疊「太早把封包包裝成重型物件」 。只要一...