已合并
【双合】【bugfix】【profiling】兼容旧的获取device id接口 #303
【双合】【bugfix】【profiling】兼容旧的获取device id接口 #303
已合并
ChenHuiwen创建于 4月14日
共 2 个文件变更+41-6
@@ -417,6 +417,26 @@ void AddMetaInfo(const char *key, const char *value)
417 msServiceProfiler::InsertExecutor2Writer<msServiceProfiler::DBFile::SERVICE>(std::move(executor));417 msServiceProfiler::InsertExecutor2Writer<msServiceProfiler::DBFile::SERVICE>(std::move(executor));
418}418}
419 419 
420+static bool g_usedNewDeviceStateApi = false;
421+ 
422+void MsprofSetDeviceCallbackLegacy(DATA_PTR data, uint32_t len)
423+{
424+ if (len != sizeof(::ProfSetDevParaDevice)) {
425+ return;
426+ }
427+ if (data == nullptr) {
428+ return;
429+ }
430+ DATA_PTR setCfg = static_cast<DATA_PTR>(data);
431+ static uint32_t sdeviceID = msServiceProfiler::INVALID_DEVICE_ID;
432+ 
433+ if (setCfg->deviceId != sdeviceID) {
434+ sdeviceID = setCfg->deviceId;
435+ msServiceProfiler::ServiceProfilerManager::GetInstance().NotifyDeviceID(sdeviceID);
436+ }
437+ return;
438+}
439+ 
420int32_t MsprofSetDeviceCallbackImpl(void *data, uint32_t len)440int32_t MsprofSetDeviceCallbackImpl(void *data, uint32_t len)
421{441{
422 // 不知道什么线程来的,一切皆有可能442 // 不知道什么线程来的,一切皆有可能
@@ -438,7 +458,7 @@ int32_t MsprofSetDeviceCallbackImpl(void *data, uint32_t len)
438 458 
439static void UnregisterDeviceStateCallback(void *profApiHandle)459static void UnregisterDeviceStateCallback(void *profApiHandle)
440{460{
441- if (profApiHandle == nullptr) {461+ if (!g_usedNewDeviceStateApi || profApiHandle == nullptr) {
442 return;462 return;
443 }463 }
444 using MsprofRegisterProfileCallbackFunc = int32_t (*)(int32_t callbackType, void *callback, uint32_t len);464 using MsprofRegisterProfileCallbackFunc = int32_t (*)(int32_t callbackType, void *callback, uint32_t len);
@@ -448,6 +468,7 @@ static void UnregisterDeviceStateCallback(void *profApiHandle)
448 return;468 return;
449 }469 }
450 (void)registerFn(kProfileDeviceStateCCallback, nullptr, sizeof(void *));470 (void)registerFn(kProfileDeviceStateCCallback, nullptr, sizeof(void *));
471+ g_usedNewDeviceStateApi = false;
451}472}
452 473 
453static LibraryHandle RegisterSetDeviceCallback()474static LibraryHandle RegisterSetDeviceCallback()
@@ -460,13 +481,26 @@ static LibraryHandle RegisterSetDeviceCallback()
460 return LibraryHandle(nullptr);481 return LibraryHandle(nullptr);
461 }482 }
462 483 
484+ // 优先尝试旧接口 profRegDeviceStateCallback
485+ using ProfSetDeviceHandle = void (*)(DATA_PTR, uint32_t);
486+ using ProfRegDeviceStateCallbackFunc = int32_t (*)(ProfSetDeviceHandle);
487+ auto legacyFn = reinterpret_cast<ProfRegDeviceStateCallbackFunc>(
488+ dlsym(handle, "profRegDeviceStateCallback"));
489+ if (legacyFn != nullptr) {
490+ PROF_LOGD("Using legacy profRegDeviceStateCallback API"); // LCOV_EXCL_LINE
491+ legacyFn(MsprofSetDeviceCallbackLegacy);
492+ g_usedNewDeviceStateApi = false;
493+ return LibraryHandle(handle);
494+ }
495+ 
496+ // 旧接口不存在,回退到新接口 MsprofRegisterProfileCallback
463 using MsprofRegisterProfileCallbackFunc = int32_t (*)(int32_t callbackType, void *callback, uint32_t len);497 using MsprofRegisterProfileCallbackFunc = int32_t (*)(int32_t callbackType, void *callback, uint32_t len);
464 auto registerFn = reinterpret_cast<MsprofRegisterProfileCallbackFunc>(498 auto registerFn = reinterpret_cast<MsprofRegisterProfileCallbackFunc>(
465 dlsym(handle, "MsprofRegisterProfileCallback"));499 dlsym(handle, "MsprofRegisterProfileCallback"));
466 if (registerFn == nullptr) {500 if (registerFn == nullptr) {
467- PROF_LOGW("Failed to get MsprofRegisterProfileCallback from libprofapi.so." // LCOV_EXCL_LINE501+ PROF_LOGW("Failed to get profRegDeviceStateCallback or MsprofRegisterProfileCallback " // LCOV_EXCL_LINE
468- "Will be not able to get device profiling data." // LCOV_EXCL_LINE502+ "from libprofapi.so. Will be not able to get device profiling data. " // LCOV_EXCL_LINE
469- " Check whether a NPU server or if cann toolkit installed."); // LCOV_EXCL_LINE503+ "Check whether a NPU server or if cann toolkit installed."); // LCOV_EXCL_LINE
470 dlclose(handle);504 dlclose(handle);
471 return LibraryHandle(nullptr);505 return LibraryHandle(nullptr);
472 }506 }
@@ -477,6 +511,7 @@ static LibraryHandle RegisterSetDeviceCallback()
477 dlclose(handle);511 dlclose(handle);
478 return LibraryHandle(nullptr);512 return LibraryHandle(nullptr);
479 }513 }
514+ g_usedNewDeviceStateApi = true;
480 return LibraryHandle(handle);515 return LibraryHandle(handle);
481}516}
482 517 
@@ -82,7 +82,7 @@ from ms_service_profiler.patcher.sglang import register_service_profiler
82register_service_profiler()82register_service_profiler()
83 83 
84# 启动 SGLang 服务(示例)84# 启动 SGLang 服务(示例)
85-python -m sglang.launch_server \85+python3 -m sglang.launch_server \
86 --model-path=/Qwen2.5-0.5B-Instruct \86 --model-path=/Qwen2.5-0.5B-Instruct \
87 --device npu87 --device npu
88```88```
@@ -157,7 +157,7 @@ python -m sglang.launch_server \
157> 或等价的:157> 或等价的:
158>158>
159> ```bash159> ```bash
160-> python -m ms_service_profiler.parse --input-path=$PWD160+> python3 -m ms_service_profiler.parse --input-path=$PWD
161> ```161> ```
162 162 
163#### 4.2 调优分析163#### 4.2 调优分析