LCOV - code coverage report
Current view: top level - aicpu_schedule/core - aicpusd_cust_so_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 12 12
Test Date: 2026-07-28 10:54:05 Functions: 100.0 % 7 7

            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_CUST_SO_MANAGER_H
      12              : #define CORE_AICPUSD_CUST_SO_MANAGER_H
      13              : 
      14              : #include <string>
      15              : #include <mutex>
      16              : #include "aicpu_context.h"
      17              : #include "aicpusd_common.h"
      18              : #include "aicpusd_info.h"
      19              : 
      20              : namespace AicpuSchedule {
      21              :     struct FileHashInfo {
      22              :         std::string filePath;
      23              :         uint64_t fileSize;
      24              :         uint64_t hashValue;
      25              :     };
      26              : 
      27              :     struct FileInfo {
      28              :         char_t *data;
      29              :         size_t size;
      30              :         std::string name;
      31              :     };
      32              : 
      33              :     class HashCalculator {
      34              :     public:
      35          129 :         HashCalculator() = default;
      36           43 :         ~HashCalculator() = default;
      37              :         bool GetSameFileInfo(const FileInfo &fileInfo, FileHashInfo &existedFileHashInfo);
      38              :         static uint64_t GetQuickHash(const void *data, const size_t size);
      39            3 :         void SetSoRootPath(const std::string &soRootPath)
      40              :         {
      41            3 :             soRootPath_ = soRootPath;
      42            3 :         }
      43              :     private:
      44              :         void UpdateCache();
      45              :         bool GetSameHashFileFromCache(const uint64_t hashValue, FileHashInfo &existedFileHashInfo) const;
      46              :         int32_t GenerateFileHashInfo(const std::string &filePath, FileHashInfo &hashInfo) const;
      47              :         void AddHashInfoToCache(const FileHashInfo &hashInfo);
      48              :         bool IsFileInCache(const std::string &filePath) const;
      49              :         int32_t GetFileNameInDir(std::vector<std::string> &file_names) const;
      50              :         bool IsFileExist(const std::string &filePath) const;
      51              :         bool IsRegularFile(const std::string &filePath) const;
      52              : 
      53              :         std::vector<FileHashInfo> cache_ = {};
      54              :         std::string soRootPath_ = "";
      55              :     };
      56              : 
      57              :     class CustSoFileLock {
      58              :     public:
      59           40 :         CustSoFileLock() = default;
      60           42 :         ~CustSoFileLock() 
      61              :         {
      62           42 :             if (locker_ > 0) {
      63            2 :                 (void)close(locker_);
      64              :             }
      65           42 :         }
      66              :         static CustSoFileLock &Instance();
      67              :         void InitFileLocker(const std::string &filePath);
      68              :         void LockFileLocker() const;
      69              :         void UnlockFileLocker() const;
      70              :         
      71              :     private:
      72              :         CustSoFileLock(const CustSoFileLock &) = delete;
      73              :         CustSoFileLock &operator=(const CustSoFileLock &) = delete;
      74              :         CustSoFileLock(CustSoFileLock &&) = delete;
      75              :         CustSoFileLock &&operator=(CustSoFileLock &&) = delete;
      76              : 
      77              :         int32_t locker_ = -1;
      78              :     };
      79              : 
      80              :     class AicpuCustSoManager {
      81              :     public:
      82              :         /**
      83              :          * @ingroup AicpuCustSoManager
      84              :          * @brief it is used to get instance of AicpuCustSoManager.
      85              :          * @return object of AicpuCustSoManager
      86              :          */
      87              :         static AicpuCustSoManager &GetInstance();
      88              : 
      89              :         /**
      90              :          * @ingroup AicpuCustSoManager
      91              :          * @brief it is used to init object of AicpuCustSoManager.
      92              :          * @param [in] mode aicpu run mode
      93              :          * @param [in] schedMode aicpu sched mode
      94              :          * @return AICPU_SCHEDULE_OK: success, other: error code
      95              :          */
      96              :         int32_t InitAicpuCustSoManager(const aicpu::AicpuRunMode mode,
      97              :                                        const AicpuSchedMode schedMode);
      98              : 
      99              :         /**
     100              :          * @ingroup AicpuCustSoManager
     101              :          * @brief it use to check and create cust so from Buf.
     102              :          * @param [in] args: LoadOpFromBufArgs
     103              :          * @param [in|out] soName: real so name
     104              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     105              :          */
     106              :         int32_t CheckAndCreateSoFile(const LoadOpFromBufArgs *const args, std::string &soName);
     107              : 
     108              :         /**
     109              :          * @ingroup AicpuCustSoManager
     110              :          * @brief it use to check and delete cust so.
     111              :          * @param [in] args: LoadOpFromBufArgs
     112              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     113              :          */
     114              :         int32_t CheckAndDeleteSoFile(const LoadOpFromBufArgs *const args);
     115              : 
     116              :         /**
     117              :          * @ingroup AicpuCustSoManager
     118              :          * @brief it use to check and delete cust so dir.
     119              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     120              :          */
     121              :         int32_t CheckAndDeleteCustSoDir();
     122              : 
     123              :         /**
     124              :          * @ingroup AicpuCustSoManager
     125              :          * @brief it use to delete cust so dir.
     126              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     127              :          */
     128              :         int32_t DeleteCustSoDir() const;
     129              : 
     130              :         /**
     131              :          * @ingroup AicpuCustSoManager
     132              :          * @brief it use to determine should support cust aicpu.
     133              :          * @return true: support, false: not supported
     134              :          */
     135              :         bool IsSupportCustAicpu() const;
     136              : 
     137              :         inline std::string GetSoRootPath() const
     138              :         {
     139              :             return custSoRootPath_;
     140              :         }
     141              :     private:
     142           40 :         AicpuCustSoManager() : runMode_(aicpu::AicpuRunMode::THREAD_MODE) {};
     143              : 
     144           40 :         ~AicpuCustSoManager() = default;
     145              : 
     146              :         // not allow copy constructor and assignment operators
     147              :         AicpuCustSoManager(const AicpuCustSoManager &) = delete;
     148              : 
     149              :         AicpuCustSoManager &operator=(const AicpuCustSoManager &) = delete;
     150              : 
     151              :         AicpuCustSoManager(AicpuCustSoManager &&) = delete;
     152              : 
     153              :         AicpuCustSoManager &&operator=(AicpuCustSoManager &&) = delete;
     154              : 
     155              :         /**
     156              :          * @ingroup AicpuCustSoManager
     157              :          * @brief it use to create cust so from Buf.
     158              :          * @param [in] fileInfo: the file info to generate file
     159              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     160              :          */
     161              :         int32_t CreateSoFile(const FileInfo &fileInfo);
     162              : 
     163              :         /**
     164              :          * @ingroup AicpuCustSoManager
     165              :          * @brief it use to create cust so from Buf.
     166              :          * @param [in] fileInfo: the file info to generate file
     167              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     168              :          */
     169              :         int32_t DoCreateSoFile(const FileInfo &fileInfo);
     170              : 
     171              :         /**
     172              :          * @ingroup AicpuCustSoManager
     173              :          * @brief Get root path for cust aicpu so
     174              :          * @param [in] dirName: dir path name
     175              :          * @return  AICPU_SCHEDULE_OK: success, other: error code
     176              :          */
     177              :         int32_t GetDirForCustAicpuSo(std::string &dirName) const;
     178              : 
     179              :         /**
     180              :          * @ingroup AicpuCustSoManager
     181              :          * @brief Get path for cust aicpu so
     182              :          * @return  string: so dir path in pcie mode
     183              :          */
     184              :         std::string GetCustAicpuUserPath(const uint32_t uniqueVfId) const;
     185              : 
     186              :         /**
     187              :          * @ingroup AicpuCustSoManager
     188              :          * @brief Get path for cust aicpu so
     189              :          * @return  string: so dir path
     190              :          */
     191              :         std::string GetPathForCustAicpuRealSo() const;
     192              : 
     193              :         /**
     194              :          * @ingroup AicpuCustSoManager
     195              :          * @brief Get path for cust aicpu so soft link
     196              :          * @return  string: soft link dir path
     197              :          */
     198              :         std::string GetPathForCustAicpuSoftLink() const;
     199              : 
     200              :         /**
     201              :          * @ingroup AicpuCustSoManager
     202              :          * @brief Check so file is valid.
     203              :          * @param [in] soFullPath: full so path
     204              :          * @return  AICPU_SCHEDULE_OK: success, other: error code
     205              :          */
     206              :         int32_t CheckSoFullPathValid(const std::string &soFullPath) const;
     207              : 
     208              :         /**
     209              :          * @ingroup AicpuCustSoManager
     210              :          * @brief it use to check and make directory.
     211              :          * @param [in] dirName: the name to directory
     212              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     213              :          */
     214              :         int32_t CheckOrMakeDirectory(const std::string &dirName) const;
     215              : 
     216              :         /**
     217              :          * @ingroup AicpuCustSoManager
     218              :          * @brief it use to check if dir is empty.
     219              :          * @param [in] dirName: dir name
     220              :          * @return bool: true, false
     221              :          */
     222              :         bool CheckDirEmpty(const std::string &dirName) const;
     223              : 
     224              :         /**
     225              :          * @ingroup AicpuCustSoManager
     226              :          * @brief it use to delete so.
     227              :          * @param [in] dirName: dir name
     228              :          * @param [in] soFullPath: so path
     229              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     230              :          */
     231              :         int32_t DeleteSoFile(const std::string &dirName, const std::string &soFullPath);
     232              : 
     233              :         /**
     234              :          * @ingroup AicpuCustSoManager
     235              :          * @brief it use to remove so.
     236              :          * @param [in] dirName: dir name
     237              :          * @param [in] soName: so name
     238              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     239              :          */
     240              :         int32_t RemoveSoFile(const std::string &dirName, const std::string &soFullPath) const;
     241              : 
     242              :         /**
     243              :          * @ingroup AicpuCustSoManager
     244              :          * @brief it use to remove so.
     245              :          * @param [in] fileInfo: The file info to write so
     246              :          * @param [in] dirPath: The full path to write so
     247              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     248              :          */
     249              :         int32_t WriteBufToSoFile(const FileInfo &fileInfo, const std::string &dirPath) const;
     250              : 
     251              :         /**
     252              :          * @ingroup AicpuCustSoManager
     253              :          * @brief it use to create soft link to existed so.
     254              :          * @param [in] fileInfo: The file info to write so
     255              :          * @return AICPU_SCHEDULE_OK: success, other: error code
     256              :          */
     257              :         int32_t CreateSoftLinkToSoFile(const std::string &softLinkPath, const std::string &existedSoPath);
     258              : 
     259              :         /**
     260              :          * @ingroup AicpuCustSoManager
     261              :          * @brief it use to check whether custSoDirName_ init.
     262              :          * @return bool: true, false
     263              :          */
     264              :         bool CheckCustSoPath() const;
     265              : 
     266              :         /**
     267              :          * @ingroup AicpuCustSoManager
     268              :          * @brief Generate the real so path
     269              :          * @return bool: true, false
     270              :          */
     271              :         std::string GenerateSoFullPath(const std::string &soName) const;
     272              : 
     273              :         // root path for cust so
     274              :         std::string custSoRootPath_;
     275              :         // dir name for cust so soft link
     276              :         std::string custSoDirName_;
     277              :         // mutex for so file and so dir
     278              :         std::mutex soFileMutex_;
     279              :         // aicpu run mode
     280              :         aicpu::AicpuRunMode runMode_;
     281              :         // aicpu sched mode
     282              :         AicpuSchedMode schedMode_;
     283              :         HashCalculator hashCalculator_;
     284              :         CustSoFileLock fileLocker_;
     285              :     };
     286              : }
     287              : 
     288              : #endif // CORE_AICPUSD_CUST_SO_MANAGER_H
        

Generated by: LCOV version 2.0-1