This is the sixth day of my participation in the First Challenge 2022.

Caton optimization idea

  • Analysis of Caton’s principle
  • Caton flow
  • Caton profile analysis
  • Caton actual data collection
  • Caton optimizes the details

Caton reason

The screen refresh rate is higher than the frame rate and the frame rate is lower than 30

Execute the flow per frame

Four queue Callbacks are maintained within Choreographer

  • Input event queue
  • The animation queue
  • Draw the queue
  • FrameCallback queue added by app

The VYSNC signal is triggered by the creation of HWC in SurfaceFlinger and sent to the target process via bitTube technology. When the target process vsync signal arrives, it executes the onVsync callback in Choreographer and finally triggers doFrame to execute the messages in these four queues in sequence.

bitTube

In Linux/Unix, bitTube technology is called socketPair. It copies the handle of a socket through DUP technology and passes it to the target process, enabling full-duplex communication of the socket.

handle

In the kernel, each process has a private “open file table,” which is an array of Pointers, each element pointing to a kernel open file object. And fd is the subscript of this table. When the user opens a file, the kernel internally generates an open file object, finds an empty entry in the table, directs this entry to the generated open file object, and returns the subscript of this entry as fd.

The UI optimization

  • Excess Bg removal
  • UI overlap region optimization cancas. ClipRect
  • Reducing UI levels
  • Time consuming method analysis and optimization
  • Single RV treatment for various layouts

The webview optimization

Webview loading process

Webiew initialization

  • The purpose is to initialize and start the browser kernel.
  • Pre-initialize webView and hide optimization 126ms

Webview separate process

  • Single process activity configuration
  • Separate process the interaction of the webview. AddJavascriptInterface (), webview. Evalute ()

security

  • AddJavaScriptInterface Adds the @addjavascriptInterface annotation to the method of the Java object to avoid XSS attacks

Caton collection strategy

Develop card to detect StrictMode

private void initStrictMode() { if (isDebug()) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .DetectCustomSlowCalls () //API level 11, Use strictmode.noteslowcode.detectDiskreads ().detectDiskwrites ().detectnetwork () // or.detectall () for just detectable levels PenaltyLog (); penaltyFlashScreen(); API level 11.build (); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() //API level 11. penaltyLog().penaltydeath ().build()); }}Copy the code

Offline stuck detection

  • adb shell dumpsys gfxinfo [packagename]
Applications Graphics Acceleration Info: Uptime: 205237819 Realtime: 436545102 ** Graphics info for pid 5842 [xxxx] ** Stats since: 198741999784549ns Total frames rendered: 50th Percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile: 50th percentile 200ms Number Missed Vsync: 46 Number High input latency: 268 Number Slow UI thread: 76 Number Slow bitmap uploads: 3 Number Slow issue draw commands: 8 Number Frame deadline missed: 92Copy the code
  • Draw bar column analysis by GPU

The bars come in eight colors, with green and blue being optimized for asynchronous applications. Includes other processing – input – animation – travel

BlockCanary detects stagnation

In the Looper loop of ActivityThread.main, looper. loopOnce keeps sending messages out of the message queue and prints two logs before and after logging. Compare the time difference between the two logs with 30ms. If the time difference exceeds 30ms, the logs are considered to be stuck.

logging.println(">>>>> Dispatching to " + msg.target + " "
                    + msg.callback + ": " + msg.what);
msg.target.dispatchMessage(msg);
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
Copy the code

Caton analyzes information collection

  • Debug.startmethodtracing Collects specific stuck methods
  • View the trace file and analyze the time-consuming method based on bottomup
  • Flame chart, horizontal axis is call method time, vertical axis is call depth
  • Call diagram, call chain, and method time

Online caton analysis and collection

In the Looper loop of ActivityThread.main, looper. loopOnce keeps sending messages out of the message queue and prints two logs before and after logging. Compare the time difference between the two logs with 30ms. If the time difference exceeds 30ms, the logs are considered to be stuck. The main thread stack information is written to the cache file and asynchronously sent to the logging background.