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

Generated by: LCOV version 2.0-1