1. BackgroundAs the number of company applications increases, it is necessary to centrally collect some crash data and logs of some of the company's online applications for analysis and processing. During this practice, it is learned that the system data/system/dropbox directory will generate relevant log files for all applications. This directory is managed by DropBoxManagerService, one of the Android system services, so I read the source code related to DropBoxManagerService in detail, hereinafter referred to as DBMS. DBMS may be one of the Android system services with less source code, so it is relatively simple to read. After reading it, I found that it is actually a simple log file management service. When we record and manage some local log files of the application, we can refer to the design scheme of file management in the DBMS source code. Assuming we don’t read the source code, what should we consider if we design a log file management system ourselves? In addition to the most basic solution for obtaining various log files, we can propose several points to consider for file management:
Let's take a look at DBMS with the above questions. 2. Introduction to DropBoxManagerServiceDropBoxManagerService is one of the services of the Android system and adopts a C/S structure:
The overall architecture relationship is shown in the following figure: 2.1 Introduction to DropBox DirectoryThe directory structure of this directory is shown in the figure below: It contains some system log files. The file names and suffixes are different for different types of files. 2.1.1 File Format
The advantage of this file naming method is that you can tell at a glance what type of file it is. 2.1.2 Common files
It also includes some other system error logs, memory, restart related, etc. 2.2 Provided Interfaces2.2.1 Add files addData/addFile/addEntry 2.2.2 Get File getNextEntry, get the desired file based on tag and timestamp. 2.2.3 Dump directory information Get some information about the DropBox directory: number of files, file list, file details, etc. You can use the command line operation (dumpsys dropbox). $ dumpsys dropbox 2.2.4 Other CMD commands Provides some other CMD operation commands, such as set-rate-limit, add-low-priority, etc. 2.3 Directory Control Configuration2.3.1 Default basic configuration and file clearing strategy These configurations are stored in the system's setting database and can be accessed through settings.global. The configuration of file storage mainly includes the following dimensions:
Based on the above configuration, we can know the log file clearing policy under this directory, and the files will be deleted in time after the configuration limit is triggered. The file cleanup policy will be executed in the following three situations to prevent DropBox from taking up too much space:
At the same time, when adding files, if they exceed the configured available space, they will be discarded. /** 2.3.2 File deletion and marking strategy When the above strategy is not met, some files will be deleted. After deletion, an empty file named .lost will be added to DropBox to mark the deleted files. 2.3.3 File type control DropBoxMangerService also has control over the types of files that can be stored, mainly control over TAGs. public boolean isTagEnabled ( String tag ) { } 2.3.4 Permission Control Using DropBox requires the READ_LOGS permission and the PACKAGE_USAGE_STATS permission. 2.4 Read and Write StrategiesThis part involves several key methods and properties of DBMS, mainly initialization (init), adding files (addEntry), getting files (getNextEntry), and file types (EntryFile). The DBMS is started by SystemServer as a system service. Adding files (addEntry) and getting files (getNextEntry) will be initialized (init) when called. Each file will be converted into an EntryFile class for management. The relationship is shown in the figure below: Let's take a look at the details of initialization, EntryFile, adding files, and getting files: 2.4.1 Initialization Initialization will cache the DropBox file list into memory. /** If never run before, scans disk contents to build in-memory tracking data. */ Initialization time:
2.4.2 EntryFile file attributes Each file corresponds to an EntryFile, and the size is counted by the number of blocks. The reading and writing involved in the DBMS are all performed based on the blockSize of the disk, which will be more efficient. static final class EntryFile implements Comparable < EntryFile > { 2.4.3 Adding Files Add a log file, common in the addErrorToDropBox method call in Ams. Adding a file control policy ① The .lost file format is not allowed to be added. // If you add a .lost file, throw an exception ② TAGs that are not allowed to be recorded will not be added. // Read from the settings whether this tag is allowed to be recorded ③ Write according to the disk block size set by the system to improve writing efficiency. int bufferSize = mBlockSize ; ④ Correction of abnormal timestamp files: Before writing files, files with modification times exceeding 10s from the current time will be renamed and added to the cache file list. // Find all files after 10 seconds of the current time ⑤ The order of adding files is to create a temporary file first, and then use the file's rename method. The rename method is an atomic operation to ensure the safety of concurrent operations. // Save the file through the rename method to ensure the safety of concurrent operations ⑥ After the file is added, a broadcast notification is sent. The broadcast is divided into real-time broadcast and delayed broadcast. Delayed broadcast is used to notify files with lower priority. // Low priority can send delayed broadcast 2.4.4 Get File The logic of DBMS to obtain files is relatively simple. We can understand the meaning of the method name getNextEntry(String tag, long millis,...). It mainly finds the first file after the timestamp passed in by the user. for ( EntryFile entry : list .contents .tailSet ( new EntryFile ( millis + 1 ) ) ) { 2.5 Source code reading summary2.5.1 Answer the questions we asked before reading ① Log access strategy
② What fool-proofing strategies should be designed?
③ What interfaces are provided to the outside world?
④ How to ensure performance
// TODO : This implementation currently uses one file per entry , which is
⑤ How to solve the problem of multiple processes
⑥ How to deal with file loss
⑦ How to notify users of file changes
2.5.2 Other points
2.5.3 User’s Perspective Of course, in the process of using the source code, I also found some points that I personally think can be optimized.
3. Source code reading application – log file management & reporting design3.1 Overviewbackground: Some applications want to report some logs of the application runtime, including runtime log, crash log, Hprof memory snapshot, captured exceptions, etc. need: Need to design a set of client log file collection, management and reporting functions refer to:
3.2 SolutionThe overall solution adopts the producer-consumer model, with several key nodes
The overall flow chart is as follows: 3.3 Determine the external interface
3.4 Determine the collection control strategy
3.5 Determine the reporting control strategy
3.6 Log Collection Solution
The above specific plans are not the focus of this article and will not be described in detail. 3.7 Log Writing SolutionThrough the study of online courses, I learned that mmap has very high performance, so I finally adopted the "multi-process write + mmap" solution, and avoided the accumulation of cross-process calls, which is very efficient. 3.8 Log reporting solutionRefer to the real-time and delayed notification schemes for adding files to DBMS. Reporting is also divided into real-time reporting and delayed reporting.
3.9 Data Monitoring3.9.1 Quality Control 3.9.2 Disaster Recovery Monitoring IV. ConclusionThis article mainly covers two topics: 1. Read and analyze the DropBoxManagerService source code, including interface design, file storage management and control mechanisms and strategies, multi-process processing, and abnormal error prevention mechanisms 2. Application log collection and reporting scheme, mainly referring to the design of DropBoxManagerService source code We often emphasize source code reading. What can source code bring us? I think there are mainly the following points:
This article is just a starting point. With the help of the above cases, I briefly talked about the application of DBMS source code and source code reading. I hope it can bring some inspiration to everyone in source code reading, and at the same time give you an understanding of some uncommon services of the Android system. |
<<: Android page rendering efficiency optimization practice
>>: The practice of cloud rendering in urban IoT projects
What are the advantages of mobile high bandwidth ...
Just after the 2015 Spring Festival, rumors began...
[[123745]] Old iPod users may remember that Apple...
As the Internet becomes more and more developed, ...
From the "unavailable" Redmi to the pop...
As an APP operator, you often need to plan activi...
A few days ago, I met a friend who worked on home...
[[131851]] Beijing time, April 15th morning news,...
The financial report recently released by Hunan K...
Traffic is so expensive and conversion is so low!...
Learning English cannot be blind. Without systemat...
I wonder if you have noticed such a man in your s...
You may have heard that nitrite can be used to pr...
Brushing teeth and checking Weibo are the first t...
Have you heard the story of A Chinese Ghost Story...