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 : #include "buffer.h"
12 : #include "range_utils.h"
13 : #include "log.h"
14 : #include "string_util.h"
15 : namespace Hccl {
16 :
17 905 : Buffer::Buffer(uintptr_t addr, std::size_t size) : addr_(addr), size_(size) {}
18 :
19 3233 : Buffer::Buffer(std::size_t size) : size_(size) {}
20 :
21 18 : Buffer::Buffer(uintptr_t addr, std::size_t size, HcclMemType memType) : addr_(addr), size_(size), memType_(memType) {}
22 :
23 84 : Buffer::Buffer(uintptr_t addr, std::size_t size, HcclMemType memType, const char* memInfo)
24 84 : : addr_(addr),
25 84 : size_(size),
26 84 : memType_(memType)
27 : {
28 84 : if (memInfo != nullptr) {
29 84 : snprintf_s(memInfo_, sizeof(memInfo_), strlen(memInfo), "%s", memInfo);
30 : } else {
31 0 : memInfo_[0] = '\0'; // 初始化为空字符串
32 : }
33 84 : }
34 :
35 2 : Buffer::Buffer(uintptr_t addr, std::size_t size, const char* memInfo) : addr_(addr), size_(size)
36 : {
37 2 : if (memInfo != nullptr) {
38 2 : snprintf_s(memInfo_, sizeof(memInfo_), strlen(memInfo), "%s", memInfo);
39 : } else {
40 0 : memInfo_[0] = '\0'; // 初始化为空字符串
41 : }
42 2 : }
43 4806481 : uintptr_t Buffer::GetAddr() const { return addr_; }
44 :
45 9625052 : size_t Buffer::GetSize() const { return size_; }
46 :
47 82 : HcclMemType Buffer::GetMemType() const { return memType_; }
48 :
49 267 : const std::string Buffer::GetMemInfo() const { return memInfo_; }
50 :
51 4 : std::string Buffer::Describe() const
52 : {
53 4 : return StringFormat("Buffer[addr=0x%llx, size=0x%llx, memInfo=%s]", addr_, size_, memInfo_);
54 : }
55 :
56 1 : bool Buffer::Contains(Buffer* buf) const { return IsRangeInclude(addr_, size_, buf->addr_, buf->size_); }
57 :
58 16 : bool Buffer::Contains(uintptr_t bufAddr, size_t bufSize) const
59 : {
60 16 : return IsRangeInclude(addr_, size_, bufAddr, bufSize);
61 : }
62 :
63 25 : Buffer Buffer::Range(std::size_t offset, std::size_t givenSize) const
64 : {
65 75 : HCCL_INFO("[Buffer::Range] offset[%zu] givenSize[%zu] size[%zu]", offset, givenSize, size_);
66 25 : if (addr_ != 0 && (offset + givenSize) <= size_) {
67 24 : return Buffer(addr_ + offset, givenSize);
68 : } else {
69 3 : HCCL_WARNING("Buffer range[%zu] size[%zu Byte] error or addr[0x%llx] null", offset + givenSize, size_, addr_);
70 1 : return Buffer(0, 0);
71 : }
72 : }
73 :
74 : } // namespace Hccl
|