diff --git a/conanfile.py b/conanfile.py index 61d77e40d..048f46f9b 100644 --- a/conanfile.py +++ b/conanfile.py @@ -2,7 +2,7 @@ class HomestoreConan(ConanFile): name = "homestore" - version = "3.7.1" + version = "3.8.1" homepage = "https://github.corp.ebay.com/SDS/homestore" description = "HomeStore" diff --git a/src/.clang-format b/src/.clang-format index 2f7712008..fdfa11f5e 100644 --- a/src/.clang-format +++ b/src/.clang-format @@ -18,7 +18,6 @@ AlignOperands: false AlignTrailingComments: true AllowShortBlocksOnASingleLine: true AllowShortIfStatementsOnASingleLine: true -AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false # AllowShortFunctionsOnASingleLine: InlineOnly # AllowShortLoopsOnASingleLine: false diff --git a/src/api/vol_interface.hpp b/src/api/vol_interface.hpp index 53627911b..7436ee5ea 100644 --- a/src/api/vol_interface.hpp +++ b/src/api/vol_interface.hpp @@ -114,6 +114,22 @@ struct vol_interface_req : public sisl::ObjLifeCounter< vol_interface_req > { bool is_write() const { return op_type == Op_type::WRITE; } bool is_unmap() const { return op_type == Op_type::UNMAP; } + bool is_zero_request(const uint64_t page_size) { + if (iovecs.empty()) { + return !buffer || hs_utils::is_buf_zero(static_cast< uint8_t* >(buffer), nlbas * page_size); + } + return is_iovec_zero(); + } + + bool is_iovec_zero() { + for (const auto& iovec : iovecs) { + auto data = static_cast< uint8_t* >(iovec.iov_base); + const size_t size = iovec.iov_len; + if (!hs_utils::is_buf_zero(data, size)) { return false; } + } + return true; + } + friend void intrusive_ptr_add_ref(vol_interface_req* req) { req->refcount.increment(1); } friend void intrusive_ptr_release(vol_interface_req* req) { @@ -316,7 +332,7 @@ class VolInterface { virtual const char* get_name(const VolumePtr& vol) = 0; virtual uint64_t get_size(const VolumePtr& vol) = 0; - virtual std::map get_used_size(const VolumePtr& vol) = 0; + virtual std::map< boost::uuids::uuid, uint64_t > get_used_size(const VolumePtr& vol) = 0; virtual uint64_t get_page_size(const VolumePtr& vol) = 0; virtual boost::uuids::uuid get_uuid(std::shared_ptr< Volume > vol) = 0; virtual sisl::blob at_offset(const boost::intrusive_ptr< BlkBuffer >& buf, uint32_t offset) = 0; diff --git a/src/engine/common/homestore_config.fbs b/src/engine/common/homestore_config.fbs index e6dfee051..6d9c9f461 100644 --- a/src/engine/common/homestore_config.fbs +++ b/src/engine/common/homestore_config.fbs @@ -142,6 +142,8 @@ table Generic { // percentage of cache used to create indx mempool. It should be more than 100 to // take into account some floating buffers in writeback cache. indx_mempool_percent : uint32 = 110; + + enable_zero_padding: bool = true; } table ResourceLimits { diff --git a/src/engine/common/homestore_utils.cpp b/src/engine/common/homestore_utils.cpp index fa60cbb3a..c161af0fa 100644 --- a/src/engine/common/homestore_utils.cpp +++ b/src/engine/common/homestore_utils.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace homestore { uint8_t* hs_utils::iobuf_alloc(const size_t size, const sisl::buftag tag, const size_t alignment) { @@ -76,6 +77,42 @@ sisl::byte_array hs_utils::extract_byte_array(const sisl::byte_view& b, const bo return (is_aligned_needed) ? b.extract(alignment) : b.extract(0); }; +constexpr unsigned long long operator"" _KB(unsigned long long x) { return x * 1024; } + +constexpr std::array< size_t, 7 > predefined_sizes = {4_KB, 8_KB, 16_KB, 32_KB, 64_KB, 128_KB, 256_KB}; + +// Function to initialize the CRC map with predefined sizes +void initialize_crc_map(std::map< size_t, uint16_t >& crc_map) { + std::vector< uint8_t > zero_buf; + for (auto s : predefined_sizes) { + zero_buf.resize(s, 0); // Resize buffer to the required size, filling with zeros + crc_map[s] = crc16_t10dif(init_crc_16, zero_buf.data(), s); + } +} + +uint16_t hs_utils::crc_zero(const size_t size) { + static std::map< size_t, uint16_t > crc_map; + static std::once_flag init_flag; + + // Thread-safe initialization of the CRC map + std::call_once(init_flag, initialize_crc_map, std::ref(crc_map)); + + // Check if the size is already in the map + if (auto it = crc_map.find(size); it != crc_map.end()) { return it->second; } + + std::vector< uint8_t > zero_buf(size, 0); + return crc16_t10dif(init_crc_16, zero_buf.data(), size); +} + +bool hs_utils::is_buf_zero(const uint8_t* buf, size_t size) { + // TODO: subsample the buffer to detect zero request instead of working on the whole buffer to achieve constant + // processing time for large buffer size requests. Needs to investigate the performance impact of this change + // in end2end testing. + auto zero_crc = crc_zero(size); + const auto crc = crc16_t10dif(init_crc_16, buf, size); + return (crc == zero_crc) ? (buf[0] == 0 && !std::memcmp(buf, buf + 1, size - 1)) : false; +} + std::string hs_utils::encodeBase64(const uint8_t* first, std::size_t size) { using Base64FromBinary = boost::archive::iterators::base64_from_binary< boost::archive::iterators::transform_width< const char*, // sequence of chars @@ -90,15 +127,12 @@ std::string hs_utils::encodeBase64(const uint8_t* first, std::size_t size) { return encoded.append(bytes_to_pad, '='); } -std::string hs_utils::encodeBase64(const sisl::byte_view& b){ - return encodeBase64(b.bytes(), b.size()); -} +std::string hs_utils::encodeBase64(const sisl::byte_view& b) { return encodeBase64(b.bytes(), b.size()); } -template -void hs_utils::decodeBase64(const std::string &encoded_data, T out) -{ +template < typename T > +void hs_utils::decodeBase64(const std::string& encoded_data, T out) { using BinaryFromBase64 = boost::archive::iterators::transform_width< - boost::archive::iterators::binary_from_base64, + boost::archive::iterators::binary_from_base64< std::string::const_iterator >, 8, // get a view of 8 bit 6 // from a sequence of 6 bit >; @@ -107,14 +141,13 @@ void hs_utils::decodeBase64(const std::string &encoded_data, T out) std::replace(begin(unpadded_data), end(unpadded_data), '=', 'A'); // A_64 == \0 std::string decoded_data{BinaryFromBase64{begin(unpadded_data)}, - BinaryFromBase64{begin(unpadded_data) + unpadded_data.length()}}; + BinaryFromBase64{begin(unpadded_data) + unpadded_data.length()}}; decoded_data.erase(end(decoded_data) - bytes_to_pad, end(decoded_data)); std::copy(begin(decoded_data), end(decoded_data), out); } -std::string hs_utils::decodeBase64(const std::string &encoded_data) -{ +std::string hs_utils::decodeBase64(const std::string& encoded_data) { std::string rv; decodeBase64(encoded_data, std::back_inserter(rv)); return rv; diff --git a/src/engine/common/homestore_utils.hpp b/src/engine/common/homestore_utils.hpp index b1313df96..de081d7f8 100644 --- a/src/engine/common/homestore_utils.hpp +++ b/src/engine/common/homestore_utils.hpp @@ -38,6 +38,8 @@ class hs_utils { static sisl::byte_array make_byte_array(const uint64_t size, const bool is_aligned_needed, const sisl::buftag tag, const size_t alignment); static hs_uuid_t gen_system_uuid(); + static uint16_t crc_zero(const size_t size); + static bool is_buf_zero(const uint8_t* buf, size_t size); static std::string encodeBase64(const uint8_t* first, std::size_t size); static std::string encodeBase64(const sisl::byte_view& b); template static void decodeBase64(const std::string &encoded_data, T out); diff --git a/src/homeblks/homeblks_http_server.cpp b/src/homeblks/homeblks_http_server.cpp index f7bffcd92..208fc0f29 100644 --- a/src/homeblks/homeblks_http_server.cpp +++ b/src/homeblks/homeblks_http_server.cpp @@ -155,12 +155,13 @@ void HomeBlksHttpServer::set_log_level(const Pistache::Rest::Request& request, response.send(Pistache::Http::Code::Ok, resp); } -void HomeBlksHttpServer::get_utilization(const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter response) -{ - const std::string vol_uuid = request.hasParam(":volumeUUID") ? request.param(":volumeUUID").as():""; +void HomeBlksHttpServer::get_utilization(const Pistache::Rest::Request& request, + Pistache::Http::ResponseWriter response) { + const std::string vol_uuid = + request.hasParam(":volumeUUID") ? request.param(":volumeUUID").as< std::string >() : ""; VolumePtr vol = nullptr; - if (vol_uuid.length() != 0) { + if (vol_uuid.length()) { boost::uuids::string_generator gen; boost::uuids::uuid uuid = gen(vol_uuid); vol = VolInterface::get_instance()->lookup_volume(uuid); @@ -170,10 +171,14 @@ void HomeBlksHttpServer::get_utilization(const Pistache::Rest::Request& request, } } nlohmann::json resp; - const auto total_data_size = VolInterface::get_instance()->get_system_capacity().initial_total_data_meta_size; + nlohmann::json partitions = nlohmann::json::array(); for (auto [uuid, vol_used] : VolInterface::get_instance()->get_used_size(vol)) { - resp[boost::uuids::to_string(uuid)] = std::to_string(static_cast (vol_used)/ total_data_size); + nlohmann::json partition; + partition["id"] = boost::uuids::to_string(uuid); + partition["usedCapacity"] = vol_used; + partitions.push_back(partition); } + resp["partitions"] = partitions; response.send(Pistache::Http::Code::Ok, resp.dump()); } void HomeBlksHttpServer::get_log_level(const Pistache::Rest::Request& request, diff --git a/src/homeblks/volume/tests/vol_gtest.cpp b/src/homeblks/volume/tests/vol_gtest.cpp index d8aa4ef17..73b96bc62 100644 --- a/src/homeblks/volume/tests/vol_gtest.cpp +++ b/src/homeblks/volume/tests/vol_gtest.cpp @@ -174,6 +174,9 @@ struct TestCfg { uint32_t p_vol_files_space; std::string flip_name; std::string vol_copy_file_path; + uint32_t p_zero_buffer; + uint32_t zero_buffer_period; + bool enable_zero_padding{false}; bool verify_csum() { return verify_type == verify_type_t::csum; } bool verify_data() { return verify_type == verify_type_t::data; } @@ -575,6 +578,7 @@ class VolTest : public ::testing::Test { friend class VolCreateDeleteJob; friend class IOTestJob; friend class VolVerifyJob; + friend class IOManualTestJob; protected: std::atomic< size_t > outstanding_ios; @@ -620,12 +624,20 @@ class VolTest : public ::testing::Test { // vol_create_del_test = false; // move_verify_to_done = false; print_startTime = Clock::now(); + if (tcfg.enable_zero_padding) { + HS_SETTINGS_FACTORY().modifiable_settings([](auto& s) { s.generic.enable_zero_padding = true; }); + HS_SETTINGS_FACTORY().save(); + } // outstanding_ios = 0; } virtual ~VolTest() override { if (init_buf) { iomanager.iobuf_free(static_cast< uint8_t* >(init_buf)); } + if (tcfg.enable_zero_padding) { + HS_SETTINGS_FACTORY().modifiable_settings([](auto& s) { s.generic.enable_zero_padding = false; }); + HS_SETTINGS_FACTORY().save(); + } } VolTest(const VolTest&) = delete; @@ -1815,13 +1827,18 @@ class IOTestJob : public TestJob { const uint64_t page_size{VolInterface::get_instance()->get_page_size(vol)}; const uint64_t size{nlbas * page_size}; + static std::atomic< uint32_t > remaining_period{tcfg.zero_buffer_period}; + uint32_t zero_counts_per_period = tcfg.p_zero_buffer * tcfg.zero_buffer_period / 100; boost::intrusive_ptr< io_req_t > vreq{}; if (tcfg.write_cache) { uint8_t* const wbuf{iomanager.iobuf_alloc(512, size)}; HS_REL_ASSERT_NOTNULL(wbuf); populate_buf(wbuf, size, lba, vinfo.get()); - + if (HS_DYNAMIC_CONFIG(generic->enable_zero_padding) && + remaining_period.fetch_sub(1) < zero_counts_per_period) { + populate_zero_buf(wbuf, size); + } vreq = boost::intrusive_ptr< io_req_t >( new io_req_t(vinfo, Op_type::WRITE, wbuf, lba, nlbas, tcfg.verify_csum(), tcfg.write_cache)); } else { @@ -1833,20 +1850,32 @@ class IOTestJob : public TestJob { HS_REL_ASSERT_NOTNULL(wbuf); iovec iov{static_cast< void* >(wbuf), static_cast< size_t >(page_size)}; iovecs.emplace_back(std::move(iov)); - populate_buf(wbuf, page_size, lba + lba_num, vinfo.get()); } + if (HS_DYNAMIC_CONFIG(generic->enable_zero_padding) && + remaining_period.fetch_sub(1) < zero_counts_per_period) { + for (const auto& iovec : iovecs) { + auto data = static_cast< uint8_t* >(iovec.iov_base); + const size_t size = iovec.iov_len; + populate_zero_buf(data, size); + } + } vreq = boost::intrusive_ptr< io_req_t >(new io_req_t(vinfo, Op_type::WRITE, std::move(iovecs), lba, nlbas, tcfg.verify_csum(), tcfg.write_cache)); } else { uint8_t* const wbuf{iomanager.iobuf_alloc(512, size)}; populate_buf(wbuf, size, lba, vinfo.get()); + if (HS_DYNAMIC_CONFIG(generic->enable_zero_padding) && + remaining_period.fetch_sub(1) < zero_counts_per_period) { + populate_zero_buf(wbuf, size); + } HS_REL_ASSERT_NOTNULL(wbuf); vreq = boost::intrusive_ptr< io_req_t >{ new io_req_t(vinfo, Op_type::WRITE, wbuf, lba, nlbas, tcfg.verify_csum(), tcfg.write_cache)}; } + if (remaining_period.load() == 0) { remaining_period.store(tcfg.zero_buffer_period); } send_iovec = !send_iovec; } vreq->cookie = static_cast< void* >(this); @@ -1880,6 +1909,8 @@ class IOTestJob : public TestJob { } } + void populate_zero_buf(uint8_t* buf, const uint64_t size) { std::fill_n(buf, size, 0); } + bool read_vol(const uint32_t cur, const uint64_t lba, const uint32_t nlbas) { const auto vinfo{m_voltest->m_vol_info[cur]}; const auto vol{vinfo->vol}; @@ -2066,6 +2097,199 @@ class IOTestJob : public TestJob { } }; +// This test job is used to test the IOs with manual requests. For sake of simplicity, we will use the same volume for +// all requests. The caller needs to load the requests before starting the job. The requests are loaded in the form of +// Write with three or four parameters and Read with three parameters. The value is optional and is used only for write +// requests. +class IOManualTestJob : public TestJob { +public: + using TupleVariant = std::variant< std::tuple< std::string, uint64_t, uint32_t >, + std::tuple< std::string, uint64_t, uint32_t, uint8_t > >; + using RequestVector = std::vector< IOManualTestJob::TupleVariant >; + IOManualTestJob(VolTest* const test) : TestJob(test, 1, true) { + vol = m_voltest->m_vol_info[0]->vol; + vinfo = m_voltest->m_vol_info[0]; + page_size = VolInterface::get_instance()->get_page_size(vol); + const auto vol_size = VolInterface::get_instance()->get_size(vol); + const auto max_lbas = vol_size / page_size; + m_validate_buf.resize(max_lbas); + std::fill(m_validate_buf.begin(), m_validate_buf.end(), 0); + LOGINFO("Manual volume size {} max_lbas {}", vol_size, max_lbas); + } + virtual ~IOManualTestJob() override = default; + IOManualTestJob(const IOManualTestJob&) = delete; + IOManualTestJob(IOManualTestJob&&) noexcept = delete; + IOManualTestJob& operator=(const IOManualTestJob&) = delete; + IOManualTestJob& operator=(IOManualTestJob&&) noexcept = delete; + + virtual void run_one_iteration() override { + if (m_outstanding_ios.load() == 0 && m_current_request < m_requests.size()) { + const auto& request = m_requests[m_current_request]; + if (std::holds_alternative< std::tuple< std::string, uint64_t, uint32_t > >(request)) { + auto& tuple = std::get< std::tuple< std::string, uint64_t, uint32_t > >(request); + auto start_lba = std::get< 1 >(tuple); + auto nlbas = std::get< 2 >(tuple); + if (std::get< 0 >(tuple) == "write") { + write_vol(start_lba, nlbas); + auto it = m_validate_buf.begin() + start_lba; + std::fill(it, it + nlbas, 0); + } else { + read_vol(start_lba, nlbas); + } + } else if (std::holds_alternative< std::tuple< std::string, uint64_t, uint32_t, uint8_t > >(request)) { + auto& tuple = std::get< std::tuple< std::string, uint64_t, uint32_t, uint8_t > >(request); + auto start_lba = std::get< 1 >(tuple); + auto nlbas = std::get< 2 >(tuple); + auto value = std::get< 3 >(tuple); + if (std::get< 0 >(tuple) == "write") { + write_vol(start_lba, nlbas, value); + auto it = m_validate_buf.begin() + start_lba; + std::fill(it, it + nlbas, value); + } else { + // in case, the caller mistakenly added a value for a read request, we will ignore the value + read_vol(start_lba, nlbas); + } + } + } + } + + void on_one_iteration_completed(const boost::intrusive_ptr< io_req_t >& req) override { + --m_outstanding_ios; + if (req->op_type == Op_type::READ) { verify_request(req); } + req->vol_info->ref_cnt.decrement_testz(1); + } + uint64_t read_buffer(std::vector< iovec >& iovecs, uint8_t* buf) { + uint8_t* current_position = buf; + for (const auto& iov : iovecs) { + std::memcpy(current_position, iov.iov_base, iov.iov_len); + current_position += iov.iov_len; + } + return static_cast< uint64_t >(current_position - buf); + } + void verify_request(const boost::intrusive_ptr< io_req_t >& req) { + std::shared_ptr< uint8_t > buf(new uint8_t[req->nlbas * page_size]); + std::fill_n(buf.get(), req->nlbas * page_size, 0); + auto total_size_read = read_buffer(req->iovecs, buf.get()); + HS_REL_ASSERT_EQ(req->nlbas * page_size, total_size_read); + auto raw_buf = buf.get(); + for (size_t i = 0; i < req->nlbas; i++) { + HS_REL_ASSERT_EQ(raw_buf[i * page_size], m_validate_buf[req->lba + i]); + } + } + bool time_to_stop() const override { return m_current_request == m_requests.size(); } + + virtual bool is_job_done() const override { return (m_outstanding_ios == 0); } + bool is_async_job() const override { return true; } + std::string job_name() const { return "IO Manual Job"; } + void load_requests(RequestVector& requests) { m_requests = requests; } + +protected: + VolumePtr vol; + std::shared_ptr< vol_info_t > vinfo; + uint64_t page_size; + std::atomic< uint64_t > m_outstanding_ios{0}; + std::atomic< uint64_t > m_current_request{0}; + std::vector< uint8_t > m_validate_buf; + RequestVector m_requests; + + bool write_vol(const uint64_t lba, const uint32_t nlbas, const uint8_t value = 0) { + ++m_current_request; + ++m_outstanding_ios; + const uint64_t size{nlbas * page_size}; + boost::intrusive_ptr< io_req_t > vreq{}; + if (tcfg.write_cache) { + uint8_t* const wbuf{iomanager.iobuf_alloc(512, size)}; + populate_buf(wbuf, size, value); + vreq = boost::intrusive_ptr< io_req_t >( + new io_req_t(vinfo, Op_type::WRITE, wbuf, lba, nlbas, tcfg.verify_csum(), tcfg.write_cache)); + } else { + static bool send_iovec{true}; + std::vector< iovec > iovecs{}; + if (send_iovec) { + for (uint32_t lba_num{0}; lba_num < nlbas; ++lba_num) { + uint8_t* const wbuf{iomanager.iobuf_alloc(512, page_size)}; + iovec iov{static_cast< void* >(wbuf), static_cast< size_t >(page_size)}; + iovecs.emplace_back(std::move(iov)); + populate_buf(wbuf, page_size, value); + } + vreq = boost::intrusive_ptr< io_req_t >(new io_req_t(vinfo, Op_type::WRITE, std::move(iovecs), lba, + nlbas, tcfg.verify_csum(), tcfg.write_cache)); + } else { + uint8_t* const wbuf{iomanager.iobuf_alloc(512, size)}; + populate_buf(wbuf, size, value); + vreq = boost::intrusive_ptr< io_req_t >{ + new io_req_t(vinfo, Op_type::WRITE, wbuf, lba, nlbas, tcfg.verify_csum(), tcfg.write_cache)}; + } + send_iovec = !send_iovec; + } + vreq->cookie = static_cast< void* >(this); + + ++m_voltest->output.write_cnt; + vinfo->ref_cnt.increment(1); + const auto ret_io{VolInterface::get_instance()->write(vol, vreq)}; + LOGDEBUG("Wrote lba: {}, nlbas: {} outstanding_ios={}, iovec(s)={}, cache={}", lba, nlbas, + m_outstanding_ios.load(), (tcfg.write_iovec != 0 ? true : false), + (tcfg.write_cache != 0 ? true : false)); + if (ret_io != no_error) { return false; } + return true; + } + + void populate_buf(uint8_t* buf, const uint64_t size, const uint8_t value = 0) { std::fill_n(buf, size, value); } + + bool read_vol(const uint64_t lba, const uint32_t nlbas) { + ++m_current_request; + if (read_vol_internal(vinfo, vol, lba, nlbas, false)) { return true; } + return false; + } + + boost::intrusive_ptr< io_req_t > read_vol_internal(std::shared_ptr< vol_info_t > vinfo, VolumePtr vol, + const uint64_t lba, const uint32_t nlbas, + const bool sync = false) { + boost::intrusive_ptr< io_req_t > vreq{}; + if (tcfg.read_cache) { + vreq = boost::intrusive_ptr< io_req_t >{ + new io_req_t{vinfo, Op_type::READ, nullptr, lba, nlbas, tcfg.verify_csum(), tcfg.read_cache, sync}}; + } else { + static bool send_iovec{true}; + if (send_iovec) { + std::vector< iovec > iovecs{}; + for (uint32_t lba_num{0}; lba_num < nlbas; ++lba_num) { + uint8_t* const rbuf{iomanager.iobuf_alloc(512, page_size)}; + std::memset(static_cast< void* >(rbuf), 0, page_size); + + HS_REL_ASSERT_NOTNULL(rbuf); + iovec iov{static_cast< void* >(rbuf), static_cast< size_t >(page_size)}; + iovecs.emplace_back(std::move(iov)); + } + + vreq = boost::intrusive_ptr< io_req_t >{new io_req_t{vinfo, Op_type::READ, std::move(iovecs), lba, + nlbas, tcfg.verify_csum(), tcfg.read_cache, sync}}; + } else { + uint8_t* const rbuf{iomanager.iobuf_alloc(512, nlbas * page_size)}; + std::memset(static_cast< void* >(rbuf), 0, nlbas * page_size); + vreq = boost::intrusive_ptr< io_req_t >{ + new io_req_t{vinfo, Op_type::READ, rbuf, lba, nlbas, tcfg.verify_csum(), tcfg.read_cache, sync}}; + } + send_iovec = !send_iovec; + } + vreq->cookie = static_cast< void* >(this); + + ++m_voltest->output.read_cnt; + ++m_outstanding_ios; + vinfo->ref_cnt.increment(1); + const auto ret_io{VolInterface::get_instance()->read(vol, vreq)}; + LOGDEBUG("Read lba: {}, nlbas: {} outstanding_ios={}, iovec(s)={}, cache={}", lba, nlbas, + m_outstanding_ios.load(), (tcfg.read_iovec != 0 ? true : false), + (tcfg.read_cache != 0 ? true : false)); + if (sync) { + --m_outstanding_ios; + vinfo->ref_cnt.decrement(1); + } + if (ret_io != no_error) { return nullptr; } + return vreq; + } +}; + class VolVerifyJob : public IOTestJob { public: VolVerifyJob(VolTest* test) : IOTestJob(test, load_type_t::sequential) { @@ -2223,6 +2447,43 @@ TEST_F(VolTest, init_io_test) { this->shutdown(); if (tcfg.remove_file_on_shutdown) { this->remove_files(); } } +TEST_F(VolTest, thin_test) { + HS_SETTINGS_FACTORY().modifiable_settings([](auto& s) { s.generic.enable_zero_padding = true; }); + HS_SETTINGS_FACTORY().save(); + tcfg.max_vols = 1; + tcfg.verify_type = static_cast< verify_type_t >(3); + tcfg.max_disk_capacity = 1 * (1ul << 30); // 1GB + tcfg.p_volume_size = 1; // 1% of 2 (devices) * 1G = 20 MB volume + output.print("thin_test"); + + this->start_homestore(); + + std::unique_ptr< IOManualTestJob > job; + job = std::make_unique< IOManualTestJob >(this); + // request = op=[write|read], lba, nlbas [value], value is optional and is used only for write requests and If not + // provided, it defaults to 0. + IOManualTestJob::RequestVector reqs = { + // Case one: normal read (no zero padding) + std::make_tuple("write", 0, 100, 4), std::make_tuple("read", 5, 20), + // Case two: zero padding, read after write + std::make_tuple("write", 1, 10), std::make_tuple("read", 1, 20), std::make_tuple("read", 5, 3), + // Case three: zero padding, overlapping for read + std::make_tuple("write", 100, 200), std::make_tuple("read", 150, 250), + // Case four: no write + std::make_tuple("read", 800, 5)}; + job->load_requests(reqs); + + this->start_job(job.get(), wait_type::for_completion); + + LOGINFO("All volumes are deleted, do a shutdown of homestore"); + this->shutdown(); + + LOGINFO("Shutdown of homestore is completed, removing files"); + this->remove_files(); + + HS_SETTINGS_FACTORY().modifiable_settings([](auto& s) { s.generic.enable_zero_padding = false; }); + HS_SETTINGS_FACTORY().save(); +} /*! @test recovery_io_test @@ -2682,6 +2943,13 @@ SISL_OPTION_GROUP( (io_size, "", "io_size", "io size in KB", ::cxxopts::value< uint32_t >()->default_value("4"), "io_size"), (vol_copy_file_path, "", "vol_copy_file_path", "file path for copied volume", ::cxxopts::value< std::string >()->default_value(""), "path [...]"), + (p_zero_buffer, "", "p_zero_buffer", + "percentage of zero buffer occurrence for testing thin provisioning within period", + ::cxxopts::value< uint32_t >()->default_value("70"), "0 to 100"), + (zero_buffer_period, "", "zero_buffer_period", " the period of consecutive zero buffer occurrence", + ::cxxopts::value< uint32_t >()->default_value("100"), "0 to 100"), + (enable_zero_padding, "", "enable_zero_padding", " enable zero padding", + ::cxxopts::value< uint32_t >()->default_value("1"), "flag"), (unmap_frequency, "", "unmap_frequency", "do unmap for every N", ::cxxopts::value< uint64_t >()->default_value("100"), "unmap_frequency")) @@ -2758,6 +3026,9 @@ int main(int argc, char* argv[]) { gcfg.app_mem_size_in_gb = SISL_OPTIONS["app_mem_size_in_gb"].as< uint32_t >(); gcfg.vol_copy_file_path = SISL_OPTIONS["vol_copy_file_path"].as< std::string >(); const auto io_size_in_kb = SISL_OPTIONS["io_size"].as< uint32_t >(); + gcfg.p_zero_buffer = SISL_OPTIONS["p_zero_buffer"].as< uint32_t >(); + gcfg.zero_buffer_period = SISL_OPTIONS["zero_buffer_period"].as< uint32_t >(); + gcfg.enable_zero_padding = SISL_OPTIONS["enable_zero_padding"].as< uint32_t >() != 0 ? true : false; gcfg.io_size = io_size_in_kb * 1024; HS_REL_ASSERT(io_size_in_kb && (io_size_in_kb % 4 == 0), diff --git a/src/homeblks/volume/volume.cpp b/src/homeblks/volume/volume.cpp index 77ff6fee7..1e26a441e 100644 --- a/src/homeblks/volume/volume.cpp +++ b/src/homeblks/volume/volume.cpp @@ -171,7 +171,7 @@ Volume::Volume(const vol_params& params) : throw std::runtime_error("shutdown in progress"); } m_sobject = m_hb->sobject_mgr()->create_object("volume", params.vol_name, - std::bind(&Volume::get_status, this, std::placeholders::_1)); + std::bind(&Volume::get_status, this, std::placeholders::_1)); m_state = vol_state::UNINITED; } @@ -190,7 +190,7 @@ Volume::Volume(meta_blk* mblk_cookie, sisl::byte_view sb_buf) : HS_REL_ASSERT_EQ(sb->magic, vol_sb_magic, "magic mismatch"); m_hb = HomeBlks::safe_instance(); m_sobject = m_hb->sobject_mgr()->create_object("volume", sb->vol_name, - std::bind(&Volume::get_status, this, std::placeholders::_1)); + std::bind(&Volume::get_status, this, std::placeholders::_1)); } void Volume::init() { @@ -335,7 +335,69 @@ indx_tbl* Volume::recover_indx_tbl(btree_super_block& sb, btree_cp_sb& cp_info) return static_cast< indx_tbl* >(tbl); } +#if 0 +// TODO: use these functions for near future optimization of write path for thin provisioning volumes to enable skipping +// writing empty blocks in subrange intervals for requested buffer instead of detecting the all-zero-buffer requests. +static std::vector< std::pair< int, int > > compute_range_intervals(const uint8_t* buf, size_t page_size, + uint32_t nlbas, bool empty_blocks = false) { + std::vector< std::pair< int, int > > intervals; + bool in_empty_region = false; + int current_range_start = -1; + int current_range_length = 1; + for (uint32_t i = 0; i < nlbas; i++) { + const uint8_t* page_start = buf + (i * page_size); + bool is_page_empty = (empty_blocks == is_buf_zero(page_start, page_size)); + if (is_page_empty) { + if (!in_empty_region) { + current_range_start = i; + current_range_length = 1; + in_empty_region = true; + } else { + current_range_length++; + } + } else { + if (in_empty_region) { intervals.push_back(std::make_pair(current_range_start, current_range_length)); } + in_empty_region = false; + } + } + if (in_empty_region) { intervals.push_back(std::make_pair(current_range_start, current_range_length)); } + return intervals; +} + +static std::string print_ranges(lba_t start_lba, const std::vector< std::pair< int, int > >& intervals) { + auto intervals_to_string = [start_lba](const std::vector< std::pair< int, int > >& intervals) -> std::string { + std::vector< std::string > result_strings; + std::transform(intervals.begin(), intervals.end(), std::back_inserter(result_strings), + [start_lba](const std::pair< int, int >& p) -> std::string { + // Use a static buffer to hold the formatted string + static char buffer[32]; + std::snprintf(buffer, sizeof(buffer), "<%ld,%d>", p.first + start_lba, p.second); + return buffer; + }); + return std::accumulate(result_strings.begin(), result_strings.end(), std::string("")); + }; + return intervals_to_string(intervals); +} +#endif + std::error_condition Volume::write(const vol_interface_req_ptr& iface_req) { + std::error_condition ret{no_error}; + if (!HS_DYNAMIC_CONFIG(generic->enable_zero_padding)) { + return write_internal(iface_req); + } else { + if (iface_req->is_zero_request(get_page_size())) { + THIS_VOL_LOG(TRACE, volume, iface_req, "zero request <{}, {}>", iface_req->lba, iface_req->nlbas); + iface_req->op_type = Op_type::UNMAP; + ret = unmap(iface_req); + } else { + ret = write_internal(iface_req); + } + } + iface_req->op_type = Op_type::WRITE; + return ret; +} + +std::error_condition Volume::write_internal(const vol_interface_req_ptr& iface_req) { static thread_local std::vector< BlkId > bid{}; std::error_condition ret{no_error}; @@ -924,11 +986,11 @@ sisl::status_response Volume::get_status(const sisl::status_request& request) { auto active_indx_json = get_active_indx()->sobject()->run_callback(request).json; if (!active_indx_json.empty()) { response.json["index"] = active_indx_json; } - response.json["name"] = sobject()->name(); + response.json["name"] = sobject()->name(); response.json["type"] = sobject()->type(); response.json["uuid"] = boost::lexical_cast< std::string >(get_uuid()); response.json["state"] = is_offline() ? "Offline" : "Online"; - response.json["size"]= get_size(); + response.json["size"] = get_size(); return response; } diff --git a/src/homeblks/volume/volume.hpp b/src/homeblks/volume/volume.hpp index 10c2fbbf5..2617dddc7 100644 --- a/src/homeblks/volume/volume.hpp +++ b/src/homeblks/volume/volume.hpp @@ -464,6 +464,7 @@ class Volume : public std::enable_shared_from_this< Volume > { * @return :- no_error if there is no error. It doesn't throw any exception */ std::error_condition write(const vol_interface_req_ptr& hb_req); + std::error_condition write_internal(const vol_interface_req_ptr& hb_req); /* Read from lba * @param hb_req :- it expects this request to be created @@ -729,7 +730,7 @@ struct volume_req : indx_req { csum_t* j_csum = (csum_t*)mem; if (!is_unmap() && active_nlbas_written != nlbas()) { - VOL_ERROR_LOG(vol()->get_name(), "all lbas are not written. lba written {}, lba supposed to write{}", + VOL_ERROR_LOG(vol()->get_name(), "all lbas are not written. lba written {}, lba supposed to write: {}", active_nlbas_written, nlbas()); } for (lba_count_t i{0}; !is_unmap() && i < active_nlbas_written; ++i) {