Merged
【PR】: support profiling for mem-api #3644
xiachanglinAdd on Jul 14
【PR】: support profiling for mem-api #3644
Merged
xiachanglinAdd on Jul 14
xiachanglin
xiachanglinCollaborators
Jul 14

Pull Request

描述

  1. 新增内存类 API profiling compact 上报能力,覆盖 memcpy类、memset类、memmng类等接口。
  2. 补充扩展上报数据结构和上报流程,支持将新增 compact 数据正确落盘。
  3. batch memcpy 按 copyKind 聚合上报。
  4. 涉及接口:
    memmng(OperationType/address/bytes/memoryType/deviceId/streamId):
  • aclrtMalloc
  • aclrtMallocAlign32
  • aclrtMallocCached
  • aclrtMallocWithCfg
  • aclrtMallocForTaskScheduler
  • aclrtFree
  • aclrtFreeWithDevSync
  • aclrtReserveMemAddress
  • aclrtReserveMemAddressNoUCMemory
  • aclrtReleaseMemAddress
  • aclrtMemAllocManaged
  • aclrtMallocPhysical
  • aclrtFreePhysical
  • aclrtMallocHostWithCfg
  • aclrtMallocHost
  • aclrtFreeHost
  • aclrtFreeHostWithDevSync

memset(value/bytes/deviceId/streamId):

  • aclrtMemset
  • aclrtMemsetAsync
  • aclrtMemsetD32
  • aclrtMemsetD32Async

memcpy(copyKind/bytes/deviceId/streamId):

  • aclrtMemcpy
  • aclrtMemcpyAsync
  • aclrtMemcpyAsyncWithCondition
  • aclrtMemcpyBatch
  • aclrtMemcpyBatchAsync
  • aclrtMemcpy2d
  • aclrtMemcpy2dAsync
  • aclrtMemcpyAsyncWithOffset
  • aclrtMemcpyBatchV2
  • aclrtMemcpyBatchAsyncV2
  • aclrtSetMemcpyDesc(用于替代aclrtMemcpyAsyncWithDesc接口信息)

变更类型

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

关联的Issue

如何测试

  1. RDV已通过
  2. hlt已通过
  3. UT已通过

核对清单

其他信息

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

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xiachanglin's contribution.)
xiachanglinxiachanglinCollaborators
Jul 14 Added label: enhancement
xiachanglinxiachanglinCollaborators
Jul 14 Created pull request, commit 6f57f198
atomgit-bot
atomgit-bot
Jul 14 Comments:

变更摘要

本次 PR 主要为 runtime 的 mem-api 增加了 profiling 支持,通过扩展 MsprofCompactInfo 联合体、新增 profiling 数据类型与扩展信息结构体,并在 ApiProfileDecorator 中为内存分配/释放、memcpy、memset 等 API 挂载扩展信息上报逻辑,使 profiler 能够在运行时捕获并上报内存操作的详细信息(字节数、拷贝方向、设备 ID、流 ID 等)。同时还支持了 batch memcpy 按拷贝类型聚合上报,并新增了相应的单元测试。

