### states that everything this article says is wrong.
### Level of user demand for mobile phone
- Full-featured: The beginning of the industry
- Stable power consumption: the development period of the industry
- Smooth performance: the industry is mature
- Security: Privacy issues will become more important
The smartphone industry started with the release of the first generation of Iphone by Apple, and rode the wind of the mobile Internet to develop rapidly. Google, meanwhile, acquired the Android project and led the development itself. To Android 2.3, Domestic Lei Jun led Xiaomi from making MIUI ROM to mobile phone hardware, and released the first Internet marketing mobile phone Mi 1 generation. Mi 1 became the most versatile smart phone in China at that time with the help of MIUI, which had been deeply developed for a long time. However, as an Internet company transforming into a hardware company, there is no technical accumulation of huawei, OPPO, Vivo and other hardware companies. Understanding of the stability of the software is not enough, Mi 1 all kinds of crash restart.
With the development of the industry, the competition with iPhone has prompted Android manufacturers to compete in the system faster CPU frequency, more memory, more flash ROM storage space; In terms of external design, the company has developed into larger screen size, higher screen ratio, faster refresh frequency, fingerprint back fingerprint unlock, home button unlock, and under-screen fingerprint unlock. As a hardware company, Apple extended MAC OS to iOS system with various tuning and deep integration, so it took several years for Android manufacturers to slowly reach the same level with iOS system. Android phone makers also pay attention to system stability.
The stability of
Stability includes OS system stability and tripartite APP compatibility. Google releases major versions of Android every year, and its own system is full of bugs. Mobile phone manufacturers will also introduce various bugs in OS function development. At the same time, due to the late adaptation of third-party App to the new version, the compatibility problem with the new system is also quite serious. Especially the introduction of all kinds of live black technology and dynamic update update hot repair. In some multi-process survival schemes, app code defects lead to the creation of new processes all the time, and even the maximum number of processes that Linux kernel can use 32768 have been used up, resulting in the system can not create processes and crash, such as cheetah browser. The hot repair schemes are the mainstream of Ali VIRTUAL machine DALVK, ART virtual machine layer Hook scheme and Tencent Tinker scheme. Ali’s style is more radical, but certainly less stable. Tencent’s products are more about stability, but less about flexibility.
Mobile phone manufacturers have also designed a variety of monkey tests, aging, pressure testing and other automation, all kinds of applications in the top 1000, top 2000, top 5000 and other compatibility automation tests. Huawei and other companies have developed their own on-cloud systems to stress test the stability of apps on Huawei phones. Every time a major Version of Android is updated, reliable phone manufacturers can find tens of thousands of bugs. There are even engineers who specialize in bug-fixes.
So many problems, how to deal with it? Relying on human analysis is not enough for human input, there are various monitoring, problem classification, automatic bill of lading, analysis and other technologies. To speed up the problem repair speed.
After so many years of development, the stability of mobile phones has gradually matured. An Android phone will not encounter abnormal crash or restart for several months.
life
But Battery life on Android phones has been poor. Various handset makers are also looking for breakthroughs, such as Huawei funding research into new graphene materials. But the success of this revolutionary technology could take years of research. It’s a lab toy, after all. A technical failure can be scientifically demonstrated in the laboratory. But to become a commercial product, it needs to be engineered, scaled up and mass-produced. The flexible screen technology of Royole is faced with the problem of high cost due to low yield.
There are two ways to extend your phone’s battery life: slower power consumption and faster charging.
Faster charging
Around 2013 and 2014, a small manufacturer of charger completed a design of high-current quick charging, and was interested in finding various mobile phone manufacturers due to safety problems such as product stability. Got a slap on the face. After an opPO engineer knew about this scheme, he liked it very much and studied it privately. By designing various dangerous situations, multi-mode switching and blocking, preventing high current, heating, explosion and so on, he successfully improved all kinds of problems and made it more stable and safer. In 2015, “Charging in five minutes, talking in two hours” was achieved. This quick charge technology made Oppo’s products taste sweet in the differentiated competition that year, and the quick charge r&d team awarded 1 million yuan. Research continued, and a faster flash charge was designed. Thinking of the flash charger designed by OPpo and the small manufacturer that approached them, the relevant leaders of ZTE Mobile (later renamed Nubia) regretted not digging deeper and missing the opportunity.
Battery capacity E= voltage U x current I x time t.
The battery capacity is relatively fixed. Mobile phones generally have 3000+mAh. Gionee directly uses two batteries to solve the problem of large capacity, which is an exception. IPhone battery appeared 2000+mAh, but they do a good combination of soft and hard, the battery life is better than Android system, of course, now the large screen, iPhone 11 seems to be about 3500mAh.
Oppo uses high current solution and patents, then Qualcomm, MTK, Huawei go high voltage solution. The whole thing used to be 5V x 4A, 20W, and now it’s 40 plus W. Zhihu Shanghai has been faced with the problem of high current safety or high voltage safety. After so many years of use, it seems that there has never been a battery explosion incident in domestic mobile phones. Of course, Samsung’s battery explosion incident in 2016 May have been a major event in the mobile phone industry caused by a relatively radical plan and insufficient attention to stability and safety.
Slower power consumption
Hardware improvements are no substitute for software improvements. Gold and silver mountains, not good housekeeping, will be defeated sooner or later.
Mobile phone power consumption includes three aspects:
- Based on power consumption
- Standby power consumption
- The power consumption of the scene
Based on power consumption
Mobile phone hardware engineers will consider the overall layout of each device (of course, Qualcomm, MTK will generally give a public board to do reference). Out of the whole machine, software engineers also need to find out the basic power consumption, to avoid design defects, leakage and other situations. In flight mode, the lowest brightness, the volume of mute and other conditions, use the Power Monitor to see how much the current of the sensor and device is concerned. For example, the standby current is about 2mA; For example, wifi standby power consumption is 0.25mA larger than wifi; Wifi search network is 2.5~4mA larger than not open wifi; For example, the lowest audio external sound current is less than 50mA; . If you find an anomaly, you need to analyze the cause. Is it electricity leakage? Or is the background running xx process?
Because the power calculation is actually an analog circuit estimate, can not be completely digital so accurate. On Android, the current of each sensor in different modes is calculated and recorded in power_profiler. XML file. Then, according to the running time of each sensor, the total estimated power consumption is stored in batteryStats file. There are inaccuracies in this scheme, such as frequent awakenings where the actual power consumption is much greater than the calculated power consumption.
Engineers who develop battery meter algorithms also use each algorithm to optimize the relationship between actual battery consumption and the percentage of battery displayed in the status bar, since the relationship is not linear. Some manufacturers will make a trick plan: more than 90% of the time when the percentage of electricity change slowly, but the actual power consumption is more than 10%, let the algorithm in the subsequent use of the percentage of electricity then slowly and gently corresponding. It also allows users to avoid the psychological discomfort of a drop in power when they have just been fully charged.
### Power consumption is caused by the operation of each sensor hardware, the calculation of each software process, CPU and GPU of the operating system. Standby power consumption for standby power consumption: is to keep the lowest power consumption when the phone is not in use and after the screen is off; When used, the background layer running program consumes the least power.
Due to the openness of Android and the inavailability of Google services in China, various apps have appeared various process survival schemes in order to keep the server able to discover app, notify APP and control app in time. The most annoying thing is that all kinds of family bucket apps wake up to each other, resulting in a serious power problem in the Android phone ecosystem. Mobile phone manufacturers put forward various self-start control, mutual start control and other schemes.
At the system level, manufacturers directly restrict the broadcast wake up of applications, provider wake up, job scheduler wake up, parent-child service wake up, multi-process monitoring wake up, analog binder call wake up and other black technologies.
Their power control to do a good part of the national applications such as wechat, manufacturers are willing to give it a white list. So, developers of other app companies are despised by the product business. Why can’t we do self-launch? Why can wechat do it?
The mutual restraint and balance between app and mobile phone manufacturers at the national level has also made important contributions to maintaining the ecological stability. Some mobile phone manufacturers even provide API interfaces to Kookmin App, which can carry out dynamic frequency modulation and other functions according to the app’s own running scenarios.
There is also a green pact emerging in the Android community, hoping that each app will join in and maintain the whole ecosystem.
Apple’s iOS is closed as a whole, and all follow Apple’s own push, so app maintenance is relatively calm.
Control background process running control power consumption, self-start control can make APP not run at will, reduce system power consumption; For running APP, power consumption can be reduced by limiting background running.
In iOS, the app can run for about 3 minutes after being removed from the platform (different versions have different policies). If the app expires, it will be killed. If the rule is followed, the process is frozen, similar to signal-stop. Stop the process to reduce power consumption.
You need a similar solution on Android. In 2014 and 15 years ago, system manufacturers mainly used to regularly clean up processes and kill processes running in the background to control the power consumption of mobile phones (another reason was that the memory of mobile phones at that time was not large, and running many applications might make the memory tight and lead to system lag). Later, a similar scheme to freeze the process in the background emerged, avoiding the brutal process killing design (but for the app with abnormal power consumption, the kill mode will still be adopted, but the notification bar may remind users of xx-APP with abnormal power consumption, so the application can be considered to end). You can send a stop signal to the process similar to iOS, so that the process is suspended; You can also use the Cgroups solution supported by Linux to directly add all processes of the application that you want to freeze to the cgroups freezer group for control.
Cell phone systems consume power, and most of it is on the CPU. The process scheduling, can be divided into the current application, foreground application, background application, etc. Android also currently uses groups for control.
CPU scheduling atmosphere scheduler, scheduling policy, scheduling priority, etc. Because Arm chip has divided core CPU, multi-core CPU, and so on, can make the background process run lower priority; Run in small core, reduce CPU frequency and so on to reduce power consumption. For the current application, the opposite strategy is adopted: increasing the CPU frequency, grouping the application by CpusET, setting affinity by Taskset, etc., so that the app used by users can have as much CPU resources as possible.
In 2017, Google announced the energy-Aware Scheduling (AES) scheduler, which was first used on Pixel phones, based on the same task can consume less power. Of course, the same power consumption will lead to better performance.
The AES solution was actually developed a few years ago by Linaro and Arm. Arm doesn’t make CPU chips itself, but it designs them and sells licenses. Both the iPhone and Android phones are based on ARM cpus. Linaro is an organization that connects companies and the open source community for development and design on ARM. Each company can pay different dues to become a gold and silver sponsor. Linaro, of course, synchronises new technologies and solutions to sponsors at different levels of delay.
The main Linux CFS: ‘Completely Fair Scheduler’ CPU Fair Scheduler is used to sort and schedule tasks based on load and priority-weight policies. Priorities and weights are just experience points. The process priority ranges from 0 to 139, with the first 0-99 bits of real-time process priority. 100-139 bits of common process priority, corresponding to the NICE parameter -20-19. The weight corresponds to the NICE value of the common priority. The smaller the NICE value is, the higher the priority is, and the corresponding weight is larger. The weight of a real-time process is 2 times that of nice=-20. The scheduler schedules the real-time process queue first and then the normal priority queue.
AES includes not only CFS, but also CPUIDLE and CPUFREQ. Integrate all the scattered schemes and consider them comprehensively. Scheduling policies based on actual usage efficiency. Instead of relying on experience values for various trial-and-error tunning methods, measurable energy efficiency models are used for tuning methods.
Of course, each device sensor also has its own low-power mode. Idle for a short period of time can automatically suspend or set low power mode.
Mobile phone standby power consumption is abnormal, mainly including:
- Standby: The application terminal and kernel terminal hold the lock.
- Frequent wake up: Wake up ap when various network requests and alarms are interrupted.
- The subsystem is not on standby: the modem, GPS, wifi, and NFC are working or suspend exception occurs.
Doze Mode \ Device Idle designed by Google will restrict the network for non-whitelisted applications after a period of time after the system is off. It will cancel wakelock to avoid system non-sleep, delay alarm and other scheduled task synchronization, and turn off wifi scanning. The App Standby scheme also allows apps that are not commonly used to be restricted from the network. However, this kind of scheme has a poor effect in mainland China. Various schemes of APP can improve the activity. For example, an audio app can fake several times on buried data in order to do daily activities, and each manufacturer also needs to improve tunning with Chinese characteristics.
In fact, in addition to these schemes of Google, manufacturers have long studied targeted optimization based on the app usage habits of different users. Data shows that a user usually has no more than 10 apps. In view of this feature, targeted optimization. For example, to release the waklock lock, disable GPS location calls, turn off wifi scanning or see what scan frequency, and optimize the scanning algorithm to avoid invalid wifi with low signal quality. Disallow app for some frequently changing broadcast responses, alarm alignment, and network heartbeat wake-up alignment. For kernel ap and subsystems, most of them are bug classes that need to be fixed. For example, Wandoujia app used to mute an audio file in the background, hoping to keep it alive, but it made the system unable to standby. There are also some apps that have a weird solution for keeping activity alive with 1 pixel.
The so-called scene power optimization is mainly aimed at applications that are frequently used by users, such as mainstream game apps, mainstream videos, social apps and cameras.
Power consumption and performance are the two ends of the balance, and any one of them fails the product. So companies are trying to strike a balance, with some focusing on performance and others on power consumption. Performance is the key. App developers face the same balance. Power consumption will also become the basic experience of a product, just like the previous stability problem, and the differences will not be too obvious.
To tune, look for standards. You can compare it with your previous products, or with competing products. For example, A and B mobile phones, A mobile phone playing King of Glory current 500mA, but there is A slight lag; So model A, it might have 550mA, but no lag. This is based on the strategy of each product.
### General optimization scheme:
- App power consumption mainly includes CPU, GPU, GPS, Audio and other sensors.
- CPU solution: number of running CPU cores, binding size and size cores, CPU frequency improvement, and minimum FPS.
- GPU solution: Reduce draw content, dynamically limit application resolution for apps, this optimization on gpus is quite obvious.
- GPS scheme: combine the reporting frequency of multiple apps, report frequency dynamically, forbid misuse of standby;
- App dynamic FPS: In addition to controlling THE CPU to ensure the minimum FPS, it can reduce unnecessary CPU and GPU consumption through the adaptive dynamic FPS of each application. The main reason for the lag is not the low FPS, but the instability of FPS, which can be compared: movies don’t lag at 24 frames, while mobile phones generally lag at 60 frames.
- Screen high Hz power consumption: the high frame rates of 90 and 120Hz make for a smoother experience, but not all apps need 120Hz. Turning on or off high Hz for different apps can also reduce power consumption to a certain extent.
- Screen: Data refreshes in performance and on-demand modes.
- Tp: firmware optimization sampling rate, click coordinate algorithm optimization, etc.
### Scenario-based optimization scheme:
For special apps, the optimization is as follows: Reduce layer: Some apps have excess layer, which can be removed by SurfaceFlinger. Long connection: The problem of long connection of wechat and Weibo apps can be dynamically restricted by Netfilter filtering in the kernel layer to reduce the synchronization frequency.
After 2015, due to the early brutal development, various kinds of app survival schemes, the Domestic Android ecosystem is much worse than overseas. System lag and other problems emerge one after another. Various manufacturers also focus on the release of their mobile phone lag, Huawei said 18 months lag, which reflects that the system of Android phones is well tuned after leaving the factory, but after a long time, various apps make the original scheme of the mobile phone system not effective, and the lag also appears again.
Product performance optimization, in addition to hardware solutions, also includes software.
Xiaomi’s early publicity copywriting is how much CPU frequency, how many CPU cores, more memory, more than the iPhone. But it’s still not as smooth as the iPhone. Of course, every generation of iPhone products will also say that CPU increases xx%, GPU performance increases XX %. With the improvement of mobile phone hardware, APP will become bigger and more complex in order to make better use of hardware performance, and the surplus of hardware may also be insufficient.
In addition to one-time deals on hardware, software can be upgraded without further tuning for real-world situations.
In 2017, Apple’s iPad Pro had an amazing 120Hz display. Later, 90Hz and 120Hz screens appeared on android. In 2020, 120Hz became standard on flagship Android devices.
Performance optimization is actually the most extensive. Android hierarchy: app – framework – native – | – kernel driver — sensor/hardware
Component optimization mainly includes CPU, GPU, Flash Disk, DDR Memory, IO, and Network. Process optimization mainly includes: system startup optimization, APP startup optimization, APP operation optimization – Caton Jank optimization; Of course, for app application development, performance and stability mainly include: CPU, GPU, memory, IO, Disk, network, lag, power, stability, package size, startup speed, UI; There’s actually some overlap.
The main ideas for performance optimization include:
- No code is the best optimization — removing some features at the stage;
- Non-critical logic, delayed processing;
- Non-critical logic, consider ahead of time;
- Key logic, multithreading, concurrent execution;
- Cache partial results to reduce repeated execution times;
- Multiple operations at a time, reduce the number of operations;
- Core point: do not let CPU, GPU idle, whether block, wait, or itself idle; Focus on multithreaded schemes and lock wait issues;
- Millisecond level optimization, serial processing of small data, may be faster than multithreaded asynchronous processes because there is no task scheduling delay, such as input events for TP touch screens;
- Big data flow logic, queue, producer, consumer and other asynchronous schemes may be more stable – cut peaks and valleys, so that the whole process stable output. – For example, inputreader- inputDispatcher -outbound->transport; Buffer of nic driver, ring length, SYNC queue of TCP, SB_buffer queue of socket, etc.
### CPU optimization
- Better scheduler: CFS is mainly for server setup, fair scheduling of each task; In fact, Android is more concerned with top-App’s current application performance, and other background processes can make appropriate restrictions. Linaro’S AES not only estimates capacity, but also conducts scheduling optimization based on cluster’s actual consumption.
Switch the number of CPU cores;
- Different tasks are bound to different sizes of cores. The CPUSET task exclusively uses the CPU core, while the Taskset task specifies the CPU core.
- The governor controls the CPU core frequency. CPU modes: On-demand and Performance
- Tasks of each process dynamically occupy CPU resources according to different scheduling policies, priorities, and priorities. For example, top-App and media processes may need to set a high real-time policy.
- Cgroups’ CPU control can limit the CPU usage of specific task groups. For example, it can limit the CPU usage of background processes to more than 50%. It can reduce background waste and give more resources to foreground tasks.
- Open interfaces to top-App so that cooperative apps can switch and set different CPU usage strategies according to their own scenarios, such as CPU boost;
- Configure DTSI, CPU overclocking – for performance;
Take an Android phone as an example: Scaling_available_governors is supported for conservative ondemand userspace powersave performance schedutil schedutil
Tool analysis:
- You can use Ftrace and Systrace to analyze the CPU usage. You can find the status of each process, CPU status, busy CPU frequency and idle CPU frequency. But it’s based on burying points.
- Sampling based scheme;
- Instrument-based solutions: All functions enter and exit for tracking;
- Hook scheme: count the execution times and time of key points;
- Turn flame diagram analysis, quickly find the system bottleneck;
For THE APP side, we should pay more attention to the problem of multi-threading in CPU. Whether to use thread pool or not, after all, it is expensive to create threads frequently. Directly caching threads can save time. Are there too few threads to take full advantage of multicore capabilities? Whether too many cause scheduling delays.
IO intensive and CPU intensive threads have different requirements:
- CPU intensive, generally CPU-core + 1 thread; If the system gives you all the CPU resources, you have to run one thread for each core, and keep one thread for backup task scheduling.
- I/O intensive, may need to start more threads, after all, threads IO block, can not do other things, the CPU is free, increase the number of threads can increase the probability of CPU preemption.
At the system level, pay attention to the CPU saturation. It is recommended that the CPU saturation be about 0.7. Specific still want to see actual test circumstance.
To reduce main thread tasks, Android uses the RenderThread thread as a separate draw frame thread.
### Gpu optimized APP UI response to input operation, through measure, layout, draw, specific composite function to GPU operation.
- Optimize UI layout, or interaction, to reduce the number of layers and objects;
- Some data can be cached, such as font resources, etc., and cache cache of all kinds of resources, such as texture, can be properly tuned.
- Cutting resolution to reduce the amount of data;
- Clock and frequency tuning of GPU;
- Gpu data is executed concurrently instead of serial;
- Optimization of Adreno and Mali 2 Gpu scheduling algorithm;
- Android began to use Skia software to draw, then in order to improve performance, the introduction of GPU drawing; Vulkan was introduced later.
Running out of memory can cause all sorts of unexplained problems; Out of memory all kinds of page reclamation, defragmentation, swap. Can lead to gridlock.
- Maintain the minimum available memory threshold of the system to reduce the lag
Early mobile phone memory is not large, generally use kill background process scheme; Subsequent large memory, more use of freezing scheme. Android integration uses Linux’S LMK: Low Memory killer scheme to kill low-priority processes when the memory is insufficient. Based on different memory thresholds, the process is killed until the specified memory amount is restored. However, default parameters are not applicable in China, and each country has its own tuning parameters. LMK In the startup stage of app, do not kill operation, so as to reduce the lag.
You can use the cgroups memory control function to monitor memory overflow – the absolute value of a process and the relative value/percentage threshold of the total memory.
Note: Do not swap. By default, the app has a maximum heap limit, which is 192MB by default. Some applications require a lot of memory to start up, and if the GC threshold is exceeded, gc collection will be triggered, causing a lag. When the app starts, the system can adjust the threshold by default, and then adjust the recovery threshold by GC after starting the background. You can also use the Cgroups Memory module to limit the process’s memory. In particular, native processes are limited to avoid memory leaks resulting in system instability.
Large apps and national apps can also set a larger memory quota. Reduce GC stackage.
- Background, standby memory reclamation and defragmentation
Memory problems are mostly caused by insufficient capacity. The solution is to find ways to ensure that there is enough memory available.
Mobile systems are used in different scenarios from traditional PC or server systems:
- There are only a few visible apps and one top app at a time, and most of them are background APP processes;
- Cell phones have working time and standby time — people sleep all the time.
Memory optimization can also be based on the nature of the background process for defragmentation; More aggressive overall defragmentation and memory optimization for long standby periods. Disk trim also takes advantage of this feature for standby defragmentation and avoids the time required to start disk check.
Optimize the memory allocation algorithm
- Kernel: The partner system allocates large memory according to page; Small memory is allocated according to slub/slab.
Memory fragmentation problem. After the system runs for a period of time, a variety of holes are left on the DDR, resulting in insufficient continuous space during alloC. The page compact page is compressed.
- User layer: Malloc allocation schemes are also many: high-performance thread-safe: Google’s TCmalloc, Facebook’s Jemalloc. Android uses DLmalloc and Jemalloc;
tcmalloc:
- Similar to kernel distribution scheme;
- Each thread has a ThreadCache local cache, and processes use a CentralCache.
- Small objects go local cache:
- Refer to slub/slab to divide into 100+ classes;
- Large objects go to the central cache: from 1Page, 2pages, 3pages,.. Array + linked list.
- At the same time added the recycling function;
jemalloc:
- Using thread local cache;
- Classification by size small, large, huge;
- The virtual memory is segmented by chunk.
- Higher overhead and better performance than TCMALloc;
IO/disk and filesystem
User-space process IO system calls to the VFS virtual file system; The VFS standardises file systems such as ext4 and f2fs. The file system interconnects with a Generic Block device. Block dev high I/O scheduler that initiates read and write requests; The IO scheduler is similar to the CPU process scheduler. It can merge some pages in order to batch process data and reduce disk I/O operations. When the time is right, the read/write process of a specific block is triggered, and various block device drivers are assigned to read/write disks.
###read
- The system call changes to the kernel state. The user thread blocks and waits.
- The VFS uses the open dirname to locate the block in which the inode corresponds and returns a fd as a token.
- Through the offset offset of the read file, the physical page corresponding to the disk block is mapped to the Page cache, which is convenient to directly read the cache next time and reduce the DISK I/O overhead.
- Data copy to user system call returns a process that blocks and continues execution;
###write
- Find the page cache page corresponding to the offset of the inode by fd, and preread it into the kernel if there is no page cache page.
- Update data to the page cache and return it directly to the user process.
- Kernel then selects the time to write back to disk block. The default value is 5s.
- A block driver is a code controller that works with a disk to read, write, and verify blocks. After all, the disk is dead. The code is alive.
### Reference scheme:
New flash specifications for mobile devices, new file system types;
- Select read/write, MMAP, or DIRECT I/O based on the service type.
- Pay attention to the number of I/O operations and the size of the buffer. Read and write the page twice as fast to reduce the number of I/OS.
- IO scheduler elevator algorithm request queue and scheduling policy Settings: CFS, NOOP, deadline; *
- Sequential IO can appropriately increase the prefetch size;
All kinds of Android grab log schemes, using MMAP scheme, because process VMA and kernel address space is shared, can reduce the overhead of 1 layer system call.
huawei: iotrace, io monitor
For app end, SQLite and shared preference have a great impact on performance.
- Sqlite optimization: Page size, buffer size; In WAL mode, the statistics function is disabled and MMAP is supported.
- Shared preference rewriting optimization: for example, mMAP substitution; Local server updates with Binder by UID.
Network optimization
No matter the system or app, network optimization is very important. If there is no Internet or poor network from time to time, this product may not sell.
Network optimization, focusing on throughput and latency. The key scenarios are weak networks and network congestion.
Data is transmitted through terminal – transport path – server. Network processes from the bottom up include:
- Inter-route forwarding outside the device;
- Network adapter sends and receives data; Network layer IP;
- Transport layer TCP, UDP;
- The application layer.
Server side: client load balancing according to different IP, multi-data center, nearby data center, CDN;
Transmission path: special line with the rich. The terminal may pass through various routers to the server. Therefore, ensure that the worst router is compatible with the server.
### DNS problem: DNS hijacking is quite common, most apps use HTTPDNS; DNS proofs; Large companies have many servers and will return to the nearest location based on the IP address. Of course, it will also prefabricate several IP hash pockets for servers to speed up access. Android system layer to do DNS local cache, similar to DNSMASQ, combined with various apps for pre-cache or app download and installation to do pre-resolution, this app, especially common app cold start speed greatly help. (But don’t break the load balancing done by large app clients).
Mobile phone manufacturers with energy can also monitor the bottom of different GPS locations, different operators, network types (fixed network or base station (GPS + base station ID)) under the various domain name resolution speed summary analysis, development of the second curve business – DNS service providers.
Network library: OkHTTP for Android and AFNetwork for iOS. At present, many companies make customization based on Chromium NET module – Cronet (open source network library with the most complete protocol support), such as reducing HTTPS certificate exchange RTT, replacing RSA with faster encryption algorithm, etc. At the same time, they cooperate with the server to achieve 0 RTT. HTTPDNS customization, etc. — Cronet better have someone take the lead to make the public network library. After all, some third-party SDK companies do not support this, which will also affect the overall network module customization of app.
###app compressed data, compression algorithm, request merge, long connection instead of short connection, multi-level cache, epoll, AIO, connection pool, preconnection, multiplexing, SPDY, QUIC, priority ipv6, etc.;
# # # socket layer:
- Optmem_max buffer size of a single socket;
- The size of the send/receive buffer of the entire socket;
- The size of the transmission layer TCP/UDP send/receive buffer range;
- Socket options can control many things, such as timeout and buffer, as well as TCP_NODELAY and TCP_CORK.
- Pay attention to the/proc/sys/net/core / *;
- There is another important issue: all kinds of timeouts should be dynamically adapted to mobile networks. Fast timeouts and fast retries can improve users’ perceived experience.
### Transport layer: Linux as a mobile system is unlikely to have the high concurrent connections of a server environment. It is positioned as the client itself, and the related problems are much simpler, such as TCP time_wait state is too much, etc. Of course, android can control the number of ports by UID. TCP is the most complex protocol with the most boundary problems.
The simplest ones include TCP/UDP RMEM, WMEM adjustment, tcp_synack_retries, tcp_fin_timeout, and tcp_tw_reuse. Tcp_keepalive_time, tcp_keepalive_intvl, and tcp_keepalive_probes for long connections. Dynamically adjust the packet size based on the MTU to reduce fragments.
Other ip_local_port_range, increase the number of fd.
In fact, TCP was originally designed based on the low network speed of wired networks. The current network congestion collision algorithm is not particularly good in mobile network scenarios. Based on the network monitoring analysis of Google’s own search engine and YouTube, the TCP BBR congestion control algorithm is proposed with satisfactory results. The Multipath TCP connected by Huawei already uses BBR.
# # # the link layer
- Nic hard interrupt Configuration CPU affinity: smp_affinity and smp_affinity_list in /proc/irq/xxx.
Irqbalance (by default, interrupts are scanned every 10 seconds for affinity reset);
- Softirqs: RPS (the Receive Packet Steering) hash Packet of quad to the corresponding CPU, / sys/class/net/xx/the queues/rx – 0 / rps_cpus;
- Soft interrupt: RFS (Receive Flow Steering) bind CPU according to Flow to improve cache hit ratio: The/proc/sys/net/core/rps_sock_flow_entries, / sys/class/net/XXX/the queues/rx – 0 / rps_flow_cnt;
- Multi-queue, queue length, buffer, etc.
- Cgroups NET subsystem is controlled by class and priority, ~ TC qos;
- Some functions of the protocol layer are processed by network adapters, such as TCP Segmentation Offload (TSO), UDP Fragmentation Offload (UFO), and Generic Receive Offload (GRO).
See DPDX XDP: kernel layer logic floats up to the user space and sinks down to the nic; Due to the particularity of mobile phone system, customized network card is a bright spot of differentiation competition. Terminal: Mobile network and wifi. Weak network: mobile network is more unstable than wired network. The problem of weak networks is quite serious. The modem performs different tuning for weak signals and congestion in the cell, eliminates weak signals, switches to other cell quickly if failure occurs, and resets the disabled time in the cell. Core point: fast trial-and-error when weak signal, fast competition pit when congestion.
Wifi: RF antenna layout, RX \ TX parameter tuning, improve transmitting power and receiving gain, wifi EDCA\QoS\ competition window, rate selection algorithm optimization, wifi TCP buffer optimization, etc.
Monitor interface network data packet sending and receiving, error, packet loss and other data, dynamically switch network or increase power and other customized optimization schemes. The modem can preferentially determine the appropriate network type based on the GPS and record information to speed up network connection.
Systrace: Based on Linux ftrace for transformation of buried point tracking technology scheme. Native is to configure parameters, crawl logs, and analyze on Chrome. It can be improved to an online real-time analysis scheme. PC to implement chrome server (you can also use Node. js to implement Chrome trace analysis function). After dynamic configuration is enabled on the server, the mobile phone uploads trace logs in real time over the network, and streaming dynamic trace data can appear on the PC, which is similar to the CPU profiler function of Android Studio.
In the compact version, you can enable basic tags such as CPU tags to check the automatic dump trace logs and upload them to the log server. You can use Looper, Choreographer, GPU, RenderThread, Surfaceflinger, etc.
Gpu tools: Adreno Profiler, Mali Debugger, etc.
Perf, SystemTap, DTrace and other customized applications on Android.
###app startup optimization
- Apk Redex optimization;
- Ensure hot start as far as possible;
- Disable kill, GC and other time-consuming tasks during startup.
- Launcher responds to click events first, and other tasks are asynchronized or delayed;
- The view executes serially ahead of the click event;
- Start animation individually designed and speed adjusted, this affects speed;
- App process pre-creation and caching – save about 30-50ms;
- App renderThread etc is pre-started, GPU etc environment is pre-initialized and cached – saving dozens of ms;
- App theme, layout and other resources unrelated to business logic can be pre-loaded and cached – can save hundreds of ms;
- App lib so usage monitor, pre-Mmap;
- App reduces unnecessary business paths and delays unimportant logic;
- System-level DNS cache – saves 100~400ms;
- App startup process task-oriented, make full use of multi-core CPU, avoid lock waiting.
- Ams starts large lock optimization, which limited ensures that each core thread of Top-APP and System_server occupies CPU advantage resources;
- All types of Boost and top-App exclusive large core binding, priority increased, background process priority decreased;
###app run optimization
- Tp point optimization: shorten sleep wake interval, improve sampling rate, optimize coordinate calculation algorithm;
Without sync signal, directly process the input event process, click ahead to process, sliding threshold, friction parameters optimization,input can also be processed by a separate thread;
- The view considers the click event to be serial rather than post, and the click effect is asynchronous or onclick is preferentially processed.
- Child threads share UI threads input/.. /measure/layout task – Facebook solution can be consulted;
- App dynamic resolution – Some apps reduce the resolution without affecting the experience;
- App itself all kinds of ListView/RecycleView item view refinement cache;
- App layout optimization, custom level, reduce level, etc.
- The framework layer measure dynamically optimizes unnecessary viewgroups and reduces levels;
- View cross-context cache, update the view context when switching activities;
- Gpu data parallelization;
- SurfaceFlinger filter excess layer, dynamic FPS;
- Other system level: CPU core binding of APP thread, CPU \ GPU frequency, scenarialized boost, reserve enough memory, reduce unnecessary IO, improve CPU, disk, NET priority. Irq balance. Cgroups can limit CPU, MEM, disk, NET, IO, and make full use of it to ensure top-app is used first.
- Pay attention to speed limiting and frequency reduction caused by thermal.
- For the purpose of high scalability and portability, the whole system mostly adopts hierarchical structure. However, customized corresponding system can break the original architectural design and design a better priority and real-time scenario-based scheme according to the unique performance of Android such as Top-App, such as giving priority to network requests and IO requests of Top-App.
Machine code optimized operation, reduce bytecode parsing operation — at present, almost all are ARM chips, Google can consider giving up generality, and independently compile into machine code level APP. In fact, Google is now paying more attention to developer opinions. The system side can open two types of apis:
App startup acceleration API: provide API for app preloading cache to improve the speed of cold startup; App running FM API: wechat provides its boost API as Hardcoder, which is also open source; ### Digression: Domestic Android ecosystem and foreign completely two code.
The second half of the emergence of wechat and other super apps, do so well that users will feel that the questions are mobile phone problems, rather than wechat APP problems, indirect “kidnapping” mobile phone manufacturers, resulting in wechat in all manufacturers are almost all kinds of white list. The app companies are threatened by the introduction of mini-programs, which skip the app store, and the loss of revenue from advertising distribution.
Domestic manufacturers of Android alliance, on the one hand to prevent Google’s more in-depth control, on the one hand to avoid hardware homogeneity and excessive performance without selling points, worried about the super APP do wedding, united to cut the cake of national APP, into the Internet, slowly reach the unification of the domestic Android system.
Since mobile phone manufacturers have all the user habits, they combine AI and big data to carry out user portraits and provide more accurate mobile phone use experience for thousands of people. For example, all the browsers launched are based on Chromium browser, but they can go deeper into the system layer and provide better use experience than Google chrome. After all, they are standing on the shoulders of giants. Of course, they can also launch their own super apps based on their user base of more than 100 million users — manufacturers will be almost burned out to play this.
### Digression: Baidu’s way of dying
Baidu has been suffering in the domestic search field for a long time. Google cannot enter mainland China due to domestic policy reasons. Google abandoned its principle (it has removed the “don’t be evil” culture) and prepared to enter mainland China, which was also opposed by Google employees. Browser manufacturers, especially mobile browser manufacturers, cooperate with Google in search services and customize search, so that Google can avoid opposition from internal employees and provide high-quality search services to mainland China. Baidu PC search traffic seems to have been less than 10%, mobile terminal only a Baidu app in the struggle to maintain (so-called 200 million users, but are “three vulgar” content). And manufacturers to provide customized browser plus customized Google search services, directly can let Baidu hit. Of course, Baidu so many years of reform, are useless. Starting from scratch (technology sharing) by splitting business lines into independent subsidiaries, with the parent company reducing control and holding on to earnings, it may still survive.
### back:
And the subsequent development direction of mobile devices will gradually deepen the customization of hardware and software, so as to differentiate. For example vivo spent 8000w and oneplus spent 100 million to deeply customize the screen with Samsung, running ahead of Samsung’s own phones.
Ideas are more important than concrete plans:
- Monitor various indicators of APP or system itself, and synchronize key points to the server — standard product development logic;
- Special user feedback problem solving — can find more extensive and close to the user’s urgent problems;
- Reference of various solutions: business direction of front-end, background and mobile terminal, mutual reference of ideas of similar products, direction of APP, Framework, native, kernel, driver, hardware and other levels; — Think across boundaries
- Reverse all kinds of ROM, APP; Patent analysis, product publicity analysis; *
- High salary recruitment xx post, interview friends business related personnel, plan, ideas to obtain.
- The first two points of thought belong to the conventional type; The last three points are extended.
Technical scheme reference, is “cross – boundary” do technical thinking. Many of the solutions found so far are reference for switching and porting between different products or system platforms.
The nice thing about Android is that there is a common master: the Google version, and then the other OEM ROMs, so differentiation is fairly simple. It is very helpful for reverse analysis of competitive products. Is the android platform more unique idea.
And the high-paying recruitment cover is more difficult to guard against crooked move. This is generally a weak company team recruiting strong company personnel will use. In order to prove their ability, the interviewees will explain the key project work, and even the details are open. The interviewer can say 7 and leave 3. The data within the score of 7 can be subject to a wide margin of error, and the score of 3 mainly includes large pits. If you really want to hire, correct it when you come on board. If malicious, it can mislead some. Since malicious recruiting is mostly done by lower-level teams, this is a solution they won’t find out about.
The goal of all previous schemes is to find direction. After finding the direction to sink into a special task, the team needs to have professional reserves in all directions – the industry has specialized.
All kinds of software and hardware security issues, privacy issues will become more and more important. There have been FPGA chip security vulnerabilities, CPU instruction rearrangement vulnerabilities, various OS systems, APP and other security vulnerabilities. Big companies have also been fined for privacy issues.
- Android ROM decompilation problem;
- Prop property is designed independently according to uid, and pkgname and User group are added as salt.
- Imei, MAC and other information are all virtualized for APP. Each APP gets different fixed values, and users can manually refresh them randomly from time to time.
- Google version of the Android permission application problem, has been abused by domestic app, if you do not give permission, app will quit. Passive Permission Control provided by domestic manufacturers is more suitable for privacy control. App can be used normally, but sensitive permission control can reject some data reading requests.
Every year, Google holds internal camps for upstream and downstream manufacturers around the Spring Festival. Domestic manufacturers can feed back typical problems of the Domestic Android ecosystem and promote The optimization and upgrading of Google.
In summary, all mobile phone products are looking for solutions that exceed users’ expectations, or that are differentiated from their competitors. From the beginning of the frequency hardware products, to the back power consumption, performance and so on. Sometimes, is not a good plan, immediately all on the horse; Instead, it studies the development trend of the industry and selects several competitive points to seize the first-mover advantage, impress users and put aside competing products.
Lenovo chose anti-counterfeiting base stations as its selling point, pleasing only a small number of security-sensitive users who had experienced fraud. That’s not a very good selling point. In fact, currently get better several mobile phone manufacturers are the main performance and photo.
To continue to sell more products and gain greater profits is the basis for the survival of mobile phone products.
The original author: Ha Ha. The original address: zhuanlan.zhihu.com/p/138828988 written personal feel good, reproduced and set aside