| @@ -1,14 +1,29 @@ | |||
| 1 | # 21. 快照管理 | 1 | # 21. 快照管理 |
| 2 | 2 | ||
| 3 | -本章节描述 CANN Runtime 的快照管理接口,用于进程状态的锁定、备份、恢复及回调注册。 | 3 | +本章节描述 CANN Runtime 的快照管理接口,用于目标进程状态的锁定、备份、恢复、查询及回调注册。 |
| 4 | + | ||
| 5 | +跨进程调用链、线程生命周期和 Socket 协议等内部设计参见[跨进程 Snapshot 设计](../design/modules/snapshot/cross_process_snapshot.md)。 | ||
| 4 | 6 | ||
| 5 | - [`aclError aclrtSnapShotProcessLock(int pid, void* reserve)`](#aclrtSnapShotProcessLock):锁定当前进程,以阻止后续的运行时接口调用,包括Device设置/释放、内存的申请/释放/拷贝、Context/Stream/Event/Notify等资源的创建与销毁、以及部分任务下发接口。 | 7 | - [`aclError aclrtSnapShotProcessLock(int pid, void* reserve)`](#aclrtSnapShotProcessLock):锁定当前进程,以阻止后续的运行时接口调用,包括Device设置/释放、内存的申请/释放/拷贝、Context/Stream/Event/Notify等资源的创建与销毁、以及部分任务下发接口。 |
| 6 | - [`aclError aclrtSnapShotProcessBackup(int pid, aclrtSnapShotBackupArgs *args)`](#aclrtSnapShotProcessBackup):备份快照进程中的Device资源,并将Device资源保存在Host侧,以便后续恢复。针对当前进程,支持多次备份,以最后一次生效。 | 8 | - [`aclError aclrtSnapShotProcessBackup(int pid, aclrtSnapShotBackupArgs *args)`](#aclrtSnapShotProcessBackup):备份快照进程中的Device资源,并将Device资源保存在Host侧,以便后续恢复。针对当前进程,支持多次备份,以最后一次生效。 |
| 7 | - [`aclError aclrtSnapShotProcessRestore(int pid, aclrtSnapShotRestoreArgs *args)`](#aclrtSnapShotProcessRestore):恢复快照进程中的Device资源。根据备份好的Device资源进行恢复,从最后一次备份点进行恢复。 | 9 | - [`aclError aclrtSnapShotProcessRestore(int pid, aclrtSnapShotRestoreArgs *args)`](#aclrtSnapShotProcessRestore):恢复快照进程中的Device资源。根据备份好的Device资源进行恢复,从最后一次备份点进行恢复。 |
| 8 | - [`aclError aclrtSnapShotProcessUnlock(int pid, void* reserve)`](#aclrtSnapShotProcessUnlock):解锁Device上的当前进程,同时解除运行时接口的阻塞调用。 | 10 | - [`aclError aclrtSnapShotProcessUnlock(int pid, void* reserve)`](#aclrtSnapShotProcessUnlock):解锁Device上的当前进程,同时解除运行时接口的阻塞调用。 |
| 11 | +- [`aclError aclrtSnapshotProcessGetState(int pid, aclrtProcessState* state)`](#aclrtSnapshotProcessGetState):查询目标进程的 Snapshot 状态。 | ||
| 12 | +- [`aclError aclrtSnapshotProcessGetRestoreThreadId(int pid, int* tid)`](#aclrtSnapshotProcessGetRestoreThreadId):查询目标进程执行跨进程 Snapshot 操作的 Linux 线程ID。 | ||
| 9 | - [`aclError aclrtSnapShotCallbackRegister(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void *args)`](#aclrtSnapShotCallbackRegister):注册一个回调函数,该回调函数将在快照操作的不同阶段被调用。不支持重复注册。 | 13 | - [`aclError aclrtSnapShotCallbackRegister(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void *args)`](#aclrtSnapShotCallbackRegister):注册一个回调函数,该回调函数将在快照操作的不同阶段被调用。不支持重复注册。 |
| 10 | - [`aclError aclrtSnapShotCallbackUnregister(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback)`](#aclrtSnapShotCallbackUnregister):取消注册回调函数。取消注册之后,对应快照阶段将不再调用该回调函数。 | 14 | - [`aclError aclrtSnapShotCallbackUnregister(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback)`](#aclrtSnapShotCallbackUnregister):取消注册回调函数。取消注册之后,对应快照阶段将不再调用该回调函数。 |
| 11 | 15 | ||
| 16 | +## 跨进程调用约束 | ||
| 17 | + | ||
| 18 | +Lock、Backup、Restore、Unlock 以及本章两个查询接口均支持目标 PID。本进程调用直接执行;跨进程调用由目标进程中名为 `RT_SNAPSHOT` 的 Snapshot 服务线程执行,回调也在目标进程执行。 | ||
| 19 | + | ||
| 20 | +- 目标进程必须已初始化 Runtime、运行在支持 Snapshot 的产品上,并成功启动服务。接口不负责启动或初始化目标进程。 | ||
| 21 | +- 调用方与目标进程须位于同一台 Linux 主机,并处于可见的 PID 和文件系统环境中。调用方有效用户须与目标进程相同,或为 root;目录和 Socket 的访问权限、对端身份校验均须通过。 | ||
| 22 | +- 服务使用目标有效用户家目录下的 `.cann_snapshot/<pid>.sock`。目录所属用户应为目标用户、权限为 `0700`,Socket 权限为 `0600`。同名端点冲突时启动失败,请排查残留端点,不要删除运行中进程的端点。 | ||
| 23 | +- 接口同步返回操作结果。客户端等待结果不设固定超时;发送请求和服务端收发协议报文采用5秒的单次收发超时。连接断开或通信失败返回错误,已开始的操作不会因此取消。目标进程一直不返回且保持连接时,调用线程会持续等待,可由应用工作线程发起调用。 | ||
| 24 | +- 备份、恢复须遵循目标进程状态:`RUNNING → Lock → LOCKED → Backup → BACKED_UP → Restore → LOCKED → Unlock → RUNNING`。资源处理或回调失败可能进入 `FAILED`,此时应终止进程。CRIU 等进程快照工具的使用及驱动能力须满足部署环境要求;跨进程调用不替代进程快照工具。 | ||
| 25 | +- 状态查询反映调用被服务线程执行时的进程状态,不是任务进度或某次请求的完成凭据。服务串行执行请求,前面有耗时操作时,查询也可能等待。 | ||
| 26 | + | ||
| 12 | <a class="method_anchor" id="aclrtSnapShotProcessLock"></a> | 27 | <a class="method_anchor" id="aclrtSnapShotProcessLock"></a> |
| 13 | 28 | ||
| 14 | ## aclrtSnapShotProcessLock | 29 | ## aclrtSnapShotProcessLock |
| @@ -58,7 +73,7 @@ aclError aclrtSnapShotProcessLock(int pid, void* reserve) | |||
| 58 | 73 | ||
| 59 | | 参数名 | 输入/输出 | 说明 | | 74 | | 参数名 | 输入/输出 | 说明 | |
| 60 | | --- | :---: | --- | | 75 | | --- | :---: | --- | |
| 61 | -| pid | 输入 | 进程ID,当前只支持传入本进程的ID,不支持跨进程操作。 | | 76 | +| pid | 输入 | 目标进程ID,必须大于0。支持本进程及满足本章跨进程调用约束的其他进程。 | |
| 62 | | reserve | 输入 | 预留参数,当前只支持传入NULL。 | | 77 | | reserve | 输入 | 预留参数,当前只支持传入NULL。 | |
| 63 | 78 | ||
| 64 | ### 返回值说明 | 79 | ### 返回值说明 |
| @@ -118,7 +133,7 @@ aclError aclrtSnapShotProcessBackup(int pid, aclrtSnapShotBackupArgs *args) | |||
| 118 | 133 | ||
| 119 | | 参数名 | 输入/输出 | 说明 | | 134 | | 参数名 | 输入/输出 | 说明 | |
| 120 | | --- | :---: | --- | | 135 | | --- | :---: | --- | |
| 121 | -| pid | 输入 | 进程ID,当前只支持传入本进程的ID,不支持跨进程操作。 | | 136 | +| pid | 输入 | 目标进程ID,必须大于0。支持本进程及满足本章跨进程调用约束的其他进程。 | |
| 122 | | args | 输入 | 备份配置参数,类型为[aclrtSnapShotBackupArgs](25-04_Structs.md#aclrtSnapShotBackupArgs)。传入NULL时走旧Runtime备份流程;传入非NULL时走驱动Device内存快照流程,并要求驱动支持Device内存快照能力。 | | 137 | | args | 输入 | 备份配置参数,类型为[aclrtSnapShotBackupArgs](25-04_Structs.md#aclrtSnapShotBackupArgs)。传入NULL时走旧Runtime备份流程;传入非NULL时走驱动Device内存快照流程,并要求驱动支持Device内存快照能力。 | |
| 123 | 138 | ||
| 124 | ### 返回值说明 | 139 | ### 返回值说明 |
| @@ -178,7 +193,7 @@ aclError aclrtSnapShotProcessRestore(int pid, aclrtSnapShotRestoreArgs *args) | |||
| 178 | 193 | ||
| 179 | | 参数名 | 输入/输出 | 说明 | | 194 | | 参数名 | 输入/输出 | 说明 | |
| 180 | | --- | :---: | --- | | 195 | | --- | :---: | --- | |
| 181 | -| pid | 输入 | 进程ID,当前只支持传入本进程的ID,不支持跨进程操作。 | | 196 | +| pid | 输入 | 目标进程ID,必须大于0。支持本进程及满足本章跨进程调用约束的其他进程。 | |
| 182 | | args | 输入 | 恢复配置参数,类型为[aclrtSnapShotRestoreArgs](25-04_Structs.md#aclrtSnapShotRestoreArgs)。传入NULL时走旧Runtime恢复流程;传入非NULL时走驱动Device内存快照流程,并要求驱动支持Device内存快照能力。 | | 197 | | args | 输入 | 恢复配置参数,类型为[aclrtSnapShotRestoreArgs](25-04_Structs.md#aclrtSnapShotRestoreArgs)。传入NULL时走旧Runtime恢复流程;传入非NULL时走驱动Device内存快照流程,并要求驱动支持Device内存快照能力。 | |
| 183 | 198 | ||
| 184 | ### 返回值说明 | 199 | ### 返回值说明 |
| @@ -242,7 +257,7 @@ aclError aclrtSnapShotProcessUnlock(int pid, void* reserve) | |||
| 242 | 257 | ||
| 243 | | 参数名 | 输入/输出 | 说明 | | 258 | | 参数名 | 输入/输出 | 说明 | |
| 244 | | --- | :---: | --- | | 259 | | --- | :---: | --- | |
| 245 | -| pid | 输入 | 进程ID,当前只支持传入本进程的ID,不支持跨进程操作。 | | 260 | +| pid | 输入 | 目标进程ID,必须大于0。支持本进程及满足本章跨进程调用约束的其他进程。 | |
| 246 | | reserve | 输入 | 预留参数,当前只支持传入NULL。 | | 261 | | reserve | 输入 | 预留参数,当前只支持传入NULL。 | |
| 247 | 262 | ||
| 248 | ### 返回值说明 | 263 | ### 返回值说明 |
| @@ -370,3 +385,127 @@ aclError aclrtSnapShotCallbackUnregister(aclrtSnapShotStage stage, aclrtSnapShot | |||
| 370 | ### 返回值说明 | 385 | ### 返回值说明 |
| 371 | 386 | ||
| 372 | 返回0表示成功,返回其他值表示失败,请参见[aclError](25-01_aclError.md#aclError)。 | 387 | 返回0表示成功,返回其他值表示失败,请参见[aclError](25-01_aclError.md#aclError)。 |
| 388 | + | ||
| 389 | +<a class="method_anchor" id="aclrtSnapshotProcessGetState"></a> | ||
| 390 | + | ||
| 391 | +## aclrtSnapshotProcessGetState | ||
| 392 | + | ||
| 393 | +```c | ||
| 394 | +aclError aclrtSnapshotProcessGetState(int pid, aclrtProcessState* state) | ||
| 395 | +``` | ||
| 396 | + | ||
| 397 | +**须知:本接口为试验特性,后续版本可能会存在变更,不支持应用于生产环境中。** | ||
| 398 | + | ||
| 399 | +### 产品支持情况 | ||
| 400 | + | ||
| 401 | +<!-- npu="950" id1779 --> | ||
| 402 | +- Ascend 950PR系列产品:支持 | ||
| 403 | +- Ascend 950DT系列产品:不支持 | ||
| 404 | +<!-- end id1779 --> | ||
| 405 | +<!-- npu="A3" id1780 --> | ||
| 406 | +- Atlas A3系列产品:支持 | ||
| 407 | +<!-- end id1780 --> | ||
| 408 | +<!-- npu="910b" id1781 --> | ||
| 409 | +- Atlas A2系列产品:支持 | ||
| 410 | +<!-- end id1781 --> | ||
| 411 | +<!-- npu="310b" id1782 --> | ||
| 412 | +- Atlas 200I/500 A2推理产品:不支持 | ||
| 413 | +<!-- end id1782 --> | ||
| 414 | +<!-- npu="310p" id1783 --> | ||
| 415 | +- Atlas推理系列产品:不支持 | ||
| 416 | +<!-- end id1783 --> | ||
| 417 | +<!-- npu="910" id1784 --> | ||
| 418 | +- Atlas训练系列产品:不支持 | ||
| 419 | +<!-- end id1784 --> | ||
| 420 | +<!-- npu="IPV350" id1785 --> | ||
| 421 | +- IPV350:不支持 | ||
| 422 | +<!-- end id1785 --> | ||
| 423 | +<!-- npu="5162A" id5007 --> | ||
| 424 | +- Hisilicon5162A:不支持 | ||
| 425 | +<!-- end id5007 --> | ||
| 426 | +<!-- @ref: runtime/res/docs/zh/api_ref/21_snapshot_management_res.md#id7 --> | ||
| 427 | + | ||
| 428 | +### 功能说明 | ||
| 429 | + | ||
| 430 | +查询目标进程的Snapshot状态,支持本进程及满足本章约束的跨进程调用,不修改目标进程状态。 | ||
| 431 | + | ||
| 432 | +### 参数说明 | ||
| 433 | + | ||
| 434 | +| 参数名 | 输入/输出 | 说明 | | ||
| 435 | +| --- | :---: | --- | | ||
| 436 | +| pid | 输入 | 目标进程ID,必须大于0。 | | ||
| 437 | +| state | 输出 | 目标进程的Snapshot状态,类型及各状态含义参见[aclrtProcessState](25-02_Enumerations.md#aclrtProcessState)。不得为NULL,成功时写入状态,失败时保持原值。 | | ||
| 438 | + | ||
| 439 | +### 返回值说明 | ||
| 440 | + | ||
| 441 | +返回0表示成功,其他值表示失败,参见 [aclError](25-01_aclError.md#aclError)。 | ||
| 442 | + | ||
| 443 | +```c | ||
| 444 | +aclrtProcessState state; | ||
| 445 | +aclError ret = aclrtSnapshotProcessGetState(targetPid, &state); | ||
| 446 | +if (ret == ACL_SUCCESS && state == ACL_RT_PROCESS_STATE_BACKED_UP) { | ||
| 447 | + /* 已观测到备份完成状态;按进程快照方案执行后续步骤。 */ | ||
| 448 | +} | ||
| 449 | +``` | ||
| 450 | + | ||
| 451 | +<a class="method_anchor" id="aclrtSnapshotProcessGetRestoreThreadId"></a> | ||
| 452 | + | ||
| 453 | +## aclrtSnapshotProcessGetRestoreThreadId | ||
| 454 | + | ||
| 455 | +```c | ||
| 456 | +aclError aclrtSnapshotProcessGetRestoreThreadId(int pid, int* tid) | ||
| 457 | +``` | ||
| 458 | + | ||
| 459 | +**须知:本接口为试验特性,后续版本可能会存在变更,不支持应用于生产环境中。** | ||
| 460 | + | ||
| 461 | +### 产品支持情况 | ||
| 462 | + | ||
| 463 | +<!-- npu="950" id1786 --> | ||
| 464 | +- Ascend 950PR系列产品:支持 | ||
| 465 | +- Ascend 950DT系列产品:不支持 | ||
| 466 | +<!-- end id1786 --> | ||
| 467 | +<!-- npu="A3" id1787 --> | ||
| 468 | +- Atlas A3系列产品:支持 | ||
| 469 | +<!-- end id1787 --> | ||
| 470 | +<!-- npu="910b" id1788 --> | ||
| 471 | +- Atlas A2系列产品:支持 | ||
| 472 | +<!-- end id1788 --> | ||
| 473 | +<!-- npu="310b" id1789 --> | ||
| 474 | +- Atlas 200I/500 A2推理产品:不支持 | ||
| 475 | +<!-- end id1789 --> | ||
| 476 | +<!-- npu="310p" id1790 --> | ||
| 477 | +- Atlas推理系列产品:不支持 | ||
| 478 | +<!-- end id1790 --> | ||
| 479 | +<!-- npu="910" id1791 --> | ||
| 480 | +- Atlas训练系列产品:不支持 | ||
| 481 | +<!-- end id1791 --> | ||
| 482 | +<!-- npu="IPV350" id1792 --> | ||
| 483 | +- IPV350:不支持 | ||
| 484 | +<!-- end id1792 --> | ||
| 485 | +<!-- npu="5162A" id5008 --> | ||
| 486 | +- Hisilicon5162A:不支持 | ||
| 487 | +<!-- end id5008 --> | ||
| 488 | +<!-- @ref: runtime/res/docs/zh/api_ref/21_snapshot_management_res.md#id8 --> | ||
| 489 | + | ||
| 490 | +### 功能说明 | ||
| 491 | + | ||
| 492 | +返回目标进程中执行跨进程 Snapshot 操作的 `RT_SNAPSHOT` 线程的 Linux TID,可用于进程快照工具识别该线程;它不是调用进程线程的ID,也不是异步任务ID。服务线程尚未发布 TID、未成功启动或已停止时返回错误。Runtime 初始化不等待服务线程发布 TID,因此初始化后立即进行本地查询也可能返回服务不可用;调用方可稍后重试。 | ||
| 493 | + | ||
| 494 | +### 参数说明 | ||
| 495 | + | ||
| 496 | +| 参数名 | 输入/输出 | 说明 | | ||
| 497 | +| --- | :---: | --- | | ||
| 498 | +| pid | 输入 | 目标进程ID,必须大于0。 | | ||
| 499 | +| tid | 输出 | 目标进程Snapshot服务线程(线程名为`RT_SNAPSHOT`)的Linux线程ID,用于进程快照工具识别该线程。不得为NULL,成功时写入大于0的TID,失败时保持原值;取值以目标所处PID命名空间为准。 | | ||
| 500 | + | ||
| 501 | +### 返回值说明 | ||
| 502 | + | ||
| 503 | +返回0表示成功,其他值表示失败,参见 [aclError](25-01_aclError.md#aclError)。 | ||
| 504 | + | ||
| 505 | +```c | ||
| 506 | +int tid = 0; | ||
| 507 | +aclError ret = aclrtSnapshotProcessGetRestoreThreadId(targetPid, &tid); | ||
| 508 | +if (ret == ACL_SUCCESS) { | ||
| 509 | + /* 在目标 PID 命名空间的 /proc/<targetPid>/task/<tid> 中识别服务线程。 */ | ||
| 510 | +} | ||
| 511 | +``` | ||
| @@ -2347,10 +2347,11 @@ typedef enum aclrtRunMode { | |||
| 2347 | ## aclrtProcessState | 2347 | ## aclrtProcessState |
| 2348 | 2348 | ||
| 2349 | ```c | 2349 | ```c |
| 2350 | -// 预留枚举 | ||
| 2351 | typedef enum aclrtProcessState { | 2350 | typedef enum aclrtProcessState { |
| 2352 | ACL_RT_PROCESS_STATE_RUNNING = 0, | 2351 | ACL_RT_PROCESS_STATE_RUNNING = 0, |
| 2353 | ACL_RT_PROCESS_STATE_LOCKED, | 2352 | ACL_RT_PROCESS_STATE_LOCKED, |
| 2353 | + ACL_RT_PROCESS_STATE_BACKED_UP, | ||
| 2354 | + ACL_RT_PROCESS_STATE_FAILED, | ||
| 2354 | } aclrtProcessState; | 2355 | } aclrtProcessState; |
| 2355 | ``` | 2356 | ``` |
| 2356 | 2357 | ||
| @@ -2360,6 +2361,8 @@ typedef enum aclrtProcessState { | |||
| 2360 | | --- | --- | | 2361 | | --- | --- | |
| 2361 | | ACL_RT_PROCESS_STATE_RUNNING | 进程处于正常运行状态。 | | 2362 | | ACL_RT_PROCESS_STATE_RUNNING | 进程处于正常运行状态。 | |
| 2362 | | ACL_RT_PROCESS_STATE_LOCKED | 进程处于锁定状态,运行时接口调用会被阻塞,直至进程解除锁定。 | | 2363 | | ACL_RT_PROCESS_STATE_LOCKED | 进程处于锁定状态,运行时接口调用会被阻塞,直至进程解除锁定。 | |
| 2364 | +| ACL_RT_PROCESS_STATE_BACKED_UP | 进程资源已备份,尚未完成恢复。 | | ||
| 2365 | +| ACL_RT_PROCESS_STATE_FAILED | 备份、恢复或相关回调失败,进程不可再解锁,应结束目标进程。 | | ||
| 2363 | 2366 | ||
| 2364 | <br> | 2367 | <br> |
| 2365 | 2368 | ||
| @@ -85,6 +85,8 @@ ACL 层在 `args` 非 `NULL` 时会校验参数:`backupFlags` / `restoreFlags` | |||
| 85 | 85 | ||
| 86 | ### 整体设计思路 | 86 | ### 整体设计思路 |
| 87 | 87 | ||
| 88 | +原有 `ApiImplSnapshot` 实现整体位于 feature/snapshot,直接持有服务线程与回调表,本地调用和跨进程分发共用其业务流程。Runtime 通过已有 Snapshot API 对象,在初始化末尾启动服务,退出时停止并 join 服务线程。 | ||
| 89 | + | ||
| 88 | Snapshot 特性采用分层架构设计: | 90 | Snapshot 特性采用分层架构设计: |
| 89 | - **ACL 接口层**:提供 C 语言接口(aclrtSnapShot* 系列) | 91 | - **ACL 接口层**:提供 C 语言接口(aclrtSnapShot* 系列) |
| 90 | - **Runtime API 层**:ACL 参数为空时调用旧无参 `rtSnapShotProcessBackup` / `rtSnapShotProcessRestore` 保持旧流程兼容语义;ACL 参数非空时通过 `rtSnapShotProcessBackupWithArgs` / `rtSnapShotProcessRestoreWithArgs` 承接并贯通 `backupFlags` / `restoreFlags` | 92 | - **Runtime API 层**:ACL 参数为空时调用旧无参 `rtSnapShotProcessBackup` / `rtSnapShotProcessRestore` 保持旧流程兼容语义;ACL 参数非空时通过 `rtSnapShotProcessBackupWithArgs` / `rtSnapShotProcessRestoreWithArgs` 承接并贯通 `backupFlags` / `restoreFlags` |
| @@ -108,7 +110,7 @@ graph TB | |||
| 108 | 110 | ||
| 109 | subgraph Core["核心协调层"] | 111 | subgraph Core["核心协调层"] |
| 110 | GSM["GlobalStateManager<br/>进程状态管理"] | 112 | GSM["GlobalStateManager<br/>进程状态管理"] |
| 111 | - SCM["SnapshotCallbackManager<br/>回调管理单例"] | 113 | + SCM["SnapshotCallbackManager<br/>Snapshot功能对象持有的回调表"] |
| 112 | end | 114 | end |
| 113 | 115 | ||
| 114 | subgraph Process["备份恢复协调层"] | 116 | subgraph Process["备份恢复协调层"] |
| @@ -0,0 +1,98 @@ | |||
| 1 | +# 跨进程 Snapshot | ||
| 2 | + | ||
| 3 | +本文说明跨进程 Snapshot 的内部实现。接口参数、返回值、产品支持范围和调用示例参见[快照管理接口参考](../../../api_ref/21_snapshot_management.md)。下文 A 表示控制进程,B 表示目标进程。 | ||
| 4 | + | ||
| 5 | +## 调用方式 | ||
| 6 | + | ||
| 7 | +四个现有接口保留原签名,`pid` 表示目标进程 PID,必须为正数: | ||
| 8 | + | ||
| 9 | +```c | ||
| 10 | +aclrtSnapShotProcessLock(pid, NULL); | ||
| 11 | +aclrtSnapShotProcessBackup(pid, args); | ||
| 12 | +aclrtSnapShotProcessRestore(pid, args); | ||
| 13 | +aclrtSnapShotProcessUnlock(pid, NULL); | ||
| 14 | +``` | ||
| 15 | + | ||
| 16 | +`pid == 当前 PID` 走本地 RT 入口;其他 PID 通过本机 Unix Socket 请求目标进程的 `ApiImplSnapshot` 执行操作。控制进程不需要创建 Device 或 Context,也不会因远程请求或错误码转换而初始化自身 Runtime。目标 Runtime 必须已经初始化,且当前实现和芯片支持快照。 | ||
| 17 | + | ||
| 18 | +新增两个查询接口: | ||
| 19 | + | ||
| 20 | +```c | ||
| 21 | +aclError aclrtSnapshotProcessGetState(int pid, aclrtProcessState *state); | ||
| 22 | +aclError aclrtSnapshotProcessGetRestoreThreadId(int pid, int *tid); | ||
| 23 | +``` | ||
| 24 | + | ||
| 25 | +成功时才写出参。状态由全局状态管理器提供;线程 ID 返回执行快照操作的 Linux TID。本地查询经过 RT 入口的 Runtime 初始化与错误处理;芯片不支持时返回不支持,芯片已支持但服务尚未发布 TID、启动失败或已停止时,TID 查询返回内部运行错误并记录错误信息。 | ||
| 26 | + | ||
| 27 | +## 实现与调用链 | ||
| 28 | + | ||
| 29 | +```text | ||
| 30 | +ACL 四操作/两个查询 | ||
| 31 | + ├─ 本进程:RT C 入口 → ApiSnapshot → ApiImplSnapshot | ||
| 32 | + └─ 其他进程:SnapshotSocketCall | ||
| 33 | + → SnapshotSocketRequest → connect → SO_PEERCRED 校验 → 发送一条请求 | ||
| 34 | + → B 的 SnapshotService 服务线程 | ||
| 35 | + → SnapshotService::DispatchSnapshotRequest → SnapshotRequestHandler → ApiImplSnapshot | ||
| 36 | + → Snapshot 业务方法 / GlobalStateManager / Driver 处理 | ||
| 37 | + → 返回内部 RT 错误码和查询值 | ||
| 38 | + → A 的 SnapshotSocketCall 返回内部错误码 → ACL 入口转换为对外错误码 | ||
| 39 | +``` | ||
| 40 | + | ||
| 41 | +`feature/snapshot/snapshot_socket.cc` 集中实现客户端、公共收发、服务线程和命令分发,`feature/snapshot/snapshot_socket.hpp` 声明协议及服务对象。该实现编入公共库,A 无须加载目标芯片实现库或初始化自己的 Runtime 即可发起远程请求。服务通过 feature 定义的 `SnapshotRequestHandler` 调用 `ApiImplSnapshot` 中的共享业务流程,不依赖 API 目录的方法。原有 `ApiImplSnapshot` 实现整体放入 feature/snapshot,直接持有服务和回调表。Runtime 通过已有 `ApiSnapshot` 接口启动、停止服务。状态和 TID 查询同样由 feature 实现。传输返回内部 RT 错误码,由 ACL 入口统一转换,不为错误映射初始化控制进程的 Runtime。通用和 cmodel 的公共库及对应 UT 源列表登记该文件,v200/v201 复用公共库。tiny 和 arch5162 沿用 Snapshot 不支持的裁剪方式,不编入真实 Socket 实现;Socket 桩合并在已有 `api_impl_snapshot_stub.cc` 中,与 Snapshot 工厂桩一起只登记一次,为跨进程调用返回不支持,并提供不创建线程或 Socket 的生命周期桩。tiny 将合并桩编入公共库,arch5162 将其编入单库。 | ||
| 42 | + | ||
| 43 | +`ApiImplSnapshot` 直接持有 `SnapshotService` 和 `SnapshotCallbackManager`;Runtime 在 `Init()` 成功路径末尾调用已有 Snapshot API 对象的 `StartSnapshotService(GetChipType())`。Snapshot 功能检查芯片能力后启动服务;`mmCreateTask()` 成功后直接返回,不等待服务线程调度或 TID 就绪。服务线程先记录自身 Linux TID,再进入请求处理循环,因此远程查询被执行前 TID 已发布。本地查询可能遇到线程尚未发布 TID,此时返回现有服务不可用错误,失败时不修改出参。Runtime 初始化返回后,Keeper 才发布 `BOOT_DONE`。服务启动失败记录警告,Runtime 仍可完成初始化,本地接口保持可用,对应远程服务和 TID 查询不可用。 | ||
| 44 | + | ||
| 45 | +Runtime 自身的析构入口通过已有 Snapshot API 对象调用 `StopSnapshotService()` 停止服务;`Runtime::PrepareProcessExitNoThrow()` 也在设置 Runtime 退出状态和回收设备前停止服务,覆盖进程退出准备路径。Keeper 只负责 Runtime 的初始化和销毁,不管理 Snapshot 服务,也不在 `RuntimeIntf` 中暴露 Snapshot 生命周期接口。停止流程设置 `stopping_`、通过 eventfd 唤醒监听或收包等待、等待已开始的操作完成、join 线程,再清理 Socket 资源。回调管理器是 `ApiImplSnapshot` 成员,在服务停止前保持存活;成员声明顺序也保证服务成员先于回调管理器析构。Device Reset 不停止服务。 | ||
| 46 | + | ||
| 47 | +服务创建一个常驻线程,按连接串行处理请求;查询排在正在执行的操作之后,反映查询被执行时的进程状态,不提供任务进度或某次请求的完成凭据。`threadId_`、`stopping_` 和 `ownerPid_` 保留原子类型,分别用于共享 TID、停止状态和创建者身份。 | ||
| 48 | + | ||
| 49 | +实现面向 Linux Runtime 构建。通用 Socket 收发、连接、监听、线程创建与回收、PID/TID 查询使用 MMPA 的 `mm*` 接口。身份校验、停止通知、目录访问和带 CLOEXEC 标志的连接接收分别使用 `SO_PEERCRED`、`eventfd`、`open/openat/fstatat` 和 `accept4` 等系统接口。 | ||
| 50 | + | ||
| 51 | +## 参数与协议 | ||
| 52 | + | ||
| 53 | +一条连接只处理一条请求和一个响应,所有整数按网络字节序编码。 | ||
| 54 | + | ||
| 55 | +| 结构 | 字段 | 字节数 | | ||
| 56 | +| --- | --- | --- | | ||
| 57 | +| 请求头 SnapshotRequestHeader | magic、version、op、argsSize,均为 uint32_t | 16 | | ||
| 58 | +| 可选参数体 | uint32_t flags、char reserved[60] | 64 | | ||
| 59 | +| 响应 | magic、version、result、value,均为 uint32_t | 16 | | ||
| 60 | + | ||
| 61 | +magic 为 `0x534E4150`,版本为 1,双端严格检查版本相等;当前不承诺未知高版本兼容。操作枚举为 `rtSnapshotOp`,命令依次为 Lock=1、Backup=2、Restore=3、Unlock=4、GetState=5、GetRestoreThreadId=6。协议及 ACL/RT 参数的尺寸、字段偏移和完整参数透传由 UT 校验。 | ||
| 62 | + | ||
| 63 | +远程 Backup/Restore 的 `args == NULL` 对应 argsSize=0,向 impl 传入空参数;非空参数对应 argsSize=64,完整传递 flags 和 reserved,并在分发时复制为 RT 参数结构体。flags 只接受 0/1。其他命令只接受 argsSize=0。ACL 公开入口校验 PID 和参数后,由内部客户端编码并发送请求;服务端独立校验外部连接收到的协议和参数。impl 与传输内部返回 `RT_ERROR_*`,响应将结果编码为网络序 uint32_t,客户端解码为 `rtError_t`,由 ACL 的 `CallRemoteSnapshot` 在公开入口边界转换成对外错误码,转换不依赖调用方 Runtime 初始化。 | ||
| 64 | + | ||
| 65 | +SendSocket/ReceiveSocket 循环收发完整报文,处理短读、短写和 EINTR。连接 Socket 为阻塞模式:客户端发送请求设置 SO_SNDTIMEO=5 秒,接收结果设置 SO_RCVTIMEO=0,不设固定操作结果超时;服务端收发协议报文保留 5 秒的单次收发超时。这些协议超时不限制 Backup/Restore 的总执行时间。服务线程通过 WaitSocketReady 同时等待 Socket 和停止 eventfd,空闲或等待半包时可被停止通知唤醒。客户端使用阻塞式 connect,失败直接返回错误。服务端 accept 遇到 FD 或内存资源耗尽时,等待 100 毫秒再重试,等待期间可被停止通知唤醒。监听 backlog 由 `SNAPSHOT_LISTEN_BACKLOG=8` 表示,不代表并行执行 8 个操作。每个请求最多 80 字节,使用固定大小的接收缓冲区。非法版本、命令或长度及截断请求不执行操作。客户端收到完整响应并校验 magic/version 后处理返回值,无需等待连接关闭。 | ||
| 66 | + | ||
| 67 | +客户端读取完整响应后关闭本端连接并返回,因此成功 Backup 返回后不保留控制连接。接口同步等待,不引入异步任务或任务 ID;目标一直不返回且连接保持时,调用线程会持续等待,可由应用工作线程发起调用。客户端断开或通信失败而收不到响应只表示结果未知,不表示 B 已取消操作,不能自动重发。已开始的操作继续执行并提交实际状态。 | ||
| 68 | + | ||
| 69 | +## 身份和路径 | ||
| 70 | + | ||
| 71 | +B 使用 `getpwuid_r(geteuid())` 查找系统账号配置的家目录,在 `<home>/.cann_snapshot/<pid>.sock` 监听。家目录和 `.cann_snapshot` 路径允许为符号链接,检查实际打开目录的属主和权限。私有目录权限为 0700,Socket 节点为 0600。A 根据 `/proc/<pid>/status` 的有效 UID 查找 B 的家目录;root 控制普通用户进程时同样使用 B 的家目录。 | ||
| 72 | + | ||
| 73 | +建联后双方检查内核提供的 `SO_PEERCRED`。B 允许自身有效 UID 或 UID=0;A 校验服务端 UID 和目标 PID。报文不传递可伪造的身份字段。实际打开的目录必须为对应用户所有、类型和权限正确。已有端点冲突时启动失败,不删除已有文件;本进程成功绑定后,退出或后续初始化失败时按绑定路径清理 Socket 文件。fork 子进程不删除父进程的 Socket 文件。 | ||
| 74 | + | ||
| 75 | +首版支持相同 PID、user、mount 路径视图下的本机调用,不处理跨 namespace 的编号映射,不提供 TCP。无有效/可写家目录、Socket 路径过长、已有不合规目录等情况明确报错。 | ||
| 76 | + | ||
| 77 | +fork 子进程继承 FD 和内存,但不继承服务线程。继承对象检测创建者 PID,拒绝返回父进程 TID;清理子进程继承的 FD 时不 join 父线程、不删除父进程 Socket。fork 后直接复用父 Runtime 不在支持范围内。 | ||
| 78 | + | ||
| 79 | +## 并发和失败 | ||
| 80 | + | ||
| 81 | +远程请求由单个服务线程按连接串行执行;本地与远程操作均进入 `ApiImplSnapshot`,通过状态管理锁检查和更新状态。调用方负责协调本地调用与远程控制,避免并发操作同一目标进程;回调遵守“不在回调中调用 Runtime API”的约束。 | ||
| 82 | + | ||
| 83 | +Lock 先执行 LOCK_PRE 回调,再由 `GlobalStateManager::Locked()` 加锁校验并切换状态。服务按请求处理控制命令,不同控制方交替请求时均检查目标进程的当前状态。 | ||
| 84 | + | ||
| 85 | +| 状态 | 值 | 含义 | | ||
| 86 | +| --- | --- | --- | | ||
| 87 | +| RUNNING | 0 | 允许 Lock | | ||
| 88 | +| LOCKED | 1 | 已锁定,允许 Backup/Unlock;Restore 成功后回到此状态 | | ||
| 89 | +| BACKED_UP | 2 | 已备份,允许 Restore,保留原 Unlock 语义 | | ||
| 90 | +| FAILED | 3 | Backup/Restore 执行失败,禁止继续操作 | | ||
| 91 | + | ||
| 92 | +在 `ApiImplSnapshot` 中通过初始状态检查后,Backup/Restore 的 PRE 回调、资源 helper(包括其前置检查)或 POST 回调任一步失败,均由 impl 使用 `COND_PROC_RETURN_ERROR` 设置 FAILED 并返回原错误码,普通四操作拒绝继续。RT 入口的非法参数、芯片能力检查以及 impl 初始状态检查失败,在执行流程开始前返回。服务端的非法协议报文同样在分发前拒绝,不执行 Snapshot 操作。 | ||
| 93 | + | ||
| 94 | +OpMemoryRestore 使用对应 Device 的控制流执行恢复。回调在服务线程同步执行,使用服务线程自身的 TLS,并遵守“不在回调中调用 Runtime API”的契约。 | ||
| 95 | + | ||
| 96 | +## CRIU | ||
| 97 | + | ||
| 98 | +外部编排工具按以下顺序调用:Lock → Backup 成功 → CRIU dump;CRIU restore 恢复 Host 和线程后 → 重新建联 Restore → Unlock。CRIU 负责 Host 进程和线程的备份恢复,Runtime 通过 Snapshot 接口完成 NPU 资源的备份恢复。 | ||
| @@ -6010,12 +6010,41 @@ ACL_FUNC_VISIBILITY aclError aclrtMemGetAccess(void* virPtr, aclrtMemLocation* l | |||
| 6010 | typedef enum aclrtProcessState { | 6010 | typedef enum aclrtProcessState { |
| 6011 | ACL_RT_PROCESS_STATE_RUNNING = 0, | 6011 | ACL_RT_PROCESS_STATE_RUNNING = 0, |
| 6012 | ACL_RT_PROCESS_STATE_LOCKED, | 6012 | ACL_RT_PROCESS_STATE_LOCKED, |
| 6013 | + ACL_RT_PROCESS_STATE_BACKED_UP, | ||
| 6014 | + ACL_RT_PROCESS_STATE_FAILED, | ||
| 6013 | } aclrtProcessState; | 6015 | } aclrtProcessState; |
| 6014 | 6016 | ||
| 6017 | +/** | ||
| 6018 | + * @ingroup AscendCL | ||
| 6019 | + * @brief Query the snapshot state | ||
| 6020 | + * | ||
| 6021 | + * @param [in] pid Target process ID. | ||
| 6022 | + * @param [out] state Snapshot state. The output is unchanged on failure. | ||
| 6023 | + * | ||
| 6024 | + * @retval ACL_SUCCESS The function is successfully executed. | ||
| 6025 | + * @retval OtherValues Failure | ||
| 6026 | + */ | ||
| 6027 | +ACL_FUNC_VISIBILITY aclError aclrtSnapshotProcessGetState(int pid, aclrtProcessState* state); | ||
Outdated 请同步快照管理 API 参考。Lock、Backup、Restore、Unlock 的 pid 说明仍写着“只支持本进程,不支持跨进程操作”,也缺少本次新增的 GetState 和 GetRestoreThreadId。调用方按现有文档无法了解新能力,请一并更新跨进程约束和两个查询接口的用法。 ![]() ![]() | |||
| 6028 | + | ||
| 6029 | +/** | ||
| 6030 | + * @ingroup AscendCL | ||
| 6031 | + * @brief Query the snapshot service thread ID | ||
| 6032 | + * | ||
| 6033 | + * @param [in] pid Target process ID. | ||
| 6034 | + * @param [out] tid Linux thread ID. The output is unchanged on failure. | ||
| 6035 | + * | ||
| 6036 | + * @retval ACL_SUCCESS The function is successfully executed. | ||
| 6037 | + * @retval OtherValues Failure | ||
| 6038 | + */ | ||
| 6039 | +ACL_FUNC_VISIBILITY aclError aclrtSnapshotProcessGetRestoreThreadId(int pid, int* tid); | ||
| 6040 | + | ||
| 6015 | /** | 6041 | /** |
| 6016 | * @ingroup AscendCL | 6042 | * @ingroup AscendCL |
| 6017 | * @brief lock the NPU process which will block further aclrt api calls | 6043 | * @brief lock the NPU process which will block further aclrt api calls |
| 6018 | * | 6044 | * |
| 6045 | + * @param [in] pid Target process ID. | ||
| 6046 | + * @param [in] reserve Reserved parameter. Must be NULL. | ||
| 6047 | + * | ||
| 6019 | * @retval ACL_SUCCESS The function is successfully executed. | 6048 | * @retval ACL_SUCCESS The function is successfully executed. |
| 6020 | * @retval OtherValues Failure | 6049 | * @retval OtherValues Failure |
| 6021 | */ | 6050 | */ |
| @@ -6025,6 +6054,9 @@ ACL_FUNC_VISIBILITY aclError aclrtSnapShotProcessLock(int pid, void* reserve); | |||
| 6025 | * @ingroup AscendCL | 6054 | * @ingroup AscendCL |
| 6026 | * @brief unlock the NPU process and allow it to continue making aclrt api calls | 6055 | * @brief unlock the NPU process and allow it to continue making aclrt api calls |
| 6027 | * | 6056 | * |
| 6057 | + * @param [in] pid Target process ID. | ||
| 6058 | + * @param [in] reserve Reserved parameter. Must be NULL. | ||
| 6059 | + * | ||
| 6028 | * @retval ACL_SUCCESS The function is successfully executed. | 6060 | * @retval ACL_SUCCESS The function is successfully executed. |
| 6029 | * @retval OtherValues Failure | 6061 | * @retval OtherValues Failure |
| 6030 | */ | 6062 | */ |
| @@ -6033,6 +6065,8 @@ ACL_FUNC_VISIBILITY aclError aclrtSnapShotProcessUnlock(int pid, void* reserve); | |||
| 6033 | /** | 6065 | /** |
| 6034 | * @ingroup AscendCL | 6066 | * @ingroup AscendCL |
| 6035 | * @brief backup the NPU process | 6067 | * @brief backup the NPU process |
| 6068 | + * | ||
| 6069 | + * @param [in] pid Target process ID. | ||
| 6036 | * @param [in] args snapshot backup args. If args is NULL, the legacy Runtime backup flow is used. | 6070 | * @param [in] args snapshot backup args. If args is NULL, the legacy Runtime backup flow is used. |
| 6037 | * | 6071 | * |
| 6038 | * @retval ACL_SUCCESS The function is successfully executed. | 6072 | * @retval ACL_SUCCESS The function is successfully executed. |
| @@ -6043,6 +6077,8 @@ ACL_FUNC_VISIBILITY aclError aclrtSnapShotProcessBackup(int pid, aclrtSnapShotBa | |||
| 6043 | /** | 6077 | /** |
| 6044 | * @ingroup AscendCL | 6078 | * @ingroup AscendCL |
| 6045 | * @brief restore the NPU process from the last backup point | 6079 | * @brief restore the NPU process from the last backup point |
| 6080 | + * | ||
| 6081 | + * @param [in] pid Target process ID. | ||
| 6046 | * @param [in] args snapshot restore args. If args is NULL, the legacy Runtime restore flow is used. | 6082 | * @param [in] args snapshot restore args. If args is NULL, the legacy Runtime restore flow is used. |
| 6047 | * | 6083 | * |
| 6048 | * @retval ACL_SUCCESS The function is successfully executed. | 6084 | * @retval ACL_SUCCESS The function is successfully executed. |
| @@ -22,6 +22,7 @@ typedef enum { | |||
| 22 | RT_PROCESS_STATE_RUNNING = 0, | 22 | RT_PROCESS_STATE_RUNNING = 0, |
| 23 | RT_PROCESS_STATE_LOCKED, | 23 | RT_PROCESS_STATE_LOCKED, |
| 24 | RT_PROCESS_STATE_BACKED_UP, | 24 | RT_PROCESS_STATE_BACKED_UP, |
| 25 | + RT_PROCESS_STATE_FAILED, | ||
| 25 | RT_PROCESS_STATE_MAX | 26 | RT_PROCESS_STATE_MAX |
| 26 | } rtProcessState; | 27 | } rtProcessState; |
| 27 | 28 | ||
| @@ -66,6 +67,8 @@ RTS_API RT_DEPRECATED_MESSAGE(RT_RUNTIME_DEPRECATED_MESSAGE) rtError_t rtSnapSho | |||
| 66 | * @param [out] state, return the process state of a NPU process, the possible values for state are as follows: | 67 | * @param [out] state, return the process state of a NPU process, the possible values for state are as follows: |
| 67 | * RT_PROCESS_STATE_RUNNING : Default process state, | 68 | * RT_PROCESS_STATE_RUNNING : Default process state, |
| 68 | * RT_PROCESS_STATE_LOCKED : RTS API locks are taken so further RTS API calls will block | 69 | * RT_PROCESS_STATE_LOCKED : RTS API locks are taken so further RTS API calls will block |
| 70 | + * RT_PROCESS_STATE_BACKED_UP : Process resources have been backed up | ||
| 71 | + * RT_PROCESS_STATE_FAILED : A resource operation failed and the process cannot be unlocked | ||
| 69 | * @return ACL_RT_SUCCESS for ok, others failed | 72 | * @return ACL_RT_SUCCESS for ok, others failed |
| 70 | */ | 73 | */ |
| 71 | RTS_API RT_DEPRECATED_MESSAGE(RT_RUNTIME_DEPRECATED_MESSAGE) rtError_t rtSnapShotProcessGetState(rtProcessState* state); | 74 | RTS_API RT_DEPRECATED_MESSAGE(RT_RUNTIME_DEPRECATED_MESSAGE) rtError_t rtSnapShotProcessGetState(rtProcessState* state); |
| @@ -518,6 +518,8 @@ | |||
| 518 | _(aclError, aclrtSnapShotProcessUnlock, (int pid, void* reserve), (pid, reserve)) \ | 518 | _(aclError, aclrtSnapShotProcessUnlock, (int pid, void* reserve), (pid, reserve)) \ |
| 519 | _(aclError, aclrtSnapShotProcessBackup, (int pid, aclrtSnapShotBackupArgs* args), (pid, args)) \ | 519 | _(aclError, aclrtSnapShotProcessBackup, (int pid, aclrtSnapShotBackupArgs* args), (pid, args)) \ |
| 520 | _(aclError, aclrtSnapShotProcessRestore, (int pid, aclrtSnapShotRestoreArgs* args), (pid, args)) \ | 520 | _(aclError, aclrtSnapShotProcessRestore, (int pid, aclrtSnapShotRestoreArgs* args), (pid, args)) \ |
| 521 | + _(aclError, aclrtSnapshotProcessGetState, (int pid, aclrtProcessState* state), (pid, state)) \ | ||
| 522 | + _(aclError, aclrtSnapshotProcessGetRestoreThreadId, (int pid, int* tid), (pid, tid)) \ | ||
| 521 | _(aclError, aclrtSnapShotCallbackRegister, (aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void* args), \ | 523 | _(aclError, aclrtSnapShotCallbackRegister, (aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void* args), \ |
| 522 | (stage, callback, args)) \ | 524 | (stage, callback, args)) \ |
| 523 | _(aclError, aclrtSnapShotCallbackUnregister, (aclrtSnapShotStage stage, aclrtSnapShotCallBack callback), \ | 525 | _(aclError, aclrtSnapShotCallbackUnregister, (aclrtSnapShotStage stage, aclrtSnapShotCallBack callback), \ |
| @@ -8,6 +8,8 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | +#include <limits> | ||
| 12 | + | ||
| 11 | #include "utils/data_type_utils.h" | 13 | #include "utils/data_type_utils.h" |
| 12 | #include "acl_rt_impl.h" | 14 | #include "acl_rt_impl.h" |
| 13 | #include "runtime/rts/rts_snapshot.h" | 15 | #include "runtime/rts/rts_snapshot.h" |
| @@ -15,24 +17,46 @@ | |||
| 15 | #include "common/error_codes_inner.h" | 17 | #include "common/error_codes_inner.h" |
| 16 | #include "common/prof_reporter.h" | 18 | #include "common/prof_reporter.h" |
| 17 | #include "mmpa/mmpa_api.h" | 19 | #include "mmpa/mmpa_api.h" |
| 20 | +#include "securec.h" | ||
| 21 | +#include "snapshot_socket.hpp" | ||
| 22 | +#include "api_snapshot.hpp" | ||
| 23 | +#include "errcode_manage.hpp" | ||
| 18 | 24 | ||
| 19 | #ifdef __cplusplus | 25 | #ifdef __cplusplus |
| 20 | extern "C" { | 26 | extern "C" { |
| 21 | #endif | 27 | #endif |
| 22 | 28 | ||
| 29 | +static rtError_t CallRemoteSnapshot(int32_t pid, cce::runtime::rtSnapshotOp op, const void* args, uint32_t& value) | ||
| 30 | +{ | ||
| 31 | + cce::runtime::SnapshotMemoryArgs localArgs = {}; | ||
| 32 | + if (args != nullptr) { | ||
| 33 | + const auto ret = memcpy_s(&localArgs, sizeof(localArgs), args, sizeof(localArgs)); | ||
| 34 | + if (ret != EOK) { | ||
| 35 | + ACL_LOG_INNER_ERROR("Failed to copy snapshot arguments, result=%d.", ret); | ||
| 36 | + return ACL_ERROR_RT_INTERNAL_ERROR; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + const rtError_t error = cce::runtime::SnapshotSocketCall(pid, op, args == nullptr ? nullptr : &localArgs, value); | ||
| 40 | + // Socket implementations, including platform stubs, return internal RT codes. | ||
| 41 | + return cce::runtime::ErrorcodeManage::Instance().GetRtExtErrCode(error); | ||
| 42 | +} | ||
| 43 | + | ||
| 23 | aclError aclrtSnapShotProcessLockImpl(int pid, void* reserve) | 44 | aclError aclrtSnapShotProcessLockImpl(int pid, void* reserve) |
| 24 | { | 45 | { |
| 25 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessLock); | 46 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessLock); |
| 26 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessLock, pid=%d, reserve=%p", pid, reserve); | 47 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessLock, pid=%d, reserve=%p", pid, reserve); |
| 27 | 48 | ||
| 28 | - ACL_CHECK_INVALID_PARAM_WITH_REASON( | 49 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); |
| 29 | - pid != static_cast<int32_t>(mmGetPid()), pid, | ||
| 30 | - "pid must be current process pid, cross-process operation is not supported"); | ||
| 31 | 50 | ||
| 32 | ACL_CHECK_INVALID_PARAM_NO_VALUE( | 51 | ACL_CHECK_INVALID_PARAM_NO_VALUE( |
| 33 | reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr"); | 52 | reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr"); |
| 34 | 53 | ||
| 35 | - ACL_REQUIRES_RTS_OK(rtSnapShotProcessLock()); | 54 | + if (pid != mmGetPid()) { |
| 55 | + uint32_t value = 0U; | ||
| 56 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::LOCK, nullptr, value)); | ||
| 57 | + } else { | ||
| 58 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessLock()); | ||
| 59 | + } | ||
| 36 | 60 | ||
| 37 | ACL_LOG_INFO("successfully execute aclrtSnapShotProcessLock"); | 61 | ACL_LOG_INFO("successfully execute aclrtSnapShotProcessLock"); |
| 38 | return ACL_SUCCESS; | 62 | return ACL_SUCCESS; |
| @@ -43,14 +67,17 @@ aclError aclrtSnapShotProcessUnlockImpl(int pid, void* reserve) | |||
| 43 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessUnlock); | 67 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessUnlock); |
| 44 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessUnlock, pid=%d, reserve=%p", pid, reserve); | 68 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessUnlock, pid=%d, reserve=%p", pid, reserve); |
| 45 | 69 | ||
| 46 | - ACL_CHECK_INVALID_PARAM_WITH_REASON( | 70 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); |
| 47 | - pid != static_cast<int32_t>(mmGetPid()), pid, | ||
| 48 | - "pid must be current process pid, cross-process operation is not supported"); | ||
| 49 | 71 | ||
| 50 | ACL_CHECK_INVALID_PARAM_NO_VALUE( | 72 | ACL_CHECK_INVALID_PARAM_NO_VALUE( |
| 51 | reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr"); | 73 | reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr"); |
| 52 | 74 | ||
| 53 | - ACL_REQUIRES_RTS_OK(rtSnapShotProcessUnlock()); | 75 | + if (pid != mmGetPid()) { |
| 76 | + uint32_t value = 0U; | ||
| 77 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::UNLOCK, nullptr, value)); | ||
| 78 | + } else { | ||
| 79 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessUnlock()); | ||
| 80 | + } | ||
| 54 | 81 | ||
| 55 | ACL_LOG_INFO("successfully execute aclrtSnapShotProcessUnlock"); | 82 | ACL_LOG_INFO("successfully execute aclrtSnapShotProcessUnlock"); |
| 56 | return ACL_SUCCESS; | 83 | return ACL_SUCCESS; |
| @@ -61,13 +88,9 @@ aclError aclrtSnapShotProcessBackupImpl(int pid, aclrtSnapShotBackupArgs* args) | |||
| 61 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessBackup); | 88 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessBackup); |
| 62 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessBackup, pid=%d, args=%p", pid, args); | 89 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessBackup, pid=%d, args=%p", pid, args); |
| 63 | 90 | ||
| 64 | - ACL_CHECK_INVALID_PARAM_WITH_REASON( | 91 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); |
| 65 | - pid != static_cast<int32_t>(mmGetPid()), pid, | ||
| 66 | - "pid must be current process pid, cross-process operation is not supported"); | ||
| 67 | 92 | ||
| 68 | - if (args == nullptr) { | 93 | + if (args != nullptr) { |
| 69 | - ACL_REQUIRES_RTS_OK(rtSnapShotProcessBackup()); | ||
| 70 | - } else { | ||
| 71 | ACL_CHECK_INVALID_VALUE_WITH_DESC( | 94 | ACL_CHECK_INVALID_VALUE_WITH_DESC( |
| 72 | args->backupFlags <= ACL_RT_SNAPSHOT_BACKUP_INCLUDE_OPTIONAL, | 95 | args->backupFlags <= ACL_RT_SNAPSHOT_BACKUP_INCLUDE_OPTIONAL, |
| 73 | acl::GetSnapshotBackupFlagDesc(args->backupFlags).c_str(), "args->backupFlags", | 96 | acl::GetSnapshotBackupFlagDesc(args->backupFlags).c_str(), "args->backupFlags", |
| @@ -77,6 +100,17 @@ aclError aclrtSnapShotProcessBackupImpl(int pid, aclrtSnapShotBackupArgs* args) | |||
| 77 | static_cast<uint32_t>(ACL_RT_SNAPSHOT_BACKUP_INCLUDE_OPTIONAL)) | 100 | static_cast<uint32_t>(ACL_RT_SNAPSHOT_BACKUP_INCLUDE_OPTIONAL)) |
| 78 | .c_str(), | 101 | .c_str(), |
| 79 | ACL_ERROR_INVALID_PARAM); | 102 | ACL_ERROR_INVALID_PARAM); |
| 103 | + } | ||
| 104 | + | ||
| 105 | + if (pid != mmGetPid()) { | ||
| 106 | + uint32_t value = 0U; | ||
| 107 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::BACKUP, args, value)); | ||
| 108 | + return ACL_SUCCESS; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + if (args == nullptr) { | ||
| 112 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessBackup()); | ||
| 113 | + } else { | ||
| 80 | ACL_REQUIRES_RTS_OK(rtSnapShotProcessBackupWithArgs(reinterpret_cast<const rtSnapShotBackupArgs*>(args))); | 114 | ACL_REQUIRES_RTS_OK(rtSnapShotProcessBackupWithArgs(reinterpret_cast<const rtSnapShotBackupArgs*>(args))); |
| 81 | } | 115 | } |
| 82 | 116 | ||
| @@ -89,13 +123,9 @@ aclError aclrtSnapShotProcessRestoreImpl(int pid, aclrtSnapShotRestoreArgs* args | |||
| 89 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessRestore); | 123 | ACL_PROFILING_REG(acl::AclProfType::AclrtSnapShotProcessRestore); |
| 90 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessRestore, pid=%d, args=%p", pid, args); | 124 | ACL_LOG_INFO("start to execute aclrtSnapShotProcessRestore, pid=%d, args=%p", pid, args); |
| 91 | 125 | ||
| 92 | - ACL_CHECK_INVALID_PARAM_WITH_REASON( | 126 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); |
| 93 | - pid != static_cast<int32_t>(mmGetPid()), pid, | ||
| 94 | - "pid must be current process pid, cross-process operation is not supported"); | ||
| 95 | 127 | ||
| 96 | - if (args == nullptr) { | 128 | + if (args != nullptr) { |
| 97 | - ACL_REQUIRES_RTS_OK(rtSnapShotProcessRestore()); | ||
| 98 | - } else { | ||
| 99 | ACL_CHECK_INVALID_VALUE_WITH_DESC( | 129 | ACL_CHECK_INVALID_VALUE_WITH_DESC( |
| 100 | args->restoreFlags <= ACL_RT_SNAPSHOT_RESTORE_INCLUDE_OPTIONAL, | 130 | args->restoreFlags <= ACL_RT_SNAPSHOT_RESTORE_INCLUDE_OPTIONAL, |
| 101 | acl::GetSnapshotRestoreFlagDesc(args->restoreFlags).c_str(), "args->restoreFlags", | 131 | acl::GetSnapshotRestoreFlagDesc(args->restoreFlags).c_str(), "args->restoreFlags", |
| @@ -105,6 +135,17 @@ aclError aclrtSnapShotProcessRestoreImpl(int pid, aclrtSnapShotRestoreArgs* args | |||
| 105 | static_cast<uint32_t>(ACL_RT_SNAPSHOT_RESTORE_INCLUDE_OPTIONAL)) | 135 | static_cast<uint32_t>(ACL_RT_SNAPSHOT_RESTORE_INCLUDE_OPTIONAL)) |
| 106 | .c_str(), | 136 | .c_str(), |
| 107 | ACL_ERROR_INVALID_PARAM); | 137 | ACL_ERROR_INVALID_PARAM); |
| 138 | + } | ||
| 139 | + | ||
| 140 | + if (pid != mmGetPid()) { | ||
| 141 | + uint32_t value = 0U; | ||
| 142 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::RESTORE, args, value)); | ||
| 143 | + return ACL_SUCCESS; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + if (args == nullptr) { | ||
| 147 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessRestore()); | ||
| 148 | + } else { | ||
| 108 | ACL_REQUIRES_RTS_OK(rtSnapShotProcessRestoreWithArgs(reinterpret_cast<const rtSnapShotRestoreArgs*>(args))); | 149 | ACL_REQUIRES_RTS_OK(rtSnapShotProcessRestoreWithArgs(reinterpret_cast<const rtSnapShotRestoreArgs*>(args))); |
| 109 | } | 150 | } |
| 110 | 151 | ||
| @@ -112,6 +153,52 @@ aclError aclrtSnapShotProcessRestoreImpl(int pid, aclrtSnapShotRestoreArgs* args | |||
| 112 | return ACL_SUCCESS; | 153 | return ACL_SUCCESS; |
| 113 | } | 154 | } |
| 114 | 155 | ||
| 156 | +aclError aclrtSnapshotProcessGetStateImpl(int pid, aclrtProcessState* state) | ||
| 157 | +{ | ||
| 158 | + ACL_PROFILING_REG(acl::AclProfType::AclrtSnapshotProcessGetState); | ||
| 159 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); | ||
| 160 | + ACL_REQUIRES_NOT_NULL(state); | ||
| 161 | + uint32_t value = 0U; | ||
| 162 | + if (pid != mmGetPid()) { | ||
| 163 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::GET_STATE, nullptr, value)); | ||
| 164 | + } else { | ||
| 165 | + rtProcessState localState = RT_PROCESS_STATE_RUNNING; | ||
| 166 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessGetState(&localState)); | ||
| 167 | + value = static_cast<uint32_t>(localState); | ||
| 168 | + } | ||
| 169 | + if (value > static_cast<uint32_t>(ACL_RT_PROCESS_STATE_FAILED)) { | ||
| 170 | + ACL_LOG_INNER_ERROR( | ||
| 171 | + "Invalid snapshot process state: pid=%d, state=%u, expected range=[%u, %u].", pid, value, | ||
| 172 | + static_cast<uint32_t>(ACL_RT_PROCESS_STATE_RUNNING), static_cast<uint32_t>(ACL_RT_PROCESS_STATE_FAILED)); | ||
| 173 | + return ACL_ERROR_RT_INTERNAL_ERROR; | ||
| 174 | + } | ||
| 175 | + *state = static_cast<aclrtProcessState>(value); | ||
| 176 | + return ACL_SUCCESS; | ||
| 177 | +} | ||
| 178 | + | ||
| 179 | +aclError aclrtSnapshotProcessGetRestoreThreadIdImpl(int pid, int* tid) | ||
| 180 | +{ | ||
| 181 | + ACL_PROFILING_REG(acl::AclProfType::AclrtSnapshotProcessGetRestoreThreadId); | ||
| 182 | + ACL_CHECK_INVALID_PARAM_WITH_REASON(pid <= 0, pid, "pid must be positive"); | ||
| 183 | + ACL_REQUIRES_NOT_NULL(tid); | ||
| 184 | + uint32_t value = 0U; | ||
| 185 | + if (pid != mmGetPid()) { | ||
| 186 | + ACL_REQUIRES_RTS_OK(CallRemoteSnapshot(pid, cce::runtime::rtSnapshotOp::GET_RESTORE_THREAD_ID, nullptr, value)); | ||
| 187 | + } else { | ||
| 188 | + int32_t localTid = 0; | ||
| 189 | + ACL_REQUIRES_RTS_OK(rtSnapShotProcessGetRestoreThreadId(&localTid)); | ||
| 190 | + value = static_cast<uint32_t>(localTid); | ||
| 191 | + } | ||
| 192 | + if ((value == 0U) || (value > static_cast<uint32_t>(std::numeric_limits<int>::max()))) { | ||
| 193 | + ACL_LOG_INNER_ERROR( | ||
| 194 | + "Invalid snapshot restore thread ID: pid=%d, tid=%u, expected range=[1, %d].", pid, value, | ||
| 195 | + std::numeric_limits<int>::max()); | ||
| 196 | + return ACL_ERROR_RT_INTERNAL_ERROR; | ||
| 197 | + } | ||
| 198 | + *tid = static_cast<int>(value); | ||
尽量减少类型转换 ![]() ![]() | |||
| 199 | + return ACL_SUCCESS; | ||
| 200 | +} | ||
| 201 | + | ||
| 115 | aclError aclrtSnapShotCallbackRegisterImpl(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void* args) | 202 | aclError aclrtSnapShotCallbackRegisterImpl(aclrtSnapShotStage stage, aclrtSnapShotCallBack callback, void* args) |
| 116 | { | 203 | { |
| 117 | ACL_LOG_INFO("start to execute aclrtSnapShotCallbackRegister"); | 204 | ACL_LOG_INFO("start to execute aclrtSnapShotCallbackRegister"); |
| @@ -278,6 +278,8 @@ const std::map<AclProfType, std::string> PROF_TYPE_TO_NAMES = { | |||
| 278 | {AclProfType::AclrtSnapShotProcessUnlock, "aclrtSnapShotProcessUnlock"}, | 278 | {AclProfType::AclrtSnapShotProcessUnlock, "aclrtSnapShotProcessUnlock"}, |
| 279 | {AclProfType::AclrtSnapShotProcessBackup, "aclrtSnapShotProcessBackup"}, | 279 | {AclProfType::AclrtSnapShotProcessBackup, "aclrtSnapShotProcessBackup"}, |
| 280 | {AclProfType::AclrtSnapShotProcessRestore, "aclrtSnapShotProcessRestore"}, | 280 | {AclProfType::AclrtSnapShotProcessRestore, "aclrtSnapShotProcessRestore"}, |
| 281 | + {AclProfType::AclrtSnapshotProcessGetState, "aclrtSnapshotProcessGetState"}, | ||
| 282 | + {AclProfType::AclrtSnapshotProcessGetRestoreThreadId, "aclrtSnapshotProcessGetRestoreThreadId"}, | ||
| 281 | {AclProfType::AclrtFreeWithDevSync, "aclrtFreeWithDevSync"}, | 283 | {AclProfType::AclrtFreeWithDevSync, "aclrtFreeWithDevSync"}, |
| 282 | {AclProfType::AclrtFreeHostWithDevSync, "aclrtFreeHostWithDevSync"}, | 284 | {AclProfType::AclrtFreeHostWithDevSync, "aclrtFreeHostWithDevSync"}, |
| 283 | {AclProfType::AclrtCacheLastTaskOpInfo, "aclrtCacheLastTaskOpInfo"}, | 285 | {AclProfType::AclrtCacheLastTaskOpInfo, "aclrtCacheLastTaskOpInfo"}, |
| @@ -348,6 +348,8 @@ enum AclProfType { | |||
| 348 | AclrtDeviceResumeExecution, | 348 | AclrtDeviceResumeExecution, |
| 349 | AclrtSetDeviceFlags, | 349 | AclrtSetDeviceFlags, |
| 350 | AclrtGetDeviceFlags, | 350 | AclrtGetDeviceFlags, |
| 351 | + AclrtSnapshotProcessGetState, | ||
| 352 | + AclrtSnapshotProcessGetRestoreThreadId, | ||
| 351 | // this is the end, can not add after AclRtProfTypeEnd | 353 | // this is the end, can not add after AclRtProfTypeEnd |
| 352 | AclRtProfTypeEnd | 354 | AclRtProfTypeEnd |
| 353 | }; | 355 | }; |
| @@ -9,9 +9,9 @@ | |||
| 9 | */ | 9 | */ |
| 10 | #include "api_snapshot.hpp" | 10 | #include "api_snapshot.hpp" |
| 11 | #include "api_c.h" | 11 | #include "api_c.h" |
| 12 | -#include "global_state_manager.hpp" | ||
| 13 | #include "rts/rts.h" | 12 | #include "rts/rts.h" |
| 14 | #include "runtime.hpp" | 13 | #include "runtime.hpp" |
| 14 | +#include "osal.hpp" | ||
| 15 | 15 | ||
| 16 | using namespace cce::runtime; | 16 | using namespace cce::runtime; |
| 17 | 17 | ||
| @@ -71,12 +71,20 @@ rtError_t rtSnapShotProcessGetState(rtProcessState* state) | |||
| 71 | return error; | 71 | return error; |
| 72 | } | 72 | } |
| 73 | PARAM_NULL_RETURN_ERROR_WITH_EXT_ERRCODE(state, RT_ERROR_INVALID_VALUE); | 73 | PARAM_NULL_RETURN_ERROR_WITH_EXT_ERRCODE(state, RT_ERROR_INVALID_VALUE); |
| 74 | - *state = GlobalStateManager::GetInstance().GetCurrentState(); | 74 | + ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance(); |
| 75 | + NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance); | ||
| 76 | + const rtError_t queryError = apiSnapshotInstance->SnapShotProcessGetState(state); | ||
| 77 | + ERROR_RETURN_WITH_EXT_ERRCODE(queryError); | ||
| 75 | return ACL_RT_SUCCESS; | 78 | return ACL_RT_SUCCESS; |
| 76 | } | 79 | } |
| 77 | 80 | ||
| 78 | static rtError_t ProcessBackup(const rtSnapShotBackupArgs* const args) | 81 | static rtError_t ProcessBackup(const rtSnapShotBackupArgs* const args) |
| 79 | { | 82 | { |
| 83 | + if ((args != nullptr) && (args->backupFlags > 1U)) { | ||
魔鬼数字改下 ![]() ![]() | |||
| 84 | + RT_LOG_OUTER_MSG_INVALID_PARAM_WITH_DESC( | ||
| 85 | + "rtSnapShotProcessBackupWithArgs", args->backupFlags, "required memory only or include optional memory"); | ||
| 86 | + return GetRtExtErrCodeAndSetGlobalErr(RT_ERROR_INVALID_VALUE); | ||
| 87 | + } | ||
🟡 Medium Priority 新增的第 82-84 行与第 106-108 行在 失败模式:用户以 ![]() ![]() Incorrect? | |||
| 80 | rtError_t error = CheckSnapShotFeatureSupport(); | 88 | rtError_t error = CheckSnapShotFeatureSupport(); |
| 81 | if (error != RT_ERROR_NONE) { | 89 | if (error != RT_ERROR_NONE) { |
| 82 | return error; | 90 | return error; |
| @@ -98,6 +106,11 @@ rtError_t rtSnapShotProcessBackupWithArgs(const rtSnapShotBackupArgs* const args | |||
| 98 | 106 | ||
| 99 | static rtError_t ProcessRestore(const rtSnapShotRestoreArgs* const args) | 107 | static rtError_t ProcessRestore(const rtSnapShotRestoreArgs* const args) |
| 100 | { | 108 | { |
| 109 | + if ((args != nullptr) && (args->restoreFlags > 1U)) { | ||
魔鬼数字改下 ![]() ![]() | |||
| 110 | + RT_LOG_OUTER_MSG_INVALID_PARAM_WITH_DESC( | ||
| 111 | + "rtSnapShotProcessRestoreWithArgs", args->restoreFlags, "required memory only or include optional memory"); | ||
| 112 | + return GetRtExtErrCodeAndSetGlobalErr(RT_ERROR_INVALID_VALUE); | ||
| 113 | + } | ||
| 101 | rtError_t error = CheckSnapShotFeatureSupport(); | 114 | rtError_t error = CheckSnapShotFeatureSupport(); |
| 102 | if (error != RT_ERROR_NONE) { | 115 | if (error != RT_ERROR_NONE) { |
| 103 | return error; | 116 | return error; |
| @@ -147,6 +160,21 @@ RTS_API rtError_t rtSnapShotCallbackUnregister(rtSnapShotStage stage, rtSnapShot | |||
| 147 | return ACL_RT_SUCCESS; | 160 | return ACL_RT_SUCCESS; |
| 148 | } | 161 | } |
| 149 | 162 | ||
| 163 | +VISIBILITY_DEFAULT | ||
| 164 | +rtError_t rtSnapShotProcessGetRestoreThreadId(int32_t* tid) | ||
| 165 | +{ | ||
| 166 | + const rtError_t error = CheckSnapShotFeatureSupport(); | ||
| 167 | + if (error != RT_ERROR_NONE) { | ||
| 168 | + return error; | ||
| 169 | + } | ||
| 170 | + PARAM_NULL_RETURN_ERROR_WITH_EXT_ERRCODE(tid, RT_ERROR_INVALID_VALUE); | ||
| 171 | + ApiSnapshot* const apiSnapshotInstance = ApiSnapshot::Instance(); | ||
| 172 | + NULL_RETURN_ERROR_WITH_EXT_ERRCODE(apiSnapshotInstance); | ||
| 173 | + const rtError_t queryError = apiSnapshotInstance->SnapShotProcessGetRestoreThreadId(tid); | ||
| 174 | + ERROR_RETURN_WITH_EXT_ERRCODE(queryError); | ||
| 175 | + return ACL_RT_SUCCESS; | ||
| 176 | +} | ||
| 177 | + | ||
| 150 | #ifdef __cplusplus | 178 | #ifdef __cplusplus |
| 151 | } | 179 | } |
| 152 | #endif // __cplusplus | 180 | #endif // __cplusplus |
| @@ -8,6 +8,7 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | #include "api_impl_creator.hpp" | 10 | #include "api_impl_creator.hpp" |
| 11 | +#include "snapshot_socket.hpp" | ||
| 11 | 12 | ||
| 12 | namespace cce { | 13 | namespace cce { |
| 13 | namespace runtime { | 14 | namespace runtime { |
| @@ -18,5 +19,18 @@ ApiSnapshot* CreateImplSnapshotAndGet() { return nullptr; } | |||
| 18 | 19 | ||
| 19 | void DestroyImplSnapshot(ApiSnapshot*& apiImplSnapshot) { apiImplSnapshot = nullptr; } | 20 | void DestroyImplSnapshot(ApiSnapshot*& apiImplSnapshot) { apiImplSnapshot = nullptr; } |
| 20 | 21 | ||
| 22 | +rtError_t SnapshotSocketCall(int32_t, rtSnapshotOp, const SnapshotMemoryArgs*, uint32_t&) | ||
| 23 | +{ | ||
| 24 | + return RT_ERROR_FEATURE_NOT_SUPPORT; | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +SnapshotService::~SnapshotService() = default; | ||
| 28 | + | ||
| 29 | +rtError_t SnapshotService::StartSnapshotService(SnapshotRequestHandler*) { return RT_ERROR_FEATURE_NOT_SUPPORT; } | ||
| 30 | + | ||
| 31 | +void SnapshotService::StopSnapshotService() {} | ||
| 32 | + | ||
| 33 | +int32_t SnapshotService::GetSnapshotThreadId() const { return 0; } | ||
| 34 | + | ||
| 21 | } // namespace runtime | 35 | } // namespace runtime |
| 22 | } // namespace cce | 36 | } // namespace cce |
| @@ -12,11 +12,15 @@ | |||
| 12 | 12 | ||
| 13 | #include "base.hpp" | 13 | #include "base.hpp" |
| 14 | #include "rts_snapshot.h" | 14 | #include "rts_snapshot.h" |
| 15 | +#include "snapshot_socket.hpp" | ||
| 16 | + | ||
| 17 | +// Private Runtime bridge, not part of the deprecated installed RT headers. | ||
| 18 | +extern "C" rtError_t rtSnapShotProcessGetRestoreThreadId(int32_t* tid); | ||
| 15 | 19 | ||
| 16 | namespace cce { | 20 | namespace cce { |
| 17 | namespace runtime { | 21 | namespace runtime { |
| 18 | 22 | ||
| 19 | -class ApiSnapshot { | 23 | +class ApiSnapshot : public SnapshotRequestHandler { |
| 20 | public: | 24 | public: |
| 21 | ApiSnapshot() = default; | 25 | ApiSnapshot() = default; |
| 22 | virtual ~ApiSnapshot() = default; | 26 | virtual ~ApiSnapshot() = default; |
| @@ -28,6 +32,11 @@ public: | |||
| 28 | 32 | ||
| 29 | static ApiSnapshot* Instance(); | 33 | static ApiSnapshot* Instance(); |
| 30 | 34 | ||
| 35 | + virtual void StartSnapshotService(rtChipType_t) {} | ||
| 36 | + virtual void StopSnapshotService() {} | ||
| 37 | + | ||
| 38 | + virtual rtError_t SnapShotProcessGetState(rtProcessState* state) = 0; | ||
| 39 | + virtual rtError_t SnapShotProcessGetRestoreThreadId(int32_t* tid) = 0; | ||
| 31 | virtual rtError_t SnapShotProcessLock() = 0; | 40 | virtual rtError_t SnapShotProcessLock() = 0; |
| 32 | virtual rtError_t SnapShotProcessUnlock() = 0; | 41 | virtual rtError_t SnapShotProcessUnlock() = 0; |
| 33 | virtual rtError_t SnapShotProcessBackup(const rtSnapShotBackupArgs* const args) = 0; | 42 | virtual rtError_t SnapShotProcessBackup(const rtSnapShotBackupArgs* const args) = 0; |
| @@ -434,6 +434,10 @@ RUNTIME_API(snapshot, rtError_t, rtSnapShotProcessGetState, | |||
| 434 | (rtProcessState* state), | 434 | (rtProcessState* state), |
| 435 | (state), FEATURE_NOT_SUPPORT, EXPORT) | 435 | (state), FEATURE_NOT_SUPPORT, EXPORT) |
| 436 | 436 | ||
| 437 | +RUNTIME_API(snapshot, rtError_t, rtSnapShotProcessGetRestoreThreadId, | ||
| 438 | + (int32_t* tid), | ||
| 439 | + (tid), FEATURE_NOT_SUPPORT, EXPORT) | ||
🟡 Medium Priority 变更在 失败模式:stub 生成脚本(generate_runtime_api_stubs.py)以 catalog + 每平台 .def 决定生成哪些弱符号,清单不一致会导致新增接口在 arch5162/tiny 上缺少预期 stub(与 catalog 声明的 FEATURE_NOT_SUPPORT/EXPORT 不符),可能出现未定义符号或平台间导出符号集合不一致;同时 arch5162.cmake:36 的 建议:在 arch5162/tiny 的 unsupported_runtime_api.def 中补入 ![]() ![]() Incorrect? | |||
| 440 | + | ||
| 437 | RUNTIME_API(snapshot, rtError_t, rtSnapShotProcessBackup, | 441 | RUNTIME_API(snapshot, rtError_t, rtSnapShotProcessBackup, |
| 438 | (), | 442 | (), |
| 439 | (), FEATURE_NOT_SUPPORT, EXPORT) | 443 | (), FEATURE_NOT_SUPPORT, EXPORT) |
| @@ -138,7 +138,6 @@ set(libruntime_api_impl_src_files | |||
| 138 | ${RUNTIME_API_DIR}/impl/api_impl_creator.cc | 138 | ${RUNTIME_API_DIR}/impl/api_impl_creator.cc |
| 139 | ${RUNTIME_API_DIR}/impl/stub/api_impl_mbuf_stub.cc | 139 | ${RUNTIME_API_DIR}/impl/stub/api_impl_mbuf_stub.cc |
| 140 | ${RUNTIME_API_DIR}/impl/stub/api_impl_esched_stub.cc | 140 | ${RUNTIME_API_DIR}/impl/stub/api_impl_esched_stub.cc |
| 141 | - ${RUNTIME_API_DIR}/impl/stub/api_impl_snapshot_stub.cc | ||
| 142 | ${RUNTIME_API_DIR}/impl/stub/api_impl_rt_config_stub.cc | 141 | ${RUNTIME_API_DIR}/impl/stub/api_impl_rt_config_stub.cc |
| 143 | ${RUNTIME_API_DIR}/impl/stub/api_impl_device_topology_stub.cc | 142 | ${RUNTIME_API_DIR}/impl/stub/api_impl_device_topology_stub.cc |
| 144 | ${RUNTIME_API_DIR}/impl/stub/api_impl_ipc_memory_stub.cc | 143 | ${RUNTIME_API_DIR}/impl/stub/api_impl_ipc_memory_stub.cc |
| @@ -158,6 +157,7 @@ set(common_src_files | |||
| 158 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc | 157 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc |
| 159 | ${RUNTIME_CORE_DIR}/src/common/dev_info_manage.cc | 158 | ${RUNTIME_CORE_DIR}/src/common/dev_info_manage.cc |
| 160 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc | 159 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc |
| 160 | + ${RUNTIME_API_DIR}/impl/stub/api_impl_snapshot_stub.cc | ||
| 161 | ${RUNTIME_CORE_DIR}/src/common/error_code.cc | 161 | ${RUNTIME_CORE_DIR}/src/common/error_code.cc |
| 162 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc | 162 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc |
| 163 | ${RUNTIME_CORE_DIR}/src/common/global_state_manager.cc | 163 | ${RUNTIME_CORE_DIR}/src/common/global_state_manager.cc |
| @@ -329,6 +329,8 @@ ACL_API_STUB(aclrtSnapShotProcessBackup, RT_FEATURE_NOT_SUPPORT) | |||
| 329 | ACL_API_STUB(aclrtSnapShotProcessLock, RT_FEATURE_NOT_SUPPORT) | 329 | ACL_API_STUB(aclrtSnapShotProcessLock, RT_FEATURE_NOT_SUPPORT) |
| 330 | ACL_API_STUB(aclrtSnapShotProcessRestore, RT_FEATURE_NOT_SUPPORT) | 330 | ACL_API_STUB(aclrtSnapShotProcessRestore, RT_FEATURE_NOT_SUPPORT) |
| 331 | ACL_API_STUB(aclrtSnapShotProcessUnlock, RT_FEATURE_NOT_SUPPORT) | 331 | ACL_API_STUB(aclrtSnapShotProcessUnlock, RT_FEATURE_NOT_SUPPORT) |
| 332 | +ACL_API_STUB(aclrtSnapshotProcessGetRestoreThreadId, RT_FEATURE_NOT_SUPPORT) | ||
| 333 | +ACL_API_STUB(aclrtSnapshotProcessGetState, RT_FEATURE_NOT_SUPPORT) | ||
| 332 | ACL_API_STUB(aclrtStreamAbort, RT_FEATURE_NOT_SUPPORT) | 334 | ACL_API_STUB(aclrtStreamAbort, RT_FEATURE_NOT_SUPPORT) |
| 333 | ACL_API_STUB(aclrtStreamGetFlags, RT_FEATURE_NOT_SUPPORT) | 335 | ACL_API_STUB(aclrtStreamGetFlags, RT_FEATURE_NOT_SUPPORT) |
| 334 | ACL_API_STUB(aclrtStreamQuery, RT_FEATURE_NOT_SUPPORT) | 336 | ACL_API_STUB(aclrtStreamQuery, RT_FEATURE_NOT_SUPPORT) |
| @@ -462,6 +462,7 @@ RUNTIME_API_STUB(rtSnapShotCallbackRegister) | |||
| 462 | RUNTIME_API_STUB(rtSnapShotCallbackUnregister) | 462 | RUNTIME_API_STUB(rtSnapShotCallbackUnregister) |
| 463 | RUNTIME_API_STUB(rtSnapShotProcessBackup) | 463 | RUNTIME_API_STUB(rtSnapShotProcessBackup) |
| 464 | RUNTIME_API_STUB(rtSnapShotProcessBackupWithArgs) | 464 | RUNTIME_API_STUB(rtSnapShotProcessBackupWithArgs) |
| 465 | +RUNTIME_API_STUB(rtSnapShotProcessGetRestoreThreadId) | ||
| 465 | RUNTIME_API_STUB(rtSnapShotProcessGetState) | 466 | RUNTIME_API_STUB(rtSnapShotProcessGetState) |
| 466 | RUNTIME_API_STUB(rtSnapShotProcessLock) | 467 | RUNTIME_API_STUB(rtSnapShotProcessLock) |
| 467 | RUNTIME_API_STUB(rtSnapShotProcessRestore) | 468 | RUNTIME_API_STUB(rtSnapShotProcessRestore) |
| @@ -224,6 +224,7 @@ set(common_src_files_cmodel | |||
| 224 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc | 224 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc |
| 225 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc | 225 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc |
| 226 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc | 226 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc |
| 227 | + ${RUNTIME_FEATURE_DIR}/snapshot/snapshot_socket.cc | ||
| 227 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc | 228 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc |
| 228 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc | 229 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc |
| 229 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager.cc | 230 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager.cc |
| @@ -346,7 +347,7 @@ set(libruntime_cmodel_callback_files | |||
| 346 | set(libruntime_cmodel_src_files_optional | 347 | set(libruntime_cmodel_src_files_optional |
| 347 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc | 348 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc |
| 348 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc | 349 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc |
| 349 | - ${RUNTIME_API_DIR}/impl/api_impl_snapshot.cc | 350 | + ${RUNTIME_FEATURE_DIR}/snapshot/api_impl_snapshot.cc |
| 350 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc | 351 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc |
| 351 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc | 352 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc |
| 352 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc | 353 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc |
| @@ -117,6 +117,7 @@ set(common_src_files | |||
| 117 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc | 117 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc |
| 118 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc | 118 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc |
| 119 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc | 119 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc |
| 120 | + ${RUNTIME_FEATURE_DIR}/snapshot/snapshot_socket.cc | ||
| 120 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc | 121 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc |
| 121 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc | 122 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc |
| 122 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager.cc | 123 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager.cc |
| @@ -183,7 +184,7 @@ set(libruntime_callback_files | |||
| 183 | set(libruntime_src_files_optional | 184 | set(libruntime_src_files_optional |
| 184 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc | 185 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc |
| 185 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc | 186 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc |
| 186 | - ${RUNTIME_API_DIR}/impl/api_impl_snapshot.cc | 187 | + ${RUNTIME_FEATURE_DIR}/snapshot/api_impl_snapshot.cc |
| 187 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc | 188 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc |
| 188 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc | 189 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc |
| 189 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc | 190 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc |
| @@ -150,6 +150,7 @@ set(common_src_files | |||
| 150 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc | 150 | ${RUNTIME_CORE_DIR}/src/common/context_data_manage.cc |
| 151 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc | 151 | ${RUNTIME_CORE_DIR}/src/common/profiling_agent.cc |
| 152 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc | 152 | ${RUNTIME_CORE_DIR}/src/common/errcode_manage.cc |
| 153 | + ${RUNTIME_API_DIR}/impl/stub/api_impl_snapshot_stub.cc | ||
| 153 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc | 154 | ${RUNTIME_CORE_DIR}/src/common/error_message_manage.cc |
| 154 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc | 155 | ${RUNTIME_CORE_DIR}/src/common/task_fail_callback_data_manager.cc |
| 155 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager_stub.cc | 156 | ${RUNTIME_FEATURE_DIR}/xpu/xpu_task_fail_callback_data_manager_stub.cc |
| @@ -213,7 +214,6 @@ set(libruntime_callback_files | |||
| 213 | set(libruntime_src_files_include_for_tiny | 214 | set(libruntime_src_files_include_for_tiny |
| 214 | ${RUNTIME_API_DIR}/impl/stub/api_error_stub.cc | 215 | ${RUNTIME_API_DIR}/impl/stub/api_error_stub.cc |
| 215 | ${RUNTIME_API_DIR}/impl/stub/api_impl_stub.cc | 216 | ${RUNTIME_API_DIR}/impl/stub/api_impl_stub.cc |
| 216 | - ${RUNTIME_API_DIR}/impl/stub/api_impl_snapshot_stub.cc | ||
| 217 | ${RUNTIME_API_DIR}/impl/stub/api_impl_rt_config_stub.cc | 217 | ${RUNTIME_API_DIR}/impl/stub/api_impl_rt_config_stub.cc |
| 218 | ${RUNTIME_API_DIR}/impl/stub/api_impl_device_topology_stub.cc | 218 | ${RUNTIME_API_DIR}/impl/stub/api_impl_device_topology_stub.cc |
| 219 | ${RUNTIME_API_DIR}/impl/stub/api_impl_ipc_memory_stub.cc | 219 | ${RUNTIME_API_DIR}/impl/stub/api_impl_ipc_memory_stub.cc |
| @@ -122,6 +122,7 @@ RUNTIME_API_STUB(rtReduceAsyncWithCfgV2) | |||
| 122 | RUNTIME_API_STUB(rtSnapShotProcessLock) | 122 | RUNTIME_API_STUB(rtSnapShotProcessLock) |
| 123 | RUNTIME_API_STUB(rtSnapShotProcessUnlock) | 123 | RUNTIME_API_STUB(rtSnapShotProcessUnlock) |
| 124 | RUNTIME_API_STUB(rtSnapShotProcessGetState) | 124 | RUNTIME_API_STUB(rtSnapShotProcessGetState) |
| 125 | +RUNTIME_API_STUB(rtSnapShotProcessGetRestoreThreadId) | ||
| 125 | RUNTIME_API_STUB(rtSnapShotProcessBackup) | 126 | RUNTIME_API_STUB(rtSnapShotProcessBackup) |
| 126 | RUNTIME_API_STUB(rtSnapShotProcessBackupWithArgs) | 127 | RUNTIME_API_STUB(rtSnapShotProcessBackupWithArgs) |
| 127 | RUNTIME_API_STUB(rtSnapShotProcessRestore) | 128 | RUNTIME_API_STUB(rtSnapShotProcessRestore) |
| @@ -236,7 +236,7 @@ set(libruntime_v200_callback_files | |||
| 236 | set(libruntime_v200_src_files_exclude_for_tiny | 236 | set(libruntime_v200_src_files_exclude_for_tiny |
| 237 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc | 237 | ${RUNTIME_API_DIR}/impl/api_error_standard_soc.cc |
| 238 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc | 238 | ${RUNTIME_API_DIR}/impl/api_impl_standard_soc.cc |
| 239 | - ${RUNTIME_API_DIR}/impl/api_impl_snapshot.cc | 239 | + ${RUNTIME_FEATURE_DIR}/snapshot/api_impl_snapshot.cc |
| 240 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc | 240 | ${RUNTIME_API_DIR}/impl/api_impl_rt_config.cc |
| 241 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc | 241 | ${RUNTIME_API_DIR}/impl/api_impl_device_topology.cc |
| 242 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc | 242 | ${RUNTIME_API_DIR}/impl/api_impl_ipc_memory.cc |
| @@ -37,6 +37,9 @@ const char* GlobalStateManager::StateToString(const rtProcessState state) | |||
| 37 | case RT_PROCESS_STATE_BACKED_UP: | 37 | case RT_PROCESS_STATE_BACKED_UP: |
| 38 | result = "BACKED_UP"; | 38 | result = "BACKED_UP"; |
| 39 | break; | 39 | break; |
| 40 | + case RT_PROCESS_STATE_FAILED: | ||
| 41 | + result = "FAILED"; | ||
| 42 | + break; | ||
| 40 | default: | 43 | default: |
| 41 | break; | 44 | break; |
| 42 | } | 45 | } |
| @@ -17,6 +17,7 @@ | |||
| 17 | #include "mmpa/mmpa_api.h" | 17 | #include "mmpa/mmpa_api.h" |
| 18 | #include "driver/ascend_hal.h" | 18 | #include "driver/ascend_hal.h" |
| 19 | #include "api_impl.hpp" | 19 | #include "api_impl.hpp" |
| 20 | +#include "api_snapshot.hpp" | ||
| 20 | #include "api_impl_event.hpp" | 21 | #include "api_impl_event.hpp" |
| 21 | #include "context.hpp" | 22 | #include "context.hpp" |
| 22 | #include "engine_stream_observer.hpp" | 23 | #include "engine_stream_observer.hpp" |
| @@ -1738,6 +1739,9 @@ rtError_t Runtime::Init() | |||
| 1738 | error = InitCcFeature(); | 1739 | error = InitCcFeature(); |
| 1739 | COND_GOTO_ERROR_MSG_AND_ASSIGN_CALL( | 1740 | COND_GOTO_ERROR_MSG_AND_ASSIGN_CALL( |
| 1740 | ERR_MODULE_SYSTEM, error != RT_ERROR_NONE, INIT_FAIL, error, error, "Failed to initialize CC feature."); | 1741 | ERR_MODULE_SYSTEM, error != RT_ERROR_NONE, INIT_FAIL, error, error, "Failed to initialize CC feature."); |
| 1742 | + if (apiImplSnapshot_ != nullptr) { | ||
| 1743 | + apiImplSnapshot_->StartSnapshotService(GetChipType()); | ||
| 1744 | + } | ||
| 1741 | return error; | 1745 | return error; |
| 1742 | 1746 | ||
| 1743 | INIT_FAIL: | 1747 | INIT_FAIL: |
| @@ -2916,6 +2920,9 @@ bool Runtime::HasRuntimeExitHostState() const | |||
| 2916 | 2920 | ||
| 2917 | void Runtime::PrepareProcessExitNoThrow() | 2921 | void Runtime::PrepareProcessExitNoThrow() |
| 2918 | { | 2922 | { |
| 2923 | + if (apiImplSnapshot_ != nullptr) { | ||
| 2924 | + apiImplSnapshot_->StopSnapshotService(); | ||
| 2925 | + } | ||
| 2919 | SetRuntimeExiting(true); | 2926 | SetRuntimeExiting(true); |
| 2920 | isExiting_ = true; | 2927 | isExiting_ = true; |
| 2921 | ResetPersistingL2CacheSizes(); | 2928 | ResetPersistingL2CacheSizes(); |
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <fstream> | 12 | #include <fstream> |
| 13 | #include "driver/ascend_hal.h" | 13 | #include "driver/ascend_hal.h" |
| 14 | #include "api_impl.hpp" | 14 | #include "api_impl.hpp" |
| 15 | +#include "api_snapshot.hpp" | ||
| 15 | #include "api_impl_soma.hpp" | 16 | #include "api_impl_soma.hpp" |
| 16 | #include "api_event.hpp" | 17 | #include "api_event.hpp" |
| 17 | #include "context.hpp" | 18 | #include "context.hpp" |
| @@ -54,6 +55,9 @@ namespace runtime { | |||
| 54 | 55 | ||
| 55 | Runtime::~Runtime() | 56 | Runtime::~Runtime() |
| 56 | { | 57 | { |
| 58 | + if (apiImplSnapshot_ != nullptr) { | ||
| 59 | + apiImplSnapshot_->StopSnapshotService(); | ||
| 60 | + } | ||
| 57 | RT_LOG(RT_LOG_EVENT, "runtime destructor."); | 61 | RT_LOG(RT_LOG_EVENT, "runtime destructor."); |
| 58 | // ConstructRuntimeImpl can create a Runtime that has not run Init(); it must not flip process-wide exit state. | 62 | // ConstructRuntimeImpl can create a Runtime that has not run Init(); it must not flip process-wide exit state. |
| 59 | const bool hasRuntimeExitHostState = HasRuntimeExitHostState(); | 63 | const bool hasRuntimeExitHostState = HasRuntimeExitHostState(); |
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "mmpa/mmpa_api.h" | 13 | #include "mmpa/mmpa_api.h" |
| 14 | #include "driver/ascend_hal.h" | 14 | #include "driver/ascend_hal.h" |
| 15 | #include "api_impl.hpp" | 15 | #include "api_impl.hpp" |
| 16 | +#include "api_snapshot.hpp" | ||
| 16 | #include "api_impl_soma.hpp" | 17 | #include "api_impl_soma.hpp" |
| 17 | #include "api_event.hpp" | 18 | #include "api_event.hpp" |
| 18 | #include "context.hpp" | 19 | #include "context.hpp" |
| @@ -167,6 +168,9 @@ rtError_t Runtime::InitCcFeature() const | |||
| 167 | 168 | ||
| 168 | Runtime::~Runtime() | 169 | Runtime::~Runtime() |
| 169 | { | 170 | { |
| 171 | + if (apiImplSnapshot_ != nullptr) { | ||
| 172 | + apiImplSnapshot_->StopSnapshotService(); | ||
| 173 | + } | ||
| 170 | RT_LOG(RT_LOG_EVENT, "runtime destructor."); | 174 | RT_LOG(RT_LOG_EVENT, "runtime destructor."); |
| 171 | // ConstructRuntimeImpl can create a Runtime that has not run Init(); it must not flip process-wide exit state. | 175 | // ConstructRuntimeImpl can create a Runtime that has not run Init(); it must not flip process-wide exit state. |
| 172 | const bool hasRuntimeExitHostState = HasRuntimeExitHostState(); | 176 | const bool hasRuntimeExitHostState = HasRuntimeExitHostState(); |
| @@ -9,13 +9,12 @@ | |||
| 9 | */ | 9 | */ |
| 10 | #include "api_impl_snapshot.hpp" | 10 | #include "api_impl_snapshot.hpp" |
| 11 | 11 | ||
| 12 | -#include <mutex> | ||
| 13 | #include <new> | 12 | #include <new> |
| 14 | 13 | ||
| 15 | #include "api_impl_creator.hpp" | 14 | #include "api_impl_creator.hpp" |
| 16 | #include "error_message_manage.hpp" | 15 | #include "error_message_manage.hpp" |
| 17 | #include "global_state_manager.hpp" | 16 | #include "global_state_manager.hpp" |
| 18 | -#include "snapshot_callback_manager.hpp" | 17 | +#include "dev_info_manage.h" |
| 19 | #include "snapshot_process_helper.hpp" | 18 | #include "snapshot_process_helper.hpp" |
| 20 | 19 | ||
| 21 | namespace cce { | 20 | namespace cce { |
| @@ -25,7 +24,7 @@ bool IsImplSnapshotSupported() { return true; } | |||
| 25 | 24 | ||
| 26 | ApiSnapshot* CreateImplSnapshotAndGet() | 25 | ApiSnapshot* CreateImplSnapshotAndGet() |
| 27 | { | 26 | { |
| 28 | - ApiSnapshot* const apiImplSnapshot = new (std::nothrow) ApiImplSnapshot(); | 27 | + ApiImplSnapshot* const apiImplSnapshot = new (std::nothrow) ApiImplSnapshot(); |
| 29 | if (apiImplSnapshot == nullptr) { | 28 | if (apiImplSnapshot == nullptr) { |
| 30 | RT_LOG_OUTER_MSG_IMPL(ErrorCode::EE1013, sizeof(ApiImplSnapshot), "new"); | 29 | RT_LOG_OUTER_MSG_IMPL(ErrorCode::EE1013, sizeof(ApiImplSnapshot), "new"); |
| 31 | RT_LOG(RT_LOG_ERROR, "create ApiImplSnapshot failed."); | 30 | RT_LOG(RT_LOG_ERROR, "create ApiImplSnapshot failed."); |
| @@ -41,9 +40,38 @@ void DestroyImplSnapshot(ApiSnapshot*& apiImplSnapshot) | |||
| 41 | apiImplSnapshot = nullptr; | 40 | apiImplSnapshot = nullptr; |
| 42 | } | 41 | } |
| 43 | 42 | ||
| 43 | +void ApiImplSnapshot::StartSnapshotService(const rtChipType_t chipType) | ||
| 44 | +{ | ||
| 45 | + if (!IS_SUPPORT_CHIP_FEATURE(chipType, RtOptionalFeatureType::RT_FEATURE_DFX_PROCESS_SNAPSHOT)) { | ||
| 46 | + return; | ||
| 47 | + } | ||
| 48 | + const rtError_t error = service_.StartSnapshotService(this); | ||
| 49 | + if (error != RT_ERROR_NONE) { | ||
| 50 | + RT_LOG(RT_LOG_WARNING, "Snapshot socket service unavailable, ret=%d. Local snapshot remains available.", error); | ||
| 51 | + } | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +void ApiImplSnapshot::StopSnapshotService() { service_.StopSnapshotService(); } | ||
| 55 | + | ||
| 56 | +rtError_t ApiImplSnapshot::SnapShotProcessGetState(rtProcessState* state) | ||
| 57 | +{ | ||
| 58 | + *state = GlobalStateManager::GetInstance().GetCurrentState(); | ||
| 59 | + return RT_ERROR_NONE; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +rtError_t ApiImplSnapshot::SnapShotProcessGetRestoreThreadId(int32_t* tid) | ||
| 63 | +{ | ||
| 64 | + const int32_t serviceTid = service_.GetSnapshotThreadId(); | ||
| 65 | + COND_RETURN_ERROR_MSG_INNER( | ||
| 66 | + serviceTid <= 0, RT_ERROR_OS_HANDLE, "Snapshot socket service is unavailable, pid=%d, tid=%d.", mmGetPid(), | ||
| 67 | + serviceTid); | ||
| 68 | + *tid = serviceTid; | ||
| 69 | + return RT_ERROR_NONE; | ||
| 70 | +} | ||
| 71 | + | ||
| 44 | rtError_t ApiImplSnapshot::SnapShotProcessLock() | 72 | rtError_t ApiImplSnapshot::SnapShotProcessLock() |
| 45 | { | 73 | { |
| 46 | - rtError_t error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_LOCK_PRE); | 74 | + rtError_t error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_LOCK_PRE); |
| 47 | COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 75 | COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); |
| 48 | error = GlobalStateManager::GetInstance().Locked(); | 76 | error = GlobalStateManager::GetInstance().Locked(); |
| 49 | return error; | 77 | return error; |
| @@ -53,7 +81,7 @@ rtError_t ApiImplSnapshot::SnapShotProcessUnlock() | |||
| 53 | { | 81 | { |
| 54 | rtError_t error = GlobalStateManager::GetInstance().Unlocked(); | 82 | rtError_t error = GlobalStateManager::GetInstance().Unlocked(); |
| 55 | COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 83 | COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); |
| 56 | - error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_UNLOCK_POST); | 84 | + error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_UNLOCK_POST); |
| 57 | return error; | 85 | return error; |
| 58 | } | 86 | } |
| 59 | 87 | ||
| @@ -71,12 +99,18 @@ rtError_t ApiImplSnapshot::SnapShotProcessBackup(const rtSnapShotBackupArgs* con | |||
| 71 | return RT_ERROR_SNAPSHOT_BACKUP_FAILED; | 99 | return RT_ERROR_SNAPSHOT_BACKUP_FAILED; |
| 72 | } | 100 | } |
| 73 | 101 | ||
| 74 | - rtError_t error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_BACKUP_PRE); | 102 | + rtError_t error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_BACKUP_PRE); |
| 75 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 103 | + COND_PROC_RETURN_ERROR( |
| 104 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), | ||
| 105 | + "Snapshot operation failed, ret=%#x.", error); | ||
| 76 | error = cce::runtime::SnapShotProcessBackup(args); | 106 | error = cce::runtime::SnapShotProcessBackup(args); |
| 77 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 107 | + COND_PROC_RETURN_ERROR( |
| 78 | - error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_BACKUP_POST); | 108 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), |
| 79 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 109 | + "Snapshot operation failed, ret=%#x.", error); |
| 110 | + error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_BACKUP_POST); | ||
| 111 | + COND_PROC_RETURN_ERROR( | ||
| 112 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), | ||
| 113 | + "Snapshot operation failed, ret=%#x.", error); | ||
| 80 | globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_BACKED_UP); | 114 | globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_BACKED_UP); |
| 81 | return RT_ERROR_NONE; | 115 | return RT_ERROR_NONE; |
| 82 | } | 116 | } |
| @@ -96,12 +130,18 @@ rtError_t ApiImplSnapshot::SnapShotProcessRestore(const rtSnapShotRestoreArgs* c | |||
| 96 | return RT_ERROR_SNAPSHOT_RESTORE_FAILED; | 130 | return RT_ERROR_SNAPSHOT_RESTORE_FAILED; |
| 97 | } | 131 | } |
| 98 | 132 | ||
| 99 | - rtError_t error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_RESTORE_PRE); | 133 | + rtError_t error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_RESTORE_PRE); |
| 100 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 134 | + COND_PROC_RETURN_ERROR( |
| 135 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), | ||
| 136 | + "Snapshot operation failed, ret=%#x.", error); | ||
| 101 | error = cce::runtime::SnapShotProcessRestore(args); | 137 | error = cce::runtime::SnapShotProcessRestore(args); |
| 102 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 138 | + COND_PROC_RETURN_ERROR( |
| 103 | - error = SnapshotCallbackManager::GetInstance().InvokeCallbacks(RT_SNAPSHOT_RESTORE_POST); | 139 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), |
| 104 | - COND_RETURN_WITH_NOLOG((error != RT_ERROR_NONE), error); | 140 | + "Snapshot operation failed, ret=%#x.", error); |
| 141 | + error = callbacks_.InvokeCallbacks(RT_SNAPSHOT_RESTORE_POST); | ||
| 142 | + COND_PROC_RETURN_ERROR( | ||
| 143 | + error != RT_ERROR_NONE, error, globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_FAILED), | ||
| 144 | + "Snapshot operation failed, ret=%#x.", error); | ||
| 105 | globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_LOCKED); | 145 | globalStateManagerInstance.SetCurrentState(RT_PROCESS_STATE_LOCKED); |
| 106 | return RT_ERROR_NONE; | 146 | return RT_ERROR_NONE; |
| 107 | } | 147 | } |
| @@ -109,12 +149,12 @@ rtError_t ApiImplSnapshot::SnapShotProcessRestore(const rtSnapShotRestoreArgs* c | |||
| 109 | rtError_t ApiImplSnapshot::SnapShotCallbackRegister( | 149 | rtError_t ApiImplSnapshot::SnapShotCallbackRegister( |
| 110 | const rtSnapShotStage stage, const rtSnapShotCallBack callback, void* const args) | 150 | const rtSnapShotStage stage, const rtSnapShotCallBack callback, void* const args) |
| 111 | { | 151 | { |
| 112 | - return SnapshotCallbackManager::GetInstance().RegisterCallback(stage, callback, args); | 152 | + return callbacks_.RegisterCallback(stage, callback, args); |
| 113 | } | 153 | } |
| 114 | 154 | ||
| 115 | rtError_t ApiImplSnapshot::SnapShotCallbackUnregister(const rtSnapShotStage stage, const rtSnapShotCallBack callback) | 155 | rtError_t ApiImplSnapshot::SnapShotCallbackUnregister(const rtSnapShotStage stage, const rtSnapShotCallBack callback) |
| 116 | { | 156 | { |
| 117 | - return SnapshotCallbackManager::GetInstance().UnregisterCallback(stage, callback); | 157 | + return callbacks_.UnregisterCallback(stage, callback); |
| 118 | } | 158 | } |
| 119 | 159 | ||
| 120 | } // namespace runtime | 160 | } // namespace runtime |
| @@ -11,18 +11,28 @@ | |||
| 11 | #define CCE_RUNTIME_API_IMPL_SNAPSHOT_HPP | 11 | #define CCE_RUNTIME_API_IMPL_SNAPSHOT_HPP |
| 12 | 12 | ||
| 13 | #include "api_snapshot.hpp" | 13 | #include "api_snapshot.hpp" |
| 14 | +#include "snapshot_callback_manager.hpp" | ||
| 15 | +#include "snapshot_socket.hpp" | ||
| 14 | 16 | ||
| 15 | namespace cce { | 17 | namespace cce { |
| 16 | namespace runtime { | 18 | namespace runtime { |
| 17 | 19 | ||
| 18 | class ApiImplSnapshot : public ApiSnapshot { | 20 | class ApiImplSnapshot : public ApiSnapshot { |
| 19 | public: | 21 | public: |
| 22 | + void StartSnapshotService(rtChipType_t chipType) override; | ||
| 23 | + void StopSnapshotService() override; | ||
| 24 | + rtError_t SnapShotProcessGetState(rtProcessState* state) override; | ||
| 25 | + rtError_t SnapShotProcessGetRestoreThreadId(int32_t* tid) override; | ||
| 20 | rtError_t SnapShotProcessLock() override; | 26 | rtError_t SnapShotProcessLock() override; |
| 21 | rtError_t SnapShotProcessUnlock() override; | 27 | rtError_t SnapShotProcessUnlock() override; |
| 22 | rtError_t SnapShotProcessBackup(const rtSnapShotBackupArgs* const args) override; | 28 | rtError_t SnapShotProcessBackup(const rtSnapShotBackupArgs* const args) override; |
| 23 | rtError_t SnapShotProcessRestore(const rtSnapShotRestoreArgs* const args) override; | 29 | rtError_t SnapShotProcessRestore(const rtSnapShotRestoreArgs* const args) override; |
| 24 | rtError_t SnapShotCallbackRegister(rtSnapShotStage stage, rtSnapShotCallBack callback, void* args) override; | 30 | rtError_t SnapShotCallbackRegister(rtSnapShotStage stage, rtSnapShotCallBack callback, void* args) override; |
| 25 | rtError_t SnapShotCallbackUnregister(rtSnapShotStage stage, rtSnapShotCallBack callback) override; | 31 | rtError_t SnapShotCallbackUnregister(rtSnapShotStage stage, rtSnapShotCallBack callback) override; |
| 32 | + | ||
| 33 | +private: | ||
| 34 | + SnapshotCallbackManager callbacks_; | ||
| 35 | + SnapshotService service_; | ||
| 26 | }; | 36 | }; |
| 27 | 37 | ||
| 28 | } // namespace runtime | 38 | } // namespace runtime |
| @@ -160,9 +160,9 @@ rtError_t DeviceSnapshot::OpMemoryRestore(void) | |||
| 160 | 160 | ||
| 161 | auto vaAddrInfos = GetOpVirtualAddrs(); | 161 | auto vaAddrInfos = GetOpVirtualAddrs(); |
| 162 | size_t offset = 0U; | 162 | size_t offset = 0U; |
| 163 | - Context* curCtx = Runtime::Instance()->CurrentContext(); | 163 | + // A remote snapshot runs on the service thread, which has no business-thread Context TLS. |
确认一下,创建的时序,保证获得有效的stream ![]() ![]() | |||
| 164 | - CHECK_CONTEXT_VALID_WITH_RETURN(curCtx, RT_ERROR_CONTEXT_NULL); | 164 | + Stream* const stm = device_->GetCtrlSQStream(device_->PrimaryStream_()); |
| 165 | - Stream* stm = curCtx->GetCtrlSQStream(); | 165 | + NULL_PTR_RETURN_MSG(stm, RT_ERROR_STREAM_NULL); |
| 166 | for (auto it = vaAddrInfos.begin(); it != vaAddrInfos.end(); ++it) { | 166 | for (auto it = vaAddrInfos.begin(); it != vaAddrInfos.end(); ++it) { |
| 167 | void* addr = it->first; | 167 | void* addr = it->first; |
| 168 | const size_t size = it->second; | 168 | const size_t size = it->second; |
| @@ -20,12 +20,6 @@ static const char_t* g_snapShotStageStr[] = {"RT_SNAPSHOT_LOCK_PRE", "RT_SNA | |||
| 20 | "RT_SNAPSHOT_RESTORE_POST", "RT_SNAPSHOT_UNLOCK_POST"}; | 20 | "RT_SNAPSHOT_RESTORE_POST", "RT_SNAPSHOT_UNLOCK_POST"}; |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | -SnapshotCallbackManager& SnapshotCallbackManager::GetInstance() | ||
| 24 | -{ | ||
| 25 | - static SnapshotCallbackManager instance; | ||
| 26 | - return instance; | ||
| 27 | -} | ||
| 28 | - | ||
| 29 | const char_t* SnapshotCallbackManager::GetStageString(rtSnapShotStage stage) | 23 | const char_t* SnapshotCallbackManager::GetStageString(rtSnapShotStage stage) |
| 30 | { | 24 | { |
| 31 | if (stage >= RT_SNAPSHOT_LOCK_PRE && stage <= RT_SNAPSHOT_UNLOCK_POST) { | 25 | if (stage >= RT_SNAPSHOT_LOCK_PRE && stage <= RT_SNAPSHOT_UNLOCK_POST) { |
| @@ -26,7 +26,8 @@ struct SnapShotCallBackInfo { | |||
| 26 | 26 | ||
| 27 | class SnapshotCallbackManager { | 27 | class SnapshotCallbackManager { |
| 28 | public: | 28 | public: |
| 29 | - static SnapshotCallbackManager& GetInstance(); | 29 | + SnapshotCallbackManager() = default; |
| 30 | + ~SnapshotCallbackManager() = default; | ||
| 30 | 31 | ||
| 31 | SnapshotCallbackManager(const SnapshotCallbackManager&) = delete; | 32 | SnapshotCallbackManager(const SnapshotCallbackManager&) = delete; |
| 32 | SnapshotCallbackManager& operator=(const SnapshotCallbackManager&) = delete; | 33 | SnapshotCallbackManager& operator=(const SnapshotCallbackManager&) = delete; |
| @@ -36,9 +37,6 @@ public: | |||
| 36 | rtError_t InvokeCallbacks(rtSnapShotStage stage); | 37 | rtError_t InvokeCallbacks(rtSnapShotStage stage); |
| 37 | 38 | ||
| 38 | private: | 39 | private: |
| 39 | - SnapshotCallbackManager() = default; | ||
| 40 | - ~SnapshotCallbackManager() = default; | ||
| 41 | - | ||
| 42 | static const char_t* GetStageString(rtSnapShotStage stage); | 40 | static const char_t* GetStageString(rtSnapShotStage stage); |
| 43 | 41 | ||
| 44 | std::mutex mutex_; | 42 | std::mutex mutex_; |
| @@ -0,0 +1,560 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#include "snapshot_socket.hpp" | ||
snapshot_socket.hpp, snapshot_socket.cc 这些放到feature目录更合适,并不是属于api层 ![]() ![]() | |||
| 11 | +#include <arpa/inet.h> | ||
| 12 | +#include <cerrno> | ||
| 13 | +#include <fcntl.h> | ||
| 14 | +#include <fstream> | ||
| 15 | +#include <poll.h> | ||
| 16 | +#include <pwd.h> | ||
| 17 | +#include <sstream> | ||
| 18 | +#include <sys/eventfd.h> | ||
| 19 | +#include <sys/socket.h> | ||
| 20 | +#include <sys/stat.h> | ||
| 21 | +#include <sys/un.h> | ||
| 22 | +#include <unistd.h> | ||
| 23 | +#include <vector> | ||
| 24 | +#include "base.hpp" | ||
| 25 | +#include "error_message_manage.hpp" | ||
| 26 | +#include "osal.hpp" | ||
| 27 | +#include "securec.h" | ||
| 28 | +namespace cce { | ||
| 29 | +namespace runtime { | ||
| 30 | +namespace { | ||
| 31 | +constexpr uint32_t SNAPSHOT_MAGIC = 0x534E4150U; | ||
| 32 | +constexpr uint32_t SNAPSHOT_VERSION = 1U; | ||
| 33 | +constexpr int32_t IO_TIMEOUT_MS = 5000; | ||
| 34 | +constexpr int32_t SNAPSHOT_LISTEN_BACKLOG = 8; | ||
| 35 | +constexpr int32_t ACCEPT_RETRY_DELAY_MS = 100; | ||
| 36 | +constexpr size_t PASSWD_BUFFER_SIZE = 16U * 1024U; | ||
| 37 | +bool SetSocketTimeout(int32_t fd, int32_t receiveTimeoutMs, int32_t sendTimeoutMs) | ||
| 38 | +{ | ||
| 39 | + const struct timeval receiveTimeout = { | ||
| 40 | + static_cast<time_t>(receiveTimeoutMs / RT_MS_PER_S), | ||
| 41 | + static_cast<suseconds_t>((receiveTimeoutMs % RT_MS_PER_S) * RT_US_TO_MS)}; | ||
| 42 | + const struct timeval sendTimeout = { | ||
| 43 | + static_cast<time_t>(sendTimeoutMs / RT_MS_PER_S), | ||
| 44 | + static_cast<suseconds_t>((sendTimeoutMs % RT_MS_PER_S) * RT_US_TO_MS)}; | ||
| 45 | + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &receiveTimeout, sizeof(receiveTimeout)) != 0) { | ||
| 46 | + RT_LOG(RT_LOG_ERROR, "Set snapshot receive timeout failed, errno=%d.", errno); | ||
| 47 | + return false; | ||
| 48 | + } | ||
| 49 | + if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &sendTimeout, sizeof(sendTimeout)) != 0) { | ||
| 50 | + RT_LOG(RT_LOG_ERROR, "Set snapshot send timeout failed, errno=%d.", errno); | ||
| 51 | + return false; | ||
| 52 | + } | ||
| 53 | + return true; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +bool WaitSocketReady(int32_t fd, short events, int32_t stopFd, int32_t timeoutMs) | ||
| 57 | +{ | ||
| 58 | + struct pollfd fds[2] = {{fd, events, 0}, {stopFd, POLLIN, 0}}; | ||
| 59 | + int32_t ret; | ||
| 60 | + do { | ||
| 61 | + ret = poll(fds, 2U, timeoutMs); | ||
| 62 | + } while ((ret < 0) && (errno == EINTR)); | ||
| 63 | + if (ret <= 0) { | ||
| 64 | + return false; | ||
| 65 | + } | ||
| 66 | + // A stop notification takes priority over socket readiness. | ||
| 67 | + if (fds[1].revents != 0) { | ||
| 68 | + return false; | ||
| 69 | + } | ||
| 70 | + return (fds[0].revents & (events | POLLHUP)) != 0; | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +bool SendSocket(int32_t fd, const void* data, int32_t length, int32_t stopFd = EN_ERROR) | ||
| 74 | +{ | ||
| 75 | + const auto* bytes = static_cast<const char*>(data); | ||
| 76 | + int32_t offset = 0; | ||
| 77 | + while (offset < length) { | ||
| 78 | + if ((stopFd >= 0) && !WaitSocketReady(fd, POLLOUT, stopFd, IO_TIMEOUT_MS)) { | ||
| 79 | + return false; | ||
| 80 | + } | ||
| 81 | + const mmSsize_t ret = mmSocketSend(fd, const_cast<char*>(bytes + offset), length - offset, MSG_NOSIGNAL); | ||
| 82 | + if ((ret < 0) && (errno == EINTR)) { | ||
| 83 | + continue; | ||
| 84 | + } | ||
| 85 | + if (ret <= 0) { | ||
| 86 | + return false; | ||
| 87 | + } | ||
| 88 | + offset += static_cast<int32_t>(ret); | ||
| 89 | + } | ||
| 90 | + return true; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +bool ReceiveSocket(int32_t fd, void* data, int32_t length, int32_t stopFd = EN_ERROR) | ||
| 94 | +{ | ||
| 95 | + auto* bytes = static_cast<char*>(data); | ||
| 96 | + int32_t offset = 0; | ||
| 97 | + while (offset < length) { | ||
| 98 | + if ((stopFd >= 0) && !WaitSocketReady(fd, POLLIN, stopFd, IO_TIMEOUT_MS)) { | ||
| 99 | + return false; | ||
| 100 | + } | ||
| @@ -0,0 +1,99 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#ifndef CCE_RUNTIME_SNAPSHOT_SOCKET_HPP | ||
| 11 | +#define CCE_RUNTIME_SNAPSHOT_SOCKET_HPP | ||
| 12 | +#include <atomic> | ||
| 13 | +#include <cstdint> | ||
| 14 | +#include <string> | ||
| 15 | +#include "base.h" | ||
| 16 | +#include "rts_snapshot.h" | ||
| 17 | +#include "mmpa/mmpa_api.h" | ||
| 18 | +namespace cce { | ||
| 19 | +namespace runtime { | ||
| 20 | +// API-independent execution contract shared by the transport and the feature implementation. | ||
| 21 | +class SnapshotRequestHandler { | ||
| 22 | +public: | ||
| 23 | + virtual ~SnapshotRequestHandler() = default; | ||
| 24 | + virtual rtError_t SnapShotProcessGetState(rtProcessState* state) = 0; | ||
| 25 | + virtual rtError_t SnapShotProcessGetRestoreThreadId(int32_t* tid) = 0; | ||
| 26 | + virtual rtError_t SnapShotProcessLock() = 0; | ||
| 27 | + virtual rtError_t SnapShotProcessUnlock() = 0; | ||
| 28 | + virtual rtError_t SnapShotProcessBackup(const rtSnapShotBackupArgs* args) = 0; | ||
| 29 | + virtual rtError_t SnapShotProcessRestore(const rtSnapShotRestoreArgs* args) = 0; | ||
| 30 | +}; | ||
| 31 | +enum class rtSnapshotOp : uint32_t { | ||
| 32 | + LOCK = 1U, | ||
| 33 | + BACKUP = 2U, | ||
| 34 | + RESTORE = 3U, | ||
| 35 | + UNLOCK = 4U, | ||
| 36 | + GET_STATE = 5U, | ||
| 37 | + GET_RESTORE_THREAD_ID = 6U, | ||
| 38 | +}; | ||
| 39 | + | ||
| 40 | +// All integer fields are encoded in network byte order. No process pointers cross the socket. | ||
| 41 | +struct SnapshotRequestHeader { | ||
| 42 | + uint32_t magic; | ||
| 43 | + uint32_t version; | ||
| 44 | + uint32_t op; | ||
| 45 | + uint32_t argsSize; | ||
| 46 | +}; | ||
| 47 | + | ||
| 48 | +struct SnapshotMemoryArgs { | ||
| 49 | + uint32_t flags; | ||
| 50 | + char reserved[60]; | ||
| 51 | +}; | ||
| 52 | + | ||
| 53 | +struct SnapshotResponse { | ||
| 54 | + uint32_t magic; | ||
| 55 | + uint32_t version; | ||
| 56 | + uint32_t result; | ||
| 57 | + uint32_t value; | ||
| 58 | +}; | ||
| 59 | + | ||
| 60 | +// One request per connection. Returns internal RT codes; the API boundary converts them. | ||
| 61 | +rtError_t SnapshotSocketCall(int32_t pid, rtSnapshotOp op, const SnapshotMemoryArgs* args, uint32_t& value); | ||
| 62 | + | ||
| 63 | +class SnapshotService { | ||
| 64 | +public: | ||
| 65 | + SnapshotService() = default; | ||
| 66 | + ~SnapshotService(); | ||
| 67 | + SnapshotService(const SnapshotService&) = delete; | ||
| 68 | + SnapshotService& operator=(const SnapshotService&) = delete; | ||
| 69 | + rtError_t StartSnapshotService(SnapshotRequestHandler* handler); | ||
| 70 | + void StopSnapshotService(); | ||
| 71 | + int32_t GetSnapshotThreadId() const; | ||
| 72 | + | ||
| 73 | +private: | ||
| 74 | + static void* ThreadMain(void* arg); | ||
| 75 | + void Run(); | ||
| 76 | + void HandleConnection(int32_t fd); | ||
| 77 | + rtError_t DispatchSnapshotRequest(rtSnapshotOp op, const SnapshotMemoryArgs* args, uint32_t& value); | ||
| 78 | + void CloseResources(); | ||
| 79 | + // The feature owner keeps the handler alive until this service has joined. | ||
| 80 | + SnapshotRequestHandler* handler_ = nullptr; | ||
| 81 | + mmThread thread_{}; | ||
| 82 | + bool started_ = false; | ||
| 83 | + // Identifies the creator so a fork child cannot join its thread or remove its endpoint. | ||
| 84 | + std::atomic<int32_t> ownerPid_{0}; | ||
| 85 | + int32_t listenFd_ = EN_ERROR; | ||
| 86 | + // eventfd used to wake poll when Stop requests service shutdown. | ||
| 87 | + int32_t stopFd_ = EN_ERROR; | ||
| 88 | + // Private directory handle used by unlinkat to remove the socket endpoint. | ||
| 89 | + int32_t directoryFd_ = EN_ERROR; | ||
| 90 | + // Set only after bind succeeds; cleanup must not remove a conflicting endpoint. | ||
| 91 | + bool socketBound_ = false; | ||
| 92 | + std::string socketName_; | ||
| 93 | + std::atomic<int32_t> threadId_{0}; | ||
| 94 | + // Prevents new requests from executing after Stop; stopFd_ wakes a blocked poll. | ||
| 95 | + std::atomic<bool> stopping_{false}; | ||
| 96 | +}; | ||
| 97 | +} // namespace runtime | ||
| 98 | +} // namespace cce | ||
| 99 | +#endif | ||
| @@ -9,16 +9,11 @@ | |||
| 9 | */ | 9 | */ |
| 10 | #include "snapshot_callback_manager.hpp" | 10 | #include "snapshot_callback_manager.hpp" |
| 11 | #include "base.hpp" | 11 | #include "base.hpp" |
| 12 | +#include "runtime.hpp" | ||
| 12 | 13 | ||
| 13 | namespace cce { | 14 | namespace cce { |
| 14 | namespace runtime { | 15 | namespace runtime { |
| 15 | 16 | ||
| 16 | -SnapshotCallbackManager& SnapshotCallbackManager::GetInstance() | ||
| 17 | -{ | ||
| 18 | - static SnapshotCallbackManager instance; | ||
| 19 | - return instance; | ||
| 20 | -} | ||
| 21 | - | ||
| 22 | const char_t* SnapshotCallbackManager::GetStageString(rtSnapShotStage stage) | 17 | const char_t* SnapshotCallbackManager::GetStageString(rtSnapShotStage stage) |
| 23 | { | 18 | { |
| 24 | UNUSED(stage); | 19 | UNUSED(stage); |
| @@ -60,6 +60,7 @@ public: | |||
| 60 | virtual std::unique_ptr<const char_t[]> GetErrMgrErrorMessage(); | 60 | virtual std::unique_ptr<const char_t[]> GetErrMgrErrorMessage(); |
| 61 | virtual int Init(); | 61 | virtual int Init(); |
| 62 | virtual int32_t ReportPredefinedErrMsg(const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values); | 62 | virtual int32_t ReportPredefinedErrMsg(const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values); |
| 63 | + virtual int32_t ReportInnerErrMsg(const char* errorCode, const char* message); | ||
| 63 | 64 | ||
| 64 | // fe function | 65 | // fe function |
| 65 | virtual uint32_t InitializePlatformInfo(); | 66 | virtual uint32_t InitializePlatformInfo(); |
| @@ -623,6 +624,8 @@ public: | |||
| 623 | virtual rtError_t rtsGetErrorVerbose(uint32_t deviceId, rtErrorInfo* errorInfo); | 624 | virtual rtError_t rtsGetErrorVerbose(uint32_t deviceId, rtErrorInfo* errorInfo); |
| 624 | virtual rtError_t rtsRepairError(uint32_t deviceId, const rtErrorInfo* errorInfo); | 625 | virtual rtError_t rtsRepairError(uint32_t deviceId, const rtErrorInfo* errorInfo); |
| 625 | virtual rtError_t rtSnapShotProcessLock(); | 626 | virtual rtError_t rtSnapShotProcessLock(); |
| 627 | + virtual rtError_t rtSnapShotProcessGetState(rtProcessState* state); | ||
| 628 | + virtual rtError_t rtSnapShotProcessGetRestoreThreadId(int32_t* tid); | ||
| 626 | virtual rtError_t rtSnapShotProcessUnlock(); | 629 | virtual rtError_t rtSnapShotProcessUnlock(); |
| 627 | virtual rtError_t rtSnapShotProcessBackup(); | 630 | virtual rtError_t rtSnapShotProcessBackup(); |
| 628 | virtual rtError_t rtSnapShotProcessBackupWithArgs(const rtSnapShotBackupArgs* const args); | 631 | virtual rtError_t rtSnapShotProcessBackupWithArgs(const rtSnapShotBackupArgs* const args); |
| @@ -668,6 +671,7 @@ public: | |||
| 668 | MOCK_METHOD0(Init, int()); | 671 | MOCK_METHOD0(Init, int()); |
| 669 | MOCK_METHOD3( | 672 | MOCK_METHOD3( |
| 670 | ReportPredefinedErrMsg, int32_t(const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values)); | 673 | ReportPredefinedErrMsg, int32_t(const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values)); |
| 674 | + MOCK_METHOD2(ReportInnerErrMsg, int32_t(const char* errorCode, const char* message)); | ||
| 671 | 675 | ||
| 672 | // fe function | 676 | // fe function |
| 673 | MOCK_METHOD0(InitializePlatformInfo, uint32_t()); | 677 | MOCK_METHOD0(InitializePlatformInfo, uint32_t()); |
| @@ -1303,6 +1307,8 @@ public: | |||
| 1303 | MOCK_METHOD2(rtsGetErrorVerbose, rtError_t(uint32_t deviceId, rtErrorInfo* errorInfo)); | 1307 | MOCK_METHOD2(rtsGetErrorVerbose, rtError_t(uint32_t deviceId, rtErrorInfo* errorInfo)); |
| 1304 | MOCK_METHOD2(rtsRepairError, rtError_t(uint32_t deviceId, const rtErrorInfo* errorInfo)); | 1308 | MOCK_METHOD2(rtsRepairError, rtError_t(uint32_t deviceId, const rtErrorInfo* errorInfo)); |
| 1305 | MOCK_METHOD0(rtSnapShotProcessLock, rtError_t()); | 1309 | MOCK_METHOD0(rtSnapShotProcessLock, rtError_t()); |
| 1310 | + MOCK_METHOD1(rtSnapShotProcessGetState, rtError_t(rtProcessState* state)); | ||
| 1311 | + MOCK_METHOD1(rtSnapShotProcessGetRestoreThreadId, rtError_t(int32_t* tid)); | ||
| 1306 | MOCK_METHOD0(rtSnapShotProcessUnlock, rtError_t()); | 1312 | MOCK_METHOD0(rtSnapShotProcessUnlock, rtError_t()); |
| 1307 | MOCK_METHOD0(rtSnapShotProcessBackup, rtError_t()); | 1313 | MOCK_METHOD0(rtSnapShotProcessBackup, rtError_t()); |
| 1308 | MOCK_METHOD1(rtSnapShotProcessBackupWithArgs, rtError_t(const rtSnapShotBackupArgs* const args)); | 1314 | MOCK_METHOD1(rtSnapShotProcessBackupWithArgs, rtError_t(const rtSnapShotBackupArgs* const args)); |
| @@ -8,6 +8,8 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | +#include <cstdarg> | ||
| 12 | +#include <cstdio> | ||
| 11 | #include <cstring> | 13 | #include <cstring> |
| 12 | #include <vector> | 14 | #include <vector> |
| 13 | #include <string> | 15 | #include <string> |
| @@ -59,6 +61,13 @@ int32_t aclStub::ReportPredefinedErrMsg(const char* errorCode, const ErrorMsgArg | |||
| 59 | return 0; | 61 | return 0; |
| 60 | } | 62 | } |
| 61 | 63 | ||
| 64 | +int32_t aclStub::ReportInnerErrMsg(const char* errorCode, const char* message) | ||
| 65 | +{ | ||
| 66 | + (void)errorCode; | ||
| 67 | + (void)message; | ||
| 68 | + return 0; | ||
| 69 | +} | ||
| 70 | + | ||
| 62 | int error_message::ErrMgrInit(error_message::ErrorMessageMode error_mode) | 71 | int error_message::ErrMgrInit(error_message::ErrorMessageMode error_mode) |
| 63 | { | 72 | { |
| 64 | (void)error_mode; | 73 | (void)error_mode; |
| @@ -81,7 +90,15 @@ int32_t error_message::ReportPredefinedErrMsg( | |||
| 81 | int32_t error_message::ReportInnerErrMsg( | 90 | int32_t error_message::ReportInnerErrMsg( |
| 82 | const char* file_name, const char* func, uint32_t line, const char* error_code, const char* format, ...) | 91 | const char* file_name, const char* func, uint32_t line, const char* error_code, const char* format, ...) |
| 83 | { | 92 | { |
| 84 | - return 0; | 93 | + char message[4096] = {}; |
| 94 | + va_list args; | ||
| 95 | + va_start(args, format); | ||
| 96 | + const int result = vsnprintf(message, sizeof(message), format, args); | ||
| 97 | + va_end(args); | ||
| 98 | + if ((result < 0) || (static_cast<size_t>(result) >= sizeof(message))) { | ||
| 99 | + return -1; | ||
| 100 | + } | ||
| 101 | + return MockFunctionTest::aclStubInstance().ReportInnerErrMsg(error_code, message); | ||
| 85 | } | 102 | } |
| 86 | 103 | ||
| 87 | namespace fe { | 104 | namespace fe { |
| @@ -209,6 +226,9 @@ void MockFunctionTest::ResetToDefaultMock() | |||
| 209 | .WillByDefault([this](const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values) { | 226 | .WillByDefault([this](const char* errorCode, const ErrorMsgArgs& keys, const ErrorMsgArgs& values) { |
| 210 | return aclStub::ReportPredefinedErrMsg(errorCode, keys, values); | 227 | return aclStub::ReportPredefinedErrMsg(errorCode, keys, values); |
| 211 | }); | 228 | }); |
| 229 | + ON_CALL(*this, ReportInnerErrMsg).WillByDefault([this](const char* errorCode, const char* message) { | ||
| 230 | + return aclStub::ReportInnerErrMsg(errorCode, message); | ||
| 231 | + }); | ||
| 212 | ON_CALL(*this, rtHostMemMapCapabilities) | 232 | ON_CALL(*this, rtHostMemMapCapabilities) |
| 213 | .WillByDefault([this](uint32_t deviceId, rtHacType hacType, rtHostMemMapCapability* capabilities) { | 233 | .WillByDefault([this](uint32_t deviceId, rtHacType hacType, rtHostMemMapCapability* capabilities) { |
| 214 | return aclStub::rtHostMemMapCapabilities(deviceId, hacType, capabilities); | 234 | return aclStub::rtHostMemMapCapabilities(deviceId, hacType, capabilities); |
| @@ -2358,6 +2358,24 @@ rtError_t aclStub::rtMemGetAccess(void* virPtr, rtMemLocation* location, uint64_ | |||
| 2358 | 2358 | ||
| 2359 | rtError_t aclStub::rtSnapShotProcessLock() { return RT_ERROR_NONE; } | 2359 | rtError_t aclStub::rtSnapShotProcessLock() { return RT_ERROR_NONE; } |
| 2360 | 2360 | ||
| 2361 | +rtError_t aclStub::rtSnapShotProcessGetState(rtProcessState* state) | ||
| 2362 | +{ | ||
| 2363 | + if (state == nullptr) { | ||
| 2364 | + return ACL_ERROR_RT_PARAM_INVALID; | ||
| 2365 | + } | ||
| 2366 | + *state = RT_PROCESS_STATE_RUNNING; | ||
| 2367 | + return RT_ERROR_NONE; | ||
| 2368 | +} | ||
| 2369 | + | ||
| 2370 | +rtError_t aclStub::rtSnapShotProcessGetRestoreThreadId(int32_t* tid) | ||
| 2371 | +{ | ||
| 2372 | + if (tid == nullptr) { | ||
| 2373 | + return ACL_ERROR_RT_PARAM_INVALID; | ||
| 2374 | + } | ||
| 2375 | + *tid = 1234; | ||
| 2376 | + return RT_ERROR_NONE; | ||
| 2377 | +} | ||
| 2378 | + | ||
| 2361 | rtError_t aclStub::rtSnapShotProcessUnlock() { return RT_ERROR_NONE; } | 2379 | rtError_t aclStub::rtSnapShotProcessUnlock() { return RT_ERROR_NONE; } |
| 2362 | 2380 | ||
| 2363 | rtError_t aclStub::rtSnapShotProcessBackup() { return RT_ERROR_NONE; } | 2381 | rtError_t aclStub::rtSnapShotProcessBackup() { return RT_ERROR_NONE; } |
| @@ -2542,6 +2560,16 @@ rtError_t rtsRepairError(uint32_t deviceId, const rtErrorInfo* errorInfo) | |||
| 2542 | 2560 | ||
| 2543 | rtError_t rtSnapShotProcessLock() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessLock(); } | 2561 | rtError_t rtSnapShotProcessLock() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessLock(); } |
| 2544 | 2562 | ||
| 2563 | +rtError_t rtSnapShotProcessGetState(rtProcessState* state) | ||
| 2564 | +{ | ||
| 2565 | + return MockFunctionTest::aclStubInstance().rtSnapShotProcessGetState(state); | ||
| 2566 | +} | ||
| 2567 | + | ||
| 2568 | +extern "C" rtError_t rtSnapShotProcessGetRestoreThreadId(int32_t* tid) | ||
| 2569 | +{ | ||
| 2570 | + return MockFunctionTest::aclStubInstance().rtSnapShotProcessGetRestoreThreadId(tid); | ||
| 2571 | +} | ||
| 2572 | + | ||
| 2545 | rtError_t rtSnapShotProcessUnlock() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessUnlock(); } | 2573 | rtError_t rtSnapShotProcessUnlock() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessUnlock(); } |
| 2546 | 2574 | ||
| 2547 | rtError_t rtSnapShotProcessBackup() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessBackup(); } | 2575 | rtError_t rtSnapShotProcessBackup() { return MockFunctionTest::aclStubInstance().rtSnapShotProcessBackup(); } |
| @@ -4605,3 +4633,15 @@ rtError_t aclStub::rtGetDeviceFlags(uint32_t* flags) | |||
| 4605 | rtError_t rtSetDeviceFlags(uint32_t flags) { return MockFunctionTest::aclStubInstance().rtSetDeviceFlags(flags); } | 4633 | rtError_t rtSetDeviceFlags(uint32_t flags) { return MockFunctionTest::aclStubInstance().rtSetDeviceFlags(flags); } |
| 4606 | 4634 | ||
| 4607 | rtError_t rtGetDeviceFlags(uint32_t* flags) { return MockFunctionTest::aclStubInstance().rtGetDeviceFlags(flags); } | 4635 | rtError_t rtGetDeviceFlags(uint32_t* flags) { return MockFunctionTest::aclStubInstance().rtGetDeviceFlags(flags); } |
| 4636 | +namespace cce { | ||
| 4637 | +namespace runtime { | ||
| 4638 | +enum class rtSnapshotOp : uint32_t; | ||
| 4639 | +struct SnapshotMemoryArgs; | ||
| 4640 | +rtError_t g_snapshotSocketResult = RT_ERROR_OS_HANDLE; | ||
| 4641 | +// ACL tests isolate Runtime here; Runtime UT exercises the real socket implementation. | ||
| 4642 | +rtError_t SnapshotSocketCall(int32_t, rtSnapshotOp, const SnapshotMemoryArgs*, uint32_t&) | ||
| 4643 | +{ | ||
| 4644 | + return g_snapshotSocketResult; | ||
| 4645 | +} | ||
| 4646 | +} // namespace runtime | ||
| 4647 | +} // namespace cce | ||
| @@ -15,10 +15,16 @@ include_directories(${BASE_DIR}) | |||
| 15 | include_directories(${BASE_DIR}/include) | 15 | include_directories(${BASE_DIR}/include) |
| 16 | include_directories(${BASE_DIR}/include/external) | 16 | include_directories(${BASE_DIR}/include/external) |
| 17 | include_directories(${BASE_DIR}/include/external/acl) | 17 | include_directories(${BASE_DIR}/include/external/acl) |
| 18 | +include_directories(${BASE_DIR}/src/runtime/core/inc) | ||
| 19 | +include_directories(${BASE_DIR}/src/runtime/core/inc/common) | ||
| 20 | +include_directories(${BASE_DIR}/src/runtime/core/inc/soc) | ||
| 21 | +include_directories(${BASE_DIR}/src/runtime/feature/snapshot) | ||
| 22 | +include_directories(${BASE_DIR}/src/runtime/api/impl/inc) | ||
| 18 | 23 | ||
| 19 | include_directories(${BASE_DIR}/src/acl/common) | 24 | include_directories(${BASE_DIR}/src/acl/common) |
| 20 | include_directories(${TOP_DIR}/pkg_inc/runtime) | 25 | include_directories(${TOP_DIR}/pkg_inc/runtime) |
| 21 | include_directories(${TOP_DIR}/pkg_inc/runtime/runtime) | 26 | include_directories(${TOP_DIR}/pkg_inc/runtime/runtime) |
| 27 | +include_directories(${TOP_DIR}/pkg_inc/runtime/runtime/rts) | ||
| 22 | include_directories(${CMAKE_BINARY_DIR}) | 28 | include_directories(${CMAKE_BINARY_DIR}) |
| 23 | include_directories(${CMAKE_BINARY_DIR}/include_acl) | 29 | include_directories(${CMAKE_BINARY_DIR}/include_acl) |
| 24 | include_directories(${CMAKE_BINARY_DIR}/include_acl/tdt) | 30 | include_directories(${CMAKE_BINARY_DIR}/include_acl/tdt) |
| @@ -28,7 +34,10 @@ include_directories(${BASE_DIR}/include/dfx/base) | |||
| 28 | include_directories(${BASE_DIR}/pkg_inc/profiling) | 34 | include_directories(${BASE_DIR}/pkg_inc/profiling) |
| 29 | include_directories(${BASE_DIR}/pkg_inc/dump) | 35 | include_directories(${BASE_DIR}/pkg_inc/dump) |
| 30 | include_directories(${BASE_DIR}/pkg_inc/watchdog) | 36 | include_directories(${BASE_DIR}/pkg_inc/watchdog) |
| 37 | +include_directories(${BASE_DIR}/pkg_inc/trace) | ||
| 31 | include_directories(${TOP_DIR}/src/dfx/log/inc) | 38 | include_directories(${TOP_DIR}/src/dfx/log/inc) |
| 39 | +include_directories(${TOP_DIR}/src/dfx/error_manager) | ||
| 40 | +include_directories(${TOP_DIR}/src/dfx/trace/inc/toolchain) | ||
| 32 | include_directories(${TOP_DIR}/src/inc) | 41 | include_directories(${TOP_DIR}/src/inc) |
| 33 | include_directories(${BASE_DIR}/src/acl) | 42 | include_directories(${BASE_DIR}/src/acl) |
| 34 | include_directories(${BASE_DIR}/src/acl/aclrt) | 43 | include_directories(${BASE_DIR}/src/acl/aclrt) |
| @@ -36,6 +45,8 @@ include_directories(${BASE_DIR}/src/acl/aclrt_impl) | |||
| 36 | include_directories(${BASE_DIR}/tests/depends/) | 45 | include_directories(${BASE_DIR}/tests/depends/) |
| 37 | include_directories(${BASE_DIR}/include/dfx) | 46 | include_directories(${BASE_DIR}/include/dfx) |
| 38 | include_directories(${BASE_DIR}/src/runtime/inc/kernel) | 47 | include_directories(${BASE_DIR}/src/runtime/inc/kernel) |
| 48 | +include_directories(${BASE_DIR}/src/runtime/core/inc/utils) | ||
| 49 | +include_directories(${BASE_DIR}/pkg_inc/mmpa/sub_inc) | ||
| 39 | include_directories(${BASE_DIR}/include/dfx) | 50 | include_directories(${BASE_DIR}/include/dfx) |
| 40 | 51 | ||
| 41 | set(UT_FILES | 52 | set(UT_FILES |
| @@ -60,6 +71,7 @@ set(UT_FILES | |||
| 60 | ) | 71 | ) |
| 61 | 72 | ||
| 62 | set(SRC_FILES | 73 | set(SRC_FILES |
| 74 | + ${BASE_DIR}/src/runtime/core/src/common/errcode_manage.cc | ||
| 63 | ######### aclrt_impl ######### | 75 | ######### aclrt_impl ######### |
| 64 | ${BASE_DIR}/src/acl/common/log_inner.cpp | 76 | ${BASE_DIR}/src/acl/common/log_inner.cpp |
| 65 | ${BASE_DIR}/src/acl/common/prof_reporter.cpp | 77 | ${BASE_DIR}/src/acl/common/prof_reporter.cpp |
| @@ -111,6 +123,11 @@ set(SRC_FILES | |||
| 111 | ) | 123 | ) |
| 112 | 124 | ||
| 113 | ############ libut_ascendcl_src.a ############ | 125 | ############ libut_ascendcl_src.a ############ |
| 126 | +# Exercise snapshot diagnostics with production logging and error-reporting macros. | ||
| 127 | +set_source_files_properties(${BASE_DIR}/src/acl/aclrt_impl/snapshot.cpp | ||
| 128 | + PROPERTIES COMPILE_OPTIONS "-URUN_TEST;-UENABLE_DVPP_INTERFACE" | ||
| 129 | +) | ||
| 130 | + | ||
| 114 | add_library(ut_ascendcl_src STATIC | 131 | add_library(ut_ascendcl_src STATIC |
| 115 | ${SRC_FILES} | 132 | ${SRC_FILES} |
| 116 | ) | 133 | ) |
| @@ -118,6 +135,7 @@ add_library(ut_ascendcl_src STATIC | |||
| 118 | add_dependencies(ut_ascendcl_src copy_acl_headers_and_libs) | 135 | add_dependencies(ut_ascendcl_src copy_acl_headers_and_libs) |
| 119 | 136 | ||
| 120 | target_include_directories(ut_ascendcl_src PRIVATE | 137 | target_include_directories(ut_ascendcl_src PRIVATE |
| 138 | + ${BASE_DIR}/src/runtime/api/inc | ||
| 121 | ${CMAKE_BINARY_DIR}/include_acl | 139 | ${CMAKE_BINARY_DIR}/include_acl |
| 122 | ${CMAKE_BINARY_DIR}/include_acl/tdt | 140 | ${CMAKE_BINARY_DIR}/include_acl/tdt |
| 123 | ) | 141 | ) |
| @@ -140,7 +158,9 @@ target_compile_options(ut_ascendcl_src PRIVATE | |||
| 140 | target_link_libraries(ut_ascendcl_src PRIVATE | 158 | target_link_libraries(ut_ascendcl_src PRIVATE |
| 141 | $<BUILD_INTERFACE:intf_pub> | 159 | $<BUILD_INTERFACE:intf_pub> |
| 142 | $<BUILD_INTERFACE:c_sec_headers> | 160 | $<BUILD_INTERFACE:c_sec_headers> |
| 161 | + $<BUILD_INTERFACE:mmpa_headers> | ||
| 143 | c_sec | 162 | c_sec |
| 163 | + mmpa | ||
| 144 | json | 164 | json |
| 145 | ) | 165 | ) |
| 146 | 166 | ||
| @@ -21,6 +21,8 @@ | |||
| 21 | #include "mmpa/mmpa_api.h" | 21 | #include "mmpa/mmpa_api.h" |
| 22 | 22 | ||
| 23 | #include "acl/acl.h" | 23 | #include "acl/acl.h" |
| 24 | +#include "snapshot_socket.hpp" | ||
| 25 | +#include "errcode_manage.hpp" | ||
| 24 | #include "acl/error_codes/rt_error_codes.h" | 26 | #include "acl/error_codes/rt_error_codes.h" |
| 25 | #include "common/log_inner.h" | 27 | #include "common/log_inner.h" |
| 26 | #include "acl/acl_rt.h" | 28 | #include "acl/acl_rt.h" |
| @@ -4974,9 +4976,14 @@ TEST_F(UTEST_ACL_Runtime, SnapShotProcessBackup_invalid_flags) | |||
| 4974 | EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessBackupWithArgs(_)).Times(0); | 4976 | EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessBackupWithArgs(_)).Times(0); |
| 4975 | 4977 | ||
| 4976 | aclrtSnapShotBackupArgs args = {}; | 4978 | aclrtSnapShotBackupArgs args = {}; |
| 4977 | - args.backupFlags = 2U; | 4979 | + for (const uint32_t flags : {2U, UINT32_MAX}) { |
| 4978 | - aclError ret = aclrtSnapShotProcessBackup(static_cast<int>(mmGetPid()), &args); | 4980 | + SCOPED_TRACE(flags); |
| 4979 | - EXPECT_EQ(ret, ACL_ERROR_INVALID_PARAM); | 4981 | + args.backupFlags = flags; |
| 4982 | + for (const int32_t pid : {static_cast<int32_t>(mmGetPid()), INT32_MAX}) { | ||
| 4983 | + SCOPED_TRACE(pid); | ||
| 4984 | + EXPECT_EQ(aclrtSnapShotProcessBackup(pid, &args), ACL_ERROR_INVALID_PARAM); | ||
| 4985 | + } | ||
| 4986 | + } | ||
| 4980 | } | 4987 | } |
| 4981 | 4988 | ||
| 4982 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessRestore_success) | 4989 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessRestore_success) |
| @@ -5002,9 +5009,14 @@ TEST_F(UTEST_ACL_Runtime, SnapShotProcessRestore_invalid_flags) | |||
| 5002 | EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessRestoreWithArgs(_)).Times(0); | 5009 | EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessRestoreWithArgs(_)).Times(0); |
| 5003 | 5010 | ||
| 5004 | aclrtSnapShotRestoreArgs args = {}; | 5011 | aclrtSnapShotRestoreArgs args = {}; |
| 5005 | - args.restoreFlags = 2U; | 5012 | + for (const uint32_t flags : {2U, UINT32_MAX}) { |
| 5006 | - aclError ret = aclrtSnapShotProcessRestore(static_cast<int>(mmGetPid()), &args); | 5013 | + SCOPED_TRACE(flags); |
| 5007 | - EXPECT_EQ(ret, ACL_ERROR_INVALID_PARAM); | 5014 | + args.restoreFlags = flags; |
| 5015 | + for (const int32_t pid : {static_cast<int32_t>(mmGetPid()), INT32_MAX}) { | ||
| 5016 | + SCOPED_TRACE(pid); | ||
| 5017 | + EXPECT_EQ(aclrtSnapShotProcessRestore(pid, &args), ACL_ERROR_INVALID_PARAM); | ||
| 5018 | + } | ||
| 5019 | + } | ||
| 5008 | } | 5020 | } |
| 5009 | 5021 | ||
| 5010 | TEST_F(UTEST_ACL_Runtime, SnapShotCallbackRegister_failed) | 5022 | TEST_F(UTEST_ACL_Runtime, SnapShotCallbackRegister_failed) |
| @@ -5021,10 +5033,123 @@ TEST_F(UTEST_ACL_Runtime, SnapShotCallbackUnregister_failed) | |||
| 5021 | 5033 | ||
| 5022 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessLock_invalid_pid) | 5034 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessLock_invalid_pid) |
| 5023 | { | 5035 | { |
| 5024 | - aclError ret = aclrtSnapShotProcessLock(9999, nullptr); | 5036 | + aclError ret = aclrtSnapShotProcessLock(0, nullptr); |
| 5025 | EXPECT_EQ(ret, ACL_ERROR_INVALID_PARAM); | 5037 | EXPECT_EQ(ret, ACL_ERROR_INVALID_PARAM); |
| 5026 | } | 5038 | } |
| 5027 | 5039 | ||
| 5040 | +TEST_F(UTEST_ACL_Runtime, SnapshotInvalidPidRejectsAllOperationsWithoutChangingOutputs) | ||
| 5041 | +{ | ||
| 5042 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessLock()).Times(0); | ||
| 5043 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessUnlock()).Times(0); | ||
| 5044 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessBackup()).Times(0); | ||
| 5045 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessRestore()).Times(0); | ||
| 5046 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessBackupWithArgs(_)).Times(0); | ||
| 5047 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessRestoreWithArgs(_)).Times(0); | ||
| 5048 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetState(_)).Times(0); | ||
| 5049 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetRestoreThreadId(_)).Times(0); | ||
| 5050 | + for (const int32_t pid : {-1, 0}) { | ||
| 5051 | + SCOPED_TRACE(pid); | ||
| 5052 | + aclrtProcessState state = ACL_RT_PROCESS_STATE_BACKED_UP; | ||
| 5053 | + int32_t tid = 77; | ||
| 5054 | + EXPECT_EQ(aclrtSnapShotProcessLock(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5055 | + EXPECT_EQ(aclrtSnapShotProcessUnlock(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5056 | + EXPECT_EQ(aclrtSnapShotProcessBackup(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5057 | + EXPECT_EQ(aclrtSnapShotProcessRestore(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5058 | + EXPECT_EQ(aclrtSnapshotProcessGetState(pid, &state), ACL_ERROR_INVALID_PARAM); | ||
| 5059 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(pid, &tid), ACL_ERROR_INVALID_PARAM); | ||
| 5060 | + EXPECT_EQ(state, ACL_RT_PROCESS_STATE_BACKED_UP); | ||
| 5061 | + EXPECT_EQ(tid, 77); | ||
| 5062 | + } | ||
| 5063 | +} | ||
| 5064 | + | ||
| 5065 | +TEST_F(UTEST_ACL_Runtime, SnapshotUnlockRejectsNonNullReservedArgumentBeforeRuntime) | ||
| 5066 | +{ | ||
| 5067 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessUnlock()).Times(0); | ||
| 5068 | + int32_t reserve = 0; | ||
| 5069 | + EXPECT_EQ(aclrtSnapShotProcessUnlock(static_cast<int32_t>(mmGetPid()), &reserve), ACL_ERROR_INVALID_PARAM); | ||
| 5070 | +} | ||
| 5071 | + | ||
| 5072 | +TEST_F(UTEST_ACL_Runtime, SnapshotQueriesLocalAndInvalidArguments) | ||
| 5073 | +{ | ||
| 5074 | + const int32_t pid = static_cast<int32_t>(mmGetPid()); | ||
| 5075 | + aclrtProcessState state = ACL_RT_PROCESS_STATE_FAILED; | ||
| 5076 | + int32_t tid = 0; | ||
| 5077 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetState(_)) | ||
| 5078 | + .WillOnce(Invoke([](rtProcessState* output) { | ||
| 5079 | + *output = RT_PROCESS_STATE_RUNNING; | ||
| 5080 | + return RT_ERROR_NONE; | ||
| 5081 | + })); | ||
| 5082 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetRestoreThreadId(_)) | ||
| 5083 | + .WillOnce(Invoke([](int32_t* output) { | ||
| 5084 | + *output = 1234; | ||
| 5085 | + return RT_ERROR_NONE; | ||
| 5086 | + })); | ||
| 5087 | + EXPECT_EQ(aclrtSnapshotProcessGetState(pid, &state), ACL_SUCCESS); | ||
| 5088 | + EXPECT_EQ(state, ACL_RT_PROCESS_STATE_RUNNING); | ||
| 5089 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(pid, &tid), ACL_SUCCESS); | ||
| 5090 | + EXPECT_EQ(tid, 1234); | ||
| 5091 | + EXPECT_EQ(aclrtSnapshotProcessGetState(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5092 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(pid, nullptr), ACL_ERROR_INVALID_PARAM); | ||
| 5093 | + EXPECT_EQ(aclrtSnapshotProcessGetState(-1, &state), ACL_ERROR_INVALID_PARAM); | ||
| 5094 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(0, &tid), ACL_ERROR_INVALID_PARAM); | ||
| 5095 | +} | ||
| 5096 | + | ||
| 5097 | +TEST_F(UTEST_ACL_Runtime, SnapshotInvalidStateReportsReturnedValue) | ||
| 5098 | +{ | ||
| 5099 | + const int32_t pid = static_cast<int32_t>(mmGetPid()); | ||
| 5100 | + aclrtProcessState state = ACL_RT_PROCESS_STATE_LOCKED; | ||
| 5101 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetState(_)) | ||
| 5102 | + .WillOnce(Invoke([](rtProcessState* output) { | ||
| 5103 | + *output = static_cast<rtProcessState>(99); | ||
| 5104 | + return RT_ERROR_NONE; | ||
| 5105 | + })); | ||
| 5106 | + EXPECT_CALL( | ||
| 5107 | + MockFunctionTest::aclStubInstance(), | ||
| 5108 | + ReportInnerErrMsg( | ||
| 5109 | + StrEq("EH9999"), | ||
| 5110 | + AllOf(HasSubstr("pid=" + std::to_string(pid)), HasSubstr("state=99"), HasSubstr("[0, 3]")))) | ||
| 5111 | + .WillOnce(Return(0)); | ||
| 5112 | + EXPECT_EQ(aclrtSnapshotProcessGetState(pid, &state), ACL_ERROR_RT_INTERNAL_ERROR); | ||
| 5113 | + EXPECT_EQ(state, ACL_RT_PROCESS_STATE_LOCKED); | ||
| 5114 | +} | ||
| 5115 | + | ||
| 5116 | +TEST_F(UTEST_ACL_Runtime, SnapshotInvalidThreadIdReportsReturnedValue) | ||
| 5117 | +{ | ||
| 5118 | + const int32_t pid = static_cast<int32_t>(mmGetPid()); | ||
| 5119 | + for (const int32_t invalidTid : {0, -1}) { | ||
| 5120 | + int32_t tid = 1234; | ||
| 5121 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetRestoreThreadId(_)) | ||
| 5122 | + .WillOnce(Invoke([invalidTid](int32_t* output) { | ||
| 5123 | + *output = invalidTid; | ||
| 5124 | + return RT_ERROR_NONE; | ||
| 5125 | + })); | ||
| 5126 | + EXPECT_CALL( | ||
| 5127 | + MockFunctionTest::aclStubInstance(), | ||
| 5128 | + ReportInnerErrMsg( | ||
| 5129 | + StrEq("EH9999"), AllOf( | ||
| 5130 | + HasSubstr("pid=" + std::to_string(pid)), | ||
| 5131 | + HasSubstr("tid=" + std::to_string(static_cast<uint32_t>(invalidTid))), | ||
| 5132 | + HasSubstr("[1, " + std::to_string(INT32_MAX) + "]")))) | ||
| 5133 | + .WillOnce(Return(0)); | ||
| 5134 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(pid, &tid), ACL_ERROR_RT_INTERNAL_ERROR); | ||
| 5135 | + EXPECT_EQ(tid, 1234); | ||
| 5136 | + } | ||
| 5137 | +} | ||
| 5138 | + | ||
| 5139 | +TEST_F(UTEST_ACL_Runtime, SnapshotRemoteDoesNotCallLocalRuntime) | ||
| 5140 | +{ | ||
| 5141 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessLock()).Times(0); | ||
| 5142 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessBackupWithArgs(_)).Times(0); | ||
| 5143 | + EXPECT_CALL(MockFunctionTest::aclStubInstance(), rtSnapShotProcessGetState(_)).Times(0); | ||
| 5144 | + EXPECT_EQ(aclrtSnapShotProcessLock(0x7FFFFFFF, nullptr), ACL_ERROR_RT_INTERNAL_ERROR); | ||
| 5145 | + aclrtProcessState state = ACL_RT_PROCESS_STATE_FAILED; | ||
| 5146 | + EXPECT_EQ(aclrtSnapshotProcessGetState(0x7FFFFFFF, &state), ACL_ERROR_RT_INTERNAL_ERROR); | ||
| 5147 | + EXPECT_EQ(state, ACL_RT_PROCESS_STATE_FAILED); | ||
| 5148 | + aclrtSnapShotBackupArgs args = {}; | ||
| 5149 | + args.backupFlags = 2U; | ||
| 5150 | + EXPECT_EQ(aclrtSnapShotProcessBackup(0x7FFFFFFF, &args), ACL_ERROR_INVALID_PARAM); | ||
| 5151 | +} | ||
| 5152 | + | ||
| 5028 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessLock_invalid_reserve) | 5153 | TEST_F(UTEST_ACL_Runtime, SnapShotProcessLock_invalid_reserve) |
| 5029 | { | 5154 | { |
| 5030 | int dummy = 0; | 5155 | int dummy = 0; |
| @@ -11289,6 +11414,49 @@ TEST_F(UTEST_ACL_Runtime, aclrtMemMapSetLink_other_error) | |||
| 11289 | EXPECT_EQ(ret, ACL_ERROR_RT_DRV_INTERNAL_ERROR); | 11414 | EXPECT_EQ(ret, ACL_ERROR_RT_DRV_INTERNAL_ERROR); |
| 11290 | } | 11415 | } |
| 11291 | 11416 | ||
| 11417 | +TEST_F(UTEST_ACL_Runtime, SnapshotMemoryArgumentsKeepWireLayout) | ||
| 11418 | +{ | ||
| 11419 | + EXPECT_EQ(sizeof(aclrtSnapShotBackupArgs), sizeof(cce::runtime::SnapshotMemoryArgs)); | ||
| 11420 | + EXPECT_EQ(sizeof(aclrtSnapShotRestoreArgs), sizeof(cce::runtime::SnapshotMemoryArgs)); | ||
| 11421 | + EXPECT_EQ(offsetof(aclrtSnapShotBackupArgs, reserved), offsetof(cce::runtime::SnapshotMemoryArgs, reserved)); | ||
| 11422 | + EXPECT_EQ(offsetof(aclrtSnapShotRestoreArgs, reserved), offsetof(cce::runtime::SnapshotMemoryArgs, reserved)); | ||
| 11423 | +} | ||
| 11424 | + | ||
| 11425 | +namespace cce { | ||
| 11426 | +namespace runtime { | ||
| 11427 | +extern rtError_t g_snapshotSocketResult; | ||
| 11428 | +} | ||
| 11429 | +} // namespace cce | ||
| 11430 | + | ||
| 11431 | +TEST_F(UTEST_ACL_Runtime, SnapshotRemoteMapsInternalErrorsAtApiBoundary) | ||
| 11432 | +{ | ||
| 11433 | + using namespace cce::runtime; | ||
| 11434 | + const rtError_t previous = g_snapshotSocketResult; | ||
| 11435 | + const std::unique_ptr<rtError_t, std::function<void(rtError_t*)>> restore( | ||
| 11436 | + &g_snapshotSocketResult, [previous](rtError_t* value) { *value = previous; }); | ||
| 11437 | + const std::pair<rtError_t, aclError> cases[] = { | ||
| 11438 | + {RT_ERROR_FEATURE_NOT_SUPPORT, ACL_ERROR_RT_FEATURE_NOT_SUPPORT}, | ||
| 11439 | + {RT_ERROR_OS_HANDLE, ACL_ERROR_RT_INTERNAL_ERROR}, | ||
| 11440 | + {RT_ERROR_INVALID_VALUE, ACL_ERROR_RT_PARAM_INVALID}, | ||
| 11441 | + {RT_ERROR_SNAPSHOT_LOCK_FAILED, ACL_ERROR_SNAPSHOT_LOCK_FAILED}, | ||
| 11442 | + }; | ||
| 11443 | + const int32_t remotePid = mmGetPid() == 1 ? 2 : 1; | ||
| 11444 | + for (const auto& item : cases) { | ||
| 11445 | + g_snapshotSocketResult = item.first; | ||
| 11446 | + SCOPED_TRACE(item.first); | ||
| 11447 | + int32_t tid = 123; | ||
| 11448 | + aclrtProcessState state = ACL_RT_PROCESS_STATE_LOCKED; | ||
| 11449 | + EXPECT_EQ(aclrtSnapShotProcessLock(remotePid, nullptr), item.second); | ||
| 11450 | + EXPECT_EQ(aclrtSnapShotProcessUnlock(remotePid, nullptr), item.second); | ||
| 11451 | + EXPECT_EQ(aclrtSnapShotProcessBackup(remotePid, nullptr), item.second); | ||
| 11452 | + EXPECT_EQ(aclrtSnapShotProcessRestore(remotePid, nullptr), item.second); | ||
| 11453 | + EXPECT_EQ(aclrtSnapshotProcessGetState(remotePid, &state), item.second); | ||
| 11454 | + EXPECT_EQ(aclrtSnapshotProcessGetRestoreThreadId(remotePid, &tid), item.second); | ||
| 11455 | + EXPECT_EQ(state, ACL_RT_PROCESS_STATE_LOCKED); | ||
| 11456 | + EXPECT_EQ(tid, 123); | ||
| 11457 | + } | ||
| 11458 | +} | ||
| 11459 | + | ||
| 11292 | TEST_F(UTEST_ACL_Runtime, aclrtSetDeviceFlags_forwards_all_modes) | 11460 | TEST_F(UTEST_ACL_Runtime, aclrtSetDeviceFlags_forwards_all_modes) |
| 11293 | { | 11461 | { |
| 11294 | const uint32_t modes[] = { | 11462 | const uint32_t modes[] = { |
| @@ -262,6 +262,7 @@ set(runtimeut_src_common_list | |||
| 262 | ${TOP_DIR}/src/runtime/core/src/common/rt_log.cc | 262 | ${TOP_DIR}/src/runtime/core/src/common/rt_log.cc |
| 263 | ${TOP_DIR}/src/runtime/core/src/common/dev_info_manage.cc | 263 | ${TOP_DIR}/src/runtime/core/src/common/dev_info_manage.cc |
| 264 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc | 264 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc |
| 265 | + ${TOP_DIR}/src/runtime/feature/snapshot/snapshot_socket.cc | ||
| 265 | ${TOP_DIR}/src/runtime/core/src/common/runtime_handle_guard.cc | 266 | ${TOP_DIR}/src/runtime/core/src/common/runtime_handle_guard.cc |
| 266 | ${TOP_DIR}/src/runtime/core/src/common/register_memory.cc | 267 | ${TOP_DIR}/src/runtime/core/src/common/register_memory.cc |
| 267 | ) | 268 | ) |
| @@ -401,6 +402,7 @@ set(runtimeut_src_callback_list | |||
| 401 | ) | 402 | ) |
| 402 | 403 | ||
| 403 | set(runtimeut_src_tiny_exclude_list | 404 | set(runtimeut_src_tiny_exclude_list |
| 405 | + ${TOP_DIR}/src/runtime/feature/snapshot/snapshot_socket.cc | ||
| 404 | ${TOP_DIR}/src/runtime/api/api_c_device_topology.cc | 406 | ${TOP_DIR}/src/runtime/api/api_c_device_topology.cc |
| 405 | ${TOP_DIR}/src/runtime/api/api_c_dqs.cc | 407 | ${TOP_DIR}/src/runtime/api/api_c_dqs.cc |
| 406 | ${TOP_DIR}/src/runtime/api/api_c_standard_soc.cc | 408 | ${TOP_DIR}/src/runtime/api/api_c_standard_soc.cc |
| @@ -414,7 +416,7 @@ set(runtimeut_src_tiny_exclude_list | |||
| 414 | ${TOP_DIR}/src/runtime/api/impl/api_error_standard_soc.cc | 416 | ${TOP_DIR}/src/runtime/api/impl/api_error_standard_soc.cc |
| 415 | ${TOP_DIR}/src/runtime/api/impl/api_impl_xpu_creator.cc | 417 | ${TOP_DIR}/src/runtime/api/impl/api_impl_xpu_creator.cc |
| 416 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc | 418 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc |
| 417 | - ${TOP_DIR}/src/runtime/api/impl/api_impl_snapshot.cc | 419 | + ${TOP_DIR}/src/runtime/feature/snapshot/api_impl_snapshot.cc |
| 418 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc | 420 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc |
| 419 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc | 421 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc |
| 420 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc | 422 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc |
| @@ -502,13 +504,13 @@ set(runtimeut_src_tiny_exclude_list | |||
| 502 | ) | 504 | ) |
| 503 | 505 | ||
| 504 | set(runtimeut_src_tiny_include_list | 506 | set(runtimeut_src_tiny_include_list |
| 507 | + ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_snapshot_stub.cc | ||
| 505 | ${RUNTIME_UT_TINY_GENERATED_API_STUB_SOURCE} | 508 | ${RUNTIME_UT_TINY_GENERATED_API_STUB_SOURCE} |
| 506 | ${TOP_DIR}/src/runtime/api/api_c_tiny.cc | 509 | ${TOP_DIR}/src/runtime/api/api_c_tiny.cc |
| 507 | ${TOP_DIR}/src/runtime/api/impl/stub/api_error_stub.cc | 510 | ${TOP_DIR}/src/runtime/api/impl/stub/api_error_stub.cc |
| 508 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_stub.cc | 511 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_stub.cc |
| 509 | ${TOP_DIR}/src/runtime/api/impl/api_impl_tiny.cc | 512 | ${TOP_DIR}/src/runtime/api/impl/api_impl_tiny.cc |
| 510 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_event_stub.cc | 513 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_event_stub.cc |
| 511 | - ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_snapshot_stub.cc | ||
| 512 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_kernel_func_stub.cc | 514 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_kernel_func_stub.cc |
| 513 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_ipc_memory_stub.cc | 515 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_ipc_memory_stub.cc |
| 514 | ${TOP_DIR}/src/runtime/api/impl/api_impl_esched.cc | 516 | ${TOP_DIR}/src/runtime/api/impl/api_impl_esched.cc |
| @@ -632,7 +634,7 @@ set(runtimeut_src_list | |||
| 632 | ${TOP_DIR}/src/runtime/api/impl/api_impl_vmm.cc | 634 | ${TOP_DIR}/src/runtime/api/impl/api_impl_vmm.cc |
| 633 | ${TOP_DIR}/src/runtime/api/impl/api_impl_mbuf.cc | 635 | ${TOP_DIR}/src/runtime/api/impl/api_impl_mbuf.cc |
| 634 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc | 636 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc |
| 635 | - ${TOP_DIR}/src/runtime/api/impl/api_impl_snapshot.cc | 637 | + ${TOP_DIR}/src/runtime/feature/snapshot/api_impl_snapshot.cc |
| 636 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc | 638 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc |
| 637 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc | 639 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc |
| 638 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc | 640 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc |
| @@ -794,7 +796,7 @@ set(runtimeut_david_src_list | |||
| 794 | ${TOP_DIR}/src/runtime/api/impl/api_impl_creator.cc | 796 | ${TOP_DIR}/src/runtime/api/impl/api_impl_creator.cc |
| 795 | ${TOP_DIR}/src/runtime/api/impl/v200/api_impl_creator_c.cc | 797 | ${TOP_DIR}/src/runtime/api/impl/v200/api_impl_creator_c.cc |
| 796 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc | 798 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc |
| 797 | - ${TOP_DIR}/src/runtime/api/impl/api_impl_snapshot.cc | 799 | + ${TOP_DIR}/src/runtime/feature/snapshot/api_impl_snapshot.cc |
| 798 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc | 800 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc |
| 799 | ${TOP_DIR}/src/runtime/config/950/dev_info_proc_func.cc | 801 | ${TOP_DIR}/src/runtime/config/950/dev_info_proc_func.cc |
| 800 | ${TOP_DIR}/src/runtime/api/impl/api_error.cc | 802 | ${TOP_DIR}/src/runtime/api/impl/api_error.cc |
| @@ -1761,7 +1763,7 @@ set(runtimeut_v201_src_list | |||
| 1761 | ${TOP_DIR}/src/runtime/api/impl/v201/api_impl_v200_adapt.cc | 1763 | ${TOP_DIR}/src/runtime/api/impl/v201/api_impl_v200_adapt.cc |
| 1762 | ${TOP_DIR}/src/runtime/api/impl/v201/api_impl_creator_c.cc | 1764 | ${TOP_DIR}/src/runtime/api/impl/v201/api_impl_creator_c.cc |
| 1763 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc | 1765 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc |
| 1764 | - ${TOP_DIR}/src/runtime/api/impl/api_impl_snapshot.cc | 1766 | + ${TOP_DIR}/src/runtime/feature/snapshot/api_impl_snapshot.cc |
| 1765 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc | 1767 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc |
| 1766 | ${TOP_DIR}/src/runtime/api/impl/api_error.cc | 1768 | ${TOP_DIR}/src/runtime/api/impl/api_error.cc |
| 1767 | ${TOP_DIR}/src/runtime/api/impl/api_error_standard_soc.cc | 1769 | ${TOP_DIR}/src/runtime/api/impl/api_error_standard_soc.cc |
| @@ -86,9 +86,9 @@ class GenerateAclApiStubsTest(unittest.TestCase): | |||
| 86 | REPO_ROOT / "src/acl/aclrt_impl/acl_rt_wrapper.h" | 86 | REPO_ROOT / "src/acl/aclrt_impl/acl_rt_wrapper.h" |
| 87 | ) | 87 | ) |
| 88 | 88 | ||
| 89 | - self.assertEqual(len(entries), 413) | 89 | + self.assertEqual(len(entries), 415) |
| 90 | self.assertEqual( | 90 | self.assertEqual( |
| 91 | - sum(entry.return_type == "aclError" for entry in entries.values()), 395 | 91 | + sum(entry.return_type == "aclError" for entry in entries.values()), 397 |
| 92 | ) | 92 | ) |
| 93 | self.assertEqual( | 93 | self.assertEqual( |
| 94 | sum(entry.return_type != "aclError" for entry in entries.values()), 18 | 94 | sum(entry.return_type != "aclError" for entry in entries.values()), 18 |
| @@ -164,9 +164,9 @@ ACL_API_STUB(aclZero, ZERO) | |||
| 164 | 164 | ||
| 165 | self.assertEqual(version, 1) | 165 | self.assertEqual(version, 1) |
| 166 | self.assertEqual(real_provider_count, 94) | 166 | self.assertEqual(real_provider_count, 94) |
| 167 | - self.assertEqual(len(entries), 319) | 167 | + self.assertEqual(len(entries), 321) |
| 168 | self.assertEqual( | 168 | self.assertEqual( |
| 169 | - sum(entry.policy == "RT_FEATURE_NOT_SUPPORT" for entry in entries), 304 | 169 | + sum(entry.policy == "RT_FEATURE_NOT_SUPPORT" for entry in entries), 306 |
| 170 | ) | 170 | ) |
| 171 | self.assertEqual(sum(entry.policy == "NULLPTR" for entry in entries), 5) | 171 | self.assertEqual(sum(entry.policy == "NULLPTR" for entry in entries), 5) |
| 172 | self.assertEqual(sum(entry.policy == "INVALID_EXCEPTION_INFO" for entry in entries), 5) | 172 | self.assertEqual(sum(entry.policy == "INVALID_EXCEPTION_INFO" for entry in entries), 5) |
| @@ -187,12 +187,12 @@ RUNTIME_API_STUB(rtNoop) | |||
| 187 | self.assertEqual(catalog_version, product_version) | 187 | self.assertEqual(catalog_version, product_version) |
| 188 | self.assertFalse(set(names) - set(catalog)) | 188 | self.assertFalse(set(names) - set(catalog)) |
| 189 | self.assertEqual(names, sorted(names)) | 189 | self.assertEqual(names, sorted(names)) |
| 190 | - self.assertEqual(len(catalog), 801) | 190 | + self.assertEqual(len(catalog), 802) |
| 191 | - self.assertEqual(len(names), 669) | 191 | + self.assertEqual(len(names), 670) |
| 192 | self.assertEqual(len(set(catalog) - set(names)), 132) | 192 | self.assertEqual(len(set(catalog) - set(names)), 132) |
| 193 | self.assertEqual( | 193 | self.assertEqual( |
| 194 | sum(catalog[name].policy == "FEATURE_NOT_SUPPORT" for name in names), | 194 | sum(catalog[name].policy == "FEATURE_NOT_SUPPORT" for name in names), |
| 195 | - 665, | 195 | + 666, |
| 196 | ) | 196 | ) |
| 197 | self.assertEqual( | 197 | self.assertEqual( |
| 198 | sum(catalog[name].policy == "SUCCESS_NOOP" for name in names), 3 | 198 | sum(catalog[name].policy == "SUCCESS_NOOP" for name in names), 3 |
| @@ -233,6 +233,7 @@ set(runtimeut_src_common_list | |||
| 233 | ${TOP_DIR}/src/runtime/core/src/common/rt_log.cc | 233 | ${TOP_DIR}/src/runtime/core/src/common/rt_log.cc |
| 234 | ${TOP_DIR}/src/runtime/core/src/common/dev_info_manage.cc | 234 | ${TOP_DIR}/src/runtime/core/src/common/dev_info_manage.cc |
| 235 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc | 235 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc |
| 236 | + ${TOP_DIR}/src/runtime/feature/snapshot/snapshot_socket.cc | ||
| 236 | ${TOP_DIR}/src/runtime/core/src/common/runtime_handle_guard.cc | 237 | ${TOP_DIR}/src/runtime/core/src/common/runtime_handle_guard.cc |
| 237 | ${TOP_DIR}/src/runtime/core/src/common/register_memory.cc | 238 | ${TOP_DIR}/src/runtime/core/src/common/register_memory.cc |
| 238 | ) | 239 | ) |
| @@ -279,7 +280,7 @@ set(runtimeut_910b_src_list | |||
| 279 | ${TOP_DIR}/src/runtime/api/impl/api_impl_vmm.cc | 280 | ${TOP_DIR}/src/runtime/api/impl/api_impl_vmm.cc |
| 280 | ${TOP_DIR}/src/runtime/api/impl/api_impl_mbuf.cc | 281 | ${TOP_DIR}/src/runtime/api/impl/api_impl_mbuf.cc |
| 281 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc | 282 | ${TOP_DIR}/src/runtime/api/impl/api_impl_standard_soc.cc |
| 282 | - ${TOP_DIR}/src/runtime/api/impl/api_impl_snapshot.cc | 283 | + ${TOP_DIR}/src/runtime/feature/snapshot/api_impl_snapshot.cc |
| 283 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc | 284 | ${TOP_DIR}/src/runtime/api/impl/api_impl_ipc_memory.cc |
| 284 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc | 285 | ${TOP_DIR}/src/runtime/api/impl/api_impl_soma.cc |
| 285 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc | 286 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc |
| @@ -11,6 +11,8 @@ | |||
| 11 | #include "gtest/gtest.h" | 11 | #include "gtest/gtest.h" |
| 12 | #include "mockcpp/mockcpp.hpp" | 12 | #include "mockcpp/mockcpp.hpp" |
| 13 | #include "securec.h" | 13 | #include "securec.h" |
| 14 | +#include <thread> | ||
| 15 | +#include "memcpy_c.hpp" | ||
| 14 | #define private public | 16 | #define private public |
| 15 | #define protected public | 17 | #define protected public |
| 16 | #include "context.hpp" | 18 | #include "context.hpp" |
| @@ -65,6 +67,8 @@ protected: | |||
| 65 | 67 | ||
| 66 | virtual void TearDown() | 68 | virtual void TearDown() |
| 67 | { | 69 | { |
| 70 | + // Resource-failure tests leave FAILED; wake background threads before releasing the device. | ||
| 71 | + GlobalStateManager::GetInstance().ForceUnlocked(); | ||
| 68 | DELETE_O(deviceSnapshot_); | 72 | DELETE_O(deviceSnapshot_); |
| 69 | DELETE_O(eventPool_); | 73 | DELETE_O(eventPool_); |
| 70 | rtDeviceReset(0); | 74 | rtDeviceReset(0); |
| @@ -97,6 +101,104 @@ rtError_t EventPoolPcieHostRegisterStub(Driver*, void*, uint64_t, uint32_t, void | |||
| 97 | } | 101 | } |
| 98 | } // namespace | 102 | } // namespace |
| 99 | 103 | ||
| 104 | +class SnapshotRestoreStreamTest : public SnapshotTest, public WithParamInterface<bool> {}; | ||
| 105 | + | ||
| 106 | +TEST_P(SnapshotRestoreStreamTest, RestoresAllRangesOnDeviceStreamWithoutBusinessContext) | ||
| 107 | +{ | ||
| 108 | + uint8_t first[3] = {}; | ||
| 109 | + uint8_t second[5] = {}; | ||
| 110 | + std::unique_ptr<uint8_t[]> backup(new uint8_t[sizeof(first) + sizeof(second)]{}); | ||
| 111 | + const uint8_t* const host = backup.get(); | ||
| 112 | + deviceSnapshot_->AddOpVirtualAddr(first, sizeof(first)); | ||
| 113 | + deviceSnapshot_->AddOpVirtualAddr(second, sizeof(second)); | ||
| 114 | + deviceSnapshot_->SetOpBackUpAddr(backup); | ||
| 115 | + Stream controlStream(device_, 0); | ||
| 116 | + Stream* const primary = device_->PrimaryStream_(); | ||
| 117 | + ASSERT_NE(primary, nullptr); | ||
| 118 | + Stream* const selected = GetParam() ? primary : &controlStream; | ||
| 119 | + MOCKER_CPP_VIRTUAL(device_, &Device::GetCtrlSQStream).expects(once()).with(eq(primary)).will(returnValue(selected)); | ||
| 120 | + MOCKER(MemcopyAsync) | ||
| 121 | + .expects(once()) | ||
| 122 | + .with( | ||
| 123 | + eq(static_cast<void*>(first)), eq(static_cast<uint64_t>(sizeof(first))), eq(static_cast<const void*>(host)), | ||
| 124 | + eq(static_cast<uint64_t>(sizeof(first))), eq(RT_MEMCPY_HOST_TO_DEVICE), eq(selected)) | ||
| 125 | + .will(returnValue(RT_ERROR_NONE)); | ||
| 126 | + MOCKER(MemcopyAsync) | ||
| 127 | + .expects(once()) | ||
| 128 | + .with( | ||
| 129 | + eq(static_cast<void*>(second)), eq(static_cast<uint64_t>(sizeof(second))), | ||
| 130 | + eq(static_cast<const void*>(host + sizeof(first))), eq(static_cast<uint64_t>(sizeof(second))), | ||
| 131 | + eq(RT_MEMCPY_HOST_TO_DEVICE), eq(selected)) | ||
| 132 | + .will(returnValue(RT_ERROR_NONE)); | ||
| 133 | + MOCKER_CPP_VIRTUAL(selected, &Stream::Synchronize).expects(once()).will(returnValue(RT_ERROR_NONE)); | ||
| 134 | + | ||
| 135 | + rtError_t result = RT_ERROR_INVALID_VALUE; | ||
| 136 | + std::thread worker([&] { | ||
| 137 | + EXPECT_EQ(Runtime::Instance()->CurrentContext(), nullptr); | ||
| 138 | + result = deviceSnapshot_->OpMemoryRestore(); | ||
| 139 | + }); | ||
| 140 | + worker.join(); | ||
| 141 | + EXPECT_EQ(result, RT_ERROR_NONE); | ||
| 142 | + EXPECT_EQ(deviceSnapshot_->GetOpBackUpAddr().get(), host); | ||
| 143 | + GlobalMockObject::verify(); | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +INSTANTIATE_TEST_SUITE_P(ControlAndPrimary, SnapshotRestoreStreamTest, Values(false, true)); | ||
| 147 | + | ||
| 148 | +TEST_F(SnapshotTest, OpMemoryRestoreEmptySkipsStreamAndCopy) | ||
| 149 | +{ | ||
| 150 | + MOCKER_CPP_VIRTUAL(device_, &Device::GetCtrlSQStream).expects(never()); | ||
| 151 | + MOCKER(MemcopyAsync).expects(never()); | ||
| 152 | + EXPECT_EQ(deviceSnapshot_->OpMemoryRestore(), RT_ERROR_NONE); | ||
| 153 | + GlobalMockObject::verify(); | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +TEST_F(SnapshotTest, OpMemoryRestoreMissingBackupRejectsBeforeSelectingStream) | ||
| 157 | +{ | ||
| 158 | + uint8_t destination = 0U; | ||
| 159 | + deviceSnapshot_->AddOpVirtualAddr(&destination, sizeof(destination)); | ||
| 160 | + MOCKER_CPP_VIRTUAL(device_, &Device::GetCtrlSQStream).expects(never()); | ||
| 161 | + MOCKER(MemcopyAsync).expects(never()); | ||
| 162 | + EXPECT_EQ(deviceSnapshot_->OpMemoryRestore(), RT_ERROR_INVALID_VALUE); | ||
| 163 | + GlobalMockObject::verify(); | ||
| 164 | +} | ||
| 165 | + | ||
| 166 | +class SnapshotRestoreFailureTest : public SnapshotTest, public WithParamInterface<int32_t> {}; | ||
| 167 | + | ||
| 168 | +TEST_P(SnapshotRestoreFailureTest, StreamCopyOrSynchronizeErrorIsPropagated) | ||
| 169 | +{ | ||
| 170 | + uint8_t destination = 0U; | ||
| 171 | + std::unique_ptr<uint8_t[]> backup(new uint8_t[sizeof(destination)]{}); | ||
| 172 | + deviceSnapshot_->AddOpVirtualAddr(&destination, sizeof(destination)); | ||
| 173 | + deviceSnapshot_->SetOpBackUpAddr(backup); | ||
| 174 | + Stream* const primary = device_->PrimaryStream_(); | ||
| 175 | + ASSERT_NE(primary, nullptr); | ||
| 176 | + const int32_t failure = GetParam(); // 0: missing stream; 1: copy; 2: synchronization. | ||
| 177 | + MOCKER_CPP_VIRTUAL(device_, &Device::GetCtrlSQStream) | ||
| 178 | + .expects(once()) | ||
| 179 | + .with(eq(primary)) | ||
| 180 | + .will(returnValue(failure == 0 ? static_cast<Stream*>(nullptr) : primary)); | ||
| 181 | + if (failure == 0) { | ||
| 182 | + MOCKER(MemcopyAsync).expects(never()); | ||
| 183 | + } else { | ||
| 184 | + MOCKER(MemcopyAsync).expects(once()).will(returnValue(failure == 1 ? RT_ERROR_DRV_ERR : RT_ERROR_NONE)); | ||
| 185 | + } | ||
| 186 | + if (failure == 2) { | ||
| 187 | + MOCKER_CPP_VIRTUAL(primary, &Stream::Synchronize) | ||
| 188 | + .expects(once()) | ||
| 189 | + .will(returnValue(RT_ERROR_STREAM_SYNC_TIMEOUT)); | ||
| 190 | + } else { | ||
| 191 | + MOCKER_CPP_VIRTUAL(primary, &Stream::Synchronize).expects(never()); | ||
| 192 | + } | ||
| 193 | + const rtError_t expected = | ||
| 194 | + failure == 0 ? RT_ERROR_STREAM_NULL : (failure == 1 ? RT_ERROR_DRV_ERR : RT_ERROR_STREAM_SYNC_TIMEOUT); | ||
| 195 | + EXPECT_EQ(deviceSnapshot_->OpMemoryRestore(), expected); | ||
| 196 | + EXPECT_EQ(destination, 0U); | ||
| 197 | + GlobalMockObject::verify(); | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +INSTANTIATE_TEST_SUITE_P(Dependencies, SnapshotRestoreFailureTest, Values(0, 1, 2)); | ||
| 201 | + | ||
| 100 | TEST_F(SnapshotTest, EventPoolPcieBarSupportedUsesP2pMemory) | 202 | TEST_F(SnapshotTest, EventPoolPcieBarSupportedUsesP2pMemory) |
| 101 | { | 203 | { |
| 102 | uint8_t buffer[16] = {}; | 204 | uint8_t buffer[16] = {}; |
| @@ -124,7 +124,6 @@ set(runtimeut_src_api_impl_list | |||
| 124 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc | 124 | ${TOP_DIR}/src/runtime/api/impl/api_impl_event.cc |
| 125 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_mbuf_stub.cc | 125 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_mbuf_stub.cc |
| 126 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_esched_stub.cc | 126 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_esched_stub.cc |
| 127 | - ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_snapshot_stub.cc | ||
| 128 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_rt_config_stub.cc | 127 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_rt_config_stub.cc |
| 129 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_device_topology_stub.cc | 128 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_device_topology_stub.cc |
| 130 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_ipc_memory_stub.cc | 129 | ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_ipc_memory_stub.cc |
| @@ -148,6 +147,7 @@ set(runtimeut_src_common_list | |||
| 148 | ${TOP_DIR}/src/runtime/core/src/common/error_code.cc | 147 | ${TOP_DIR}/src/runtime/core/src/common/error_code.cc |
| 149 | ${TOP_DIR}/src/runtime/core/src/common/error_message_manage.cc | 148 | ${TOP_DIR}/src/runtime/core/src/common/error_message_manage.cc |
| 150 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc | 149 | ${TOP_DIR}/src/runtime/core/src/common/global_state_manager.cc |
| 150 | + ${TOP_DIR}/src/runtime/api/impl/stub/api_impl_snapshot_stub.cc | ||
| 151 | ${TOP_DIR}/src/runtime/core/src/common/heterogenous.cc | 151 | ${TOP_DIR}/src/runtime/core/src/common/heterogenous.cc |
| 152 | ${TOP_DIR}/src/runtime/core/src/common/inner_thread_local.cpp | 152 | ${TOP_DIR}/src/runtime/core/src/common/inner_thread_local.cpp |
| 153 | ${TOP_DIR}/src/runtime/core/src/common/performance_record.cc | 153 | ${TOP_DIR}/src/runtime/core/src/common/performance_record.cc |
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "api_error.hpp" | 15 | #include "api_error.hpp" |
| 16 | #include "aicpu_dfx.hpp" | 16 | #include "aicpu_dfx.hpp" |
| 17 | #include "model.hpp" | 17 | #include "model.hpp" |
| 18 | +#include "snapshot_socket.hpp" | ||
| 18 | 19 | ||
| 19 | using namespace cce::runtime; | 20 | using namespace cce::runtime; |
| 20 | 21 | ||
| @@ -26,6 +27,22 @@ TEST(Arch5162ApiTest, SnapshotApiImplStub_NotSupport) | |||
| 26 | DestroyImplSnapshot(apiImplSnapshot); | 27 | DestroyImplSnapshot(apiImplSnapshot); |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 30 | +TEST(Arch5162ApiTest, SnapshotSocketStubDoesNotStartServiceOrChangeQueryOutput) | ||
| 31 | +{ | ||
| 32 | + SnapshotService service; | ||
| 33 | + EXPECT_EQ(service.StartSnapshotService(nullptr), RT_ERROR_FEATURE_NOT_SUPPORT); | ||
| 34 | + EXPECT_EQ(service.GetSnapshotThreadId(), 0); | ||
| 35 | + service.StopSnapshotService(); | ||
| 36 | + uint32_t value = 123U; | ||
| 37 | + const rtSnapshotOp operations[] = {rtSnapshotOp::LOCK, rtSnapshotOp::BACKUP, | ||
| 38 | + rtSnapshotOp::RESTORE, rtSnapshotOp::UNLOCK, | ||
| 39 | + rtSnapshotOp::GET_STATE, rtSnapshotOp::GET_RESTORE_THREAD_ID}; | ||
| 40 | + for (const auto op : operations) { | ||
| 41 | + EXPECT_EQ(SnapshotSocketCall(mmGetPid(), op, nullptr, value), RT_ERROR_FEATURE_NOT_SUPPORT); | ||
| 42 | + EXPECT_EQ(value, 123U); | ||
| 43 | + } | ||
| 44 | +} | ||
| 45 | + | ||
| 29 | TEST(Arch5162ApiTest, KernelArgsImplLifecycleNotSupport) | 46 | TEST(Arch5162ApiTest, KernelArgsImplLifecycleNotSupport) |
| 30 | { | 47 | { |
| 31 | EXPECT_FALSE(IsImplKernelArgsSupported()); | 48 | EXPECT_FALSE(IsImplKernelArgsSupported()); |
| @@ -64,6 +64,7 @@ | |||
| 64 | #include "device_snapshot.hpp" | 64 | #include "device_snapshot.hpp" |
| 65 | #include "snapshot_process_helper.hpp" | 65 | #include "snapshot_process_helper.hpp" |
| 66 | #include "snapshot_callback_manager.hpp" | 66 | #include "snapshot_callback_manager.hpp" |
| 67 | +#include "snapshot_socket.hpp" | ||
| 67 | #include "notify.hpp" | 68 | #include "notify.hpp" |
| 68 | #include "event.hpp" | 69 | #include "event.hpp" |
| 69 | #include "xpu_task_fail_callback_data_manager.h" | 70 | #include "xpu_task_fail_callback_data_manager.h" |
| @@ -805,6 +806,23 @@ TEST_F(TinyStubTest, npu_snapshot_stub) | |||
| 805 | EXPECT_EQ(apiImplSnapshot, nullptr); | 806 | EXPECT_EQ(apiImplSnapshot, nullptr); |
| 806 | DestroyImplSnapshot(apiImplSnapshot); | 807 | DestroyImplSnapshot(apiImplSnapshot); |
| 807 | 808 | ||
| 809 | + SnapshotService service; | ||
| 810 | + EXPECT_EQ(service.StartSnapshotService(nullptr), RT_ERROR_FEATURE_NOT_SUPPORT); | ||
| 811 | + EXPECT_EQ(service.GetSnapshotThreadId(), 0); | ||
| 812 | + service.StopSnapshotService(); | ||
| 813 | + EXPECT_FALSE(service.started_); | ||
| 814 | + EXPECT_EQ(service.listenFd_, EN_ERROR); | ||
| 815 | + EXPECT_EQ(service.stopFd_, EN_ERROR); | ||
| 816 | + EXPECT_EQ(service.directoryFd_, EN_ERROR); | ||
| 817 | + uint32_t value = 123U; | ||
| 818 | + const rtSnapshotOp operations[] = {rtSnapshotOp::LOCK, rtSnapshotOp::BACKUP, | ||
| 819 | + rtSnapshotOp::RESTORE, rtSnapshotOp::UNLOCK, | ||
| 820 | + rtSnapshotOp::GET_STATE, rtSnapshotOp::GET_RESTORE_THREAD_ID}; | ||
| 821 | + for (const auto op : operations) { | ||
| 822 | + EXPECT_EQ(SnapshotSocketCall(mmGetPid(), op, nullptr, value), RT_ERROR_FEATURE_NOT_SUPPORT); | ||
| 823 | + EXPECT_EQ(value, 123U); | ||
| 824 | + } | ||
| 825 | + | ||
| 808 | auto ret = rtSnapShotProcessLock(); | 826 | auto ret = rtSnapShotProcessLock(); |
| 809 | EXPECT_EQ(ret, ACL_ERROR_RT_FEATURE_NOT_SUPPORT); | 827 | EXPECT_EQ(ret, ACL_ERROR_RT_FEATURE_NOT_SUPPORT); |
| 810 | ret = rtSnapShotProcessUnlock(); | 828 | ret = rtSnapShotProcessUnlock(); |
| @@ -1205,7 +1223,7 @@ TEST_F(TinyStubTest, device_snapshot_stub) | |||
| 1205 | 1223 | ||
| 1206 | TEST_F(TinyStubTest, snapshot_callback_manager_stub) | 1224 | TEST_F(TinyStubTest, snapshot_callback_manager_stub) |
| 1207 | { | 1225 | { |
| 1208 | - SnapshotCallbackManager& manager = SnapshotCallbackManager::GetInstance(); | 1226 | + SnapshotCallbackManager manager; |
| 1209 | EXPECT_NE(&manager, nullptr); | 1227 | EXPECT_NE(&manager, nullptr); |
| 1210 | 1228 | ||
| 1211 | rtError_t ret = manager.RegisterCallback(RT_SNAPSHOT_LOCK_PRE, nullptr, nullptr); | 1229 | rtError_t ret = manager.RegisterCallback(RT_SNAPSHOT_LOCK_PRE, nullptr, nullptr); |
| @@ -0,0 +1,630 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +r"""Exercise the production Snapshot APIs with a real NPU and two processes. | ||
| 10 | + | ||
| 11 | +Requires Python 3.8+, Linux, and an NPU with its Driver and CANN environment loaded. | ||
| 12 | +RTS_LIBRARY_DIR must contain libacl_rt.so, libruntime.so, libruntime_common.so, | ||
| 13 | +the chip's libruntime_v100/v200/v201.so, and dependencies from the same build. | ||
| 14 | +The test checks /proc/self/maps to reject accidentally loaded older libraries. | ||
| 15 | + | ||
| 16 | +Example after loading the CANN environment: | ||
| 17 | + export LD_LIBRARY_PATH="${RTS_LIBRARY_DIR}:/usr/local/Ascend/driver/lib64/driver:${LD_LIBRARY_PATH}" | ||
| 18 | + python3 tests/ut/runtime/runtime/test/snapshot_npu_test.py \ | ||
| 19 | + --library-dir "${RTS_LIBRARY_DIR}" --device 0 --criu /path/to/criu --criu-sudo \ | ||
| 20 | + --output snapshot_npu_result.json | ||
| 21 | + | ||
| 22 | +The device ID is the logical ID visible to this process. Tests cover pre-init TID | ||
| 23 | +queries, real EE1004 reporting, memory/Stream/Event operations, two-process | ||
| 24 | +queries and Lock/Unlock, callback thread IDs, and Backup/Restore with null args | ||
| 25 | +and flags 0/1. They use production libraries and real NPU operations, without | ||
| 26 | +Runtime or Driver stubs. FAILED targets exit directly to release NPU resources. | ||
| 27 | +User memory allocated by aclrtMalloc is not included in Snapshot content backup, | ||
| 28 | +including with flags=1. After restore the test reloads its input, then checks | ||
| 29 | +readback and operations using the original allocation, Stream, and Event. | ||
| 30 | + | ||
| 31 | +Results are PASS, FAIL, or UNSUPPORTED. UNSUPPORTED means the Driver cannot run | ||
| 32 | +that backup operation; it does not count as a successful Backup/Restore test. | ||
| 33 | +Use --require-snapshot for acceptance: UNSUPPORTED then also returns a nonzero | ||
| 34 | +exit code. Successful backup tests always dump and restore the target with CRIU | ||
| 35 | +before calling Runtime Restore. CRIU must support this host's kernel and target | ||
| 36 | +resources; use --criu-sudo when noninteractive sudo is required. CRIU failures | ||
| 37 | +are FAIL, never UNSUPPORTED. Logs and images are retained under --work-dir. | ||
| 38 | +""" | ||
| 39 | + | ||
| 40 | +import argparse | ||
| 41 | +import ctypes as c | ||
| 42 | +import json | ||
| 43 | +import os | ||
| 44 | +from pathlib import Path | ||
| 45 | +import pwd | ||
| 46 | +import selectors | ||
| 47 | +import shlex | ||
| 48 | +import shutil | ||
| 49 | +import signal | ||
| 50 | +import subprocess | ||
| 51 | +import sys | ||
| 52 | +import tempfile | ||
| 53 | +import threading | ||
| 54 | +import time | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +PREFIX = "SNAPSHOT_NPU_RESULT " | ||
| 58 | +PARAM_INVALID = 107000 | ||
| 59 | +ACL_PARAM_INVALID = 100000 | ||
| 60 | +NOT_SUPPORTED = 207000 | ||
| 61 | +RUNNING, LOCKED, BACKED_UP, FAILED = range(4) | ||
| 62 | +CALLBACK = c.CFUNCTYPE(c.c_uint32, c.c_int32, c.c_void_p) | ||
| 63 | + | ||
| 64 | + | ||
| 65 | +class SnapshotArgs(c.Structure): | ||
| 66 | + _fields_ = [("flags", c.c_uint32), ("reserved", c.c_char * 60)] | ||
| 67 | + | ||
| 68 | + | ||
| 69 | +def check(condition, message): | ||
| 70 | + if not condition: | ||
| 71 | + raise AssertionError(message) | ||
| 72 | + | ||
| 73 | + | ||
| 74 | +def emit(**data): | ||
| 75 | + print(PREFIX + json.dumps(data), flush=True) | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +def publish(directory, kind, **data): | ||
| 79 | + """Use regular files so CRIU does not need to checkpoint controller pipes.""" | ||
| 80 | + path = directory / (kind + ".json") | ||
| 81 | + temporary = path.with_suffix(".tmp") | ||
| 82 | + temporary.write_text(json.dumps(dict(kind=kind, **data))) | ||
| 83 | + temporary.replace(path) | ||
| 84 | + | ||
| 85 | + | ||
| 86 | +def enable_subreaper(): | ||
| 87 | + # Detached CRIU children are adopted here, so their actual exit status can | ||
| 88 | + # be checked even though the original Popen child has already exited. | ||
| 89 | + libc = c.CDLL(None, use_errno=True) | ||
| 90 | + libc.prctl.argtypes = [c.c_int, c.c_ulong, c.c_ulong, c.c_ulong, c.c_ulong] | ||
| 91 | + libc.prctl.restype = c.c_int | ||
| 92 | + if libc.prctl(36, 1, 0, 0, 0) != 0: # PR_SET_CHILD_SUBREAPER | ||
| 93 | + raise OSError(c.get_errno(), "cannot supervise restored children") | ||
| 94 | + | ||
| 95 | + | ||
| 96 | +def criu_command(arguments): | ||
| 97 | + return (["sudo", "-n"] if arguments.criu_sudo else []) + [arguments.criu] | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +def read_criu_file(arguments, path, limit=8000): | ||


用户如果没有家目录,功能还能否正常实现?