主要改动

  • 新增内存操作 profiling 数据结构: 在 aprof_pub.h 中定义 MsprofMemcpyInfo、MsprofMemsetInfo、MsprofMemMngInfo 三个结构体,并加入 MsprofCompactInfo.data 联合体,同时通过 static_assert 确保其大小不超过 MSPROF_COMPACT_INFO_DATA_LENGTH。

  • 扩展 profiling 类型体系: 在 profiler_struct.hpp 中新增 RT_PROF_API_SET_MEMCPY_DESC API 类型、RT_PROFILE_TYPE_MEMCPY_EXT_INFO/RT_PROFILE_TYPE_MEMSET_INFO/RT_PROFILE_TYPE_MEMMNG_INFO 三种数据类型、RuntimeMemMngType 枚举,以及 RuntimeProfExtInfo 联合体和 RuntimeProfExtInfoItem 结构,同时扩展 RuntimeProfApiData 以支持单个扩展信息和批量扩展信息数组。

  • 实现扩展信息上报逻辑: 在 profiling_agent.cc 中新增 GetRuntimeExtInfoLen、SetRuntimeExtCompactData、ReportRuntimeExtCompactInfo 三个辅助函数,并在 ReportProfApi 中集成单个扩展信息与批量扩展信息的上报流程,同时注册了新的 profiling 类型名称。

  • 在 ApiProfileDecorator 中挂载内存 profiling 钩子: 在 api_profile_decorator.cc 中新增 FillMemcpyExtInfo、FillMemsetExtInfo、FillMemMngExtInfo、FillMemcpyBatchExtInfoByCopyKind 等方法,并在 DevMalloc、DevFree、HostMalloc、HostFree、MemCopySync、MemcpyAsync、MemsetD32、MemcpyBatch 等大量内存相关 API 的成功路径上调用相应的 Fill 方法,同时新增 HostMallocWithCfg、SetMemcpyDesc、ReserveMemAddress、ReleaseMemAddress、MallocPhysical、FreePhysical 等方法的重写。

  • 新增单元测试覆盖: 在 rt_utest_agent.cc 中新增 PROF_REPORT_EXT_MEMCPY_INFO、PROF_REPORT_EXT_MEMSET_INFO、PROF_REPORT_EXT_MEMMNG_INFO、PROF_REPORT_BATCH_EXT_MEMCPY_INFO 等测试用例;在 rt_utest_profiler.cc 中新增 ApiProfileDecoratorMemCopySyncExtInfo、ApiProfileDecoratorMemcpyBatchMultiKindExtInfo 等测试用例,覆盖单条与批量扩展信息的上报正确性。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 14 Comments:

代码审查

All seven files have been reviewed. Let me write the closing summary.


审查总结

审查覆盖的文件

文件 审查结论
pkg_inc/profiling/aprof_pub.h 无问题 — 新增结构体 MsprofMemcpyInfo、MsprofMemsetInfo、MsprofMemMngInfo 已正确加入 MsprofCompactInfo union,static_assert 大小检查均通过 (≤40字节)
src/runtime/core/inc/common/profiler_struct.hpp 无问题 — 新增枚举 RuntimeMemMngType、联合体 RuntimeProfExtInfo、结构体 RuntimeProfExtInfoItem、常量 RUNTIME_PROF_BATCH_EXT_INFO_NUM,以及 RuntimeProfApiData 新增字段,定义一致
src/runtime/core/src/common/profiling_agent.cc 无问题 — ReportRuntimeExtCompactInfo、SetRuntimeExtCompactData、GetRuntimeExtInfoLen 逻辑正确;ReportProfApi 的 extInfo 和 batch 上报流程正确;新类型注册正确;同时修复了 GetDevArgsAddr 和 MemcpyHostTask 缺少 CallApiEnd/return 的已有 bug
src/runtime/core/src/profiler/api_profile_decorator.cc 发现 2 个问题 — P2 双重上报、P3 sentinel 值泄漏
src/runtime/core/src/profiler/api_profile_decorator.hpp 无问题 — 声明与实现一致
tests/ut/runtime/runtime/test/rt_utest_agent.cc 无问题 — 测试覆盖了 ext info 上报、batch 上报和无效类型路径
tests/ut/runtime/runtime/test/rt_utest_profiler.cc 无问题 — 测试覆盖了 Fill 系列函数、batch 归类和 streamId 传递

问题统计

  • P2: 1 个 — memcpy 操作双重上报 profiling 数据
  • P3: 1 个 — RT_MEMCPY_KIND_MAX sentinel 被当作有效 copyKind 上报
  • P1: 0 个
  • P0: 0 个

整体风险判断

中低风险。核心功能变更(memory API profiling)实现基本正确。主要风险在于:memcpy 操作会产生重复的 profiling 数据条目(旧格式 type 801 和新格式 type 808 同时上报),可能导致 profiling 分析工具的数据失真。建议在合入前修复此双重上报问题。batch memcpy 中 sentinel 值泄漏的问题影响较小,可在后续迭代中优化。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 14 Added label: cann-cla/yes
302 messages are collapsed here。 See more
xiachanglinxiachanglinCollaborators
Jul 28 Resolved final issue
xiachanglinxiachanglinCollaborators
Jul 28 Resolved final issue
chenhao_1209
chenhao_1209Collaborators
Jul 28 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 28 Added label: approved
CANN-robotCANN-robotCollaborators
Jul 28 Merged pull request