跳到主要內容

發表文章

目前顯示的是有「Kernel」標籤的文章

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...

從 EEVDF 看 Linux 核心對端側 AI 延遲敏感度的排程配額優化 / Optimizing Edge AI Latency via Linux EEVDF: Scheduling Quotas and Virtual Deadlines

daily-tech:2026-11-26 從 EEVDF 看 Linux 核心對端側 AI 延遲敏感度的排程配額優化 / Optimizing Edge AI Latency via Linux EEVDF: Scheduling Quotas and Virtual Deadlines 現象: 在 Linux 與 Android 邊緣裝置上部屬端側 AI(例如即時語音辨識、手勢追蹤或端側小型語言模型 SLM Token 生成)時,許多開發團隊在處理高負載背景任務時,常發現 AI 推論會出現不可預期的延遲抖動(Latency Jitter)。即便系統已經透過前篇討論的 DMA-BUF 零拷貝技術消除了大部分的記憶體搬移開銷,但當 AI 執行緒被硬體中斷(如相機新圖框就緒、麥克風音訊緩衝區填滿)喚醒時,往往無法第一時間搶佔 CPU 核心,而是在就緒佇列(Runqueue)中痛苦排隊 5 到 15 毫秒。這類延遲使得即時互動系統的影音同步失效,嚴重傷害端側 AI 的流暢體驗。 Symptom: When deploying on-device AI workloads (such as real-time keyword spotting, gesture tracking, or edge small language model SLM token streaming) on Linux and Android devices under concurrent system loads, developers frequently encounter severe latency jitter. Even after eliminating redundant memory copies using the DMA-BUF zero-copy pipeline analyzed in our previous article, AI threads awakened by hardware interrupts (e.g., camera frame completions or audio buffer fills) often fail to seize CPU cores immediately. Instead, they languish in run...

從 DMA-BUF 到 AHardwareBuffer:拆解 Android 與 Linux 端側 AI 零拷貝管線 / From DMA-BUF to AHardwareBuffer: Deconstructing Zero-Copy Pipelines in Android and Linux Edge AI

daily-tech:2026-11-25 從 DMA-BUF 到 AHardwareBuffer:拆解 Android 與 Linux 端側 AI 零拷貝管線 / From DMA-BUF to AHardwareBuffer: Deconstructing Zero-Copy Pipelines in Android and Linux Edge AI 現象: 在智慧型手機或邊緣嵌入式裝置上執行即時影像 AI(例如物件偵測、語意分割或姿態辨識)時,許多團隊常遇到一種難堪的效能瓶頸:明明選用了輕量化的量化神經網路,推論核心花費的時間僅需 15 毫秒,然而整體管線的端到端延遲卻高達 45 毫秒以上,甚至伴隨著 CPU 使用率飆高與手機發燙。深入分析效能剖析數據後會發現,推論本身很快,耗時全卡在「相機取像後把點陣圖複製給 GPU 前處理」、「GPU 處理完再拷貝成張量陣列交給 NPU」這兩次不必要的記憶體搬移上。 Symptom: When deploying real-time vision AI (such as object detection, semantic segmentation, or pose estimation) on smartphones or edge embedded devices, engineering teams often face an unexpected performance bottleneck: despite choosing an ultra-lightweight quantized neural network whose core inference takes only 15 milliseconds, the end-to-end pipeline latency exceeds 45 milliseconds, accompanied by soaring CPU utilization and device throttling. Profiling reveals that raw inference is swift; the real culprits are redundant memory copies between camera ISP buffers, GPU preproces...

Linux EEVDF 排程:為什麼低延遲不等於永遠優先 / Linux EEVDF Scheduling: Why Low Latency Does Not Mean Always First

Linux EEVDF 排程:為什麼低延遲不等於永遠優先 / Linux EEVDF Scheduling: Why Low Latency Does Not Mean Always First 現象: 同一台 Linux 主機上,終端機輸入偶爾卡頓,AI 推論或編譯工作卻沒有停止。很多人第一個反應是把互動程式設成「最高優先權」,但這可能只是把延遲轉移給別的工作。Linux 近年的公平排程路徑使用 EEVDF(Earliest Eligible Virtual Deadline First)整理可執行工作:它不是承諾某個程式永遠先跑,而是在公平性與回應時間之間計算下一個選擇。 Symptom: On the same Linux host, terminal input can feel sticky while an AI inference or build continues to run. The first reaction is often to give the interactive task the highest priority, but that may only move the latency elsewhere. Recent Linux fair-scheduling work uses EEVDF, or Earliest Eligible Virtual Deadline First, to order runnable tasks. It does not promise that one program always runs first; it computes a next choice between fairness and responsiveness. 圖一|EEVDF 先判斷工作是否 eligible,再比較虛擬截止時間。 / Figure 1 | EEVDF checks eligibility before comparing virtual deadlines. 機制:先理解 eligible,再談 deadline / Mechanism: Eligibility Comes Before the Deadline 公平排程器不只看「誰等最久」。每個工作會在虛擬執行時間軸上累積進度,並依...

