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