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

Generated by: LCOV version 2.0-1