LCOV - code coverage report
Current view: top level - aicpu_schedule/core - aicpusd_drv_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.8 % 45 44
Test Date: 2026-08-12 11:05:02 Functions: 93.3 % 15 14

            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              : #ifndef CORE_AICPUSD_DRV_MANAGER_H
      12              : #define CORE_AICPUSD_DRV_MANAGER_H
      13              : 
      14              : #include <cstdint>
      15              : #include <fcntl.h>
      16              : #include <unistd.h>
      17              : #include <vector>
      18              : #include <map>
      19              : #include <string>
      20              : #include "aicpusd_context.h"
      21              : #include "ascend_hal.h"
      22              : #include "aicpu_context.h"
      23              : #include "aicpusd_feature_ctrl.h"
      24              : 
      25              : namespace AicpuSchedule {
      26              : // invalid aicpuid
      27              : constexpr uint32_t INVALID_AICPU_ID = 65535U;
      28              : class AicpuDrvManager {
      29              : public:
      30              :     static AicpuDrvManager& GetInstance();
      31              : 
      32              :     int32_t GetNormalAicpuDCpuInfo(const std::vector<uint32_t>& deviceVec);
      33              :     int32_t GetThreadVfAicpuDCpuInfo(const std::vector<uint32_t>& deviceVec);
      34              :     int32_t GetNormalAicpuInfo(const std::vector<uint32_t>& deviceVec);
      35              :     int32_t GetCcpuInfo(const std::vector<uint32_t>& deviceVec);
      36              : 
      37              :     /**
      38              :      * @ingroup AicpuDrvManager
      39              :      * @brief it is used to init.
      40              :      * @param [in] deviceId : device id.
      41              :      * @param [in] hostPid : host pid.
      42              :      * @param [in] vfId : vf id.
      43              :      * @param [in] hasThread : has thread flag.
      44              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      45              :      */
      46              :     int32_t InitDrvMgr(
      47              :         const std::vector<uint32_t>& deviceVec, const pid_t hostPid, const uint32_t vfId, const bool hasThread,
      48              :         const aicpu::AicpuRunMode runMode = aicpu::AicpuRunMode::INVALID_MODE, const std::string& hostProcName = "");
      49              : 
      50              :     /**
      51              :      * @ingroup AicpuDrvManager
      52              :      * @brief it is used to init driver sched module.
      53              :      * @param [in] grpId : the group id.
      54              :      * @param [in] priority : event priority
      55              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      56              :      */
      57              :     int32_t InitDrvSchedModule(const uint32_t grpId, const std::map<EVENT_ID, SCHEDULE_PRIORITY>& priority);
      58              : 
      59              :     /**
      60              :      * @ingroup AicpuDrvManager
      61              :      * @brief it is used to subscribe queue not-empty event.
      62              :      * @param [in] queueId : the queue id.
      63              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      64              :      */
      65              :     int32_t SubscribeQueueNotEmptyEvent(const uint32_t queueId) const;
      66              : 
      67              :     /**
      68              :      * @ingroup AicpuDrvManager
      69              :      * @brief it is used to un-subscribe queue not-empty event.
      70              :      * @param [in] queueId : the queue id.
      71              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      72              :      */
      73              :     int32_t UnSubscribeQueueNotEmptyEvent(const uint32_t queueId) const;
      74              : 
      75              :     /**
      76              :      * @ingroup AicpuDrvManager
      77              :      * @brief it is used to subscribe queue not-full event.
      78              :      * @param [in] queueId : the queue id.
      79              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      80              :      */
      81              :     int32_t SubscribeQueueNotFullEvent(const uint32_t queueId) const;
      82              : 
      83              :     /**
      84              :      * @ingroup AicpuDrvManager
      85              :      * @brief it is used to un-subscribe queue not-full event.
      86              :      * @param [in] queueId : the queue id.
      87              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      88              :      */
      89              :     int32_t UnSubscribeQueueNotFullEvent(const uint32_t queueId) const;
      90              : 
      91              :     /**
      92              :      * @ingroup AicpuDrvManager
      93              :      * @brief it is used qurey buff group info including groupname and attribute.
      94              :      * @param [in] procId : process id.
      95              :      * @param [in] buffGrpInfo : buff info include name and attribute.
      96              :      * @return AICPU_SCHEDULE_OK: success, other: error code
      97              :      */
      98              :     int32_t QueryProcBuffInfo(uint32_t procId, std::map<std::string, GroupShareAttr>& buffGrpInfo) const;
      99              : 
     100          852 :     uint32_t GetDeviceId() const { return (deviceVec_.size() == 0LU) ? 0U : deviceVec_[0]; }
     101              : 
     102          102 :     uint32_t GetUniqueVfId() const { return uniqueVfId_; }
     103              : 
     104          332 :     pid_t GetHostPid() const
     105              :     {
     106          332 :         if (isInit_) {
     107          317 :             return hostPid_;
     108              :         }
     109           15 :         return drvDeviceGetBareTgid();
     110              :     }
     111              : 
     112          192 :     uint32_t GetVfId() const { return vfId_; }
     113              : 
     114            2 :     uint32_t GetGroupId() const { return groupId_; }
     115              : 
     116           16 :     bool HasThread() const { return hasThread_; }
     117              : 
     118              :     std::vector<uint32_t> GetDeviceList() const;
     119              : 
     120              :     uint32_t GetAicpuNum() const;
     121              : 
     122              :     uint32_t GetAicpuNumPerDevice() const;
     123              : 
     124              :     uint32_t GetAicpuPhysIndex(const uint32_t aicpuLogIndex, const uint32_t deviceId) const;
     125              : 
     126              :     std::vector<uint32_t> GetAicpuPhysIndexs(const uint32_t deviceId);
     127              : 
     128            1 :     uint32_t GetAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
     129              :     {
     130            1 :         if (FeatureCtrl::IsVfModeDie1(deviceId)) {
     131            0 :             return coreNumPerDev_ + aicpuIdVec_[static_cast<size_t>(aicpuLogIndex)];
     132              :         }
     133            1 :         return aicpuIdVec_[static_cast<size_t>(aicpuLogIndex)];
     134              :     }
     135              : 
     136            4 :     void GetDcpuRange(uint32_t& dcpuBase, uint32_t& dcpuNum) const
     137              :     {
     138            4 :         dcpuBase = dcpuBase_;
     139            4 :         dcpuNum = dcpuNum_;
     140            4 :     }
     141              : 
     142              :     int32_t BindHostPid(
     143              :         const std::string& signStr, const int32_t mode, const uint32_t vfId,
     144              :         const enum devdrv_process_type cpType) const;
     145              : 
     146              :     int32_t CheckBindHostPid() const;
     147              : 
     148            3 :     inline std::string GetHostProcName() const { return hostProcName_; }
     149              : 
     150           11 :     uint32_t GetCcpuPhysIndex(const uint32_t ccpuLogIndex, const uint32_t deviceId) const
     151              :     {
     152           11 :         if (ccpuLogIndex >= ccpuIdVec_.size()) {
     153            1 :             aicpusd_err("Invalid ccpu index %u %zu", ccpuLogIndex, ccpuIdVec_.size());
     154            1 :             return 0;
     155              :         }
     156           10 :         const uint32_t phyIndex = (coreNumPerDev_ * deviceId) + ccpuIdVec_[ccpuLogIndex];
     157           10 :         return phyIndex;
     158              :     }
     159              : 
     160           17 :     std::vector<uint32_t> GetCcpuList() { return ccpuIdVec_; }
     161              : 
     162           11 :     uint32_t GetCcpuNum() const { return static_cast<uint32_t>(ccpuIdVec_.size()); }
     163              : 
     164              : private:
     165           12 :     AicpuDrvManager()
     166           12 :         : deviceVec_({}),
     167           12 :           deviceNum_(0UL),
     168           12 :           coreNumPerDev_(0U),
     169           12 :           hostPid_(0),
     170           12 :           groupId_(0U),
     171           12 :           hasThread_(false),
     172           12 :           isInit_(false),
     173           12 :           aicpuNumPerDev_(0U),
     174           12 :           aicpuBaseId_(0U),
     175           12 :           dcpuBase_(0U),
     176           12 :           dcpuNum_(0U),
     177           12 :           vfId_(0U),
     178           12 :           uniqueVfId_(0U),
     179           24 :           hostProcName_(""),
     180           24 :           aicpuIdVec_({}),
     181           24 :           aicpuPhyIds_({}),
     182           48 :           ccpuIdVec_({}){};
     183              : 
     184           12 :     virtual ~AicpuDrvManager() = default;
     185              : 
     186              :     AicpuDrvManager(const AicpuDrvManager&) = delete;
     187              : 
     188              :     AicpuDrvManager& operator=(const AicpuDrvManager&) = delete;
     189              : 
     190              :     int32_t InitDrvZone() const;
     191              : 
     192              :     void DettachAllDevice(const size_t deviceIndex) const;
     193              : 
     194              :     int32_t AicpuGetDeviceInfo(
     195              :         uint32_t deviceId, const DEV_MODULE_TYPE moduleType, const DEV_INFO_TYPE infoType, int64_t* value) const;
     196              : 
     197              :     void InitDrvMgrCaluniqueVfId(const std::vector<uint32_t>& deviceVec, const uint32_t vfId);
     198              : 
     199              :     void InitSocType(const uint32_t deviceId);
     200              : 
     201              :     bool IsSvmShareGrp(const std::string& grpName) const;
     202              : 
     203              :     // the id of the chip.
     204              :     std::vector<uint32_t> deviceVec_;
     205              : 
     206              :     // device num
     207              :     size_t deviceNum_;
     208              : 
     209              :     // core num of device
     210              :     uint32_t coreNumPerDev_;
     211              : 
     212              :     // the pid of host process
     213              :     pid_t hostPid_;
     214              : 
     215              :     // the id is used to group in process.
     216              :     uint32_t groupId_;
     217              : 
     218              :     // it is used to mark which is in call mode or multi-thread mode.
     219              :     bool hasThread_;
     220              : 
     221              :     // init function called or not, lhisi is will be false
     222              :     bool isInit_;
     223              : 
     224              :     // aicpu core num
     225              :     uint32_t aicpuNumPerDev_;
     226              : 
     227              :     // aicpu base index
     228              :     uint32_t aicpuBaseId_;
     229              : 
     230              :     // dcpu base index
     231              :     uint32_t dcpuBase_;
     232              : 
     233              :     // dcpu number
     234              :     uint32_t dcpuNum_;
     235              : 
     236              :     // vf id
     237              :     uint32_t vfId_;
     238              : 
     239              :     // calc by deviceId and vfId for vfId uniqueization
     240              :     uint32_t uniqueVfId_;
     241              : 
     242              :     // host proc name
     243              :     std::string hostProcName_;
     244              : 
     245              :     // aicpu index
     246              :     std::vector<uint32_t> aicpuIdVec_;
     247              : 
     248              :     // aicpu physic index
     249              :     std::vector<uint32_t> aicpuPhyIds_;
     250              : 
     251              :     // ccpu index
     252              :     std::vector<uint32_t> ccpuIdVec_;
     253              : };
     254              : } // namespace AicpuSchedule
     255              : 
     256              : #endif // CORE_AICPUSD_DRV_MANAGER_H
        

Generated by: LCOV version 2.0-1