eBPF 為什麼不能隨便讀記憶體?看懂 Linux Verifier 的安全檢查 / Why the Linux eBPF Verifier Refuses Unsafe Memory Access

daily-tech:2026-11-16 現象:一個指標,為什麼不能直接讀? / Symptom: Why can’t one pointer simply be read? 寫 eBPF 時,最容易踩到的坑不是語法,而是「我明明先算出位址,為什麼載入核心就被拒絕?」原因是 eBPF 程式會在核心內執行;Linux 不能等錯誤發生後才補救,因此載入前先由 verifier 檢查指標型別、數值範圍、對齊與控制流程。 When writing eBPF, the frustrating failure is often not syntax: “I calculated the address, so why did the kernel reject the program?” eBPF runs inside the kernel. Linux therefore checks pointer types, value ranges, alignment, and control flow before loading instead of repairing a fault afterward. 機制:verifier 追蹤的是狀態,不是猜測 / Mechanism: the verifier tracks states, not guesses 官方文件把暫存器分成 scalar 與多種 pointer。scalar 會保存最小、最大值與未知位元;指標則帶著來源與可存取範圍。以封包為例,先取出 data 與 data_end,再確認 data 加上標頭長度沒有越過 data_end,之後那個分支內的讀取才被視為安全。若中間把指標做成無法證明範圍的運算,狀態會退化成 scalar,後面的記憶體讀取就會失效。 The kernel documentation describes registers as scalars or several pointer types. A scalar carries bounds and unknown bits; a pointer carries its origin and an accessible range. For packet data, the program loads data and data_end,...

Linux io_uring 為什麼仍可能卡住?從固定 Buffer 到 Completion Queue 的 I/O 路徑 / Why Linux io_uring Can Still Stall: Fixed Buffers and Completion Queues

 現象:io_uring 非同步,為什麼服務仍會卡住? / Symptom: Why can an io_uring service still stall? 你把大量檔案讀取改成 Linux io_uring,CPU 使用率下降,卻仍看到請求延遲突然拉高。提交請求變得非同步,不代表儲存裝置立刻完成;queue depth、檔案系統與 block I/O 仍會把等待時間留在 completion 之前。 You moved file reads to Linux io_uring, CPU usage fell, yet request latency still spikes. Submission becomes asynchronous; storage, the filesystem, and block I/O can still delay completion. io_uring 的基本路徑是:應用程式準備 submission queue entry(SQE),核心取出並執行,最後把結果寫成 completion queue entry(CQE)。共享 ring 減少系統呼叫,但 CQE 的 res 才是實際結果;不能只看到 SQE 已提交,就把工作算成完成。 The path is: the application prepares an SQE, the kernel executes it, and a CQE reports the result. Shared rings reduce syscall traffic, but CQE res is the real outcome. Submission is not completion. 圖一|io_uring 的 SQE→核心執行→CQE 路徑;固定 buffer 的生命週期與完成延遲必須分開觀察。 / Figure 1 | The SQE-to-kernel-to-CQE path; buffer lifetime and completion latency are separate signals. Original conceptual diagram. 機制:固定 Buffer 解決什麼,又帶來什麼責任? / Mechanism: what registered bu...

Linux 為什麼還沒 OOM 就開始卡頓?用 PSI 讀懂 CPU、記憶體與 I/O 壓力 / Before OOM: Reading Linux Pressure Stall Information

現象:為什麼還沒 OOM 就開始卡? / Symptom: Why does Linux stall before OOM? 你是否遇過這種情況:記憶體還有剩、CPU 也沒有 100%,但 SSH 變慢、服務延遲突然拉高,甚至工作負載尚未被 OOM Killer 終止就已經「像死機」?這不是單看使用率就能回答的問題。Linux Pressure Stall Information(PSI)量的是工作真的因資源不足而等待了多久,讓「還能跑」與「已經在浪費時間」分開。 Have you seen a Linux host become sluggish while memory is still available and CPU is below 100%? PSI addresses the missing signal: not how full a resource looks, but how long runnable work is actually delayed by contention. A machine can be operational and already wasting latency before the OOM killer appears. PSI 的核心不是額外的效能計數器,而是把資源競爭轉成「停滯時間」。核心分別在 /proc/pressure/cpu、memory、io 匯出資料;some 表示至少一部分工作正在等待,full 則表示所有非閒置工作同時卡住。memory full 或 io full 長時間上升,比單獨看到 free 變少更接近「系統正在失去生產力」的現象。 PSI turns contention into stall time. The kernel exports cpu, memory, and io pressure files under /proc/pressure. “some” means at least some work is stalled; “full” means all non-idle work is stalled at once. A sustained memory-full or io-full signal is often more actionable tha...