Skip to content

Commit

Permalink
Add command line override to setting definition
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kevin Doherty authored and facebook-github-bot committed Oct 2, 2024
1 parent 19095b7 commit 4a4a803
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
64 changes: 36 additions & 28 deletions folly/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> \
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<uint64_t> \
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)

/**
Expand Down
9 changes: 9 additions & 0 deletions folly/settings/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ enum class Mutability {
Mutable,
Immutable,
};
enum class CommandLine {
AcceptOverrides,
RejectOverrides,
};

/**
* Static information about the setting definition
Expand Down Expand Up @@ -78,6 +82,11 @@ struct SettingMetadata {
*/
Mutability mutability;

/**
* Determines if the setting can be set from the command line.
*/
CommandLine commandLine;

/**
* Setting description field.
*/
Expand Down
1 change: 1 addition & 0 deletions folly/settings/test/SettingsBenchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ FOLLY_SETTING_DEFINE(
int,
100,
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"desc");

BENCHMARK(settings_get_bench, iters) {
Expand Down
8 changes: 8 additions & 0 deletions folly/settings/test/SettingsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ FOLLY_SETTING_DEFINE(
std::string,
"default",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Description");
FOLLY_SETTING_DEFINE(
follytest,
unused,
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,
multi_token_type,
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
Expand All @@ -54,6 +57,7 @@ FOLLY_SETTING_DEFINE(
std::string,
"collision_with_a",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Collision_with_a");
#endif

Expand Down Expand Up @@ -119,27 +123,31 @@ FOLLY_SETTING_DEFINE(
UserDefinedType,
"b",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"User defined type constructed from string");
FOLLY_SETTING_DEFINE(
follytest,
user_defined_with_meta,
UserDefinedWithMeta,
{"default"},
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"User defined type constructed from string and metadata");
FOLLY_SETTING_DEFINE(
follytest,
immutable_setting,
UserDefinedType,
"b",
folly::settings::Mutability::Immutable,
folly::settings::CommandLine::AcceptOverrides,
"User defined type constructed from string");
FOLLY_SETTING_DEFINE(
otherproj,
some_flag,
std::string,
"default",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Description");

} // namespace some_ns
Expand Down
2 changes: 2 additions & 0 deletions folly/settings/test/a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -33,6 +34,7 @@ FOLLY_SETTING_DEFINE(
int,
789,
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Desc of int");
}

Expand Down
2 changes: 2 additions & 0 deletions folly/settings/test/b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ FOLLY_SETTING_DEFINE(
std::string,
"basdf",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Public flag to b");

namespace {
Expand All @@ -35,6 +36,7 @@ FOLLY_SETTING_DEFINE(
std::string,
"test",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"Desc of str");
}

Expand Down

0 comments on commit 4a4a803

Please sign in to comment.