1e41f4b71Sopenharmony_ci# Power Consumption Statistics Customization
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciBy default, OpenHarmony provides the power consumption statistics feature. However, the power consumption benchmarks vary according to hardware specifications of different products. To address this issue, OpenHarmony provides the power consumption statistics customization function, allowing you to customize power consumption benchmarks depending on your hardware specifications.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Basic Concepts
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciPower consumption statistics: When a user uses a device, software and hardware services running on the device report usage events through [HiSysEvent](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-overview.md). The usage duration of software and hardware can be calculated based on these reported events. Then, the power consumption of the software and hardware can be calculated based on the hardware power consumption benchmarks.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciPower consumption benchmark: baseline power consumption (unit: mA) of the product hardware in various states, for example, the baseline power consumption when the camera is turned on and the baseline power consumption of the CPU at different frequencies.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci### Constraints
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciThe configuration path for power consumption statistics customization is subject to the [configuration policy](https://gitee.com/openharmony/customization_config_policy). In this development guide, `/vendor` is used as an example of the configuration path. During actual development, you need to modify the customization path based on the product configuration policy.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## How to Develop
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci### Setting Up the Environment
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Hardware requirements:**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ciDevelopment board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**Environment requirements:**
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciFor details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md).
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci### Getting Started with Development
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ciThe following uses [DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568) as an example to illustrate power consumption statistics customization.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci1. Create the `battery_statistics` folder in the product directory [/vendor/hihope/rk3568](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568).
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci2. Create a target folder by referring to the [default folder of power consumption statistics configuration](https://gitee.com/openharmony/powermgr_battery_statistics/tree/master/services/profile), and install it in `//vendor/hihope/rk3568/battery_statistics`. The content is as follows:
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci    ```shell
41e41f4b71Sopenharmony_ci    profile
42e41f4b71Sopenharmony_ci    ├── BUILD.gn           # BUILD.gn file
43e41f4b71Sopenharmony_ci    └── power_average.json # Configuration file for power consumption statistics, including the hardware power consumption benchmarks
44e41f4b71Sopenharmony_ci    ```
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci3. Write the custom `power_average.json` file by referring to the [power_average.json](https://gitee.com/openharmony/powermgr_battery_statistics/blob/master/services/profile/power_average.json) file in the default folder of power consumption statistics configuration. The following table describes the configuration items for power consumption benchmarks.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci    **Table 1** Description of the configuration items for power consumption benchmarks
49e41f4b71Sopenharmony_ci    | Configuration Item| Hardware Type| Data Type| Description|
50e41f4b71Sopenharmony_ci    |----------|------|---------|-------------------------------------------------------------------------|
51e41f4b71Sopenharmony_ci    | alarm_on | - | Double | Power consumption when the timer is triggered by the system or application once; that is, the baseline power consumption.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Number of triggering times x Power consumption benchmark|
52e41f4b71Sopenharmony_ci    | bluetooth_br_on | Bluetooth | Double | Baseline power consumption when Bluetooth is enabled.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
53e41f4b71Sopenharmony_ci    | bluetooth_br_scan | Bluetooth | Double | Baseline power consumption for Bluetooth scanning.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
54e41f4b71Sopenharmony_ci    | bluetooth_ble_on | Bluetooth | Double | Baseline power consumption when Bluetooth Low Energy (BLE) is enabled.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
55e41f4b71Sopenharmony_ci    | bluetooth_ble_scan | Bluetooth | Double | Baseline power consumption for Bluetooth Low Energy (BLE) scanning.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
56e41f4b71Sopenharmony_ci    | wifi_on | WIFI | Double | Baseline power consumption when Wi-Fi is enabled.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
57e41f4b71Sopenharmony_ci    | wifi_scan | WIFI | Double | Power consumption when Wi-Fi scanning is performed once.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Number of triggering times x Power consumption benchmark|
58e41f4b71Sopenharmony_ci    | radio_on | Phone | Double array| Baseline power consumption when the call service is enabled. The value is in array format and is used to configure the baseline power consumption for different signal strength levels. The default configuration provides the baseline power consumption for four signal strength levels.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
59e41f4b71Sopenharmony_ci    | radio_data | Phone | Double array| Baseline power consumption when the network service is enabled. The value is in array format and is used to configure the baseline power consumption for different signal strength levels. The default configuration provides the baseline power consumption for four signal strength levels.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
60e41f4b71Sopenharmony_ci    | camera_on | Camera | Double | Baseline power consumption when the camera is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
61e41f4b71Sopenharmony_ci    | flashlight_on | Flashlight | Double | Baseline power consumption when the flashlight is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
62e41f4b71Sopenharmony_ci    | gnss_on | GNSS | Double | Baseline power consumption when the Global Navigation Satellite System (GNSS) is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
63e41f4b71Sopenharmony_ci    | sensor_gravity_on | Gravity Sensor | Double | Baseline power consumption when the gravity sensor is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
64e41f4b71Sopenharmony_ci    | sensor_proximity_on | Proximity Sensor | Double | Baseline power consumption when the proximity sensor is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
65e41f4b71Sopenharmony_ci    | audio_on | Audio | Double | Baseline power consumption when the audio is enabled.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
66e41f4b71Sopenharmony_ci    | screen_on<br>screen_brightness | Screen | Double | **screen\_on**: baseline power consumption of the screen in the basic state (excluding power consumption generated by screen brightness).<br>**screen\_brightness**: baseline power consumption additionally required when the screen brightness increases by one level.<br>For example, if **screen_on** is **90**, **screen_brightness** is **2**, and the screen brightness is **100**, then the baseline power consumption is calculated as follows: 90 + 2 x 100 = 290.<br>- Power consumption type: hardware power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
67e41f4b71Sopenharmony_ci    | cpu_awake | CPU | Double | Baseline power consumption when the CPU wakes up the lock from the lock holding state.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
68e41f4b71Sopenharmony_ci    | cpu_idle | CPU | Double | Baseline power consumption when the CPU is in the idle state.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
69e41f4b71Sopenharmony_ci    | cpu_suspend | CPU | Double | Baseline power consumption when the CPU is in the sleep state.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
70e41f4b71Sopenharmony_ci    | cpu_active | CPU | Double | Baseline power consumption when the CPU is in the active state.<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
71e41f4b71Sopenharmony_ci    | cpu_clusters<br>cpu_speed_clusterX | CPU | Double array| **cpu\_clusters**: baseline power consumption of a CPU cluster. The value is in array format and is used to configure the baseline power consumption for different CPU clusters. The default configuration provides the baseline power consumption for three CPU clusters.<br>**cpu\_speed\_clusterX**: baseline power consumption of a CPU cluster at different frequencies, in array format. **X** indicates the sequence number. By default, its value ranges from **0** to **2**, corresponding to the size of the **cpu_clusters** array. For example, if the default size of the **cpu_clusters** array is **3**, the sequence number corresponds to the configuration items **cpu_speed_cluster0**, **cpu_speed_cluster1**, and **cpu_speed_cluster2**.<br>- Scalability: scalability of CPU clusters<br>- Power consumption type: software power consumption<br>- Statistical method: Power consumption = Usage duration x Power consumption benchmark|
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci    >**NOTE**<br>The hardware type described in the table is the actual hardware name and does not represent the power consumption statistics type. For details about the power consumption statistics type, see [ConsumptionType](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-batteryStatistics.md#consumptiontype).
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci4. Write the `BUILD.gn` file by referring to the [BUILD.gn](https://gitee.com/openharmony/powermgr_battery_statistics/blob/master/services/profile/BUILD.gn) file in the default folder of power consumption statistics configuration to pack the `power_average.json` file to the `//vendor/etc/profile` directory. The configuration is as follows:
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci    ```shell
78e41f4b71Sopenharmony_ci    import("//build/ohos.gni")                  # Reference build/ohos.gni.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci    # Install power_average.json to /vendor/etc/profile/power_average.json
81e41f4b71Sopenharmony_ci    ohos_prebuilt_etc("power_average_config") { # Custom name, for example, power_average_config.
82e41f4b71Sopenharmony_ci        source = "power_average.json"
83e41f4b71Sopenharmony_ci        relative_install_dir = "profile"
84e41f4b71Sopenharmony_ci        install_images = [ chipset_base_dir ]   # Required configuration for installing the power_average_config file in the vendor directory, where chipset_base_dir = "vendor". If this field is left unspecified, the power_average_config file is installed in the system directory by default.
85e41f4b71Sopenharmony_ci        part_name = "product_rk3568"            # Set part_name to product_rk3568 for subsequent build.
86e41f4b71Sopenharmony_ci    }
87e41f4b71Sopenharmony_ci    ```
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci5. Add the build target to `module_list` in [ohos.build](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/ohos.build) in the `/vendor/hihope/rk3568` directory. For example:
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci    ```json
92e41f4b71Sopenharmony_ci    {
93e41f4b71Sopenharmony_ci      "parts": {
94e41f4b71Sopenharmony_ci          "product_rk3568": {
95e41f4b71Sopenharmony_ci          "module_list": [
96e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/default_app_config:default_app_config",
97e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/image_conf:custom_image_conf",
98e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/battery_statistics/profile:power_average_config", # Add the configuration for building of power_average_config.
99e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
100e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
101e41f4b71Sopenharmony_ci              "//vendor/hihope/rk3568/etc:product_etc_conf"
102e41f4b71Sopenharmony_ci          ]
103e41f4b71Sopenharmony_ci          }
104e41f4b71Sopenharmony_ci      },
105e41f4b71Sopenharmony_ci      "subsystem": "product_hihope"
106e41f4b71Sopenharmony_ci    }
107e41f4b71Sopenharmony_ci    ``` 
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci    In the preceding code, `//vendor/hihope/rk3568/power/battery_statistics/` is the folder path, `profile` is the folder name, and `power_average_config` is the build target.
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci6. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md).
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci    ```shell
114e41f4b71Sopenharmony_ci    ./build.sh --product-name rk3568 --ccache
115e41f4b71Sopenharmony_ci    ```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci7. Burn the customized version to the DAYU200 development board.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci### Debugging and Verification
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci1. After startup, run the following command to launch the shell command line:
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci    ```shell
124e41f4b71Sopenharmony_ci    hdc shell
125e41f4b71Sopenharmony_ci    ```
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci2. Run the following command to check whether the `power\_average\_config` file is successfully created in the `vendor` directory:
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci    ```shell
130e41f4b71Sopenharmony_ci    ls -l /vendor/etc/profile/
131e41f4b71Sopenharmony_ci    ```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci    The `power\_average\_config` file is successfully created if it exists in `/vendor/etc/profile/`.
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci    ```shell
136e41f4b71Sopenharmony_ci    # ls -l /vendor/etc/profile/                                                   
137e41f4b71Sopenharmony_ci    total 4
138e41f4b71Sopenharmony_ci    -rw-r--r-- 1 root root 1446 2023-03-26 16:47 power_average.json
139e41f4b71Sopenharmony_ci    # 
140e41f4b71Sopenharmony_ci    ```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci3. If the `power_average_config` file is successfully created, run the following command to check whether the information in the `power_average_config` file in the `vendor` directory is consistent with the customized information:
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci    ```shell
145e41f4b71Sopenharmony_ci    cat /vendor/etc/profile/power_average.json
146e41f4b71Sopenharmony_ci    ```
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci4. If the information is consistent, run the following command to view the power consumption statistics:
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci    ```shell
151e41f4b71Sopenharmony_ci    hidumper -s 3304 -a -poweraverage
152e41f4b71Sopenharmony_ci    ```
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci5. Check the console output for the custom power consumption benchmark information.
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci    For example, the default power consumption benchmark information is as follows:
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci    ```shell
159e41f4b71Sopenharmony_ci    # hidumper -s 3304 -a -poweraverage                                            
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci    -------------------------------[ability]-------------------------------
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci    ----------------------------------BatteryStatisticsService---------------------------------
165e41f4b71Sopenharmony_ci    POWER AVERAGE CONFIGATION DUMP:
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci    ··· (Only the camera configuration is displayed here. Other information is omitted.)
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci    camera_on : 810.000000
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci    ······
172e41f4b71Sopenharmony_ci    ```
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci    If the power consumption statistics benchmark is set to **camera_on:3000** (default value: **810**), the console output is as follows:
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci    ```shell
177e41f4b71Sopenharmony_ci    # hidumper -s 3304 -a -poweraverage                                            
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci    -------------------------------[ability]-------------------------------
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci    ----------------------------------BatteryStatisticsService---------------------------------
183e41f4b71Sopenharmony_ci    POWER AVERAGE CONFIGATION DUMP:
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci    ··· (Only the camera configuration is displayed here. Other information is omitted.)
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci    camera_on : 3000.000000 # Set the power consumption benchmark to "camera_on": 3000.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci    ······
190e41f4b71Sopenharmony_ci    ```
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci6. Check whether the power consumption statistics are calculated according to the custom power consumption benchmark in the `power_average_config` file.
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci7. You can use the [JS APIs](https://gitee.com/openharmony/interface_sdk-js/blob/master/api/@ohos.batteryStatistics.d.ts) or [Inner APIs](https://gitee.com/openharmony/powermgr_battery_statistics/blob/master/interfaces/inner_api/include/battery_stats_client.h) provided by the **batterystatistics** module to obtain detailed power consumption information and verify the custom power consumption benchmark.
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci## Reference
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ciDuring development, you can refer to the [default power consumption statistics configuration](https://gitee.com/openharmony/powermgr_battery_statistics/blob/master/services/profile/power_average.json).
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ciDefault packing path: `/system/etc/profile`
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ciCustomization path: `/vendor/etc/profile`
203