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 QUEUE_SCHEDULE_STATISTIC_MANAGER_H
12 : #define QUEUE_SCHEDULE_STATISTIC_MANAGER_H
13 :
14 : #include <atomic>
15 : #include <thread>
16 : #include <mutex>
17 : #include <condition_variable>
18 :
19 : #include "common/type_def.h"
20 : #include "qs_proc_mem_statistic.h"
21 :
22 : namespace bqs {
23 : struct EntityStatisticInfo {
24 : // call HcclImprobe total times (envelope-probed + non-envelope-probed + failed times)
25 : uint64_t hcclImprobeTotalTimes;
26 : // call HcclImprobe succ times (envelope-probed times)
27 : uint64_t hcclImprobeSuccTimes;
28 : // call HcclImprobe failed times
29 : uint64_t hcclImprobeFailTimes;
30 : // alloc mbuf times
31 : uint64_t allocMbufTimes;
32 : // call HcclImrecv succ times
33 : uint64_t hcclImrecvSuccTimes;
34 : // call HcclImrecv failed times
35 : uint64_t hcclImrecvFailTimes;
36 : // uncompReqQueue push times
37 : uint64_t uncompReqQueuePushTimes;
38 : // uncompReqQueue pop times
39 : uint64_t uncompReqQueuePopTimes;
40 : // call HcclIsend succ times
41 : uint64_t hcclIsendSuccTimes;
42 : // call HcclIsend full times
43 : uint64_t hcclIsendFullTimes;
44 : // call HcclIsend fail times
45 : uint64_t hcclIsendFailTimes;
46 : // call HcclTestSome succ times
47 : uint64_t hcclTestSomeSuccTimes;
48 : // free mbuf times (hccl send succ)
49 : uint64_t freeMbufTimes;
50 : // hccl enqueue succ times
51 : uint64_t hcclEnqueueSuccTimes;
52 : // hccl dequeue fail times
53 : uint64_t hcclEnqueueFailTimes;
54 : // tag or queue dequeue succ times in peek state
55 : uint64_t dequeueSuccTimes;
56 : // tag or queue dequeue fail times in peek state
57 : uint64_t dequeueFailTimes;
58 : // tag or queue dequeue empty times in peek state
59 : uint64_t dequeueEmptyTimes;
60 : // statistic for head transfering on tag
61 : uint64_t maxCompletionGapTickForHead;
62 : uint64_t minCompletionGapTickForHead;
63 : uint64_t totalCompletionGapTickForHead;
64 : uint64_t totalCompletionCountForHead;
65 : // statistic for body transfering on tag
66 : uint64_t maxCompletionGapTickForBody;
67 : uint64_t minCompletionGapTickForBody;
68 : uint64_t totalCompletionGapTickForBody;
69 : uint64_t totalCompletionCountForBody;
70 : // tag or queue enqueue succ times in peek state
71 : uint64_t enqueueSuccTimes;
72 :
73 369 : EntityStatisticInfo()
74 369 : : hcclImprobeTotalTimes(0UL),
75 369 : hcclImprobeSuccTimes(0UL),
76 369 : hcclImprobeFailTimes(0UL),
77 369 : allocMbufTimes(0UL),
78 369 : hcclImrecvSuccTimes(0UL),
79 369 : hcclImrecvFailTimes(0UL),
80 369 : uncompReqQueuePushTimes(0UL),
81 369 : uncompReqQueuePopTimes(0UL),
82 369 : hcclIsendSuccTimes(0UL),
83 369 : hcclIsendFullTimes(0UL),
84 369 : hcclIsendFailTimes(0UL),
85 369 : hcclTestSomeSuccTimes(0UL),
86 369 : freeMbufTimes(0UL),
87 369 : hcclEnqueueSuccTimes(0UL),
88 369 : hcclEnqueueFailTimes(0UL),
89 369 : dequeueSuccTimes(0UL),
90 369 : dequeueFailTimes(0UL),
91 369 : dequeueEmptyTimes(0UL),
92 369 : maxCompletionGapTickForHead(0UL),
93 369 : minCompletionGapTickForHead(0UL),
94 369 : totalCompletionGapTickForHead(0UL),
95 369 : totalCompletionCountForHead(0UL),
96 369 : maxCompletionGapTickForBody(0UL),
97 369 : minCompletionGapTickForBody(0UL),
98 369 : totalCompletionGapTickForBody(0UL),
99 369 : totalCompletionCountForBody(0UL),
100 369 : enqueueSuccTimes(0UL)
101 369 : {}
102 : };
103 :
104 : struct StatisticInfo {
105 : // event schedule times
106 : std::atomic<uint64_t> eventScheduleTimes;
107 : // enqueue event false waken times
108 : std::atomic<uint64_t> enqueueFalseAwakenTimes;
109 : // event schedule times
110 : std::atomic<uint64_t> daemonEventScheduleTimes;
111 : // awaken times
112 : std::atomic<uint64_t> awakenTimes;
113 : // data dequeue times
114 : std::atomic<uint64_t> dataDequeueTimes;
115 : // sched empty times
116 : std::atomic<uint64_t> scheduleEmptyTimes;
117 : // data schedule failed times
118 : std::atomic<uint64_t> dataScheduleFailedTimes;
119 : // relation queue enqueue times
120 : std::atomic<uint64_t> relationEnqueueTimes;
121 : // relation queue dequeue times
122 : std::atomic<uint64_t> relationDequeueTimes;
123 : // async mem queue enqueue times
124 : std::atomic<uint64_t> asynMemEnqueueTimes;
125 : // async mem queue dequeue times
126 : std::atomic<uint64_t> asynMemDequeueTimes;
127 : // full ot not full queue enqueue times
128 : std::atomic<uint64_t> f2nfEnqueueTimes;
129 : // full ot not full queue dequeue times
130 : std::atomic<uint64_t> f2nfDequeueTimes;
131 : // bind relation request times
132 : std::atomic<uint64_t> bindTimes;
133 : // unbind relation request times
134 : std::atomic<uint64_t> unbindTimes;
135 : // get bind relation request times
136 : std::atomic<uint64_t> getBindTimes;
137 : // get all bind relation request times
138 : std::atomic<uint64_t> getAllBindTimes;
139 : // relation response times
140 : std::atomic<uint64_t> responseTimes;
141 : // enqueue success times
142 : std::atomic<uint64_t> dataEnqueueSuccTimes;
143 : // enqueue fail times
144 : std::atomic<uint64_t> dataEnqueueFailTimes;
145 : // enqueue full times
146 : std::atomic<uint64_t> dataEnqueueFullTimes;
147 : // hccl mpi recv request event times
148 : std::atomic<uint64_t> hcclMpiRecvRequestEventTimes;
149 : // hccl mpi recv request event false awaken times
150 : std::atomic<uint64_t> hcclMpiRecvReqFalseAwakenTimes;
151 : // hccl mpi recv request event empty sched times
152 : std::atomic<uint64_t> hcclMpiRecvReqEmptySchedTimes;
153 : // hccl mpi recv request event callback times
154 : std::atomic<uint64_t> hcclMpiRecvReqCallbackTimes;
155 : // hccl mpi send completion event times
156 : std::atomic<uint64_t> hcclMpiSendCompEventTimes;
157 : // hccl mpi send completion event false awaken times
158 : std::atomic<uint64_t> hcclMpiSendCompFalseAwakenTimes;
159 : // hccl mpi send completion event empty sched times
160 : std::atomic<uint64_t> hcclMpiSendCompEmptySchedTimes;
161 : // hccl mpi recv request event callback times
162 : std::atomic<uint64_t> hcclMpiSendCompCallbackTimes;
163 : // hccl mpi recv completion event times
164 : std::atomic<uint64_t> hcclMpiRecvCompEventTimes;
165 : // hccl mpi recv completion event false awaken times
166 : std::atomic<uint64_t> hcclMpiRecvCompFalseAwakenTimes;
167 : // hccl mpi recv completion event empty sched times
168 : std::atomic<uint64_t> hcclMpiRecvCompEmptySchedTimes;
169 : // hccl mpi recv completion event callback times
170 : std::atomic<uint64_t> hcclMpiRecvCompCallbackTimes;
171 : // f2nf event times
172 : std::atomic<uint64_t> f2nfEventTimes;
173 : // f2nf event false awaken times
174 : std::atomic<uint64_t> f2nfFalseAwakenTimes;
175 : // hccl mpi call recv success times
176 : std::atomic<uint64_t> hcclMpiRecvSuccTimes;
177 : // hccl mpi call recv failed taimes
178 : std::atomic<uint64_t> hcclMpiRecvFailTimes;
179 : // hccl mpi call send success times
180 : std::atomic<uint64_t> hcclMpiSendSuccTimes;
181 : // hccl mpi call send failed taimes
182 : std::atomic<uint64_t> hcclMpiSendFailTimes;
183 : // hccl mpi call send full taimes
184 : std::atomic<uint64_t> hcclMpiSendFullTimes;
185 : // hccl mpi congestion relief event times
186 : std::atomic<uint64_t> hcclMpiF2nfEventTimes;
187 : // mbuf alloc size
188 : std::atomic<uint64_t> mbufAllocSize;
189 : // mbuf alloc times
190 : std::atomic<uint64_t> mbufAllocTimes;
191 : // mbuf free size
192 : std::atomic<uint64_t> mbufFreeSize;
193 : // mbuf free times
194 : std::atomic<uint64_t> mbufFreeTimes;
195 : // supply recv request event times
196 : std::atomic<uint64_t> supplyRecvReqEventTimes;
197 :
198 6 : StatisticInfo() { Reset(); }
199 :
200 18 : void Reset()
201 : {
202 18 : eventScheduleTimes.store(0UL);
203 18 : enqueueFalseAwakenTimes.store(0UL);
204 18 : daemonEventScheduleTimes.store(0UL);
205 18 : awakenTimes.store(0UL);
206 18 : dataDequeueTimes.store(0UL);
207 18 : scheduleEmptyTimes.store(0UL);
208 18 : dataScheduleFailedTimes.store(0UL);
209 18 : relationEnqueueTimes.store(0UL);
210 18 : relationDequeueTimes.store(0UL);
211 18 : asynMemEnqueueTimes.store(0UL);
212 18 : asynMemDequeueTimes.store(0UL);
213 18 : f2nfEnqueueTimes.store(0UL);
214 18 : f2nfDequeueTimes.store(0UL);
215 18 : bindTimes.store(0UL);
216 18 : unbindTimes.store(0UL);
217 18 : getBindTimes.store(0UL);
218 18 : getAllBindTimes.store(0UL);
219 18 : responseTimes.store(0UL);
220 18 : dataEnqueueSuccTimes.store(0UL);
221 18 : dataEnqueueFailTimes.store(0UL);
222 18 : dataEnqueueFullTimes.store(0UL);
223 18 : hcclMpiRecvRequestEventTimes.store(0UL);
224 18 : hcclMpiRecvReqFalseAwakenTimes.store(0UL);
225 18 : hcclMpiRecvReqEmptySchedTimes.store(0UL);
226 18 : hcclMpiRecvReqCallbackTimes.store(0UL);
227 18 : hcclMpiSendCompEventTimes.store(0UL);
228 18 : hcclMpiSendCompFalseAwakenTimes.store(0UL);
229 18 : hcclMpiSendCompEmptySchedTimes.store(0UL);
230 18 : hcclMpiSendCompCallbackTimes.store(0UL);
231 18 : hcclMpiRecvCompEventTimes.store(0UL);
232 18 : hcclMpiRecvCompFalseAwakenTimes.store(0UL);
233 18 : hcclMpiRecvCompEmptySchedTimes.store(0UL);
234 18 : hcclMpiRecvCompCallbackTimes.store(0UL);
235 18 : f2nfEventTimes.store(0UL);
236 18 : f2nfFalseAwakenTimes.store(0UL);
237 18 : hcclMpiRecvSuccTimes.store(0UL);
238 18 : hcclMpiRecvFailTimes.store(0UL);
239 18 : hcclMpiSendSuccTimes.store(0UL);
240 18 : hcclMpiSendFailTimes.store(0UL);
241 18 : hcclMpiSendFullTimes.store(0UL);
242 18 : hcclMpiF2nfEventTimes.store(0UL);
243 18 : mbufAllocSize.store(0UL);
244 18 : mbufAllocTimes.store(0UL);
245 18 : mbufFreeSize.store(0UL);
246 18 : mbufFreeTimes.store(0UL);
247 18 : supplyRecvReqEventTimes.store(0UL);
248 18 : };
249 : };
250 :
251 : class ScheduleStatistic {
252 : public:
253 3 : ScheduleStatistic() { Reset(); };
254 :
255 : ~ScheduleStatistic() = default;
256 :
257 : void Reset();
258 :
259 : void UpdateProcessCost(const float64_t cost);
260 :
261 : void UpdateScheduleDelay(const float64_t delay);
262 :
263 : float64_t GetMaxProcessCost();
264 :
265 : float64_t GetSecondProcessCost();
266 :
267 : float64_t GetMaxScheduleDelay();
268 :
269 : float64_t GetAvgProcessCost();
270 :
271 : private:
272 : float64_t maxProcessCostUs_;
273 : float64_t secondMaxProcessUs_;
274 : float64_t maxScheduleDelayUs_;
275 : std::mutex mutex_;
276 : float64_t totalProcessCostUs_;
277 : uint64_t totalProcessCount_;
278 : };
279 :
280 : class StatisticManager {
281 : public:
282 : static StatisticManager& GetInstance();
283 :
284 : ~StatisticManager();
285 :
286 : StatisticManager(const StatisticManager&) = delete;
287 :
288 : StatisticManager& operator=(const StatisticManager&) = delete;
289 :
290 : StatisticManager(StatisticManager&&) = delete;
291 :
292 : StatisticManager& operator=(StatisticManager&&) = delete;
293 :
294 : public:
295 : /**
296 : * event schedule statistic.
297 : * @param scheduleNum schedule num, default is 1.
298 : */
299 : uint64_t EventScheduleStat(const uint32_t scheduleNum = 1U);
300 :
301 : /**
302 : * enqueue event false awaken times
303 : */
304 : void EnqueueEventFalseAwakenStat();
305 :
306 : /**
307 : * get event schedule statistic.
308 : */
309 : uint64_t GetEventScheduleStat() const;
310 :
311 : /**
312 : * daemon event schedule statistic.
313 : */
314 : void DaemonEventScheduleStat();
315 :
316 : /**
317 : * awaken count add.
318 : */
319 : void AwakenAdd();
320 :
321 : /**
322 : * return awaken times.
323 : */
324 : uint64_t GetAwakenTimes() const;
325 :
326 : /**
327 : * data sched empty statistic.
328 : */
329 : void AddScheduleEmpty();
330 :
331 : /**
332 : * data schedule failed statistic.
333 : * @param failedNum: data schedule failed, default is 1
334 : */
335 : void DataScheduleFailedStat(const uint64_t failedNum = 1UL);
336 :
337 : /**
338 : * relation queue enqueue statistic.
339 : */
340 : void RelationEnqueueStat();
341 :
342 : /**
343 : * relation queue dequeue statistic.
344 : */
345 : void RelationDequeueStat();
346 :
347 : /**
348 : * get relation queue enqueue statistic.
349 : */
350 : uint64_t GetRelationEnqueCnt() const;
351 :
352 : /**
353 : * get relation queue dequeue statistic.
354 : */
355 : uint64_t GetRelationDequeCnt() const;
356 :
357 : /**
358 : * asyn mem queue enqueue statistic.
359 : */
360 : void AsynMemEnqueueStat();
361 :
362 : /**
363 : * asyn mem queue dequeue statistic.
364 : */
365 : void AsynMemDequeueStat();
366 :
367 : /**
368 : * get asyn mem queue enqueue statistic.
369 : */
370 : uint64_t GetAsynMemEnqueCnt() const;
371 :
372 : /**
373 : * get asyn mem queue dequeue statistic.
374 : */
375 : uint64_t GetAsynMemDequeCnt() const;
376 :
377 : /**
378 : * full to not full queue enqueue statistic.
379 : */
380 : void F2nfEnqueueStat();
381 :
382 : /**
383 : * full to not full queue dequeue statistic.
384 : */
385 : void F2nfDequeueStat();
386 :
387 : /**
388 : * hccl mpi recv request event times statistic.
389 : */
390 : uint64_t HcclMpiRecvRequestEventStat();
391 :
392 : /**
393 : * hccl mpi recv request event false awaken times statistic.
394 : */
395 : void HcclMpiRecvReqFalseAwakenStat();
396 :
397 : /**
398 : * hccl mpi recv request event empty sched times statistic.
399 : */
400 : void HcclMpiRecvReqEmptySchedStat();
401 :
402 : /**
403 : * hccl mpi recv request event callback times statistic.
404 : */
405 : void HcclMpiRecvReqCallbackStat();
406 :
407 : /**
408 : * hccl mpi send completion event times statistic.
409 : */
410 : uint64_t HcclMpiSendCompEventStat();
411 :
412 : /**
413 : * hccl mpi send completion event false awaken times statistic.
414 : */
415 : void HcclMpiSendCompFalseAwakenStat();
416 :
417 : /**
418 : * hccl mpi send completion event empty sched times statistic.
419 : */
420 : void HcclMpiSendCompEmptySchedStat();
421 :
422 : /**
423 : * hccl mpi send completion event callback times statistic.
424 : */
425 : void HcclMpiSendCompCallbackStat();
426 :
427 : /**
428 : * hccl mpi recv completion event times statistic.
429 : */
430 : uint64_t HcclMpiRecvCompEventStat();
431 :
432 : /**
433 : * hccl mpi recv completion event false awaken times statistic.
434 : */
435 : void HcclMpiRecvCompFalseAwakenStat();
436 :
437 : /**
438 : * hccl mpi recv completion event empty sched times statistic.
439 : */
440 : void HcclMpiRecvCompEmptySchedStat();
441 :
442 : /**
443 : * hccl mpi recv completion event callback times statistic.
444 : */
445 : void HcclMpiRecvCompCallbackStat();
446 :
447 : /**
448 : * hccl mpi call hcclImrecv success times statistic.
449 : */
450 : void HcclMpiRecvSuccStat();
451 :
452 : /**
453 : * hccl mpi call hcclImrecv failed times statistic.
454 : */
455 : void HcclMpiRecvFailStat();
456 :
457 : /**
458 : * hccl mpi call hcclIsend success times statistic.
459 : */
460 : void HcclMpiSendSuccStat();
461 :
462 : /**
463 : * hccl mpi call hcclIsend failed times statistic.
464 : */
465 : void HcclMpiSendFailStat();
466 :
467 : /**
468 : * hccl mpi call hcclIsend full times statistic.
469 : */
470 : void HcclMpiSendFullStat();
471 :
472 : /**
473 : * hccl mpi congestion relief event times statistic
474 : */
475 : void HcclMpiF2nfEventStat();
476 :
477 : /**
478 : * mbuf alloc statistic
479 : */
480 : void MbufAllocStat(const uint64_t size);
481 :
482 : /**
483 : * mbuf free statistic
484 : */
485 : void MbufFreeStat(const uint64_t size);
486 :
487 : /**
488 : * recv request event supply statistic
489 : */
490 : void RecvReqEventSupplyStat();
491 :
492 : /**
493 : * f2nf event times statistic.
494 : */
495 : void F2nfEventStat();
496 :
497 : /**
498 : * f2nf event false awaken times statistic.
499 : */
500 : void F2nfEventFalseAwakenStat();
501 :
502 : /**
503 : * total dequeue times statistic.
504 : */
505 : void DataDequeueStat();
506 :
507 : void DataQueueEnqueueSuccStat();
508 :
509 : void DataQueueEnqueueFailStat();
510 :
511 : void DataQueueEnqueueFullStat();
512 :
513 : /**
514 : * bind relation request statistic
515 : */
516 : void BindStat();
517 :
518 : /**
519 : * unbind relation request statistic
520 : */
521 : void UnbindStat();
522 :
523 : /**
524 : * get bind relation request statistic
525 : */
526 : void GetBindStat();
527 :
528 : /**
529 : * get all bind relation request statistic
530 : */
531 : void GetAllBindStat();
532 :
533 : /**
534 : * relation response statistic
535 : */
536 : void ResponseStat();
537 :
538 : /**
539 : * subscribe queue statistic
540 : */
541 : void SubscribeNum(const uint32_t subscribeNum);
542 :
543 : /**
544 : * pause subscribe queue statistic, pause then +1
545 : */
546 : void PauseSubscribe();
547 :
548 : /**
549 : * pause subscribe queue statistic, resume then -1
550 : */
551 : void ResumeSubscribe();
552 :
553 : /**
554 : * bind relation number statistic
555 : */
556 : void BindNum(const uint32_t bindNum);
557 :
558 : /**
559 : * abnormal bind relation number statistic
560 : */
561 : void AbnormalBindNum(const uint32_t bindNum);
562 :
563 : /**
564 : * start dump static thread.
565 : */
566 : void StartStatisticManager(
567 : const uint32_t abnormalInterval, const uint32_t hostPid, const bool numaFlag = false,
568 : const uint32_t deviceIdExtra = 0U, const uint32_t enqueGroupIdExtra = 0U);
569 :
570 : /**
571 : * stop dump static thread.
572 : */
573 : void StopStatisticManager();
574 :
575 : /**
576 : * dump static thread function.
577 : */
578 : void ThreadFunc();
579 :
580 : /**
581 : * set exist entity flag
582 : */
583 : void SetExistEntityFlag(const bool flag);
584 :
585 : /**
586 : * Dump statistic info
587 : */
588 : void DumpStatistic();
589 :
590 : /**
591 : * Reset statistic info
592 : */
593 : void ResetStatistic();
594 :
595 : /**
596 : * Add unlink tag count
597 : * @return unlink tag count
598 : */
599 : const uint32_t AddUnlinkCount();
600 :
601 : /**
602 : * Reduce unlink tag count
603 : * @return unlink tag count
604 : */
605 : const uint32_t ReduceUnlinkCount();
606 :
607 : void RefreshEnqueHeartBeat();
608 :
609 : void AddTagCount();
610 :
611 : void ReduceTagCount();
612 :
613 : void UpdateScheuleStatistic(const float64_t delay, const float64_t cost);
614 :
615 : void DumpOutProcMemStatInfo();
616 :
617 : void RecordProcMemInfo();
618 :
619 : private:
620 3 : StatisticManager() = default;
621 :
622 : /**
623 : * dump channel statistic info
624 : */
625 : void DumpChannelStatistic();
626 :
627 : private:
628 : // lock for timerThread_
629 : std::mutex timerMutex_;
630 : // thread for record static
631 : std::thread timerThread_;
632 :
633 : // thread run flag
634 : volatile bool runFlag_ = false;
635 :
636 : // total statistic
637 : StatisticInfo totalStat_;
638 : // period statistic
639 : StatisticInfo periodStat_;
640 : // bind relation number
641 : std::atomic<uint32_t> bindNum_ = {0U};
642 : // need subscribe number
643 : std::atomic<uint32_t> subscribeNum_ = {0U};
644 : // need pause subscribe number
645 : std::atomic<uint32_t> pauseSubscribeNum_ = {0U};
646 :
647 : // exist entity flag
648 : std::atomic<bool> existEntityFlag_ = {false};
649 : // unlink tag number
650 : std::atomic<uint32_t> unLinkTagNum_ = {0U};
651 : // abnormal bind relation number
652 : std::atomic<uint32_t> abnormalBindNum_ = {0U};
653 : std::atomic<uint32_t> enqueThreadHearBeat_ = {0U};
654 : uint32_t abnormalInterval_;
655 : std::atomic<uint32_t> totalTagNum_ = {0U};
656 : ScheduleStatistic scheduleStatistic_{};
657 :
658 : bool numaFlag_{false};
659 : uint32_t deviceIdExtra_{0U};
660 : uint32_t enqueGroupIdExtra_{0U};
661 :
662 : QsProcMemStatistic procMemStat_;
663 : uint32_t hostPid_{0U};
664 : };
665 : } // namespace bqs
666 : #endif // QUEUE_SCHEDULE_STATISTIC_MANAGER_H
|