27. API Hook
This chapter describes the API hook APIs of CANN Runtime, which allow performance analysis tools (such as Profiling) to intercept some Runtime APIs (referring to APIs whose names start with aclrt or aclmdlRI, excluding the APIs in this chapter) by injecting hook functions.
aclError aclrtApiInjectionSetFunc(const char* name, aclrtApiFunc func): Sets the current implementation function pointer of the Runtime API with the specified name to the specified hook function.aclError aclrtApiInjectionGetFunc(const char* name, aclrtApiFunc* originFunc, aclrtApiFunc* currentFunc): Obtains the original implementation function pointer and the current implementation function pointer of the Runtime API with the specified name.
aclrtApiInjectionSetFunc
aclError aclrtApiInjectionSetFunc(const char* name, aclrtApiFunc func)
Applicable Products
- Ascend 950PR&950DT products: Supported
- Atlas A3 products: Supported
- Atlas A2 products: Supported
- Atlas 200I/500 A2 inference products: Supported
- Atlas inference products: Supported
- Atlas training products: Supported
Description
Redirects the Runtime API with the specified name to the corresponding hook function implementation. After this is set, calling the Runtime API jumps to the hook function for execution.
You can inject a hook function through this API to intercept and count Runtime API calls. To restore the original behavior, call this API and pass the original function pointer. The original function pointer can be obtained through aclrtApiInjectionGetFunc.
Parameters
| Parameter | Input/Output | Description |
|---|---|---|
| name | Input | Runtime API name, which must exactly match the Runtime API name, for example, "aclrtMemcpy". |
| func | Input | Hook function pointer. See aclrtApiFunc for the type definition. |
Return Values
0: success; others: failure. For details, see aclError.
Constraints
- This API is recommended to be called before the corresponding Runtime API is called. You must ensure the call sequence.
- When calling the aclrtApiInjectionSetFunc API and the aclrtApiInjectionGetFunc API, you must ensure the multithreaded call sequence.
- The APIs that currently support hooking are APIs starting with aclrt or aclmdlRI. APIs such as aclInit and aclFinalize are not supported.
- For product models that do not support the hook feature, this API returns
ACL_ERROR_FEATURE_UNSUPPORTED.
aclrtApiInjectionGetFunc
aclError aclrtApiInjectionGetFunc(const char* name, aclrtApiFunc* originFunc, aclrtApiFunc* currentFunc)
Applicable Products
- Ascend 950PR&950DT products: Supported
- Atlas A3 products: Supported
- Atlas A2 products: Supported
- Atlas 200I/500 A2 inference products: Supported
- Atlas inference products: Supported
- Atlas training products: Supported
Description
Obtains the original implementation function pointer and the current implementation function pointer of the Runtime API with the specified name.
During the runtime initialization phase, the original implementation pointer (originFunc) is equal to the current implementation pointer (currentFunc). After a hook function is injected by calling aclrtApiInjectionSetFunc, currentFunc points to the hook function, while originFunc remains unchanged. If the queried originFunc and currentFunc differ, the implementation of that Runtime API has been replaced by the hook function.
Parameters
| Parameter | Input/Output | Description |
|---|---|---|
| name | Input | Runtime API name, which must exactly match the Runtime API name, for example, "aclrtMemcpy". |
| originFunc | Output | Original function pointer. Pass nullptr if not needed. See aclrtApiFunc for the type definition. |
| currentFunc | Output | Current function pointer. Pass nullptr if not needed. See aclrtApiFunc for the type definition. |
Return Values
0: success; others: failure. For details, see aclError.
Constraints
- For product models that do not support the hook feature, this API returns
ACL_ERROR_FEATURE_UNSUPPORTED. - When calling the aclrtApiInjectionSetFunc API and the aclrtApiInjectionGetFunc API, you must ensure the multithreaded call sequence.