Value of data

APM

The target

Online monitoring and early warning for application performance and service reliability

Collection content

System indicators, application performance indicators,Crash, custom logs, etc

User behavior

The target

Fine operation

Collection content

From user attributes — gender, region, income, family status

From the user lifecycle — register, active, churn

From user behavior — features, content, product preferences, etc

Technical means




Java layer implements functionality

1. Customize service data links

2. Memory specifications

3. The CPU

4. The FPS

5. ANR log

6. Caton detection

7. The GC log

8. Crash logs

9.Http metric data (OkHttp only supported for now)

10. Electric quantity index

11.MAOP, using annotations and configuration files AOP specify method before execution, after execution, exception to insert the specified code function (the project also implements dynamic logging function, but does not seem to have a good use scenario)

12.Remote delivers commands, executes shell and dynamically executes code functions

13. Interaction analysis: Analyze the Activity lifecycle time

An overview of the

Because there are too many knowledge points involved, limited by space, this paper only lists the key knowledge points and steps, and the specific knowledge points will list the reference links or books, I believe that these authors are very detailed and transparent.

Android AOP

In the Android compilation process, the use of custom Gradle plug-in, register the new Tranform task, compile after Java compile, modify the content of the compiled class file, in the pointcut to increase the corresponding code to achieve AOP, to achieve the function of no buried point.

The Android compilation process





Gradle

Transform API

Android Gradle tools since version 1.5.0 provide a Transfrom API that allows third-party plugins to manipulate.class files during compilation prior to packaging dex files. At present, jarMerge, ProGuard, Multi-dex and instant-Run have all been changed into Transform implementation. We registered our Transform implementation to modify the class file

Detailed explanation of Transform

Jarinput.getname () is used to check if jar packages start with “:xx”. These jar packages need to be accessed as directory code.




Customize Gradle plug-ins

The Gradle Plugin uses the Groovy language and requires some understanding of Groovy, especially its closure concepts and ellipsis rules.

In-depth understanding of Android Gradle

Detailed explanation of specific operation

Debug gradle plug-ins

1. Create a new Configuration and Run->Edit Configurations->Remote




2. The terminal invokes the command

./gradlew clean

./gradlew assembleDebug -Dorg.gradle.daemon=false -Dorg.gradle.debug=true

3. In App bar, select gradleDebug and click debug button. AS will stop at the breakpoint and wait for debugging




Class file format

The overall format of the Class file




The method property of Class




Method’s code property




Class file format analysis

In-depth understanding of the Java Virtual Machine (Version 2) Extract code: TMNH

Bytecode instruction

Java AOP

Common bytecode generation tools are: ASM, AspectJ,Javassit, Javapoet,Spring,CGLib, etc. Spring and CGLib are not available in Android due to their mechanism of action. ASM has good analysis capability for instruction flow, which is very suitable for our scenario. ASM was chosen, and the other tools have their own usage scenarios.

ASM

ASM is a Java bytecode manipulation framework. It can be used to dynamically generate classes or enhance the functionality of existing classes. ASM can either generate binary class files directly or dynamically change the behavior of classes before they are loaded into the Java virtual machine. Java classes are stored in rigorously formatted.class files that have enough metadata to parse all the elements of the class: class names, methods, attributes, and Java bytecodes (instructions). After reading information from a class file,ASM can change the behavior of the class, analyze the class information, and even generate new classes based on user requirements. ASM uses a “Visitor” pattern to scan the contents of the. Class class file from beginning to end.

ASM,

ASM4 Usage guide

Efficient tools developed by ASM

Decompiler

A bytecode decompression tool, which comes with AS, to check that the generated code meets the class file format

Classpy

Graphical class file analysis tools, functions and JavAP similar interface reference Java Class Viewer

Making the address




ASM Bytecode Outline

It has a Bytecode view that can see how classes for the currently active source view should be generated using ASM. That is, if you know the bytecode of the modified class, you can use this view to see how the modified class and asm call statements should be written. It is particularly useful and recommended when studying ASM.

IDEA Plugin




Dx command

It is highly recommended that you use this command when you are done with ASM and dx. It will tell you what is wrong with the specific instruction, which is very efficient.

Dx –debug –dex — output= ~ /Desktop/out.dex ~ /Desktop/dex

The log

Data link

1. Define the ID and parentId attributes to establish the event relationship

2. Events executed in the same thread have links

3. Events of different threads need to be passed ParentId

MAOP

Provide simple using a configuration file or use annotations to specify which kind of method which need before method execution, execution, after performing exception to intercept, realize the logging code and the separation of business code, if the business matching rules flexible and rich enough, can also choose to match more AspectJ syntax and scene, also need a certain learning costs.

