Skip to content

Commit c93d0e0

Browse files
authored
Fix gflag namespace (#2877)
1 parent 6e0d1b3 commit c93d0e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+189
-237
lines changed

BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ COPTS = [
2929
"-D__STDC_FORMAT_MACROS",
3030
"-D__STDC_LIMIT_MACROS",
3131
"-D__STDC_CONSTANT_MACROS",
32-
"-DGFLAGS_NS=google",
3332
] + select({
3433
"//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
3534
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],

config_brpc.sh

+1-10
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,6 @@ fi
261261
PROTOC=$(find_bin_or_die protoc)
262262

263263
GFLAGS_HDR=$(find_dir_of_header_or_die gflags/gflags.h)
264-
# namespace of gflags may not be google, grep it from source.
265-
GFLAGS_NS=$(grep "namespace [_A-Za-z0-9]\+ {" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $2}')
266-
if [ "$GFLAGS_NS" = "GFLAGS_NAMESPACE" ]; then
267-
GFLAGS_NS=$(grep "#define GFLAGS_NAMESPACE [_A-Za-z0-9]\+" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $3}')
268-
fi
269-
if [ -z "$GFLAGS_NS" ]; then
270-
>&2 $ECHO "Fail to grep namespace of gflags source $GFLAGS_HDR/gflags/gflags_declare.h"
271-
exit 1
272-
fi
273264

274265
PROTOBUF_HDR=$(find_dir_of_header_or_die google/protobuf/message.h)
275266
PROTOBUF_VERSION=$(grep '#define GOOGLE_PROTOBUF_VERSION [0-9]\+' $PROTOBUF_HDR/google/protobuf/stubs/common.h | awk '{print $3}')
@@ -434,7 +425,7 @@ append_to_output "STATIC_LINKINGS=$STATIC_LINKINGS"
434425
append_to_output "DYNAMIC_LINKINGS=$DYNAMIC_LINKINGS"
435426

436427
# CPP means C PreProcessing, not C PlusPlus
437-
CPPFLAGS="${CPPFLAGS} -DBRPC_WITH_GLOG=$WITH_GLOG -DGFLAGS_NS=$GFLAGS_NS -DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=$BRPC_DEBUG_BTHREAD_SCHE_SAFETY -DBRPC_DEBUG_LOCK=$BRPC_DEBUG_LOCK"
428+
CPPFLAGS="${CPPFLAGS} -DBRPC_WITH_GLOG=$WITH_GLOG -DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=$BRPC_DEBUG_BTHREAD_SCHE_SAFETY -DBRPC_DEBUG_LOCK=$BRPC_DEBUG_LOCK"
438429

439430
# Avoid over-optimizations of TLS variables by GCC>=4.8
440431
# See: https://github.com/apache/brpc/issues/1693

src/brpc/builtin/flags_service.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static std::string HtmlReplace(const std::string& s) {
6363
}
6464
}
6565

66-
static void PrintFlag(std::ostream& os, const GFLAGS_NS::CommandLineFlagInfo& flag,
66+
static void PrintFlag(std::ostream& os, const GFLAGS_NAMESPACE::CommandLineFlagInfo& flag,
6767
bool use_html) {
6868
if (use_html) {
6969
os << "<tr><td>";
@@ -108,8 +108,8 @@ void FlagsService::set_value_page(Controller* cntl,
108108
::google::protobuf::Closure* done) {
109109
ClosureGuard done_guard(done);
110110
const std::string& name = cntl->http_request().unresolved_path();
111-
GFLAGS_NS::CommandLineFlagInfo info;
112-
if (!GFLAGS_NS::GetCommandLineFlagInfo(name.c_str(), &info)) {
111+
GFLAGS_NAMESPACE::CommandLineFlagInfo info;
112+
if (!GFLAGS_NAMESPACE::GetCommandLineFlagInfo(name.c_str(), &info)) {
113113
cntl->SetFailed(ENOMETHOD, "No such gflag");
114114
return;
115115
}
@@ -155,8 +155,8 @@ void FlagsService::default_method(::google::protobuf::RpcController* cntl_base,
155155
if (use_html && cntl->http_request().uri().GetQuery("withform")) {
156156
return set_value_page(cntl, done_guard.release());
157157
}
158-
GFLAGS_NS::CommandLineFlagInfo info;
159-
if (!GFLAGS_NS::GetCommandLineFlagInfo(constraint.c_str(), &info)) {
158+
GFLAGS_NAMESPACE::CommandLineFlagInfo info;
159+
if (!GFLAGS_NAMESPACE::GetCommandLineFlagInfo(constraint.c_str(), &info)) {
160160
cntl->SetFailed(ENOMETHOD, "No such gflag");
161161
return;
162162
}
@@ -169,8 +169,8 @@ void FlagsService::default_method(::google::protobuf::RpcController* cntl_base,
169169
constraint.c_str());
170170
return;
171171
}
172-
if (GFLAGS_NS::SetCommandLineOption(constraint.c_str(),
173-
value_str->c_str()).empty()) {
172+
if (GFLAGS_NAMESPACE::SetCommandLineOption(constraint.c_str(),
173+
value_str->c_str()).empty()) {
174174
cntl->SetFailed(EPERM, "Fail to set `%s' to %s",
175175
constraint.c_str(),
176176
(value_str->empty() ? "empty string" : value_str->c_str()));
@@ -218,19 +218,19 @@ void FlagsService::default_method(::google::protobuf::RpcController* cntl_base,
218218
// Only exact names. We don't have to iterate all flags in this case.
219219
for (std::set<std::string>::iterator it = exact.begin();
220220
it != exact.end(); ++it) {
221-
GFLAGS_NS::CommandLineFlagInfo info;
222-
if (GFLAGS_NS::GetCommandLineFlagInfo(it->c_str(), &info)) {
221+
GFLAGS_NAMESPACE::CommandLineFlagInfo info;
222+
if (GFLAGS_NAMESPACE::GetCommandLineFlagInfo(it->c_str(), &info)) {
223223
PrintFlag(os, info, use_html);
224224
os << '\n';
225225
}
226226
}
227227

228228
} else {
229229
// Iterate all flags and filter.
230-
std::vector<GFLAGS_NS::CommandLineFlagInfo> flag_list;
230+
std::vector<GFLAGS_NAMESPACE::CommandLineFlagInfo> flag_list;
231231
flag_list.reserve(128);
232-
GFLAGS_NS::GetAllFlags(&flag_list);
233-
for (std::vector<GFLAGS_NS::CommandLineFlagInfo>::iterator
232+
GFLAGS_NAMESPACE::GetAllFlags(&flag_list);
233+
for (std::vector<GFLAGS_NAMESPACE::CommandLineFlagInfo>::iterator
234234
it = flag_list.begin(); it != flag_list.end(); ++it) {
235235
if (!constraint.empty() &&
236236
exact.find(it->name) == exact.end() &&

src/brpc/builtin/rpcz_service.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void RpczService::enable(::google::protobuf::RpcController* cntl_base,
6565
const bool use_html = UseHTML(cntl->http_request());
6666
cntl->http_response().set_content_type(
6767
use_html ? "text/html" : "text/plain");
68-
if (!GFLAGS_NS::SetCommandLineOption("enable_rpcz", "true").empty()) {
68+
if (!GFLAGS_NAMESPACE::SetCommandLineOption("enable_rpcz", "true").empty()) {
6969
if (use_html) {
7070
// Redirect to /rpcz
7171
cntl->response_attachment().append(
@@ -94,7 +94,7 @@ void RpczService::disable(::google::protobuf::RpcController* cntl_base,
9494
const bool use_html = UseHTML(cntl->http_request());
9595
cntl->http_response().set_content_type(
9696
use_html ? "text/html" : "text/plain");
97-
if (!GFLAGS_NS::SetCommandLineOption("enable_rpcz", "false").empty()) {
97+
if (!GFLAGS_NAMESPACE::SetCommandLineOption("enable_rpcz", "false").empty()) {
9898
if (use_html) {
9999
// Redirect to /rpcz
100100
cntl->response_attachment().append(

src/brpc/rpc_dump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void SampledRequest::destroy() {
139139
// Save gflags which could be reloaded at anytime.
140140
void RpcDumpContext::SaveFlags() {
141141
std::string dir;
142-
CHECK(GFLAGS_NS::GetCommandLineOption("rpc_dump_dir", &dir));
142+
CHECK(GFLAGS_NAMESPACE::GetCommandLineOption("rpc_dump_dir", &dir));
143143

144144
const size_t pos = dir.find("<app>");
145145
if (pos != std::string::npos) {

src/bthread/bthread.cpp

+14-26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "butil/macros.h" // BAIDU_CASSERT
2525
#include "butil/logging.h"
2626
#include "butil/thread_local.h"
27+
#include "butil/reloadable_flags.h"
2728
#include "bthread/task_group.h" // TaskGroup
2829
#include "bthread/task_control.h" // TaskControl
2930
#include "bthread/timer_thread.h"
@@ -32,47 +33,34 @@
3233

3334
namespace bthread {
3435

36+
static bool validate_bthread_concurrency(const char*, int32_t val) {
37+
// bthread_setconcurrency sets the flag on success path which should
38+
// not be strictly in a validator. But it's OK for a int flag.
39+
return bthread_setconcurrency(val) == 0;
40+
}
41+
static bool validate_bthread_min_concurrency(const char*, int32_t val);
42+
static bool validate_bthread_current_tag(const char*, int32_t val);
43+
static bool validate_bthread_concurrency_by_tag(const char*, int32_t val);
44+
3545
DEFINE_int32(bthread_concurrency, 8 + BTHREAD_EPOLL_THREAD_NUM,
3646
"Number of pthread workers");
47+
BUTIL_VALIDATE_GFLAG(bthread_concurrency, validate_bthread_concurrency);
3748

3849
DEFINE_int32(bthread_min_concurrency, 0,
3950
"Initial number of pthread workers which will be added on-demand."
4051
" The laziness is disabled when this value is non-positive,"
4152
" and workers will be created eagerly according to -bthread_concurrency and bthread_setconcurrency(). ");
53+
BUTIL_VALIDATE_GFLAG(bthread_min_concurrency, validate_bthread_min_concurrency);
4254

4355
DEFINE_int32(bthread_current_tag, BTHREAD_TAG_INVALID, "Set bthread concurrency for this tag");
56+
BUTIL_VALIDATE_GFLAG(bthread_current_tag, validate_bthread_current_tag);
4457

4558
DEFINE_int32(bthread_concurrency_by_tag, 8 + BTHREAD_EPOLL_THREAD_NUM,
4659
"Number of pthread workers of FLAGS_bthread_current_tag");
60+
BUTIL_VALIDATE_GFLAG(bthread_concurrency_by_tag, validate_bthread_concurrency_by_tag);
4761

4862
static bool never_set_bthread_concurrency = true;
4963

50-
static bool validate_bthread_concurrency(const char*, int32_t val) {
51-
// bthread_setconcurrency sets the flag on success path which should
52-
// not be strictly in a validator. But it's OK for a int flag.
53-
return bthread_setconcurrency(val) == 0;
54-
}
55-
const int ALLOW_UNUSED register_FLAGS_bthread_concurrency =
56-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_bthread_concurrency,
57-
validate_bthread_concurrency);
58-
59-
static bool validate_bthread_min_concurrency(const char*, int32_t val);
60-
61-
const int ALLOW_UNUSED register_FLAGS_bthread_min_concurrency =
62-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_bthread_min_concurrency,
63-
validate_bthread_min_concurrency);
64-
65-
static bool validate_bthread_current_tag(const char*, int32_t val);
66-
67-
const int ALLOW_UNUSED register_FLAGS_bthread_current_tag =
68-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_bthread_current_tag, validate_bthread_current_tag);
69-
70-
static bool validate_bthread_concurrency_by_tag(const char*, int32_t val);
71-
72-
const int ALLOW_UNUSED register_FLAGS_bthread_concurrency_by_tag =
73-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_bthread_concurrency_by_tag,
74-
validate_bthread_concurrency_by_tag);
75-
7664
BAIDU_CASSERT(sizeof(TaskControl*) == sizeof(butil::atomic<TaskControl*>), atomic_size_match);
7765

7866
pthread_mutex_t g_task_control_mutex = PTHREAD_MUTEX_INITIALIZER;

src/bthread/task_group.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "butil/fast_rand.h"
2929
#include "butil/unique_ptr.h"
3030
#include "butil/third_party/murmurhash3/murmurhash3.h" // fmix64
31+
#include "butil/reloadable_flags.h"
3132
#include "bthread/errno.h" // ESTOP
3233
#include "bthread/butex.h" // butex_*
3334
#include "bthread/sys_futex.h" // futex_wake_private
@@ -41,24 +42,17 @@ namespace bthread {
4142
static const bthread_attr_t BTHREAD_ATTR_TASKGROUP = {
4243
BTHREAD_STACKTYPE_UNKNOWN, 0, NULL, BTHREAD_TAG_INVALID };
4344

44-
static bool pass_bool(const char*, bool) { return true; }
45-
4645
DEFINE_bool(show_bthread_creation_in_vars, false, "When this flags is on, The time "
4746
"from bthread creation to first run will be recorded and shown in /vars");
48-
const bool ALLOW_UNUSED dummy_show_bthread_creation_in_vars =
49-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_show_bthread_creation_in_vars,
50-
pass_bool);
47+
BUTIL_VALIDATE_GFLAG(show_bthread_creation_in_vars, butil::PassValidate);
5148

5249
DEFINE_bool(show_per_worker_usage_in_vars, false,
5350
"Show per-worker usage in /vars/bthread_per_worker_usage_<tid>");
54-
const bool ALLOW_UNUSED dummy_show_per_worker_usage_in_vars =
55-
::GFLAGS_NS::RegisterFlagValidator(&FLAGS_show_per_worker_usage_in_vars,
56-
pass_bool);
51+
BUTIL_VALIDATE_GFLAG(show_per_worker_usage_in_vars, butil::PassValidate);
5752

5853
DEFINE_bool(bthread_enable_cpu_clock_stat, false,
5954
"Enable CPU clock statistics for bthread");
60-
const bool ALLOW_UNUSED dummy_bthread_enable_cpu_clock_stat = ::GFLAGS_NS::RegisterFlagValidator(&FLAGS_bthread_enable_cpu_clock_stat,
61-
pass_bool);
55+
BUTIL_VALIDATE_GFLAG(bthread_enable_cpu_clock_stat, butil::PassValidate);
6256

6357
BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group, NULL);
6458
// Sync with TaskMeta::local_storage when a bthread is created or destroyed.

src/butil/logging.cc

+7-21
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef pthread_mutex_t* MutexHandle;
102102
#include "butil/containers/doubly_buffered_data.h"
103103
#include "butil/memory/singleton.h"
104104
#include "butil/endpoint.h"
105+
#include "butil/reloadable_flags.h"
105106
#ifdef BAIDU_INTERNAL
106107
#include "butil/comlog_sink.h"
107108
#endif
@@ -122,8 +123,11 @@ namespace logging {
122123

123124
DEFINE_bool(crash_on_fatal_log, false,
124125
"Crash process when a FATAL log is printed");
126+
BUTIL_VALIDATE_GFLAG(crash_on_fatal_log, butil::PassValidate);
127+
125128
DEFINE_bool(print_stack_on_check, true,
126129
"Print the stack trace when a CHECK was failed");
130+
BUTIL_VALIDATE_GFLAG(print_stack_on_check, butil::PassValidate);
127131

128132
DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this."
129133
" Overridable by --vmodule.");
@@ -140,6 +144,7 @@ DEFINE_bool(log_bid, true, "Log bthread id");
140144
DEFINE_int32(minloglevel, 0, "Any log at or above this level will be "
141145
"displayed. Anything below this level will be silently ignored. "
142146
"0=INFO 1=NOTICE 2=WARNING 3=ERROR 4=FATAL");
147+
BUTIL_VALIDATE_GFLAG(minloglevel, butil::NonNegativeInteger);
143148

144149
DEFINE_bool(log_hostname, false, "Add host after pid in each log so"
145150
" that we know where logs came from when using aggregation tools"
@@ -1930,7 +1935,7 @@ static bool validate_vmodule(const char*, const std::string& vmodule) {
19301935
return on_reset_vmodule(vmodule.c_str()) == 0;
19311936
}
19321937

1933-
const bool ALLOW_UNUSED validate_vmodule_dummy = GFLAGS_NS::RegisterFlagValidator(
1938+
const bool ALLOW_UNUSED validate_vmodule_dummy = GFLAGS_NAMESPACE::RegisterFlagValidator(
19341939
&FLAGS_vmodule, &validate_vmodule);
19351940

19361941
// [Thread-safe] Reset FLAGS_v.
@@ -1959,26 +1964,7 @@ static bool validate_v(const char*, int32_t v) {
19591964
on_reset_verbose(v);
19601965
return true;
19611966
}
1962-
1963-
const bool ALLOW_UNUSED validate_v_dummy = GFLAGS_NS::RegisterFlagValidator(
1964-
&FLAGS_v, &validate_v);
1965-
1966-
static bool PassValidate(const char*, bool) {
1967-
return true;
1968-
}
1969-
1970-
const bool ALLOW_UNUSED validate_crash_on_fatal_log =
1971-
GFLAGS_NS::RegisterFlagValidator(&FLAGS_crash_on_fatal_log, PassValidate);
1972-
1973-
const bool ALLOW_UNUSED validate_print_stack_on_check =
1974-
GFLAGS_NS::RegisterFlagValidator(&FLAGS_print_stack_on_check, PassValidate);
1975-
1976-
static bool NonNegativeInteger(const char*, int32_t v) {
1977-
return v >= 0;
1978-
}
1979-
1980-
const bool ALLOW_UNUSED validate_min_log_level = GFLAGS_NS::RegisterFlagValidator(
1981-
&FLAGS_minloglevel, NonNegativeInteger);
1967+
BUTIL_VALIDATE_GFLAG(v, validate_v);
19821968

19831969
} // namespace logging
19841970

src/butil/reloadable_flags.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
//
3535
// This macro does not work for string-flags because they're thread-unsafe to
3636
// modify directly. To emphasize this, you have to write the validator by
37-
// yourself and use GFLAGS_NS::GetCommandLineOption() to acess the flag.
38-
#define BUTIL_VALIDATE_GFLAG(flag, validate_fn) \
39-
namespace butil_flags {} \
40-
const int register_FLAGS_ ## flag ## _dummy \
41-
__attribute__((__unused__)) = \
42-
::butil::RegisterFlagValidatorOrDieImpl< \
37+
// yourself and use GFLAGS_NAMESPACE::GetCommandLineOption() to acess the flag.
38+
#define BUTIL_VALIDATE_GFLAG(flag, validate_fn) \
39+
namespace butil_flags {} \
40+
const int ALLOW_UNUSED register_FLAGS_ ## flag ## _dummy = \
41+
::butil::RegisterFlagValidatorOrDieImpl< \
4342
decltype(FLAGS_##flag)>(&FLAGS_##flag, (validate_fn))
4443

4544

@@ -55,12 +54,17 @@ bool PositiveInteger(const char*, T v) {
5554
return v > 0;
5655
}
5756

57+
template <typename T>
58+
bool NonNegativeInteger(const char*, T v) {
59+
return v >= 0;
60+
}
61+
5862
template <typename T>
5963
bool RegisterFlagValidatorOrDieImpl(
6064
const T* flag, bool (*validate_fn)(const char*, T val)) {
6165
static_assert(!butil::is_same<std::string, T>::value,
6266
"Not support string flags");
63-
if (GFLAGS_NS::RegisterFlagValidator(flag, validate_fn)) {
67+
if (::GFLAGS_NAMESPACE::RegisterFlagValidator(flag, validate_fn)) {
6468
return true;
6569
}
6670
// Error printed by gflags does not have newline. Add one to it.

src/bvar/gflag.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ GFlag::GFlag(const butil::StringPiece& prefix,
3434
}
3535

3636
void GFlag::describe(std::ostream& os, bool quote_string) const {
37-
GFLAGS_NS::CommandLineFlagInfo info;
38-
if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
37+
GFLAGS_NAMESPACE::CommandLineFlagInfo info;
38+
if (!GFLAGS_NAMESPACE::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
3939
if (quote_string) {
4040
os << '"';
4141
}
@@ -54,8 +54,8 @@ void GFlag::describe(std::ostream& os, bool quote_string) const {
5454

5555
#ifdef BAIDU_INTERNAL
5656
void GFlag::get_value(boost::any* value) const {
57-
GFLAGS_NS::CommandLineFlagInfo info;
58-
if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
57+
GFLAGS_NAMESPACE::CommandLineFlagInfo info;
58+
if (!GFLAGS_NAMESPACE::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
5959
*value = "Unknown gflag=" + gflag_name();
6060
} else if (info.type == "string") {
6161
*value = info.current_value;
@@ -78,14 +78,14 @@ void GFlag::get_value(boost::any* value) const {
7878

7979
std::string GFlag::get_value() const {
8080
std::string str;
81-
if (!GFLAGS_NS::GetCommandLineOption(gflag_name().c_str(), &str)) {
81+
if (!GFLAGS_NAMESPACE::GetCommandLineOption(gflag_name().c_str(), &str)) {
8282
return "Unknown gflag=" + gflag_name();
8383
}
8484
return str;
8585
}
8686

8787
bool GFlag::set_value(const char* value) {
88-
return !GFLAGS_NS::SetCommandLineOption(gflag_name().c_str(), value).empty();
88+
return !GFLAGS_NAMESPACE::SetCommandLineOption(gflag_name().c_str(), value).empty();
8989
}
9090

9191
} // namespace bvar

0 commit comments

Comments
 (0)