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 QS_INTERFACE_PROCESS_H
12 : #define QS_INTERFACE_PROCESS_H
13 :
14 : #include <atomic>
15 : #include <functional>
16 : #include <mutex>
17 : #include <thread>
18 : #include <vector>
19 : #include <memory>
20 : #include <string>
21 : #include "driver/ascend_hal.h"
22 : #include "queue_schedule.h"
23 : #include "common/bqs_log.h"
24 : #include "bind_cpu_utils.h"
25 : #include "common/bqs_feature_ctrl.h"
26 :
27 : namespace bqs {
28 : class QueueScheduleInterface {
29 : public:
30 : __attribute__((visibility("default"))) static QueueScheduleInterface& GetInstance();
31 :
32 : /**
33 : * @ingroup QueueScheduleInterface
34 : * @brief it use to destroy all model.
35 : * @return AICPU_SCHEDULE_OK:success, other failed.
36 : */
37 : __attribute__((visibility("default"))) int32_t Destroy() const;
38 :
39 : /**
40 : * @ingroup QueueScheduleInterface
41 : * @brief it use to initialize aicpu schedule.
42 : * @param [in] params InitQsParams
43 : * @return AICPU_SCHEDULE_OK: success, other: error code
44 : */
45 : __attribute__((visibility("default"))) int32_t InitQueueScheduler(const InitQsParams& params);
46 :
47 : /**
48 : * @ingroup QueueScheduleInterface
49 : * @brief it use to get cpu info.
50 : * @return AICPU_SCHEDULE_OK: success, other: error code
51 : */
52 : const CpuInfo& GetCpuInfo() const { return cpuInfo_; }
53 :
54 : /**
55 : * @ingroup QueueScheduleInterface
56 : * @brief it use to get control cpu ids.
57 : * @return AICPU_SCHEDULE_OK: success, other: error code
58 : */
59 1 : const std::vector<uint32_t>& GetCtrlCpuIds() const { return ctrlCpuIds_; }
60 :
61 : const std::vector<uint32_t>& GetAiCpuIds() const { return aiCpuIds_; }
62 :
63 : uint32_t GetAicpuPhysIndex(uint32_t deviceId, const uint32_t aicpuLogIndex) const;
64 :
65 : uint32_t GetExtraAicpuPhysIndex(uint32_t deviceId, const uint32_t aicpuLogIndex) const;
66 :
67 2 : uint32_t GetAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
68 : {
69 2 : if (aicpuLogIndex >= aiCpuIds_.size()) {
70 1 : BQS_LOG_ERROR("Get aicpu index error");
71 1 : return 0U;
72 : }
73 1 : if (FeatureCtrl::IsVfModeDie1(deviceId)) {
74 0 : return coreNumPerDev_ + aiCpuIds_[aicpuLogIndex];
75 : }
76 1 : return aiCpuIds_[aicpuLogIndex];
77 : }
78 :
79 2 : uint32_t GetExtraAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
80 : {
81 2 : if (aicpuLogIndex >= aiCpuIdsExtra_.size()) {
82 1 : BQS_LOG_ERROR("Get aicpu index error");
83 1 : return 0U;
84 : }
85 1 : if (FeatureCtrl::IsVfModeDie1(deviceId)) {
86 0 : return coreNumPerDevExtra_ + aiCpuIdsExtra_[aicpuLogIndex];
87 : }
88 1 : return aiCpuIdsExtra_[aicpuLogIndex];
89 : }
90 2 : uint32_t GetAiCpuNum() const { return aicpuNum_; }
91 :
92 1 : uint32_t GetExtraAiCpuNum() const { return aicpuNumExtra_; }
93 : int32_t CheckBindHostPid(const uint32_t selfHostPid) const;
94 : /**
95 : * wait for stop.
96 : */
97 : __attribute__((visibility("default"))) void WaitForStop();
98 :
99 : void ReportAbnormal() const;
100 :
101 : private:
102 1 : QueueScheduleInterface() = default;
103 :
104 1 : ~QueueScheduleInterface() = default;
105 :
106 : QueueScheduleInterface(const QueueScheduleInterface&) = delete;
107 :
108 : QueueScheduleInterface& operator=(const QueueScheduleInterface&) = delete;
109 :
110 : // it is used in updating model status.
111 : std::mutex mutexForInit_;
112 : std::unique_ptr<bqs::QueueSchedule> queueSchedule_;
113 : CpuInfo cpuInfo_;
114 : std::vector<uint32_t> ctrlCpuIds_;
115 : std::vector<uint32_t> aiCpuIds_;
116 : uint32_t coreNumPerDev_;
117 : uint32_t aicpuNum_;
118 : uint32_t aicpuBaseId_;
119 :
120 : std::vector<uint32_t> ctrlCpuIdsExtra_;
121 : std::vector<uint32_t> aiCpuIdsExtra_;
122 : uint32_t coreNumPerDevExtra_;
123 : uint32_t aicpuNumExtra_;
124 : uint32_t aicpuBaseIdExtra_;
125 : };
126 : } // namespace bqs
127 : #endif // QS_INTERFACE_PROCESS_H
|