跳到主要內容

發表文章

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 傳統網路協定堆疊「太早把封包包裝成重型物件」 。只要一...

Linux 核心 OOM Killer 與 Android LMKd 協同機制深度拆解:從 oom_badness 評分算法到端側 LLM 記憶體防禦實戰 / Deconstructing Linux Kernel OOM Killer and Android LMKd Orchestration: From oom_badness Scoring Algorithm to Edge LLM Memory Defense in Practice

daily-tech:2026-11-30 Linux 核心 OOM Killer 與 Android LMKd 協同機制深度拆解:從 oom_badness 評分算法到端側 LLM 記憶體防禦實戰 / Deconstructing Linux Kernel OOM Killer and Android LMKd Orchestration: From oom_badness Scoring Algorithm to Edge LLM Memory Defense in Practice 現象: 在現代旗艦 Android 智慧型手機、智慧車載系統與邊緣運算(Edge AI)硬體上,部署 2B 至 7B 參數規模的端側大語言模型(On-Device LLM,如 Gemma 2、Llama 3 或 Qwen 2.5)已經成為可能。然而,許多軟體團隊在實機壓測時常遭遇令人費解的崩潰現象:當模型剛載入時運作正常,但在長對話或使用者輸入複雜多輪提示時,模型吐字進行到第 2,000 個 Token 左右,整個應用程式突然毫無預警閃退;嚴重時,甚至整個系統 UI 出現長達數秒的凍結(System Freezing),隨後桌面啟動器(Launcher)重新加載,終端日誌中僅留下怵目驚心的 Out of memory: Kill process or sacrifice child 訊息。為什麼明明設備具備 8GB 甚至 12GB 實體 LPDDR 記憶體,系統卻依然會慘遭「爆記憶體」?當實體記憶體耗盡時,Linux 核心底層的 OOM Killer 究竟是依據什麼規則決定要殺死哪一個行程?而 Android 的低記憶體守護行程(LMKd)又是如何與 Linux 核心協同作戰,避免系統在端側 AI 狂暴的記憶體消耗下直接崩潰? Symptom: On modern Android flagship smartphones, automotive cockpits, and edge computing platforms, deploying 2B to 7B parameter on-device Large Language Models (LLMs)—such as Gemma 2, Llama 3, or Qwen 2.5—has become feasible. Ho...

Linux DMA-BUF 跨晶片零拷貝架構深度拆解:從 GPU/NPU 緩衝共享到 Android 端側 AI 推論管線優化 / Deconstructing Linux DMA-BUF Cross-Device Zero-Copy Architecture: From GPU/NPU Buffer Sharing to Android Edge AI Inference Optimization

daily-tech:2026-11-29 Linux DMA-BUF 跨晶片零拷貝架構深度拆解:從 GPU/NPU 緩衝共享到 Android 端側 AI 推論管線優化 / Deconstructing Linux DMA-BUF Cross-Device Zero-Copy Architecture: From GPU/NPU Buffer Sharing to Android Edge AI Inference Optimization 現象: 在現代 Android 與邊緣運算(Edge Computing)裝置上,端側 AI 即時影像分析(如即時物件偵測、語意分割或姿態辨識)已成為核心應用場景。然而,當開發團隊將相機模組(Camera HAL / ISP)捕獲的高畫質 4K 60fps 影像送往神經網路處理器(NPU)或 GPU 執行深度學習推論時,系統往往面臨嚴重的效能瓶頸:即時影像預覽嚴重掉幀、推論延遲從預期的 15 毫秒暴增至 50 毫秒以上,同時設備迅速發燙並引發 CPU 溫控降頻(Thermal Throttling)。深入分析系統效能日誌時會發現,CPU 使用率居高不下,大量運算資源並非耗費在神經網路模型運算上,而是浪費在使用者空間與核心空間之間頻繁的記憶體資料搬移( memcpy )。為什麼多個硬體單元(相機感測器、GPU、NPU)明明掛接在同一個晶片系統(SoC)且共用實體 LPDDR 記憶體,系統卻無法直接讀寫同一塊緩衝區?Linux 核心的 DMA-BUF 機制究竟是如何打破硬體壁壘,實現極致的跨晶片零拷貝(Zero-Copy)資料傳輸? Symptom: In modern Android devices and edge computing platforms, real-time on-device computer vision—such as object detection, semantic segmentation, and human pose estimation—has become a foundational workload. However, when feeding 4K 60fps video frames captured by camera modules (Camera HAL / ISP) i...

Android Binder IPC 通訊機制深度拆解:從交易緩衝區限制到 Ashmem/零拷貝傳輸架構 / Deconstructing Android Binder IPC: From Transaction Buffer Limits to Ashmem and Zero-Copy Architecture

daily-tech:2026-11-28 Android Binder IPC 通訊機制深度拆解:從交易緩衝區限制到 Ashmem/零拷貝傳輸架構 / Deconstructing Android Binder IPC: From Transaction Buffer Limits to Ashmem and Zero-Copy Architecture 現象: 在 Android 應用與系統服務開發中,跨行程通訊(IPC, Inter-Process Communication)是底層運作的骨幹。無論是 Activity 啟動、呼叫系統 LocationManager、或是跨行程 AIDL 服務傳遞資料,背後全部依賴 Binder 機制。然而,許多開發者在透過 Intent 或 AIDL 傳遞高解析度點陣圖(Bitmap)、大型清單資料、甚至神經網絡模型輸入張量時,常遭遇突如其來的崩潰:應用程式噴出 android.os.TransactionTooLargeException 或直接拋出底層死鎖。即使傳遞的資料看似只有數百 KB,在多執行緒併發傳輸時依然會瞬間觸發交易失敗。為什麼聲稱高效能的 Binder IPC 存在嚴格的大小限制?當面對多媒體串流或端側 AI 大型特徵圖時,系統架構又是如何突破這層限制的? Symptom: In Android app and system service engineering, Inter-Process Communication (IPC) serves as the fundamental backbone. Whether launching an Activity, querying LocationManager, or invoking cross-process AIDL services, all interactions rely on the Binder subsystem. However, developers frequently encounter sudden crashes when passing high-resolution Bitmaps, extensive lists, or neural network input tensors via Intents or AI...