@nxtedition/rocksdb 9.0.0 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.cc +0 -21
- package/deps/rocksdb/rocksdb/CMakeLists.txt +13 -9
- package/deps/rocksdb/rocksdb/Makefile +15 -6
- package/deps/rocksdb/rocksdb/README.md +29 -0
- package/deps/rocksdb/rocksdb/TARGETS +17 -2
- package/deps/rocksdb/rocksdb/cache/cache.cc +35 -0
- package/deps/rocksdb/rocksdb/cache/cache_bench_tool.cc +74 -15
- package/deps/rocksdb/rocksdb/cache/cache_helpers.cc +2 -1
- package/deps/rocksdb/rocksdb/cache/cache_reservation_manager.h +4 -3
- package/deps/rocksdb/rocksdb/cache/cache_test.cc +16 -4
- package/deps/rocksdb/rocksdb/cache/charged_cache.cc +4 -2
- package/deps/rocksdb/rocksdb/cache/charged_cache.h +5 -3
- package/deps/rocksdb/rocksdb/cache/clock_cache.cc +2024 -14
- package/deps/rocksdb/rocksdb/cache/clock_cache.h +349 -23
- package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache.cc +126 -51
- package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache.h +9 -0
- package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache_test.cc +182 -7
- package/deps/rocksdb/rocksdb/cache/lru_cache_test.cc +31 -14
- package/deps/rocksdb/rocksdb/cache/secondary_cache.cc +0 -33
- package/deps/rocksdb/rocksdb/cache/secondary_cache_adapter.cc +293 -17
- package/deps/rocksdb/rocksdb/cache/secondary_cache_adapter.h +21 -5
- package/deps/rocksdb/rocksdb/cache/sharded_cache.cc +10 -0
- package/deps/rocksdb/rocksdb/cache/sharded_cache.h +8 -3
- package/deps/rocksdb/rocksdb/cache/tiered_secondary_cache.cc +119 -0
- package/deps/rocksdb/rocksdb/cache/tiered_secondary_cache.h +155 -0
- package/deps/rocksdb/rocksdb/cache/tiered_secondary_cache_test.cc +711 -0
- package/deps/rocksdb/rocksdb/cache/typed_cache.h +17 -11
- package/deps/rocksdb/rocksdb/db/arena_wrapped_db_iter.cc +25 -11
- package/deps/rocksdb/rocksdb/db/arena_wrapped_db_iter.h +1 -0
- package/deps/rocksdb/rocksdb/db/blob/blob_contents.h +2 -1
- package/deps/rocksdb/rocksdb/db/blob/blob_file_reader.cc +2 -1
- package/deps/rocksdb/rocksdb/db/blob/db_blob_basic_test.cc +8 -0
- package/deps/rocksdb/rocksdb/db/blob/db_blob_index_test.cc +7 -3
- package/deps/rocksdb/rocksdb/db/builder.cc +3 -3
- package/deps/rocksdb/rocksdb/db/c.cc +64 -0
- package/deps/rocksdb/rocksdb/db/c_test.c +36 -0
- package/deps/rocksdb/rocksdb/db/column_family.cc +23 -15
- package/deps/rocksdb/rocksdb/db/column_family.h +9 -0
- package/deps/rocksdb/rocksdb/db/column_family_test.cc +101 -5
- package/deps/rocksdb/rocksdb/db/compaction/compaction.cc +36 -23
- package/deps/rocksdb/rocksdb/db/compaction/compaction.h +24 -10
- package/deps/rocksdb/rocksdb/db/compaction/compaction_iterator.cc +3 -5
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job.cc +42 -18
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job.h +7 -3
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job_test.cc +4 -2
- package/deps/rocksdb/rocksdb/db/compaction/compaction_outputs.cc +8 -6
- package/deps/rocksdb/rocksdb/db/compaction/compaction_outputs.h +1 -1
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_fifo.cc +3 -0
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_test.cc +61 -0
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_universal.cc +146 -64
- package/deps/rocksdb/rocksdb/db/compaction/tiered_compaction_test.cc +13 -39
- package/deps/rocksdb/rocksdb/db/comparator_db_test.cc +1 -0
- package/deps/rocksdb/rocksdb/db/db_basic_test.cc +29 -7
- package/deps/rocksdb/rocksdb/db/db_block_cache_test.cc +8 -3
- package/deps/rocksdb/rocksdb/db/db_bloom_filter_test.cc +59 -0
- package/deps/rocksdb/rocksdb/db/db_compaction_filter_test.cc +27 -3
- package/deps/rocksdb/rocksdb/db/db_compaction_test.cc +186 -2
- package/deps/rocksdb/rocksdb/db/db_flush_test.cc +1 -0
- package/deps/rocksdb/rocksdb/db/db_impl/compacted_db_impl.cc +17 -5
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl.cc +519 -240
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl.h +104 -43
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_compaction_flush.cc +169 -66
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_debug.cc +2 -1
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_files.cc +12 -4
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_open.cc +50 -14
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_readonly.cc +85 -53
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_readonly.h +3 -7
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_secondary.cc +99 -82
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_secondary.h +4 -14
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_write.cc +24 -21
- package/deps/rocksdb/rocksdb/db/db_info_dumper.cc +6 -0
- package/deps/rocksdb/rocksdb/db/db_iter.cc +83 -55
- package/deps/rocksdb/rocksdb/db/db_iter.h +10 -2
- package/deps/rocksdb/rocksdb/db/db_iter_test.cc +29 -0
- package/deps/rocksdb/rocksdb/db/db_iterator_test.cc +276 -21
- package/deps/rocksdb/rocksdb/db/db_log_iter_test.cc +35 -0
- package/deps/rocksdb/rocksdb/db/db_merge_operator_test.cc +187 -1
- package/deps/rocksdb/rocksdb/db/db_options_test.cc +258 -0
- package/deps/rocksdb/rocksdb/db/db_range_del_test.cc +258 -0
- package/deps/rocksdb/rocksdb/db/db_rate_limiter_test.cc +1 -0
- package/deps/rocksdb/rocksdb/db/db_readonly_with_timestamp_test.cc +52 -0
- package/deps/rocksdb/rocksdb/db/db_secondary_test.cc +74 -1
- package/deps/rocksdb/rocksdb/db/db_sst_test.cc +22 -4
- package/deps/rocksdb/rocksdb/db/db_tailing_iter_test.cc +3 -1
- package/deps/rocksdb/rocksdb/db/db_test.cc +134 -30
- package/deps/rocksdb/rocksdb/db/db_test2.cc +3 -0
- package/deps/rocksdb/rocksdb/db/db_test_util.cc +11 -6
- package/deps/rocksdb/rocksdb/db/db_test_util.h +5 -2
- package/deps/rocksdb/rocksdb/db/db_universal_compaction_test.cc +1 -0
- package/deps/rocksdb/rocksdb/db/db_wal_test.cc +12 -0
- package/deps/rocksdb/rocksdb/db/db_with_timestamp_basic_test.cc +337 -1
- package/deps/rocksdb/rocksdb/db/deletefile_test.cc +2 -0
- package/deps/rocksdb/rocksdb/db/error_handler.cc +51 -34
- package/deps/rocksdb/rocksdb/db/error_handler.h +7 -6
- package/deps/rocksdb/rocksdb/db/external_sst_file_test.cc +58 -0
- package/deps/rocksdb/rocksdb/db/flush_job.cc +17 -19
- package/deps/rocksdb/rocksdb/db/flush_job.h +3 -3
- package/deps/rocksdb/rocksdb/db/flush_job_test.cc +2 -1
- package/deps/rocksdb/rocksdb/db/manual_compaction_test.cc +2 -0
- package/deps/rocksdb/rocksdb/db/memtable.cc +18 -70
- package/deps/rocksdb/rocksdb/db/memtable_list.cc +1 -1
- package/deps/rocksdb/rocksdb/db/memtable_list.h +11 -1
- package/deps/rocksdb/rocksdb/db/memtable_list_test.cc +1 -1
- package/deps/rocksdb/rocksdb/db/merge_helper.cc +330 -115
- package/deps/rocksdb/rocksdb/db/merge_helper.h +100 -12
- package/deps/rocksdb/rocksdb/db/merge_operator.cc +82 -0
- package/deps/rocksdb/rocksdb/db/merge_test.cc +267 -0
- package/deps/rocksdb/rocksdb/db/perf_context_test.cc +3 -0
- package/deps/rocksdb/rocksdb/db/periodic_task_scheduler.h +4 -4
- package/deps/rocksdb/rocksdb/db/plain_table_db_test.cc +2 -0
- package/deps/rocksdb/rocksdb/db/prefix_test.cc +1 -0
- package/deps/rocksdb/rocksdb/db/range_del_aggregator.h +4 -0
- package/deps/rocksdb/rocksdb/db/range_tombstone_fragmenter.h +4 -0
- package/deps/rocksdb/rocksdb/db/repair.cc +4 -3
- package/deps/rocksdb/rocksdb/db/seqno_time_test.cc +454 -70
- package/deps/rocksdb/rocksdb/db/seqno_to_time_mapping.cc +105 -69
- package/deps/rocksdb/rocksdb/db/seqno_to_time_mapping.h +83 -46
- package/deps/rocksdb/rocksdb/db/table_cache.cc +32 -19
- package/deps/rocksdb/rocksdb/db/table_cache.h +12 -6
- package/deps/rocksdb/rocksdb/db/version_edit.h +10 -4
- package/deps/rocksdb/rocksdb/db/version_set.cc +75 -73
- package/deps/rocksdb/rocksdb/db/version_set.h +8 -8
- package/deps/rocksdb/rocksdb/db/version_set_sync_and_async.h +2 -5
- package/deps/rocksdb/rocksdb/db/version_set_test.cc +22 -11
- package/deps/rocksdb/rocksdb/db/wide/db_wide_basic_test.cc +525 -0
- package/deps/rocksdb/rocksdb/db/wide/wide_column_serialization.cc +6 -22
- package/deps/rocksdb/rocksdb/db/wide/wide_column_serialization.h +0 -20
- package/deps/rocksdb/rocksdb/db/wide/wide_column_serialization_test.cc +0 -29
- package/deps/rocksdb/rocksdb/db/wide/wide_columns_helper.cc +46 -0
- package/deps/rocksdb/rocksdb/db/wide/wide_columns_helper.h +40 -0
- package/deps/rocksdb/rocksdb/db/wide/wide_columns_helper_test.cc +39 -0
- package/deps/rocksdb/rocksdb/db/write_batch.cc +44 -20
- package/deps/rocksdb/rocksdb/db_stress_tool/CMakeLists.txt +1 -0
- package/deps/rocksdb/rocksdb/db_stress_tool/batched_ops_stress.cc +4 -4
- package/deps/rocksdb/rocksdb/db_stress_tool/cf_consistency_stress.cc +4 -7
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_common.cc +88 -10
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_common.h +15 -10
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_driver.cc +108 -58
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_gflags.cc +36 -14
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_listener.h +34 -0
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_shared_state.h +1 -1
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_test_base.cc +195 -130
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_test_base.h +4 -2
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_tool.cc +12 -12
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_wide_merge_operator.cc +51 -0
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_wide_merge_operator.h +27 -0
- package/deps/rocksdb/rocksdb/db_stress_tool/expected_state.cc +3 -6
- package/deps/rocksdb/rocksdb/db_stress_tool/multi_ops_txns_stress.cc +14 -11
- package/deps/rocksdb/rocksdb/db_stress_tool/no_batched_ops_stress.cc +44 -38
- package/deps/rocksdb/rocksdb/env/env.cc +5 -0
- package/deps/rocksdb/rocksdb/env/unique_id_gen.cc +1 -0
- package/deps/rocksdb/rocksdb/file/file_prefetch_buffer.cc +50 -29
- package/deps/rocksdb/rocksdb/file/file_prefetch_buffer.h +32 -2
- package/deps/rocksdb/rocksdb/file/prefetch_test.cc +513 -30
- package/deps/rocksdb/rocksdb/file/random_access_file_reader.cc +8 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/advanced_cache.h +38 -13
- package/deps/rocksdb/rocksdb/include/rocksdb/advanced_options.h +14 -7
- package/deps/rocksdb/rocksdb/include/rocksdb/c.h +42 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/cache.h +65 -12
- package/deps/rocksdb/rocksdb/include/rocksdb/compaction_filter.h +11 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/comparator.h +26 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/db.h +37 -4
- package/deps/rocksdb/rocksdb/include/rocksdb/env.h +2 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/file_system.h +1 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/filter_policy.h +8 -3
- package/deps/rocksdb/rocksdb/include/rocksdb/iterator.h +10 -4
- package/deps/rocksdb/rocksdb/include/rocksdb/listener.h +4 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/memory_allocator.h +1 -1
- package/deps/rocksdb/rocksdb/include/rocksdb/merge_operator.h +55 -4
- package/deps/rocksdb/rocksdb/include/rocksdb/options.h +45 -5
- package/deps/rocksdb/rocksdb/include/rocksdb/port_defs.h +4 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/rate_limiter.h +9 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/secondary_cache.h +79 -8
- package/deps/rocksdb/rocksdb/include/rocksdb/statistics.h +16 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/status.h +35 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/system_clock.h +15 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/table_properties.h +14 -3
- package/deps/rocksdb/rocksdb/include/rocksdb/thread_status.h +2 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/types.h +7 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/utilities/ldb_cmd.h +6 -1
- package/deps/rocksdb/rocksdb/include/rocksdb/utilities/options_type.h +2 -1
- package/deps/rocksdb/rocksdb/include/rocksdb/utilities/transaction.h +9 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/version.h +2 -2
- package/deps/rocksdb/rocksdb/include/rocksdb/wide_columns.h +53 -2
- package/deps/rocksdb/rocksdb/include/rocksdb/write_batch.h +0 -2
- package/deps/rocksdb/rocksdb/memory/jemalloc_nodump_allocator.cc +2 -2
- package/deps/rocksdb/rocksdb/memory/jemalloc_nodump_allocator.h +1 -1
- package/deps/rocksdb/rocksdb/microbench/README.md +60 -0
- package/deps/rocksdb/rocksdb/monitoring/instrumented_mutex.h +1 -1
- package/deps/rocksdb/rocksdb/monitoring/statistics.cc +6 -0
- package/deps/rocksdb/rocksdb/monitoring/stats_history_test.cc +18 -7
- package/deps/rocksdb/rocksdb/monitoring/thread_status_util_debug.cc +4 -0
- package/deps/rocksdb/rocksdb/options/customizable_test.cc +4 -0
- package/deps/rocksdb/rocksdb/options/db_options.cc +47 -2
- package/deps/rocksdb/rocksdb/options/db_options.h +3 -0
- package/deps/rocksdb/rocksdb/options/options_helper.cc +12 -0
- package/deps/rocksdb/rocksdb/options/options_settable_test.cc +3 -1
- package/deps/rocksdb/rocksdb/options/options_test.cc +6 -1
- package/deps/rocksdb/rocksdb/plugin/README.md +43 -0
- package/deps/rocksdb/rocksdb/port/README +10 -0
- package/deps/rocksdb/rocksdb/port/port_example.h +1 -1
- package/deps/rocksdb/rocksdb/port/port_posix.cc +1 -1
- package/deps/rocksdb/rocksdb/port/port_posix.h +7 -4
- package/deps/rocksdb/rocksdb/port/stack_trace.cc +5 -0
- package/deps/rocksdb/rocksdb/port/win/port_win.h +5 -2
- package/deps/rocksdb/rocksdb/src.mk +7 -1
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_builder.cc +1 -1
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_factory.cc +3 -1
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_iterator.cc +275 -61
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_iterator.h +96 -4
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader.cc +179 -62
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader.h +35 -22
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader_impl.h +12 -8
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader_sync_and_async.h +14 -9
- package/deps/rocksdb/rocksdb/table/block_based/block_cache.cc +3 -1
- package/deps/rocksdb/rocksdb/table/block_based/block_cache.h +26 -7
- package/deps/rocksdb/rocksdb/table/block_based/block_prefetcher.cc +15 -12
- package/deps/rocksdb/rocksdb/table/block_based/block_prefetcher.h +10 -5
- package/deps/rocksdb/rocksdb/table/block_based/block_test.cc +39 -18
- package/deps/rocksdb/rocksdb/table/block_based/filter_block_reader_common.cc +6 -6
- package/deps/rocksdb/rocksdb/table/block_based/filter_policy.cc +44 -26
- package/deps/rocksdb/rocksdb/table/block_based/filter_policy_internal.h +2 -1
- package/deps/rocksdb/rocksdb/table/block_based/index_reader_common.cc +1 -1
- package/deps/rocksdb/rocksdb/table/block_based/partitioned_filter_block.cc +10 -8
- package/deps/rocksdb/rocksdb/table/block_based/partitioned_index_iterator.cc +4 -2
- package/deps/rocksdb/rocksdb/table/block_based/partitioned_index_reader.cc +3 -2
- package/deps/rocksdb/rocksdb/table/block_based/uncompression_dict_reader.cc +1 -1
- package/deps/rocksdb/rocksdb/table/block_fetcher.cc +3 -2
- package/deps/rocksdb/rocksdb/table/block_fetcher.h +4 -0
- package/deps/rocksdb/rocksdb/table/block_fetcher_test.cc +6 -2
- package/deps/rocksdb/rocksdb/table/get_context.cc +52 -89
- package/deps/rocksdb/rocksdb/table/get_context.h +12 -3
- package/deps/rocksdb/rocksdb/table/internal_iterator.h +11 -0
- package/deps/rocksdb/rocksdb/table/iterator_wrapper.h +29 -1
- package/deps/rocksdb/rocksdb/table/merging_iterator.cc +12 -0
- package/deps/rocksdb/rocksdb/table/sst_file_dumper.cc +33 -6
- package/deps/rocksdb/rocksdb/table/sst_file_reader_test.cc +1 -0
- package/deps/rocksdb/rocksdb/table/sst_file_writer.cc +2 -4
- package/deps/rocksdb/rocksdb/table/table_reader.h +6 -0
- package/deps/rocksdb/rocksdb/test_util/mock_time_env.h +31 -0
- package/deps/rocksdb/rocksdb/test_util/secondary_cache_test_util.cc +2 -1
- package/deps/rocksdb/rocksdb/tools/block_cache_analyzer/block_cache_pysim.py +3 -3
- package/deps/rocksdb/rocksdb/tools/db_bench_tool.cc +26 -43
- package/deps/rocksdb/rocksdb/tools/ldb_cmd.cc +213 -28
- package/deps/rocksdb/rocksdb/tools/ldb_cmd_impl.h +36 -0
- package/deps/rocksdb/rocksdb/tools/ldb_tool.cc +0 -1
- package/deps/rocksdb/rocksdb/tools/sst_dump_test.cc +33 -10
- package/deps/rocksdb/rocksdb/util/bloom_test.cc +32 -11
- package/deps/rocksdb/rocksdb/util/cast_util.h +10 -0
- package/deps/rocksdb/rocksdb/util/comparator.cc +26 -1
- package/deps/rocksdb/rocksdb/util/compression.h +9 -3
- package/deps/rocksdb/rocksdb/util/crc32c.cc +7 -1
- package/deps/rocksdb/rocksdb/util/distributed_mutex.h +1 -1
- package/deps/rocksdb/rocksdb/util/overload.h +23 -0
- package/deps/rocksdb/rocksdb/util/rate_limiter.cc +53 -18
- package/deps/rocksdb/rocksdb/util/rate_limiter_impl.h +6 -1
- package/deps/rocksdb/rocksdb/util/rate_limiter_test.cc +90 -19
- package/deps/rocksdb/rocksdb/util/slice_test.cc +30 -0
- package/deps/rocksdb/rocksdb/util/status.cc +1 -0
- package/deps/rocksdb/rocksdb/util/string_util.cc +39 -0
- package/deps/rocksdb/rocksdb/util/string_util.h +10 -0
- package/deps/rocksdb/rocksdb/util/thread_operation.h +2 -0
- package/deps/rocksdb/rocksdb/util/udt_util.cc +42 -0
- package/deps/rocksdb/rocksdb/util/udt_util.h +19 -0
- package/deps/rocksdb/rocksdb/util/udt_util_test.cc +14 -0
- package/deps/rocksdb/rocksdb/util/xxhash.h +0 -3
- package/deps/rocksdb/rocksdb/util/xxph3.h +0 -4
- package/deps/rocksdb/rocksdb/utilities/blob_db/blob_db_impl.cc +2 -1
- package/deps/rocksdb/rocksdb/utilities/fault_injection_env.h +1 -0
- package/deps/rocksdb/rocksdb/utilities/fault_injection_fs.cc +19 -15
- package/deps/rocksdb/rocksdb/utilities/fault_injection_fs.h +11 -7
- package/deps/rocksdb/rocksdb/utilities/fault_injection_secondary_cache.h +5 -0
- package/deps/rocksdb/rocksdb/utilities/merge_operators/string_append/stringappend_test.cc +3 -0
- package/deps/rocksdb/rocksdb/utilities/option_change_migration/option_change_migration_test.cc +9 -0
- package/deps/rocksdb/rocksdb/utilities/simulator_cache/sim_cache.cc +7 -4
- package/deps/rocksdb/rocksdb/utilities/transactions/lock/range/range_tree/lib/README +13 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/optimistic_transaction_test.cc +41 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/pessimistic_transaction.cc +15 -9
- package/deps/rocksdb/rocksdb/utilities/transactions/pessimistic_transaction.h +4 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/transaction_test.cc +155 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/transaction_test.h +6 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/write_committed_transaction_ts_test.cc +81 -1
- package/deps/rocksdb/rocksdb/utilities/transactions/write_prepared_txn.cc +2 -6
- package/deps/rocksdb/rocksdb/utilities/transactions/write_prepared_txn_db.cc +7 -5
- package/deps/rocksdb/rocksdb/utilities/transactions/write_unprepared_txn.cc +2 -1
- package/deps/rocksdb/rocksdb/utilities/transactions/write_unprepared_txn_db.cc +3 -2
- package/deps/rocksdb/rocksdb/utilities/write_batch_with_index/write_batch_with_index.cc +57 -27
- package/deps/rocksdb/rocksdb/utilities/write_batch_with_index/write_batch_with_index_internal.cc +127 -120
- package/deps/rocksdb/rocksdb/utilities/write_batch_with_index/write_batch_with_index_internal.h +129 -59
- package/deps/rocksdb/rocksdb/utilities/write_batch_with_index/write_batch_with_index_test.cc +105 -8
- package/deps/rocksdb/rocksdb.gyp +4 -2
- package/index.js +0 -8
- package/package.json +1 -1
- package/prebuilds/darwin-arm64/node.napi.node +0 -0
- package/deps/rocksdb/rocksdb/cmake/modules/CxxFlags.cmake +0 -7
- package/deps/rocksdb/rocksdb/cmake/modules/FindJeMalloc.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/FindNUMA.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/FindSnappy.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/FindTBB.cmake +0 -33
- package/deps/rocksdb/rocksdb/cmake/modules/Findgflags.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/Findlz4.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/Finduring.cmake +0 -26
- package/deps/rocksdb/rocksdb/cmake/modules/Findzstd.cmake +0 -29
- package/deps/rocksdb/rocksdb/cmake/modules/ReadVersion.cmake +0 -10
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
#include <atomic>
|
|
14
14
|
#include <bitset>
|
|
15
15
|
#include <cassert>
|
|
16
|
+
#include <cinttypes>
|
|
16
17
|
#include <cstddef>
|
|
17
|
-
#include <cstdint>
|
|
18
18
|
#include <exception>
|
|
19
19
|
#include <functional>
|
|
20
20
|
#include <numeric>
|
|
@@ -94,9 +94,32 @@ inline void Unref(const ClockHandle& h, uint64_t count = 1) {
|
|
|
94
94
|
(void)old_meta;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
inline bool ClockUpdate(ClockHandle& h) {
|
|
98
|
-
uint64_t meta
|
|
97
|
+
inline bool ClockUpdate(ClockHandle& h, bool* purgeable = nullptr) {
|
|
98
|
+
uint64_t meta;
|
|
99
|
+
if (purgeable) {
|
|
100
|
+
assert(*purgeable == false);
|
|
101
|
+
// In AutoHCC, our eviction process follows the chain structure, so we
|
|
102
|
+
// should ensure that we see the latest state of each entry, at least for
|
|
103
|
+
// assertion checking.
|
|
104
|
+
meta = h.meta.load(std::memory_order_acquire);
|
|
105
|
+
} else {
|
|
106
|
+
// In FixedHCC, our eviction process is a simple iteration without regard
|
|
107
|
+
// to probing order, displacements, etc., so it doesn't matter if we see
|
|
108
|
+
// somewhat stale data.
|
|
109
|
+
meta = h.meta.load(std::memory_order_relaxed);
|
|
110
|
+
}
|
|
99
111
|
|
|
112
|
+
if (((meta >> ClockHandle::kStateShift) & ClockHandle::kStateShareableBit) ==
|
|
113
|
+
0) {
|
|
114
|
+
// Only clock update Shareable entries
|
|
115
|
+
if (purgeable) {
|
|
116
|
+
*purgeable = true;
|
|
117
|
+
// AutoHCC only: make sure we only attempt to update non-empty slots
|
|
118
|
+
assert((meta >> ClockHandle::kStateShift) &
|
|
119
|
+
ClockHandle::kStateOccupiedBit);
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
100
123
|
uint64_t acquire_count =
|
|
101
124
|
(meta >> ClockHandle::kAcquireCounterShift) & ClockHandle::kCounterMask;
|
|
102
125
|
uint64_t release_count =
|
|
@@ -105,10 +128,6 @@ inline bool ClockUpdate(ClockHandle& h) {
|
|
|
105
128
|
// Only clock update entries with no outstanding refs
|
|
106
129
|
return false;
|
|
107
130
|
}
|
|
108
|
-
if (!((meta >> ClockHandle::kStateShift) & ClockHandle::kStateShareableBit)) {
|
|
109
|
-
// Only clock update Shareable entries
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
131
|
if ((meta >> ClockHandle::kStateShift == ClockHandle::kStateVisible) &&
|
|
113
132
|
acquire_count > 0) {
|
|
114
133
|
// Decrement clock
|
|
@@ -886,6 +905,9 @@ bool FixedHyperClockTable::Release(HandleImpl* h, bool useful,
|
|
|
886
905
|
|
|
887
906
|
if (erase_if_last_ref || UNLIKELY(old_meta >> ClockHandle::kStateShift ==
|
|
888
907
|
ClockHandle::kStateInvisible)) {
|
|
908
|
+
// FIXME: There's a chance here that another thread could replace this
|
|
909
|
+
// entry and we end up erasing the wrong one.
|
|
910
|
+
|
|
889
911
|
// Update for last fetch_add op
|
|
890
912
|
if (useful) {
|
|
891
913
|
old_meta += ClockHandle::kReleaseIncrement;
|
|
@@ -1463,7 +1485,7 @@ class LoadVarianceStats {
|
|
|
1463
1485
|
"), Min/Max/Window = " + PercentStr(min_, N) + "/" +
|
|
1464
1486
|
PercentStr(max_, N) + "/" + std::to_string(N) +
|
|
1465
1487
|
", MaxRun{Pos/Neg} = " + std::to_string(max_pos_run_) + "/" +
|
|
1466
|
-
std::to_string(max_neg_run_)
|
|
1488
|
+
std::to_string(max_neg_run_);
|
|
1467
1489
|
}
|
|
1468
1490
|
|
|
1469
1491
|
void Add(bool positive) {
|
|
@@ -1498,7 +1520,11 @@ class LoadVarianceStats {
|
|
|
1498
1520
|
std::bitset<N> recent_;
|
|
1499
1521
|
|
|
1500
1522
|
static std::string PercentStr(size_t a, size_t b) {
|
|
1501
|
-
|
|
1523
|
+
if (b == 0) {
|
|
1524
|
+
return "??%";
|
|
1525
|
+
} else {
|
|
1526
|
+
return std::to_string(uint64_t{100} * a / b) + "%";
|
|
1527
|
+
}
|
|
1502
1528
|
}
|
|
1503
1529
|
};
|
|
1504
1530
|
|
|
@@ -1613,6 +1639,1995 @@ void FixedHyperClockCache::ReportProblems(
|
|
|
1613
1639
|
}
|
|
1614
1640
|
}
|
|
1615
1641
|
|
|
1642
|
+
// =======================================================================
|
|
1643
|
+
// AutoHyperClockCache
|
|
1644
|
+
// =======================================================================
|
|
1645
|
+
|
|
1646
|
+
// See AutoHyperClockTable::length_info_ etc. for how the linear hashing
|
|
1647
|
+
// metadata is encoded. Here are some example values:
|
|
1648
|
+
//
|
|
1649
|
+
// Used length | min shift | threshold | max shift
|
|
1650
|
+
// 2 | 1 | 0 | 1
|
|
1651
|
+
// 3 | 1 | 1 | 2
|
|
1652
|
+
// 4 | 2 | 0 | 2
|
|
1653
|
+
// 5 | 2 | 1 | 3
|
|
1654
|
+
// 6 | 2 | 2 | 3
|
|
1655
|
+
// 7 | 2 | 3 | 3
|
|
1656
|
+
// 8 | 3 | 0 | 3
|
|
1657
|
+
// 9 | 3 | 1 | 4
|
|
1658
|
+
// ...
|
|
1659
|
+
// Note:
|
|
1660
|
+
// * min shift = floor(log2(used length))
|
|
1661
|
+
// * max shift = ceil(log2(used length))
|
|
1662
|
+
// * used length == (1 << shift) + threshold
|
|
1663
|
+
// Also, shift=0 is never used in practice, so is reserved for "unset"
|
|
1664
|
+
|
|
1665
|
+
namespace {
|
|
1666
|
+
|
|
1667
|
+
inline int LengthInfoToMinShift(uint64_t length_info) {
|
|
1668
|
+
int mask_shift = BitwiseAnd(length_info, int{255});
|
|
1669
|
+
assert(mask_shift <= 63);
|
|
1670
|
+
assert(mask_shift > 0);
|
|
1671
|
+
return mask_shift;
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
inline size_t LengthInfoToThreshold(uint64_t length_info) {
|
|
1675
|
+
return static_cast<size_t>(length_info >> 8);
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
inline size_t LengthInfoToUsedLength(uint64_t length_info) {
|
|
1679
|
+
size_t threshold = LengthInfoToThreshold(length_info);
|
|
1680
|
+
int shift = LengthInfoToMinShift(length_info);
|
|
1681
|
+
assert(threshold < (size_t{1} << shift));
|
|
1682
|
+
size_t used_length = (size_t{1} << shift) + threshold;
|
|
1683
|
+
assert(used_length >= 2);
|
|
1684
|
+
return used_length;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
inline uint64_t UsedLengthToLengthInfo(size_t used_length) {
|
|
1688
|
+
assert(used_length >= 2);
|
|
1689
|
+
int shift = FloorLog2(used_length);
|
|
1690
|
+
uint64_t threshold = BottomNBits(used_length, shift);
|
|
1691
|
+
uint64_t length_info =
|
|
1692
|
+
(uint64_t{threshold} << 8) + static_cast<uint64_t>(shift);
|
|
1693
|
+
assert(LengthInfoToUsedLength(length_info) == used_length);
|
|
1694
|
+
assert(LengthInfoToMinShift(length_info) == shift);
|
|
1695
|
+
assert(LengthInfoToThreshold(length_info) == threshold);
|
|
1696
|
+
return length_info;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
inline size_t GetStartingLength(size_t capacity) {
|
|
1700
|
+
if (capacity > port::kPageSize) {
|
|
1701
|
+
// Start with one memory page
|
|
1702
|
+
return port::kPageSize / sizeof(AutoHyperClockTable::HandleImpl);
|
|
1703
|
+
} else {
|
|
1704
|
+
// Mostly to make unit tests happy
|
|
1705
|
+
return 4;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
inline size_t GetHomeIndex(uint64_t hash, int shift) {
|
|
1710
|
+
return static_cast<size_t>(BottomNBits(hash, shift));
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
inline void GetHomeIndexAndShift(uint64_t length_info, uint64_t hash,
|
|
1714
|
+
size_t* home, int* shift) {
|
|
1715
|
+
int min_shift = LengthInfoToMinShift(length_info);
|
|
1716
|
+
size_t threshold = LengthInfoToThreshold(length_info);
|
|
1717
|
+
bool extra_shift = GetHomeIndex(hash, min_shift) < threshold;
|
|
1718
|
+
*home = GetHomeIndex(hash, min_shift + extra_shift);
|
|
1719
|
+
*shift = min_shift + extra_shift;
|
|
1720
|
+
assert(*home < LengthInfoToUsedLength(length_info));
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
inline int GetShiftFromNextWithShift(uint64_t next_with_shift) {
|
|
1724
|
+
return BitwiseAnd(next_with_shift,
|
|
1725
|
+
AutoHyperClockTable::HandleImpl::kShiftMask);
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
inline size_t GetNextFromNextWithShift(uint64_t next_with_shift) {
|
|
1729
|
+
return static_cast<size_t>(next_with_shift >>
|
|
1730
|
+
AutoHyperClockTable::HandleImpl::kNextShift);
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
inline uint64_t MakeNextWithShift(size_t next, int shift) {
|
|
1734
|
+
return (uint64_t{next} << AutoHyperClockTable::HandleImpl::kNextShift) |
|
|
1735
|
+
static_cast<uint64_t>(shift);
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
inline uint64_t MakeNextWithShiftEnd(size_t head, int shift) {
|
|
1739
|
+
return AutoHyperClockTable::HandleImpl::kNextEndFlags |
|
|
1740
|
+
MakeNextWithShift(head, shift);
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
// Helper function for Lookup
|
|
1744
|
+
inline bool MatchAndRef(const UniqueId64x2* hashed_key, const ClockHandle& h,
|
|
1745
|
+
int shift = 0, size_t home = 0,
|
|
1746
|
+
bool* full_match_or_unknown = nullptr) {
|
|
1747
|
+
// Must be at least something to match
|
|
1748
|
+
assert(hashed_key || shift > 0);
|
|
1749
|
+
|
|
1750
|
+
uint64_t old_meta;
|
|
1751
|
+
// (Optimistically) increment acquire counter.
|
|
1752
|
+
old_meta = h.meta.fetch_add(ClockHandle::kAcquireIncrement,
|
|
1753
|
+
std::memory_order_acquire);
|
|
1754
|
+
// Check if it's a referencable (sharable) entry
|
|
1755
|
+
if ((old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
|
1756
|
+
<< ClockHandle::kStateShift)) == 0) {
|
|
1757
|
+
// For non-sharable states, incrementing the acquire counter has no effect
|
|
1758
|
+
// so we don't need to undo it. Furthermore, we cannot safely undo
|
|
1759
|
+
// it because we did not acquire a read reference to lock the
|
|
1760
|
+
// entry in a Shareable state.
|
|
1761
|
+
if (full_match_or_unknown) {
|
|
1762
|
+
*full_match_or_unknown = true;
|
|
1763
|
+
}
|
|
1764
|
+
return false;
|
|
1765
|
+
}
|
|
1766
|
+
// Else acquired a read reference
|
|
1767
|
+
assert(GetRefcount(old_meta + ClockHandle::kAcquireIncrement) > 0);
|
|
1768
|
+
if (hashed_key && h.hashed_key == *hashed_key &&
|
|
1769
|
+
LIKELY(old_meta & (uint64_t{ClockHandle::kStateVisibleBit}
|
|
1770
|
+
<< ClockHandle::kStateShift))) {
|
|
1771
|
+
// Match on full key, visible
|
|
1772
|
+
if (full_match_or_unknown) {
|
|
1773
|
+
*full_match_or_unknown = true;
|
|
1774
|
+
}
|
|
1775
|
+
return true;
|
|
1776
|
+
} else if (shift > 0 && home == BottomNBits(h.hashed_key[1], shift)) {
|
|
1777
|
+
// NOTE: upper 32 bits of hashed_key[0] is used for sharding
|
|
1778
|
+
// Match on home address, possibly invisible
|
|
1779
|
+
if (full_match_or_unknown) {
|
|
1780
|
+
*full_match_or_unknown = false;
|
|
1781
|
+
}
|
|
1782
|
+
return true;
|
|
1783
|
+
} else {
|
|
1784
|
+
// Mismatch. Pretend we never took the reference
|
|
1785
|
+
Unref(h);
|
|
1786
|
+
if (full_match_or_unknown) {
|
|
1787
|
+
*full_match_or_unknown = false;
|
|
1788
|
+
}
|
|
1789
|
+
return false;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// Assumes a chain rewrite lock prevents concurrent modification of
|
|
1794
|
+
// these chain pointers
|
|
1795
|
+
void UpgradeShiftsOnRange(AutoHyperClockTable::HandleImpl* arr,
|
|
1796
|
+
size_t& frontier, uint64_t stop_before_or_new_tail,
|
|
1797
|
+
int old_shift, int new_shift) {
|
|
1798
|
+
assert(frontier != SIZE_MAX);
|
|
1799
|
+
assert(new_shift == old_shift + 1);
|
|
1800
|
+
(void)old_shift;
|
|
1801
|
+
(void)new_shift;
|
|
1802
|
+
using HandleImpl = AutoHyperClockTable::HandleImpl;
|
|
1803
|
+
for (;;) {
|
|
1804
|
+
uint64_t next_with_shift =
|
|
1805
|
+
arr[frontier].chain_next_with_shift.load(std::memory_order_acquire);
|
|
1806
|
+
assert(GetShiftFromNextWithShift(next_with_shift) == old_shift);
|
|
1807
|
+
if (next_with_shift == stop_before_or_new_tail) {
|
|
1808
|
+
// Stopping at entry with pointer matching "stop before"
|
|
1809
|
+
assert(!HandleImpl::IsEnd(next_with_shift));
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
if (HandleImpl::IsEnd(next_with_shift)) {
|
|
1813
|
+
// Also update tail to new tail
|
|
1814
|
+
assert(HandleImpl::IsEnd(stop_before_or_new_tail));
|
|
1815
|
+
arr[frontier].chain_next_with_shift.store(stop_before_or_new_tail,
|
|
1816
|
+
std::memory_order_release);
|
|
1817
|
+
// Mark nothing left to upgrade
|
|
1818
|
+
frontier = SIZE_MAX;
|
|
1819
|
+
return;
|
|
1820
|
+
}
|
|
1821
|
+
// Next is another entry to process, so upgrade and advance frontier
|
|
1822
|
+
arr[frontier].chain_next_with_shift.fetch_add(1U,
|
|
1823
|
+
std::memory_order_acq_rel);
|
|
1824
|
+
assert(GetShiftFromNextWithShift(next_with_shift + 1) == new_shift);
|
|
1825
|
+
frontier = GetNextFromNextWithShift(next_with_shift);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
size_t CalcOccupancyLimit(size_t used_length) {
|
|
1830
|
+
return static_cast<size_t>(used_length * AutoHyperClockTable::kMaxLoadFactor +
|
|
1831
|
+
0.999);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
} // namespace
|
|
1835
|
+
|
|
1836
|
+
// An RAII wrapper for locking a chain of entries (flag bit on the head)
|
|
1837
|
+
// so that there is only one thread allowed to remove entries from the
|
|
1838
|
+
// chain, or to rewrite it by splitting for Grow. Without the lock,
|
|
1839
|
+
// all lookups and insertions at the head can proceed wait-free.
|
|
1840
|
+
// The class also provides functions for safely manipulating the head pointer
|
|
1841
|
+
// while holding the lock--or wanting to should it become non-empty.
|
|
1842
|
+
//
|
|
1843
|
+
// The flag bits on the head are such that the head cannot be locked if it
|
|
1844
|
+
// is an empty chain, so that a "blind" fetch_or will try to lock a non-empty
|
|
1845
|
+
// chain but have no effect on an empty chain. When a potential rewrite
|
|
1846
|
+
// operation see an empty head pointer, there is no need to lock as the
|
|
1847
|
+
// operation is a no-op. However, there are some cases such as CAS-update
|
|
1848
|
+
// where locking might be required after initially not being needed, if the
|
|
1849
|
+
// operation is forced to revisit the head pointer.
|
|
1850
|
+
class AutoHyperClockTable::ChainRewriteLock {
|
|
1851
|
+
public:
|
|
1852
|
+
using HandleImpl = AutoHyperClockTable::HandleImpl;
|
|
1853
|
+
explicit ChainRewriteLock(HandleImpl* h, std::atomic<uint64_t>& yield_count,
|
|
1854
|
+
bool already_locked_or_end = false)
|
|
1855
|
+
: head_ptr_(&h->head_next_with_shift) {
|
|
1856
|
+
if (already_locked_or_end) {
|
|
1857
|
+
new_head_ = head_ptr_->load(std::memory_order_acquire);
|
|
1858
|
+
// already locked or end
|
|
1859
|
+
assert(new_head_ & HandleImpl::kHeadLocked);
|
|
1860
|
+
return;
|
|
1861
|
+
}
|
|
1862
|
+
Acquire(yield_count);
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
~ChainRewriteLock() {
|
|
1866
|
+
if (!IsEnd()) {
|
|
1867
|
+
// Release lock
|
|
1868
|
+
uint64_t old = head_ptr_->fetch_and(~HandleImpl::kHeadLocked,
|
|
1869
|
+
std::memory_order_release);
|
|
1870
|
+
(void)old;
|
|
1871
|
+
assert((old & HandleImpl::kNextEndFlags) == HandleImpl::kHeadLocked);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
void Reset(HandleImpl* h, std::atomic<uint64_t>& yield_count) {
|
|
1876
|
+
this->~ChainRewriteLock();
|
|
1877
|
+
new (this) ChainRewriteLock(h, yield_count);
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
// Expected current state, assuming no parallel updates.
|
|
1881
|
+
uint64_t GetNewHead() const { return new_head_; }
|
|
1882
|
+
|
|
1883
|
+
// Only safe if we know that the value hasn't changed from other threads
|
|
1884
|
+
void SimpleUpdate(uint64_t next_with_shift) {
|
|
1885
|
+
assert(head_ptr_->load(std::memory_order_acquire) == new_head_);
|
|
1886
|
+
new_head_ = next_with_shift | HandleImpl::kHeadLocked;
|
|
1887
|
+
head_ptr_->store(new_head_, std::memory_order_release);
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
bool CasUpdate(uint64_t next_with_shift, std::atomic<uint64_t>& yield_count) {
|
|
1891
|
+
uint64_t new_head = next_with_shift | HandleImpl::kHeadLocked;
|
|
1892
|
+
uint64_t expected = GetNewHead();
|
|
1893
|
+
bool success = head_ptr_->compare_exchange_strong(
|
|
1894
|
+
expected, new_head, std::memory_order_acq_rel);
|
|
1895
|
+
if (success) {
|
|
1896
|
+
// Ensure IsEnd() is kept up-to-date, including for dtor
|
|
1897
|
+
new_head_ = new_head;
|
|
1898
|
+
} else {
|
|
1899
|
+
// Parallel update to head, such as Insert()
|
|
1900
|
+
if (IsEnd()) {
|
|
1901
|
+
// Didn't previously hold a lock
|
|
1902
|
+
if (HandleImpl::IsEnd(expected)) {
|
|
1903
|
+
// Still don't need to
|
|
1904
|
+
new_head_ = expected;
|
|
1905
|
+
} else {
|
|
1906
|
+
// Need to acquire lock before proceeding
|
|
1907
|
+
Acquire(yield_count);
|
|
1908
|
+
}
|
|
1909
|
+
} else {
|
|
1910
|
+
// Parallel update must preserve our lock
|
|
1911
|
+
assert((expected & HandleImpl::kNextEndFlags) ==
|
|
1912
|
+
HandleImpl::kHeadLocked);
|
|
1913
|
+
new_head_ = expected;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
return success;
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
bool IsEnd() const { return HandleImpl::IsEnd(new_head_); }
|
|
1920
|
+
|
|
1921
|
+
private:
|
|
1922
|
+
void Acquire(std::atomic<uint64_t>& yield_count) {
|
|
1923
|
+
for (;;) {
|
|
1924
|
+
// Acquire removal lock on the chain
|
|
1925
|
+
uint64_t old_head = head_ptr_->fetch_or(HandleImpl::kHeadLocked,
|
|
1926
|
+
std::memory_order_acq_rel);
|
|
1927
|
+
if ((old_head & HandleImpl::kNextEndFlags) != HandleImpl::kHeadLocked) {
|
|
1928
|
+
// Either acquired the lock or lock not needed (end)
|
|
1929
|
+
assert((old_head & HandleImpl::kNextEndFlags) == 0 ||
|
|
1930
|
+
(old_head & HandleImpl::kNextEndFlags) ==
|
|
1931
|
+
HandleImpl::kNextEndFlags);
|
|
1932
|
+
|
|
1933
|
+
new_head_ = old_head | HandleImpl::kHeadLocked;
|
|
1934
|
+
break;
|
|
1935
|
+
}
|
|
1936
|
+
// NOTE: one of the few yield-wait loops, which is rare enough in practice
|
|
1937
|
+
// for its performance to be insignificant. (E.g. using C++20 atomic
|
|
1938
|
+
// wait/notify would likely be worse because of wasted notify costs.)
|
|
1939
|
+
yield_count.fetch_add(1, std::memory_order_relaxed);
|
|
1940
|
+
std::this_thread::yield();
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
std::atomic<uint64_t>* head_ptr_;
|
|
1945
|
+
uint64_t new_head_;
|
|
1946
|
+
};
|
|
1947
|
+
|
|
1948
|
+
AutoHyperClockTable::AutoHyperClockTable(
|
|
1949
|
+
size_t capacity, bool /*strict_capacity_limit*/,
|
|
1950
|
+
CacheMetadataChargePolicy metadata_charge_policy,
|
|
1951
|
+
MemoryAllocator* allocator,
|
|
1952
|
+
const Cache::EvictionCallback* eviction_callback, const uint32_t* hash_seed,
|
|
1953
|
+
const Opts& opts)
|
|
1954
|
+
: BaseClockTable(metadata_charge_policy, allocator, eviction_callback,
|
|
1955
|
+
hash_seed),
|
|
1956
|
+
array_(MemMapping::AllocateLazyZeroed(
|
|
1957
|
+
sizeof(HandleImpl) * CalcMaxUsableLength(capacity,
|
|
1958
|
+
opts.min_avg_value_size,
|
|
1959
|
+
metadata_charge_policy))),
|
|
1960
|
+
length_info_(UsedLengthToLengthInfo(GetStartingLength(capacity))),
|
|
1961
|
+
occupancy_limit_(
|
|
1962
|
+
CalcOccupancyLimit(LengthInfoToUsedLength(length_info_.load()))),
|
|
1963
|
+
clock_pointer_mask_(
|
|
1964
|
+
BottomNBits(UINT64_MAX, LengthInfoToMinShift(length_info_.load()))) {
|
|
1965
|
+
if (metadata_charge_policy ==
|
|
1966
|
+
CacheMetadataChargePolicy::kFullChargeCacheMetadata) {
|
|
1967
|
+
// NOTE: ignoring page boundaries for simplicity
|
|
1968
|
+
usage_ += size_t{GetTableSize()} * sizeof(HandleImpl);
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
static_assert(sizeof(HandleImpl) == 64U,
|
|
1972
|
+
"Expecting size / alignment with common cache line size");
|
|
1973
|
+
|
|
1974
|
+
// Populate head pointers
|
|
1975
|
+
uint64_t length_info = length_info_.load();
|
|
1976
|
+
int min_shift = LengthInfoToMinShift(length_info);
|
|
1977
|
+
int max_shift = min_shift + 1;
|
|
1978
|
+
size_t major = uint64_t{1} << min_shift;
|
|
1979
|
+
size_t used_length = GetTableSize();
|
|
1980
|
+
|
|
1981
|
+
assert(major <= used_length);
|
|
1982
|
+
assert(used_length <= major * 2);
|
|
1983
|
+
|
|
1984
|
+
// Initialize the initial usable set of slots. This slightly odd iteration
|
|
1985
|
+
// order makes it easier to get the correct shift amount on each head.
|
|
1986
|
+
for (size_t i = 0; i < major; ++i) {
|
|
1987
|
+
#ifndef NDEBUG
|
|
1988
|
+
int shift;
|
|
1989
|
+
size_t home;
|
|
1990
|
+
#endif
|
|
1991
|
+
if (major + i < used_length) {
|
|
1992
|
+
array_[i].head_next_with_shift = MakeNextWithShiftEnd(i, max_shift);
|
|
1993
|
+
array_[major + i].head_next_with_shift =
|
|
1994
|
+
MakeNextWithShiftEnd(major + i, max_shift);
|
|
1995
|
+
#ifndef NDEBUG // Extra invariant checking
|
|
1996
|
+
GetHomeIndexAndShift(length_info, i, &home, &shift);
|
|
1997
|
+
assert(home == i);
|
|
1998
|
+
assert(shift == max_shift);
|
|
1999
|
+
GetHomeIndexAndShift(length_info, major + i, &home, &shift);
|
|
2000
|
+
assert(home == major + i);
|
|
2001
|
+
assert(shift == max_shift);
|
|
2002
|
+
#endif
|
|
2003
|
+
} else {
|
|
2004
|
+
array_[i].head_next_with_shift = MakeNextWithShiftEnd(i, min_shift);
|
|
2005
|
+
#ifndef NDEBUG // Extra invariant checking
|
|
2006
|
+
GetHomeIndexAndShift(length_info, i, &home, &shift);
|
|
2007
|
+
assert(home == i);
|
|
2008
|
+
assert(shift == min_shift);
|
|
2009
|
+
GetHomeIndexAndShift(length_info, major + i, &home, &shift);
|
|
2010
|
+
assert(home == i);
|
|
2011
|
+
assert(shift == min_shift);
|
|
2012
|
+
#endif
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
AutoHyperClockTable::~AutoHyperClockTable() {
|
|
2018
|
+
// As usual, destructor assumes there are no references or active operations
|
|
2019
|
+
// on any slot/element in the table.
|
|
2020
|
+
|
|
2021
|
+
// It's possible that there were not enough Insert() after final concurrent
|
|
2022
|
+
// Grow to ensure length_info_ (published GetTableSize()) is fully up to
|
|
2023
|
+
// date. Probe for first unused slot to ensure we see the whole structure.
|
|
2024
|
+
size_t used_end = GetTableSize();
|
|
2025
|
+
while (used_end < array_.Count() &&
|
|
2026
|
+
array_[used_end].head_next_with_shift.load() !=
|
|
2027
|
+
HandleImpl::kUnusedMarker) {
|
|
2028
|
+
used_end++;
|
|
2029
|
+
}
|
|
2030
|
+
#ifndef NDEBUG
|
|
2031
|
+
for (size_t i = used_end; i < array_.Count(); i++) {
|
|
2032
|
+
assert(array_[i].head_next_with_shift.load() == 0);
|
|
2033
|
+
assert(array_[i].chain_next_with_shift.load() == 0);
|
|
2034
|
+
assert(array_[i].meta.load() == 0);
|
|
2035
|
+
}
|
|
2036
|
+
std::vector<bool> was_populated(used_end);
|
|
2037
|
+
std::vector<bool> was_pointed_to(used_end);
|
|
2038
|
+
#endif
|
|
2039
|
+
for (size_t i = 0; i < used_end; i++) {
|
|
2040
|
+
HandleImpl& h = array_[i];
|
|
2041
|
+
switch (h.meta >> ClockHandle::kStateShift) {
|
|
2042
|
+
case ClockHandle::kStateEmpty:
|
|
2043
|
+
// noop
|
|
2044
|
+
break;
|
|
2045
|
+
case ClockHandle::kStateInvisible: // rare but possible
|
|
2046
|
+
case ClockHandle::kStateVisible:
|
|
2047
|
+
assert(GetRefcount(h.meta) == 0);
|
|
2048
|
+
h.FreeData(allocator_);
|
|
2049
|
+
#ifndef NDEBUG // Extra invariant checking
|
|
2050
|
+
usage_.fetch_sub(h.total_charge, std::memory_order_relaxed);
|
|
2051
|
+
occupancy_.fetch_sub(1U, std::memory_order_relaxed);
|
|
2052
|
+
was_populated[i] = true;
|
|
2053
|
+
if (!HandleImpl::IsEnd(h.chain_next_with_shift)) {
|
|
2054
|
+
assert((h.chain_next_with_shift & HandleImpl::kHeadLocked) == 0);
|
|
2055
|
+
size_t next = GetNextFromNextWithShift(h.chain_next_with_shift);
|
|
2056
|
+
assert(!was_pointed_to[next]);
|
|
2057
|
+
was_pointed_to[next] = true;
|
|
2058
|
+
}
|
|
2059
|
+
#endif
|
|
2060
|
+
break;
|
|
2061
|
+
// otherwise
|
|
2062
|
+
default:
|
|
2063
|
+
assert(false);
|
|
2064
|
+
break;
|
|
2065
|
+
}
|
|
2066
|
+
#ifndef NDEBUG // Extra invariant checking
|
|
2067
|
+
if (!HandleImpl::IsEnd(h.head_next_with_shift)) {
|
|
2068
|
+
size_t next = GetNextFromNextWithShift(h.head_next_with_shift);
|
|
2069
|
+
assert(!was_pointed_to[next]);
|
|
2070
|
+
was_pointed_to[next] = true;
|
|
2071
|
+
}
|
|
2072
|
+
#endif
|
|
2073
|
+
}
|
|
2074
|
+
#ifndef NDEBUG // Extra invariant checking
|
|
2075
|
+
// This check is not perfect, but should detect most reasonable cases
|
|
2076
|
+
// of abandonned or floating entries, etc. (A floating cycle would not
|
|
2077
|
+
// be reported as bad.)
|
|
2078
|
+
for (size_t i = 0; i < used_end; i++) {
|
|
2079
|
+
if (was_populated[i]) {
|
|
2080
|
+
assert(was_pointed_to[i]);
|
|
2081
|
+
} else {
|
|
2082
|
+
assert(!was_pointed_to[i]);
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
#endif
|
|
2086
|
+
|
|
2087
|
+
// Metadata charging only follows the published table size
|
|
2088
|
+
assert(usage_.load() == 0 ||
|
|
2089
|
+
usage_.load() == GetTableSize() * sizeof(HandleImpl));
|
|
2090
|
+
assert(occupancy_ == 0);
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
size_t AutoHyperClockTable::GetTableSize() const {
|
|
2094
|
+
return LengthInfoToUsedLength(length_info_.load(std::memory_order_acquire));
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
size_t AutoHyperClockTable::GetOccupancyLimit() const {
|
|
2098
|
+
return occupancy_limit_.load(std::memory_order_acquire);
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
void AutoHyperClockTable::StartInsert(InsertState& state) {
|
|
2102
|
+
state.saved_length_info = length_info_.load(std::memory_order_acquire);
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
// Because we have linked lists, bugs or even hardware errors can make it
|
|
2106
|
+
// possible to create a cycle, which would lead to infinite loop.
|
|
2107
|
+
// Furthermore, when we have retry cases in the code, we want to be sure
|
|
2108
|
+
// these are not (and do not become) spin-wait loops. Given the assumption
|
|
2109
|
+
// of quality hashing and the infeasibility of consistently recurring
|
|
2110
|
+
// concurrent modifications to an entry or chain, we can safely bound the
|
|
2111
|
+
// number of loop iterations in feasible operation, whether following chain
|
|
2112
|
+
// pointers or retrying with some backtracking. A smaller limit is used for
|
|
2113
|
+
// stress testing, to detect potential issues such as cycles or spin-waits,
|
|
2114
|
+
// and a larger limit is used to break cycles should they occur in production.
|
|
2115
|
+
#define CHECK_TOO_MANY_ITERATIONS(i) \
|
|
2116
|
+
{ \
|
|
2117
|
+
assert(i < 768); \
|
|
2118
|
+
if (UNLIKELY(i >= 4096)) { \
|
|
2119
|
+
std::terminate(); \
|
|
2120
|
+
} \
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
bool AutoHyperClockTable::GrowIfNeeded(size_t new_occupancy,
|
|
2124
|
+
InsertState& state) {
|
|
2125
|
+
// new_occupancy has taken into account other threads that are also trying
|
|
2126
|
+
// to insert, so as soon as we see sufficient *published* usable size, we
|
|
2127
|
+
// can declare success even if we aren't the one that grows the table.
|
|
2128
|
+
// However, there's an awkward state where other threads own growing the
|
|
2129
|
+
// table to sufficient usable size, but the udpated size is not yet
|
|
2130
|
+
// published. If we wait, then that likely slows the ramp-up cache
|
|
2131
|
+
// performance. If we unblock ourselves by ensure we grow by at least one
|
|
2132
|
+
// slot, we could technically overshoot required size by number of parallel
|
|
2133
|
+
// threads accessing block cache. On balance considering typical cases and
|
|
2134
|
+
// the modest consequences of table being slightly too large, the latter
|
|
2135
|
+
// seems preferable.
|
|
2136
|
+
//
|
|
2137
|
+
// So if the published occupancy limit is too small, we unblock ourselves
|
|
2138
|
+
// by committing to growing the table by at least one slot. Also note that
|
|
2139
|
+
// we might need to grow more than once to actually increase the occupancy
|
|
2140
|
+
// limit (due to max load factor < 1.0)
|
|
2141
|
+
|
|
2142
|
+
while (UNLIKELY(new_occupancy >
|
|
2143
|
+
occupancy_limit_.load(std::memory_order_relaxed))) {
|
|
2144
|
+
// At this point we commit the thread to growing unless we've reached the
|
|
2145
|
+
// limit (returns false).
|
|
2146
|
+
if (!Grow(state)) {
|
|
2147
|
+
return false;
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
// Success (didn't need to grow, or did successfully)
|
|
2151
|
+
return true;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
bool AutoHyperClockTable::Grow(InsertState& state) {
|
|
2155
|
+
size_t used_length = LengthInfoToUsedLength(state.saved_length_info);
|
|
2156
|
+
|
|
2157
|
+
// Try to take ownership of a grow slot as the first thread to set its
|
|
2158
|
+
// head_next_with_shift to non-zero, specifically a valid empty chain
|
|
2159
|
+
// in case that is to be the final value.
|
|
2160
|
+
// (We don't need to be super efficient here.)
|
|
2161
|
+
size_t grow_home = used_length;
|
|
2162
|
+
int old_shift;
|
|
2163
|
+
for (;; ++grow_home) {
|
|
2164
|
+
if (grow_home >= array_.Count()) {
|
|
2165
|
+
// Can't grow any more.
|
|
2166
|
+
// (Tested by unit test ClockCacheTest/Limits)
|
|
2167
|
+
return false;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
old_shift = FloorLog2(grow_home);
|
|
2171
|
+
assert(old_shift >= 1);
|
|
2172
|
+
|
|
2173
|
+
uint64_t empty_head = MakeNextWithShiftEnd(grow_home, old_shift + 1);
|
|
2174
|
+
uint64_t expected_zero = HandleImpl::kUnusedMarker;
|
|
2175
|
+
bool own = array_[grow_home].head_next_with_shift.compare_exchange_strong(
|
|
2176
|
+
expected_zero, empty_head, std::memory_order_acq_rel);
|
|
2177
|
+
if (own) {
|
|
2178
|
+
assert(array_[grow_home].meta.load(std::memory_order_acquire) == 0);
|
|
2179
|
+
break;
|
|
2180
|
+
} else {
|
|
2181
|
+
// Taken by another thread. Try next slot.
|
|
2182
|
+
assert(expected_zero != 0);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
#ifdef COERCE_CONTEXT_SWITCH
|
|
2186
|
+
// This is useful in reproducing concurrency issues in Grow()
|
|
2187
|
+
while (Random::GetTLSInstance()->OneIn(2)) {
|
|
2188
|
+
std::this_thread::yield();
|
|
2189
|
+
}
|
|
2190
|
+
#endif
|
|
2191
|
+
// Basically, to implement https://en.wikipedia.org/wiki/Linear_hashing
|
|
2192
|
+
// entries that belong in a new chain starting at grow_home will be
|
|
2193
|
+
// split off from the chain starting at old_home, which is computed here.
|
|
2194
|
+
size_t old_home = BottomNBits(grow_home, old_shift);
|
|
2195
|
+
assert(old_home + (size_t{1} << old_shift) == grow_home);
|
|
2196
|
+
|
|
2197
|
+
// Wait here to ensure any Grow operations that would directly feed into
|
|
2198
|
+
// this one are finished, though the full waiting actually completes in
|
|
2199
|
+
// acquiring the rewrite lock for old_home in SplitForGrow.
|
|
2200
|
+
size_t old_old_home = BottomNBits(grow_home, old_shift - 1);
|
|
2201
|
+
for (;;) {
|
|
2202
|
+
uint64_t old_old_head = array_[old_old_home].head_next_with_shift.load(
|
|
2203
|
+
std::memory_order_acquire);
|
|
2204
|
+
if (GetShiftFromNextWithShift(old_old_head) >= old_shift) {
|
|
2205
|
+
if ((old_old_head & HandleImpl::kNextEndFlags) !=
|
|
2206
|
+
HandleImpl::kHeadLocked) {
|
|
2207
|
+
break;
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
// NOTE: one of the few yield-wait loops, which is rare enough in practice
|
|
2211
|
+
// for its performance to be insignificant.
|
|
2212
|
+
yield_count_.fetch_add(1, std::memory_order_relaxed);
|
|
2213
|
+
std::this_thread::yield();
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
// Do the dirty work of splitting the chain, including updating heads and
|
|
2217
|
+
// chain nexts for new shift amounts.
|
|
2218
|
+
SplitForGrow(grow_home, old_home, old_shift);
|
|
2219
|
+
|
|
2220
|
+
// length_info_ can be updated any time after the new shift amount is
|
|
2221
|
+
// published to both heads, potentially before the end of SplitForGrow.
|
|
2222
|
+
// But we also can't update length_info_ until the previous Grow operation
|
|
2223
|
+
// (with grow_home := this grow_home - 1) has published the new shift amount
|
|
2224
|
+
// to both of its heads. However, we don't want to artificially wait here
|
|
2225
|
+
// on that Grow that is otherwise irrelevant.
|
|
2226
|
+
//
|
|
2227
|
+
// We could have each Grow operation advance length_info_ here as far as it
|
|
2228
|
+
// can without waiting, by checking for updated shift on the corresponding
|
|
2229
|
+
// old home and also stopping at an empty head value for possible grow_home.
|
|
2230
|
+
// However, this could increase CPU cache line sharing and in 1/64 cases
|
|
2231
|
+
// bring in an extra page from our mmap.
|
|
2232
|
+
//
|
|
2233
|
+
// Instead, part of the strategy is delegated to DoInsert():
|
|
2234
|
+
// * Here we try to bring length_info_ up to date with this grow_home as
|
|
2235
|
+
// much as we can without waiting. It will fall short if a previous Grow
|
|
2236
|
+
// is still between reserving the grow slot and making the first big step
|
|
2237
|
+
// to publish the new shift amount.
|
|
2238
|
+
// * To avoid length_info_ being perpetually out-of-date (for a small number
|
|
2239
|
+
// of heads) after our last Grow, we do the same when Insert has to "fall
|
|
2240
|
+
// forward" due to length_info_ being out-of-date.
|
|
2241
|
+
CatchUpLengthInfoNoWait(grow_home);
|
|
2242
|
+
|
|
2243
|
+
// See usage in DoInsert()
|
|
2244
|
+
state.likely_empty_slot = grow_home;
|
|
2245
|
+
|
|
2246
|
+
// Success
|
|
2247
|
+
return true;
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
// See call in Grow()
|
|
2251
|
+
void AutoHyperClockTable::CatchUpLengthInfoNoWait(
|
|
2252
|
+
size_t known_usable_grow_home) {
|
|
2253
|
+
uint64_t current_length_info = length_info_.load(std::memory_order_acquire);
|
|
2254
|
+
size_t published_usable_size = LengthInfoToUsedLength(current_length_info);
|
|
2255
|
+
while (published_usable_size <= known_usable_grow_home) {
|
|
2256
|
+
// For when published_usable_size was grow_home
|
|
2257
|
+
size_t next_usable_size = published_usable_size + 1;
|
|
2258
|
+
uint64_t next_length_info = UsedLengthToLengthInfo(next_usable_size);
|
|
2259
|
+
|
|
2260
|
+
// known_usable_grow_home is known to be ready for Lookup/Insert with
|
|
2261
|
+
// the new shift amount, but between that and published usable size, we
|
|
2262
|
+
// need to check.
|
|
2263
|
+
if (published_usable_size < known_usable_grow_home) {
|
|
2264
|
+
int old_shift = FloorLog2(next_usable_size - 1);
|
|
2265
|
+
size_t old_home = BottomNBits(published_usable_size, old_shift);
|
|
2266
|
+
int shift =
|
|
2267
|
+
GetShiftFromNextWithShift(array_[old_home].head_next_with_shift.load(
|
|
2268
|
+
std::memory_order_acquire));
|
|
2269
|
+
if (shift <= old_shift) {
|
|
2270
|
+
// Not ready
|
|
2271
|
+
break;
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
// CAS update length_info_. This only moves in one direction, so if CAS
|
|
2275
|
+
// fails, someone else made progress like we are trying, and we can just
|
|
2276
|
+
// pick up the new value and keep going as appropriate.
|
|
2277
|
+
if (length_info_.compare_exchange_strong(
|
|
2278
|
+
current_length_info, next_length_info, std::memory_order_acq_rel)) {
|
|
2279
|
+
current_length_info = next_length_info;
|
|
2280
|
+
// Update usage_ if metadata charge policy calls for it
|
|
2281
|
+
if (metadata_charge_policy_ ==
|
|
2282
|
+
CacheMetadataChargePolicy::kFullChargeCacheMetadata) {
|
|
2283
|
+
// NOTE: ignoring page boundaries for simplicity
|
|
2284
|
+
usage_.fetch_add(sizeof(HandleImpl), std::memory_order_relaxed);
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
published_usable_size = LengthInfoToUsedLength(current_length_info);
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
// After updating lengh_info_ we can update occupancy_limit_,
|
|
2291
|
+
// allowing for later operations to update it before us.
|
|
2292
|
+
// Note: there is no std::atomic max operation, so we have to use a CAS loop
|
|
2293
|
+
size_t old_occupancy_limit = occupancy_limit_.load(std::memory_order_acquire);
|
|
2294
|
+
size_t new_occupancy_limit = CalcOccupancyLimit(published_usable_size);
|
|
2295
|
+
while (old_occupancy_limit < new_occupancy_limit) {
|
|
2296
|
+
if (occupancy_limit_.compare_exchange_weak(old_occupancy_limit,
|
|
2297
|
+
new_occupancy_limit,
|
|
2298
|
+
std::memory_order_acq_rel)) {
|
|
2299
|
+
break;
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
void AutoHyperClockTable::SplitForGrow(size_t grow_home, size_t old_home,
|
|
2305
|
+
int old_shift) {
|
|
2306
|
+
int new_shift = old_shift + 1;
|
|
2307
|
+
HandleImpl* const arr = array_.Get();
|
|
2308
|
+
|
|
2309
|
+
// We implement a somewhat complicated splitting algorithm to ensure that
|
|
2310
|
+
// entries are always wait-free visible to Lookup, without Lookup needing
|
|
2311
|
+
// to double-check length_info_ to ensure every potentially relevant
|
|
2312
|
+
// existing entry is seen. This works step-by-step, carefully sharing
|
|
2313
|
+
// unmigrated parts of the chain between the source chain and the new
|
|
2314
|
+
// destination chain. This means that Lookup might see a partially migrated
|
|
2315
|
+
// chain so has to take that into consideration when checking that it hasn't
|
|
2316
|
+
// "jumped off" its intended chain (due to a parallel modification to an
|
|
2317
|
+
// "under (de)construction" entry that was found on the chain but has
|
|
2318
|
+
// been reassigned).
|
|
2319
|
+
//
|
|
2320
|
+
// We use a "rewrite lock" on the source and desination chains to exclude
|
|
2321
|
+
// removals from those, and we have a prior waiting step that ensures any Grow
|
|
2322
|
+
// operations feeding into this one have completed. But this process does have
|
|
2323
|
+
// to gracefully handle concurrent insertions to the head of the source chain,
|
|
2324
|
+
// and once marked ready, the destination chain.
|
|
2325
|
+
//
|
|
2326
|
+
// With those considerations, the migration starts with one "big step,"
|
|
2327
|
+
// potentially with retries to deal with insertions in parallel. Part of the
|
|
2328
|
+
// big step is to mark the two chain heads as updated with the new shift
|
|
2329
|
+
// amount, which redirects Lookups to the appropriate new chain.
|
|
2330
|
+
//
|
|
2331
|
+
// After that big step that updates the heads, the rewrite lock makes it
|
|
2332
|
+
// relatively easy to deal with the rest of the migration. Big
|
|
2333
|
+
// simplifications come from being able to read the hashed_key of each
|
|
2334
|
+
// entry on the chain without needing to hold a read reference, and
|
|
2335
|
+
// from never "jumping our to another chain." Concurrent insertions only
|
|
2336
|
+
// happen at the chain head, which is outside of what is left to migrate.
|
|
2337
|
+
//
|
|
2338
|
+
// A series of smaller steps finishes splitting apart the existing chain into
|
|
2339
|
+
// two distinct chains, followed by some steps to fully commit the result.
|
|
2340
|
+
//
|
|
2341
|
+
// Except for trivial cases in which all entries (or remaining entries)
|
|
2342
|
+
// on the input chain go to one output chain, there is an important invariant
|
|
2343
|
+
// after each step of migration, including after the initial "big step":
|
|
2344
|
+
// For each output chain, the "zero chain" (new hash bit is zero) and the
|
|
2345
|
+
// "one chain" (new hash bit is one) we have a "frontier" entry marking the
|
|
2346
|
+
// boundary between what has been migrated and what has not. One of the
|
|
2347
|
+
// frontiers is along the old chain after the other, and all entries between
|
|
2348
|
+
// them are for the same target chain as the earlier frontier. Thus, the
|
|
2349
|
+
// chains share linked list tails starting at the latter frontier. All
|
|
2350
|
+
// pointers from the new head locations to the frontier entries are marked
|
|
2351
|
+
// with the new shift amount, while all pointers after the frontiers use the
|
|
2352
|
+
// old shift amount.
|
|
2353
|
+
//
|
|
2354
|
+
// And after each step there is a strengthening step to reach a stronger
|
|
2355
|
+
// invariant: the frontier earlier in the original chain is advanced to be
|
|
2356
|
+
// immediately before the other frontier.
|
|
2357
|
+
//
|
|
2358
|
+
// Consider this original input chain,
|
|
2359
|
+
//
|
|
2360
|
+
// OldHome -Old-> A0 -Old-> B0 -Old-> A1 -Old-> C0 -Old-> OldHome(End)
|
|
2361
|
+
// GrowHome (empty)
|
|
2362
|
+
//
|
|
2363
|
+
// == BIG STEP ==
|
|
2364
|
+
// The initial big step finds the first entry that will be on the each
|
|
2365
|
+
// output chain (in this case A0 and A1). We use brackets ([]) to mark them
|
|
2366
|
+
// as our prospective frontiers.
|
|
2367
|
+
//
|
|
2368
|
+
// OldHome -Old-> [A0] -Old-> B0 -Old-> [A1] -Old-> C0 -Old-> OldHome(End)
|
|
2369
|
+
// GrowHome (empty)
|
|
2370
|
+
//
|
|
2371
|
+
// Next we speculatively update grow_home head to point to the first entry for
|
|
2372
|
+
// the one chain. This will not be used by Lookup until the head at old_home
|
|
2373
|
+
// uses the new shift amount.
|
|
2374
|
+
//
|
|
2375
|
+
// OldHome -Old-> [A0] -Old-> B0 -Old-> [A1] -Old-> C0 -Old-> OldHome(End)
|
|
2376
|
+
// GrowHome --------------New------------/
|
|
2377
|
+
//
|
|
2378
|
+
// Observe that if Lookup were to use the new head at GrowHome, it would be
|
|
2379
|
+
// able to find all relevant entries. Finishing the initial big step
|
|
2380
|
+
// requires a CAS (compare_exchange) of the OldHome head because there
|
|
2381
|
+
// might have been parallel insertions there, in which case we roll back
|
|
2382
|
+
// and try again. (We might need to point GrowHome head differently.)
|
|
2383
|
+
//
|
|
2384
|
+
// OldHome -New-> [A0] -Old-> B0 -Old-> [A1] -Old-> C0 -Old-> OldHome(End)
|
|
2385
|
+
// GrowHome --------------New------------/
|
|
2386
|
+
//
|
|
2387
|
+
// Upgrading the OldHome head pointer with the new shift amount, with a
|
|
2388
|
+
// compare_exchange, completes the initial big step, with [A0] as zero
|
|
2389
|
+
// chain frontier and [A1] as one chain frontier. Links before the frontiers
|
|
2390
|
+
// use the new shift amount and links after use the old shift amount.
|
|
2391
|
+
// == END BIG STEP==
|
|
2392
|
+
// == STRENGTHENING ==
|
|
2393
|
+
// Zero chain frontier is advanced to [B0] (immediately before other
|
|
2394
|
+
// frontier) by updating pointers with new shift amounts.
|
|
2395
|
+
//
|
|
2396
|
+
// OldHome -New-> A0 -New-> [B0] -Old-> [A1] -Old-> C0 -Old-> OldHome(End)
|
|
2397
|
+
// GrowHome -------------New-----------/
|
|
2398
|
+
//
|
|
2399
|
+
// == END STRENGTHENING ==
|
|
2400
|
+
// == SMALL STEP #1 ==
|
|
2401
|
+
// From the strong invariant state, we need to find the next entry for
|
|
2402
|
+
// the new chain with the earlier frontier. In this case, we need to find
|
|
2403
|
+
// the next entry for the zero chain that comes after [B0], which in this
|
|
2404
|
+
// case is C0. This will be our next zero chain frontier, at least under
|
|
2405
|
+
// the weak invariant. To get there, we simply update the link between
|
|
2406
|
+
// the current two frontiers to skip over the entries irreleveant to the
|
|
2407
|
+
// ealier frontier chain. In this case, the zero chain skips over A1. As a
|
|
2408
|
+
// result, he other chain is now the "earlier."
|
|
2409
|
+
//
|
|
2410
|
+
// OldHome -New-> A0 -New-> B0 -New-> [C0] -Old-> OldHome(End)
|
|
2411
|
+
// GrowHome -New-> [A1] ------Old-----/
|
|
2412
|
+
//
|
|
2413
|
+
// == END SMALL STEP #1 ==
|
|
2414
|
+
//
|
|
2415
|
+
// Repeating the cycle and end handling is not as interesting.
|
|
2416
|
+
|
|
2417
|
+
// Acquire rewrite lock on zero chain (if it's non-empty)
|
|
2418
|
+
ChainRewriteLock zero_head_lock(&arr[old_home], yield_count_);
|
|
2419
|
+
// Create an RAII wrapper for one chain rewrite lock, for once it becomes
|
|
2420
|
+
// non-empty. This head is unused by Lookup and DoInsert until the zero
|
|
2421
|
+
// head is updated with new shift amount.
|
|
2422
|
+
ChainRewriteLock one_head_lock(&arr[grow_home], yield_count_,
|
|
2423
|
+
/*already_locked_or_end=*/true);
|
|
2424
|
+
assert(one_head_lock.IsEnd());
|
|
2425
|
+
|
|
2426
|
+
// old_home will also the head of the new "zero chain" -- all entries in the
|
|
2427
|
+
// "from" chain whose next hash bit is 0. grow_home will be head of the new
|
|
2428
|
+
// "one chain".
|
|
2429
|
+
|
|
2430
|
+
// For these, SIZE_MAX is like nullptr (unknown)
|
|
2431
|
+
size_t zero_chain_frontier = SIZE_MAX;
|
|
2432
|
+
size_t one_chain_frontier = SIZE_MAX;
|
|
2433
|
+
size_t cur = SIZE_MAX;
|
|
2434
|
+
|
|
2435
|
+
// Set to 0 (zero chain frontier earlier), 1 (one chain), or -1 (unknown)
|
|
2436
|
+
int chain_frontier_first = -1;
|
|
2437
|
+
|
|
2438
|
+
// Might need to retry initial update of heads
|
|
2439
|
+
for (int i = 0;; ++i) {
|
|
2440
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
2441
|
+
assert(zero_chain_frontier == SIZE_MAX);
|
|
2442
|
+
assert(one_chain_frontier == SIZE_MAX);
|
|
2443
|
+
assert(cur == SIZE_MAX);
|
|
2444
|
+
assert(chain_frontier_first == -1);
|
|
2445
|
+
|
|
2446
|
+
uint64_t next_with_shift = zero_head_lock.GetNewHead();
|
|
2447
|
+
|
|
2448
|
+
// Find a single representative for each target chain, or scan the whole
|
|
2449
|
+
// chain if some target chain has no representative.
|
|
2450
|
+
for (;; ++i) {
|
|
2451
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
2452
|
+
|
|
2453
|
+
// Loop invariants
|
|
2454
|
+
assert((chain_frontier_first < 0) == (zero_chain_frontier == SIZE_MAX &&
|
|
2455
|
+
one_chain_frontier == SIZE_MAX));
|
|
2456
|
+
assert((cur == SIZE_MAX) == (zero_chain_frontier == SIZE_MAX &&
|
|
2457
|
+
one_chain_frontier == SIZE_MAX));
|
|
2458
|
+
|
|
2459
|
+
assert(GetShiftFromNextWithShift(next_with_shift) == old_shift);
|
|
2460
|
+
|
|
2461
|
+
// Check for end of original chain
|
|
2462
|
+
if (HandleImpl::IsEnd(next_with_shift)) {
|
|
2463
|
+
cur = SIZE_MAX;
|
|
2464
|
+
break;
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
// next_with_shift is not End
|
|
2468
|
+
cur = GetNextFromNextWithShift(next_with_shift);
|
|
2469
|
+
|
|
2470
|
+
if (BottomNBits(arr[cur].hashed_key[1], new_shift) == old_home) {
|
|
2471
|
+
// Entry for zero chain
|
|
2472
|
+
if (zero_chain_frontier == SIZE_MAX) {
|
|
2473
|
+
zero_chain_frontier = cur;
|
|
2474
|
+
if (one_chain_frontier != SIZE_MAX) {
|
|
2475
|
+
// Ready to update heads
|
|
2476
|
+
break;
|
|
2477
|
+
}
|
|
2478
|
+
// Nothing yet for one chain
|
|
2479
|
+
chain_frontier_first = 0;
|
|
2480
|
+
}
|
|
2481
|
+
} else {
|
|
2482
|
+
assert(BottomNBits(arr[cur].hashed_key[1], new_shift) == grow_home);
|
|
2483
|
+
// Entry for one chain
|
|
2484
|
+
if (one_chain_frontier == SIZE_MAX) {
|
|
2485
|
+
one_chain_frontier = cur;
|
|
2486
|
+
if (zero_chain_frontier != SIZE_MAX) {
|
|
2487
|
+
// Ready to update heads
|
|
2488
|
+
break;
|
|
2489
|
+
}
|
|
2490
|
+
// Nothing yet for zero chain
|
|
2491
|
+
chain_frontier_first = 1;
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
next_with_shift =
|
|
2496
|
+
arr[cur].chain_next_with_shift.load(std::memory_order_acquire);
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
// Try to update heads for initial migration info
|
|
2500
|
+
// We only reached the end of the migrate-from chain already if one of the
|
|
2501
|
+
// target chains will be empty.
|
|
2502
|
+
assert((cur == SIZE_MAX) ==
|
|
2503
|
+
(zero_chain_frontier == SIZE_MAX || one_chain_frontier == SIZE_MAX));
|
|
2504
|
+
assert((chain_frontier_first < 0) ==
|
|
2505
|
+
(zero_chain_frontier == SIZE_MAX && one_chain_frontier == SIZE_MAX));
|
|
2506
|
+
|
|
2507
|
+
// Always update one chain's head first (safe).
|
|
2508
|
+
one_head_lock.SimpleUpdate(
|
|
2509
|
+
one_chain_frontier != SIZE_MAX
|
|
2510
|
+
? MakeNextWithShift(one_chain_frontier, new_shift)
|
|
2511
|
+
: MakeNextWithShiftEnd(grow_home, new_shift));
|
|
2512
|
+
|
|
2513
|
+
// Make sure length_info_ hasn't been updated too early, as we're about
|
|
2514
|
+
// to make the change that makes it safe to update (e.g. in DoInsert())
|
|
2515
|
+
assert(LengthInfoToUsedLength(
|
|
2516
|
+
length_info_.load(std::memory_order_acquire)) <= grow_home);
|
|
2517
|
+
|
|
2518
|
+
// Try to set zero's head.
|
|
2519
|
+
if (zero_head_lock.CasUpdate(
|
|
2520
|
+
zero_chain_frontier != SIZE_MAX
|
|
2521
|
+
? MakeNextWithShift(zero_chain_frontier, new_shift)
|
|
2522
|
+
: MakeNextWithShiftEnd(old_home, new_shift),
|
|
2523
|
+
yield_count_)) {
|
|
2524
|
+
// Both heads successfully updated to new shift
|
|
2525
|
+
break;
|
|
2526
|
+
} else {
|
|
2527
|
+
// Concurrent insertion. This should not happen too many times.
|
|
2528
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
2529
|
+
// The easiest solution is to restart.
|
|
2530
|
+
zero_chain_frontier = SIZE_MAX;
|
|
2531
|
+
one_chain_frontier = SIZE_MAX;
|
|
2532
|
+
cur = SIZE_MAX;
|
|
2533
|
+
chain_frontier_first = -1;
|
|
2534
|
+
continue;
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
// Except for trivial cases, we have something like
|
|
2539
|
+
// AHome -New-> [A0] -Old-> [B0] -Old-> [C0] \ |
|
|
2540
|
+
// BHome --------------------New------------> [A1] -Old-> ...
|
|
2541
|
+
// And we need to upgrade as much as we can on the "first" chain
|
|
2542
|
+
// (the one eventually pointing to the other's frontier). This will
|
|
2543
|
+
// also finish off any case in which one of the target chains will be empty.
|
|
2544
|
+
if (chain_frontier_first >= 0) {
|
|
2545
|
+
size_t& first_frontier = chain_frontier_first == 0
|
|
2546
|
+
? /*&*/ zero_chain_frontier
|
|
2547
|
+
: /*&*/ one_chain_frontier;
|
|
2548
|
+
size_t& other_frontier = chain_frontier_first != 0
|
|
2549
|
+
? /*&*/ zero_chain_frontier
|
|
2550
|
+
: /*&*/ one_chain_frontier;
|
|
2551
|
+
uint64_t stop_before_or_new_tail =
|
|
2552
|
+
other_frontier != SIZE_MAX
|
|
2553
|
+
? /*stop before*/ MakeNextWithShift(other_frontier, old_shift)
|
|
2554
|
+
: /*new tail*/ MakeNextWithShiftEnd(
|
|
2555
|
+
chain_frontier_first == 0 ? old_home : grow_home, new_shift);
|
|
2556
|
+
UpgradeShiftsOnRange(arr, first_frontier, stop_before_or_new_tail,
|
|
2557
|
+
old_shift, new_shift);
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
if (zero_chain_frontier == SIZE_MAX) {
|
|
2561
|
+
// Already finished migrating
|
|
2562
|
+
assert(one_chain_frontier == SIZE_MAX);
|
|
2563
|
+
assert(cur == SIZE_MAX);
|
|
2564
|
+
} else {
|
|
2565
|
+
// Still need to migrate between two target chains
|
|
2566
|
+
for (int i = 0;; ++i) {
|
|
2567
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
2568
|
+
// Overall loop invariants
|
|
2569
|
+
assert(zero_chain_frontier != SIZE_MAX);
|
|
2570
|
+
assert(one_chain_frontier != SIZE_MAX);
|
|
2571
|
+
assert(cur != SIZE_MAX);
|
|
2572
|
+
assert(chain_frontier_first >= 0);
|
|
2573
|
+
size_t& first_frontier = chain_frontier_first == 0
|
|
2574
|
+
? /*&*/ zero_chain_frontier
|
|
2575
|
+
: /*&*/ one_chain_frontier;
|
|
2576
|
+
size_t& other_frontier = chain_frontier_first != 0
|
|
2577
|
+
? /*&*/ zero_chain_frontier
|
|
2578
|
+
: /*&*/ one_chain_frontier;
|
|
2579
|
+
assert(cur != first_frontier);
|
|
2580
|
+
assert(GetNextFromNextWithShift(
|
|
2581
|
+
arr[first_frontier].chain_next_with_shift.load(
|
|
2582
|
+
std::memory_order_acquire)) == other_frontier);
|
|
2583
|
+
|
|
2584
|
+
uint64_t next_with_shift =
|
|
2585
|
+
arr[cur].chain_next_with_shift.load(std::memory_order_acquire);
|
|
2586
|
+
|
|
2587
|
+
// Check for end of original chain
|
|
2588
|
+
if (HandleImpl::IsEnd(next_with_shift)) {
|
|
2589
|
+
// Can set upgraded tail on first chain
|
|
2590
|
+
uint64_t first_new_tail = MakeNextWithShiftEnd(
|
|
2591
|
+
chain_frontier_first == 0 ? old_home : grow_home, new_shift);
|
|
2592
|
+
arr[first_frontier].chain_next_with_shift.store(
|
|
2593
|
+
first_new_tail, std::memory_order_release);
|
|
2594
|
+
// And upgrade remainder of other chain
|
|
2595
|
+
uint64_t other_new_tail = MakeNextWithShiftEnd(
|
|
2596
|
+
chain_frontier_first != 0 ? old_home : grow_home, new_shift);
|
|
2597
|
+
UpgradeShiftsOnRange(arr, other_frontier, other_new_tail, old_shift,
|
|
2598
|
+
new_shift);
|
|
2599
|
+
assert(other_frontier == SIZE_MAX); // Finished
|
|
2600
|
+
break;
|
|
2601
|
+
}
|
|
2602
|
+
|
|
2603
|
+
// next_with_shift is not End
|
|
2604
|
+
cur = GetNextFromNextWithShift(next_with_shift);
|
|
2605
|
+
|
|
2606
|
+
int target_chain;
|
|
2607
|
+
if (BottomNBits(arr[cur].hashed_key[1], new_shift) == old_home) {
|
|
2608
|
+
// Entry for zero chain
|
|
2609
|
+
target_chain = 0;
|
|
2610
|
+
} else {
|
|
2611
|
+
assert(BottomNBits(arr[cur].hashed_key[1], new_shift) == grow_home);
|
|
2612
|
+
// Entry for one chain
|
|
2613
|
+
target_chain = 1;
|
|
2614
|
+
}
|
|
2615
|
+
if (target_chain == chain_frontier_first) {
|
|
2616
|
+
// Found next entry to skip to on the first chain
|
|
2617
|
+
uint64_t skip_to = MakeNextWithShift(cur, new_shift);
|
|
2618
|
+
arr[first_frontier].chain_next_with_shift.store(
|
|
2619
|
+
skip_to, std::memory_order_release);
|
|
2620
|
+
first_frontier = cur;
|
|
2621
|
+
// Upgrade other chain up to entry before that one
|
|
2622
|
+
UpgradeShiftsOnRange(arr, other_frontier, next_with_shift, old_shift,
|
|
2623
|
+
new_shift);
|
|
2624
|
+
// Swap which is marked as first
|
|
2625
|
+
chain_frontier_first = 1 - chain_frontier_first;
|
|
2626
|
+
} else {
|
|
2627
|
+
// Nothing to do yet, as we need to keep old generation pointers in
|
|
2628
|
+
// place for lookups
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
|
|
2634
|
+
// Variant of PurgeImplLocked: Removes all "under (de) construction" entries
|
|
2635
|
+
// from a chain where already holding a rewrite lock
|
|
2636
|
+
using PurgeLockedOpData = void;
|
|
2637
|
+
// Variant of PurgeImplLocked: Clock-updates all entries in a chain, in
|
|
2638
|
+
// addition to functionality of PurgeLocked, where already holding a rewrite
|
|
2639
|
+
// lock. (Caller finalizes eviction on entries added to the autovector, in part
|
|
2640
|
+
// so that we don't hold the rewrite lock while doing potentially expensive
|
|
2641
|
+
// callback and allocator free.)
|
|
2642
|
+
using ClockUpdateChainLockedOpData =
|
|
2643
|
+
autovector<AutoHyperClockTable::HandleImpl*>;
|
|
2644
|
+
|
|
2645
|
+
template <class OpData>
|
|
2646
|
+
void AutoHyperClockTable::PurgeImplLocked(OpData* op_data,
|
|
2647
|
+
ChainRewriteLock& rewrite_lock,
|
|
2648
|
+
size_t home) {
|
|
2649
|
+
constexpr bool kIsPurge = std::is_same_v<OpData, PurgeLockedOpData>;
|
|
2650
|
+
constexpr bool kIsClockUpdateChain =
|
|
2651
|
+
std::is_same_v<OpData, ClockUpdateChainLockedOpData>;
|
|
2652
|
+
|
|
2653
|
+
// Exactly one op specified
|
|
2654
|
+
static_assert(kIsPurge + kIsClockUpdateChain == 1);
|
|
2655
|
+
|
|
2656
|
+
HandleImpl* const arr = array_.Get();
|
|
2657
|
+
|
|
2658
|
+
uint64_t next_with_shift = rewrite_lock.GetNewHead();
|
|
2659
|
+
assert(!HandleImpl::IsEnd(next_with_shift));
|
|
2660
|
+
int home_shift = GetShiftFromNextWithShift(next_with_shift);
|
|
2661
|
+
(void)home;
|
|
2662
|
+
(void)home_shift;
|
|
2663
|
+
size_t next = GetNextFromNextWithShift(next_with_shift);
|
|
2664
|
+
assert(next < array_.Count());
|
|
2665
|
+
HandleImpl* h = &arr[next];
|
|
2666
|
+
HandleImpl* prev_to_keep = nullptr;
|
|
2667
|
+
#ifndef NDEBUG
|
|
2668
|
+
uint64_t prev_to_keep_next_with_shift = 0;
|
|
2669
|
+
#endif
|
|
2670
|
+
// Whether there are entries between h and prev_to_keep that should be
|
|
2671
|
+
// purged from the chain.
|
|
2672
|
+
bool pending_purge = false;
|
|
2673
|
+
|
|
2674
|
+
// Walk the chain, and stitch together any entries that are still
|
|
2675
|
+
// "shareable," possibly after clock update. prev_to_keep tells us where
|
|
2676
|
+
// the last "stitch back to" location is (nullptr => head).
|
|
2677
|
+
for (size_t i = 0;; ++i) {
|
|
2678
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
2679
|
+
|
|
2680
|
+
bool purgeable = false;
|
|
2681
|
+
// In last iteration, h will be nullptr, to stitch together the tail of
|
|
2682
|
+
// the chain.
|
|
2683
|
+
if (h) {
|
|
2684
|
+
// NOTE: holding a rewrite lock on the chain prevents any "under
|
|
2685
|
+
// (de)construction" entries in the chain from being marked empty, which
|
|
2686
|
+
// allows us to access the hashed_keys without holding a read ref.
|
|
2687
|
+
assert(home == BottomNBits(h->hashed_key[1], home_shift));
|
|
2688
|
+
if constexpr (kIsClockUpdateChain) {
|
|
2689
|
+
// Clock update and/or check for purgeable (under (de)construction)
|
|
2690
|
+
if (ClockUpdate(*h, &purgeable)) {
|
|
2691
|
+
// Remember for finishing eviction
|
|
2692
|
+
op_data->push_back(h);
|
|
2693
|
+
// Entries for eviction become purgeable
|
|
2694
|
+
purgeable = true;
|
|
2695
|
+
assert((h->meta.load(std::memory_order_acquire) >>
|
|
2696
|
+
ClockHandle::kStateShift) == ClockHandle::kStateConstruction);
|
|
2697
|
+
}
|
|
2698
|
+
} else {
|
|
2699
|
+
(void)op_data;
|
|
2700
|
+
purgeable = ((h->meta.load(std::memory_order_acquire) >>
|
|
2701
|
+
ClockHandle::kStateShift) &
|
|
2702
|
+
ClockHandle::kStateShareableBit) == 0;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2706
|
+
if (purgeable) {
|
|
2707
|
+
assert((h->meta.load(std::memory_order_acquire) >>
|
|
2708
|
+
ClockHandle::kStateShift) == ClockHandle::kStateConstruction);
|
|
2709
|
+
pending_purge = true;
|
|
2710
|
+
} else if (pending_purge) {
|
|
2711
|
+
if (prev_to_keep) {
|
|
2712
|
+
// Update chain next to skip purgeable entries
|
|
2713
|
+
assert(prev_to_keep->chain_next_with_shift.load(
|
|
2714
|
+
std::memory_order_acquire) == prev_to_keep_next_with_shift);
|
|
2715
|
+
prev_to_keep->chain_next_with_shift.store(next_with_shift,
|
|
2716
|
+
std::memory_order_release);
|
|
2717
|
+
} else if (rewrite_lock.CasUpdate(next_with_shift, yield_count_)) {
|
|
2718
|
+
// Managed to update head without any parallel insertions
|
|
2719
|
+
} else {
|
|
2720
|
+
// Parallel insertion must have interfered. Need to do a purge
|
|
2721
|
+
// from updated head to here. Since we have no prev_to_keep, there's
|
|
2722
|
+
// no risk of duplicate clock updates to entries. Any entries already
|
|
2723
|
+
// updated must have been evicted (purgeable) and it's OK to clock
|
|
2724
|
+
// update any new entries just inserted in parallel.
|
|
2725
|
+
// Can simply restart (GetNewHead() already updated from CAS failure).
|
|
2726
|
+
next_with_shift = rewrite_lock.GetNewHead();
|
|
2727
|
+
assert(!HandleImpl::IsEnd(next_with_shift));
|
|
2728
|
+
next = GetNextFromNextWithShift(next_with_shift);
|
|
2729
|
+
assert(next < array_.Count());
|
|
2730
|
+
h = &arr[next];
|
|
2731
|
+
pending_purge = false;
|
|
2732
|
+
assert(prev_to_keep == nullptr);
|
|
2733
|
+
assert(GetShiftFromNextWithShift(next_with_shift) == home_shift);
|
|
2734
|
+
continue;
|
|
2735
|
+
}
|
|
2736
|
+
pending_purge = false;
|
|
2737
|
+
prev_to_keep = h;
|
|
2738
|
+
} else {
|
|
2739
|
+
prev_to_keep = h;
|
|
2740
|
+
}
|
|
2741
|
+
|
|
2742
|
+
if (h == nullptr) {
|
|
2743
|
+
// Reached end of the chain
|
|
2744
|
+
return;
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
// Read chain pointer
|
|
2748
|
+
next_with_shift = h->chain_next_with_shift.load(std::memory_order_acquire);
|
|
2749
|
+
#ifndef NDEBUG
|
|
2750
|
+
if (prev_to_keep == h) {
|
|
2751
|
+
prev_to_keep_next_with_shift = next_with_shift;
|
|
2752
|
+
}
|
|
2753
|
+
#endif
|
|
2754
|
+
|
|
2755
|
+
assert(GetShiftFromNextWithShift(next_with_shift) == home_shift);
|
|
2756
|
+
|
|
2757
|
+
// Check for end marker
|
|
2758
|
+
if (HandleImpl::IsEnd(next_with_shift)) {
|
|
2759
|
+
h = nullptr;
|
|
2760
|
+
} else {
|
|
2761
|
+
next = GetNextFromNextWithShift(next_with_shift);
|
|
2762
|
+
assert(next < array_.Count());
|
|
2763
|
+
h = &arr[next];
|
|
2764
|
+
assert(h != prev_to_keep);
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
// Variant of PurgeImpl: Removes all "under (de) construction" entries in a
|
|
2770
|
+
// chain, such that any entry with the given key must have been purged.
|
|
2771
|
+
using PurgeOpData = const UniqueId64x2;
|
|
2772
|
+
// Variant of PurgeImpl: Clock-updates all entries in a chain, in addition to
|
|
2773
|
+
// purging as appropriate. (Caller finalizes eviction on entries added to the
|
|
2774
|
+
// autovector, in part so that we don't hold the rewrite lock while doing
|
|
2775
|
+
// potentially expensive callback and allocator free.)
|
|
2776
|
+
using ClockUpdateChainOpData = ClockUpdateChainLockedOpData;
|
|
2777
|
+
|
|
2778
|
+
template <class OpData>
|
|
2779
|
+
void AutoHyperClockTable::PurgeImpl(OpData* op_data, size_t home) {
|
|
2780
|
+
// Early efforts to make AutoHCC fully wait-free ran into too many problems
|
|
2781
|
+
// that needed obscure and potentially inefficient work-arounds to have a
|
|
2782
|
+
// chance at working.
|
|
2783
|
+
//
|
|
2784
|
+
// The implementation settled on "essentially wait-free" which can be
|
|
2785
|
+
// achieved by locking at the level of each probing chain and only for
|
|
2786
|
+
// operations that might remove entries from the chain. Because parallel
|
|
2787
|
+
// clock updates and Grow operations are ordered, contention is very rare.
|
|
2788
|
+
// However, parallel insertions at any chain head have to be accommodated
|
|
2789
|
+
// to keep them wait-free.
|
|
2790
|
+
//
|
|
2791
|
+
// This function implements Purge and ClockUpdateChain functions (see above
|
|
2792
|
+
// OpData type definitions) as part of higher-level operations. This function
|
|
2793
|
+
// ensures the correct chain is (eventually) covered and handles rewrite
|
|
2794
|
+
// locking the chain. PurgeImplLocked has lower level details.
|
|
2795
|
+
//
|
|
2796
|
+
// In general, these operations and Grow are kept simpler by allowing eager
|
|
2797
|
+
// purging of under (de-)construction entries. For example, an Erase
|
|
2798
|
+
// operation might find that another thread has purged the entry from the
|
|
2799
|
+
// chain by the time its own purge operation acquires the rewrite lock and
|
|
2800
|
+
// proceeds. This is OK, and potentially reduces the number of lock/unlock
|
|
2801
|
+
// cycles because empty chains are not rewrite-lockable.
|
|
2802
|
+
|
|
2803
|
+
constexpr bool kIsPurge = std::is_same_v<OpData, PurgeOpData>;
|
|
2804
|
+
constexpr bool kIsClockUpdateChain =
|
|
2805
|
+
std::is_same_v<OpData, ClockUpdateChainOpData>;
|
|
2806
|
+
|
|
2807
|
+
// Exactly one op specified
|
|
2808
|
+
static_assert(kIsPurge + kIsClockUpdateChain == 1);
|
|
2809
|
+
|
|
2810
|
+
int home_shift = 0;
|
|
2811
|
+
if constexpr (kIsPurge) {
|
|
2812
|
+
// Purge callers leave home unspecified, to be determined from key
|
|
2813
|
+
assert(home == SIZE_MAX);
|
|
2814
|
+
GetHomeIndexAndShift(length_info_.load(std::memory_order_acquire),
|
|
2815
|
+
(*op_data)[1], &home, &home_shift);
|
|
2816
|
+
assert(home_shift > 0);
|
|
2817
|
+
} else {
|
|
2818
|
+
// Evict callers must specify home
|
|
2819
|
+
assert(home < SIZE_MAX);
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
HandleImpl* const arr = array_.Get();
|
|
2823
|
+
|
|
2824
|
+
// Acquire the RAII rewrite lock (if not an empty chain)
|
|
2825
|
+
ChainRewriteLock rewrite_lock(&arr[home], yield_count_);
|
|
2826
|
+
|
|
2827
|
+
int shift;
|
|
2828
|
+
for (;;) {
|
|
2829
|
+
shift = GetShiftFromNextWithShift(rewrite_lock.GetNewHead());
|
|
2830
|
+
|
|
2831
|
+
if constexpr (kIsPurge) {
|
|
2832
|
+
if (shift > home_shift) {
|
|
2833
|
+
// At head. Thus, we know the newer shift applies to us.
|
|
2834
|
+
// Newer shift might not yet be reflected in length_info_ (an atomicity
|
|
2835
|
+
// gap in Grow), so operate as if it is. Note that other insertions
|
|
2836
|
+
// could happen using this shift before length_info_ is updated, and
|
|
2837
|
+
// it's possible (though unlikely) that multiple generations of Grow
|
|
2838
|
+
// have occurred. If shift is more than one generation ahead of
|
|
2839
|
+
// home_shift, it's possible that not all descendent homes have
|
|
2840
|
+
// reached the `shift` generation. Thus, we need to advance only one
|
|
2841
|
+
// shift at a time looking for a home+head with a matching shift
|
|
2842
|
+
// amount.
|
|
2843
|
+
home_shift++;
|
|
2844
|
+
home = GetHomeIndex((*op_data)[1], home_shift);
|
|
2845
|
+
rewrite_lock.Reset(&arr[home], yield_count_);
|
|
2846
|
+
continue;
|
|
2847
|
+
} else {
|
|
2848
|
+
assert(shift == home_shift);
|
|
2849
|
+
}
|
|
2850
|
+
} else {
|
|
2851
|
+
assert(home_shift == 0);
|
|
2852
|
+
home_shift = shift;
|
|
2853
|
+
}
|
|
2854
|
+
break;
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
// If the chain is empty, nothing to do
|
|
2858
|
+
if (!rewrite_lock.IsEnd()) {
|
|
2859
|
+
if constexpr (kIsPurge) {
|
|
2860
|
+
PurgeLockedOpData* locked_op_data{};
|
|
2861
|
+
PurgeImplLocked(locked_op_data, rewrite_lock, home);
|
|
2862
|
+
} else {
|
|
2863
|
+
PurgeImplLocked(op_data, rewrite_lock, home);
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2868
|
+
AutoHyperClockTable::HandleImpl* AutoHyperClockTable::DoInsert(
|
|
2869
|
+
const ClockHandleBasicData& proto, uint64_t initial_countdown,
|
|
2870
|
+
bool take_ref, InsertState& state) {
|
|
2871
|
+
size_t home;
|
|
2872
|
+
int orig_home_shift;
|
|
2873
|
+
GetHomeIndexAndShift(state.saved_length_info, proto.hashed_key[1], &home,
|
|
2874
|
+
&orig_home_shift);
|
|
2875
|
+
HandleImpl* const arr = array_.Get();
|
|
2876
|
+
|
|
2877
|
+
// We could go searching through the chain for any duplicate, but that's
|
|
2878
|
+
// not typically helpful, except for the REDUNDANT block cache stats.
|
|
2879
|
+
// (Inferior duplicates will age out with eviction.) However, we do skip
|
|
2880
|
+
// insertion if the home slot (or some other we happen to probe) already
|
|
2881
|
+
// has a match (already_matches below). This helps to keep better locality
|
|
2882
|
+
// when we can.
|
|
2883
|
+
//
|
|
2884
|
+
// And we can do that as part of searching for an available slot to
|
|
2885
|
+
// insert the new entry, because our preferred location and first slot
|
|
2886
|
+
// checked will be the home slot.
|
|
2887
|
+
//
|
|
2888
|
+
// As the table initially grows to size, few entries will be in the same
|
|
2889
|
+
// cache line as the chain head. However, churn in the cache relatively
|
|
2890
|
+
// quickly improves the proportion of entries sharing that cache line with
|
|
2891
|
+
// the chain head. Data:
|
|
2892
|
+
//
|
|
2893
|
+
// Initial population only: (cache_bench with -ops_per_thread=1)
|
|
2894
|
+
// Entries at home count: 29,202 (out of 129,170 entries in 94,411 chains)
|
|
2895
|
+
// Approximate average cache lines read to find an existing entry:
|
|
2896
|
+
// 129.2 / 94.4 [without the heads]
|
|
2897
|
+
// + (94.4 - 29.2) / 94.4 [the heads not included with entries]
|
|
2898
|
+
// = 2.06 cache lines
|
|
2899
|
+
//
|
|
2900
|
+
// After 10 million ops: (-threads=10 -ops_per_thread=100000)
|
|
2901
|
+
// Entries at home count: 67,556 (out of 129,359 entries in 94,756 chains)
|
|
2902
|
+
// That's a majority of entries and more than 2/3rds of chains.
|
|
2903
|
+
// Approximate average cache lines read to find an existing entry:
|
|
2904
|
+
// = 1.65 cache lines
|
|
2905
|
+
|
|
2906
|
+
size_t used_length = LengthInfoToUsedLength(state.saved_length_info);
|
|
2907
|
+
assert(home < used_length);
|
|
2908
|
+
|
|
2909
|
+
size_t idx = home;
|
|
2910
|
+
bool already_matches = false;
|
|
2911
|
+
bool already_matches_ignore = false;
|
|
2912
|
+
if (TryInsert(proto, arr[idx], initial_countdown, take_ref,
|
|
2913
|
+
&already_matches)) {
|
|
2914
|
+
assert(idx == home);
|
|
2915
|
+
} else if (already_matches) {
|
|
2916
|
+
return nullptr;
|
|
2917
|
+
// Here we try to populate newly-opened slots in the table, but not
|
|
2918
|
+
// when we can add something to its home slot. This makes the structure
|
|
2919
|
+
// more performant more quickly on (initial) growth. We ignore "already
|
|
2920
|
+
// matches" in this case because it is unlikely and difficult to
|
|
2921
|
+
// incorporate logic for here cleanly and efficiently.
|
|
2922
|
+
} else if (UNLIKELY(state.likely_empty_slot > 0) &&
|
|
2923
|
+
TryInsert(proto, arr[state.likely_empty_slot], initial_countdown,
|
|
2924
|
+
take_ref, &already_matches_ignore)) {
|
|
2925
|
+
idx = state.likely_empty_slot;
|
|
2926
|
+
} else {
|
|
2927
|
+
// We need to search for an available slot outside of the home.
|
|
2928
|
+
// Linear hashing provides nice resizing but does typically mean
|
|
2929
|
+
// that some heads (home locations) have (in expectation) twice as
|
|
2930
|
+
// many entries mapped to them as other heads. For example if the
|
|
2931
|
+
// usable length is 80, then heads 16-63 are (in expectation) twice
|
|
2932
|
+
// as loaded as heads 0-15 and 64-79, which are using another hash bit.
|
|
2933
|
+
//
|
|
2934
|
+
// This means that if we just use linear probing (by a small constant)
|
|
2935
|
+
// to find an available slot, part of the structure could easily fill up
|
|
2936
|
+
// and resort to linear time operations even when the overall load factor
|
|
2937
|
+
// is only modestly high, like 70%. Even though each slot has its own CPU
|
|
2938
|
+
// cache line, there appears to be a small locality benefit (e.g. TLB and
|
|
2939
|
+
// paging) to iterating one by one, as long as we don't afoul of the
|
|
2940
|
+
// linear hashing imbalance.
|
|
2941
|
+
//
|
|
2942
|
+
// In a traditional non-concurrent structure, we could keep a "free list"
|
|
2943
|
+
// to ensure immediate access to an available slot, but maintaining such
|
|
2944
|
+
// a structure could require more cross-thread coordination to ensure
|
|
2945
|
+
// all entries are eventually available to all threads.
|
|
2946
|
+
//
|
|
2947
|
+
// The way we solve this problem is to use unit-increment linear probing
|
|
2948
|
+
// with a small bound, and then fall back on big jumps to have a good
|
|
2949
|
+
// chance of finding a slot in an under-populated region quickly if that
|
|
2950
|
+
// doesn't work.
|
|
2951
|
+
size_t i = 0;
|
|
2952
|
+
constexpr size_t kMaxLinearProbe = 4;
|
|
2953
|
+
for (; i < kMaxLinearProbe; i++) {
|
|
2954
|
+
idx++;
|
|
2955
|
+
if (idx >= used_length) {
|
|
2956
|
+
idx -= used_length;
|
|
2957
|
+
}
|
|
2958
|
+
if (TryInsert(proto, arr[idx], initial_countdown, take_ref,
|
|
2959
|
+
&already_matches)) {
|
|
2960
|
+
break;
|
|
2961
|
+
}
|
|
2962
|
+
if (already_matches) {
|
|
2963
|
+
return nullptr;
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
if (i == kMaxLinearProbe) {
|
|
2967
|
+
// Keep searching, but change to a search method that should quickly
|
|
2968
|
+
// find any under-populated region. Switching to an increment based
|
|
2969
|
+
// on the golden ratio helps with that, but we also inject some minor
|
|
2970
|
+
// variation (less than 2%, 1 in 2^6) to avoid clustering effects on
|
|
2971
|
+
// this larger increment (if it were a fixed value in steady state
|
|
2972
|
+
// operation). Here we are primarily using upper bits of hashed_key[1]
|
|
2973
|
+
// while home is based on lowest bits.
|
|
2974
|
+
uint64_t incr_ratio = 0x9E3779B185EBCA87U + (proto.hashed_key[1] >> 6);
|
|
2975
|
+
size_t incr = FastRange64(incr_ratio, used_length);
|
|
2976
|
+
assert(incr > 0);
|
|
2977
|
+
size_t start = idx;
|
|
2978
|
+
for (;; i++) {
|
|
2979
|
+
idx += incr;
|
|
2980
|
+
if (idx >= used_length) {
|
|
2981
|
+
// Wrap around (faster than %)
|
|
2982
|
+
idx -= used_length;
|
|
2983
|
+
}
|
|
2984
|
+
if (idx == start) {
|
|
2985
|
+
// We have just completed a cycle that might not have covered all
|
|
2986
|
+
// slots. (incr and used_length could have common factors.)
|
|
2987
|
+
// Increment for the next cycle, which eventually ensures complete
|
|
2988
|
+
// iteration over the set of slots before repeating.
|
|
2989
|
+
idx++;
|
|
2990
|
+
if (idx >= used_length) {
|
|
2991
|
+
idx -= used_length;
|
|
2992
|
+
}
|
|
2993
|
+
start++;
|
|
2994
|
+
if (start >= used_length) {
|
|
2995
|
+
start -= used_length;
|
|
2996
|
+
}
|
|
2997
|
+
if (i >= used_length) {
|
|
2998
|
+
used_length = LengthInfoToUsedLength(
|
|
2999
|
+
length_info_.load(std::memory_order_acquire));
|
|
3000
|
+
if (i >= used_length * 2) {
|
|
3001
|
+
// Cycling back should not happen unless there is enough random
|
|
3002
|
+
// churn in parallel that we happen to hit each slot at a time
|
|
3003
|
+
// that it's occupied, which is really only feasible for small
|
|
3004
|
+
// structures, though with linear probing to find empty slots,
|
|
3005
|
+
// "small" here might be larger than for double hashing.
|
|
3006
|
+
assert(used_length <= 256);
|
|
3007
|
+
// Fall back on standalone insert in case something goes awry to
|
|
3008
|
+
// cause this
|
|
3009
|
+
return nullptr;
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
if (TryInsert(proto, arr[idx], initial_countdown, take_ref,
|
|
3014
|
+
&already_matches)) {
|
|
3015
|
+
break;
|
|
3016
|
+
}
|
|
3017
|
+
if (already_matches) {
|
|
3018
|
+
return nullptr;
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
|
|
3024
|
+
// Now insert into chain using head pointer
|
|
3025
|
+
uint64_t next_with_shift;
|
|
3026
|
+
int home_shift = orig_home_shift;
|
|
3027
|
+
|
|
3028
|
+
// Might need to retry
|
|
3029
|
+
for (int i = 0;; ++i) {
|
|
3030
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
3031
|
+
next_with_shift =
|
|
3032
|
+
arr[home].head_next_with_shift.load(std::memory_order_acquire);
|
|
3033
|
+
int shift = GetShiftFromNextWithShift(next_with_shift);
|
|
3034
|
+
|
|
3035
|
+
if (UNLIKELY(shift != home_shift)) {
|
|
3036
|
+
// NOTE: shift increases with table growth
|
|
3037
|
+
if (shift > home_shift) {
|
|
3038
|
+
// Must be grow in progress or completed since reading length_info.
|
|
3039
|
+
// Pull out one more hash bit. (See Lookup() for why we can't
|
|
3040
|
+
// safely jump to the shift that was read.)
|
|
3041
|
+
home_shift++;
|
|
3042
|
+
uint64_t hash_bit_mask = uint64_t{1} << (home_shift - 1);
|
|
3043
|
+
assert((home & hash_bit_mask) == 0);
|
|
3044
|
+
// BEGIN leftover updates to length_info_ for Grow()
|
|
3045
|
+
size_t grow_home = home + hash_bit_mask;
|
|
3046
|
+
assert(arr[grow_home].head_next_with_shift.load(
|
|
3047
|
+
std::memory_order_acquire) != HandleImpl::kUnusedMarker);
|
|
3048
|
+
CatchUpLengthInfoNoWait(grow_home);
|
|
3049
|
+
// END leftover updates to length_info_ for Grow()
|
|
3050
|
+
home += proto.hashed_key[1] & hash_bit_mask;
|
|
3051
|
+
continue;
|
|
3052
|
+
} else {
|
|
3053
|
+
// Should not happen because length_info_ is only updated after both
|
|
3054
|
+
// old and new home heads are marked with new shift
|
|
3055
|
+
assert(false);
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
|
|
3059
|
+
// Values to update to
|
|
3060
|
+
uint64_t head_next_with_shift = MakeNextWithShift(idx, home_shift);
|
|
3061
|
+
uint64_t chain_next_with_shift = next_with_shift;
|
|
3062
|
+
|
|
3063
|
+
// Preserve the locked state in head, without propagating to chain next
|
|
3064
|
+
// where it is meaningless (and not allowed)
|
|
3065
|
+
if (UNLIKELY((next_with_shift & HandleImpl::kNextEndFlags) ==
|
|
3066
|
+
HandleImpl::kHeadLocked)) {
|
|
3067
|
+
head_next_with_shift |= HandleImpl::kHeadLocked;
|
|
3068
|
+
chain_next_with_shift &= ~HandleImpl::kHeadLocked;
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
arr[idx].chain_next_with_shift.store(chain_next_with_shift,
|
|
3072
|
+
std::memory_order_release);
|
|
3073
|
+
if (arr[home].head_next_with_shift.compare_exchange_weak(
|
|
3074
|
+
next_with_shift, head_next_with_shift, std::memory_order_acq_rel)) {
|
|
3075
|
+
// Success
|
|
3076
|
+
return arr + idx;
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
|
|
3081
|
+
AutoHyperClockTable::HandleImpl* AutoHyperClockTable::Lookup(
|
|
3082
|
+
const UniqueId64x2& hashed_key) {
|
|
3083
|
+
// Lookups are wait-free with low occurrence of retries, back-tracking,
|
|
3084
|
+
// and fallback. We do not have the benefit of holding a rewrite lock on
|
|
3085
|
+
// the chain so must be prepared for many kinds of mayhem, most notably
|
|
3086
|
+
// "falling off our chain" where a slot that Lookup has identified but
|
|
3087
|
+
// has not read-referenced is removed from one chain and inserted into
|
|
3088
|
+
// another. The full algorithm uses the following mitigation strategies to
|
|
3089
|
+
// ensure every relevant entry inserted before this Lookup, and not yet
|
|
3090
|
+
// evicted, is seen by Lookup, without excessive backtracking etc.:
|
|
3091
|
+
// * Keep a known good read ref in the chain for "island hopping." When
|
|
3092
|
+
// we observe that a concurrent write takes us off to another chain, we
|
|
3093
|
+
// only need to fall back to our last known good read ref (most recent
|
|
3094
|
+
// entry on the chain that is not "under construction," which is a transient
|
|
3095
|
+
// state). We don't want to compound the CPU toil of a long chain with
|
|
3096
|
+
// operations that might need to retry from scratch, with probability
|
|
3097
|
+
// in proportion to chain length.
|
|
3098
|
+
// * Only detect a chain is potentially incomplete because of a Grow in
|
|
3099
|
+
// progress by looking at shift in the next pointer tags (rather than
|
|
3100
|
+
// re-checking length_info_).
|
|
3101
|
+
// * SplitForGrow, Insert, and PurgeImplLocked ensure that there are no
|
|
3102
|
+
// transient states that might cause this full Lookup algorithm to skip over
|
|
3103
|
+
// live entries.
|
|
3104
|
+
|
|
3105
|
+
// Reading length_info_ is not strictly required for Lookup, if we were
|
|
3106
|
+
// to increment shift sizes until we see a shift size match on the
|
|
3107
|
+
// relevant head pointer. Thus, reading with relaxed memory order gives
|
|
3108
|
+
// us a safe and almost always up-to-date jump into finding the correct
|
|
3109
|
+
// home and head.
|
|
3110
|
+
size_t home;
|
|
3111
|
+
int home_shift;
|
|
3112
|
+
GetHomeIndexAndShift(length_info_.load(std::memory_order_relaxed),
|
|
3113
|
+
hashed_key[1], &home, &home_shift);
|
|
3114
|
+
assert(home_shift > 0);
|
|
3115
|
+
|
|
3116
|
+
// The full Lookup algorithm however is not great for hot path efficiency,
|
|
3117
|
+
// because of the extra careful tracking described above. Overwhelmingly,
|
|
3118
|
+
// we can find what we're looking for with a naive linked list traversal
|
|
3119
|
+
// of the chain. Even if we "fall off our chain" to another, we don't
|
|
3120
|
+
// violate memory safety. We just won't match the key we're looking for.
|
|
3121
|
+
// And we would eventually reach an end state, possibly even experiencing a
|
|
3122
|
+
// cycle as an entry is freed and reused during our traversal (though at
|
|
3123
|
+
// any point in time the structure doesn't have cycles).
|
|
3124
|
+
//
|
|
3125
|
+
// So for hot path efficiency, we start with a naive Lookup attempt, and
|
|
3126
|
+
// then fall back on full Lookup if we don't find the correct entry. To
|
|
3127
|
+
// cap how much we invest into the naive Lookup, we simply cap the traversal
|
|
3128
|
+
// length before falling back. Also, when we do fall back on full Lookup,
|
|
3129
|
+
// we aren't paying much penalty by starting over. Much or most of the cost
|
|
3130
|
+
// of Lookup is memory latency in following the chain pointers, and the
|
|
3131
|
+
// naive Lookup has warmed the CPU cache for these entries, using as tight
|
|
3132
|
+
// of a loop as possible.
|
|
3133
|
+
|
|
3134
|
+
HandleImpl* const arr = array_.Get();
|
|
3135
|
+
uint64_t next_with_shift = arr[home].head_next_with_shift;
|
|
3136
|
+
for (size_t i = 0; !HandleImpl::IsEnd(next_with_shift) && i < 10; ++i) {
|
|
3137
|
+
HandleImpl* h = &arr[GetNextFromNextWithShift(next_with_shift)];
|
|
3138
|
+
// Attempt cheap key match without acquiring a read ref. This could give a
|
|
3139
|
+
// false positive, which is re-checked after acquiring read ref, or false
|
|
3140
|
+
// negative, which is re-checked in the full Lookup. Also, this is a
|
|
3141
|
+
// technical UB data race according to TSAN, but we don't need to read
|
|
3142
|
+
// a "correct" value here for correct overall behavior.
|
|
3143
|
+
#ifdef __SANITIZE_THREAD__
|
|
3144
|
+
bool probably_equal = Random::GetTLSInstance()->OneIn(2);
|
|
3145
|
+
#else
|
|
3146
|
+
bool probably_equal = h->hashed_key == hashed_key;
|
|
3147
|
+
#endif
|
|
3148
|
+
if (probably_equal) {
|
|
3149
|
+
// Increment acquire counter for definitive check
|
|
3150
|
+
uint64_t old_meta = h->meta.fetch_add(ClockHandle::kAcquireIncrement,
|
|
3151
|
+
std::memory_order_acquire);
|
|
3152
|
+
// Check if it's a referencable (sharable) entry
|
|
3153
|
+
if (LIKELY(old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
|
3154
|
+
<< ClockHandle::kStateShift))) {
|
|
3155
|
+
assert(GetRefcount(old_meta + ClockHandle::kAcquireIncrement) > 0);
|
|
3156
|
+
if (LIKELY(h->hashed_key == hashed_key) &&
|
|
3157
|
+
LIKELY(old_meta & (uint64_t{ClockHandle::kStateVisibleBit}
|
|
3158
|
+
<< ClockHandle::kStateShift))) {
|
|
3159
|
+
return h;
|
|
3160
|
+
} else {
|
|
3161
|
+
Unref(*h);
|
|
3162
|
+
}
|
|
3163
|
+
} else {
|
|
3164
|
+
// For non-sharable states, incrementing the acquire counter has no
|
|
3165
|
+
// effect so we don't need to undo it. Furthermore, we cannot safely
|
|
3166
|
+
// undo it because we did not acquire a read reference to lock the entry
|
|
3167
|
+
// in a Shareable state.
|
|
3168
|
+
}
|
|
3169
|
+
}
|
|
3170
|
+
|
|
3171
|
+
next_with_shift = h->chain_next_with_shift.load(std::memory_order_relaxed);
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
// If we get here, falling back on full Lookup algorithm.
|
|
3175
|
+
HandleImpl* h = nullptr;
|
|
3176
|
+
HandleImpl* read_ref_on_chain = nullptr;
|
|
3177
|
+
|
|
3178
|
+
for (size_t i = 0;; ++i) {
|
|
3179
|
+
CHECK_TOO_MANY_ITERATIONS(i);
|
|
3180
|
+
// Read head or chain pointer
|
|
3181
|
+
next_with_shift =
|
|
3182
|
+
h ? h->chain_next_with_shift : arr[home].head_next_with_shift;
|
|
3183
|
+
int shift = GetShiftFromNextWithShift(next_with_shift);
|
|
3184
|
+
|
|
3185
|
+
// Make sure it's usable
|
|
3186
|
+
size_t effective_home = home;
|
|
3187
|
+
if (UNLIKELY(shift != home_shift)) {
|
|
3188
|
+
// We have potentially gone awry somehow, but it's possible we're just
|
|
3189
|
+
// hitting old data that is not yet completed Grow.
|
|
3190
|
+
// NOTE: shift bits goes up with table growth.
|
|
3191
|
+
if (shift < home_shift) {
|
|
3192
|
+
// To avoid waiting on Grow in progress, an old shift amount needs
|
|
3193
|
+
// to be processed as if we were still using it and (potentially
|
|
3194
|
+
// different or the same) the old home.
|
|
3195
|
+
// We can assert it's not too old, because each generation of Grow
|
|
3196
|
+
// waits on its ancestor in the previous generation.
|
|
3197
|
+
assert(shift + 1 == home_shift);
|
|
3198
|
+
effective_home = GetHomeIndex(home, shift);
|
|
3199
|
+
} else if (h == read_ref_on_chain) {
|
|
3200
|
+
assert(shift > home_shift);
|
|
3201
|
+
// At head or coming from an entry on our chain where we're holding
|
|
3202
|
+
// a read reference. Thus, we know the newer shift applies to us.
|
|
3203
|
+
// Newer shift might not yet be reflected in length_info_ (an atomicity
|
|
3204
|
+
// gap in Grow), so operate as if it is. Note that other insertions
|
|
3205
|
+
// could happen using this shift before length_info_ is updated, and
|
|
3206
|
+
// it's possible (though unlikely) that multiple generations of Grow
|
|
3207
|
+
// have occurred. If shift is more than one generation ahead of
|
|
3208
|
+
// home_shift, it's possible that not all descendent homes have
|
|
3209
|
+
// reached the `shift` generation. Thus, we need to advance only one
|
|
3210
|
+
// shift at a time looking for a home+head with a matching shift
|
|
3211
|
+
// amount.
|
|
3212
|
+
home_shift++;
|
|
3213
|
+
// Update home in case it has changed
|
|
3214
|
+
home = GetHomeIndex(hashed_key[1], home_shift);
|
|
3215
|
+
// This should be rare enough occurrence that it's simplest just
|
|
3216
|
+
// to restart (TODO: improve in some cases?)
|
|
3217
|
+
h = nullptr;
|
|
3218
|
+
if (read_ref_on_chain) {
|
|
3219
|
+
Unref(*read_ref_on_chain);
|
|
3220
|
+
read_ref_on_chain = nullptr;
|
|
3221
|
+
}
|
|
3222
|
+
// Didn't make progress & retry
|
|
3223
|
+
continue;
|
|
3224
|
+
} else {
|
|
3225
|
+
assert(shift > home_shift);
|
|
3226
|
+
assert(h != nullptr);
|
|
3227
|
+
// An "under (de)construction" entry has a new shift amount, which
|
|
3228
|
+
// means we have either gotten off our chain or our home shift is out
|
|
3229
|
+
// of date. If we revert back to saved ref, we will get updated info.
|
|
3230
|
+
h = read_ref_on_chain;
|
|
3231
|
+
// Didn't make progress & retry
|
|
3232
|
+
continue;
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
|
|
3236
|
+
// Check for end marker
|
|
3237
|
+
if (HandleImpl::IsEnd(next_with_shift)) {
|
|
3238
|
+
// To ensure we didn't miss anything in the chain, the end marker must
|
|
3239
|
+
// point back to the correct home.
|
|
3240
|
+
if (LIKELY(GetNextFromNextWithShift(next_with_shift) == effective_home)) {
|
|
3241
|
+
// Complete, clean iteration of the chain, not found.
|
|
3242
|
+
// Clean up.
|
|
3243
|
+
if (read_ref_on_chain) {
|
|
3244
|
+
Unref(*read_ref_on_chain);
|
|
3245
|
+
}
|
|
3246
|
+
return nullptr;
|
|
3247
|
+
} else {
|
|
3248
|
+
// Something went awry. Revert back to a safe point (if we have it)
|
|
3249
|
+
h = read_ref_on_chain;
|
|
3250
|
+
// Didn't make progress & retry
|
|
3251
|
+
continue;
|
|
3252
|
+
}
|
|
3253
|
+
}
|
|
3254
|
+
|
|
3255
|
+
// Follow the next and check for full key match, home match, or neither
|
|
3256
|
+
h = &arr[GetNextFromNextWithShift(next_with_shift)];
|
|
3257
|
+
bool full_match_or_unknown = false;
|
|
3258
|
+
if (MatchAndRef(&hashed_key, *h, shift, effective_home,
|
|
3259
|
+
&full_match_or_unknown)) {
|
|
3260
|
+
// Got a read ref on next (h).
|
|
3261
|
+
//
|
|
3262
|
+
// There is a very small chance that between getting the next pointer
|
|
3263
|
+
// (now h) and doing MatchAndRef on it, another thread erased/evicted it
|
|
3264
|
+
// reinserted it into the same chain, causing us to cycle back in the
|
|
3265
|
+
// same chain and potentially see some entries again if we keep walking.
|
|
3266
|
+
// Newly-inserted entries are inserted before older ones, so we are at
|
|
3267
|
+
// least guaranteed not to miss anything. Here in Lookup, it's just a
|
|
3268
|
+
// transient, slight hiccup in performance.
|
|
3269
|
+
|
|
3270
|
+
if (full_match_or_unknown) {
|
|
3271
|
+
// Full match.
|
|
3272
|
+
// Release old read ref on chain if applicable
|
|
3273
|
+
if (read_ref_on_chain) {
|
|
3274
|
+
// Pretend we never took the reference.
|
|
3275
|
+
Unref(*read_ref_on_chain);
|
|
3276
|
+
}
|
|
3277
|
+
// Update the hit bit
|
|
3278
|
+
if (eviction_callback_) {
|
|
3279
|
+
h->meta.fetch_or(uint64_t{1} << ClockHandle::kHitBitShift,
|
|
3280
|
+
std::memory_order_relaxed);
|
|
3281
|
+
}
|
|
3282
|
+
// All done.
|
|
3283
|
+
return h;
|
|
3284
|
+
} else if (UNLIKELY(shift != home_shift) &&
|
|
3285
|
+
home != BottomNBits(h->hashed_key[1], home_shift)) {
|
|
3286
|
+
// This chain is in a Grow operation and we've landed on an entry
|
|
3287
|
+
// that belongs to the wrong destination chain. We can keep going, but
|
|
3288
|
+
// there's a chance we'll need to backtrack back *before* this entry,
|
|
3289
|
+
// if the Grow finishes before this Lookup. We cannot save this entry
|
|
3290
|
+
// for backtracking because it might soon or already be on the wrong
|
|
3291
|
+
// chain.
|
|
3292
|
+
// NOTE: if we simply backtrack rather than continuing, we would
|
|
3293
|
+
// be in a wait loop (not allowed in Lookup!) until the other thread
|
|
3294
|
+
// finishes its Grow.
|
|
3295
|
+
Unref(*h);
|
|
3296
|
+
} else {
|
|
3297
|
+
// Correct home location, so we are on the right chain.
|
|
3298
|
+
// With new usable read ref, can release old one (if applicable).
|
|
3299
|
+
if (read_ref_on_chain) {
|
|
3300
|
+
// Pretend we never took the reference.
|
|
3301
|
+
Unref(*read_ref_on_chain);
|
|
3302
|
+
}
|
|
3303
|
+
// And keep the new one.
|
|
3304
|
+
read_ref_on_chain = h;
|
|
3305
|
+
}
|
|
3306
|
+
} else {
|
|
3307
|
+
if (full_match_or_unknown) {
|
|
3308
|
+
// Must have been an "under construction" entry. Can safely skip it,
|
|
3309
|
+
// but there's a chance we'll have to backtrack later
|
|
3310
|
+
} else {
|
|
3311
|
+
// Home mismatch! Revert back to a safe point (if we have it)
|
|
3312
|
+
h = read_ref_on_chain;
|
|
3313
|
+
// Didn't make progress & retry
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
|
|
3319
|
+
void AutoHyperClockTable::Remove(HandleImpl* h) {
|
|
3320
|
+
assert((h->meta.load() >> ClockHandle::kStateShift) ==
|
|
3321
|
+
ClockHandle::kStateConstruction);
|
|
3322
|
+
|
|
3323
|
+
const HandleImpl& c_h = *h;
|
|
3324
|
+
PurgeImpl(&c_h.hashed_key);
|
|
3325
|
+
}
|
|
3326
|
+
|
|
3327
|
+
bool AutoHyperClockTable::TryEraseHandle(HandleImpl* h, bool holding_ref,
|
|
3328
|
+
bool mark_invisible) {
|
|
3329
|
+
uint64_t meta;
|
|
3330
|
+
if (mark_invisible) {
|
|
3331
|
+
// Set invisible
|
|
3332
|
+
meta = h->meta.fetch_and(
|
|
3333
|
+
~(uint64_t{ClockHandle::kStateVisibleBit} << ClockHandle::kStateShift),
|
|
3334
|
+
std::memory_order_acq_rel);
|
|
3335
|
+
// To local variable also
|
|
3336
|
+
meta &=
|
|
3337
|
+
~(uint64_t{ClockHandle::kStateVisibleBit} << ClockHandle::kStateShift);
|
|
3338
|
+
} else {
|
|
3339
|
+
meta = h->meta.load(std::memory_order_acquire);
|
|
3340
|
+
}
|
|
3341
|
+
|
|
3342
|
+
// Take ownership if no other refs
|
|
3343
|
+
do {
|
|
3344
|
+
if (GetRefcount(meta) != uint64_t{holding_ref}) {
|
|
3345
|
+
// Not last ref at some point in time during this call
|
|
3346
|
+
return false;
|
|
3347
|
+
}
|
|
3348
|
+
if ((meta & (uint64_t{ClockHandle::kStateShareableBit}
|
|
3349
|
+
<< ClockHandle::kStateShift)) == 0) {
|
|
3350
|
+
// Someone else took ownership
|
|
3351
|
+
return false;
|
|
3352
|
+
}
|
|
3353
|
+
// Note that if !holding_ref, there's a small chance that we release,
|
|
3354
|
+
// another thread replaces this entry with another, reaches zero refs, and
|
|
3355
|
+
// then we end up erasing that other entry. That's an acceptable risk /
|
|
3356
|
+
// imprecision.
|
|
3357
|
+
} while (!h->meta.compare_exchange_weak(
|
|
3358
|
+
meta,
|
|
3359
|
+
uint64_t{ClockHandle::kStateConstruction} << ClockHandle::kStateShift,
|
|
3360
|
+
std::memory_order_acquire));
|
|
3361
|
+
// Took ownership
|
|
3362
|
+
// TODO? Delay freeing?
|
|
3363
|
+
h->FreeData(allocator_);
|
|
3364
|
+
size_t total_charge = h->total_charge;
|
|
3365
|
+
if (UNLIKELY(h->IsStandalone())) {
|
|
3366
|
+
// Delete detached handle
|
|
3367
|
+
delete h;
|
|
3368
|
+
standalone_usage_.fetch_sub(total_charge, std::memory_order_relaxed);
|
|
3369
|
+
} else {
|
|
3370
|
+
Remove(h);
|
|
3371
|
+
MarkEmpty(*h);
|
|
3372
|
+
occupancy_.fetch_sub(1U, std::memory_order_release);
|
|
3373
|
+
}
|
|
3374
|
+
usage_.fetch_sub(total_charge, std::memory_order_relaxed);
|
|
3375
|
+
assert(usage_.load(std::memory_order_relaxed) < SIZE_MAX / 2);
|
|
3376
|
+
return true;
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
bool AutoHyperClockTable::Release(HandleImpl* h, bool useful,
|
|
3380
|
+
bool erase_if_last_ref) {
|
|
3381
|
+
// In contrast with LRUCache's Release, this function won't delete the handle
|
|
3382
|
+
// when the cache is above capacity and the reference is the last one. Space
|
|
3383
|
+
// is only freed up by Evict/PurgeImpl (called by Insert when space
|
|
3384
|
+
// is needed) and Erase. We do this to avoid an extra atomic read of the
|
|
3385
|
+
// variable usage_.
|
|
3386
|
+
|
|
3387
|
+
uint64_t old_meta;
|
|
3388
|
+
if (useful) {
|
|
3389
|
+
// Increment release counter to indicate was used
|
|
3390
|
+
old_meta = h->meta.fetch_add(ClockHandle::kReleaseIncrement,
|
|
3391
|
+
std::memory_order_release);
|
|
3392
|
+
// Correct for possible (but rare) overflow
|
|
3393
|
+
CorrectNearOverflow(old_meta, h->meta);
|
|
3394
|
+
} else {
|
|
3395
|
+
// Decrement acquire counter to pretend it never happened
|
|
3396
|
+
old_meta = h->meta.fetch_sub(ClockHandle::kAcquireIncrement,
|
|
3397
|
+
std::memory_order_release);
|
|
3398
|
+
}
|
|
3399
|
+
|
|
3400
|
+
assert((old_meta >> ClockHandle::kStateShift) &
|
|
3401
|
+
ClockHandle::kStateShareableBit);
|
|
3402
|
+
// No underflow
|
|
3403
|
+
assert(((old_meta >> ClockHandle::kAcquireCounterShift) &
|
|
3404
|
+
ClockHandle::kCounterMask) !=
|
|
3405
|
+
((old_meta >> ClockHandle::kReleaseCounterShift) &
|
|
3406
|
+
ClockHandle::kCounterMask));
|
|
3407
|
+
|
|
3408
|
+
if ((erase_if_last_ref || UNLIKELY(old_meta >> ClockHandle::kStateShift ==
|
|
3409
|
+
ClockHandle::kStateInvisible))) {
|
|
3410
|
+
// FIXME: There's a chance here that another thread could replace this
|
|
3411
|
+
// entry and we end up erasing the wrong one.
|
|
3412
|
+
return TryEraseHandle(h, /*holding_ref=*/false, /*mark_invisible=*/false);
|
|
3413
|
+
} else {
|
|
3414
|
+
return false;
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
|
|
3418
|
+
#ifndef NDEBUG
|
|
3419
|
+
void AutoHyperClockTable::TEST_ReleaseN(HandleImpl* h, size_t n) {
|
|
3420
|
+
if (n > 0) {
|
|
3421
|
+
// Do n-1 simple releases first
|
|
3422
|
+
TEST_ReleaseNMinus1(h, n);
|
|
3423
|
+
|
|
3424
|
+
// Then the last release might be more involved
|
|
3425
|
+
Release(h, /*useful*/ true, /*erase_if_last_ref*/ false);
|
|
3426
|
+
}
|
|
3427
|
+
}
|
|
3428
|
+
#endif
|
|
3429
|
+
|
|
3430
|
+
void AutoHyperClockTable::Erase(const UniqueId64x2& hashed_key) {
|
|
3431
|
+
// Don't need to be efficient.
|
|
3432
|
+
// Might be one match masking another, so loop.
|
|
3433
|
+
while (HandleImpl* h = Lookup(hashed_key)) {
|
|
3434
|
+
bool gone =
|
|
3435
|
+
TryEraseHandle(h, /*holding_ref=*/true, /*mark_invisible=*/true);
|
|
3436
|
+
if (!gone) {
|
|
3437
|
+
// Only marked invisible, which is ok.
|
|
3438
|
+
// Pretend we never took the reference from Lookup.
|
|
3439
|
+
Unref(*h);
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
}
|
|
3443
|
+
|
|
3444
|
+
void AutoHyperClockTable::EraseUnRefEntries() {
|
|
3445
|
+
size_t usable_size = GetTableSize();
|
|
3446
|
+
for (size_t i = 0; i < usable_size; i++) {
|
|
3447
|
+
HandleImpl& h = array_[i];
|
|
3448
|
+
|
|
3449
|
+
uint64_t old_meta = h.meta.load(std::memory_order_relaxed);
|
|
3450
|
+
if (old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
|
3451
|
+
<< ClockHandle::kStateShift) &&
|
|
3452
|
+
GetRefcount(old_meta) == 0 &&
|
|
3453
|
+
h.meta.compare_exchange_strong(old_meta,
|
|
3454
|
+
uint64_t{ClockHandle::kStateConstruction}
|
|
3455
|
+
<< ClockHandle::kStateShift,
|
|
3456
|
+
std::memory_order_acquire)) {
|
|
3457
|
+
// Took ownership
|
|
3458
|
+
h.FreeData(allocator_);
|
|
3459
|
+
usage_.fetch_sub(h.total_charge, std::memory_order_relaxed);
|
|
3460
|
+
// NOTE: could be more efficient with a dedicated variant of
|
|
3461
|
+
// PurgeImpl, but this is not a common operation
|
|
3462
|
+
Remove(&h);
|
|
3463
|
+
MarkEmpty(h);
|
|
3464
|
+
occupancy_.fetch_sub(1U, std::memory_order_release);
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
void AutoHyperClockTable::Evict(size_t requested_charge, InsertState& state,
|
|
3470
|
+
EvictionData* data) {
|
|
3471
|
+
// precondition
|
|
3472
|
+
assert(requested_charge > 0);
|
|
3473
|
+
|
|
3474
|
+
// We need the clock pointer to seemlessly "wrap around" at the end of the
|
|
3475
|
+
// table, and to be reasonably stable under Grow operations. This is
|
|
3476
|
+
// challenging when the linear hashing progressively opens additional
|
|
3477
|
+
// most-significant-hash-bits in determining home locations.
|
|
3478
|
+
|
|
3479
|
+
// TODO: make a tuning parameter?
|
|
3480
|
+
// Up to 2x this number of homes will be evicted per step. In very rare
|
|
3481
|
+
// cases, possibly more, as homes of an out-of-date generation will be
|
|
3482
|
+
// resolved to multiple in a newer generation.
|
|
3483
|
+
constexpr size_t step_size = 4;
|
|
3484
|
+
|
|
3485
|
+
// A clock_pointer_mask_ field separate from length_info_ enables us to use
|
|
3486
|
+
// the same mask (way of dividing up the space among evicting threads) for
|
|
3487
|
+
// iterating over the whole structure before considering changing the mask
|
|
3488
|
+
// at the beginning of each pass. This ensures we do not have a large portion
|
|
3489
|
+
// of the space that receives redundant or missed clock updates. However,
|
|
3490
|
+
// with two variables, for each update to clock_pointer_mask (< 64 ever in
|
|
3491
|
+
// the life of the cache), there will be a brief period where concurrent
|
|
3492
|
+
// eviction threads could use the old mask value, possibly causing redundant
|
|
3493
|
+
// or missed clock updates for a *small* portion of the table.
|
|
3494
|
+
size_t clock_pointer_mask =
|
|
3495
|
+
clock_pointer_mask_.load(std::memory_order_relaxed);
|
|
3496
|
+
|
|
3497
|
+
uint64_t max_clock_pointer = 0; // unset
|
|
3498
|
+
|
|
3499
|
+
// TODO: consider updating during a long eviction
|
|
3500
|
+
size_t used_length = LengthInfoToUsedLength(state.saved_length_info);
|
|
3501
|
+
|
|
3502
|
+
autovector<HandleImpl*> to_finish_eviction;
|
|
3503
|
+
|
|
3504
|
+
// Loop until enough freed, or limit reached (see bottom of loop)
|
|
3505
|
+
for (;;) {
|
|
3506
|
+
// First (concurrent) increment clock pointer
|
|
3507
|
+
uint64_t old_clock_pointer =
|
|
3508
|
+
clock_pointer_.fetch_add(step_size, std::memory_order_relaxed);
|
|
3509
|
+
|
|
3510
|
+
if (UNLIKELY((old_clock_pointer & clock_pointer_mask) == 0)) {
|
|
3511
|
+
// Back at the beginning. See if clock_pointer_mask should be updated.
|
|
3512
|
+
uint64_t mask = BottomNBits(
|
|
3513
|
+
UINT64_MAX, LengthInfoToMinShift(state.saved_length_info));
|
|
3514
|
+
if (clock_pointer_mask != mask) {
|
|
3515
|
+
clock_pointer_mask = static_cast<size_t>(mask);
|
|
3516
|
+
clock_pointer_mask_.store(clock_pointer_mask,
|
|
3517
|
+
std::memory_order_relaxed);
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3520
|
+
|
|
3521
|
+
size_t major_step = clock_pointer_mask + 1;
|
|
3522
|
+
assert((major_step & clock_pointer_mask) == 0);
|
|
3523
|
+
|
|
3524
|
+
for (size_t base_home = old_clock_pointer & clock_pointer_mask;
|
|
3525
|
+
base_home < used_length; base_home += major_step) {
|
|
3526
|
+
for (size_t i = 0; i < step_size; i++) {
|
|
3527
|
+
size_t home = base_home + i;
|
|
3528
|
+
if (home >= used_length) {
|
|
3529
|
+
break;
|
|
3530
|
+
}
|
|
3531
|
+
PurgeImpl(&to_finish_eviction, home);
|
|
3532
|
+
}
|
|
3533
|
+
}
|
|
3534
|
+
|
|
3535
|
+
for (HandleImpl* h : to_finish_eviction) {
|
|
3536
|
+
TrackAndReleaseEvictedEntry(h, data);
|
|
3537
|
+
// NOTE: setting likely_empty_slot here can cause us to reduce the
|
|
3538
|
+
// portion of "at home" entries, probably because an evicted entry
|
|
3539
|
+
// is more likely to come back than a random new entry and would be
|
|
3540
|
+
// unable to go into its home slot.
|
|
3541
|
+
}
|
|
3542
|
+
to_finish_eviction.clear();
|
|
3543
|
+
|
|
3544
|
+
// Loop exit conditions
|
|
3545
|
+
if (data->freed_charge >= requested_charge) {
|
|
3546
|
+
return;
|
|
3547
|
+
}
|
|
3548
|
+
|
|
3549
|
+
if (max_clock_pointer == 0) {
|
|
3550
|
+
// Cap the eviction effort at this thread (along with those operating in
|
|
3551
|
+
// parallel) circling through the whole structure kMaxCountdown times.
|
|
3552
|
+
// In other words, this eviction run must find something/anything that is
|
|
3553
|
+
// unreferenced at start of and during the eviction run that isn't
|
|
3554
|
+
// reclaimed by a concurrent eviction run.
|
|
3555
|
+
// TODO: Does HyperClockCache need kMaxCountdown + 1?
|
|
3556
|
+
max_clock_pointer =
|
|
3557
|
+
old_clock_pointer +
|
|
3558
|
+
(uint64_t{ClockHandle::kMaxCountdown + 1} * major_step);
|
|
3559
|
+
}
|
|
3560
|
+
|
|
3561
|
+
if (old_clock_pointer + step_size >= max_clock_pointer) {
|
|
3562
|
+
return;
|
|
3563
|
+
}
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
size_t AutoHyperClockTable::CalcMaxUsableLength(
|
|
3568
|
+
size_t capacity, size_t min_avg_value_size,
|
|
3569
|
+
CacheMetadataChargePolicy metadata_charge_policy) {
|
|
3570
|
+
double min_avg_slot_charge = min_avg_value_size * kMaxLoadFactor;
|
|
3571
|
+
if (metadata_charge_policy == kFullChargeCacheMetadata) {
|
|
3572
|
+
min_avg_slot_charge += sizeof(HandleImpl);
|
|
3573
|
+
}
|
|
3574
|
+
assert(min_avg_slot_charge > 0.0);
|
|
3575
|
+
size_t num_slots =
|
|
3576
|
+
static_cast<size_t>(capacity / min_avg_slot_charge + 0.999999);
|
|
3577
|
+
|
|
3578
|
+
const size_t slots_per_page = port::kPageSize / sizeof(HandleImpl);
|
|
3579
|
+
|
|
3580
|
+
// Round up to page size
|
|
3581
|
+
return ((num_slots + slots_per_page - 1) / slots_per_page) * slots_per_page;
|
|
3582
|
+
}
|
|
3583
|
+
|
|
3584
|
+
namespace {
|
|
3585
|
+
bool IsHeadNonempty(const AutoHyperClockTable::HandleImpl& h) {
|
|
3586
|
+
return !AutoHyperClockTable::HandleImpl::IsEnd(
|
|
3587
|
+
h.head_next_with_shift.load(std::memory_order_relaxed));
|
|
3588
|
+
}
|
|
3589
|
+
bool IsEntryAtHome(const AutoHyperClockTable::HandleImpl& h, int shift,
|
|
3590
|
+
size_t home) {
|
|
3591
|
+
if (MatchAndRef(nullptr, h, shift, home)) {
|
|
3592
|
+
Unref(h);
|
|
3593
|
+
return true;
|
|
3594
|
+
} else {
|
|
3595
|
+
return false;
|
|
3596
|
+
}
|
|
3597
|
+
}
|
|
3598
|
+
} // namespace
|
|
3599
|
+
|
|
3600
|
+
void AutoHyperClockCache::ReportProblems(
|
|
3601
|
+
const std::shared_ptr<Logger>& info_log) const {
|
|
3602
|
+
BaseHyperClockCache::ReportProblems(info_log);
|
|
3603
|
+
|
|
3604
|
+
if (info_log->GetInfoLogLevel() <= InfoLogLevel::DEBUG_LEVEL) {
|
|
3605
|
+
LoadVarianceStats head_stats;
|
|
3606
|
+
size_t entry_at_home_count = 0;
|
|
3607
|
+
uint64_t yield_count = 0;
|
|
3608
|
+
this->ForEachShard([&](const Shard* shard) {
|
|
3609
|
+
size_t count = shard->GetTableAddressCount();
|
|
3610
|
+
uint64_t length_info = UsedLengthToLengthInfo(count);
|
|
3611
|
+
for (size_t i = 0; i < count; ++i) {
|
|
3612
|
+
const auto& h = *shard->GetTable().HandlePtr(i);
|
|
3613
|
+
head_stats.Add(IsHeadNonempty(h));
|
|
3614
|
+
int shift;
|
|
3615
|
+
size_t home;
|
|
3616
|
+
GetHomeIndexAndShift(length_info, i, &home, &shift);
|
|
3617
|
+
assert(home == i);
|
|
3618
|
+
entry_at_home_count += IsEntryAtHome(h, shift, home);
|
|
3619
|
+
}
|
|
3620
|
+
yield_count += shard->GetTable().GetYieldCount();
|
|
3621
|
+
});
|
|
3622
|
+
ROCKS_LOG_AT_LEVEL(info_log, InfoLogLevel::DEBUG_LEVEL,
|
|
3623
|
+
"Head occupancy stats: %s", head_stats.Report().c_str());
|
|
3624
|
+
ROCKS_LOG_AT_LEVEL(info_log, InfoLogLevel::DEBUG_LEVEL,
|
|
3625
|
+
"Entries at home count: %zu", entry_at_home_count);
|
|
3626
|
+
ROCKS_LOG_AT_LEVEL(info_log, InfoLogLevel::DEBUG_LEVEL,
|
|
3627
|
+
"Yield count: %" PRIu64, yield_count);
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
|
|
1616
3631
|
} // namespace clock_cache
|
|
1617
3632
|
|
|
1618
3633
|
// DEPRECATED (see public API)
|
|
@@ -1640,11 +3655,6 @@ std::shared_ptr<Cache> HyperClockCacheOptions::MakeSharedCache() const {
|
|
|
1640
3655
|
}
|
|
1641
3656
|
std::shared_ptr<Cache> cache;
|
|
1642
3657
|
if (opts.estimated_entry_charge == 0) {
|
|
1643
|
-
// BEGIN placeholder logic to be removed
|
|
1644
|
-
// This is sufficient to get the placeholder Auto working in unit tests
|
|
1645
|
-
// much like the Fixed version.
|
|
1646
|
-
opts.estimated_entry_charge = opts.min_avg_entry_charge;
|
|
1647
|
-
// END placeholder logic to be removed
|
|
1648
3658
|
cache = std::make_shared<clock_cache::AutoHyperClockCache>(opts);
|
|
1649
3659
|
} else {
|
|
1650
3660
|
cache = std::make_shared<clock_cache::FixedHyperClockCache>(opts);
|