已关闭
[feat] Arch5162 aicpu support timeout #5289
FengHaozhan创建于 18 天前关闭于 17 天前
[feat] Arch5162 aicpu support timeout #5289
已关闭
FengHaozhan创建于 18 天前关闭于 17 天前
FengHaozhan
FengHaozhan成员
18 天前

Pull Request

描述

请清晰准确地描述本次 Pull Request 的意图和变更内容。

变更类型

请选择本次引入的变更类型:

关联的Issue

如何测试

描述测试此变更的步骤和前提条件:
1.
2.

核对清单

其他信息

在此添加任何其他关于本次 PR 的说明。

likedislike
当前Pull Request已关闭, 关闭人@FengHaozhan
FengHaozhanFengHaozhan成员
18 天前 添加了label:enhancement
FengHaozhanFengHaozhan成员
18 天前 创建了 pull request,commit 451d53c8
atomgit-bot
atomgit-bot
18 天前 评论:

变更摘要

本次变更主要为 AICPU 执行增加超时监控能力:在 AICPU 插件接口与运行时 hooks 中新增超时相关的扩展点,在服务侧记录每个执行中任务的起始时间并按配置超时阈值检测,超时后通过新增的 setStreamTimeoutError 回调上报;同时新增 Stars 引擎的监控任务注册机制,使 AICPU 超时检测能够挂载到既有监控线程中周期执行,并将 SetOpExecuteTimeOutV2 的计算结果同步下发给 AICPU 插件。整体上通过 RuntimeThreadAicpuPluginApi、RuntimeThreadAicpuRuntimeHooks、RuntimeThreadAicpuService、StarsEngine 与 runtime_thread_aicpu 适配层串联起「设置超时—记录执行—周期检测—上报流错误」的链路。

主要改动

  • 插件接口与运行时 hooks 扩展: RuntimeThreadAicpuPluginApi 新增 setExecuteTimeout、monitorExecutionTimeout,RuntimeThreadAicpuRuntimeHooks 新增 clockGetTimeUs、setStreamTimeoutError,并在 HooksAreValid、插件初始化校验与错误日志中同步校验这些新回调。
  • 服务侧执行超时监控: RuntimeThreadAicpuService 新增 SetExecuteTimeout、BeginExecution、EndExecution、MonitorExecutionTimeout 及 ExecutingTaskInfo、ExecutionStateGuard、executingTasks_、executionMutex_ 与原子量 executeTimeoutUs_(默认 28000000ULL),在 MonitorExecutionTimeout 中按边界条件判定超时并逐个上报,且同一执行仅上报一次;StreamDestroyed 同时清理该流对应的执行记录。
  • Stars 引擎监控任务注册机制: stars.hpp/stars_engine.cc 新增 RegisterStarsMonitorTask、UnregisterStarsMonitorTask(基于 g_starsMonitorTaskMutex 去重注册)与 RunRegisteredMonitorTasks,并在 MonitoringRun 的设备存活分支中调用,使注册的监控任务随监控线程周期运行。
  • 运行时适配层接入与超时错误上报: runtime_thread_aicpu.cc 中适配器新增 SetExecuteTimeout 缓存与下发逻辑,在插件加载成功后 setExecuteTimeout 并注册 Stars 监控任务与流观察者(析构时反注册),新增 SetStreamTimeoutError、RuntimeThreadAicpuClockGetTimeUs 回调,并通过 g_streamErrorMutex 保证超时错误 TS_ERROR_AICPU_TIMEOUT 不被执行失败错误覆盖。
  • SetOpExecuteTimeOutV2 下沉与超时下发: 该方法从 api_impl.cc 移除,在 api_impl_arch5162.cc 与 api_impl_cpu_kernel.cc 中重新实现,并在 arch5162 版本中新增 SetRuntimeThreadAicpuExecuteTimeout(runtime_thread_aicpu.hpp 声明)调用以把计算后的 actualTimeout 下发给 AICPU。
