LCOV - code coverage report
Current view: top level - aicpu_sharder - aicpu_timer.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.2 % 84 80
Test Date: 2026-08-12 11:05:02 Functions: 100.0 % 12 12

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 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 "aicpu_sharder.h"
      12              : #include "aicpu_context.h"
      13              : #include "aicpu_sharder_log.h"
      14              : #include "aicpu_timer.h"
      15              : 
      16              : namespace aicpu {
      17              : 
      18          115 : AicpuTimer& AicpuTimer::GetInstance()
      19              : {
      20          115 :     static AicpuTimer instance;
      21          115 :     return instance;
      22              : }
      23              : 
      24           49 : void AicpuTimer::SetSupportTimer(const bool flag) { isSupportTimer_ = flag; }
      25              : 
      26           40 : void AicpuTimer::RegistMonitorFunc(const StartMonitorFunc& startFunc, const StopMonitorFunc& stopFunc)
      27              : {
      28           40 :     startTimerFunc_ = startFunc;
      29           40 :     stopTimerFunc_ = stopFunc;
      30              : 
      31           40 :     return;
      32              : }
      33              : 
      34           12 : TimerStatus AicpuTimer::StartTimer(TimerHandle& timerHandle, const TimeoutCallback& callback, const uint32_t timeInS)
      35              : {
      36           12 :     if (!isSupportTimer_) {
      37            1 :         AICPUE_LOGI("No need start timer");
      38            1 :         return TimerStatus::AICPU_TIMER_SUCCESS;
      39              :     }
      40              : 
      41           11 :     if (callback == nullptr) {
      42            1 :         AICPUE_LOGE("Async timer callback is null.");
      43            1 :         return TimerStatus::AICPU_TIMER_FAILED;
      44              :     }
      45              : 
      46              :     {
      47           10 :         const std::lock_guard<std::mutex> lk(timerHandleMutex_);
      48           10 :         ++timerHandleCnt_;
      49           10 :         timerHandle = timerHandleCnt_;
      50           10 :     }
      51              : 
      52           10 :     TimerStatus ret = RegistTimeoutCallback(timerHandle, callback);
      53           10 :     if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
      54            1 :         AICPUE_LOGE(
      55              :             "Register op timeout callback func failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret),
      56              :             timerHandle);
      57            1 :         return ret;
      58              :     }
      59              : 
      60            9 :     ret = StartTimerInMonitor(timerHandle, timeInS);
      61            9 :     if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
      62            1 :         AICPUE_LOGE("Start op timer failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret), timerHandle);
      63            1 :         return ret;
      64              :     }
      65              : 
      66            8 :     AICPUE_LOGI("Start op timer success, TimerHandle=%lu.", timerHandle);
      67              : 
      68            8 :     return ret;
      69              : }
      70              : 
      71            6 : TimerStatus AicpuTimer::StopTimer(const TimerHandle timerHandle)
      72              : {
      73            6 :     if ((!isSupportTimer_) && (timeoutCbkMap_.empty())) {
      74            1 :         AICPUE_LOGI("No need stop timer");
      75            1 :         return TimerStatus::AICPU_TIMER_SUCCESS;
      76              :     }
      77              : 
      78            5 :     TimerStatus ret = StopTimerInMonitor(timerHandle);
      79            5 :     if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
      80            2 :         AICPUE_LOGE("Stop op timer failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret), timerHandle);
      81            2 :         return ret;
      82              :     }
      83              : 
      84            3 :     ret = UnregistTimeoutCallback(timerHandle);
      85            3 :     if (ret != TimerStatus::AICPU_TIMER_SUCCESS) {
      86            2 :         AICPUE_LOGE(
      87              :             "Unregister op timeout callback func failed, ret=%d, TimerHandle=%lu.", static_cast<int32_t>(ret),
      88              :             timerHandle);
      89            2 :         return ret;
      90              :     }
      91              : 
      92            1 :     AICPUE_LOGI("Stop op timer success, TimerHandle=%lu.", timerHandle);
      93              : 
      94            1 :     return ret;
      95              : }
      96              : 
      97            2 : void AicpuTimer::CallTimeoutCallback(const TimerHandle timerHandle)
      98              : {
      99              :     {
     100            2 :         const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
     101            2 :         const auto iter = timeoutCbkMap_.find(timerHandle);
     102            2 :         if (iter == timeoutCbkMap_.end()) {
     103            2 :             AICPUE_LOGE("Call timeout callback func, TimerHandle=%lu.", timerHandle);
     104            2 :             return;
     105              :         }
     106              : 
     107            0 :         iter->second();
     108            2 :     }
     109              : 
     110            0 :     return;
     111              : }
     112              : 
     113           10 : TimerStatus AicpuTimer::RegistTimeoutCallback(const TimerHandle timerHandle, const TimeoutCallback& callback)
     114              : {
     115              :     {
     116           10 :         const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
     117           10 :         const auto ret = timeoutCbkMap_.emplace(timerHandle, callback);
     118           10 :         if (!ret.second) {
     119            1 :             AICPUE_LOGE("Register timeout callback func in map failed, TimerHandle=%lu.", timerHandle);
     120            1 :             return TimerStatus::AICPU_TIMER_FAILED;
     121              :         }
     122           10 :     }
     123              : 
     124            9 :     return TimerStatus::AICPU_TIMER_SUCCESS;
     125              : }
     126              : 
     127            1 : TimerStatus AicpuTimer::UnregistTimeoutCallback(const TimerHandle timerHandle)
     128              : {
     129              :     {
     130            1 :         const std::lock_guard<std::mutex> lk(timeoutCbkMapMutex_);
     131            1 :         const auto eraseNum = timeoutCbkMap_.erase(timerHandle);
     132            1 :         if (eraseNum <= 0) {
     133            0 :             AICPUE_LOGE("Erase op callback in map failed, TimerHandle=%lu.", timerHandle);
     134            0 :             return TimerStatus::AICPU_TIMER_FAILED;
     135              :         }
     136            1 :     }
     137              : 
     138            1 :     return TimerStatus::AICPU_TIMER_SUCCESS;
     139              : }
     140              : 
     141           10 : TimerStatus AicpuTimer::StartTimerInMonitor(const TimerHandle timerHandle, const uint32_t timeInS) const
     142              : {
     143           10 :     if (startTimerFunc_ == nullptr) {
     144            1 :         AICPUE_LOGE("Start op timer failed by nullptr.");
     145            1 :         return TimerStatus::AICPU_TIMER_FAILED;
     146              :     }
     147              : 
     148            9 :     startTimerFunc_(timerHandle, timeInS);
     149              : 
     150            9 :     return TimerStatus::AICPU_TIMER_SUCCESS;
     151              : }
     152              : 
     153            5 : TimerStatus AicpuTimer::StopTimerInMonitor(const TimerHandle timerHandle) const
     154              : {
     155            5 :     if (stopTimerFunc_ == nullptr) {
     156            1 :         AICPUE_LOGE("Stop op timer failed by nullptr");
     157            1 :         return TimerStatus::AICPU_TIMER_FAILED;
     158              :     }
     159              : 
     160            4 :     stopTimerFunc_(timerHandle);
     161              : 
     162            4 :     return TimerStatus::AICPU_TIMER_SUCCESS;
     163              : }
     164              : 
     165            1 : bool StartTimer(TimerHandle& timerHandle, const TimeoutCallback& callBack, const uint32_t timeInS)
     166              : {
     167            1 :     return AicpuTimer::GetInstance().StartTimer(timerHandle, callBack, timeInS) == TimerStatus::AICPU_TIMER_SUCCESS ?
     168              :                true :
     169            1 :                false;
     170              : }
     171              : 
     172            1 : void StopTimer(const TimerHandle timerHandle) { (void)AicpuTimer::GetInstance().StopTimer(timerHandle); }
     173              : } // namespace aicpu
        

Generated by: LCOV version 2.0-1