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