likedislike
不准确?
atomgit-bot
atomgit-bot
18 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
18 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
18 天前 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
pkg_inc ❌ (0/2)(You can also ask: 张子菁, zhangpengpeng8, yanmingxiang, Reyn52166, 卢煜坤) ❌ (0/1)(You can also ask: 张子菁, 卢煜坤, 侯延保, 王涛)
src/aicpu_sched ❌ (0/2)(You can also ask: yanmingxiang, 侯延保, 卢煜坤, 张子菁, 王涛) ❌ (0/1)(You can also ask: ZhaiPeiChao, LiWei79)
src/runtime ❌ (0/2)(You can also ask: liubin1986, Axiaolei, 侯延保, sunnana_004434229, wda1991) ❌ (0/1)(You can also ask: liubin1986, Henry_ascend, 王涛, YzQnWyx, WangHuiH)
tests/ut/aicpu_sched ❌ (0/2)(You can also ask: yanmingxiang, 张子菁, zhangpengpeng8, 侯延保, 王涛) ❌ (0/1)(You can also ask: LiWei79, ZhaiPeiChao)
tests/ut/runtime ❌ (0/2)(You can also ask: wda1991, vaceciliachen, YzQnWyx, liubin1986, zhou-wuxiao) ❌ (0/1)(You can also ask: wangluyao17, WangHuiH, weiying_101, 王涛, 卢煜坤)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

FengHaozhan, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robot成员
18 天前 将SongAnyin,zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,vaceciliachen,yanmingxiang,weiying_101,LiWei79,turing_yhy,ZhaiPeiChao,Henry_ascend,Reyn52166,dragon_q,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,tingwood,liubin1986设为评审人
CANN-robotCANN-robot成员
18 天前 将SongAnyin,zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,vaceciliachen,yanmingxiang,weiying_101,LiWei79,turing_yhy,ZhaiPeiChao,Henry_ascend,Reyn52166,dragon_q,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,tingwood,liubin1986设为审查人
FengHaozhan
FengHaozhan成员18 天前进行代码检视1
src/runtime/feature/runtime_thread_aicpu/runtime_thread_aicpu.cc
@@ -285,29 +373,30 @@
285373 
374+ ~RuntimeThreadAicpuAdapter() noexcept
375+ {
376+ if (monitorTaskRegistered_ && (pluginApi_ != nullptr)) {
377+ (void)UnregisterStarsMonitorTask(&MonitorRuntimeThreadAicpuExecution);
378+ g_monitorPluginApi.store(nullptr, std::memory_order_release);
379+ }
380+ if (streamObserverRegistered_) {
381+ (void)StreamStateCallbackManager::Instance().RegStreamStateCallback(
382+ STREAM_OBSERVER_NAME, nullptr, nullptr, cce::runtime::StreamStateCallback::RTS_STREAM_STATE_CALLBACK);
383+ }
384+ }
385+ 
386+ rtError_t SetExecuteTimeout(const uint64_t timeoutUs)
387+ {
388+ const std::lock_guard<std::mutex> lock(mutex_);
389+ executeTimeoutUs_ = timeoutUs;
390+ if (pluginApi_ != nullptr) {
391+ pluginApi_->setExecuteTimeout(timeoutUs);
392+ }
393+ return RT_ERROR_NONE;
394+ }
395+ 
286396 rtError_t PrepareKernel(
287397 const RuntimeThreadAicpuKernelRequest& request, RuntimeThreadAicpuPreparedKernel& preparedKernel)
288398 {
@@ -354,22 +464,27 @@ private:
354464 const bool prepareValid = (pluginApi_ != nullptr) && (pluginApi_->prepareKernel != nullptr);
355465 const bool releaseValid = (pluginApi_ != nullptr) && (pluginApi_->releasePreparedKernel != nullptr);
356466 const bool streamDestroyedValid = (pluginApi_ != nullptr) && (pluginApi_->streamDestroyed != nullptr);
467+ const bool setTimeoutValid = (pluginApi_ != nullptr) && (pluginApi_->setExecuteTimeout != nullptr);
FengHaozhan
FengHaozhan18 天前评论:

🟡 Medium:在确认 apiSize 足够前读取了新增尾部回调。

setTimeoutValid 和 monitorValid 会先解引用 pluginApi_->setExecuteTimeout / monitorExecutionTimeout,而短表检查要到后面的 apiSize < sizeof(RuntimeThreadAicpuPluginApi) 才执行。若插件返回的确实是旧版或截短表,这里会越过对象边界读取,产生未定义行为;当前短表 UT 只是把完整 g_pluginApi 的 structSize 改小,因此覆盖不到该越界。

建议先计算 apiTableValid = (pluginApi_ != nullptr) && (apiSize >= sizeof(RuntimeThreadAicpuPluginApi)),再通过短路条件读取所有回调,并补一个使用真实短存储的回归用例。

likedislike
FengHaozhan
FengHaozhan成员18 天前进行代码检视1
src/runtime/feature/runtime_thread_aicpu/runtime_thread_aicpu.cc
@@ -242,0 +299,4 @@
299+ return;
300+ }
301+ 
302+ const std::lock_guard<std::mutex> lock(g_streamErrorMutex);
FengHaozhan
FengHaozhan18 天前评论:

