Design and Implementation of a High‑Performance Mobile Logging System Using mmap
This article describes the design, mmap‑based implementation, architecture, and performance evaluation of a lightweight, low‑overhead mobile logging SDK for Android/iOS, highlighting its compression, encryption, policy‑driven upload, and significant CPU and GC improvements over traditional Java log libraries.
The article introduces the need for a robust mobile logging system that can capture complete logs with minimal performance impact, especially when issues are hard to reproduce in production environments.
It proposes a high‑performance logging SDK that leverages the Linux mmap mechanism, similar to XLOG, MMKV, and Logan, to achieve efficient I/O, low overhead, and cross‑process sharing.
What is mmap? mmap maps a file or device into a process's virtual address space, allowing direct memory reads/writes without additional system calls, reducing data copies between kernel and user space and enabling fast large‑scale data transfer.
The article lists mmap advantages: fewer data copies, efficient user‑kernel interaction, shared memory for inter‑process communication, and reduced disk I/O for repeated reads.
mmap usage in native code is illustrated with the function declaration and error codes, followed by a concrete SDK snippet that creates a cache file, opens it, maps it with mmap , and performs read/write operations:
void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset); // Buffer for file writes
static unsigned char* _buffer = NULL;
static int mmap_cache_file = 100*1024;
void init() {
makedir_mmapfile(cache_path);
int fd = open(cache_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd != -1) {
// mmap the file into memory
_buffer = (unsigned char*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
}
close(fd);
}
void write(...) {
data_zlib_compress();
fwrite(_buffer, sizeof(char), _buffer.length, dest_file);
fflush(dest_file);
update();
}The SDK architecture is divided into three layers: a native layer handling mmap‑based buffering, a compression/encryption layer using stream‑based AES, and a policy‑driven upload layer that fetches server strategies to control log size, retention, and level.
Performance tests on a Xiaomi Note3 (Android 9) compare the native mmap‑based SDK with a Java logging library, measuring write speed, GC frequency, and CPU usage. Results show the native SDK reduces GC frequency from ~1 s per GC to ~7.5 s and lowers CPU usage from ~13 % to ~5 % during intensive logging.
Further comparisons with other mmap‑based libraries (Logan, XLog) demonstrate consistent advantages in memory and CPU consumption. A practical case study from JD’s self‑service checkout devices illustrates how the SDK enables operations teams to retrieve logs independently, accelerating issue resolution.
Future work includes extending support to iOS, React Native, Flutter, mini‑programs, and H5, as well as adding proactive log upload channels to improve timeliness.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.