Annotation way




Configuration file Mode




Insert pile code







Interaction analysis

General interaction analysis requirements, general business methods to insert code or monitor the callback interface provided by the system can be achieved, Mas achieved: App start, App end, page browsing, OnClick several basic event monitoring.

Click on the event

Using Aop in the View. An OnClickListener. OnClick insert code implementation, the implementation View the implementation of the project under the plugin MasAnalyticsClassVisitor

App starts, App ends, page browsing

Activity page views can be covered by the Application. The ActivityLifecycleCallbacks, specific view MasDataActivityLifecycleCallbacks in a project

Crash

Through the Thread. SetDefaultUncaughtExceptionHandler (this)

Memory monitoring

Implemented a curve that monitors memory usage as events change

Check the number of FD and Thread during the OOM event

Activity leak detection

Low memory status detection

Detailed analysis

Cpu

Read /proc/stat and /proc/pid/stat files to obtain CPU data for calculation. For specific calculation methods, refer to CpuSnapshot and CpuMonitor classes in the project

The GC log

The Runtime can capture the logCAT logs printed by its own process. There is a better way to capture the GC log. Dalvik and ART can find the source code of dalvik and art. Gc printing will call the logCAT interface of the system. Is it a better implementation if we can hook method invocation in the native layer? Later, we will continue to study the technical implementation of native inline and PLT hook

electricity

Through the registration system recevier, you can view BatteryMonitor in detail, but you can only know the changes of the system battery, which is not a good description of the application battery consumption. How to describe the application battery consumption needs to be implemented later. Google later found that after Android4.4, After the system takes away the power permission, I refer to the implementation of other projects, which seems to imitate the formula for calculating power of the entire Android system. I still need to learn how to calculate power of the system.

ANR log

Use FileObserver to listen for /data/anr/ tetrace. TXT read/write events, and parse anR logs that belong to the current package name according to the fixed format of ANR logs. Anr data is usually the last data, but the latest ANR of some Huawei models is written at the end. You need to collect the latest ANR to view ANR logs. In practice, the FileObserver can only be notified at the first time an Anr has occurred, but not at the second time.




Caton detection

The print mechanism of Looper is used to calculate the execution time of the current MainThread operation to determine whether it is stuck. The implementation can be achieved through watchdog, but it is difficult to obtain the correct stack data. The accuracy can be increased by obtaining the stack of MainThread for several times

FPS

Through the Choreographer. GetInstance (). PostFrameCallback (frameCallback), statistical frames set callbacks, but is not very accurate, specific see Fps class, under only process the permissions have not found a better way, This can be done with gxInfo for higher privileges.

Http Indicator Data




For now, only OkHttp3 is supported, but other HttpClient implementations work in much the same way. The current implementation of the project HTTP request time, anomaly, data size, status code acquisition, directly use the previous implementation of MAOP, intercept OkHttpClient.Builder build method to add statistics Interceptor,DNSLookUp time, connection time, SSL time, By setting eventListener. Factory after OkhttpClient 3.10, you can directly collect the first packet time by intercepting the OkHttp read request data. OKHttpClient finally calls the CallServerInterceptor, and the key code is when to read readResponseHeaders.




implementation

Use the MAOP functionality provided earlier to add, Intercept OkHttpClient builder method and Http1Codec readHeaderLine method and okhttp3 internal. Http2. Http2Stream takeResponseHeaders method of configuration







Add the statistics Interceptor and EventListenerFactory to the build() method of the Builder that intercepts OkHttpClient




First packet time through: It is considered that the header time when the first read response header is returned, Intercept okhttp3. Internal. Http1. Http1Code. The method and okhttp3 readHeaderLine. Internal. Http2. Http2Stream. TakeResponseHeaders computed first time




Remote execution

Remoting has proven to be a very useful feature in daily development and maintenance practices, providing a way to develop direct contact with users, changing the situation when things go wrong and developers communicate inefficiently with users.

However, the current project has not realized the function, ~ ~ ~ ~ ~. The existing idea is to send commands through the push channel, and the client parses and executes them. The client uses Lua to have dynamic execution capability and executes shell commands through the Runtime. Available Lua library has been connected to the project, but shell execution. During the development process, IT was found that the Android Runtime.exc shell command with blank space could not be executed as correctly as the direct shell command, which needs to be solved.

subsequent

Current project basic capability of the collection, but the index data and problem solving natural spans a huge gap between, these also need to find a solution to a problem in the process of practice requires all of the data, need to know more about the Android provides the principle, operating environment and to achieve a perfect quality guarantee system of mature it is a long way.

The project address