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_PROFILER_H
12 : #define CORE_AICPUSD_PROFILER_H
13 :
14 : #include <thread>
15 : #include "aicpusd_status.h"
16 : #include "aicpusd_common.h"
17 : #include "hiperf_common.h"
18 : #include "aicpusd_feature_ctrl.h"
19 :
20 : namespace AicpuSchedule {
21 : class AicpuProfiler {
22 : public:
23 : AicpuProfiler() noexcept;
24 :
25 : ~AicpuProfiler() = default;
26 :
27 : void Uninit();
28 :
29 : void Profiler();
30 :
31 : __attribute__((visibility("default"))) static void ProfilerAgentInit();
32 :
33 : void InitProfiler(const pid_t processId, const pid_t threadId);
34 :
35 7 : Hiva::KernelTrack GetKernelTrack() const
36 : {
37 7 : return kernelTrack_;
38 : }
39 :
40 137 : void SetEventId(const uint32_t kernelTrackEventId)
41 : {
42 137 : if (!hiperfExisted_) {
43 137 : return;
44 : }
45 0 : kernelTrack_.eventId = kernelTrackEventId;
46 : }
47 :
48 86 : void SetMbufHead(const void *const mbuf)
49 : {
50 86 : if (!hiperfExisted_) {
51 86 : return;
52 : }
53 0 : if (mbuf != nullptr) {
54 : // 40/44 is the offset of timestamp, 36 is the offset of sequenece id (ROS)
55 0 : uint32_t *const seqId = reinterpret_cast<uint32_t *>(reinterpret_cast<uintptr_t>(mbuf) + 36UL);
56 0 : uint32_t *const sec = reinterpret_cast<uint32_t *>(reinterpret_cast<uintptr_t>(mbuf) + 40UL);
57 0 : uint32_t *const nsec = reinterpret_cast<uint32_t *>(reinterpret_cast<uintptr_t>(mbuf) + 44UL);
58 : // Take the upper eight bits of data
59 0 : kernelTrack_.sensorId = (*seqId) >> 24U;
60 0 : if (FeatureCtrl::IsAosCore()) {
61 0 : kernelTrack_.rawStamp.tv_sec = static_cast<time_t>(*sec);
62 : } else {
63 0 : kernelTrack_.rawStamp.tv_sec = static_cast<__time_t>(*sec);
64 : }
65 0 : kernelTrack_.rawStamp.tv_nsec = static_cast<int64_t>(*nsec);
66 0 : kernelTrack_.uniqueId = (*seqId & 0X000FFFFFU);
67 : }
68 : }
69 :
70 12 : void SetModelId(const uint32_t modelId)
71 : {
72 12 : if (!hiperfExisted_) {
73 12 : return;
74 : }
75 0 : kernelTrack_.modelId = modelId;
76 : }
77 :
78 12 : void SetStreamId(const uint32_t streamId)
79 : {
80 12 : if (!hiperfExisted_) {
81 12 : return;
82 : }
83 0 : kernelTrack_.streamId = streamId;
84 : }
85 :
86 7 : void SetTsStreamId(const uint32_t tsStreamId)
87 : {
88 7 : if (!hiperfExisted_) {
89 7 : return;
90 : }
91 0 : kernelTrack_.tsStreamId = tsStreamId;
92 : }
93 :
94 89 : void SetQueueId(const uint32_t queueId)
95 : {
96 89 : if (!hiperfExisted_) {
97 89 : return;
98 : }
99 0 : kernelTrack_.queueId = queueId;
100 : }
101 :
102 : uint64_t SchedGetCurCpuTick(void) const;
103 :
104 : uint64_t GetAicpuSysFreq() const;
105 :
106 24 : void SetDqStart()
107 : {
108 24 : if (!hiperfExisted_) {
109 24 : return;
110 : }
111 0 : kernelTrack_.dqStart = SchedGetCurCpuTick();
112 : }
113 :
114 16 : void SetDqEnd()
115 : {
116 16 : if (!hiperfExisted_) {
117 16 : return;
118 : }
119 0 : kernelTrack_.dqEnd = SchedGetCurCpuTick();
120 : }
121 :
122 18 : void SetModelStart()
123 : {
124 18 : if (!hiperfExisted_) {
125 18 : return;
126 : }
127 0 : kernelTrack_.modelStart = SchedGetCurCpuTick();
128 : }
129 :
130 20 : void SetPrepareOutStart()
131 : {
132 20 : if (!hiperfExisted_) {
133 20 : return;
134 : }
135 0 : kernelTrack_.prepareOutStart = SchedGetCurCpuTick();
136 : }
137 :
138 11 : void SetPrepareOutEnd()
139 : {
140 11 : if (!hiperfExisted_) {
141 11 : return;
142 : }
143 0 : kernelTrack_.prepareOutEnd = SchedGetCurCpuTick();
144 : }
145 :
146 7 : void SetActiveStream()
147 : {
148 7 : if (!hiperfExisted_) {
149 7 : return;
150 : }
151 0 : kernelTrack_.activeStream = SchedGetCurCpuTick();
152 : }
153 :
154 11 : void SetEndGraph()
155 : {
156 11 : if (!hiperfExisted_) {
157 11 : return;
158 : }
159 0 : kernelTrack_.endGraph = SchedGetCurCpuTick();
160 : }
161 :
162 11 : void SetEqStart()
163 : {
164 11 : if (!hiperfExisted_) {
165 11 : return;
166 : }
167 0 : kernelTrack_.eqStart = SchedGetCurCpuTick();
168 : }
169 :
170 9 : void SetEqEnd()
171 : {
172 9 : if (!hiperfExisted_) {
173 9 : return;
174 : }
175 0 : kernelTrack_.eqEnd = SchedGetCurCpuTick();
176 : }
177 :
178 6 : void SetModelEnd()
179 : {
180 6 : if (!hiperfExisted_) {
181 6 : return;
182 : }
183 0 : kernelTrack_.modelEnd = SchedGetCurCpuTick();
184 : }
185 :
186 4 : void SetRepeatStart()
187 : {
188 4 : if (!hiperfExisted_) {
189 4 : return;
190 : }
191 0 : kernelTrack_.repeatStart = SchedGetCurCpuTick();
192 : }
193 :
194 4 : void SetRepeatEnd()
195 : {
196 4 : if (!hiperfExisted_) {
197 4 : return;
198 : }
199 0 : kernelTrack_.repeatEnd = SchedGetCurCpuTick();
200 : }
201 :
202 137 : void SetProcEventStart()
203 : {
204 137 : if (!hiperfExisted_) {
205 137 : return;
206 : }
207 0 : kernelTrack_.procEventStart = SchedGetCurCpuTick();
208 : }
209 :
210 136 : void SetProcEventEnd()
211 : {
212 136 : if (!hiperfExisted_) {
213 136 : return;
214 : }
215 0 : kernelTrack_.procEventEnd = SchedGetCurCpuTick();
216 : }
217 :
218 136 : inline bool GetHiperfSoStatus() const
219 : {
220 136 : return ((!accessHiperfSo_) || (hiperfExisted_));
221 : }
222 :
223 : private:
224 :
225 : AicpuProfiler(const AicpuProfiler &) = delete;
226 :
227 : AicpuProfiler &operator=(const AicpuProfiler &) = delete;
228 :
229 : uint64_t BuildKey();
230 :
231 : pid_t pid_;
232 : pid_t tid_;
233 : Hiva::KernelTrack kernelTrack_;
234 : float64_t frequence_;
235 : bool hiperfExisted_;
236 : bool accessHiperfSo_;
237 : uint64_t oneMsForTick_;
238 : uint64_t tenMsForTick_;
239 : };
240 :
241 : extern thread_local AicpuProfiler g_aicpuProfiler;
242 : }
243 :
244 : #endif // CORE_AICPUSD_PROFILER_H
|