From 4a4a80343c2a5ff925423955d6e187d6bc90e275 Mon Sep 17 00:00:00 2001 From: Kevin Doherty Date: Wed, 2 Oct 2024 11:30:54 -0700 Subject: [PATCH] Add command line override to setting definition Summary: Add ability to control if settings can be overridden by a command line argument to setting definitions Reviewed By: yfeldblum Differential Revision: D63698031 fbshipit-source-id: f7c8ee29f0fbed280deee65c609d5f0e702ade8c --- folly/settings/Settings.h | 64 ++++++++++++---------- folly/settings/Types.h | 9 +++ folly/settings/test/SettingsBenchmarks.cpp | 1 + folly/settings/test/SettingsTest.cpp | 8 +++ folly/settings/test/a.cpp | 2 + folly/settings/test/b.cpp | 2 + 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/folly/settings/Settings.h b/folly/settings/Settings.h index 786c54289c8..05928a9b27f 100644 --- a/folly/settings/Settings.h +++ b/folly/settings/Settings.h @@ -189,36 +189,44 @@ class SettingWrapper { * @param _Type setting value type * @param _def default value for the setting * @param _mut mutability of the setting + * @param _cli determines if a setting can be set through the command line * @param _desc setting documentation */ -#define FOLLY_SETTING_DEFINE(_project, _name, _Type, _def, _mut, _desc) \ - /* Fastpath optimization, see notes in FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__. \ - Aggregate all off these together in a single section for better TLB \ - and cache locality. */ \ - __attribute__((__section__(".folly.settings.cache"))) ::std::atomic< \ - ::folly::settings::detail::SettingCore<_Type>*> \ - FOLLY_SETTINGS_CACHE__##_project##_##_name; \ - /* Location for the small value cache (if _Type is small and trivial). \ - Intentionally located right after the pointer cache above to take \ - advantage of the prefetching */ \ - __attribute__(( \ - __section__(".folly.settings.cache"))) ::std::atomic \ - FOLLY_SETTINGS_TRIVIAL__##_project##_##_name; \ - /* Meyers singleton to avoid SIOF */ \ - FOLLY_NOINLINE ::folly::settings::detail::SettingCore<_Type>& \ - FOLLY_SETTINGS_FUNC__##_project##_##_name() { \ - static ::folly::Indestructible< \ - ::folly::settings::detail::SettingCore<_Type>> \ - setting( \ - ::folly::settings::SettingMetadata{ \ - #_project, #_name, #_Type, typeid(_Type), #_def, _mut, _desc}, \ - ::folly::type_t<_Type>{_def}, \ - FOLLY_SETTINGS_TRIVIAL__##_project##_##_name); \ - return *setting; \ - } \ - /* Ensure the setting is registered even if not used in program */ \ - auto& FOLLY_SETTINGS_INIT__##_project##_##_name = \ - FOLLY_SETTINGS_FUNC__##_project##_##_name(); \ +#define FOLLY_SETTING_DEFINE(_project, _name, _Type, _def, _mut, _cli, _desc) \ + /* Fastpath optimization, see notes in FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__. \ + Aggregate all off these together in a single section for better TLB \ + and cache locality. */ \ + __attribute__((__section__(".folly.settings.cache"))) ::std::atomic< \ + ::folly::settings::detail::SettingCore<_Type>*> \ + FOLLY_SETTINGS_CACHE__##_project##_##_name; \ + /* Location for the small value cache (if _Type is small and trivial). \ + Intentionally located right after the pointer cache above to take \ + advantage of the prefetching */ \ + __attribute__(( \ + __section__(".folly.settings.cache"))) ::std::atomic \ + FOLLY_SETTINGS_TRIVIAL__##_project##_##_name; \ + /* Meyers singleton to avoid SIOF */ \ + FOLLY_NOINLINE ::folly::settings::detail::SettingCore<_Type>& \ + FOLLY_SETTINGS_FUNC__##_project##_##_name() { \ + static ::folly::Indestructible< \ + ::folly::settings::detail::SettingCore<_Type>> \ + setting( \ + ::folly::settings::SettingMetadata{ \ + #_project, \ + #_name, \ + #_Type, \ + typeid(_Type), \ + #_def, \ + _mut, \ + _cli, \ + _desc}, \ + ::folly::type_t<_Type>{_def}, \ + FOLLY_SETTINGS_TRIVIAL__##_project##_##_name); \ + return *setting; \ + } \ + /* Ensure the setting is registered even if not used in program */ \ + auto& FOLLY_SETTINGS_INIT__##_project##_##_name = \ + FOLLY_SETTINGS_FUNC__##_project##_##_name(); \ FOLLY_DETAIL_SETTINGS_DEFINE_LOCAL_FUNC__(_project, _name, _Type, char) /** diff --git a/folly/settings/Types.h b/folly/settings/Types.h index cddf195e061..dd1a6b0f9f4 100644 --- a/folly/settings/Types.h +++ b/folly/settings/Types.h @@ -42,6 +42,10 @@ enum class Mutability { Mutable, Immutable, }; +enum class CommandLine { + AcceptOverrides, + RejectOverrides, +}; /** * Static information about the setting definition @@ -78,6 +82,11 @@ struct SettingMetadata { */ Mutability mutability; + /** + * Determines if the setting can be set from the command line. + */ + CommandLine commandLine; + /** * Setting description field. */ diff --git a/folly/settings/test/SettingsBenchmarks.cpp b/folly/settings/test/SettingsBenchmarks.cpp index b5cab928778..7f211a8326b 100644 --- a/folly/settings/test/SettingsBenchmarks.cpp +++ b/folly/settings/test/SettingsBenchmarks.cpp @@ -33,6 +33,7 @@ FOLLY_SETTING_DEFINE( int, 100, folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "desc"); BENCHMARK(settings_get_bench, iters) { diff --git a/folly/settings/test/SettingsTest.cpp b/folly/settings/test/SettingsTest.cpp index bbb76287b0a..7bd8316a52e 100644 --- a/folly/settings/test/SettingsTest.cpp +++ b/folly/settings/test/SettingsTest.cpp @@ -31,6 +31,7 @@ FOLLY_SETTING_DEFINE( std::string, "default", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Description"); FOLLY_SETTING_DEFINE( follytest, @@ -38,6 +39,7 @@ FOLLY_SETTING_DEFINE( std::string, "unused_default", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Not used, but should still be in the list"); FOLLY_SETTING_DEFINE( follytest, @@ -45,6 +47,7 @@ FOLLY_SETTING_DEFINE( unsigned int, 123, folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Test that multi-token type names can be used"); // Enable to test runtime collision checking logic #if 0 @@ -54,6 +57,7 @@ FOLLY_SETTING_DEFINE( std::string, "collision_with_a", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Collision_with_a"); #endif @@ -119,6 +123,7 @@ FOLLY_SETTING_DEFINE( UserDefinedType, "b", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "User defined type constructed from string"); FOLLY_SETTING_DEFINE( follytest, @@ -126,6 +131,7 @@ FOLLY_SETTING_DEFINE( UserDefinedWithMeta, {"default"}, folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "User defined type constructed from string and metadata"); FOLLY_SETTING_DEFINE( follytest, @@ -133,6 +139,7 @@ FOLLY_SETTING_DEFINE( UserDefinedType, "b", folly::settings::Mutability::Immutable, + folly::settings::CommandLine::AcceptOverrides, "User defined type constructed from string"); FOLLY_SETTING_DEFINE( otherproj, @@ -140,6 +147,7 @@ FOLLY_SETTING_DEFINE( std::string, "default", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Description"); } // namespace some_ns diff --git a/folly/settings/test/a.cpp b/folly/settings/test/a.cpp index be46601c706..18f664b82c6 100644 --- a/folly/settings/test/a.cpp +++ b/folly/settings/test/a.cpp @@ -25,6 +25,7 @@ FOLLY_SETTING_DEFINE( int, 456, folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Public flag to a"); namespace { FOLLY_SETTING_DEFINE( @@ -33,6 +34,7 @@ FOLLY_SETTING_DEFINE( int, 789, folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Desc of int"); } diff --git a/folly/settings/test/b.cpp b/folly/settings/test/b.cpp index ec0e03ef9d7..731f0cce0d4 100644 --- a/folly/settings/test/b.cpp +++ b/folly/settings/test/b.cpp @@ -26,6 +26,7 @@ FOLLY_SETTING_DEFINE( std::string, "basdf", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Public flag to b"); namespace { @@ -35,6 +36,7 @@ FOLLY_SETTING_DEFINE( std::string, "test", folly::settings::Mutability::Mutable, + folly::settings::CommandLine::AcceptOverrides, "Desc of str"); }