目录

@hms.fast.schedulingOptimization (System Performance Optimization)

Apps are allowed to provide performance scenario information to the system. The system will then optimize app performance to the greatest extent within the API's effective scope, thereby enhancing user experience.

  1. Apps must catch exceptions when calling this API.

  2. perfHint is merely a performance optimization hint sent from the app to the system. Upon receiving the hint, the system will evaluate factors such as the overall CPU load and system temperature to make a decision. Performance enhancement is not guaranteed.

  3. Performance hints take effect only when the app is running in the foreground and will become invalid once the app switches to the background.

  4. Reporting a thread ID to improve the QoS priority cannot be used together with the QoS API.

Since: 26.0.0

Modules to Import

import { schedulingOptimization } from '@kit.FASTKit';

SceneType

Enumerates scenario types requiring system performance optimization.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.FAST.SchedulingOptimization

Since: 26.0.0

Name Value Description
APP_LAUNCH 1 App launch.
PAGE_TRANSITION 2 Page switching.
PAGE_LOAD 3 Page loading.
NETWORK_FILE_PROCESSING 4 Network file processing.
LOCAL_FILE_PROCESSING 5 Local file processing.
PAGE_DRAWING 6 Page drawing.
ANIMATION 7 Animation.
MEDIA_PLAYBACK 8 Media playback.
MEDIA_ENCODING_AND_DECODING 9 Media encoding and decoding.

SceneState

Enumerates the scenario states for system performance optimization.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.FAST.SchedulingOptimization

Since: 26.0.0

Name Value Description
END 0 System performance optimization ends.
BEGIN 1 System performance optimization starts.

DurationType

Enumerates duration options for system performance optimization.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.FAST.SchedulingOptimization

Since: 26.0.0

Name Value Description
SHORT 1 Short duration. The maximum duration for a single optimization operation is 1 second, and the interval between operations must be greater than 3 seconds. Unit: second.
MEDIUM 2 Medium duration. The maximum duration for a single optimization operation is 10 seconds, and the interval between operations must be greater than 30 seconds. Unit: second.
LONG 3 Long duration. The maximum duration for a single optimization operation is 60 seconds, and the interval between operations must be greater than 180 seconds. Unit: second.

PerfHintConfig

Configuration parameters for system performance optimization.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.FAST.SchedulingOptimization

Since: 26.0.0

Name Type Read-Only Optional Description
sceneType SceneType No No System performance optimization scenario type.
sceneState SceneState No No System performance optimization scenario state.
durationType DurationType No No System performance optimization duration type.
tids number[] No No Thread IDs for system performance optimization. The maximum length is 16.

perfHint

perfHint(config: PerfHintConfig): Promise<void>

System performance optimization API. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.FAST.SchedulingOptimization

Since: 26.0.0

Parameters:

Name Type Mandatory Description
config PerfHintConfig Yes Configuration parameters for system performance optimization.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Error Code Error Message
1027700001 High system load.
1027700002 Power Saving Mode.
1027700003 Low Power Mode.
1027700004 Non-frontend calling scenarios.
1027700005 The interval does not meet the requirement.
1027700006 Failed to execute scheduling optimization.

Example:

import { schedulingOptimization } from '@kit.FASTKit';

@Entry
@Component
struct PerfHintDemo {

  build() {
    Column() {
      Button("perfHint")
        .onClick(async () => {
          try {
            let config: schedulingOptimization.PerfHintConfig = {
              sceneType: schedulingOptimization.SceneType.APP_LAUNCH,
              sceneState: schedulingOptimization.SceneState.BEGIN,
              durationType: schedulingOptimization.DurationType.SHORT,
              tids: [] // Enter the thread ID as required.
            };

            await schedulingOptimization.perfHint(config);
            console.info('perfHint success');
          } catch (error) {
            console.error(`perfHint error.code is ${error.code}, message is ${error.message}`);
            // Handle the error based on the error code.
          }
        })
        .width('100%')
    }
    .margin(16)
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}