Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <absl/base/config.h>
 
#include "core/framework/allocator.h"
#include "core/framework/allocator_utils.h"
#include "core/session/allocator_adapters.h"
#include "core/session/abi_key_value_pairs.h"
#include "core/session/ort_apis.h"
 
#include "test/unittest_util/framework_test_utils.h"
#include "gtest/gtest.h"
 
namespace onnxruntime {
namespace test {
TEST(AllocatorTest, CPUAllocatorTest) {
  auto cpu_arena = TestCPUExecutionProvider()->CreatePreferredAllocators()[0];
 
  ASSERT_STREQ(cpu_arena->Info().name.c_str(), CPU);
  EXPECT_EQ(cpu_arena->Info().device.Id(), 0);
 
  const auto expected_allocator_type = DoesCpuAllocatorSupportArenaUsage()
                                           ? OrtAllocatorType::OrtArenaAllocator
                                           : OrtAllocatorType::OrtDeviceAllocator;
  EXPECT_EQ(cpu_arena->Info().alloc_type, expected_allocator_type);
 
  size_t size = 1024;
  auto bytes = cpu_arena->Alloc(size);
  EXPECT_TRUE(bytes);
  // test the bytes are ok for read/write
  memset(bytes, -1, 1024);
 
  EXPECT_EQ(*((int*)bytes), -1);
  cpu_arena->Free(bytes);
  // todo: test the used / max api.
}
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(disable : 26400)
#endif
// helper class to validate values in Alloc and Free calls made via IAllocator::MakeUniquePtr
class TestAllocator : public IAllocator {
 public:
  TestAllocator(size_t expected_size)
      : IAllocator(OrtMemoryInfo("test", OrtDeviceAllocator)),
        expected_size_{expected_size} {
  }
 
  void* Reserve(size_t size) override {
    reserve_is_called_ = true;
    return Alloc(size);
  }
  void* Alloc(size_t size) override {
    EXPECT_EQ(size, expected_size_);
    // return a pointer to the expected size in the result.
    // this isn't valid as a real allocator would return memory of the correct size,
    // however the unit test won't be using the memory and via this mechanism we can validate
    // that the Free argument matches.
    size_t* result = new size_t(size);
    return result;
  }
 
  void Free(void* p) override {
    // the IAllocatorUniquePtr should be calling this with the contents of what was returned from the Alloc
    size_t* p_sizet = (size_t*)p;
    EXPECT_EQ(*p_sizet, expected_size_);
    delete p_sizet;
  }
 
  bool reserve_is_called_ = false;
 