🟡 Medium:这把局部互斥锁没有覆盖 Stream::errCode_ 的全部访问,新增超时上报仍存在数据竞争。

该函数由 Stars 监控线程调用并在这里读写 errCode_;但 Stream::GetError() 会在同步调用线程中直接读取并清零同一个普通 uint32_t,其他 SetErrCode() 调用点也不持有 g_streamErrorMutex。因此这把锁只能串行化两个 RuntimeThread hook,不能为 errCode_ 建立完整的 happens-before 关系,既可能触发 C++ 未定义行为,也可能使这里的错误优先级检查被并发写入破坏。

建议把“按优先级设置错误”下沉为 Stream 自身的同步操作,并让所有读写走同一把锁或原子 CAS;同时增加监控线程与同步线程并发的回归用例。

likedislike
FengHaozhanFengHaozhan成员
18 天前 预合并成功(commit_id: 087d129aebb35a80c32ea49f67221552513d5f03)
FengHaozhanFengHaozhan成员
18 天前 强制推送  1 个提交:c11dc4e6-arch5162 aicpu timeout
FengHaozhanFengHaozhan成员
18 天前 预合并成功(commit_id: e75351a50cddfa002f358f836c6c0613a2ba3d23)
CANN-robotCANN-robot成员
18 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
18 天前 添加了label:cann-cla/yes
yanhaiyan成员18 天前进行代码检视2
src/runtime/api/impl/api_impl.cc
已过期
@@ -5459,33 +5459,6 @@ rtError_t ApiImpl::GetOpTimeOutInterval(uint64_t* interval)
54595459 return RT_ERROR_NONE;
54605460}
54615461 
5462-rtError_t ApiImpl::SetOpExecuteTimeOutV2(uint64_t timeout, uint64_t* actualTimeout)
yanhaiyan18 天前评论:

#include "task_execute_time.h" 在 SetOpExecuteTimeOutV2 迁出后已无任何符号使用,
应删除。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/runtime/api/impl/api_impl_arch5162.cc
@@ -58,0 +75,4 @@
75+ TransExeTimeoutCfgToKernelCredit(timeout, credit);
76+ *actualTimeout = static_cast<uint64_t>(static_cast<double>(credit) * timeoutCfg.interval);
77+ }
78+ ERROR_RETURN_MSG_INNER(
yanhaiyan18 天前评论:

error是只打用户 format,不自动附加错误码,失败时无法定位具体返回值。建议和SetTimeoutConfig一样,增加error打印

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

函数返回及是具体返回值

yanhaiyan成员18 天前进行代码检视2
src/runtime/api/impl/api_impl_arch5162.cc
@@ -58,0 +76,4 @@
76+ *actualTimeout = static_cast<uint64_t>(static_cast<double>(credit) * timeoutCfg.interval);
77+ }
78+ ERROR_RETURN_MSG_INNER(
79+ SetRuntimeThreadAicpuExecuteTimeout(this, *actualTimeout), "Failed to set RuntimeThreadAicpu execute timeout.");
yanhaiyan18 天前评论:

