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_MONITOR_H
12 : #define CORE_AICPUSD_MONITOR_H
13 :
14 : #include <atomic>
15 : #include <mutex>
16 : #include <functional>
17 : #include <memory>
18 : #include <thread>
19 : #include <sstream>
20 : #include <string>
21 : #include <semaphore.h>
22 : #include "aicpusd_status.h"
23 : #include "aicpusd_common.h"
24 :
25 : namespace AicpuSchedule {
26 : class TaskTimer {
27 : public:
28 8 : TaskTimer() : startTick_(0ULL), runFlag_(false) {}
29 : TaskTimer(const uint64_t tick, const bool flag) : startTick_(tick), runFlag_(flag) {}
30 : ~TaskTimer() = default;
31 :
32 6 : inline uint64_t GetStartTick() const { return startTick_.load(); }
33 :
34 3 : inline void SetStartTick(const uint64_t tick) { startTick_ = tick; }
35 :
36 4 : inline bool GetRunFlag() const { return runFlag_.load(); }
37 :
38 6 : inline void SetRunFlag(const bool flag) { runFlag_ = flag; }
39 :
40 : private:
41 : std::atomic<uint64_t> startTick_;
42 : std::atomic<bool> runFlag_;
43 : };
44 :
45 : struct TaskInfoForMonitor {
46 : uint64_t serialNo;
47 : uint64_t taskId;
48 : uint32_t streamId;
49 : bool isHwts;
50 1 : std::string DebugString() const
51 : {
52 1 : std::ostringstream oss;
53 1 : oss << "serialNo=" << serialNo << ", stream_id=" << streamId << ", task_id=" << taskId;
54 2 : return oss.str();
55 1 : }
56 : };
57 :
58 : class AicpuMonitor {
59 : public:
60 : static AicpuMonitor& GetInstance();
61 :
62 : int32_t InitAicpuMonitor(const uint32_t deviceId, const bool online);
63 :
64 : void SetTaskInfo(const uint64_t taskIndex, const TaskInfoForMonitor& taskInfo);
65 : void SetTaskStartTime(const uint64_t taskIndex);
66 : void SetTaskEndTime(const uint64_t taskIndex);
67 :
68 6 : void SetCloseMonitorFlag(const bool closeFlag) { closeMonitorFlag_.store(closeFlag); }
69 :
70 14 : bool IsMonitorClosed() const { return closeMonitorFlag_.load(); }
71 :
72 : int32_t Run();
73 :
74 : void Stop();
75 :
76 : ~AicpuMonitor();
77 :
78 : private:
79 : AicpuMonitor();
80 :
81 : AicpuMonitor(const AicpuMonitor&) = delete;
82 :
83 : AicpuMonitor& operator=(const AicpuMonitor&) = delete;
84 :
85 : int32_t SetTaskTimeoutFlag();
86 :
87 : static void Work(AicpuMonitor* const monitor);
88 :
89 : void HandleTaskTimeout();
90 :
91 : void SendKillMsgToTsd() const;
92 :
93 : uint32_t deviceId_;
94 : bool taskTimeoutFlag_;
95 : std::unique_ptr<TaskInfoForMonitor[]> taskInfo_;
96 :
97 : // aicpu-cust-sd need too
98 : std::mutex mutex_;
99 : std::atomic<bool> done_;
100 :
101 : uint64_t taskTimeout_;
102 : uint64_t taskTimeoutTick_;
103 : std::unique_ptr<TaskTimer[]> taskTimer_;
104 : sem_t sem_;
105 : bool running_;
106 : uint32_t aicpuCoreNum_;
107 : bool online_; // true when exist in process mode; false when exist in thread mode
108 : std::atomic<bool> closeMonitorFlag_{false};
109 : };
110 : } // namespace AicpuSchedule
111 : #endif // CORE_AICPUSD_MONITOR_H
|