 private:
  size_t expected_size_;
};
 
// test that IAllocator::MakeUniquePtr allocates buffers of the expected size
TEST(AllocatorTest, MakeUniquePtrTest) {
  // test float creates buffer of size * sizeof(float)
  size_t num_floats = 16;
  for (bool use_reserve : {true, false}) {
    // create allocator that will check the call to Alloc matches the expected size
    auto allocator = std::make_shared<TestAllocator>(num_floats * sizeof(float));
    IAllocatorUniquePtr<float> float_ptr = IAllocator::MakeUniquePtr<float>(allocator, num_floats, use_reserve);
    float_ptr = nullptr;  // reset so TestAllocator.Free is called here
    ASSERT_EQ(allocator->reserve_is_called_, use_reserve);
    // void should create buffer of size 16 for void*
    // Create new TestAllocator to validate that.
    allocator = std::make_shared<TestAllocator>(16);
    auto void_ptr = IAllocator::MakeUniquePtr<void>(allocator, 16);
    void_ptr = nullptr;
  }
}
 
TEST(AllocatorTest, TestOverflowChecks) {
  size_t size;
  size_t element_size = sizeof(float);
  size_t num_elements = std::numeric_limits<size_t>::max() / element_size;
 
  EXPECT_TRUE(IAllocator::CalcMemSizeForArrayWithAlignment<0>(num_elements, element_size, &size));
  EXPECT_FALSE(IAllocator::CalcMemSizeForArrayWithAlignment<0>(num_elements + 1, element_size, &size));
 
  // we need to add kAllocAlignment-1 bytes to apply the alignment mask, so num_elements * element_size must be kAllocAlignment-bytes short of the max
  EXPECT_TRUE(IAllocator::CalcMemSizeForArrayWithAlignment<kAllocAlignment>(num_elements - (kAllocAlignment / element_size), element_size, &size));
  EXPECT_FALSE(IAllocator::CalcMemSizeForArrayWithAlignment<kAllocAlignment>(num_elements, element_size, &size));
 
  element_size = std::numeric_limits<size_t>::max() / 8;
  num_elements = 8;
 
  EXPECT_TRUE(IAllocator::CalcMemSizeForArrayWithAlignment<0>(num_elements, element_size, &size));
  EXPECT_FALSE(IAllocator::CalcMemSizeForArrayWithAlignment<0>(num_elements + 1, element_size, &size));
 
  // we need to add kAllocAlignment-1 bytes to apply the alignment mask, so num_elements * element_size must be kAllocAlignment-bytes short of the max
  EXPECT_TRUE(IAllocator::CalcMemSizeForArrayWithAlignment<kAllocAlignment>(num_elements, element_size - (kAllocAlignment / num_elements), &size));
  EXPECT_FALSE(IAllocator::CalcMemSizeForArrayWithAlignment<kAllocAlignment>(num_elements, element_size, &size));
}
 
// --- AsArena / SafeArenaCast tests ---
 
TEST(AllocatorTest, AsArena_ReturnsNullForNonArena) {
  auto cpu_allocator = std::make_shared<CPUAllocator>();
  EXPECT_EQ(cpu_allocator->AsArena(), nullptr);
  EXPECT_EQ(static_cast<const IAllocator*>(cpu_allocator.get())->AsArena(), nullptr);
  EXPECT_EQ(IArena::SafeArenaCast(cpu_allocator.get()), nullptr);
}
 
TEST(AllocatorTest, AsArena_ReturnsNonNullForArena) {
  if (!DoesCpuAllocatorSupportArenaUsage()) {
    GTEST_SKIP() << "CPU arena not enabled in this build";
  }
  auto cpu_arena = TestCPUExecutionProvider()->CreatePreferredAllocators()[0];
  EXPECT_NE(cpu_arena->AsArena(), nullptr);
  EXPECT_EQ(cpu_arena->AsArena(), IArena::SafeArenaCast(cpu_arena.get()));
}
 
TEST(AllocatorTest, SafeArenaCast_NullInput) {
  EXPECT_EQ(IArena::SafeArenaCast(nullptr), nullptr);
}
 
// --- IArenaImplWrappingOrtAllocator tests ---
 
namespace {
// Minimal OrtAllocator with arena-like Shrink support for unit testing.
struct MockArenaOrtAllocator : OrtAllocator {
  int alloc_count = 0;
  int free_count = 0;
  int reserve_count = 0;
  int shrink_count = 0;
  bool shrink_should_fail = false;
 
  static OrtMemoryInfo mem_info_;
 
  MockArenaOrtAllocator() {
    version = ORT_API_VERSION;
    Alloc = AllocImpl;
    Free = FreeImpl;
    Info = InfoImpl;
    Reserve = ReserveImpl;
    GetStats = GetStatsImpl;
    AllocOnStream = nullptr;
    Shrink = ShrinkImpl;
  }
 
  static void* ORT_API_CALL AllocImpl(OrtAllocator* this_, size_t size) {
    auto& self = *static_cast<MockArenaOrtAllocator*>(this_);
    self.alloc_count++;
    if (size == 0) return nullptr;
    return malloc(size);
  }
 
  static void ORT_API_CALL FreeImpl(OrtAllocator* this_, void* p) {
    auto& self = *static_cast<MockArenaOrtAllocator*>(this_);
    self.free_count++;
    free(p);
  }
 
  static const OrtMemoryInfo* ORT_API_CALL InfoImpl(const OrtAllocator* /*this_*/) {
    return &mem_info_;
  }
 
  static void* ORT_API_CALL ReserveImpl(OrtAllocator* this_, size_t size) {
    auto& self = *static_cast<MockArenaOrtAllocator*>(this_);
    self.reserve_count++;
    if (size == 0) return nullptr;
    return malloc(size);
  }
 