arch5162和非arch5162的差异仅在当前2行,配置超时不是性能敏感的热路径,建议当前函数改成弱符号。所有芯片共一个函数,函数中进行地址非空判断,便于后续统一维护,也便于其他芯片扩展。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已记检视意见,当前需要按文件拆分实现编译隔离,后续再看下有无更合理的方案,单独提PR修改

FengHaozhan
FengHaozhan成员
18 天前 评论:

compile

likedislike
FengHaozhanFengHaozhan成员
18 天前 预合并成功(commit_id: e65f8be1a6b3f30f17995923e57c96a88185b446)
yanhaiyan成员18 天前进行代码检视2
pkg_inc/aicpu_sched/runtime_thread_aicpu_plugin.h
@@ -29,6 +29,11 @@ enum class RuntimeThreadAicpuSqeSubtype : uint32_t {
2929 AICPU = 22U,
3030};
3131 
32+enum class RuntimeThreadAicpuStreamErrorType : uint32_t {
yanhaiyan18 天前评论:

当前头文件仅用于runtime,pkg_inc 为对外发布头文件,放在这里后要保证后续兼容性。建议将这个头文件放到runtime仓。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

该类型同时被 Runtime adapter 和 AICPU 插件使用,属于双方共享的插件 ABI,不是插件单侧私有结构;该头文件也未作为 Runtime 公共 C API 对外发布

CANN-robot
CANN-robot成员
18 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
18 天前 添加了label:ci-pipeline-running
yanhaiyan成员18 天前进行代码检视2
pkg_inc/aicpu_sched/runtime_thread_aicpu_plugin.h
@@ -80,2 +87,4 @@
8087 void (*releasePreparedKernel)(uint64_t taskCookie);
8188 void (*streamDestroyed)(void* streamHandle);
89+ void (*setExecuteTimeout)(uint64_t timeoutUs);
90+ void (*monitorExecutionTimeout)();
yanhaiyan18 天前评论:

void (*monitorExecutionTimeout)(); 空参数列表未写 (void)。头文件含 extern "C" 声明属对外 C 接口,若被纯 C 翻译单元引用,() 为非原型(K&R)声明失去参数校验;建议统一写 (void)。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

保留 void (*monitorExecutionTimeout)()。这个头文件使用了 、enum class、using 等 C++ 语法,纯 C 翻译单元本身无法包含;在 C++ 中 () 已明确表示无参数。extern "C" 只控制导出函数的语言链接,不会把整个头文件变成 C 接口,因此 K&R 非原型问题不成立。

yanhaiyan成员18 天前进行代码检视2
src/aicpu_sched/runtime_thread_aicpu/runtime_thread_aicpu_service.cc
@@ -452,0 +475,4 @@
475+ 
476+void RuntimeThreadAicpuService::SetExecuteTimeout(const uint64_t timeoutUs)
477+{
478+ executeTimeoutUs_.store(timeoutUs, std::memory_order_release);
yanhaiyan18 天前评论:

SetExecuteTimeout 对 timeoutUs 无取值校验:0 会使所有任务立即判定超时(elapsed >= 0 恒真),与 runtime 侧 never-timeout=max 的哨兵语义共同构成协议,但 0 值是否合法无校验、无注释。建议至少在注释中写明 0/max 两哨兵的语
义,或对 0 返回 INVALID_PARAM(需与 runtime 侧行为对齐后决定)。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已记遗留检视意见,对于传入0,以及传入值过大的max,当前没有限定,这个需要确定方案后,单独提PR修改

yanhaiyan成员18 天前进行代码检视2
src/aicpu_sched/runtime_thread_aicpu/runtime_thread_aicpu_service.cc
@@ -452,0 +536,4 @@
536+ try {
537+ timeoutTasks.reserve(executingTasks_.size());
538+ } catch (const std::bad_alloc&) {
539+ aicpusd_err("Collect RuntimeThreadAicpu timeout tasks failed because memory allocation failed.");
yanhaiyan18 天前评论:

要不要打印task或者device的关键信息如executingTasks_.size()

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/aicpu_sched/runtime_thread_aicpu/runtime_thread_aicpu_service.cc
@@ -452,0 +534,4 @@
534+ {
535+ const std::lock_guard<std::mutex> lock(executionMutex_);
536+ try {
537+ timeoutTasks.reserve(executingTasks_.size());
yanhaiyan18 天前评论:

executingTasks_ 为空时仍无条件调用 clockGetTimeUs + 加锁 + 构造空 vector。请确认是否要先判空(或 atomic 计数)快速返回。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/aicpu_sched/runtime_thread_aicpu/runtime_thread_aicpu_service.cc
@@ -452,0 +509,4 @@
509+ return RuntimeThreadAicpuStatus::NO_MEMORY;
510+ } catch (...) {
511+ aicpusd_err(
512+ "Start AICPU execution monitoring failed, task_cookie=%llu, stream_id=%u, task_id=%u.",
yanhaiyan18 天前评论:

是否可以打印异常类型,便于定位

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/aicpu_sched/runtime_thread_aicpu/runtime_thread_aicpu_service.hpp
@@ -60,0 +68,4 @@
68+ bool timeoutReported = false;
69+ };
70+ 
71+ class ExecutionStateGuard final {
yanhaiyan18 天前评论:

ExecutionStateGuard 未显式删除拷贝构造:引用成员 service_ 仅使拷贝赋值被隐式删除(引用无法重绑定),拷贝构造仍隐式生成(原样复制引用,ExecutionStateGuard copy = guard; 可编译;类含用户声明析构,属 deprecated
隐式生成,GCC 9+ 在 -Wextra 下有 -Wdeprecated-copy 类告警)。
误拷贝的后果:副本析构会对同一 taskCookie 提前/重复执行 EndExecution——若副本先析构(临时对象/内层作用域),任务被提前移出 executingTasks_,ExecuteKernel 仍在执行而超时监控已失效。
建议 ExecutionStateGuard(const ExecutionStateGuard&) = delete;。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/runtime/core/inc/stars/stars.hpp
@@ -36,0 +36,4 @@
36+ 
37+// Monitor tasks run under the registry lock and must not register or unregister monitor tasks.
38+rtError_t RegisterStarsMonitorTask(StarsMonitorTask task);
39+rtError_t UnregisterStarsMonitorTask(StarsMonitorTask task);
yanhaiyan18 天前评论:

函数定义在枚举和常量之间,是否和最后的函数放到一起更合适?

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/runtime/core/src/engine/stars/stars_engine.cc
@@ -1884,0 +1918,4 @@
1918+ try {
1919+ task(dev);
1920+ } catch (...) {
1921+ RT_LOG(RT_LOG_ERROR, "Stars monitor task threw an exception.");
yanhaiyan18 天前评论:

没有任何task信息、dev数据、错误信息,无法定位哪个任务异常。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

已修改

yanhaiyan成员18 天前进行代码检视2
src/runtime/feature/runtime_thread_aicpu/runtime_thread_aicpu.cc
@@ -285,1 +365,4 @@
285365 
366+ ~RuntimeThreadAicpuAdapter() noexcept
367+ {
368+ if (monitorTaskRegistered_ && (pluginApi_ != nullptr)) {
yanhaiyan18 天前评论:

~RuntimeThreadAicpuAdapter() 的注销逻辑不可达:GetAdapter(526-530)为函数级 static 泄漏式单例,new 后无任何 delete 路径,生产永不析构;当前
仅 UT 局部对象触发。虽然插件 .so 与 g_pluginService 同为进程级永生对象,但析构代码给读者"退出期会注销"的错误预期。
建议删除析构并注释单例永生设计,或提供显式 Destroy。

likedislike
FengHaozhan
FengHaozhan成员
17 天前 评论:

当前adapter的设计模式就是永不退出,当前先不做修改,后续架构有变动同步改

FengHaozhanFengHaozhan成员
18 天前 预合并成功(commit_id: bfd3662442903d5a6a1f203a702a9ac7841e70ef)
此处折叠了6条事件消息 查看更多
FengHaozhanFengHaozhan成员
17 天前 关闭了 pull request