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 DFX_CIRCULAR_QUEUE_H
12 : #define DFX_CIRCULAR_QUEUE_H
13 : #include <cstring>
14 : #include "securec.h"
15 : #include "hccl/base.h"
16 : #include "log.h"
17 : #include "res_pub.h"
18 :
19 : namespace Hccl {
20 :
21 : template <typename T, u32 CAPACITY>
22 : class DfxCircularQueue {
23 : static constexpr u32 ITEM_SIZE = sizeof(T);
24 :
25 : public:
26 313 : DfxCircularQueue() : begin_(0), end_(0), count_(0)
27 : {
28 313 : (void)memset_sp(buffer_, sizeof(buffer_), 0, sizeof(buffer_));
29 313 : }
30 :
31 7 : u16 GetBegin() const { return begin_; }
32 :
33 2 : u16 GetEnd() const { return end_; }
34 :
35 0 : void MarkAllRead()
36 : {
37 0 : begin_ = end_;
38 0 : count_ = 0;
39 0 : }
40 :
41 47 : void* NextSlot()
42 : {
43 47 : void* slot = buffer_ + end_ * ITEM_SIZE;
44 47 : if (count_ == CAPACITY) {
45 0 : begin_ = static_cast<u16>((begin_ + 1) % CAPACITY);
46 : }
47 47 : end_ = static_cast<u16>((end_ + 1) % CAPACITY);
48 47 : if (count_ < CAPACITY) {
49 47 : count_++;
50 : }
51 47 : return slot;
52 : }
53 :
54 12 : constexpr u32 GetCapacity() const { return CAPACITY; }
55 :
56 8 : u16 GetCount() const { return count_; }
57 :
58 16 : bool IsEmpty() const { return count_ == 0; }
59 :
60 21 : bool IsFull() const { return count_ == CAPACITY; }
61 :
62 20 : T* GetSlot(u16 index) const
63 : {
64 20 : return reinterpret_cast<T*>(const_cast<u8*>(buffer_) + static_cast<size_t>(index) * ITEM_SIZE);
65 : }
66 :
67 : private:
68 : u8 buffer_[CAPACITY * ITEM_SIZE];
69 : u16 begin_;
70 : u16 end_;
71 : u16 count_;
72 : };
73 :
74 : using TaskInfoCircularQueue = DfxCircularQueue<DfxTaskInfo, DFX_TASK_INFO_QUEUE_CAPACITY>;
75 : using DfxOpInfoCircularQueue = DfxCircularQueue<DfxDfxOpInfo, DFX_OP_INFO_QUEUE_CAPACITY>;
76 :
77 : } // namespace Hccl
78 : #endif // DFX_CIRCULAR_QUEUE_H
|