  static OrtStatusPtr ORT_API_CALL GetStatsImpl(const OrtAllocator* this_, OrtKeyValuePairs** out) noexcept {
    auto& self = *static_cast<const MockArenaOrtAllocator*>(this_);
    auto kvp = std::make_unique<OrtKeyValuePairs>();
    kvp->CopyFromMap(std::map<std::string, std::string>{
        {"NumAllocs", std::to_string(self.alloc_count)},
        {"NumArenaShrinkages", std::to_string(self.shrink_count)},
        {"InUse", "0"},
        {"TotalAllocated", "0"},
        {"MaxInUse", "0"},
        {"Limit", "0"},
        {"NumReserves", std::to_string(self.reserve_count)},
        {"NumArenaExtensions", "0"},
        {"MaxAllocSize", "0"},
    });
    *out = kvp.release();
    return nullptr;
  }
 
  static OrtStatusPtr ORT_API_CALL ShrinkImpl(OrtAllocator* this_) noexcept {
    auto& self = *static_cast<MockArenaOrtAllocator*>(this_);
    if (self.shrink_should_fail) {
      return OrtApis::CreateStatus(ORT_EP_FAIL, "Mock shrink failure");
    }
    self.shrink_count++;
    return nullptr;
  }
};
 
OrtMemoryInfo MockArenaOrtAllocator::mem_info_{"MockArena", OrtAllocatorType::OrtDeviceAllocator};
}  // namespace
 
TEST(AllocatorTest, IArenaWrapper_AsArenaReturnsThis) {
  MockArenaOrtAllocator mock;
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  EXPECT_NE(wrapper->AsArena(), nullptr);
  EXPECT_EQ(wrapper->AsArena(), wrapper.get());
  EXPECT_EQ(IArena::SafeArenaCast(wrapper.get()), wrapper.get());
}
 
TEST(AllocatorTest, IArenaWrapper_AllocFreeReserve) {
  MockArenaOrtAllocator mock;
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  void* p = wrapper->Alloc(256);
  EXPECT_NE(p, nullptr);
  EXPECT_EQ(mock.alloc_count, 1);
 
  wrapper->Free(p);
  EXPECT_EQ(mock.free_count, 1);
 
  void* r = wrapper->Reserve(512);
  EXPECT_NE(r, nullptr);
  EXPECT_EQ(mock.reserve_count, 1);
  wrapper->Free(r);
}
 
TEST(AllocatorTest, IArenaWrapper_ShrinkForwards) {
  MockArenaOrtAllocator mock;
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  auto status = wrapper->Shrink();
  EXPECT_TRUE(status.IsOK());
  EXPECT_EQ(mock.shrink_count, 1);
}
 
TEST(AllocatorTest, IArenaWrapper_ShrinkPropagatesError) {
  MockArenaOrtAllocator mock;
  mock.shrink_should_fail = true;
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  auto status = wrapper->Shrink();
  EXPECT_FALSE(status.IsOK());
}
 
TEST(AllocatorTest, IArenaWrapper_GetStatsRoundTrip) {
  MockArenaOrtAllocator mock;
  // Do some operations to populate counters
  void* p = MockArenaOrtAllocator::AllocImpl(&mock, 100);
  MockArenaOrtAllocator::FreeImpl(&mock, p);
  void* r = MockArenaOrtAllocator::ReserveImpl(&mock, 200);
  MockArenaOrtAllocator::FreeImpl(&mock, r);
  MockArenaOrtAllocator::ShrinkImpl(&mock);
 
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  AllocatorStats stats{};
  wrapper->GetStats(&stats);
  EXPECT_EQ(stats.num_allocs, 1);
  EXPECT_EQ(stats.num_reserves, 1);
  EXPECT_EQ(stats.num_arena_shrinkages, 1);
}
 
TEST(AllocatorTest, IArenaWrapper_ReleaseStreamBuffersIsNoop) {
  MockArenaOrtAllocator mock;
  auto wrapper = std::make_shared<IArenaImplWrappingOrtAllocator>(
      OrtAllocatorUniquePtr(&mock, [](OrtAllocator*) {}));
 
  // Should not crash — ReleaseStreamBuffers is inherited no-op from IArena
  wrapper->ReleaseStreamBuffers(nullptr);
}
 
}  // namespace test
}  // namespace onnxruntime