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