diff options
Diffstat (limited to 'libtransport/includes/hicn/transport/utils')
25 files changed, 456 insertions, 87 deletions
diff --git a/libtransport/includes/hicn/transport/utils/CMakeLists.txt b/libtransport/includes/hicn/transport/utils/CMakeLists.txt index 75f727f03..060b83b63 100644 --- a/libtransport/includes/hicn/transport/utils/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/utils/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -19,6 +19,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/chrono_typedefs.h ${CMAKE_CURRENT_SOURCE_DIR}/branch_prediction.h ${CMAKE_CURRENT_SOURCE_DIR}/ring_buffer.h + ${CMAKE_CURRENT_SOURCE_DIR}/rtc_quality_score.h ${CMAKE_CURRENT_SOURCE_DIR}/literals.h ${CMAKE_CURRENT_SOURCE_DIR}/conversions.h ${CMAKE_CURRENT_SOURCE_DIR}/linux.h @@ -31,9 +32,9 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/string_utils.h ${CMAKE_CURRENT_SOURCE_DIR}/file.h ${CMAKE_CURRENT_SOURCE_DIR}/shared_ptr_utils.h - ${CMAKE_CURRENT_SOURCE_DIR}/move_wrapper.h ${CMAKE_CURRENT_SOURCE_DIR}/noncopyable.h ${CMAKE_CURRENT_SOURCE_DIR}/singleton.h + ${CMAKE_CURRENT_SOURCE_DIR}/traffic_generator.h ) if(NOT WIN32) diff --git a/libtransport/includes/hicn/transport/utils/array.h b/libtransport/includes/hicn/transport/utils/array.h index 7c0ed65d8..d57bfabaf 100644 --- a/libtransport/includes/hicn/transport/utils/array.h +++ b/libtransport/includes/hicn/transport/utils/array.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/branch_prediction.h b/libtransport/includes/hicn/transport/utils/branch_prediction.h index 8cbfaca76..626cb1573 100644 --- a/libtransport/includes/hicn/transport/utils/branch_prediction.h +++ b/libtransport/includes/hicn/transport/utils/branch_prediction.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h index 8f28e763c..14234eaa1 100644 --- a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h +++ b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -17,11 +17,102 @@ #include <chrono> +namespace std { +namespace chrono { +namespace detail { + +template <typename From, typename To> +struct posix_duration_cast; + +// chrono -> timespec caster +template <typename Rep, typename Period> +struct posix_duration_cast<std::chrono::duration<Rep, Period>, + struct timespec> { + static struct timespec cast(std::chrono::duration<Rep, Period> const &d) { + struct timespec tv; + + std::chrono::seconds const sec = + std::chrono::duration_cast<std::chrono::seconds>(d); + + tv.tv_sec = sec.count(); + tv.tv_nsec = + std::chrono::duration_cast<std::chrono::nanoseconds>(d - sec).count(); + + return tv; + } +}; + +// timespec -> chrono caster +template <typename Rep, typename Period> +struct posix_duration_cast<struct timespec, + std::chrono::duration<Rep, Period>> { + static std::chrono::duration<Rep, Period> cast(struct timespec const &tv) { + return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>( + std::chrono::seconds(tv.tv_sec) + std::chrono::nanoseconds(tv.tv_nsec)); + } +}; + +} // namespace detail + +// chrono -> timespec +template <typename T, typename Rep, typename Period> +auto duration_cast(std::chrono::duration<Rep, Period> const &d) -> + typename std::enable_if<std::is_same<T, struct timespec>::value, + struct timespec>::type { + return detail::posix_duration_cast<std::chrono::duration<Rep, Period>, + timespec>::cast(d); +} + +// timespec -> chrono +template <typename Duration> +Duration duration_cast(struct timespec const &tv) { + return detail::posix_duration_cast<struct timespec, Duration>::cast(tv); +} + +} // namespace chrono +} // namespace std + namespace utils { -using SteadyClock = std::chrono::steady_clock; -using TimePoint = SteadyClock::time_point; -using Milliseconds = std::chrono::milliseconds; -using Microseconds = std::chrono::microseconds; +template <typename T> +class Time { + public: + using Clock = T; + using TimePoint = typename Clock::time_point; + using Rep = uint64_t; + using Seconds = std::chrono::duration<Rep>; + using Milliseconds = std::chrono::duration<Rep, std::milli>; + using Microseconds = std::chrono::duration<Rep, std::micro>; + + static auto now() { return Clock::now(); } + + // From epochs + static auto nowMs() { + return std::chrono::duration_cast<Milliseconds>(now().time_since_epoch()); + } + + // From epoch + static auto nowUs() { + return std::chrono::duration_cast<Microseconds>(now().time_since_epoch()); + } + + template <typename D> + static auto getDuration(const TimePoint &start, const TimePoint &end) { + return std::chrono::duration_cast<D>(end - start); + } + + static auto getDurationS(const TimePoint &start, const TimePoint &end) { + return getDuration<Seconds>(start, end); + } + static auto getDurationMs(const TimePoint &start, const TimePoint &end) { + return getDuration<Milliseconds>(start, end); + } + static auto getDurationUs(const TimePoint &start, const TimePoint &end) { + return getDuration<Microseconds>(start, end); + } +}; + +using SteadyTime = Time<std::chrono::steady_clock>; +using SystemTime = Time<std::chrono::system_clock>; } // namespace utils diff --git a/libtransport/includes/hicn/transport/utils/color.h b/libtransport/includes/hicn/transport/utils/color.h new file mode 100644 index 000000000..3e8d93e14 --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/color.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <array> +#include <ostream> +#include <random> +#include <sstream> + +namespace utils { + +#define foreach_modifier \ + _(RESET, 0) \ + _(BOLD, 1) \ + _(FG_DEFAULT, 39) \ + _(FG_BLACK, 30) \ + _(FG_RED, 31) \ + _(FG_GREEN, 32) \ + _(FG_YELLOW, 33) \ + _(FG_BLUE, 34) \ + _(FG_MAGENTA, 35) \ + _(FG_CYAN, 36) \ + _(FG_LIGHT_GRAY, 37) \ + _(FG_DARK_GRAY, 90) \ + _(FG_LIGHT_RED, 91) \ + _(FG_LIGHT_GREEN, 92) \ + _(FG_LIGHT_YELLOW, 93) \ + _(FG_LIGHT_BLUE, 94) \ + _(FG_LIGHT_MAGENTA, 95) \ + _(FG_LIGHT_CYAN, 96) \ + _(FG_WHITE, 97) \ + _(BG_RED, 41) \ + _(BG_GREEN, 42) \ + _(BG_BLUE, 44) \ + _(BG_DEFAULT, 49) + +class ColorModifier { + static inline const std::size_t n_modifiers = 23; + static inline const char format_string_start[] = "\033["; + static inline const char format_string_end[] = "m"; + + public: + enum class Code { +#define _(name, value) name = value, + foreach_modifier +#undef _ + }; + + static inline std::array<Code, n_modifiers> code_array = { +#define _(name, value) Code::name, + foreach_modifier +#undef _ + }; + + static Code getRandomModifier() { + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::uniform_int_distribution<> distr(4, 17); + + return code_array[distr(gen)]; + } + + ColorModifier(Code code) : code_(code), color_string_() { + std::stringstream ss; + if (std::getenv("COLORTERM") != nullptr) { + ss << format_string_start << static_cast<int>(code_) << format_string_end; + color_string_ = ss.str(); + } + } + + ColorModifier() : ColorModifier(getRandomModifier()) {} + + friend std::ostream& operator<<(std::ostream& os, const ColorModifier& mod) { + return os << mod.color_string_; + } + + private: + Code code_; + std::string color_string_; +}; + +} // namespace utils
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/conversions.h b/libtransport/includes/hicn/transport/utils/conversions.h index 52d3e3168..812803175 100644 --- a/libtransport/includes/hicn/transport/utils/conversions.h +++ b/libtransport/includes/hicn/transport/utils/conversions.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/daemonizator.h b/libtransport/includes/hicn/transport/utils/daemonizator.h index 028d74865..1d0a0b309 100644 --- a/libtransport/includes/hicn/transport/utils/daemonizator.h +++ b/libtransport/includes/hicn/transport/utils/daemonizator.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/event_thread.h b/libtransport/includes/hicn/transport/utils/event_thread.h index 15ec1d62c..164c853a5 100644 --- a/libtransport/includes/hicn/transport/utils/event_thread.h +++ b/libtransport/includes/hicn/transport/utils/event_thread.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -29,16 +29,16 @@ class EventThread { EventThread(asio::io_service& io_service, bool detached = false) : internal_io_service_(nullptr), io_service_(std::ref(io_service)), - work_(std::make_unique<asio::io_service::work>(io_service_)), + work_guard_(asio::make_work_guard(io_service_.get())), thread_(nullptr), detached_(detached) { run(); } - EventThread(bool detached = false) + explicit EventThread(bool detached = false) : internal_io_service_(std::make_unique<asio::io_service>()), io_service_(std::ref(*internal_io_service_)), - work_(std::make_unique<asio::io_service::work>(io_service_)), + work_guard_(asio::make_work_guard(io_service_.get())), thread_(nullptr), detached_(detached) { run(); @@ -47,22 +47,12 @@ class EventThread { EventThread(const EventThread&) = delete; EventThread& operator=(const EventThread&) = delete; - EventThread(EventThread&& other) + EventThread(EventThread&& other) noexcept : internal_io_service_(std::move(other.internal_io_service_)), io_service_(std::move(other.io_service_)), - work_(std::move(other.work_)), + work_guard_(std::move(other.work_guard_)), thread_(std::move(other.thread_)), - detached_(std::move(other.detached_)) {} - - EventThread& operator=(EventThread&& other) { - internal_io_service_ = std::move(other.internal_io_service_); - io_service_ = std::move(other.io_service_); - work_ = std::move(other.work_); - thread_ = std::move(other.thread_); - detached_ = other.detached_; - - return *this; - } + detached_(other.detached_) {} ~EventThread() { stop(); } @@ -89,16 +79,29 @@ class EventThread { template <typename Func> void add(Func&& f) { - io_service_.get().post(std::forward<Func&&>(f)); + io_service_.get().post(std::forward<Func>(f)); } template <typename Func> void tryRunHandlerNow(Func&& f) { - io_service_.get().dispatch(std::forward<Func&&>(f)); + io_service_.get().dispatch(std::forward<Func>(f)); + } + + template <typename Func> + void addAndWaitForExecution(Func&& f) const { + auto promise = std::promise<void>(); + auto future = promise.get_future(); + + asio::dispatch(io_service_.get(), [&promise, f = std::forward<Func>(f)]() { + f(); + promise.set_value(); + }); + + future.wait(); } void stop() { - work_.reset(); + add([this]() { work_guard_.reset(); }); if (thread_ && thread_->joinable()) { thread_->join(); @@ -107,14 +110,14 @@ class EventThread { thread_.reset(); } - bool stopped() { return io_service_.get().stopped(); } + bool stopped() const { return io_service_.get().stopped(); } asio::io_service& getIoService() { return io_service_; } private: std::unique_ptr<asio::io_service> internal_io_service_; std::reference_wrapper<asio::io_service> io_service_; - std::unique_ptr<asio::io_service::work> work_; + asio::executor_work_guard<asio::io_context::executor_type> work_guard_; std::unique_ptr<std::thread> thread_; bool detached_; }; diff --git a/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h b/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h index 298b6f9d1..19b52b37e 100644 --- a/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h +++ b/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h @@ -1,23 +1,28 @@ /* - * Copyright (c) 2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. */ #pragma once #include <hicn/transport/portability/c_portability.h> +#include <hicn/transport/utils/branch_prediction.h> #include <hicn/transport/utils/singleton.h> #include <hicn/transport/utils/spinlock.h> #include <stdint.h> #include <cassert> #include <cstdlib> +#include <list> #include <memory> namespace utils { template <std::size_t SIZE = 512, std::size_t OBJECTS = 4096> class FixedBlockAllocator - : public utils::Singleton<FixedBlockAllocator<SIZE, OBJECTS>> { - friend class utils::Singleton<FixedBlockAllocator<SIZE, OBJECTS>>; + : public utils::ThreadLocalSingleton<FixedBlockAllocator<SIZE, OBJECTS>> { + friend class utils::ThreadLocalSingleton<FixedBlockAllocator<SIZE, OBJECTS>>; + + static inline const std::size_t BLOCK_SIZE = SIZE; + static inline const std::size_t BLOCKS_PER_POOL = OBJECTS; public: ~FixedBlockAllocator() { @@ -26,19 +31,19 @@ class FixedBlockAllocator } } - void* allocateBlock(size_t size = SIZE) { - assert(size <= SIZE); + void* allocateBlock() { uint32_t index; - SpinLock::Acquire locked(lock_); void* p_block = pop(); if (!p_block) { - if (TRANSPORT_EXPECT_FALSE(current_pool_index_ >= max_objects_)) { + if (TRANSPORT_EXPECT_FALSE(current_pool_index_ >= BLOCKS_PER_POOL)) { // Allocate new memory block p_pools_.emplace_front( - new typename std::aligned_storage<SIZE>::type[max_objects_]); + new typename std::aligned_storage<SIZE>::type[BLOCKS_PER_POOL]); // reset current_pool_index_ current_pool_index_ = 0; + // Increase total block count + block_count_ += BLOCKS_PER_POOL; } auto& latest = p_pools_.front(); @@ -59,7 +64,7 @@ class FixedBlockAllocator } public: - std::size_t blockSize() { return block_size_; } + std::size_t blockSize() { return BLOCK_SIZE; } uint32_t blockCount() { return block_count_; } @@ -69,20 +74,32 @@ class FixedBlockAllocator uint32_t deallocations() { return deallocations_; } + void reset() { + p_head_ = nullptr; + blocks_in_use_ = 0; + allocations_ = 0; + deallocations_ = 0; + current_pool_index_ = 0; + block_count_ = BLOCKS_PER_POOL; + + // Delete all memory pools but the first one + for (auto it = std::next(p_pools_.begin()); it != p_pools_.end();) { + delete[] * it; + it = p_pools_.erase(it); + } + } + private: FixedBlockAllocator() - : block_size_(SIZE), - object_size_(SIZE), - max_objects_(OBJECTS), - p_head_(NULL), + : p_head_(NULL), current_pool_index_(0), - block_count_(0), + block_count_(BLOCKS_PER_POOL), blocks_in_use_(0), allocations_(0), deallocations_(0) { static_assert(SIZE >= sizeof(long*), "SIZE must be at least 8 bytes"); p_pools_.emplace_front( - new typename std::aligned_storage<SIZE>::type[max_objects_]); + new typename std::aligned_storage<SIZE>::type[BLOCKS_PER_POOL]); } void push(void* p_memory) { @@ -106,12 +123,6 @@ class FixedBlockAllocator Block* p_next; }; - static std::unique_ptr<FixedBlockAllocator> instance_; - - const std::size_t block_size_; - const std::size_t object_size_; - const std::size_t max_objects_; - Block* p_head_; uint32_t current_pool_index_; std::list<typename std::aligned_storage<SIZE>::type*> p_pools_; @@ -123,10 +134,6 @@ class FixedBlockAllocator SpinLock lock_; }; -template <std::size_t A, std::size_t B> -std::unique_ptr<FixedBlockAllocator<A, B>> - FixedBlockAllocator<A, B>::instance_ = nullptr; - /** * STL Allocator trait to be used with allocate_shared. */ diff --git a/libtransport/includes/hicn/transport/utils/hash.h b/libtransport/includes/hicn/transport/utils/hash.h index 6815ca4bf..7943596e6 100644 --- a/libtransport/includes/hicn/transport/utils/hash.h +++ b/libtransport/includes/hicn/transport/utils/hash.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/libtransport/includes/hicn/transport/utils/linux.h b/libtransport/includes/hicn/transport/utils/linux.h index 4fbf5f01e..14ef179ac 100644 --- a/libtransport/includes/hicn/transport/utils/linux.h +++ b/libtransport/includes/hicn/transport/utils/linux.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -44,7 +44,7 @@ static TRANSPORT_ALWAYS_INLINE int retrieveInterfaceAddress( uint16_t prefix = 0; memcpy(&prefix, tmp->sin6_addr.s6_addr, sizeof(uint16_t)); - if (htons(LINK_LOCAL_PREFIX) != prefix) { + if (portability::host_to_net(LINK_LOCAL_PREFIX) != prefix) { *address = *(struct sockaddr_in6 *)ifa->ifa_addr; getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), addr, sizeof(addr), NULL, 0, NI_NUMERICHOST); diff --git a/libtransport/includes/hicn/transport/utils/literals.h b/libtransport/includes/hicn/transport/utils/literals.h index bd00e0a58..531f67362 100644 --- a/libtransport/includes/hicn/transport/utils/literals.h +++ b/libtransport/includes/hicn/transport/utils/literals.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/log.h b/libtransport/includes/hicn/transport/utils/log.h index 0947b755e..f4d39b6b1 100644 --- a/libtransport/includes/hicn/transport/utils/log.h +++ b/libtransport/includes/hicn/transport/utils/log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/membuf.h b/libtransport/includes/hicn/transport/utils/membuf.h index 6f92c2208..4b442dda3 100644 --- a/libtransport/includes/hicn/transport/utils/membuf.h +++ b/libtransport/includes/hicn/transport/utils/membuf.h @@ -30,6 +30,7 @@ #include <cinttypes> #include <cstddef> #include <cstring> +#include <iostream> #include <iterator> #include <limits> #include <memory> diff --git a/libtransport/includes/hicn/transport/utils/noncopyable.h b/libtransport/includes/hicn/transport/utils/noncopyable.h index 83923e647..0c54d24a3 100644 --- a/libtransport/includes/hicn/transport/utils/noncopyable.h +++ b/libtransport/includes/hicn/transport/utils/noncopyable.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/object_pool.h b/libtransport/includes/hicn/transport/utils/object_pool.h index d9b28e663..63288c655 100644 --- a/libtransport/includes/hicn/transport/utils/object_pool.h +++ b/libtransport/includes/hicn/transport/utils/object_pool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -16,6 +16,7 @@ #pragma once #include <hicn/transport/utils/branch_prediction.h> +#include <hicn/transport/utils/noncopyable.h> #include <hicn/transport/utils/spinlock.h> #include <deque> @@ -25,7 +26,7 @@ namespace utils { template <typename T> -class ObjectPool { +class ObjectPool : private utils::NonCopyable { class ObjectDeleter { public: ObjectDeleter(ObjectPool<T> *pool = nullptr) : pool_(pool) {} @@ -47,8 +48,20 @@ class ObjectPool { ObjectPool() : destructor_(false) {} - // No copies - ObjectPool(const ObjectPool &other) = delete; + ObjectPool(ObjectPool &&other) + : object_pool_lock_(std::move(other.object_pool_lock_)), + object_pool_(std::move(other.object_pool_)), + destructor_(other.destructor_) {} + + ObjectPool &operator=(ObjectPool &&other) { + if (this != &other) { + object_pool_lock_ = std::move(other.object_pool_lock_); + object_pool_ = std::move(other.object_pool_); + destructor_ = other.destructor_; + } + + return *this; + } ~ObjectPool() { destructor_ = true; diff --git a/libtransport/includes/hicn/transport/utils/ring_buffer.h b/libtransport/includes/hicn/transport/utils/ring_buffer.h index 52629b82b..3032032c9 100644 --- a/libtransport/includes/hicn/transport/utils/ring_buffer.h +++ b/libtransport/includes/hicn/transport/utils/ring_buffer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/rtc_quality_score.h b/libtransport/includes/hicn/transport/utils/rtc_quality_score.h new file mode 100644 index 000000000..2e8ca97d3 --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/rtc_quality_score.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +namespace transport { + +namespace protocol { + +namespace rtc { + +class RTCQualityScore { + public: + RTCQualityScore(){}; + ~RTCQualityScore(){}; + + uint8_t getQualityScore(uint64_t RTT, uint32_t loss_rate) { + uint32_t delay_index = getDelay(RTT); + uint32_t loss_index = getLossRate(loss_rate); + return quality_score_[loss_index][delay_index]; + } + + private: + // see quality score map + uint8_t quality_score_[7][6]{{5, 5, 5, 4, 3, 1}, {5, 4, 3, 2, 1, 1}, + {5, 3, 2, 1, 1, 1}, {5, 2, 1, 1, 1, 1}, + {4, 1, 1, 1, 1, 1}, {3, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1}}; + + uint8_t getDelay(uint64_t RTT) { + uint64_t OWD = RTT / 2; + // we should never get a OWD of 0. so we take the first col if OWD is < 5ms + if (OWD < 5) return 0; + if (OWD < 50) return 1; + if (OWD < 100) return 2; + if (OWD < 200) return 3; + if (OWD < 300) return 4; + return 5; + } + + uint8_t getLossRate(uint32_t loss_rate) { + // we use 3% as mean loss rate + if (loss_rate < 3) return 0; + if (loss_rate < 10) return 1; + if (loss_rate < 20) return 2; + if (loss_rate < 30) return 3; + if (loss_rate < 40) return 4; + if (loss_rate < 50) return 5; + return 6; + } +}; + +} // namespace rtc + +} // namespace protocol + +} // namespace transport diff --git a/libtransport/includes/hicn/transport/utils/singleton.h b/libtransport/includes/hicn/transport/utils/singleton.h index 4b7b19c0a..cdd8b03bf 100644 --- a/libtransport/includes/hicn/transport/utils/singleton.h +++ b/libtransport/includes/hicn/transport/utils/singleton.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -32,4 +32,17 @@ class Singleton : NonCopyable { ~Singleton() {} }; +template <typename T> +class ThreadLocalSingleton : NonCopyable { + public: + static T& getInstance() { + static thread_local T instance; + return instance; + } + + protected: + ThreadLocalSingleton() {} + ~ThreadLocalSingleton() {} +}; + } // namespace utils
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/spinlock.h b/libtransport/includes/hicn/transport/utils/spinlock.h index 009a94454..40fc48de3 100644 --- a/libtransport/includes/hicn/transport/utils/spinlock.h +++ b/libtransport/includes/hicn/transport/utils/spinlock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/string_tokenizer.h b/libtransport/includes/hicn/transport/utils/string_tokenizer.h index 36630eb58..2b2b893a0 100644 --- a/libtransport/includes/hicn/transport/utils/string_tokenizer.h +++ b/libtransport/includes/hicn/transport/utils/string_tokenizer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/string_utils.h b/libtransport/includes/hicn/transport/utils/string_utils.h index 313c28cc6..5f9cca13b 100644 --- a/libtransport/includes/hicn/transport/utils/string_utils.h +++ b/libtransport/includes/hicn/transport/utils/string_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: diff --git a/libtransport/includes/hicn/transport/utils/move_wrapper.h b/libtransport/includes/hicn/transport/utils/thread_pool.h index 5dc3b461d..76218ff09 100644 --- a/libtransport/includes/hicn/transport/utils/move_wrapper.h +++ b/libtransport/includes/hicn/transport/utils/thread_pool.h @@ -1,6 +1,5 @@ /* * Copyright (c) 2021 Cisco and/or its affiliates. - * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -16,23 +15,28 @@ #pragma once -#include <type_traits> +#include <hicn/transport/utils/event_thread.h> +#include <hicn/transport/utils/noncopyable.h> + +#include <thread> +#include <vector> namespace utils { -template <typename F> -struct MoveWrapper : F { - MoveWrapper(F&& f) : F(std::move(f)) {} +class ThreadPool : public NonCopyable { + public: + explicit ThreadPool( + std::size_t n_threads = std::thread::hardware_concurrency()) + : workers_(n_threads > 0 ? n_threads : 1) {} + + ~ThreadPool() = default; - MoveWrapper(MoveWrapper&&) = default; - MoveWrapper& operator=(MoveWrapper&&) = default; + std::size_t getNThreads() const { return workers_.size(); } + EventThread &getWorker(std::size_t i) { return workers_.at(i); } + std::vector<EventThread> &getWorkers() { return workers_; } - MoveWrapper(const MoveWrapper&); - MoveWrapper& operator=(const MoveWrapper&); + private: + std::vector<EventThread> workers_; }; -template <typename T> -auto moveHandler(T&& t) -> MoveWrapper<typename std::decay<T>::type> { - return std::move(t); -} } // namespace utils
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/traffic_generator.h b/libtransport/includes/hicn/transport/utils/traffic_generator.h new file mode 100644 index 000000000..abd84886d --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/traffic_generator.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <random> +#include <string> + +namespace transport { + +class TrafficGenerator { + public: + TrafficGenerator(uint32_t count); + virtual ~TrafficGenerator() = default; + bool hasFinished(); + uint32_t getSentCount(); + virtual std::pair<std::string, uint32_t> getPrefixAndSuffix(); + + virtual std::string getPrefix() = 0; + virtual uint32_t getSuffix() = 0; + virtual void reset(); + + protected: + void onSuffixGenerated(); + + uint32_t count_; + uint32_t sent_; +}; + +/* Fixed prefix, incremental suffix */ +class IncrSuffixTrafficGenerator : public TrafficGenerator { + public: + explicit IncrSuffixTrafficGenerator(std::string prefix, uint32_t suffix, + uint32_t count); + std::string getPrefix() override; + uint32_t getSuffix() override; + void reset() override; + + private: + std::string prefix_; + uint32_t suffix_; + uint32_t initial_suffix_; +}; + +/* Random prefix, random suffix */ +class RandomTrafficGenerator : public TrafficGenerator { + public: + static constexpr char NET_PREFIX[] = "2001:db8:1::/64"; + + RandomTrafficGenerator(uint32_t count, std::string net_prefix = NET_PREFIX); + std::string getPrefix() override; + uint32_t getSuffix() override; + + private: + std::string net_prefix_; + std::default_random_engine rand_engine_; + std::uniform_int_distribution<uint32_t> uniform_distribution_; +}; + +} // namespace transport
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/uri.h b/libtransport/includes/hicn/transport/utils/uri.h index 7c28e8552..30675e880 100644 --- a/libtransport/includes/hicn/transport/utils/uri.h +++ b/libtransport/includes/hicn/transport/utils/uri.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: |