├── .copyrightignore ├── .gitignore ├── CMakeLists.txt ├── LICENSE.txt ├── cbcompress ├── CMakeLists.txt ├── compress.cc ├── compress_bench.cc └── compression_test.cc ├── cbcrypto ├── CMakeLists.txt ├── EncryptedFileFormat.md ├── cbcat.cc ├── cbcrypto_test.cc ├── common.cc ├── digest.cc ├── dump_keys_runner.cc ├── encrypted_file_associated_data.h ├── encrypted_file_header.cc ├── file_reader.cc ├── file_utilities.cc ├── file_utilities_test.cc ├── file_writer.cc ├── fileio_test.cc ├── key_store.cc ├── key_store_test.cc ├── random_gen.cc └── symmetric.cc ├── cbsocket ├── CMakeLists.txt ├── cbsocket_test.cc └── socket.cc ├── cgroup ├── CMakeLists.txt ├── cgroup.cc ├── cgroup_private.cc ├── cgroup_private.h ├── cgroup_tests.cc ├── cgroup_trace.cc ├── cgroups.md └── test │ ├── README.md │ ├── v1 │ ├── proc │ │ ├── mounts │ │ └── pressure │ │ │ ├── cpu │ │ │ ├── io │ │ │ └── memory │ └── sys │ │ └── fs │ │ └── cgroup │ │ ├── cpu │ │ ├── cgroup.clone_children │ │ ├── cgroup.procs │ │ ├── cpu.cfs_period_us │ │ ├── cpu.cfs_quota_us │ │ ├── cpu.shares │ │ ├── cpu.stat │ │ ├── cpu.uclamp.max │ │ ├── cpu.uclamp.min │ │ ├── cpuacct.stat │ │ ├── cpuacct.usage │ │ ├── cpuacct.usage_all │ │ ├── cpuacct.usage_percpu │ │ ├── cpuacct.usage_percpu_sys │ │ ├── cpuacct.usage_percpu_user │ │ ├── cpuacct.usage_sys │ │ ├── cpuacct.usage_user │ │ ├── notify_on_release │ │ └── tasks │ │ ├── cpuacct │ │ ├── cgroup.clone_children │ │ ├── cgroup.procs │ │ ├── cpu.cfs_period_us │ │ ├── cpu.cfs_quota_us │ │ ├── cpu.shares │ │ ├── cpu.stat │ │ ├── cpu.uclamp.max │ │ ├── cpu.uclamp.min │ │ ├── cpuacct.stat │ │ ├── cpuacct.usage │ │ ├── cpuacct.usage_all │ │ ├── cpuacct.usage_percpu │ │ ├── cpuacct.usage_percpu_sys │ │ ├── cpuacct.usage_percpu_user │ │ ├── cpuacct.usage_sys │ │ ├── cpuacct.usage_user │ │ ├── notify_on_release │ │ └── tasks │ │ └── memory │ │ ├── cgroup.clone_children │ │ ├── cgroup.procs │ │ ├── memory.failcnt │ │ ├── memory.kmem.failcnt │ │ ├── memory.kmem.limit_in_bytes │ │ ├── memory.kmem.max_usage_in_bytes │ │ ├── memory.kmem.slabinfo │ │ ├── memory.kmem.tcp.failcnt │ │ ├── memory.kmem.tcp.limit_in_bytes │ │ ├── memory.kmem.tcp.max_usage_in_bytes │ │ ├── memory.kmem.tcp.usage_in_bytes │ │ ├── memory.kmem.usage_in_bytes │ │ ├── memory.limit_in_bytes │ │ ├── memory.max_usage_in_bytes │ │ ├── memory.move_charge_at_immigrate │ │ ├── memory.numa_stat │ │ ├── memory.oom_control │ │ ├── memory.soft_limit_in_bytes │ │ ├── memory.stat │ │ ├── memory.swappiness │ │ ├── memory.usage_in_bytes │ │ ├── memory.use_hierarchy │ │ ├── notify_on_release │ │ └── tasks │ └── v2 │ ├── proc │ ├── mounts │ └── pressure │ │ ├── cpu │ │ ├── io │ │ └── memory │ └── sys │ └── fs │ └── cgroup │ ├── cgroup.controllers │ ├── cgroup.events │ ├── cgroup.freeze │ ├── cgroup.max.depth │ ├── cgroup.max.descendants │ ├── cgroup.procs │ ├── cgroup.stat │ ├── cgroup.subtree_control │ ├── cgroup.threads │ ├── cgroup.type │ ├── cpu │ ├── cpu.idle │ ├── cpu.max │ ├── cpu.max.burst │ ├── cpu.pressure │ ├── cpu.stat │ ├── cpu.uclamp.max │ ├── cpu.uclamp.min │ ├── cpu.weight │ ├── cpu.weight.nice │ ├── cpuset.cpus │ ├── cpuset.cpus.effective │ ├── cpuset.cpus.partition │ ├── cpuset.mems │ ├── cpuset.mems.effective │ ├── hugetlb.1GB.current │ ├── hugetlb.1GB.events │ ├── hugetlb.1GB.events.local │ ├── hugetlb.1GB.max │ ├── hugetlb.1GB.rsvd.current │ ├── hugetlb.1GB.rsvd.max │ ├── hugetlb.2MB.current │ ├── hugetlb.2MB.events │ ├── hugetlb.2MB.events.local │ ├── hugetlb.2MB.max │ ├── hugetlb.2MB.rsvd.current │ ├── hugetlb.2MB.rsvd.max │ ├── io │ ├── io.max │ ├── io.pressure │ ├── io.prio.class │ ├── io.stat │ ├── io.weight │ ├── memory │ ├── memory.current │ ├── memory.events │ ├── memory.events.local │ ├── memory.high │ ├── memory.low │ ├── memory.max │ ├── memory.min │ ├── memory.numa_stat │ ├── memory.oom.group │ ├── memory.pressure │ ├── memory.stat │ ├── memory.swap.current │ ├── memory.swap.events │ ├── memory.swap.high │ ├── memory.swap.max │ ├── misc.current │ ├── misc.max │ ├── pids.current │ ├── pids.events │ ├── pids.max │ ├── rdma.current │ └── rdma.max ├── external ├── README.txt └── valgrind │ ├── memcheck.h │ └── valgrind.h ├── hdrhistogram ├── CMakeLists.txt ├── hdrhistogram.cc └── hdrhistogram_test.cc ├── include ├── JSON_checker.h ├── breakpad_wrapper │ └── breakpad_wrapper.h ├── cbcrypto │ ├── common.h │ ├── digest.h │ ├── dump_keys_runner.h │ ├── encrypted_file_header.h │ ├── file_reader.h │ ├── file_utilities.h │ ├── file_writer.h │ ├── key_store.h │ ├── random_gen.h │ └── symmetric.h ├── cgroup │ └── cgroup.h ├── hdrhistogram │ ├── hdrhistogram.h │ └── iterator_range.h ├── platform │ ├── atomic.h │ ├── atomic_duration.h │ ├── awaitable_semaphore.h │ ├── backtrace.h │ ├── base64.h │ ├── bifurcated_counter.h │ ├── bitset.h │ ├── byte_buffer_dump.h │ ├── cb_arena_malloc.h │ ├── cb_arena_malloc_client.h │ ├── cb_malloc.h │ ├── cb_time.h │ ├── cbassert.h │ ├── checked_snprintf.h │ ├── command_line_options_parser.h │ ├── comparators.h │ ├── compress.h │ ├── compression │ │ ├── allocator.h │ │ └── buffer.h │ ├── corestore.h │ ├── crc32c.h │ ├── define_enum_class_bitmask_functions.h │ ├── dirutils.h │ ├── dynamic.in.h │ ├── exceptions.h │ ├── file_sink.h │ ├── getopt.h │ ├── getpass.h │ ├── guarded.h │ ├── histogram.h │ ├── interrupt.h │ ├── je_arena_corelocal_tracker.h │ ├── je_arena_malloc.h │ ├── je_arena_simple_tracker.h │ ├── json_log.h │ ├── json_log_conversions.h │ ├── memory_tracking_allocator.h │ ├── monotonic.h │ ├── monotonic_queue.h │ ├── murmurhash3.h │ ├── n_byte_integer.h │ ├── non_blocking_mutex.h │ ├── non_negative_counter.h │ ├── optional.h │ ├── ordered_map.h │ ├── platform_socket.h │ ├── platform_thread.h │ ├── platform_time.h │ ├── process_monitor.h │ ├── processclock.h │ ├── random.h │ ├── rwlock.h │ ├── scope_timer.h │ ├── semaphore.h │ ├── semaphore_guard.h │ ├── simd │ │ ├── scan.h │ │ ├── scan_neon.h │ │ └── scan_sse42.h │ ├── sized_buffer.h │ ├── socket.h │ ├── split_string.h │ ├── strerror.h │ ├── string_hex.h │ ├── string_utilities.h │ ├── syncobject.h │ ├── sysinfo.h │ ├── system_arena_malloc.h │ ├── terminal_color.h │ ├── terminal_size.h │ ├── thread.h │ ├── thread_local_monotonic_resource.h │ ├── timeutils.h │ ├── unique_waiter_queue.h │ ├── unshared.h │ └── uuid.h ├── relaxed_atomic.h └── win32 │ └── getopt.h ├── licenses ├── APL2.txt ├── BSD-3-Clause-Julian-Seward.txt ├── BSL-Couchbase.txt ├── MIT-JSON.org.txt └── ZLIB-Mark-Adler.txt ├── precompiled_headers.cc ├── precompiled_headers.h ├── src ├── JSON_checker.cc ├── awaitable_semaphore.cc ├── backtrace.cc ├── base64.cc ├── breakpad_wrapper.cc ├── byte_buffer_dump.cc ├── cb_arena_malloc.cc ├── cb_malloc_arena.cc ├── cb_malloc_default.cc ├── cb_pthreads.cc ├── cb_time.cc ├── cb_win32.cc ├── cbassert.cc ├── checked_snprintf.cc ├── command_line_options_parser.cc ├── crc32c.cc ├── crc32c_hw_accel.cc ├── crc32c_private.h ├── dirutils.cc ├── exceptions.cc ├── file_sink.cc ├── getopt.cc ├── getpass.cc ├── global_new_replacement.cc ├── histogram.cc ├── interrupt.cc ├── je_arena_corelocal_tracker.cc ├── je_arena_malloc.cc ├── je_arena_malloc_stats.cc ├── je_arena_simple_tracker.cc ├── je_malloc_conf.cc ├── loadfile.cc ├── murmurhash3.cc ├── process_monitor.cc ├── processclock.cc ├── random.cc ├── save_file.cc ├── semaphore.cc ├── semaphore_guard.cc ├── sized_buffer.cc ├── split_string.cc ├── strerror.cc ├── string_hex.cc ├── string_utilities.cc ├── sysinfo.cc ├── system_arena_malloc.cc ├── terminal_color.cc ├── terminal_size.cc ├── thread.cc ├── thread_local_monotonic_resource.cc ├── timeutils.cc ├── unique_waiter_queue.cc └── uuid.cc ├── tests ├── CMakeLists.txt ├── arena_malloc │ ├── CMakeLists.txt │ ├── arena_malloc_test.cc │ ├── arena_tracking_bench.cc │ └── cb_malloc_default.cc ├── benchmarks │ ├── CMakeLists.txt │ ├── base64_test_bench.cc │ ├── json_checker_bench.cc │ └── json_log_bench.cc ├── crc32 │ ├── CMakeLists.txt │ ├── crc32c_bench.cc │ └── crc32c_test.cc ├── histogram │ ├── CMakeLists.txt │ └── histogram_test.cc ├── interrupt │ ├── CMakeLists.txt │ ├── interrupt_test.cc │ └── interrupt_test.py ├── memory_tracking_test │ ├── CMakeLists.txt │ ├── memory_tracking_allocator_test.cc │ └── memory_tracking_test.cc ├── rwlock │ ├── CMakeLists.txt │ └── rwlock_test.cc └── unit_tests │ ├── CMakeLists.txt │ ├── atomic_duration_test.cc │ ├── backtrace_test.cc │ ├── base64_test.cc │ ├── bifurcated_counter_test.cc │ ├── bitset_test.cc │ ├── cb_getopt_test.cc │ ├── checked_snprintf_test.cc │ ├── command_line_options_parser_test.cc │ ├── corestore_test.cc │ ├── corestore_test.h │ ├── dirutils_test.cc │ ├── dirutils_test_2.cc │ ├── enum_class_bitmask_functions_test.cc │ ├── file_sink_tests.cc │ ├── getopt_test.cc │ ├── guarded_test.cc │ ├── hex_test.cc │ ├── json_checker_test.cc │ ├── json_log_test.cc │ ├── monotonic_test.cc │ ├── n_byte_integer_test.cc │ ├── non_blocking_mutex_test.cc │ ├── non_negative_counter_test.cc │ ├── optional_test.cc │ ├── ordered_map_test.cc │ ├── process_monitor_child.cc │ ├── process_monitor_test.cc │ ├── processclock_test.cc │ ├── random_test.cc │ ├── relaxed_atomic_test.cc │ ├── save_file_test.cc │ ├── scope_timer_test.cc │ ├── semaphore_test.cc │ ├── simd_scan_test.cc │ ├── split_string_test.cc │ ├── static_clock_test.cc │ ├── string_utilities_test.cc │ ├── sysinfo_test.cc │ ├── thread_local_monotonic_resource_test.cc │ ├── thread_test.cc │ ├── timeutils_test.cc │ ├── unshared_test.cc │ └── uuid_test.cc └── tools ├── CMakeLists.txt └── check_pragma_once.cc /.copyrightignore: -------------------------------------------------------------------------------- 1 | include/JSON_checker.h 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Keep the entries sorted to reduce the risk for a merge conflict 2 | .DS_Store 3 | *.[ao] 4 | *.dll 5 | *.dll.embed.manifest 6 | *.dll.embed.manifest.res 7 | *.dll.intermediate.manifest 8 | *.dll.resource.txt 9 | *.dylib 10 | *.so 11 | *.so.* 12 | *.exe 13 | *.exe.embed.manifest 14 | *.exe.embed.manifest.res 15 | *.exe.intermediate.manifest 16 | *.exe.resource.txt 17 | *.exp 18 | *.ilk 19 | *.lib 20 | *.pdb 21 | *~ 22 | /CMakeCache.txt 23 | /CMakeFiles/ 24 | /CTestTestfile.cmake 25 | /Makefile 26 | /Testing/ 27 | /cmake_install.cmake 28 | /install_manifest.txt 29 | /platform-*-test 30 | /*_test 31 | /.cproject 32 | /.project 33 | /.settings/ 34 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Source code in this repository is licensed under various licenses. The 2 | Business Source License 1.1 (BSL) is one such license. Each file indicates in 3 | a section at the beginning of the file the name of the license that applies to 4 | it. All licenses used in this repository can be found in the top-level 5 | licenses directory. 6 | -------------------------------------------------------------------------------- /cbcompress/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(cbcompress STATIC 2 | ${Platform_SOURCE_DIR}/include/platform/compress.h 3 | ${Platform_SOURCE_DIR}/include/platform/compression/allocator.h 4 | ${Platform_SOURCE_DIR}/include/platform/compression/buffer.h 5 | compress.cc) 6 | set_target_properties(cbcompress PROPERTIES POSITION_INDEPENDENT_CODE true) 7 | target_include_directories(cbcompress SYSTEM PRIVATE ${SNAPPY_INCLUDE_DIR}) 8 | target_include_directories(cbcompress INTERFACE ${Platform_SOURCE_DIR}/include) 9 | platform_enable_pch(cbcompress) 10 | target_link_libraries(cbcompress 11 | PUBLIC Folly::headers fmt::fmt 12 | PRIVATE platform ${SNAPPY_LIBRARIES} ZLIB::ZLIB) 13 | 14 | cb_add_test_executable(platform-compression-test 15 | ${Platform_SOURCE_DIR}/include/platform/compress.h 16 | compression_test.cc) 17 | target_link_libraries(platform-compression-test 18 | PRIVATE 19 | cbcompress 20 | GTest::gtest 21 | GTest::gtest_main) 22 | platform_enable_pch(platform-compression-test) 23 | add_test(platform-compression-test platform-compression-test) 24 | 25 | cb_add_test_executable(platform-compression-bench compress_bench.cc) 26 | target_include_directories(platform-compression-bench 27 | SYSTEM PRIVATE 28 | ${SNAPPY_INCLUDE_DIR}) 29 | target_link_libraries(platform-compression-bench 30 | PRIVATE 31 | ${SNAPPY_LIBRARIES} 32 | benchmark::benchmark 33 | GTest::gtest) 34 | -------------------------------------------------------------------------------- /cbcompress/compress_bench.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | // We don't use the cbcompress API here, as it include memory allocation 17 | // in each operation.. 18 | #define START 256 19 | #define END 40960 20 | #define FACTOR 2 21 | 22 | std::array blob; 23 | 24 | static void SnappyCompress(benchmark::State& state) { 25 | const auto size = size_t(state.range(0)); 26 | size_t compressed_length = snappy::MaxCompressedLength(size); 27 | std::unique_ptr temp(new char[compressed_length]); 28 | 29 | while (state.KeepRunning()) { 30 | benchmark::DoNotOptimize(temp.get()); 31 | snappy::RawCompress(blob.data(), size, temp.get(), &compressed_length); 32 | #ifndef WIN32 33 | benchmark::ClobberMemory(); 34 | #endif 35 | } 36 | state.counters["compressed"] = compressed_length; 37 | } 38 | 39 | BENCHMARK(SnappyCompress)->RangeMultiplier(FACTOR)->Range(START, END); 40 | 41 | int main(int argc, char** argv) { 42 | int ii = 0; 43 | for (auto& a : blob) { 44 | a = 'a' + (ii++ % ('z' - 'a')); 45 | } 46 | 47 | ::benchmark::Initialize(&argc, argv); 48 | ::benchmark::RunSpecifiedBenchmarks(); 49 | } 50 | -------------------------------------------------------------------------------- /cbcrypto/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(cbcrypto STATIC 2 | common.cc 3 | digest.cc 4 | dump_keys_runner.cc 5 | encrypted_file_associated_data.h 6 | encrypted_file_header.cc 7 | file_reader.cc 8 | file_utilities.cc 9 | file_writer.cc 10 | key_store.cc 11 | random_gen.cc 12 | symmetric.cc 13 | ${Platform_SOURCE_DIR}/include/cbcrypto/common.h 14 | ${Platform_SOURCE_DIR}/include/cbcrypto/digest.h 15 | ${Platform_SOURCE_DIR}/include/cbcrypto/dump_keys_runner.h 16 | ${Platform_SOURCE_DIR}/include/cbcrypto/encrypted_file_header.h 17 | ${Platform_SOURCE_DIR}/include/cbcrypto/file_reader.h 18 | ${Platform_SOURCE_DIR}/include/cbcrypto/file_utilities.h 19 | ${Platform_SOURCE_DIR}/include/cbcrypto/file_writer.h 20 | ${Platform_SOURCE_DIR}/include/cbcrypto/key_store.h 21 | ${Platform_SOURCE_DIR}/include/cbcrypto/random_gen.h 22 | ${Platform_SOURCE_DIR}/include/cbcrypto/symmetric.h 23 | ) 24 | set_property(TARGET cbcrypto PROPERTY POSITION_INDEPENDENT_CODE 1) 25 | platform_enable_pch(cbcrypto) 26 | target_link_libraries(cbcrypto 27 | PUBLIC 28 | gsl::gsl-lite 29 | nlohmann_json::nlohmann_json 30 | PRIVATE 31 | cbcompress 32 | fmt::fmt 33 | libsodium::libsodium 34 | OpenSSL::SSL 35 | phosphor 36 | platform 37 | ZLIB::ZLIB) 38 | 39 | cb_add_test_executable(cbcrypto_unit_test 40 | cbcrypto_test.cc 41 | file_utilities_test.cc 42 | fileio_test.cc 43 | key_store_test.cc 44 | ) 45 | cb_enable_unity_build(cbcrypto_unit_test) 46 | platform_enable_pch(cbcrypto_unit_test) 47 | target_link_libraries(cbcrypto_unit_test 48 | PRIVATE 49 | cbcrypto 50 | GTest::gtest 51 | GTest::gtest_main 52 | platform) 53 | add_test(cbcrypto_unit_test cbcrypto_unit_test) 54 | 55 | add_executable(cbcat cbcat.cc) 56 | target_compile_definitions(cbcat 57 | PRIVATE DESTINATION_ROOT="${CMAKE_INSTALL_PREFIX}" 58 | PRODUCT_VERSION="${PRODUCT_VERSION}") 59 | target_link_libraries(cbcat 60 | PRIVATE 61 | cbcrypto 62 | platform 63 | ${Boost_LIBRARIES}) 64 | install(TARGETS cbcat RUNTIME DESTINATION bin) 65 | -------------------------------------------------------------------------------- /cbcrypto/encrypted_file_associated_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb::crypto { 17 | 18 | class EncryptedFileAssociatedData { 19 | public: 20 | EncryptedFileAssociatedData(EncryptedFileHeader header) : header(header) { 21 | } 22 | void set_offset(uint64_t value) { 23 | offset = htonll(value); 24 | } 25 | 26 | /// Convenience function to convert to string_view 27 | [[nodiscard]] operator std::string_view() const { 28 | return {reinterpret_cast(this), sizeof(*this)}; 29 | } 30 | 31 | protected: 32 | #pragma pack(1) 33 | EncryptedFileHeader header; 34 | uint64_t offset{0}; 35 | #pragma pack() 36 | }; 37 | 38 | static_assert(sizeof(EncryptedFileAssociatedData) == 88, 39 | "Incorrect compiler padding"); 40 | 41 | } // namespace cb::crypto 42 | -------------------------------------------------------------------------------- /cbcrypto/encrypted_file_header.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | namespace cb::crypto { 15 | 16 | std::string format_as(Compression compression) { 17 | switch (compression) { 18 | case Compression::None: 19 | return "None"; 20 | case Compression::Snappy: 21 | return "Snappy"; 22 | case Compression::ZLIB: 23 | return "ZLIB"; 24 | case Compression::GZIP: 25 | return "GZIP"; 26 | case Compression::ZSTD: 27 | return "ZSTD"; 28 | case Compression::BZIP2: 29 | return "BZIP2"; 30 | } 31 | return fmt::format("Unknown-{}", static_cast(compression)); 32 | } 33 | 34 | EncryptedFileHeader::EncryptedFileHeader(std::string_view key_id, 35 | cb::uuid::uuid_t salt_, 36 | Compression compression) 37 | : compression(static_cast(compression)), 38 | id_size(gsl::narrow_cast(key_id.size())) { 39 | if (key_id.size() > id.size()) { 40 | throw std::invalid_argument("FileHeader::FileHeader(): key too long"); 41 | } 42 | 43 | std::copy(Magic.begin(), Magic.end(), magic.begin()); 44 | std::copy(key_id.begin(), key_id.end(), id.begin()); 45 | std::copy(salt_.data, salt_.data + salt_.size(), salt.begin()); 46 | } 47 | 48 | bool EncryptedFileHeader::is_encrypted() const { 49 | const auto header = std::string_view{magic.data(), magic.size()}; 50 | return header == Magic; 51 | } 52 | 53 | bool EncryptedFileHeader::is_supported() const { 54 | return is_encrypted() && version == 0; 55 | } 56 | 57 | Compression EncryptedFileHeader::get_compression() const { 58 | return static_cast(compression); 59 | } 60 | 61 | std::string_view EncryptedFileHeader::get_id() const { 62 | return std::string_view{id.data(), id_size}; 63 | } 64 | 65 | cb::uuid::uuid_t EncryptedFileHeader::get_salt() const { 66 | cb::uuid::uuid_t ret; 67 | std::copy(salt.begin(), salt.end(), ret.data); 68 | return ret; 69 | } 70 | 71 | } // namespace cb::crypto 72 | -------------------------------------------------------------------------------- /cbsocket/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories(AFTER ${LIBEVENT_INCLUDE_DIR}) 2 | add_library(cbsocket STATIC 3 | ${Platform_SOURCE_DIR}/include/platform/socket.h 4 | socket.cc) 5 | 6 | target_link_libraries(cbsocket 7 | PRIVATE 8 | gsl::gsl-lite 9 | platform 10 | nlohmann_json::nlohmann_json 11 | ${LIBEVENT_LIBRARIES} 12 | ${COUCHBASE_NETWORK_LIBS}) 13 | target_link_libraries(cbsocket PUBLIC fmt::fmt) 14 | platform_enable_pch(cbsocket) 15 | 16 | cb_add_test_executable(cbsocket_test cbsocket_test.cc) 17 | target_include_directories(cbsocket_test PRIVATE ${Platform_SOURCE_DIR}/include) 18 | target_link_libraries(cbsocket_test 19 | PRIVATE 20 | Folly::headers 21 | nlohmann_json::nlohmann_json 22 | cbsocket 23 | GTest::gtest GTest::gtest_main) 24 | platform_enable_pch(cbsocket_test) 25 | add_test(cbsocket_test cbsocket_test) 26 | -------------------------------------------------------------------------------- /cbsocket/cbsocket_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | TEST(CbSocket, getIpAddresses_all) { 15 | auto [ipv4, ipv6] = cb::net::getIpAddresses(false); 16 | if (!ipv4.empty()) { 17 | EXPECT_NE(ipv4.end(), std::find(ipv4.begin(), ipv4.end(), "127.0.0.1")); 18 | } 19 | if (!ipv6.empty()) { 20 | EXPECT_NE(ipv6.end(), std::find(ipv6.begin(), ipv6.end(), "::1")); 21 | } 22 | } 23 | 24 | TEST(CbSocket, getIpAddresses_skipLocal) { 25 | auto [ipv4, ipv6] = cb::net::getIpAddresses(true); 26 | if (!ipv4.empty()) { 27 | EXPECT_EQ(ipv4.end(), std::find(ipv4.begin(), ipv4.end(), "127.0.0.1")); 28 | } 29 | if (!ipv6.empty()) { 30 | EXPECT_EQ(ipv6.end(), std::find(ipv6.begin(), ipv6.end(), "::1")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cgroup/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(cgroup_objs OBJECT 2 | cgroup_private.cc 3 | cgroup.cc 4 | ${Platform_SOURCE_DIR}/include/cgroup/cgroup.h 5 | cgroup_private.h) 6 | target_include_directories(cgroup_objs SYSTEM PUBLIC 7 | ${Platform_SOURCE_DIR}/include) 8 | target_link_libraries(cgroup_objs PRIVATE fmt::fmt) 9 | target_link_libraries(cgroup_objs PUBLIC nlohmann_json::nlohmann_json) 10 | set_property(TARGET cgroup_objs PROPERTY POSITION_INDEPENDENT_CODE 1) 11 | cb_enable_unity_build(cgroup_objs) 12 | 13 | add_library(cgroup STATIC 14 | ${Platform_SOURCE_DIR}/src/loadfile.cc 15 | ${Platform_SOURCE_DIR}/src/split_string.cc 16 | $) 17 | target_include_directories(cgroup SYSTEM PUBLIC ${Platform_SOURCE_DIR}/include) 18 | target_link_libraries(cgroup PRIVATE fmt::fmt Folly::folly) 19 | target_link_libraries(cgroup PUBLIC nlohmann_json::nlohmann_json) 20 | cb_enable_unity_build(cgroup) 21 | set_property(TARGET cgroup PROPERTY POSITION_INDEPENDENT_CODE 1) 22 | 23 | cb_add_test_executable(cgroup_test cgroup_tests.cc) 24 | target_compile_definitions(cgroup_test PRIVATE SOURCE_ROOT="${CMAKE_CURRENT_SOURCE_DIR}") 25 | target_link_libraries(cgroup_test 26 | platform 27 | Folly::folly 28 | GTest::gtest 29 | GTest::gtest_main) 30 | add_test(cgroup_test cgroup_test) 31 | add_executable(cgroup_trace cgroup_trace.cc) 32 | target_link_libraries(cgroup_trace platform) 33 | -------------------------------------------------------------------------------- /cgroup/cgroup_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace cb::cgroup { 19 | class ControlGroup; 20 | namespace priv { 21 | // Allow the root of the filesystem to be passed in to allow for unit 22 | // tests 23 | std::unique_ptr make_control_group(std::string root = "", 24 | pid_t pid = getpid()); 25 | 26 | void setTraceCallback(std::function cb); 27 | } // namespace priv 28 | } // namespace cb::cgroup 29 | -------------------------------------------------------------------------------- /cgroup/cgroup_trace.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | int main() { 16 | cb::cgroup::ControlGroup::setTraceCallback( 17 | [](auto msg) { std::cout << msg << std::endl; }); 18 | try { 19 | auto& instance = cb::cgroup::ControlGroup::instance(); 20 | const auto stats = instance.get_cpu_stats(); 21 | nlohmann::json json{{"num_cpu_prc", instance.get_available_cpu()}, 22 | {"memory_max", instance.get_max_memory()}, 23 | {"memory_current", instance.get_current_memory()}, 24 | {"usage_usec", stats.usage.count()}, 25 | {"user_usec", stats.user.count()}, 26 | {"system_usec", stats.system.count()}, 27 | {"nr_periods", stats.nr_periods}, 28 | {"nr_throttled", stats.nr_throttled}, 29 | {"throttled_usec", stats.throttled.count()}, 30 | {"nr_bursts", stats.nr_bursts}, 31 | {"burst_usec", stats.burst.count()}}; 32 | 33 | std::cout << std::endl 34 | << std::endl 35 | << "Got the following information: " << std::endl 36 | << json.dump(2) << std::endl; 37 | } catch (const std::exception& exception) { 38 | std::cerr << "ERROR: " << exception.what() << std::endl; 39 | return EXIT_FAILURE; 40 | } 41 | return EXIT_SUCCESS; 42 | } 43 | -------------------------------------------------------------------------------- /cgroup/cgroups.md: -------------------------------------------------------------------------------- 1 | # libcgroups 2 | 3 | libcgroups offers a minimalistic implementation to access information 4 | for Linux Resource Control Group V1 and V2. 5 | 6 | ## Limitations 7 | 8 | V1 and V2 differs in the information they provide, so the library 9 | (currently) won't provide any information which isn't available in 10 | both underlying implementations. This may however change in the 11 | future if we decide to drop support for V1 as distributions migrate 12 | over to V2. 13 | 14 | The cgroup for the current process gets determined during _startup_ 15 | and won't change for the lifetime of the process. That was a design 16 | decision and could "easily" be modified (by ripping out the 17 | singleton) logic. The reason for this is that in a typical deployment 18 | the cgroup doesn't change so we wouldn't have to locate the current 19 | cgroup every time. I would call it a design flaw in Linux that 20 | `/proc/self/cgroups` is a file and not a directory where one could 21 | have found the actual files; Instead one _could_ parse that file 22 | and do another mapping with the information from `/proc/mounts` 23 | and `/proc/cgroups`. Another "flaw" in the current design is 24 | the content of `/proc/self/cgroup`. It reports the path relative 25 | to the resource root for the current group. The problem arise 26 | when you try to do this from within a docker container which 27 | did an overlay mount of `/sys/fs/cgroup` so that the path in 28 | `/proc/self/cgroups` can't be accessed from the process itself. 29 | 30 | ## How it works 31 | 32 | During initialization the library try to determine the current 33 | mount points for the cgroups by reading `/proc/mounts`. With 34 | that information in place all the various mount points are 35 | traversed and we try to look for the current pid in the 36 | `cgroup.procs`. If the current pid is located in the file 37 | the process lives in that control group. In V1 we need 38 | to scan all of the mount points as different controllers 39 | may be mounted at different locations. For V2 this is the 40 | cgroup the process belongs to. 41 | 42 | ## Resources 43 | 44 | * https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cgroups.html 45 | * https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html 46 | * https://www.kernel.org/doc/html/latest/scheduler/sched-bwc.html 47 | -------------------------------------------------------------------------------- /cgroup/test/README.md: -------------------------------------------------------------------------------- 1 | # Test files used by the unit tests 2 | 3 | To make the life easier for unit testing this directory 4 | contains files collected from various deployments. 5 | 6 | We don't use all the files (yet?), but instead of just adding 7 | the ones we currently use I decided to add all of them to make 8 | the life easier _when_ we're adding support for additional 9 | features without having to spin up a V1 and V2 instance and collect 10 | the new files. 11 | 12 | ## V1 13 | 14 | The `proc/mounts` file was collected from Docker Desktop on Windows 15 | as part of MB-52817. The rest of the files comes from Ubuntu 20.04 LTS 16 | where both `cpuacct` and `cpu` are symbolic links to a common directory. 17 | I didn't want to mess up the git repo by using symbolic links (nor 18 | did I want to add special hacks to the parsing) so I ended up copying 19 | the same files into both directories. 20 | 21 | ## V2 22 | 23 | The directory v2 contains files collected on a docker instance 24 | running on Ubuntu 22.04 LTS. 25 | -------------------------------------------------------------------------------- /cgroup/test/v1/proc/mounts: -------------------------------------------------------------------------------- 1 | # mount file from Docker desktop on Windows 2 | tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,relatime,mode=755 0 0 3 | cpuset /sys/fs/cgroup/cpuset cgroup ro,nosuid,nodev,noexec,relatime,cpuset 0 0 4 | cpu /sys/fs/cgroup/cpu cgroup ro,nosuid,nodev,noexec,relatime,cpu 0 0 5 | cpuacct /sys/fs/cgroup/cpuacct cgroup ro,nosuid,nodev,noexec,relatime,cpuacct 0 0 6 | blkio /sys/fs/cgroup/blkio cgroup ro,nosuid,nodev,noexec,relatime,blkio 0 0 7 | memory /sys/fs/cgroup/memory cgroup ro,nosuid,nodev,noexec,relatime,memory 0 0 8 | devices /sys/fs/cgroup/devices cgroup ro,nosuid,nodev,noexec,relatime,devices 0 0 9 | freezer /sys/fs/cgroup/freezer cgroup ro,nosuid,nodev,noexec,relatime,freezer 0 0 10 | net_cls /sys/fs/cgroup/net_cls cgroup ro,nosuid,nodev,noexec,relatime,net_cls 0 0 11 | perf_event /sys/fs/cgroup/perf_event cgroup ro,nosuid,nodev,noexec,relatime,perf_event 0 0 12 | net_prio /sys/fs/cgroup/net_prio cgroup ro,nosuid,nodev,noexec,relatime,net_prio 0 0 13 | hugetlb /sys/fs/cgroup/hugetlb cgroup ro,nosuid,nodev,noexec,relatime,hugetlb 0 0 14 | pids /sys/fs/cgroup/pids cgroup ro,nosuid,nodev,noexec,relatime,pids 0 0 15 | rdma /sys/fs/cgroup/rdma cgroup ro,nosuid,nodev,noexec,relatime,rdma 0 0 16 | cgroup /sys/fs/cgroup/systemd cgroup ro,nosuid,nodev,noexec,relatime,name=system d 0 0 17 | cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0 18 | -------------------------------------------------------------------------------- /cgroup/test/v1/proc/pressure/cpu: -------------------------------------------------------------------------------- 1 | some avg10=78.29 avg60=75.76 avg300=66.71 total=733785593 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=0 3 | -------------------------------------------------------------------------------- /cgroup/test/v1/proc/pressure/io: -------------------------------------------------------------------------------- 1 | some avg10=0.01 avg60=0.03 avg300=0.00 total=6691960 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=4176792 3 | -------------------------------------------------------------------------------- /cgroup/test/v1/proc/pressure/memory: -------------------------------------------------------------------------------- 1 | some avg10=0.00 avg60=0.04 avg300=0.08 total=855273 2 | full avg10=0.00 avg60=0.02 avg300=0.04 total=527201 3 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cgroup.clone_children: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cgroup.procs: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.cfs_period_us: -------------------------------------------------------------------------------- 1 | 100000 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.cfs_quota_us: -------------------------------------------------------------------------------- 1 | 250000 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.shares: -------------------------------------------------------------------------------- 1 | 1024 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.stat: -------------------------------------------------------------------------------- 1 | nr_periods 13498 2 | nr_throttled 12651 3 | throttled_time 6807531343393 4 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.uclamp.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpu.uclamp.min: -------------------------------------------------------------------------------- 1 | 0.00 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.stat: -------------------------------------------------------------------------------- 1 | user 316870 2 | system 20595 3 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage: -------------------------------------------------------------------------------- 1 | 3321492315750 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_all: -------------------------------------------------------------------------------- 1 | cpu user system 2 | 0 415329996354 0 3 | 1 405315678051 0 4 | 2 421136278488 0 5 | 3 427106896324 0 6 | 4 410565373809 0 7 | 5 415216846869 0 8 | 6 400283863716 0 9 | 7 426538096343 0 10 | 8 0 0 11 | 9 0 0 12 | 10 0 0 13 | 11 0 0 14 | 12 0 0 15 | 13 0 0 16 | 14 0 0 17 | 15 0 0 18 | 16 0 0 19 | 17 0 0 20 | 18 0 0 21 | 19 0 0 22 | 20 0 0 23 | 21 0 0 24 | 22 0 0 25 | 23 0 0 26 | 24 0 0 27 | 25 0 0 28 | 26 0 0 29 | 27 0 0 30 | 28 0 0 31 | 29 0 0 32 | 30 0 0 33 | 31 0 0 34 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_percpu: -------------------------------------------------------------------------------- 1 | 415329996354 405315678051 421136278488 427107824153 410565651999 415216846869 400283863716 426538096343 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_percpu_sys: -------------------------------------------------------------------------------- 1 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_percpu_user: -------------------------------------------------------------------------------- 1 | 415334085075 405316935792 421136278488 427107824153 410566750326 415220856122 400284407493 426538096343 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_sys: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/cpuacct.usage_user: -------------------------------------------------------------------------------- 1 | 3321507582547 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/notify_on_release: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpu/tasks: -------------------------------------------------------------------------------- 1 | 1 2 | 6 3 | 15 4 | 33 5 | 97 6 | 6790 7 | 6798 8 | 6807 9 | 6813 10 | 6814 11 | 6815 12 | 6816 13 | 6892 14 | 6914 15 | 6915 16 | 6921 17 | 6922 18 | 6923 19 | 6967 20 | 6975 21 | 6980 22 | 6983 23 | 6987 24 | 6990 25 | 6991 26 | 6993 27 | 7004 28 | 7025 29 | 7501 30 | 7503 31 | 8807 32 | 9985 33 | 10165 34 | 10166 35 | 10167 36 | 10168 37 | 10169 38 | 10170 39 | 10366 40 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cgroup.clone_children: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cgroup.procs: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.cfs_period_us: -------------------------------------------------------------------------------- 1 | 100000 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.cfs_quota_us: -------------------------------------------------------------------------------- 1 | 250000 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.shares: -------------------------------------------------------------------------------- 1 | 1024 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.stat: -------------------------------------------------------------------------------- 1 | nr_periods 13498 2 | nr_throttled 12651 3 | throttled_time 6807531343393 4 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.uclamp.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpu.uclamp.min: -------------------------------------------------------------------------------- 1 | 0.00 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.stat: -------------------------------------------------------------------------------- 1 | user 316870 2 | system 20595 3 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage: -------------------------------------------------------------------------------- 1 | 3321492315750 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_all: -------------------------------------------------------------------------------- 1 | cpu user system 2 | 0 415329996354 0 3 | 1 405315678051 0 4 | 2 421136278488 0 5 | 3 427106896324 0 6 | 4 410565373809 0 7 | 5 415216846869 0 8 | 6 400283863716 0 9 | 7 426538096343 0 10 | 8 0 0 11 | 9 0 0 12 | 10 0 0 13 | 11 0 0 14 | 12 0 0 15 | 13 0 0 16 | 14 0 0 17 | 15 0 0 18 | 16 0 0 19 | 17 0 0 20 | 18 0 0 21 | 19 0 0 22 | 20 0 0 23 | 21 0 0 24 | 22 0 0 25 | 23 0 0 26 | 24 0 0 27 | 25 0 0 28 | 26 0 0 29 | 27 0 0 30 | 28 0 0 31 | 29 0 0 32 | 30 0 0 33 | 31 0 0 34 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_percpu: -------------------------------------------------------------------------------- 1 | 415329996354 405315678051 421136278488 427107824153 410565651999 415216846869 400283863716 426538096343 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_percpu_sys: -------------------------------------------------------------------------------- 1 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_percpu_user: -------------------------------------------------------------------------------- 1 | 415334085075 405316935792 421136278488 427107824153 410566750326 415220856122 400284407493 426538096343 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_sys: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/cpuacct.usage_user: -------------------------------------------------------------------------------- 1 | 3321507582547 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/notify_on_release: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/cpuacct/tasks: -------------------------------------------------------------------------------- 1 | 1 2 | 6 3 | 15 4 | 33 5 | 97 6 | 6790 7 | 6798 8 | 6807 9 | 6813 10 | 6814 11 | 6815 12 | 6816 13 | 6892 14 | 6914 15 | 6915 16 | 6921 17 | 6922 18 | 6923 19 | 6967 20 | 6975 21 | 6980 22 | 6983 23 | 6987 24 | 6990 25 | 6991 26 | 6993 27 | 7004 28 | 7025 29 | 7501 30 | 7503 31 | 8807 32 | 9985 33 | 10165 34 | 10166 35 | 10167 36 | 10168 37 | 10169 38 | 10170 39 | 10366 40 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/cgroup.clone_children: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/cgroup.procs: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.failcnt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.failcnt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes: -------------------------------------------------------------------------------- 1 | 9223372036854771712 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.max_usage_in_bytes: -------------------------------------------------------------------------------- 1 | 51744768 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.tcp.failcnt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.tcp.limit_in_bytes: -------------------------------------------------------------------------------- 1 | 9223372036854771712 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.tcp.max_usage_in_bytes: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.tcp.usage_in_bytes: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.kmem.usage_in_bytes: -------------------------------------------------------------------------------- 1 | 44257280 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.limit_in_bytes: -------------------------------------------------------------------------------- 1 | 17179869184 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.max_usage_in_bytes: -------------------------------------------------------------------------------- 1 | 12191121408 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.move_charge_at_immigrate: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.numa_stat: -------------------------------------------------------------------------------- 1 | total=1661649 N0=1661862 2 | file=1450944 N0=1451180 3 | anon=210705 N0=210682 4 | unevictable=0 N0=0 5 | hierarchical_total=1661649 N0=1661862 6 | hierarchical_file=1450944 N0=1451180 7 | hierarchical_anon=210705 N0=210682 8 | hierarchical_unevictable=0 N0=0 9 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.oom_control: -------------------------------------------------------------------------------- 1 | oom_kill_disable 0 2 | under_oom 0 3 | oom_kill 0 4 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.soft_limit_in_bytes: -------------------------------------------------------------------------------- 1 | 9223372036854771712 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.stat: -------------------------------------------------------------------------------- 1 | cache 5943459840 2 | rss 863055872 3 | rss_huge 0 4 | shmem 0 5 | mapped_file 0 6 | dirty 405504 7 | writeback 0 8 | pgpgin 25787784 9 | pgpgout 24125905 10 | pgfault 25444122 11 | pgmajfault 33 12 | inactive_anon 0 13 | active_anon 862777344 14 | inactive_file 4192505856 15 | active_file 1750560768 16 | unevictable 0 17 | hierarchical_memory_limit 17179869184 18 | total_cache 5943459840 19 | total_rss 863055872 20 | total_rss_huge 0 21 | total_shmem 0 22 | total_mapped_file 0 23 | total_dirty 405504 24 | total_writeback 0 25 | total_pgpgin 25787784 26 | total_pgpgout 24125905 27 | total_pgfault 25444122 28 | total_pgmajfault 33 29 | total_inactive_anon 0 30 | total_active_anon 862777344 31 | total_inactive_file 4192505856 32 | total_active_file 1750560768 33 | total_unevictable 0 34 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.swappiness: -------------------------------------------------------------------------------- 1 | 60 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.usage_in_bytes: -------------------------------------------------------------------------------- 1 | 6852075520 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/memory.use_hierarchy: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/notify_on_release: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v1/sys/fs/cgroup/memory/tasks: -------------------------------------------------------------------------------- 1 | 1 2 | 6 3 | 15 4 | 33 5 | 97 6 | 6790 7 | 6798 8 | 6807 9 | 6813 10 | 6814 11 | 6815 12 | 6816 13 | 6892 14 | 6914 15 | 6915 16 | 6921 17 | 6922 18 | 6923 19 | 6967 20 | 6975 21 | 6980 22 | 6983 23 | 6987 24 | 6990 25 | 6991 26 | 6993 27 | 7004 28 | 7025 29 | 7501 30 | 7503 31 | 8807 32 | 9985 33 | 10165 34 | 10166 35 | 10167 36 | 10168 37 | 10169 38 | 10170 39 | 10314 40 | -------------------------------------------------------------------------------- /cgroup/test/v2/proc/pressure/cpu: -------------------------------------------------------------------------------- 1 | some avg10=78.29 avg60=75.76 avg300=66.71 total=733785593 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=0 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/proc/pressure/io: -------------------------------------------------------------------------------- 1 | some avg10=0.01 avg60=0.03 avg300=0.00 total=6691960 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=4176792 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/proc/pressure/memory: -------------------------------------------------------------------------------- 1 | some avg10=0.00 avg60=0.04 avg300=0.08 total=855273 2 | full avg10=0.00 avg60=0.02 avg300=0.04 total=527201 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.controllers: -------------------------------------------------------------------------------- 1 | cpuset cpu io memory hugetlb pids rdma misc 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.events: -------------------------------------------------------------------------------- 1 | populated 1 2 | frozen 0 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.freeze: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.max.depth: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.max.descendants: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.procs: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.stat: -------------------------------------------------------------------------------- 1 | nr_descendants 0 2 | nr_dying_descendants 0 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.subtree_control: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/cgroup.subtree_control -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.threads: -------------------------------------------------------------------------------- 1 | 1 2 | 7 3 | 22 4 | 781 5 | 2019 6 | 2488 7 | 2489 8 | 2492 9 | 2493 10 | 2495 11 | 2496 12 | 2499 13 | 2500 14 | 2511 15 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cgroup.type: -------------------------------------------------------------------------------- 1 | domain 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu: -------------------------------------------------------------------------------- 1 | some avg10=78.29 avg60=75.76 avg300=66.71 total=733785593 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=0 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.idle: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.max: -------------------------------------------------------------------------------- 1 | 250000 100000 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.max.burst: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.pressure: -------------------------------------------------------------------------------- 1 | some avg10=65.95 avg60=69.61 avg300=60.79 total=576908731 2 | full avg10=32.86 avg60=32.71 avg300=30.53 total=338250181 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.stat: -------------------------------------------------------------------------------- 1 | usage_usec 528946079 2 | user_usec 486652575 3 | system_usec 42293504 4 | nr_periods 2914 5 | nr_throttled 222 6 | throttled_usec 2261444 7 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.uclamp.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.uclamp.min: -------------------------------------------------------------------------------- 1 | 0.00 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.weight: -------------------------------------------------------------------------------- 1 | 100 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpu.weight.nice: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpuset.cpus: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpuset.cpus.effective: -------------------------------------------------------------------------------- 1 | 0-7 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpuset.cpus.partition: -------------------------------------------------------------------------------- 1 | member 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpuset.mems: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/cpuset.mems.effective: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.current: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.events: -------------------------------------------------------------------------------- 1 | max 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.events.local: -------------------------------------------------------------------------------- 1 | max 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.rsvd.current: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.1GB.rsvd.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.current: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.events: -------------------------------------------------------------------------------- 1 | max 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.events.local: -------------------------------------------------------------------------------- 1 | max 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.rsvd.current: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/hugetlb.2MB.rsvd.max: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io: -------------------------------------------------------------------------------- 1 | some avg10=0.01 avg60=0.03 avg300=0.00 total=6691960 2 | full avg10=0.00 avg60=0.00 avg300=0.00 total=4176792 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io.max: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/io.max -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io.pressure: -------------------------------------------------------------------------------- 1 | some avg10=0.60 avg60=1.30 avg300=0.76 total=4120174 2 | full avg10=0.60 avg60=1.28 avg300=0.75 total=4106664 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io.prio.class: -------------------------------------------------------------------------------- 1 | no-change 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io.stat: -------------------------------------------------------------------------------- 1 | 8:32 rbytes=40824832 wbytes=824999936 rios=2703 wios=1073 dbytes=0 dios=0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/io.weight: -------------------------------------------------------------------------------- 1 | default 100 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory: -------------------------------------------------------------------------------- 1 | some avg10=0.00 avg60=0.04 avg300=0.08 total=855273 2 | full avg10=0.00 avg60=0.02 avg300=0.04 total=527201 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.current: -------------------------------------------------------------------------------- 1 | 2766684160 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.events: -------------------------------------------------------------------------------- 1 | low 0 2 | high 0 3 | max 0 4 | oom 0 5 | oom_kill 0 6 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.events.local: -------------------------------------------------------------------------------- 1 | low 0 2 | high 0 3 | max 0 4 | oom 0 5 | oom_kill 0 6 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.high: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.low: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.max: -------------------------------------------------------------------------------- 1 | 8589934592 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.min: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.numa_stat: -------------------------------------------------------------------------------- 1 | anon N0=2147504128 2 | file N0=590389248 3 | kernel_stack N0=196608 4 | pagetables N0=5373952 5 | shmem N0=0 6 | file_mapped N0=79003648 7 | file_dirty N0=5771264 8 | file_writeback N0=0 9 | swapcached N0=0 10 | anon_thp N0=0 11 | file_thp N0=0 12 | shmem_thp N0=0 13 | inactive_anon N0=2147352576 14 | active_anon N0=53248 15 | inactive_file N0=42106880 16 | active_file N0=548282368 17 | unevictable N0=0 18 | slab_reclaimable N0=21523352 19 | slab_unreclaimable N0=1011552 20 | workingset_refault_anon N0=0 21 | workingset_refault_file N0=1855 22 | workingset_activate_anon N0=0 23 | workingset_activate_file N0=252 24 | workingset_restore_anon N0=0 25 | workingset_restore_file N0=252 26 | workingset_nodereclaim N0=0 27 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.oom.group: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.pressure: -------------------------------------------------------------------------------- 1 | some avg10=1.05 avg60=0.29 avg300=0.06 total=446327 2 | full avg10=0.36 avg60=0.11 avg300=0.02 total=308567 3 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.stat: -------------------------------------------------------------------------------- 1 | anon 2147508224 2 | file 590389248 3 | kernel_stack 196608 4 | pagetables 5378048 5 | percpu 864 6 | sock 0 7 | shmem 0 8 | file_mapped 79003648 9 | file_dirty 5771264 10 | file_writeback 0 11 | swapcached 0 12 | anon_thp 0 13 | file_thp 0 14 | shmem_thp 0 15 | inactive_anon 2147323904 16 | active_anon 53248 17 | inactive_file 42106880 18 | active_file 548282368 19 | unevictable 0 20 | slab_reclaimable 21523056 21 | slab_unreclaimable 1047096 22 | slab 22570152 23 | workingset_refault_anon 0 24 | workingset_refault_file 1855 25 | workingset_activate_anon 0 26 | workingset_activate_file 252 27 | workingset_restore_anon 0 28 | workingset_restore_file 252 29 | workingset_nodereclaim 0 30 | pgfault 12266523 31 | pgmajfault 140013 32 | pgrefill 1075 33 | pgscan 7057 34 | pgsteal 7057 35 | pgactivate 147117 36 | pgdeactivate 1075 37 | pglazyfree 0 38 | pglazyfreed 0 39 | thp_fault_alloc 0 40 | thp_collapse_alloc 0 41 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.swap.current: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.swap.events: -------------------------------------------------------------------------------- 1 | high 0 2 | max 0 3 | fail 0 4 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.swap.high: -------------------------------------------------------------------------------- 1 | max 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/memory.swap.max: -------------------------------------------------------------------------------- 1 | 8589934592 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/misc.current: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/misc.current -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/misc.max: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/misc.max -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/pids.current: -------------------------------------------------------------------------------- 1 | 14 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/pids.events: -------------------------------------------------------------------------------- 1 | max 0 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/pids.max: -------------------------------------------------------------------------------- 1 | 19043 2 | -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/rdma.current: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/rdma.current -------------------------------------------------------------------------------- /cgroup/test/v2/sys/fs/cgroup/rdma.max: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/couchbase/platform/d829453d45b8d59aaed23b710b9eb2c9d5a87268/cgroup/test/v2/sys/fs/cgroup/rdma.max -------------------------------------------------------------------------------- /external/README.txt: -------------------------------------------------------------------------------- 1 | This directory contains header files from external, third parties 2 | which are used by platform and/or components using platform. 3 | 4 | See their respective files for licence details. 5 | -------------------------------------------------------------------------------- /hdrhistogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(hdrhistogram STATIC 2 | ${Platform_SOURCE_DIR}/include/hdrhistogram/hdrhistogram.h 3 | ${Platform_SOURCE_DIR}/include/hdrhistogram/iterator_range.h 4 | hdrhistogram.cc) 5 | 6 | target_include_directories(hdrhistogram PUBLIC ${Platform_SOURCE_DIR}/include) 7 | # Mark hdr_histogram as 'system' so we skip any warnings it generates. 8 | target_include_directories(hdrhistogram SYSTEM BEFORE PUBLIC 9 | ${hdr_histogram_SOURCE_DIR}/src) 10 | target_link_libraries(hdrhistogram PRIVATE fmt::fmt hdr_histogram_static 11 | Folly::folly platform nlohmann_json::nlohmann_json) 12 | 13 | set_property(TARGET hdrhistogram PROPERTY POSITION_INDEPENDENT_CODE true) 14 | 15 | cb_add_test_executable(hdrhistogram_test hdrhistogram_test.cc) 16 | target_link_libraries(hdrhistogram_test hdrhistogram 17 | fmt::fmt 18 | nlohmann_json::nlohmann_json 19 | ${Boost_LIBRARIES} 20 | Folly::folly 21 | GTest::gtest 22 | GTest::gtest_main) 23 | add_test(hdrhistogram_test hdrhistogram_test) 24 | -------------------------------------------------------------------------------- /include/breakpad_wrapper/breakpad_wrapper.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2015-Present Couchbase, Inc. 4 | * 5 | * Use of this software is governed by the Business Source License included 6 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 7 | * in that file, in accordance with the Business Source License, use of this 8 | * software will be governed by the Apache License, Version 2.0, included in 9 | * the file licenses/APL2.txt. 10 | */ 11 | 12 | /* 13 | * A set of "extern C" functions to allow non-C++ applications to invoke 14 | * Breakpad. 15 | */ 16 | 17 | #pragma once 18 | 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* Setup Breakpad, specifying the directory where any minidumps should be 28 | * written. Should be invoked as part of program initialization, to reserve 29 | * memory for Breakpad's operation. *MUST* have been called before attempting 30 | * to write a minidump with breakpad_write_minidump(). 31 | * 32 | * Note: Breakpad is configured to *NOT* automatically handle crashes and 33 | * write a minidump file, users must call breakpad_write_minidump() when 34 | * they wish to create a dump file. 35 | * 36 | * @param minidump_dir Path to an existing, writable directory which 37 | * minidumps will be written to. 38 | */ 39 | void breakpad_initialize(const char* minidump_dir); 40 | 41 | /* Writes a minidump of the current application state, to the directory 42 | * previously specified to breakpad_initialize(). 43 | * @return True if the minidump was successfully written, else false. 44 | */ 45 | bool breakpad_write_minidump(); 46 | 47 | /* Returns the address of breakpad_write_minidump() function. 48 | * Provided to facilite passing that symbol into foreign environments 49 | * (e.g. Golang) for later use as a C function pointer. 50 | */ 51 | uintptr_t breakpad_get_write_minidump_addr(); 52 | 53 | #ifdef __cplusplus 54 | } // extern "C" 55 | #endif 56 | -------------------------------------------------------------------------------- /include/cbcrypto/dump_keys_runner.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "common.h" 14 | 15 | #include 16 | #include 17 | 18 | namespace cb::crypto { 19 | 20 | namespace dump_keys { 21 | 22 | class DumpKeysError : public std::runtime_error { 23 | public: 24 | explicit DumpKeysError(const std::string& msg); 25 | }; 26 | 27 | /// Exception thrown when dump-keys returns a non-zero exit code 28 | class ExecuteError : public DumpKeysError { 29 | public: 30 | ExecuteError(int ec, std::string out, std::string err); 31 | int ec; 32 | const std::string out; 33 | const std::string err; 34 | }; 35 | 36 | class IncorrectPasswordError : public DumpKeysError { 37 | public: 38 | IncorrectPasswordError(); 39 | }; 40 | 41 | class InvalidOutputError : public DumpKeysError { 42 | public: 43 | InvalidOutputError(std::string msg, std::string out); 44 | const std::string msg; 45 | const std::string out; 46 | }; 47 | 48 | class InvalidFormatError : public DumpKeysError { 49 | public: 50 | InvalidFormatError(std::string msg, nlohmann::json json); 51 | const std::string msg; 52 | const nlohmann::json json; 53 | }; 54 | 55 | class KeyLookupError : public DumpKeysError { 56 | public: 57 | KeyLookupError(std::string id, std::string error); 58 | const std::string id; 59 | const std::string error; 60 | }; 61 | 62 | class UnsupportedCipherError : public DumpKeysError { 63 | public: 64 | UnsupportedCipherError(std::string id, std::string cipher); 65 | const std::string id; 66 | const std::string cipher; 67 | }; 68 | 69 | } // namespace dump_keys 70 | 71 | class DumpKeysRunner { 72 | public: 73 | virtual ~DumpKeysRunner() = default; 74 | static std::unique_ptr create( 75 | std::string password, 76 | std::filesystem::path executable, 77 | std::filesystem::path gosecrets); 78 | 79 | [[nodiscard]] virtual SharedEncryptionKey lookup( 80 | std::string_view id) const = 0; 81 | }; 82 | 83 | } // namespace cb::crypto 84 | -------------------------------------------------------------------------------- /include/cbcrypto/encrypted_file_header.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | #include // uint8_t 16 | #include 17 | 18 | namespace cb::crypto { 19 | enum class Compression : uint8_t { 20 | None = 0, 21 | Snappy = 1, 22 | ZLIB = 2, 23 | GZIP = 3, 24 | ZSTD = 4, 25 | BZIP2 = 5, 26 | }; 27 | 28 | std::string format_as(Compression compression); 29 | 30 | /// Class representing the fixed size Encrypted Files 31 | class EncryptedFileHeader { 32 | public: 33 | constexpr static std::string_view Magic = {"\0Couchbase Encrypted\0", 21}; 34 | /// Initialize a new instance of the FileHeader and set the key id to 35 | /// use 36 | explicit EncryptedFileHeader(std::string_view key_id, 37 | cb::uuid::uuid_t salt = cb::uuid::random(), 38 | Compression compression = Compression::None); 39 | 40 | /// Is "this" an encrypted header (contains the correct magic) 41 | [[nodiscard]] bool is_encrypted() const; 42 | /// Is "this" encrypted and the version is something we support 43 | [[nodiscard]] bool is_supported() const; 44 | /// Get the compression type used in the file 45 | [[nodiscard]] Compression get_compression() const; 46 | /// Get the key identifier in the header 47 | [[nodiscard]] std::string_view get_id() const; 48 | /// Get the salt used in the encryption 49 | [[nodiscard]] cb::uuid::uuid_t get_salt() const; 50 | /// Convenience function to convert to string_view 51 | [[nodiscard]] operator std::string_view() const { 52 | return {reinterpret_cast(this), sizeof(*this)}; 53 | } 54 | 55 | protected: 56 | std::array magic{}; 57 | uint8_t version{0}; 58 | uint8_t compression{0}; 59 | std::array unused{}; 60 | uint8_t id_size = 0; 61 | std::array id{}; 62 | std::array salt{}; 63 | }; 64 | 65 | static_assert(sizeof(EncryptedFileHeader) == 80); 66 | } // namespace cb::crypto 67 | -------------------------------------------------------------------------------- /include/cbcrypto/file_utilities.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace cb::crypto { 19 | struct DataEncryptionKey; 20 | using SharedEncryptionKey = std::shared_ptr; 21 | 22 | /** 23 | * Find all the DEKs in use in the specified directory 24 | * 25 | * @param directory the directory to search 26 | * @param filefilter a function to filter out files we're not interested in 27 | * (return true to inspect the file, false to skip it) 28 | * @param error a function to call if we encounter an error 29 | */ 30 | std::unordered_set findDeksInUse( 31 | const std::filesystem::path& directory, 32 | const std::function& filefilter, 33 | const std::function& 34 | error); 35 | 36 | /** 37 | * Iterate over all files in the specified directory and potentially 38 | * rewrite all files. 39 | * 40 | * @param directory The directory to scan 41 | * @param filefilter a function to filter out files we're not interested in 42 | * (return true to inspect the file, false to skip it) 43 | * @param encryption_key The key to use when rewriting the files (if empty the 44 | * file will be written unencrypted) 45 | * @param key_lookup_function A function used to look up encryption keys from 46 | * the id 47 | * @param error a callback to add log messages when errors occurs 48 | * @param unencrypted_extension The extension to use for unencrypted files 49 | */ 50 | void maybeRewriteFiles( 51 | const std::filesystem::path& directory, 52 | const std::function& filefilter, 54 | SharedEncryptionKey encryption_key, 55 | const std::function& 56 | key_lookup_function, 57 | const std::function& 58 | error, 59 | std::string_view unencrypted_extension = ".txt"); 60 | 61 | } // namespace cb::crypto 62 | -------------------------------------------------------------------------------- /include/cbcrypto/file_writer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace cb::crypto { 17 | 18 | /** 19 | * The FileWriter class allows for writing to a file with or without 20 | * encryption (depending on if an encryption key is present). The primary 21 | * motivation for the class is to simplify the logic in the other classes 22 | * which wants to write both encrypted and plain versions of the files 23 | * depending on the configuration. 24 | */ 25 | class FileWriter { 26 | public: 27 | /** 28 | * Create a new instance of the FileWriter 29 | * 30 | * @param dek The key to use to write the files. If no key is present 31 | * the data is written unencrypted 32 | * @param path The name of the file to write 33 | * @param buffer_size An optional buffer size to let the underlying file 34 | * writer buffer data before writing to disk. This 35 | * is useful for encrypted logfiles ot avoid writing 36 | * small chunks 37 | * @return A new FileWriter instance 38 | */ 39 | static std::unique_ptr create( 40 | const SharedEncryptionKey& dek, 41 | std::filesystem::path path, 42 | size_t buffer_size = 0, 43 | Compression compression = Compression::None); 44 | 45 | /// Is the file being read encrypted or not 46 | [[nodiscard]] virtual bool is_encrypted() const = 0; 47 | 48 | /// Return the current size of the file (in bytes) 49 | [[nodiscard]] virtual size_t size() const = 0; 50 | 51 | /** 52 | * Write the provided chunk of data to the file 53 | * @param chunk The data to write 54 | * @throws std::runtime_error if an error occurs 55 | */ 56 | virtual void write(std::string_view chunk) = 0; 57 | 58 | /// Flush data to the file, and throw an exception if an error occurs 59 | virtual void flush() = 0; 60 | 61 | /// Close the stream. No more data can be written after this call 62 | virtual void close() = 0; 63 | 64 | virtual ~FileWriter() = default; 65 | 66 | protected: 67 | FileWriter() = default; 68 | }; 69 | 70 | } // namespace cb::crypto 71 | -------------------------------------------------------------------------------- /include/cbcrypto/key_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "common.h" 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace cb ::crypto { 20 | /** 21 | * The KeyStore is a class which holds multiple keys and allow for 22 | * looking up keys with an ID. The keystore includes an active key 23 | */ 24 | class KeyStore { 25 | public: 26 | /// Get the current active key (note: it may be empty) 27 | [[nodiscard]] SharedEncryptionKey getActiveKey() const { 28 | return active; 29 | } 30 | 31 | /// Look up a given key by its id (or return {} if the id is unknown) 32 | [[nodiscard]] SharedEncryptionKey lookup(std::string_view id) const; 33 | 34 | /// set the provided key as active (and add it to the list of keys_ 35 | void setActiveKey(SharedEncryptionKey key); 36 | 37 | /// Add a key to the list of keys (but do not mark it as active) 38 | void add(SharedEncryptionKey key); 39 | 40 | /// Iterate over all known keys and call the provided callback 41 | void iterateKeys( 42 | const std::function& callback) const; 43 | 44 | protected: 45 | std::vector keys; 46 | SharedEncryptionKey active; 47 | }; 48 | 49 | [[nodiscard]] std::string format_as(const KeyStore& ks); 50 | void to_json(nlohmann::json& json, const KeyStore& ks); 51 | void from_json(const nlohmann::json& json, KeyStore& ks); 52 | 53 | } // namespace cb::crypto 54 | -------------------------------------------------------------------------------- /include/cbcrypto/random_gen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include "common.h" 13 | 14 | #include 15 | 16 | namespace cb::crypto { 17 | 18 | /** 19 | * Cryptographically secure random bit generator 20 | */ 21 | class RandomBitGenerator { 22 | public: 23 | /** 24 | * Instantiates a RandomBitGenerator. 25 | * 26 | * @param properties Properties to pass to OpenSSL (e.g. provider) 27 | * @throws OpenSslError 28 | */ 29 | static std::unique_ptr create( 30 | const char* properties = nullptr); 31 | 32 | /** 33 | * Populate `buf` with random bytes. 34 | * 35 | * @throws OpenSslError 36 | */ 37 | virtual void generate(gsl::span buf) = 0; 38 | 39 | virtual ~RandomBitGenerator() = default; 40 | }; 41 | 42 | } // namespace cb::crypto 43 | -------------------------------------------------------------------------------- /include/platform/awaitable_semaphore.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2022-Present Couchbase, Inc. 4 | * 5 | * Use of this software is governed by the Business Source License included 6 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 7 | * in that file, in accordance with the Business Source License, use of this 8 | * software will be governed by the Apache License, Version 2.0, included in 9 | * the file licenses/APL2.txt. 10 | */ 11 | #pragma once 12 | 13 | #include "semaphore.h" 14 | 15 | #include "unique_waiter_queue.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace cb { 24 | 25 | /** 26 | * Semaphore variant tracking a queue of actors waiting to acquire a token. 27 | * 28 | * Useful if users do not wish to block to wait for a resource (e.g., 29 | * GlobalTask) but wish to be notified once a token becomes available. 30 | * 31 | * Example usage: 32 | * 33 | * FooBarTask::run() { 34 | * if (!semaphore.acquire_or_wait(shared_from_this())) { 35 | * // snooze() forever. 36 | * return true; 37 | * } 38 | * // token was acquired, do some semaphore-protected work 39 | * semaphore.release(); 40 | * } 41 | * 42 | * FooBarTask::signal() { 43 | * // wake the task, so that it calls run() again, and tries to acquire 44 | * // a token again. 45 | * } 46 | * 47 | */ 48 | class AwaitableSemaphore : public Semaphore { 49 | public: 50 | // pull in constructors 51 | using Semaphore::Semaphore; 52 | 53 | using Semaphore::release; 54 | 55 | /** 56 | * Return @p count tokens to the semaphore. 57 | * 58 | * If there are queued waiters, they will be signalled to run again. 59 | */ 60 | void release(size_t count) override; 61 | 62 | /** 63 | * Attempt to acquire a token from the semaphore, or queue for notification 64 | * if no tokens are available. 65 | * 66 | * @param waiter waiter which will be queued if a token cannot be acquired 67 | * @return true if a token was acquired, else false. 68 | */ 69 | bool acquire_or_wait(std::weak_ptr waiter); 70 | 71 | /** 72 | * Get the current tasks waiting for this semaphore. 73 | * 74 | * Test-only. 75 | */ 76 | std::vector> getWaiters(); 77 | 78 | protected: 79 | void signalWaiters(size_t count); 80 | 81 | folly::Synchronized waiters; 82 | }; 83 | 84 | } // namespace cb -------------------------------------------------------------------------------- /include/platform/backtrace.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using write_cb_t = void (*)(void*, const char*); 17 | 18 | /** 19 | * Prints a backtrace from the current thread. For each frame, the 20 | * `write_cb` function is called with `context` and a string describing 21 | * the frame. 22 | */ 23 | void print_backtrace(write_cb_t write_cb, void* context); 24 | 25 | void print_backtrace_frames(const boost::stacktrace::stacktrace& frames, 26 | std::function callback); 27 | 28 | /** 29 | * Convenience function - prints a backtrace to the specified FILE. 30 | */ 31 | void print_backtrace_to_file(FILE* stream); 32 | 33 | /** 34 | * print a backtrace to a buffer 35 | * 36 | * @param indent the indent used for each entry in the callstack 37 | * @param buffer the buffer to populate with the backtrace 38 | * @param size the size of the input buffer 39 | */ 40 | bool print_backtrace_to_buffer(const char *indent, char *buffer, size_t size); 41 | 42 | namespace cb::backtrace { 43 | /** 44 | * Prepare the process for being able to call the backtrace methods. 45 | * 46 | * On Windows we need to load the symbol tables and from the looks of it 47 | * that won't work if we try to do it after we crashed (in the signal 48 | * handler). 49 | */ 50 | void initialize(); 51 | } 52 | -------------------------------------------------------------------------------- /include/platform/base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | namespace cb::base64 { 15 | 16 | /** 17 | * Base64 encode data 18 | * 19 | * @param source the string to encode 20 | * @return the base64 encoded value 21 | */ 22 | std::string encode(std::string_view source); 23 | 24 | /** 25 | * Decode a base64 encoded blob (which may be pretty-printed to avoid 26 | * super-long lines) 27 | * 28 | * @param blob string to decode 29 | * @return the decoded data 30 | */ 31 | std::string decode(std::string_view blob); 32 | 33 | } // namespace cb::base64 34 | 35 | /** 36 | * An alternative encoding using '-' and '_' for the 62 and 63rd character 37 | * in the alphabet. 38 | */ 39 | namespace cb::base64url { 40 | std::string encode(std::string_view source); 41 | std::string decode(std::string_view source); 42 | } // namespace cb::base64url 43 | -------------------------------------------------------------------------------- /include/platform/bitset.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | /** 12 | * Variadic function wrapper around std::bitset. The general intent is that if 13 | * you had: 14 | * enum states {a, b, c, d}; 15 | * 16 | * You can create bitset to represent a set of states: 17 | * 18 | * cb::bitset<4, states> permittedStates(a, c); 19 | * std::cout << permittedStates << "\n"; // 0101 20 | * 21 | * Or if you had: 22 | * enum states {a = 1, b, c, d}; 23 | * struct states_mapper { 24 | * size_t map(states in) { 25 | * return in - 1; 26 | * } 27 | * } 28 | * cb::bitset<4, states, states_mapper> permittedStates(a, d); 29 | * std::cout << permittedStates << "\n"; // 1001 30 | */ 31 | 32 | #pragma once 33 | 34 | #include 35 | 36 | namespace cb { 37 | template 38 | struct default_bitset_mapper { 39 | [[nodiscard]] constexpr size_t map(Type t) const { 40 | return size_t(t); 41 | } 42 | }; 43 | 44 | template > 45 | class bitset { 46 | public: 47 | /// Default construct an empty bitset 48 | bitset() { 49 | } 50 | 51 | /// Construct bitset setting the specified values 52 | template 53 | bitset(Type t, Targs... Fargs) { 54 | init(t, Fargs...); 55 | } 56 | 57 | /// map and set the input t 58 | void set(Type t) { 59 | bits.set(Map().map(t)); 60 | } 61 | 62 | /// map and reset the input t 63 | void reset(Type t) { 64 | bits.reset(Map().map(t)); 65 | } 66 | 67 | /// map and test the input t 68 | [[nodiscard]] bool test(Type t) const { 69 | return bits.test(Map().map(t)); 70 | } 71 | 72 | private: 73 | template 74 | void init(Type t, Targs... Fargs) { 75 | init(t); 76 | init(Fargs...); 77 | } 78 | 79 | void init(Type t) { 80 | this->set(t); 81 | } 82 | 83 | std::bitset bits; 84 | }; 85 | 86 | } // end namespace cb 87 | -------------------------------------------------------------------------------- /include/platform/byte_buffer_dump.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | 15 | std::ostream& operator<<(std::ostream& out, const cb::byte_buffer& buffer); 16 | 17 | std::ostream& operator<<(std::ostream& out, const cb::const_byte_buffer& buffer); 18 | 19 | -------------------------------------------------------------------------------- /include/platform/cb_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb::time { 17 | 18 | struct steady_clock { 19 | using duration = std::chrono::steady_clock::duration; 20 | using rep = duration::rep; 21 | using period = duration::period; 22 | using time_point = std::chrono::steady_clock::time_point; 23 | static constexpr bool is_steady = true; 24 | 25 | /** 26 | * @return time_point for now from a steady clock, either std::chrono or our 27 | * static variant (which only ticks when requested via advance). 28 | */ 29 | static time_point now() { 30 | if (use_chrono) { 31 | return std::chrono::steady_clock::now(); 32 | } 33 | return static_now(); 34 | } 35 | 36 | /** 37 | * @return a time_point which begins as std::chrono::steady_clock::now and 38 | * only advances with calls to advance 39 | */ 40 | static time_point static_now(); 41 | 42 | /** 43 | * Advance the static clock - function performs time += duration 44 | * @param duration to advance clock by 45 | */ 46 | static void advance(std::chrono::nanoseconds duration); 47 | 48 | /** 49 | * Flag controlling what steady_clock::now returns. When true (the default) 50 | * steady_clock::now will return the value of std::chrono::steady_clock::now 51 | * and when false steady_clock::now will return the value a value which only 52 | * advances with calls to advance(duration). 53 | */ 54 | static std::atomic use_chrono; 55 | }; 56 | } // end namespace cb::time -------------------------------------------------------------------------------- /include/platform/cbassert.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #ifdef __cplusplus 13 | /** 14 | * If running on Windows with a Debug build, direct error and assertion 15 | * messages from the CRT to stderr, in addition to the default GUI dialog 16 | * box. 17 | * Also includes a backtrace of the error. 18 | * Ensures that errors from Debug-mode tests etc are visible even if running 19 | * in a non-graphical mode (e.g. Jenkins CV job). 20 | * No-op on non-Windows, non-Debug build. 21 | */ 22 | void setupWindowsDebugCRTAssertHandling(); 23 | 24 | extern "C" { 25 | [[noreturn]] 26 | #endif 27 | void cb_assert_die(const char *expression, const char *file, int line); 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | 32 | #define cb_assert(e) \ 33 | ((void)((e) ? (void)0 : cb_assert_die(#e, __FILE__, __LINE__))) 34 | -------------------------------------------------------------------------------- /include/platform/checked_snprintf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * This is a checked version of snprintf that will throw exceptions. It 18 | * allows you to do stuff like: 19 | * 20 | * 21 | * int offset = checked_snprintf(...) 22 | * offset += checked_snprintf(...) 23 | * 24 | * 25 | * Unfortunately the behaviour of snprintf differs between the platforms. 26 | * On Windows snprintf returns -1 if snprintf fails or the provided buffer 27 | * is too small to contain the entire formatted message (dest has to 28 | * be a non-null value for this to be true. if dest is set to null, it 29 | * returns the length of the formatted string). On other platforms snprintf 30 | * returns -1 on error and then the number of bytes the entire formatted 31 | * string would take (if there was room for it), but not write outside 32 | * the provided buffer. Since we can't cleanly determine between the 33 | * two error scenarios on windows, I'm treating all errors from 34 | * the underlying snprintf the same and throw std::overflow_error. 35 | * (the only times I've seen snprintf fail is when it failed to allocate 36 | * memory anyway) 37 | * 38 | * @param str destination buffer 39 | * @param size the size of the destination buffer 40 | * @param format the format string 41 | * @param ... the optional arguments (just like snprintf) 42 | * @returns the number of bytes added to the output buffer 43 | * @throws std::invalid_argument if str == 0 or size == 0 44 | * @throws std::overflow_error if the destination buffer isn't big enough, 45 | * (or snprintf returned -1) 46 | */ 47 | int checked_snprintf(char* str, const size_t size, const char* format, ...) 48 | CB_FORMAT_PRINTF(3, 4); 49 | -------------------------------------------------------------------------------- /include/platform/comparators.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2021-Present Couchbase, Inc. 4 | * 5 | * Use of this software is governed by the Business Source License included 6 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 7 | * in that file, in accordance with the Business Source License, use of this 8 | * software will be governed by the Apache License, Version 2.0, included in 9 | * the file licenses/APL2.txt. 10 | */ 11 | 12 | #pragma once 13 | 14 | namespace cb { 15 | /** 16 | * Function object which returns true if lhs > rhs. 17 | * Equivalent to std::greater, but without having to pull in all of 18 | */ 19 | template 20 | struct greater { 21 | constexpr bool operator()(const T& lhs, const T& rhs) const { 22 | return lhs > rhs; 23 | } 24 | }; 25 | 26 | /** 27 | * Function object which returns true if lhs >= rhs. 28 | * Equivalent to std::greater_equal, but without having to pull in all of 29 | * 30 | */ 31 | template 32 | struct greater_equal { 33 | constexpr bool operator()(const T& lhs, const T& rhs) const { 34 | return lhs >= rhs; 35 | } 36 | }; 37 | 38 | /** 39 | * Function object which returns true if lhs < rhs. 40 | * Equivalent to std::less, but without having to pull in all of 41 | * 42 | */ 43 | template 44 | struct less { 45 | constexpr bool operator()(const T& lhs, const T& rhs) const { 46 | return lhs < rhs; 47 | } 48 | }; 49 | 50 | /** 51 | * Function object which returns true if lhs <= rhs. 52 | * Equivalent to std::less_equal, but without having to pull in all of 53 | * 54 | */ 55 | template 56 | struct less_equal { 57 | constexpr bool operator()(const T& lhs, const T& rhs) const { 58 | return lhs <= rhs; 59 | } 60 | }; 61 | } // namespace cb 62 | -------------------------------------------------------------------------------- /include/platform/compression/allocator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | namespace cb::compression { 18 | 19 | /** 20 | * The memory allocator allows you to choose if you want the memory to 21 | * be allocated with cb_malloc/cb_free or new/delete. It should probably be 22 | * reimplemented with the fancier Allocator stuff in C++, but from a quick 23 | * glance that looked a bit more complex than what's needed for now.. 24 | */ 25 | struct Allocator { 26 | enum class Mode { 27 | /** 28 | * Use the new array allocator to allocate backing space. 29 | * The buffer must be released with delete[] if the memory 30 | * is released from the buffer 31 | */ 32 | New, 33 | /** 34 | * Use cb_malloc to allocate backing space. The memory must 35 | * be freed with cb_free if the memory is released from the buffer 36 | */ 37 | Malloc 38 | }; 39 | 40 | explicit Allocator(Mode mode_ = Mode::New) : mode(mode_) { 41 | } 42 | 43 | char* allocate(size_t nbytes) { 44 | char* ret; 45 | 46 | switch (mode) { 47 | case Mode::New: 48 | return new char[nbytes]; 49 | case Mode::Malloc: 50 | ret = static_cast(cb_malloc(nbytes)); 51 | if (ret == nullptr) { 52 | throw std::bad_alloc(); 53 | } 54 | return ret; 55 | } 56 | throw std::runtime_error("Allocator::allocate: Unknown mode"); 57 | } 58 | 59 | void deallocate(char* ptr) { 60 | switch (mode) { 61 | case Mode::New: 62 | delete[] ptr; 63 | return; 64 | case Mode::Malloc: 65 | cb_free(static_cast(ptr)); 66 | return; 67 | } 68 | throw std::runtime_error("Allocator::deallocate: Unknown mode"); 69 | } 70 | 71 | const Mode mode; 72 | }; 73 | } // namespace cb::compression 74 | -------------------------------------------------------------------------------- /include/platform/crc32c.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | // 12 | // Generate a CRC-32C (Castagnolia) 13 | // CRC polynomial of 0x1EDC6F41 14 | // 15 | // When available a hardware assisted function is used for increased 16 | // performance. 17 | // 18 | #pragma once 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #if FOLLY_X64 || FOLLY_AARCH64 25 | #define CB_CRC32_HW_SUPPORTED 1 26 | #endif 27 | 28 | uint32_t crc32c(const uint8_t* buf, size_t len, uint32_t crc_in); 29 | 30 | static inline uint32_t crc32c(std::string_view data, uint32_t crc_in = 0) { 31 | return crc32c( 32 | reinterpret_cast(data.data()), data.size(), crc_in); 33 | } 34 | 35 | // The following methods are used by unit testing to force the calculation 36 | // of the checksum by using a given implementation. 37 | uint32_t crc32c_sw(const uint8_t* buf, size_t len, uint32_t crc_in); 38 | #ifdef CB_CRC32_HW_SUPPORTED 39 | uint32_t crc32c_hw(const uint8_t* buf, size_t len, uint32_t crc_in); 40 | uint32_t crc32c_hw_1way(const uint8_t* buf, size_t len, uint32_t crc_in); 41 | #endif 42 | -------------------------------------------------------------------------------- /include/platform/define_enum_class_bitmask_functions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | /** 14 | * Define operators (|, |=, &, &= and ~) and a function to check if a bit 15 | * is set in the enum (bitmask) 16 | * 17 | * @param T The name of the enum to define the operators for 18 | */ 19 | #define DEFINE_ENUM_CLASS_BITMASK_FUNCTIONS(T) \ 20 | constexpr T operator|(T a, T b) { \ 21 | return T(std::underlying_type_t(a) | std::underlying_type_t(b)); \ 22 | } \ 23 | constexpr T& operator|=(T& a, T b) { \ 24 | a = a | b; \ 25 | return a; \ 26 | } \ 27 | constexpr T operator&(T a, T b) { \ 28 | return T(std::underlying_type_t(a) & std::underlying_type_t(b)); \ 29 | } \ 30 | constexpr T& operator&=(T& a, T b) { \ 31 | a = a & b; \ 32 | return a; \ 33 | } \ 34 | constexpr T operator~(T a) { \ 35 | return T(~std::underlying_type_t(a)); \ 36 | } \ 37 | constexpr bool isFlagSet(T mask, T flag) { \ 38 | return (mask & flag) == flag; \ 39 | } 40 | -------------------------------------------------------------------------------- /include/platform/dynamic.in.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #cmakedefine CB_DONT_NEED_BYTEORDER 1 13 | 14 | /* Function attribute to be used for printf-style format checking 15 | */ 16 | #cmakedefine HAVE_ATTR_FORMAT 17 | #if defined(HAVE_ATTR_FORMAT) 18 | #define CB_FORMAT_PRINTF(FMT_IDX, STR_IDX) __attribute__((format(printf, FMT_IDX, STR_IDX))) 19 | #else 20 | #define CB_FORMAT_PRINTF(FMT_IDX, STR_IDX) 21 | #endif 22 | -------------------------------------------------------------------------------- /include/platform/getopt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | /** 13 | * This file contains a getopt implementation which is only used 14 | * on Windows (but it is built on all platforms to make it easier 15 | * to test ;) 16 | */ 17 | 18 | 19 | namespace cb::getopt { 20 | 21 | const int no_argument = 0; 22 | const int required_argument = 1; 23 | const int optional_argument = 2; 24 | 25 | struct option { 26 | const char* name; 27 | int has_arg; 28 | int* flag; 29 | int val; 30 | }; 31 | 32 | extern char* optarg; 33 | extern int opterr; 34 | extern int optind; 35 | extern int optopt; 36 | 37 | extern int getopt_long(int argc, 38 | char* const* argv, 39 | const char* optstring, 40 | const struct option* longopts, 41 | int* longindex); 42 | 43 | extern int getopt(int argc, char* const* argv, const char* optstring); 44 | 45 | /** 46 | * This is for unit tests only and used to reset the internal state 47 | * of the library 48 | */ 49 | void reset(); 50 | 51 | /** 52 | * This is used for unit tests to mute the unit tests from writing error 53 | * messages to stderr 54 | */ 55 | void mute_stderr(); 56 | } // namespace cb::getopt 57 | -------------------------------------------------------------------------------- /include/platform/getpass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | namespace cb { 15 | std::string getpass(std::string_view prompt = "Password: "); 16 | } 17 | -------------------------------------------------------------------------------- /include/platform/interrupt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | 13 | namespace cb::console { 14 | using interrupt_handler = void (*)(); 15 | 16 | /** 17 | * Sets a handler that will be invoked when a ctrl+c event is detected 18 | * 19 | * @throws std::system_error if an error occurs while setting up the 20 | * handler 21 | */ 22 | void set_sigint_handler(interrupt_handler h); 23 | 24 | /** 25 | * Clears any handler that might have been invoked where a ctrl+c event 26 | * was detected. 27 | */ 28 | void clear_sigint_handler(); 29 | } // namespace cb::console 30 | -------------------------------------------------------------------------------- /include/platform/monotonic_queue.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2019-Present Couchbase, Inc. 4 | * 5 | * Use of this software is governed by the Business Source License included 6 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 7 | * in that file, in accordance with the Business Source License, use of this 8 | * software will be governed by the Apache License, Version 2.0, included in 9 | * the file licenses/APL2.txt. 10 | */ 11 | 12 | #pragma once 13 | 14 | #include 15 | 16 | #include 17 | 18 | /** 19 | * MonotonicQueue enforces a parameterisable invariant on values added to a 20 | * queue e.g., values added to the queue must be greater than or equal to the 21 | * preceding value 22 | * 23 | * @tparam T value type used in queue. 24 | * @tparam OrderReversePolicy Policy class which controls the behaviour if 25 | * an operation would break the monotonic invariant. 26 | * @tparam Name a pointer to a string literal that stores the name of the 27 | * variable name given to this Monotonic 28 | * @tparam LabelPolicy A class that proves a function getLabel() which 29 | * takes a cont char* pointing to the Name and returns a label as 30 | * std::string about the Monotnic<> in question 31 | * @tparam Invariant The invariant to maintain across pushes. 32 | */ 33 | template class OrderReversedPolicy = 35 | DefaultOrderReversedPolicy, 36 | const char* Name = nullptr, 37 | class LabelPolicy = BasicNameLabelPolicy, 38 | template class Invariant = cb::greater> 39 | class MonotonicQueue { 40 | public: 41 | const T& front() const { 42 | return queue.front(); 43 | } 44 | T& front() { 45 | return queue.front(); 46 | } 47 | const T& back() const { 48 | return queue.back(); 49 | } 50 | T& back() { 51 | return queue.back(); 52 | } 53 | bool empty() const { 54 | return queue.empty(); 55 | } 56 | size_t size() const { 57 | return queue.size(); 58 | } 59 | 60 | void push(const T& value) { 61 | latestValue = value; 62 | queue.push(value); 63 | } 64 | void push(T&& value) { 65 | latestValue = value; 66 | queue.push(std::move(value)); 67 | } 68 | void pop() { 69 | queue.pop(); 70 | } 71 | 72 | private: 73 | std::queue queue; 74 | Monotonic latestValue; 75 | }; 76 | -------------------------------------------------------------------------------- /include/platform/murmurhash3.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // MurmurHash3 was written by Austin Appleby, and is placed in the public 3 | // domain. The author hereby disclaims copyright to this source code. 4 | 5 | #pragma once 6 | 7 | //----------------------------------------------------------------------------- 8 | // Platform-specific functions and macros 9 | 10 | #include 11 | 12 | //----------------------------------------------------------------------------- 13 | 14 | void MurmurHash3_x86_32(const void* key, int len, uint32_t seed, uint32_t* out); 15 | 16 | /** 17 | * The following 2 functions have been altered to just return the 18 | * 64 most significant bits. 19 | */ 20 | void MurmurHash3_x86_128(const void* key, 21 | int len, 22 | uint32_t seed, 23 | uint64_t* out); 24 | 25 | void MurmurHash3_x64_128(const void* key, 26 | int len, 27 | uint32_t seed, 28 | uint64_t* out); 29 | 30 | //----------------------------------------------------------------------------- 31 | -------------------------------------------------------------------------------- /include/platform/optional.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | /** 17 | * Returns a string representation of `val` if the optional is non-empty using 18 | * to_string(T), otherwise returns "none". 19 | */ 20 | template 21 | std::string to_string_or_none(std::optional val) { 22 | using namespace std; 23 | if (val) { 24 | return to_string(*val); 25 | } 26 | return "none"; 27 | } 28 | -------------------------------------------------------------------------------- /include/platform/platform_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | /* 12 | * Header including C socket portability declarations 13 | */ 14 | #pragma once 15 | 16 | #include 17 | #include 18 | 19 | #ifndef CB_DONT_NEED_BYTEORDER 20 | #include 21 | 22 | inline uint64_t ntohll(uint64_t x) { 23 | return folly::Endian::big(x); 24 | } 25 | 26 | inline uint64_t htonll(uint64_t x) { 27 | return folly::Endian::big(x); 28 | } 29 | #endif // CB_DONT_NEED_BYTEORDER 30 | -------------------------------------------------------------------------------- /include/platform/platform_thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | /* 12 | * Header including threading portability declarations 13 | */ 14 | #pragma once 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | /*********************************************************************** 21 | * Thread related functions * 22 | **********************************************************************/ 23 | 24 | /** 25 | * Create a thread which runs the provided function (and tries to set the 26 | * thread name to the provided name) 27 | * 28 | * @param main The method to run within the thread object 29 | * @param name The name for the thread (not const as it gets moved 30 | * over to the running thread to clean up) 31 | * @return The (running) thread object 32 | */ 33 | std::thread create_thread(std::function main, std::string name); 34 | 35 | /// We'll only support a thread name up to 32 characters 36 | constexpr size_t MaxThreadNameLength = 32; 37 | 38 | /** 39 | * Sets the current threads' name. 40 | * 41 | * @param name New value for the current threads' name 42 | * @return true for success, false otherwise 43 | * @throws std::logic_error if the thread name exceeds the max length 44 | */ 45 | bool cb_set_thread_name(std::string_view name); 46 | 47 | -------------------------------------------------------------------------------- /include/platform/processclock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | 13 | #include 14 | #include 15 | 16 | namespace cb { 17 | 18 | // Simple wrapper function that returns std::chrono::nanoseconds 19 | // given a std::chrono::steady_clock::time_point 20 | std::chrono::nanoseconds to_ns_since_epoch( 21 | const std::chrono::steady_clock::time_point& tp); 22 | 23 | /** 24 | * Interface for a source of 'now' for the std::chrono::steady_clock to allow 25 | * for dependency injection of time. 26 | */ 27 | struct ProcessClockSource { 28 | virtual std::chrono::steady_clock::time_point now() = 0; 29 | virtual ~ProcessClockSource() = default; 30 | }; 31 | 32 | /** 33 | * Default 'now' source for std::chrono::steady_clock, simply 34 | * proxies std::chrono::steady_clock::now() 35 | */ 36 | struct DefaultProcessClockSource : ProcessClockSource { 37 | std::chrono::steady_clock::time_point now() override; 38 | }; 39 | 40 | /** 41 | * Singleton instance of DefaultProcessClockSource 42 | */ 43 | DefaultProcessClockSource& defaultProcessClockSource(); 44 | } 45 | 46 | inline std::chrono::nanoseconds to_ns_since_epoch( 47 | const std::chrono::steady_clock::time_point& tp) { 48 | return cb::to_ns_since_epoch(tp); 49 | } 50 | -------------------------------------------------------------------------------- /include/platform/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | 15 | namespace cb { 16 | class RandomGeneratorProvider; 17 | 18 | /** 19 | * The RandomGenerator use windows crypto framework on windows and 20 | * /dev/urandom on the other platforms in order to get random data. 21 | */ 22 | class RandomGenerator { 23 | public: 24 | RandomGenerator(); 25 | 26 | uint64_t next(); 27 | 28 | bool getBytes(void* dest, size_t size); 29 | 30 | private: 31 | /// Return the singleton instance of RandomGeneratorProvider. 32 | static RandomGeneratorProvider& getInstance(); 33 | }; 34 | } // namespace cb 35 | -------------------------------------------------------------------------------- /include/platform/simd/scan.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | 15 | #if FOLLY_X64 16 | #include "scan_sse42.h" 17 | #endif // FOLLY_X64 18 | 19 | #if FOLLY_AARCH64 20 | #include "scan_neon.h" 21 | #endif // FOLLY_AARCH64 22 | 23 | namespace cb::simd { 24 | 25 | /** 26 | * Reads 16 bytes of data, then matches all of the characters passed in as 27 | * template parameters and returns the number of bytes until the first match 28 | * (or 16 if none of the characters was seen in the input). 29 | * 30 | * @tparam Chars The characters to match. 31 | * @param data The 16 byte input. 32 | * @return Number of characters until the first match 33 | */ 34 | template 35 | inline int scan_any_of_128bit(gsl::span data) { 36 | static_assert(sizeof...(Chars) != 0); 37 | auto bytes = detail::load_128bit(data); 38 | auto rv = detail::eq_any_of_128bit(bytes); 39 | return detail::scan_matches(rv); 40 | } 41 | 42 | /** 43 | * Reads 16 bytes of data, then matches all of the characters passed in as 44 | * template parameters and returns the number of bytes until the first match 45 | * (or 16 if none of the characters was seen in the input). 46 | * 47 | * @tparam LessThan A constant value below which to match any input. 48 | * @tparam OrChars Additional characters to match. 49 | * @param data The 16 byte input. 50 | * @return Number of characters until the first match 51 | */ 52 | template 53 | inline int scan_lt_or_any_of_128bit(gsl::span data) { 54 | static_assert(sizeof...(OrChars) != 0); 55 | auto bytes = detail::load_128bit(data); 56 | auto rv = detail::eq_any_of_128bit(bytes); 57 | rv = detail::or_128bit(rv, detail::lt_128bit(bytes)); 58 | return detail::scan_matches(rv); 59 | } 60 | 61 | } // namespace cb::simd 62 | -------------------------------------------------------------------------------- /include/platform/split_string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb::string { 17 | /** 18 | * Split an input string into its various parts. 19 | * 20 | * @param s The input string to split 21 | * @param delim The delimeter used to separate the fields 22 | * @param allowEmpty If false an "empty" token will not be added to the vector 23 | * @return A vector containing the the various tokens 24 | */ 25 | std::vector split(std::string_view s, 26 | char delim = ' ', 27 | bool allowEmpty = true); 28 | } // namespace cb::string 29 | -------------------------------------------------------------------------------- /include/platform/strerror.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | #ifdef WIN32 15 | // Need DWORD 16 | #ifndef WIN32_LEAN_AND_MEAN 17 | #define DO_UNDEF_WIN32_LEAN_AND_MEAN 18 | #define WIN32_LEAN_AND_MEAN 19 | #endif 20 | #include 21 | #ifdef DO_UNDEF_WIN32_LEAN_AND_MEAN 22 | #undef WIN32_LEAN_AND_MEAN 23 | #endif 24 | #endif 25 | 26 | /** 27 | * Get a textual string of the current system error code (GetLastError 28 | * on windows and errno on mac/linux/unix) 29 | */ 30 | std::string cb_strerror(); 31 | 32 | #ifdef WIN32 33 | #define cb_os_error_t DWORD 34 | #else 35 | #define cb_os_error_t int 36 | #endif 37 | 38 | /** 39 | * Get a textual string representation of the specified error code. 40 | * On windows system this is a DWORD returned by GetLastError or 41 | * WSAGetLastError, and on unix systems this is an integer (normally 42 | * the value set by errno) 43 | * 44 | * @param error The error code to look up 45 | * @return a textual representation of the error 46 | */ 47 | std::string cb_strerror(cb_os_error_t error); 48 | -------------------------------------------------------------------------------- /include/platform/string_hex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | 13 | #include "sized_buffer.h" 14 | #include 15 | 16 | namespace cb { 17 | /** 18 | * Get the value for a string of hex characters 19 | * 20 | * @param buffer the input buffer 21 | * @return the value of the string 22 | * @throws std::invalid_argument for an invalid character in the string 23 | * std::overflow_error if the input string won't fit in uint64_t 24 | */ 25 | uint64_t from_hex(std::string_view buffer); 26 | 27 | std::string to_hex(uint8_t val); 28 | 29 | std::string to_hex(uint16_t val); 30 | 31 | std::string to_hex(uint32_t val); 32 | 33 | std::string to_hex(uint64_t val); 34 | 35 | std::string to_hex(const_byte_buffer buffer); 36 | 37 | /** 38 | * Encode a sequence of bytes in hex: (ex: {0xde, 0xad, 0xca, 0xfe} would 39 | * return "deadcafe" 40 | * 41 | * @param buffer Input data to dump 42 | * @return the hex string 43 | */ 44 | std::string hex_encode(const_byte_buffer buffer); 45 | 46 | inline std::string hex_encode(std::string_view buffer) { 47 | return hex_encode( 48 | {reinterpret_cast(buffer.data()), buffer.size()}); 49 | } 50 | 51 | } // namespace cb 52 | -------------------------------------------------------------------------------- /include/platform/string_utilities.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb { 17 | /** 18 | * Convert a size to a format easier for a human to interpret by convering to 19 | * "k", "M" etc 20 | * 21 | * @param value the value to convert 22 | * @param an optional "suffix" to add to the string. By default adding 'b' to 23 | * return sizes like: 10Mb etc 24 | * @return the size in a format easier to read for a human 25 | */ 26 | std::string size2human(std::size_t value, 27 | const std::optional& suffix = "B"); 28 | } // namespace cb 29 | -------------------------------------------------------------------------------- /include/platform/syncobject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | 15 | /** 16 | * Abstraction built on top of std::condition_variable & std::mutex 17 | */ 18 | class SyncObject : public std::mutex { 19 | public: 20 | SyncObject() = default; 21 | SyncObject(const SyncObject&) = delete; 22 | void operator=(const SyncObject&) = delete; 23 | ~SyncObject() = default; 24 | 25 | // Note: there is no `wait(lock)` override (without Predicate) provided. 26 | // This is because the underlying std::condition_variable::wait(lock) 27 | // method can get spurious wakeups, and hence can miss notifications if 28 | // using a non-predicated wait - see 29 | // http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rconc-wait 30 | 31 | template 32 | void wait(std::unique_lock& lock, Predicate pred) { 33 | cond.wait(lock, pred); 34 | } 35 | 36 | void wait_for(std::unique_lock& lock, const double secs) { 37 | cond.wait_for(lock, std::chrono::milliseconds(int64_t(secs * 1000.0))); 38 | } 39 | 40 | void wait_for(std::unique_lock& lock, 41 | const std::chrono::nanoseconds nanoSecs) { 42 | cond.wait_for(lock, nanoSecs); 43 | } 44 | 45 | template 46 | void wait_for(std::unique_lock& lock, 47 | const std::chrono::nanoseconds nanoSecs, 48 | Predicate pred) { 49 | cond.wait_for(lock, nanoSecs, pred); 50 | } 51 | 52 | void notify_all() { 53 | cond.notify_all(); 54 | } 55 | 56 | void notify_one() { 57 | cond.notify_one(); 58 | } 59 | 60 | protected: 61 | std::condition_variable cond; 62 | }; 63 | -------------------------------------------------------------------------------- /include/platform/sysinfo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | namespace cb { 15 | 16 | /** 17 | * Returns the number of logical threads (CPUs) this process has 18 | * access to - i.e. the maximum number of concurrent threads of 19 | * execution available. 20 | * 21 | * The user may override the number of CPUs to use by using the 22 | * environment variable COUCHBASE_CPU_COUNT 23 | * 24 | * @throws std::logic_error if the environemnt variable can't be parsed 25 | * std::runtime_error if the system call to fetch available CPUs fail 26 | */ 27 | size_t get_available_cpu_count(); 28 | 29 | /** 30 | * Returns the number of logical threads (CPUs) this process has 31 | * access to - i.e. the maximum number of concurrent threads of 32 | * execution available. 33 | * 34 | * @throws std::runtime_error if the system call to fetch available CPUs fail 35 | */ 36 | size_t get_cpu_count(); 37 | 38 | /** 39 | * Get a (potentially cached) stripe index for the current core. 40 | * One or more cores may be mapped to a given stripe; if numStripes 41 | * is equal to the number of cores they will be mapped 1-to-1, if there 42 | * are fewer stripes than cores, multiple cores will share a stripe. 43 | * See folly::AccessSpreader for core to stripe allocation logic. 44 | * 45 | * @param numStripes number of stripes 46 | * @return stripe index for the current cpu core. 47 | */ 48 | size_t stripe_for_current_cpu(size_t numStripes); 49 | 50 | /** 51 | * Get the number of last level caches in the system. 52 | */ 53 | size_t get_num_last_level_cache(); 54 | } 55 | 56 | // For backwards compatibility 57 | namespace Couchbase { 58 | inline size_t get_available_cpu_count() { 59 | return cb::get_available_cpu_count(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /include/platform/terminal_color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb::terminal { 17 | enum class TerminalColor { 18 | Black, 19 | Red, 20 | Green, 21 | Yellow, 22 | Blue, 23 | Magenta, 24 | Cyan, 25 | White, 26 | Reset 27 | }; 28 | 29 | /// Enable/disable support for using colors. When enabled the << operator 30 | /// will print out the escape sequence to set the font color. When disabled 31 | /// the operator don't insert anything to the stream. 32 | void setTerminalColorSupport(bool enable); 33 | 34 | /// Is terminal color currently enabled 35 | bool isTerminalColorEnabled(); 36 | 37 | /// Insert the formatting code the requested color to the stream 38 | std::ostream& operator<<(std::ostream& os, const TerminalColor& color); 39 | } // namespace cb::terminal 40 | 41 | template <> 42 | struct fmt::formatter : ostream_formatter {}; 43 | -------------------------------------------------------------------------------- /include/platform/terminal_size.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | namespace cb::terminal { 17 | /** 18 | * Get the current terminal width and height 19 | * 20 | * @return width and height 21 | * @throws std::system_error if an error occurs 22 | */ 23 | std::pair getTerminalSize(); 24 | } // namespace cb::terminal 25 | -------------------------------------------------------------------------------- /include/platform/uuid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace cb::uuid { 18 | 19 | using uuid_t = boost::uuids::uuid; 20 | 21 | /** 22 | * Generate a new random uuid and return it 23 | */ 24 | uuid_t random(); 25 | 26 | /** 27 | * Convert a textual version of a UUID to a uuid type 28 | * @throw std::invalid_argument if the textual uuid is not 29 | * formatted correctly 30 | */ 31 | uuid_t from_string(std::string_view str); 32 | } // namespace cb::uuid 33 | 34 | /** 35 | * Print a textual version of the UUID in the form: 36 | * 37 | * 00000000-0000-0000-0000-000000000000 38 | */ 39 | std::string to_string(const cb::uuid::uuid_t& uuid); 40 | -------------------------------------------------------------------------------- /include/win32/getopt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #pragma once 11 | 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define no_argument 0 18 | #define required_argument 1 19 | #define optional_argument 2 20 | 21 | struct option { 22 | const char* name; 23 | int has_arg; 24 | int* flag; 25 | int val; 26 | }; 27 | 28 | extern char* optarg; 29 | extern int opterr; 30 | extern int optind; 31 | extern int optopt; 32 | 33 | extern int getopt_long(int argc, 34 | char* const* argv, 35 | const char* optstring, 36 | const struct option* longopts, 37 | int* longindex); 38 | 39 | extern int getopt(int argc, char* const* argv, const char* optstring); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | -------------------------------------------------------------------------------- /licenses/BSD-3-Clause-Julian-Seward.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2000-2013 Julian Seward. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions 5 | are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. The origin of this software must not be misrepresented; you must 11 | not claim that you wrote the original software. If you use this 12 | software in a product, an acknowledgment in the product 13 | documentation would be appreciated but is not required. 14 | 15 | 3. Altered source versions must be plainly marked as such, and must 16 | not be misrepresented as being the original software. 17 | 18 | 4. The name of the author may not be used to endorse or promote 19 | products derived from this software without specific prior written 20 | permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 23 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 26 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 28 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /licenses/MIT-JSON.org.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005 JSON.org 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | The Software shall be used for Good, not Evil. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /licenses/ZLIB-Mark-Adler.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Mark Adler 2 | Version 1.1 1 Aug 2013 Mark Adler 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the author be held liable for any damages 6 | arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any purpose, 9 | including commercial applications, and to alter it and redistribute it 10 | freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must not 13 | claim that you wrote the original software. If you use this software 14 | in a product, an acknowledgment in the product documentation would be 15 | appreciated but is not required. 16 | 2. Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 3. This notice may not be removed or altered from any source distribution. 19 | 20 | Mark Adler 21 | madler@alumni.caltech.edu 22 | -------------------------------------------------------------------------------- /precompiled_headers.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | // This file is required to build the precompiled header object libraries 12 | // (platform_pch et. al.) because object libraries need some source to compile. 13 | 14 | // To silence linker errors when we attempt to link an empty file, 15 | // define a totally unused symbol: 16 | // ranlib: file: platform/libplatform.a(precompiled_headers.cc.o) has no 17 | // symbols 18 | void platform_precompiled_headers_dummy_symbol() { 19 | return; 20 | } 21 | -------------------------------------------------------------------------------- /precompiled_headers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #pragma once 12 | 13 | // Used by most unit test files. 14 | #include 15 | 16 | // Used throughout the codebase 17 | #include 18 | 19 | #include 20 | #include 21 | -------------------------------------------------------------------------------- /src/awaitable_semaphore.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2022-Present Couchbase, Inc. 4 | * 5 | * Use of this software is governed by the Business Source License included 6 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 7 | * in that file, in accordance with the Business Source License, use of this 8 | * software will be governed by the Apache License, Version 2.0, included in 9 | * the file licenses/APL2.txt. 10 | */ 11 | 12 | #include 13 | 14 | namespace cb { 15 | 16 | void AwaitableSemaphore::release(size_t count) { 17 | signalWaiters(count); 18 | } 19 | 20 | bool AwaitableSemaphore::acquire_or_wait(std::weak_ptr waiter) { 21 | auto wh = waiters.lock(); 22 | if (try_acquire()) { 23 | // token was available and has been acquired. 24 | // If the waiter is already queued for notification, remove it. 25 | // If we did not, a later release() could notify it, even though 26 | // it already has a token. 27 | wh->erase(waiter); 28 | return true; 29 | } 30 | 31 | wh->pushUnique(waiter); 32 | return false; 33 | } 34 | 35 | std::vector> AwaitableSemaphore::getWaiters() { 36 | return waiters.lock()->getWaiters(); 37 | } 38 | 39 | void AwaitableSemaphore::signalWaiters(size_t count) { 40 | std::vector> selectedWaiters; 41 | 42 | { 43 | auto waitersHandle = waiters.lock(); 44 | Semaphore::release(count); 45 | while (count && !waitersHandle->empty()) { 46 | auto waiter = waitersHandle->pop().lock(); 47 | if (waiter) { 48 | selectedWaiters.push_back(waiter); 49 | count--; 50 | } 51 | } 52 | } 53 | 54 | // signal the waiters outside of the lock. 55 | // signal could potentially acquire other locks. 56 | for (auto& waiter : selectedWaiters) { 57 | waiter->signal(); 58 | } 59 | } 60 | 61 | } // namespace cb -------------------------------------------------------------------------------- /src/base64.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | static std::string encode_impl(std::string_view source, bool uri) { 16 | std::string buffer; 17 | buffer.resize(simdutf::base64_length_from_binary(source.size())); 18 | auto nbutes = simdutf::binary_to_base64( 19 | source.data(), 20 | source.size(), 21 | buffer.data(), 22 | uri ? simdutf::base64_url : simdutf::base64_default); 23 | buffer.resize(nbutes); 24 | return buffer; 25 | } 26 | 27 | static std::string decode_impl(std::string_view blob, bool uri) { 28 | std::string buffer; 29 | buffer.resize(simdutf::maximal_binary_length_from_base64(blob.data(), 30 | blob.size())); 31 | simdutf::result r = simdutf::base64_to_binary( 32 | blob.data(), 33 | blob.size(), 34 | buffer.data(), 35 | uri ? simdutf::base64_url : simdutf::base64_default, 36 | uri ? simdutf::loose : simdutf::strict); 37 | if (r.error) { 38 | throw std::invalid_argument("cb::base64::decode invalid input"); 39 | } 40 | buffer.resize( 41 | r.count); // resize the buffer according to actual number of bytes 42 | return buffer; 43 | } 44 | 45 | namespace cb::base64 { 46 | std::string encode(std::string_view view) { 47 | return encode_impl(view, false); 48 | } 49 | 50 | std::string decode(std::string_view blob) { 51 | return decode_impl(blob, false); 52 | } 53 | } // namespace cb::base64 54 | 55 | namespace cb::base64url { 56 | std::string encode(std::string_view source) { 57 | return encode_impl(source, true); 58 | } 59 | 60 | std::string decode(std::string_view source) { 61 | return decode_impl(source, true); 62 | } 63 | } // namespace cb::base64url 64 | -------------------------------------------------------------------------------- /src/byte_buffer_dump.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | 13 | static void printByte(uint8_t b, std::ostream& out) { 14 | uint32_t val = b & 0xff; 15 | if (b < 0x10) { 16 | out << " 0x0"; 17 | } else { 18 | out << " 0x"; 19 | } 20 | 21 | out.flags(std::ios::hex); 22 | out << val; 23 | out.flags(std::ios::dec); 24 | 25 | if (isprint((int)b)) { 26 | out << " ('" << (char)b << "')"; 27 | } else { 28 | out << " "; 29 | } 30 | out << " |"; 31 | } 32 | 33 | static void dump(std::ostream& out, const cb::const_byte_buffer buffer) { 34 | out << std::endl; 35 | out << 36 | " Byte/ 0 | 1 | 2 | 3 |" << 37 | std::endl; 38 | out << 39 | " / | | | |" << 40 | std::endl; 41 | out << 42 | " |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|"; 43 | 44 | size_t ii = 0; 45 | for (; ii < buffer.size(); ++ii) { 46 | if (ii % 4 == 0) { 47 | out << std::endl; 48 | out << 49 | " +---------------+---------------+---------------+---------------+" << 50 | std::endl; 51 | out.setf(std::ios::right); 52 | out << std::setw(8) << ii << "|"; 53 | out.setf(std::ios::fixed); 54 | } 55 | printByte(buffer[ii], out); 56 | } 57 | 58 | out << std::endl; 59 | if (ii % 4 != 0) { 60 | out << " "; 61 | for (size_t jj = 0; jj < ii % 4; ++jj) { 62 | out << "+---------------"; 63 | } 64 | out << "+" << std::endl; 65 | } else { 66 | out << " +---------------+---------------+---------------+---------------+" 67 | << std::endl; 68 | } 69 | } 70 | 71 | std::ostream& operator<<(std::ostream& out, const cb::byte_buffer& buffer) { 72 | cb::const_byte_buffer buf { buffer.data(), buffer.size() }; 73 | dump(out, buf); 74 | return out; 75 | } 76 | 77 | std::ostream& operator<<(std::ostream& out, const cb::const_byte_buffer& buffer) { 78 | dump(out, buffer); 79 | return out; 80 | } 81 | -------------------------------------------------------------------------------- /src/cb_arena_malloc.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | namespace cb { 18 | 19 | std::ostream& operator<<(std::ostream& os, const MemoryDomain& md) { 20 | switch (md) { 21 | case MemoryDomain::Primary: 22 | return os << "Primary"; 23 | case MemoryDomain::Secondary: 24 | return os << "Secondary"; 25 | case MemoryDomain::None: 26 | return os << "None"; 27 | } 28 | folly::assume_unreachable(); 29 | } 30 | 31 | ArenaMallocGuard::ArenaMallocGuard(const ArenaMallocClient& client) { 32 | ArenaMalloc::switchToClient(client); 33 | } 34 | 35 | ArenaMallocGuard::~ArenaMallocGuard() { 36 | ArenaMalloc::switchFromClient(); 37 | } 38 | 39 | NoArenaGuard::NoArenaGuard() : previous(ArenaMalloc::switchFromClient()) { 40 | } 41 | 42 | NoArenaGuard::~NoArenaGuard() { 43 | ArenaMalloc::switchToClient(previous); 44 | } 45 | 46 | void ArenaMallocClient::setEstimateUpdateThreshold(size_t maxDataSize, 47 | float percentage) { 48 | // Set the threshold to the smaller of the % calculation and max u32 49 | estimateUpdateThreshold = uint32_t(std::min( 50 | size_t(std::numeric_limits::max()), 51 | size_t(maxDataSize * (percentage / 100.0) / cb::get_cpu_count()))); 52 | } 53 | 54 | std::ostream& operator<<(std::ostream& os, const FragmentationStats& stats) { 55 | os << "allocated:" << stats.getAllocatedBytes() 56 | << ", resident:" << stats.getResidentBytes() 57 | << ", fragmentation:" << stats.getFragmentationRatio(); 58 | return os; 59 | } 60 | 61 | #if defined(HAVE_JEMALLOC) 62 | template class _ArenaMalloc; 63 | #else 64 | template class _ArenaMalloc; 65 | #endif 66 | 67 | } // namespace cb 68 | -------------------------------------------------------------------------------- /src/cb_pthreads.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | void cb_rw_lock_initialize(cb_rwlock_t *rw) 19 | { 20 | int rv = pthread_rwlock_init(rw, nullptr); 21 | if (rv != 0) { 22 | throw std::system_error(rv, std::system_category(), 23 | "Failed to initialize rw lock"); 24 | } 25 | } 26 | 27 | void cb_rw_lock_destroy(cb_rwlock_t *rw) 28 | { 29 | int rv = pthread_rwlock_destroy(rw); 30 | if (rv != 0) { 31 | throw std::system_error(rv, std::system_category(), 32 | "Failed to destroy rw lock"); 33 | } 34 | } 35 | 36 | int cb_rw_reader_enter(cb_rwlock_t *rw) 37 | { 38 | int result = pthread_rwlock_rdlock(rw); 39 | if (result != 0) { 40 | auto err = cb_strerror(result); 41 | fprintf(stderr, "pthread_rwlock_rdlock returned %d (%s)\n", 42 | result, err.c_str()); 43 | } 44 | return result; 45 | } 46 | 47 | int cb_rw_reader_exit(cb_rwlock_t *rw) 48 | { 49 | int result = pthread_rwlock_unlock(rw); 50 | if (result != 0) { 51 | auto err = cb_strerror(result); 52 | fprintf(stderr, "pthread_rwlock_unlock returned %d (%s)\n", 53 | result, err.c_str()); 54 | } 55 | return result; 56 | } 57 | 58 | int cb_rw_writer_enter(cb_rwlock_t *rw) 59 | { 60 | int result = pthread_rwlock_wrlock(rw); 61 | if (result != 0) { 62 | auto err = cb_strerror(result); 63 | fprintf(stderr, "pthread_rwlock_wrlock returned %d (%s)\n", 64 | result, err.c_str()); 65 | } 66 | return result; 67 | } 68 | 69 | int cb_rw_writer_exit(cb_rwlock_t *rw) 70 | { 71 | int result = pthread_rwlock_unlock(rw); 72 | if (result != 0) { 73 | auto err = cb_strerror(result); 74 | fprintf(stderr, "pthread_rwlock_unlock returned %d (%s)\n", 75 | result, err.c_str()); 76 | } 77 | return result; 78 | } 79 | -------------------------------------------------------------------------------- /src/cb_win32.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | void cb_rw_lock_initialize(cb_rwlock_t* rw) { 21 | InitializeSRWLock(rw); 22 | } 23 | 24 | void cb_rw_lock_destroy(cb_rwlock_t* rw) { 25 | (void)rw; 26 | // Nothing todo on windows 27 | } 28 | 29 | int cb_rw_reader_enter(cb_rwlock_t* rw) { 30 | AcquireSRWLockShared(rw); 31 | return 0; 32 | } 33 | 34 | int cb_rw_reader_exit(cb_rwlock_t* rw) { 35 | ReleaseSRWLockShared(rw); 36 | return 0; 37 | } 38 | 39 | int cb_rw_writer_enter(cb_rwlock_t* rw) { 40 | AcquireSRWLockExclusive(rw); 41 | return 0; 42 | } 43 | 44 | int cb_rw_writer_exit(cb_rwlock_t* rw) { 45 | ReleaseSRWLockExclusive(rw); 46 | return 0; 47 | } 48 | 49 | // Wrapper into cb::getopt (which we now unit tests on all platforms) 50 | char* optarg; 51 | int opterr; 52 | int optind = 1; 53 | int optopt; 54 | 55 | int getopt_long(int argc, 56 | char* const* argv, 57 | const char* optstring, 58 | const struct option* longopts, 59 | int* longindex) { 60 | cb::getopt::optind = optind; 61 | cb::getopt::opterr = opterr; 62 | cb::getopt::optopt = optopt; 63 | auto ret = cb::getopt::getopt_long( 64 | argc, 65 | argv, 66 | optstring, 67 | reinterpret_cast(longopts), 68 | longindex); 69 | optarg = cb::getopt::optarg; 70 | opterr = cb::getopt::opterr; 71 | optind = cb::getopt::optind; 72 | optopt = cb::getopt::optopt; 73 | return ret; 74 | } 75 | 76 | int getopt(int argc, char* const* argv, const char* optstring) { 77 | cb::getopt::optind = optind; 78 | cb::getopt::opterr = opterr; 79 | cb::getopt::optopt = optopt; 80 | auto ret = cb::getopt::getopt(argc, argv, optstring); 81 | optarg = cb::getopt::optarg; 82 | opterr = cb::getopt::opterr; 83 | optind = cb::getopt::optind; 84 | optopt = cb::getopt::optopt; 85 | return ret; 86 | } 87 | -------------------------------------------------------------------------------- /src/cbassert.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #if defined(WIN32) 18 | #include 19 | #endif 20 | 21 | void cb_assert_die(const char* expression, const char* file, int line) { 22 | fprintf(stderr, "assertion failed [%s] at %s:%u\n", expression, file, line); 23 | fprintf(stderr, "Called from:\n"); 24 | print_backtrace_to_file(stderr); 25 | fflush(stderr); 26 | std::abort(); 27 | } 28 | 29 | #if defined(WIN32) 30 | int backtraceReportHook(int reportType, char* message, int* returnValue) { 31 | fprintf(stderr, message); 32 | fprintf(stderr, "Called from:\n"); 33 | print_backtrace_to_file(stderr); 34 | return FALSE; 35 | } 36 | 37 | void setupWindowsDebugCRTAssertHandling() { 38 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW); 39 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); 40 | _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW); 41 | _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); 42 | _CrtSetReportHook(backtraceReportHook); 43 | } 44 | #else 45 | void setupWindowsDebugCRTAssertHandling() { 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /src/checked_snprintf.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int checked_snprintf(char* str, const size_t size, const char* format, ...) { 18 | if (str == nullptr) { 19 | throw std::invalid_argument( 20 | "checked_snprintf: destination buffer can't be NULL"); 21 | } 22 | 23 | if (size == 0) { 24 | throw std::invalid_argument( 25 | "checked_snprintf: destination buffersize can't be 0"); 26 | } 27 | 28 | va_list ap; 29 | va_start(ap, format); 30 | int ret = vsnprintf(str, size, format, ap); 31 | va_end(ap); 32 | 33 | if (ret < 0) { 34 | // unfortunately this isn't always the case.. but we can't 35 | // determine the difference on windows so we'll just treat it as an 36 | // overflow error... 37 | throw std::overflow_error( 38 | "checked_snprintf: Destination buffer too small."); 39 | } 40 | 41 | if (size_t(ret) >= size) { 42 | std::string msg("checked_snprintf: Destination buffer too small. ("); 43 | msg += std::to_string(ret); 44 | msg += " >= "; 45 | msg += std::to_string(size); 46 | msg += ")"; 47 | throw std::overflow_error(msg); 48 | } 49 | 50 | return ret; 51 | } 52 | -------------------------------------------------------------------------------- /src/crc32c_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | // 12 | // crc32c_private - constants/functions etc... 13 | // required by software and hardware implementations 14 | // 15 | 16 | #pragma once 17 | #include 18 | 19 | const uintptr_t ALIGN64_MASK = sizeof(uint64_t)-1; 20 | const int LONG_BLOCK = 8192; 21 | const int SHORT_BLOCK = 256; 22 | const int SHIFT_TABLE_X = 4, SHIFT_TABLE_Y = 256; 23 | extern uint32_t crc32c_long[SHIFT_TABLE_X][SHIFT_TABLE_Y]; 24 | extern uint32_t crc32c_short[SHIFT_TABLE_X][SHIFT_TABLE_Y]; 25 | 26 | /* Apply the zeros operator table to crc. */ 27 | inline uint32_t crc32c_shift(uint32_t zeros[SHIFT_TABLE_X][SHIFT_TABLE_Y], 28 | uint32_t crc) { 29 | return zeros[0][crc & 0xff] ^ zeros[1][(crc >> 8) & 0xff] ^ 30 | zeros[2][(crc >> 16) & 0xff] ^ zeros[3][crc >> 24]; 31 | } 32 | -------------------------------------------------------------------------------- /src/exceptions.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | namespace cb { 17 | 18 | struct tag_stacktrace; 19 | using traced = 20 | boost::error_info; 21 | 22 | template 23 | FOLLY_NOINLINE void throwWithTrace(const T& exception) { 24 | throw boost::enable_error_info(exception) 25 | << traced(boost::stacktrace::stacktrace()); 26 | } 27 | 28 | // Add required explicit instantiations of throwWithTrace here - 29 | // will need one per exception type thrown via throwWithTrace(): 30 | template void throwWithTrace(const std::underflow_error&); 31 | template void throwWithTrace(const std::logic_error&); 32 | template void throwWithTrace(const std::runtime_error&); 33 | 34 | template 35 | const boost::stacktrace::stacktrace* getBacktrace(T& exception) { 36 | return boost::get_error_info(exception); 37 | } 38 | 39 | // Add required explicit instantiations of getBacktrace here - 40 | // will need one per exception type passed to getBacktrace(): 41 | using boost_st = boost::stacktrace::stacktrace; 42 | 43 | template const boost_st* getBacktrace(const std::exception& exception); 44 | template const boost_st* getBacktrace(const std::underflow_error& exception); 45 | 46 | } // namespace cb 47 | -------------------------------------------------------------------------------- /src/getpass.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | #ifdef WIN32 16 | #include 17 | #define isatty(a) true 18 | #else 19 | #include 20 | #include 21 | #endif 22 | 23 | static void setEcho(bool enable) { 24 | #ifdef WIN32 25 | HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE); 26 | DWORD mode; 27 | GetConsoleMode(stdinHandle, &mode); 28 | 29 | if (!enable) { 30 | mode &= ~ENABLE_ECHO_INPUT; 31 | } else { 32 | mode |= ENABLE_ECHO_INPUT; 33 | } 34 | 35 | SetConsoleMode(stdinHandle, mode); 36 | #else 37 | struct termios tty {}; 38 | tcgetattr(STDIN_FILENO, &tty); 39 | // ECHO is defined as a signed number and we get a warning by using 40 | // signed variables when doing bit manipulations 41 | const auto echoflag = tcflag_t(ECHO); 42 | if (!enable) { 43 | tty.c_lflag &= ~echoflag; 44 | } else { 45 | tty.c_lflag |= echoflag; 46 | } 47 | 48 | (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty); 49 | #endif 50 | } 51 | 52 | std::string cb::getpass(std::string_view prompt) { 53 | std::string password; 54 | if (isatty(STDIN_FILENO)) { 55 | std::cerr << prompt << std::flush; 56 | setEcho(false); 57 | std::getline(std::cin, password); 58 | setEcho(true); 59 | 60 | std::cout << std::endl; 61 | } else { 62 | std::getline(std::cin, password); 63 | } 64 | 65 | return password; 66 | } 67 | -------------------------------------------------------------------------------- /src/interrupt.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #ifdef WIN32 16 | #include 17 | #else 18 | #include 19 | #endif 20 | 21 | namespace cb::console { 22 | 23 | static std::atomic sigint_handler{nullptr}; 24 | 25 | #ifdef WIN32 26 | static bool WINAPI HandlerRoutine(_In_ DWORD signal) { 27 | if (signal == CTRL_C_EVENT) { 28 | auto si_handler = sigint_handler.load(); 29 | if (si_handler) { 30 | si_handler(); 31 | } 32 | return true; 33 | } 34 | 35 | return false; 36 | } 37 | #else 38 | static void sigint_handler_wrapper(int s) { 39 | auto si_handler = sigint_handler.load(); 40 | if (si_handler) { 41 | si_handler(); 42 | } 43 | } 44 | #endif 45 | 46 | void set_sigint_handler(interrupt_handler h) { 47 | bool initialised = sigint_handler.load() != nullptr; 48 | sigint_handler = h; 49 | 50 | if (!initialised) { 51 | #ifdef WIN32 52 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)HandlerRoutine, true)) { 53 | throw std::system_error(GetLastError(), 54 | std::system_category(), 55 | "cb::console::set_sigint_handler: Couldn't " 56 | "register ConsoleCtrlHandler"); 57 | } 58 | #else 59 | // Register our SIGINT handler 60 | struct sigaction sigIntHandler {}; 61 | sigIntHandler.sa_handler = sigint_handler_wrapper; 62 | sigemptyset(&sigIntHandler.sa_mask); 63 | sigIntHandler.sa_flags = 0; 64 | if (sigaction(SIGINT, &sigIntHandler, nullptr) != 0) { 65 | throw std::system_error(errno, 66 | std::system_category(), 67 | "cb::console::set_sigint_handler: Failed " 68 | "to register sigaction"); 69 | } 70 | #endif 71 | } 72 | } 73 | 74 | void clear_sigint_handler() { 75 | sigint_handler = nullptr; 76 | } 77 | } // namespace cb::console 78 | -------------------------------------------------------------------------------- /src/je_malloc_conf.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | /* jemalloc checks for this symbol, and it's contents for the config to use. */ 14 | const char* je_malloc_conf = 15 | /* Enable background worker thread for asynchronous purging. 16 | * Background threads are non-functional in jemalloc 5.1.0 on macOS due to 17 | * implementation discrepancies between the background threads and mutexes. 18 | */ 19 | #ifndef __APPLE__ 20 | "background_thread:true," 21 | #endif 22 | /* Use just one arena, instead of the default based on number of CPUs. 23 | Helps to minimize heap fragmentation. */ 24 | "narenas:1" 25 | #ifdef __linux__ 26 | /* Start with profiling enabled but inactive; this allows us to 27 | turn it on/off at runtime. */ 28 | ",prof:true,prof_active:false" 29 | #endif 30 | ; 31 | -------------------------------------------------------------------------------- /src/processclock.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | std::chrono::steady_clock::time_point cb::DefaultProcessClockSource::now() { 14 | return std::chrono::steady_clock::now(); 15 | } 16 | 17 | static cb::DefaultProcessClockSource clockSource; 18 | 19 | cb::DefaultProcessClockSource& cb::defaultProcessClockSource() { 20 | return clockSource; 21 | } 22 | 23 | std::chrono::nanoseconds cb::to_ns_since_epoch( 24 | const std::chrono::steady_clock::time_point& tp) { 25 | return std::chrono::duration_cast( 26 | tp.time_since_epoch()); 27 | } -------------------------------------------------------------------------------- /src/save_file.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | namespace cb::io { 15 | void saveFile(const std::filesystem::path& path, 16 | std::string_view content, 17 | std::ios_base::openmode mode) { 18 | std::ofstream file; 19 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit); 20 | file.open(path.string().c_str(), mode); 21 | file.write(content.data(), content.size()); 22 | file.flush(); 23 | file.close(); 24 | } 25 | 26 | bool saveFile(const std::filesystem::path& path, 27 | std::string_view content, 28 | std::error_code& ec, 29 | std::ios_base::openmode mode) noexcept { 30 | try { 31 | saveFile(path, content, mode); 32 | } catch (const std::system_error& e) { 33 | ec = e.code(); 34 | return false; 35 | } catch (const std::bad_alloc&) { 36 | ec = std::make_error_code(std::errc::not_enough_memory); 37 | return false; 38 | } catch (const std::exception&) { 39 | // This isn't exactly right, but good enough for now 40 | ec = std::make_error_code(std::errc::io_error); 41 | return false; 42 | } 43 | return true; 44 | } 45 | } // namespace cb::io -------------------------------------------------------------------------------- /src/semaphore.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace cb { 18 | 19 | Semaphore::Semaphore(size_t numTokens) 20 | : capacity(numTokens), tokens(numTokens) { 21 | if (!capacity) { 22 | throw std::invalid_argument("Semaphore capacity should be non-zero"); 23 | } 24 | } 25 | 26 | void Semaphore::release(size_t count) { 27 | Expects(count != 0); 28 | tokens.fetch_add(count, std::memory_order_acq_rel); 29 | } 30 | 31 | bool Semaphore::try_acquire(size_t count) { 32 | Expects(count != 0); 33 | 34 | ssize_t availableTokens = tokens.load(std::memory_order_acquire); 35 | ssize_t desired; 36 | 37 | do { 38 | if (ssize_t(count) > availableTokens) { 39 | return false; 40 | } 41 | 42 | desired = availableTokens - ssize_t(count); 43 | } while (!tokens.compare_exchange_weak( 44 | availableTokens, 45 | desired, 46 | std::memory_order_release /* success */, 47 | std::memory_order_acquire /* failure */)); 48 | return true; 49 | } 50 | 51 | void Semaphore::setCapacity(size_t newCapacity) { 52 | if (!newCapacity) { 53 | throw std::invalid_argument( 54 | "Semaphore::setCapacity newCapacity should be greater than " 55 | "zero"); 56 | } 57 | auto newValue = newCapacity; 58 | auto oldValue = capacity.exchange(newValue); 59 | auto delta = ssize_t(newCapacity) - ssize_t(oldValue); 60 | if (delta > 0) { 61 | // capacity has increased, release some "new" tokens 62 | release(delta); 63 | } else if (delta < 0) { 64 | // capacity has decreased, throw away some tokens 65 | // this might make tokens temporarily negative, but that's 66 | // okay. Once all outstanding users release, tokens == newCapacity 67 | tokens.fetch_add(delta); 68 | } 69 | } 70 | 71 | } // end namespace cb -------------------------------------------------------------------------------- /src/sized_buffer.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | std::string cb::to_string(cb::char_buffer cb) { 14 | return std::string{cb.data(), cb.size()}; 15 | } 16 | -------------------------------------------------------------------------------- /src/split_string.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | std::vector cb::string::split(std::string_view s, 14 | char delim, 15 | bool allowEmpty) { 16 | std::vector result; 17 | while (true) { 18 | const auto m = s.find(delim); 19 | if (m == std::string_view::npos) { 20 | break; 21 | } 22 | 23 | result.emplace_back(s.data(), m); 24 | s.remove_prefix(m + 1); 25 | if (!allowEmpty) { 26 | while (s.front() == delim) { 27 | s.remove_prefix(1); 28 | } 29 | } 30 | } 31 | if (!s.empty()) { 32 | result.emplace_back(s); 33 | } 34 | return result; 35 | } 36 | -------------------------------------------------------------------------------- /src/strerror.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #ifdef WIN32 13 | #include 14 | #else 15 | #include 16 | #include 17 | #endif 18 | 19 | std::string cb_strerror() { 20 | #ifdef WIN32 21 | return cb_strerror(GetLastError()); 22 | #else 23 | return cb_strerror(errno); 24 | #endif 25 | } 26 | 27 | std::string cb_strerror(cb_os_error_t error) 28 | { 29 | #ifdef WIN32 30 | std::string reason; 31 | char *win_msg = NULL; 32 | if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 33 | FORMAT_MESSAGE_FROM_SYSTEM | 34 | FORMAT_MESSAGE_IGNORE_INSERTS, 35 | NULL, error, 36 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 37 | (LPTSTR)&win_msg, 38 | 0, NULL) > 0) { 39 | reason.assign(win_msg); 40 | LocalFree(win_msg); 41 | } else { 42 | reason = std::string("Windows error: ") + std::to_string(error); 43 | } 44 | return reason; 45 | #else 46 | return std::string(strerror(error)); 47 | #endif 48 | } 49 | -------------------------------------------------------------------------------- /src/string_utilities.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | std::string cb::size2human(std::size_t value, 15 | const std::optional& suffix) { 16 | const std::array size_suffix{{"", "k", "M", "G", "T", "P"}}; 17 | std::size_t index = 0; 18 | while (value > 10240 && index < (size_suffix.size() - 1)) { 19 | value /= 1024; 20 | ++index; 21 | } 22 | return fmt::format("{}{}{}", 23 | value, 24 | size_suffix[index], 25 | suffix.has_value() ? *suffix : ""); 26 | } 27 | -------------------------------------------------------------------------------- /src/terminal_color.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | namespace cb::terminal { 16 | std::atomic_bool color_supported{ 17 | #ifdef WIN32 18 | false 19 | #else 20 | true 21 | #endif 22 | }; 23 | 24 | void setTerminalColorSupport(bool enable) { 25 | color_supported.store(enable); 26 | } 27 | 28 | bool isTerminalColorEnabled() { 29 | return color_supported; 30 | } 31 | 32 | static std::string to_string(TerminalColor color) { 33 | if (!color_supported) { 34 | return ""; 35 | } 36 | switch (color) { 37 | case TerminalColor::Black: 38 | return "\033[30m"; 39 | case TerminalColor::Red: 40 | return "\033[31m"; 41 | case TerminalColor::Green: 42 | return "\033[32m"; 43 | case TerminalColor::Yellow: 44 | return "\033[33m"; 45 | case TerminalColor::Blue: 46 | return "\033[34m"; 47 | case TerminalColor::Magenta: 48 | return "\033[35m"; 49 | case TerminalColor::Cyan: 50 | return "\033[36m"; 51 | case TerminalColor::White: 52 | return "\033[37m"; 53 | case TerminalColor::Reset: 54 | return "\033[m"; 55 | } 56 | throw std::invalid_argument( 57 | "to_string(TerminalColor color): Invalid color: " + 58 | std::to_string(int(color))); 59 | } 60 | 61 | std::ostream& operator<<(std::ostream& os, const TerminalColor& color) { 62 | os << to_string(color); 63 | return os; 64 | } 65 | } // namespace cb::terminal 66 | -------------------------------------------------------------------------------- /src/terminal_size.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | #ifdef WIN32 16 | #include 17 | 18 | std::pair cb::terminal::getTerminalSize() { 19 | CONSOLE_SCREEN_BUFFER_INFO csbi; 20 | 21 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { 22 | return {csbi.srWindow.Right - csbi.srWindow.Left + 1, 23 | csbi.srWindow.Bottom - csbi.srWindow.Top + 1}; 24 | } 25 | throw std::system_error(GetLastError(), 26 | std::system_category(), 27 | "getTerminalSize(): ioctl failed"); 28 | } 29 | #else 30 | #include 31 | #include 32 | #include 33 | 34 | std::pair cb::terminal::getTerminalSize() { 35 | if (isatty(fileno(stdout))) { 36 | winsize winsize; 37 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == -1) { 38 | throw std::system_error(errno, 39 | std::system_category(), 40 | "getTerminalSize(): ioctl failed"); 41 | } 42 | return {winsize.ws_col, winsize.ws_row}; 43 | } 44 | // Not a tty 45 | return {std::numeric_limits::max(), 46 | std::numeric_limits::max()}; 47 | } 48 | #endif 49 | -------------------------------------------------------------------------------- /src/thread_local_monotonic_resource.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | namespace cb { 17 | 18 | MonotonicBufferResource::MonotonicBufferResource(size_t initialSize, 19 | size_t maxSize) 20 | : maxSize(maxSize) { 21 | cb::NoArenaGuard guard; 22 | initialBuffer.resize(initialSize); 23 | resource = std::make_unique( 24 | initialBuffer.data(), initialBuffer.size()); 25 | } 26 | 27 | MonotonicBufferResource::~MonotonicBufferResource() { 28 | cb::NoArenaGuard guard; 29 | resource.reset(); 30 | std::exchange(initialBuffer, {}); 31 | } 32 | 33 | void* MonotonicBufferResource::allocate(size_t bytes) { 34 | if (allocatedBytes + bytes > maxSize) { 35 | throw std::bad_alloc(); 36 | } 37 | 38 | cb::NoArenaGuard guard; 39 | void* ptr = resource->allocate(bytes); 40 | if (!ptr) { 41 | throw std::bad_alloc(); 42 | } 43 | 44 | allocatedBytes += bytes; 45 | ++allocationCount; 46 | 47 | maxAllocatedBytes = std::max(maxAllocatedBytes, allocatedBytes); 48 | maxAllocationCount = std::max(maxAllocationCount, allocationCount); 49 | 50 | return ptr; 51 | } 52 | 53 | void MonotonicBufferResource::deallocate(void* ptr, size_t size) { 54 | allocatedBytes -= size; 55 | --allocationCount; 56 | if (!allocatedBytes) { 57 | cb::NoArenaGuard guard; 58 | // Time to reset the memory resource. 59 | resource->release(); 60 | } 61 | } 62 | 63 | size_t MonotonicBufferResource::getAllocatedBytes() const { 64 | return allocatedBytes; 65 | } 66 | 67 | size_t MonotonicBufferResource::getAllocationCount() const { 68 | return allocationCount; 69 | } 70 | 71 | size_t MonotonicBufferResource::getMaxAllocatedBytes() const { 72 | return maxAllocatedBytes; 73 | } 74 | 75 | size_t MonotonicBufferResource::getMaxAllocationCount() const { 76 | return maxAllocationCount; 77 | } 78 | 79 | } // namespace cb 80 | -------------------------------------------------------------------------------- /src/unique_waiter_queue.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | namespace cb { 16 | 17 | void UniqueWaiterQueue::pushUnique(UniqueWaiterQueue::WaiterPtr waiter) { 18 | // try to insert into the set 19 | auto [itr, inserted] = waiterSet.insert(waiter); 20 | if (inserted) { 21 | // the waiter was not already in the set, queue 22 | queue.push_back(itr); 23 | } 24 | } 25 | 26 | void UniqueWaiterQueue::erase(const UniqueWaiterQueue::WaiterPtr& waiter) { 27 | // try to find the waiter the set 28 | auto itr = waiterSet.find(waiter); 29 | if (itr == waiterSet.end()) { 30 | // not in the queue, nothing to do 31 | return; 32 | } 33 | 34 | // remove it from the ordered queue 35 | queue.erase(std::remove(queue.begin(), queue.end(), itr), queue.end()); 36 | // and from the set 37 | waiterSet.erase(itr); 38 | } 39 | 40 | UniqueWaiterQueue::WaiterPtr UniqueWaiterQueue::pop() { 41 | if (empty()) { 42 | return {}; 43 | } 44 | // find the next waiter 45 | auto waiterItr = queue.front(); 46 | // remove it from the queue 47 | queue.pop_front(); 48 | auto waiter = *waiterItr; 49 | // remove it from the set 50 | waiterSet.erase(waiterItr); 51 | 52 | return waiter; 53 | } 54 | bool UniqueWaiterQueue::empty() const { 55 | return queue.empty(); 56 | } 57 | 58 | std::vector UniqueWaiterQueue::getWaiters() 59 | const { 60 | return {waiterSet.begin(), waiterSet.end()}; 61 | } 62 | 63 | Waiter::~Waiter() = default; 64 | 65 | } // namespace cb 66 | -------------------------------------------------------------------------------- /src/uuid.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | cb::uuid::uuid_t cb::uuid::random() { 17 | return boost::uuids::random_generator()(); 18 | } 19 | 20 | cb::uuid::uuid_t cb::uuid::from_string(std::string_view str) { 21 | try { 22 | return boost::lexical_cast(str); 23 | } catch (const boost::exception&) { 24 | // backwards compat to make sure we don't crash for different reasons 25 | throw std::invalid_argument( 26 | "cb::uuid::from_string: Failed to convert string"); 27 | } 28 | } 29 | 30 | std::string to_string(const cb::uuid::uuid_t& uuid) { 31 | return boost::uuids::to_string(uuid); 32 | } 33 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_SUBDIRECTORY(arena_malloc) 2 | add_subdirectory(benchmarks) 3 | ADD_SUBDIRECTORY(crc32) 4 | ADD_SUBDIRECTORY(histogram) 5 | ADD_SUBDIRECTORY(interrupt) 6 | add_subdirectory(memory_tracking_test) 7 | ADD_SUBDIRECTORY(rwlock) 8 | add_subdirectory(unit_tests) 9 | -------------------------------------------------------------------------------- /tests/arena_malloc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cb_add_test_executable(platform-arena_malloc-test arena_malloc_test.cc) 2 | TARGET_LINK_LIBRARIES(platform-arena_malloc-test PRIVATE 3 | GTest::gtest 4 | GTest::gtest_main 5 | platform 6 | platform_cb_malloc_arena 7 | ${MALLOC_LIBRARIES}) 8 | platform_enable_pch(platform-arena_malloc-test) 9 | ADD_TEST(platform-arena_malloc-test platform-arena_malloc-test) 10 | 11 | if (MEMORY_ALLOCATOR STREQUAL "jemalloc") 12 | cb_add_test_executable(platform-arena_tracking_bench 13 | arena_tracking_bench.cc) 14 | 15 | TARGET_LINK_LIBRARIES(platform-arena_tracking_bench PRIVATE 16 | benchmark::benchmark 17 | benchmark::benchmark_main 18 | GTest::gtest 19 | platform 20 | platform_cb_malloc_arena) 21 | platform_enable_pch(platform-arena_tracking_bench) 22 | endif() 23 | 24 | if (UNIX) 25 | cb_add_test_executable(platform-cb_malloc-test cb_malloc_default.cc) 26 | # This test must not link against platform_cb_malloc 27 | TARGET_LINK_LIBRARIES(platform-cb_malloc-test PRIVATE 28 | GTest::gtest 29 | GTest::gtest_main 30 | platform 31 | ${MALLOC_LIBRARIES}) 32 | platform_enable_pch(platform-cb_malloc-test) 33 | ADD_TEST(platform-cb_malloc-test platform-cb_malloc-test) 34 | endif() 35 | -------------------------------------------------------------------------------- /tests/arena_malloc/cb_malloc_default.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #if defined(HAVE_JEMALLOC) 13 | #include 14 | #endif 15 | 16 | #include 17 | 18 | // This program is not linked with the arenas library, so we expect cb_malloc 19 | // to return false for the is_using_arenas function 20 | TEST(CbMallocDefault, cb_malloc_is_not_using_arenas) { 21 | EXPECT_FALSE(cb_malloc_is_using_arenas()); 22 | } 23 | 24 | #if defined(HAVE_JEMALLOC) 25 | // This program was built with je_malloc available, so we expect that the 26 | // default cb_malloc will call down to jemalloc 27 | TEST(CbMallocDefault, cb_malloc_is_jemalloc) { 28 | // Grab the current allocated/deallocated values for this thread 29 | uint64_t allocated, deallocated; 30 | size_t len = sizeof(allocated); 31 | je_mallctl("thread.allocated", &allocated, &len, nullptr, 0); 32 | len = sizeof(deallocated); 33 | je_mallctl("thread.deallocated", &deallocated, &len, nullptr, 0); 34 | 35 | // do an allocation and check je_malloc allocated increases 36 | auto* p = cb_malloc(512); 37 | 38 | uint64_t allocated_1; 39 | len = sizeof(allocated_1); 40 | je_mallctl("thread.allocated", &allocated_1, &len, nullptr, 0); 41 | 42 | EXPECT_EQ(allocated_1, allocated + 512); 43 | 44 | // do a deallocation and check je_malloc deallocated increases 45 | cb_free(p); 46 | 47 | uint64_t deallocated_1; 48 | len = sizeof(deallocated_1); 49 | je_mallctl("thread.deallocated", &deallocated_1, &len, nullptr, 0); 50 | EXPECT_EQ(deallocated_1, deallocated + 512); 51 | } 52 | #endif -------------------------------------------------------------------------------- /tests/benchmarks/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cb_add_test_executable(platform_benchmarks base64_test_bench.cc json_checker_bench.cc json_log_bench.cc) 2 | target_link_libraries(platform_benchmarks PRIVATE 3 | benchmark::benchmark GTest::gtest platform JSON_checker) 4 | platform_enable_pch(platform_benchmarks) 5 | -------------------------------------------------------------------------------- /tests/benchmarks/base64_test_bench.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | static void BM_DecodeEmptyString(benchmark::State& state) { 15 | while (state.KeepRunning()) { 16 | cb::base64::decode(""); 17 | } 18 | } 19 | BENCHMARK(BM_DecodeEmptyString); 20 | 21 | static void BM_EncodeEmptyString(benchmark::State& state) { 22 | while (state.KeepRunning()) { 23 | cb::base64::encode({}); 24 | } 25 | } 26 | BENCHMARK(BM_EncodeEmptyString); 27 | 28 | static void BM_EncodeBlob(benchmark::State& state) { 29 | std::string buffer; 30 | buffer.resize(state.range(0)); 31 | while (state.KeepRunning()) { 32 | cb::base64::encode(buffer); 33 | } 34 | } 35 | 36 | BENCHMARK(BM_EncodeBlob)->RangeMultiplier(100)->Range(1, 100000); 37 | 38 | static void BM_DecodeBlob(benchmark::State& state) { 39 | std::string buffer; 40 | buffer.resize(state.range(0)); 41 | auto input = cb::base64::encode(buffer); 42 | 43 | while (state.KeepRunning()) { 44 | cb::base64::decode(input); 45 | } 46 | } 47 | 48 | BENCHMARK(BM_DecodeBlob)->RangeMultiplier(100)->Range(1, 100000); 49 | -------------------------------------------------------------------------------- /tests/benchmarks/json_log_bench.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static void BM_Logger_LogJson(benchmark::State& state) { 17 | while (state.KeepRunning()) { 18 | cb::logger::Json x{{"pi", 3.141}, 19 | {"happy", true}, 20 | {"name", "Niels"}, 21 | {"nothing", nullptr}, 22 | {"answer", {{"everything", 42}}}, 23 | {"list", {1, 0, 2}}, 24 | {"object", {{"currency", "USD"}, {"value", 42.99}}}}; 25 | (void)x.dump(); 26 | } 27 | } 28 | BENCHMARK(BM_Logger_LogJson); 29 | 30 | static void BM_Logger_NlohmannOrdered(benchmark::State& state) { 31 | while (state.KeepRunning()) { 32 | nlohmann::ordered_json x{ 33 | {"pi", 3.141}, 34 | {"happy", true}, 35 | {"name", "Niels"}, 36 | {"nothing", nullptr}, 37 | {"answer", {{"everything", 42}}}, 38 | {"list", {1, 0, 2}}, 39 | {"object", {{"currency", "USD"}, {"value", 42.99}}}}; 40 | (void)x.dump(); 41 | } 42 | } 43 | BENCHMARK(BM_Logger_NlohmannOrdered); 44 | 45 | static void BM_Logger_NlohmannJson(benchmark::State& state) { 46 | while (state.KeepRunning()) { 47 | nlohmann::json x{{"pi", 3.141}, 48 | {"happy", true}, 49 | {"name", "Niels"}, 50 | {"nothing", nullptr}, 51 | {"answer", {{"everything", 42}}}, 52 | {"list", {1, 0, 2}}, 53 | {"object", {{"currency", "USD"}, {"value", 42.99}}}}; 54 | (void)x.dump(); 55 | } 56 | } 57 | BENCHMARK(BM_Logger_NlohmannJson); 58 | -------------------------------------------------------------------------------- /tests/crc32/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | cb_add_test_executable(platform-crc32c-test crc32c_test.cc) 4 | target_link_libraries(platform-crc32c-test PRIVATE platform) 5 | platform_enable_pch(platform-crc32c-test) 6 | 7 | set(crc32_hw_archs "aarch64;AMD64;x86_64") 8 | if(CMAKE_SYSTEM_PROCESSOR IN_LIST crc32_hw_archs) 9 | cb_add_test_executable(platform-crc32c-sw_hw-test crc32c_test.cc) 10 | target_compile_definitions(platform-crc32c-sw_hw-test PRIVATE CRC32C_UNIT_TEST) 11 | target_link_libraries(platform-crc32c-sw_hw-test PRIVATE platform) 12 | platform_enable_pch(platform-crc32c-sw_hw-test) 13 | 14 | add_test(platform-crc32c-sw_hw-test platform-crc32c-sw_hw-test) 15 | endif() 16 | 17 | cb_add_test_executable(platform-crc32c-bench crc32c_bench.cc) 18 | target_link_libraries(platform-crc32c-bench PRIVATE platform) 19 | platform_enable_pch(platform-crc32c-bench) 20 | 21 | add_test(platform-crc32c-test platform-crc32c-test) 22 | -------------------------------------------------------------------------------- /tests/histogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # We use "_USE_MATH_DEFINES" which is some windows shenanigans in 2 | # histogram_test.cc. This creates some macros that are not created if you 3 | # have already included already via another file. The precompiled header 4 | # is including via something and appears to be included before any other 5 | # includes so the PCH is incompatible with this file on windows. 6 | set_source_files_properties(histogram_test.cc 7 | PROPERTIES 8 | SKIP_PRECOMPILE_HEADERS ON) 9 | 10 | cb_add_test_executable(platform-histogram-test 11 | ${Platform_SOURCE_DIR}/include/platform/histogram.h 12 | ${Platform_SOURCE_DIR}/src/histogram.cc 13 | histogram_test.cc) 14 | TARGET_LINK_LIBRARIES(platform-histogram-test PRIVATE platform GTest::gtest GTest::gtest_main) 15 | platform_enable_pch(platform-histogram-test) 16 | ADD_TEST(platform-histogram-test platform-histogram-test) 17 | -------------------------------------------------------------------------------- /tests/interrupt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Disable the test for Windows due to the fact that the event is 2 | # sent to the entire process group causing the python interpreter 3 | # to exit with the same error code (keyboard interrupt) 4 | if (NOT WIN32) 5 | cb_add_test_executable(platform-interrupt-test 6 | ${Platform_SOURCE_DIR}/include/platform/interrupt.h 7 | interrupt_test.cc) 8 | target_link_libraries(platform-interrupt-test PRIVATE platform) 9 | platform_enable_pch(platform-interrupt-test) 10 | add_test(platform-interrupt-test 11 | ${Platform_SOURCE_DIR}/tests/interrupt/interrupt_test.py 12 | ${Platform_BINARY_DIR}/tests/interrupt/platform-interrupt-test) 13 | endif (NOT WIN32) 14 | -------------------------------------------------------------------------------- /tests/interrupt/interrupt_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | /** 12 | * This program/test is designed to block waiting for a ctrl+c signal. If the 13 | * signal handler has been setup properly then program should return 0 when 14 | * SIGINT is triggered. If it has not been setup properly then the program 15 | * will be forced to exit and will return 1 instead. 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | 24 | static std::atomic go{false}; 25 | 26 | void handler() { 27 | go = true; 28 | } 29 | 30 | int main(int argc, char** argv) { 31 | std::cerr << "Registering SIGINT handler\n"; 32 | cb::console::set_sigint_handler(handler); 33 | 34 | std::cerr << "Busy waiting for signal\n"; 35 | while (!go) { 36 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); 37 | } 38 | std::cerr << "SIGINT detected!\n"; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /tests/interrupt/interrupt_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | Copyright 2017-Present Couchbase, Inc. 5 | 6 | Use of this software is governed by the Business Source License included in 7 | the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that 8 | file, in accordance with the Business Source License, use of this software will 9 | be governed by the Apache License, Version 2.0, included in the file 10 | licenses/APL2.txt. 11 | """ 12 | 13 | import os 14 | import signal 15 | import subprocess 16 | import sys 17 | import time 18 | 19 | """ 20 | This test runs the underlying 'platform-interrupt-test' binary, 21 | sends it a SIGINT/CTRL_C_EVENT, and then propogates the return 22 | code that the underlying binary gave. 23 | """ 24 | 25 | if len(sys.argv) != 2: 26 | exit(1) 27 | 28 | print(sys.argv[1]) 29 | proc = subprocess.Popen(sys.argv[1], stderr=subprocess.PIPE, 30 | universal_newlines=True) 31 | 32 | # Let the underlying process register the signal handler 33 | while True: 34 | line = proc.stderr.readline() 35 | if 'Busy waiting for signal' in line: 36 | break 37 | 38 | if os.name == 'nt': 39 | # The most consistent way to detect ctrl+c on Windows 40 | # is via the specific CTRL_C_EVENT rather than SIGINT 41 | os.kill(proc.pid, signal.CTRL_C_EVENT) 42 | else: 43 | os.kill(proc.pid, signal.SIGINT) 44 | 45 | # Propogate the returncode of the underlying test 46 | exit(proc.wait()) 47 | -------------------------------------------------------------------------------- /tests/memory_tracking_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if (NOT MEMORY_ALLOCATOR STREQUAL "system") 2 | cb_add_test_executable(platform_memory_tracking_test 3 | memory_tracking_allocator_test.cc 4 | memory_tracking_test.cc) 5 | target_link_libraries(platform_memory_tracking_test 6 | PRIVATE 7 | platform 8 | platform_cb_malloc_arena 9 | GTest::gtest 10 | ${MALLOC_LIBRARIES}) 11 | platform_enable_pch(platform_memory_tracking_test) 12 | 13 | add_test(NAME platform-memory-tracking 14 | WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} 15 | COMMAND platform_memory_tracking_test) 16 | endif (NOT MEMORY_ALLOCATOR STREQUAL "system") 17 | -------------------------------------------------------------------------------- /tests/rwlock/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cb_add_test_executable(rwlock_test rwlock_test.cc) 2 | TARGET_LINK_LIBRARIES(rwlock_test PRIVATE GTest::gtest GTest::gtest_main platform) 3 | platform_enable_pch(rwlock_test) 4 | ADD_TEST(rwlock_test rwlock_test) 5 | 6 | # The death tests in this suite rely on halting as soon as an error is detected. 7 | # For macOS must explicitly enable deadlock detection. 8 | SET_TESTS_PROPERTIES(rwlock_test PROPERTIES 9 | ENVIRONMENT "TSAN_OPTIONS=halt_on_error=1 detect_deadlocks=1") 10 | -------------------------------------------------------------------------------- /tests/unit_tests/checked_snprintf_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | TEST(checked_snprintf, DestinationNullptr) { 16 | EXPECT_THROW(checked_snprintf(nullptr, 10, "xyz"), std::invalid_argument); 17 | } 18 | 19 | TEST(checked_snprintf, DestinationSize0) { 20 | std::array buf{}; 21 | EXPECT_THROW(checked_snprintf(buf.data(), 0, "xyz"), std::invalid_argument); 22 | } 23 | 24 | TEST(checked_snprintf, FitInBuffer) { 25 | char buffer[10]; 26 | EXPECT_EQ(4, checked_snprintf(buffer, sizeof(buffer), "test")); 27 | EXPECT_EQ(std::string("test"), buffer); 28 | } 29 | 30 | TEST(checked_snprintf, BufferTooSmall) { 31 | char buffer[10]; 32 | EXPECT_THROW(checked_snprintf(buffer, sizeof(buffer), "test %s %u", 33 | "with a buffer that is too big", 10), 34 | std::overflow_error); 35 | } 36 | -------------------------------------------------------------------------------- /tests/unit_tests/enum_class_bitmask_functions_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | #include 12 | 13 | enum class Bitmask { None = 0, Two = 2, Four = 4, Eight = 8 }; 14 | DEFINE_ENUM_CLASS_BITMASK_FUNCTIONS(Bitmask); 15 | 16 | TEST(Bitmask, Operators) { 17 | Bitmask bm = {}; 18 | EXPECT_EQ(Bitmask::None, bm); 19 | bm |= Bitmask::Eight; 20 | EXPECT_EQ(Bitmask::Eight, bm); 21 | EXPECT_TRUE(isFlagSet(bm, Bitmask::Eight)); 22 | EXPECT_FALSE(isFlagSet(bm, Bitmask::Four)); 23 | 24 | bm |= Bitmask::Four; 25 | EXPECT_TRUE(isFlagSet(bm, Bitmask::Eight)); 26 | EXPECT_TRUE(isFlagSet(bm, Bitmask::Four)); 27 | EXPECT_TRUE(isFlagSet(bm, Bitmask::Eight | Bitmask::Four)); 28 | EXPECT_FALSE(isFlagSet(bm, Bitmask::Eight | Bitmask::Four | Bitmask::Two)); 29 | 30 | bm &= ~Bitmask::Four; 31 | EXPECT_TRUE(isFlagSet(bm, Bitmask::Eight)); 32 | EXPECT_FALSE(isFlagSet(bm, Bitmask::Four)); 33 | EXPECT_EQ(Bitmask::Eight, bm); 34 | } 35 | -------------------------------------------------------------------------------- /tests/unit_tests/getopt_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | /* 12 | * Unit tests for the getopt shim implementation of getopt / getopt_long, 13 | * as required by Win32 which doesn't have 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | // Under Win32 we alias cb::getopt() to getopt(), as Win32 doesn't have 21 | // getopt(). Test that a second call to getopt() succeeds as long as it is 22 | // reset via optind. 23 | TEST(SystemGetoptTest, TestMultipleCalls) { 24 | std::array opts{{"program", "-a"}}; 25 | const auto argc = opts.size(); 26 | char** argv = const_cast(opts.data()); 27 | 28 | // Call getopt once; advancing it's state. 29 | ASSERT_EQ('a', getopt(argc, argv, "ab")); 30 | ASSERT_EQ(-1, getopt(argc, argv, "ab")); 31 | 32 | // Reset optind, check that this allows us to parse a second time. 33 | optind = 1; 34 | EXPECT_EQ('a', getopt(argc, argv, "ab")); 35 | EXPECT_EQ(-1, getopt(argc, argv, "ab")); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /tests/unit_tests/hex_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | TEST(Hex, InputStringTooLong) { 15 | std::stringstream ss; 16 | ss << std::hex << uint64_t(-1); 17 | EXPECT_EQ(-1ULL, cb::from_hex(ss.str())); 18 | // Make sure it won't fit in there 19 | ss << "0"; 20 | EXPECT_THROW(cb::from_hex(ss.str()), std::overflow_error); 21 | } 22 | 23 | TEST(Hex, InputStringData) { 24 | const std::string value{"0123456789abcdef"}; 25 | const uint64_t expected{0x0123456789abcdefULL}; 26 | EXPECT_EQ(expected, cb::from_hex(value)); 27 | } 28 | 29 | TEST(Hex, InputWithPrefix) { 30 | EXPECT_EQ(0x4096, cb::from_hex("0x4096")); 31 | } 32 | 33 | TEST(Hex, ToHexUint8) { 34 | auto val = uint8_t(-1); 35 | EXPECT_EQ("0xff", cb::to_hex(val)); 36 | val = 0; 37 | EXPECT_EQ("0x00", cb::to_hex(val)); 38 | } 39 | 40 | TEST(Hex, ToHexUint16) { 41 | auto val = uint16_t(-1); 42 | EXPECT_EQ("0xffff", cb::to_hex(val)); 43 | val = 0; 44 | EXPECT_EQ("0x0000", cb::to_hex(val)); 45 | } 46 | 47 | TEST(Hex, ToHexUint32) { 48 | auto val = uint32_t(-1); 49 | EXPECT_EQ("0xffffffff", cb::to_hex(val)); 50 | val = 0; 51 | EXPECT_EQ("0x00000000", cb::to_hex(val)); 52 | } 53 | 54 | TEST(Hex, ToHexUint64) { 55 | auto val = uint64_t(-1); 56 | EXPECT_EQ("0xffffffffffffffff", cb::to_hex(val)); 57 | val = 0; 58 | EXPECT_EQ("0x0000000000000000", cb::to_hex(val)); 59 | } 60 | 61 | TEST(Hex, ToHexByteBuffer) { 62 | std::vector buffer(4); 63 | for (auto& c : buffer) { 64 | c = 0xa5; 65 | } 66 | EXPECT_EQ("0xa5 0xa5 0xa5 0xa5", 67 | cb::to_hex({buffer.data(), buffer.size()})); 68 | } 69 | 70 | TEST(Hex, HexEncode) { 71 | std::vector buffer = {{0xde, 0xad, 0xbe, 0xef, 0xff}}; 72 | EXPECT_EQ("deadbeefff", cb::hex_encode({buffer.data(), buffer.size()})); 73 | EXPECT_EQ("deadbeefff", 74 | cb::hex_encode({reinterpret_cast(buffer.data()), 75 | buffer.size()})); 76 | } 77 | -------------------------------------------------------------------------------- /tests/unit_tests/n_byte_integer_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #include 13 | 14 | TEST(NByteCounterTest, basic) { 15 | cb::UnsignedNByteInteger<6> counter; 16 | EXPECT_EQ(6, sizeof(counter)); 17 | 18 | EXPECT_EQ(0, counter); 19 | counter += 1; 20 | EXPECT_EQ(1, counter); 21 | EXPECT_EQ(1, counter++); 22 | EXPECT_EQ(2, counter); 23 | EXPECT_EQ(3, ++counter); 24 | EXPECT_EQ(3, counter--); 25 | EXPECT_EQ(2, counter); 26 | EXPECT_EQ(1, --counter); 27 | EXPECT_EQ(1, counter); 28 | } 29 | 30 | // Designed behaviour is to truncate when initialising from 8-bytes 31 | TEST(NByteCounterTest, truncate) { 32 | cb::UnsignedNByteInteger<6> counter(0x0080A80000000000); 33 | EXPECT_EQ(0x0000A80000000000, counter); 34 | } 35 | 36 | TEST(NByteCounterTest, overflow) { 37 | cb::UnsignedNByteInteger<6> counter(0x0080ffffffffffff); 38 | EXPECT_EQ(0x0000ffffffffffff, counter); 39 | counter += 2; 40 | EXPECT_EQ(1, counter); 41 | } 42 | 43 | TEST(NByteCounterTest, underflow) { 44 | cb::UnsignedNByteInteger<6> counter(0x0080ffffffffffff); 45 | EXPECT_EQ(0x0000ffffffffffff, counter); 46 | counter -= 0x0080ffffffffffff; 47 | EXPECT_EQ(0, counter); 48 | counter -= 1; 49 | EXPECT_EQ(0x0000ffffffffffff, counter); 50 | } 51 | 52 | TEST(NByteCounterTest, byteSwap) { 53 | cb::UnsignedNByteInteger<6> counter(0x123456789ABC); 54 | auto swapped = counter.byteSwap(); 55 | EXPECT_EQ(0xBC9A78563412, swapped); 56 | } 57 | 58 | TEST(NByteCounterTest, ntoh) { 59 | cb::UnsignedNByteInteger<6> counter(12345); 60 | EXPECT_EQ(0x3039, counter); 61 | 62 | auto networkOrder = counter.hton(); 63 | if (folly::kIsLittleEndian) { 64 | EXPECT_EQ(0x393000000000, networkOrder); 65 | } else { 66 | EXPECT_EQ(0x3039, counter); 67 | } 68 | 69 | auto hostOrder = networkOrder.ntoh(); 70 | EXPECT_EQ(0x3039, hostOrder); 71 | } -------------------------------------------------------------------------------- /tests/unit_tests/optional_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | TEST(OptionalTest, ToStringEmptyInt) { 16 | auto empty = std::optional{}; 17 | EXPECT_EQ("none", to_string_or_none(empty)); 18 | } 19 | 20 | TEST(OptionalTest, ToStringPresentInt) { 21 | auto present = std::make_optional(123); 22 | EXPECT_EQ("123", to_string_or_none(present)); 23 | } 24 | 25 | struct Custom { 26 | int x; 27 | }; 28 | std::string to_string(const Custom& custom) { 29 | return std::to_string(custom.x); 30 | } 31 | 32 | TEST(OptionalTest, ToStringPresentUserDefinedToString) { 33 | auto present = std::make_optional(Custom{123}); 34 | EXPECT_EQ("123", to_string_or_none(present)); 35 | } 36 | -------------------------------------------------------------------------------- /tests/unit_tests/ordered_map_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | #include 11 | 12 | #include 13 | 14 | TEST(OrderedMap, Basic) { 15 | cb::OrderedMap o; 16 | o.emplace(1, 2); 17 | o.emplace(3, 4); 18 | 19 | EXPECT_NE(o.end(), o.find(1)); 20 | EXPECT_EQ(o.end(), o.find(2)); 21 | EXPECT_NE(o.end(), o.find(3)); 22 | EXPECT_EQ(o.end(), o.find(4)); 23 | } 24 | -------------------------------------------------------------------------------- /tests/unit_tests/process_monitor_child.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021-Present Couchbase, Inc. 3 | * 4 | * Use of this software is governed by the Business Source License included 5 | * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified 6 | * in that file, in accordance with the Business Source License, use of this 7 | * software will be governed by the Apache License, Version 2.0, included in 8 | * the file licenses/APL2.txt. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | [[noreturn]] void usage(const std::filesystem::path name) { 19 | std::cerr << "Usage: " << name.filename().string() << " [options]\n" 20 | << " --lockfile filename" << std::endl 21 | << " --exitcode exitcode" << std::endl; 22 | exit(5); 23 | } 24 | 25 | void waitWhileLockFile(const std::filesystem::path lockfile) { 26 | while (exists(lockfile)) { 27 | std::this_thread::sleep_for(std::chrono::microseconds{10}); 28 | } 29 | } 30 | 31 | int main(int argc, char** argv) { 32 | std::vector