LCOV - code coverage report
Current view: top level - base_comm/dfx - exception_callback_mgr.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.5 % 40 39
Test Date: 2026-08-18 17:47:01 Functions: 87.5 % 8 7

            Line data    Source code
       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              : 
      11              : #include <algorithm>
      12              : #include "exception_callback_mgr.h"
      13              : #include "exception_util.h"
      14              : #include "log.h"
      15              : 
      16              : using Hccl::HcclException;
      17              : using std::exception;
      18              : using std::string;
      19              : 
      20              : namespace hcomm {
      21              : 
      22           82 : ExceptionCallbackMgr& ExceptionCallbackMgr::GetInstance()
      23              : {
      24           82 :     static ExceptionCallbackMgr instance;
      25           82 :     return instance;
      26              : }
      27              : 
      28           19 : HcclResult ExceptionCallbackMgr::Register(HcommExceptionCallback cb, void* userData)
      29              : {
      30           19 :     CHK_PTR_NULL(cb);
      31           17 :     std::unique_lock<std::shared_mutex> lock(mutex_);
      32           17 :     auto it = std::find_if(callbacks_.begin(), callbacks_.end(), [cb](const CallbackEntry& entry) {
      33            6 :         return entry.cb == cb;
      34              :     });
      35           17 :     if (it != callbacks_.end()) {
      36            2 :         it->userData = userData;
      37            2 :         HCCL_INFO("[%s] update existing cb[%p], userData[%p], total[%zu]", __func__, cb, userData, callbacks_.size());
      38            2 :         return HCCL_SUCCESS;
      39              :     }
      40           15 :     callbacks_.push_back({cb, userData});
      41           15 :     HCCL_INFO("[%s] success, cb[%p], userData[%p], total[%zu]", __func__, cb, userData, callbacks_.size());
      42           15 :     return HCCL_SUCCESS;
      43           17 : }
      44              : 
      45           50 : HcclResult ExceptionCallbackMgr::Unregister(HcommExceptionCallback cb)
      46              : {
      47           50 :     CHK_PTR_NULL(cb);
      48           47 :     std::unique_lock<std::shared_mutex> lock(mutex_);
      49           47 :     size_t beforeSize = callbacks_.size();
      50           94 :     callbacks_.erase(
      51           47 :         std::remove_if(
      52              :             callbacks_.begin(), callbacks_.end(),
      53           19 :             [cb](const CallbackEntry& entry) {
      54           19 :                 return entry.cb == cb;
      55              :             }),
      56           47 :         callbacks_.end());
      57           47 :     size_t removed = (beforeSize > callbacks_.size()) ? (beforeSize - callbacks_.size()) : 0;
      58           47 :     HCCL_INFO("[%s] cb[%p], removed[%zu], remaining[%zu]", __func__, cb, removed, callbacks_.size());
      59           47 :     return HCCL_SUCCESS;
      60           47 : }
      61              : 
      62           11 : void ExceptionCallbackMgr::NotifyAll(const HcommExceptionInfo& exceptionInfo)
      63              : {
      64           11 :     std::shared_lock<std::shared_mutex> lock(mutex_);
      65           22 :     for (const auto& entry : callbacks_) {
      66           11 :         if (entry.cb == nullptr) {
      67            0 :             continue;
      68              :         }
      69           11 :         TRY_CATCH_PRINT_ERROR(entry.cb(&exceptionInfo, entry.userData));
      70              :     }
      71           11 : }
      72              : 
      73            2 : bool ExceptionCallbackMgr::IsEmpty()
      74              : {
      75            2 :     std::shared_lock<std::shared_mutex> lock(mutex_);
      76            4 :     return callbacks_.empty();
      77            2 : }
      78              : 
      79              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1