* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "api_snapshot.hpp"
#include "api_c.h"
#include "global_state_manager.hpp"
#include "rts/rts.h"
#include "runtime.hpp"
using namespace cce::runtime;
#ifdef __cplusplus
extern "C" {
#endif
static rtError_t CheckSnapShotFeatureSupport()
{
const Runtime* const rtInstance = Runtime::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(rtInstance);
const rtChipType_t chipType = rtInstance->GetChipType();
if (!IS_SUPPORT_CHIP_FEATURE(chipType, RtOptionalFeatureType::RT_FEATURE_DFX_PROCESS_SNAPSHOT)) {
RT_LOG(
RT_LOG_WARNING, "chip type(%d) does not support process Snapshot feature.",
static_cast<int32_t>(rtInstance->GetChipType()));
return GetRtExtErrCodeAndSetGlobalErr(RT_ERROR_FEATURE_NOT_SUPPORT);
}
return RT_ERROR_NONE;
}
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessLock()
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotProcessLock();
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessUnlock()
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotProcessUnlock();
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessGetState(rtProcessState* state)
{
const rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
PARAM_NULL_RETURN_ERROR_WITH_EXT_ERRCODE(state, RT_ERROR_INVALID_VALUE);
*state = GlobalStateManager::GetInstance().GetCurrentState();
return ACL_RT_SUCCESS;
}
static rtError_t ProcessBackup(const rtSnapShotBackupArgs* const args)
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotProcessBackup(args);
COND_RETURN_WITH_NOLOG(error == RT_ERROR_FEATURE_NOT_SUPPORT, ACL_ERROR_RT_FEATURE_NOT_SUPPORT);
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessBackup() { return ProcessBackup(nullptr); }
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessBackupWithArgs(const rtSnapShotBackupArgs* const args) { return ProcessBackup(args); }
static rtError_t ProcessRestore(const rtSnapShotRestoreArgs* const args)
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotProcessRestore(args);
COND_RETURN_WITH_NOLOG(error == RT_ERROR_FEATURE_NOT_SUPPORT, ACL_ERROR_RT_FEATURE_NOT_SUPPORT);
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessRestore() { return ProcessRestore(nullptr); }
VISIBILITY_DEFAULT
rtError_t rtSnapShotProcessRestoreWithArgs(const rtSnapShotRestoreArgs* const args) { return ProcessRestore(args); }
VISIBILITY_DEFAULT
rtError_t rtSnapShotCallbackRegister(rtSnapShotStage stage, rtSnapShotCallBack callback, void* args)
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotCallbackRegister(stage, callback, args);
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
VISIBILITY_DEFAULT
RTS_API rtError_t rtSnapShotCallbackUnregister(rtSnapShotStage stage, rtSnapShotCallBack callback)
{
rtError_t error = CheckSnapShotFeatureSupport();
if (error != RT_ERROR_NONE) {
return error;
}
ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance();
NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance);
error = apiSnapshotInstance->SnapShotCallbackUnregister(stage, callback);
ERROR_RETURN_WITH_EXT_ERRCODE(error);
return ACL_RT_SUCCESS;
}
#ifdef __cplusplus
}
#endif