From f7e8e08e882bc6beaecf21b24acf82a01840e416 Mon Sep 17 00:00:00 2001 From: mou Date: Fri, 7 Apr 2023 05:34:32 +0800 Subject: [PATCH] Finished rdmaAllocator.hpp - Add a `construct` method in `rdmaAllocator.hpp` to enable allocation with std::vector. - Test demonstrating standard memory allocation practices. - Test demonstrating allocation with our custom allocator. - Test demonstrating allocation with std::vector. - Improve coding style and adhering to clang-tidy standards. Signed-off-by: mou --- benchmarks/warm_benchmark.cpp | 34 ++++++++++---------- rfaas/include/rfaas/rdma_allocator.hpp | 44 ++++++++++++-------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/benchmarks/warm_benchmark.cpp b/benchmarks/warm_benchmark.cpp index 68ed242..427cef2 100644 --- a/benchmarks/warm_benchmark.cpp +++ b/benchmarks/warm_benchmark.cpp @@ -71,38 +71,40 @@ int main(int argc, char ** argv) // FIXME: move me to a memory allocator // Sample: test demonstrating standard memory allocation. + // rdmalib::Buffer in(opts.input_size, rdmalib::functions::Submission::DATA_HEADER_SIZE), out(opts.input_size); // in.register_memory(executor._state.pd(), IBV_ACCESS_LOCAL_WRITE); // out.register_memory(executor._state.pd(), IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE); // Sample: test demonstrating allocation with our custom allocator. - rfaas::RdmaInfo info_in(executor,IBV_ACCESS_LOCAL_WRITE,rdmalib::functions::Submission::DATA_HEADER_SIZE); - rfaas::RdmaAllocator> allocator_in{info_in}; - rdmalib::Buffer* in0 = allocator_in.allocate(opts.input_size); - allocator_in.construct(in0, opts.input_size, rdmalib::functions::Submission::DATA_HEADER_SIZE); - rfaas::RdmaInfo info_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE)); - rfaas::RdmaAllocator> allocator_out{info_out}; - rdmalib::Buffer* out0 = allocator_out.allocate(opts.input_size); - allocator_out.construct(out0, opts.input_size); + // rfaas::RdmaInfo info_in(executor,IBV_ACCESS_LOCAL_WRITE,rdmalib::functions::Submission::DATA_HEADER_SIZE); + // rfaas::RdmaAllocator> allocator_in{info_in}; + // rdmalib::Buffer* in0 = allocator_in.allocate(opts.input_size); + // allocator_in.construct(in0, opts.input_size, rdmalib::functions::Submission::DATA_HEADER_SIZE); + // + // rfaas::RdmaInfo info_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE)); + // rfaas::RdmaAllocator> allocator_out{info_out}; + // rdmalib::Buffer* out0 = allocator_out.allocate(opts.input_size); + // allocator_out.construct(out0, opts.input_size); + // Sample: test demonstrating allocation with std::vector. - rfaas::RdmaInfo info_v_in(executor,IBV_ACCESS_LOCAL_WRITE, rdmalib::functions::Submission::DATA_HEADER_SIZE); + + rfaas::RdmaInfo info_v_in(executor, IBV_ACCESS_LOCAL_WRITE, rdmalib::functions::Submission::DATA_HEADER_SIZE); rfaas::RdmaAllocator> allocator_v_in{info_v_in}; std::vector, rfaas::RdmaAllocator>> v_in(allocator_v_in); - rfaas::RdmaInfo info_v_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE)); + rfaas::RdmaInfo info_v_out(executor, (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE)); rfaas::RdmaAllocator> allocator_v_out{info_v_out}; std::vector, rfaas::RdmaAllocator>> v_out(allocator_v_out); - // allocator_out.construct(out, opts.input_size); - - v_in.push_back({static_cast(opts.input_size),rdmalib::functions::Submission::DATA_HEADER_SIZE}); - v_out.push_back({static_cast(opts.input_size)}); - rdmalib::Buffer* in = &v_in[0]; - rdmalib::Buffer* out = &v_out[0]; + v_in.emplace_back(static_cast(opts.input_size), rdmalib::functions::Submission::DATA_HEADER_SIZE); + v_out.emplace_back(static_cast(opts.input_size)); + rdmalib::Buffer *in = &v_in[0]; + rdmalib::Buffer *out = &v_out[0]; // TODO: Since the for loop writes a value of 1 to each byte of the in buffer, diff --git a/rfaas/include/rfaas/rdma_allocator.hpp b/rfaas/include/rfaas/rdma_allocator.hpp index 7d8a6aa..731db11 100644 --- a/rfaas/include/rfaas/rdma_allocator.hpp +++ b/rfaas/include/rfaas/rdma_allocator.hpp @@ -15,11 +15,12 @@ namespace rfaas { struct RdmaInfo { public: - RdmaInfo(executor& executor, const int access, const int header_size=0) + RdmaInfo(executor &executor, const int &access, const int &header_size = 0) : executor(executor), access(access), header_size(header_size) {} - const executor& executor; - const int access; - const int header_size = 0; + + const executor &executor; + const int &access; + const int &header_size = 0; }; template @@ -28,45 +29,42 @@ namespace rfaas { public: typedef T value_type; - inline explicit RdmaAllocator(RdmaInfo& info) noexcept: _info(info) {} + inline constexpr explicit RdmaAllocator(RdmaInfo &info) noexcept: _info(info) {} template - constexpr explicit RdmaAllocator(const RdmaAllocator &) noexcept {} + inline constexpr explicit RdmaAllocator(const RdmaAllocator &) noexcept {} [[nodiscard]] inline T *allocate(const size_t &size) { if (size > std::numeric_limits::max() / sizeof(T)) throw std::bad_array_new_length(); - if (auto p = static_cast(std::malloc(size * sizeof(T) + _info.header_size))) - { - report(p, size * sizeof(T) + _info.header_size); + if (auto p = static_cast(std::malloc(size * sizeof(T) + _info.header_size))) { + report(p, size * sizeof(T) + _info.header_size); return p; } -// // Maybe we could directly call the memset function here -// if (auto buffer = new rdmalib::Buffer(size, _info.header_size)) { -// report(buffer, size); -// buffer->register_memory(_info.executor._state.pd(), _info.access); -// return buffer; -// } throw std::bad_alloc(); } - template - void construct(U* p, Args&&... args) { + template + inline void construct(U *p, Args &&... args) { ::new(p) U(std::forward(args)...); -// ::new(static_cast(p)) U(std::forward(args)...); p->register_memory(_info.executor._state.pd(), _info.access); } inline void deallocate(T *p, std::size_t size) noexcept { report(p, size, 0); - p->~T(); + std::free(p); } + template + struct rebind { + using other = RdmaAllocator; + }; + private: const RdmaInfo &_info; - void report(T *p, std::size_t size, bool alloc = true) const { + inline void report(T *p, std::size_t size, bool alloc = true) const { std::cout << (alloc ? "Alloc: " : "Dealloc: ") << size << " bytes at " << std::hex << std::showbase << reinterpret_cast(p) << std::dec << '\n'; @@ -74,10 +72,10 @@ namespace rfaas { }; template - bool operator==(const RdmaAllocator &, const RdmaAllocator &) { return true; } + inline bool operator==(const RdmaAllocator &, const RdmaAllocator &) { return true; } template - bool operator!=(const RdmaAllocator &, const RdmaAllocator &) { return false; } + inline bool operator!=(const RdmaAllocator &, const RdmaAllocator &) { return false; } } -#endif //__RFAAS_RDMA_ALLOCATOR_HPP__ +#endif //__RFAAS_RDMA_ALLOCATOR_HPP__ \ No newline at end of file