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 ¶ms);
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
53 : {
54 : return cpuInfo_;
55 : }
56 :
57 : /**
58 : * @ingroup QueueScheduleInterface
59 : * @brief it use to get control cpu ids.
60 : * @return AICPU_SCHEDULE_OK: success, other: error code
61 : */
62 1 : const std::vector<uint32_t> &GetCtrlCpuIds() const
63 : {
64 1 : return ctrlCpuIds_;
65 : }
66 :
67 : const std::vector<uint32_t> &GetAiCpuIds() const
68 : {
69 : return aiCpuIds_;
70 : }
71 :
72 : uint32_t GetAicpuPhysIndex(uint32_t deviceId, const uint32_t aicpuLogIndex) const;
73 :
74 : uint32_t GetExtraAicpuPhysIndex(uint32_t deviceId, const uint32_t aicpuLogIndex) const;
75 :
76 2 : uint32_t GetAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
77 : {
78 2 : if (aicpuLogIndex >= aiCpuIds_.size()) {
79 1 : BQS_LOG_ERROR("Get aicpu index error");
80 1 : return 0U;
81 : }
82 1 : if (FeatureCtrl::IsVfModeDie1(deviceId)) {
83 0 : return coreNumPerDev_ + aiCpuIds_[aicpuLogIndex];
84 : }
85 1 : return aiCpuIds_[aicpuLogIndex];
86 : }
87 :
88 2 : uint32_t GetExtraAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
89 : {
90 2 : if (aicpuLogIndex >= aiCpuIdsExtra_.size()) {
91 1 : BQS_LOG_ERROR("Get aicpu index error");
92 1 : return 0U;
93 : }
94 1 : if (FeatureCtrl::IsVfModeDie1(deviceId)) {
95 0 : return coreNumPerDevExtra_ + aiCpuIdsExtra_[aicpuLogIndex];
96 : }
97 1 : return aiCpuIdsExtra_[aicpuLogIndex];
98 : }
99 2 : uint32_t GetAiCpuNum() const
100 : {
101 2 : return aicpuNum_;
102 : }
103 :
104 1 : uint32_t GetExtraAiCpuNum() const
105 : {
106 1 : return aicpuNumExtra_;
107 : }
108 : int32_t CheckBindHostPid(const uint32_t selfHostPid) const;
109 : /**
110 : * wait for stop.
111 : */
112 : __attribute__((visibility("default"))) void WaitForStop();
113 :
114 : void ReportAbnormal() const;
115 :
116 : private:
117 1 : QueueScheduleInterface() = default;
118 :
119 1 : ~QueueScheduleInterface() = default;
120 :
121 : QueueScheduleInterface(const QueueScheduleInterface &) = delete;
122 :
123 : QueueScheduleInterface &operator=(const QueueScheduleInterface &) = delete;
124 :
125 : // it is used in updating model status.
126 : std::mutex mutexForInit_;
127 : std::unique_ptr<bqs::QueueSchedule> queueSchedule_;
128 : CpuInfo cpuInfo_;
129 : std::vector<uint32_t> ctrlCpuIds_;
130 : std::vector<uint32_t> aiCpuIds_;
131 : uint32_t coreNumPerDev_;
132 : uint32_t aicpuNum_;
133 : uint32_t aicpuBaseId_;
134 :
135 : std::vector<uint32_t> ctrlCpuIdsExtra_;
136 : std::vector<uint32_t> aiCpuIdsExtra_;
137 : uint32_t coreNumPerDevExtra_;
138 : uint32_t aicpuNumExtra_;
139 : uint32_t aicpuBaseIdExtra_;
140 : };
141 : }
142 : #endif // QS_INTERFACE_PROCESS_H
|