Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Android Shared Memory Log Daemon

License Platform

高性能 Android 日志采集守护进程,使用共享内存实现零拷贝低延迟的跨进程日志传输。

特性

  • 🚀 零拷贝传输: mmap 共享内存,数据传输零系统调用
  • 低延迟: 日志写入延迟 < 1μs,端到端延迟 < 5ms
  • 🔄 无锁设计: 原子操作环形缓冲区,无竞争
  • 📊 高吞吐: > 100,000 条/秒处理能力
  • 🛡️ 安全可靠: 完整的 SELinux 策略,支持 Android 15
  • 📦 易于集成: 提供消费者 SDK,一行代码接入

架构

┌────────────────────────────────────────────────────────────┐
│                   共享内存 (ashmem)                         │
│  ┌──────────────────────────────────────────────────────┐  │
│  │           无锁环形缓冲区 (Lock-free Ring Buffer)     │  │
│  │                                                       │  │
│  │   [Entry 0] [Entry 1] [Entry 2] ... [Entry N-1]      │  │
│  │                                                       │  │
│  │   head ──────────────────────────► 写指针 (原子)     │  │
│  │   tail ──────────────────────────► 读指针 (原子)     │  │
│  └──────────────────────────────────────────────────────┘  │
│         ▲ mmap 写入                    │ mmap 读取         │
└─────────┼──────────────────────────────┼───────────────────┘
          │                              │
   ┌──────┴───────┐               ┌──────┴───────┐
   │ logcat_daemon│               │ 消费者进程   │
   │ (生产者)     │               │              │
   │              │    Unix       │ liblog_      │
   │ 读取 logd    │────Socket────►│ consumer.so  │
   │ 写入 shm     │    FD传递     │ 零拷贝读取   │
   └──────────────┘               └──────────────┘
          │
          │ recv()
          ▼
   ┌──────────────┐
   │   logd       │
   │ (系统进程)   │
   └──────────────┘

快速开始

编译

# 1. 放入 AOSP 源码树
cp -r logcat_daemon/ aosp/system/core/

# 2. 编译
source build/envsetup.sh
lunch <target>
mma logcat_daemon liblog_consumer log_consumer_example

部署

# 推送到设备
adb root && adb remount
adb push out/target/product/<device>/system/bin/logcat_daemon /system/bin/
adb push out/target/product/<device>/system/lib64/liblog_consumer.so /system/lib64/
adb shell chmod 755 /system/bin/logcat_daemon

# 启动服务
adb shell /system/bin/logcat_daemon

# 测试消费者
adb shell /system/bin/log_consumer_example

使用 SDK

#include <log_consumer.h>

int main() {
    logdaemon::LogConsumer consumer;
    
    // 连接到守护进程
    if (!consumer.connect("/dev/socket/system_logs_v1")) {
        return -1;
    }
    
    // 读取日志
    logdaemon::LogEntry entry;
    while (consumer.read(entry)) {
        std::cout << logdaemon::LogParser::format(entry) << std::endl;
    }
    
    return 0;
}

性能指标

指标 数值 说明
写入延迟 < 1 μs 单条日志写入共享内存
读取延迟 < 1 μs 单条日志从共享内存读取
端到端延迟 < 5 ms 日志产生到消费者读取
吞吐量 > 100,000 条/秒 单生产者单消费者场景
内存占用 ~4.5 MB 默认 4096 条缓冲区
CPU 占用 < 1% 100 条/秒负载

文档

目录结构

logcat_daemon/
├── Android.bp              # 编译配置
├── logcat_daemon.rc        # init 启动脚本
├── README.md               # 本文档
├── docs/                   # 文档目录
│   ├── ARCHITECTURE.md
│   ├── API.md
│   ├── DEPLOYMENT.md
│   ├── PERFORMANCE.md
│   └── FAQ.md
├── include/                # 头文件
│   ├── log_shared_buffer.h
│   ├── logcat_daemon.h
│   ├── log_consumer.h
│   └── log_utils.h
├── src/                    # 源文件
│   ├── logcat_daemon.cpp
│   ├── log_consumer.cpp
│   ├── log_utils.cpp
│   └── shared_buffer.cpp
├── selinux/                # SELinux 策略
│   ├── logcat_daemon.te
│   ├── file_contexts
│   └── service_contexts
└── test/                   # 测试代码
    ├── log_consumer_test.cpp
    └── log_benchmark.cpp

系统要求

  • Android 10+ (API 29+)
  • Android 15 需要额外 SELinux 配置
  • NDK r21+ 或 AOSP 编译环境

许可证

Apache License 2.0

贡献

欢迎提交 Issue 和 Pull Request。

致谢

本项目灵感来自 Android logd 和 Chrome tracing 系统。

About

Android high-performance log collection daemon with zero-copy shared memory transmission

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages