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 : #include "exception_util.h"
16 : #include "internal_exception.h"
17 : namespace Hccl {
18 :
19 917 : Buffer::Buffer(uintptr_t addr, std::size_t size) : addr_(addr), size_(size) {}
20 :
21 3258 : Buffer::Buffer(std::size_t size) : size_(size) {}
22 :
23 18 : Buffer::Buffer(uintptr_t addr, std::size_t size, HcclMemType memType) : addr_(addr), size_(size), memType_(memType) {}
24 :
25 84 : Buffer::Buffer(uintptr_t addr, std::size_t size, HcclMemType memType, const char* memInfo)
26 84 : : addr_(addr),
27 84 : size_(size),
28 84 : memType_(memType)
29 : {
30 84 : SetMemInfo(memInfo);
31 84 : }
32 :
33 3 : Buffer::Buffer(uintptr_t addr, std::size_t size, const char* memInfo) : addr_(addr), size_(size)
34 : {
35 3 : SetMemInfo(memInfo);
36 3 : }
37 :
38 87 : void Buffer::SetMemInfo(const char* memInfo)
39 : {
40 87 : if (memInfo == nullptr || memInfo[0] == '\0') {
41 0 : memInfo_[0] = '\0'; // 初始化为空字符串
42 0 : return;
43 : }
44 87 : size_t memLen = strlen(memInfo);
45 87 : if (memLen >= sizeof(memInfo_)) {
46 0 : THROW<InternalException>("[Buffer] memInfo too long, len[%zu], max[%zu].", memLen, sizeof(memInfo_) - 1);
47 : }
48 87 : int sret = snprintf_s(memInfo_, sizeof(memInfo_), memLen, "%s", memInfo);
49 87 : if (sret <= 0) {
50 0 : THROW<InternalException>(
51 0 : "[Buffer] snprintf_s failed, dest[%p], destMax[%zu], count[%zu], ret[%d].", static_cast<void*>(memInfo_),
52 : sizeof(memInfo_), memLen, sret);
53 : }
54 : }
55 4708148 : uintptr_t Buffer::GetAddr() const { return addr_; }
56 :
57 9424285 : size_t Buffer::GetSize() const { return size_; }
58 :
59 82 : HcclMemType Buffer::GetMemType() const { return memType_; }
60 :
61 267 : const std::string Buffer::GetMemInfo() const { return memInfo_; }
62 :
63 4 : std::string Buffer::Describe() const
64 : {
65 4 : return StringFormat("Buffer[addr=0x%llx, size=0x%llx, memInfo=%s]", addr_, size_, memInfo_);
66 : }
67 :
68 1 : bool Buffer::Contains(Buffer* buf) const { return IsRangeInclude(addr_, size_, buf->addr_, buf->size_); }
69 :
70 16 : bool Buffer::Contains(uintptr_t bufAddr, size_t bufSize) const
71 : {
72 16 : return IsRangeInclude(addr_, size_, bufAddr, bufSize);
73 : }
74 :
75 25 : Buffer Buffer::Range(std::size_t offset, std::size_t givenSize) const
76 : {
77 75 : HCCL_INFO("[Buffer::Range] offset[%zu] givenSize[%zu] size[%zu]", offset, givenSize, size_);
78 25 : if (addr_ != 0 && (offset + givenSize) <= size_) {
79 24 : return Buffer(addr_ + offset, givenSize);
80 : } else {
81 3 : HCCL_WARNING("Buffer range[%zu] size[%zu Byte] error or addr[0x%llx] null", offset + givenSize, size_, addr_);
82 1 : return Buffer(0, 0);
83 : }
84 : }
85 :
86 : } // namespace Hccl
|