/Users/deen/code/yugabyte-db/src/yb/util/flags.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/flags.h" |
34 | | |
35 | | #include <string> |
36 | | #include <unordered_set> |
37 | | #include <vector> |
38 | | |
39 | | #ifdef TCMALLOC_ENABLED |
40 | | #include <gperftools/heap-profiler.h> |
41 | | #endif |
42 | | |
43 | | #include "yb/gutil/strings/join.h" |
44 | | #include "yb/gutil/strings/substitute.h" |
45 | | #include "yb/util/flag_tags.h" |
46 | | #include "yb/util/metrics.h" |
47 | | #include "yb/util/path_util.h" |
48 | | #include "yb/util/url-coding.h" |
49 | | #include "yb/util/version_info.h" |
50 | | |
51 | | using google::CommandLineFlagInfo; |
52 | | using std::cout; |
53 | | using std::endl; |
54 | | using std::string; |
55 | | using std::unordered_set; |
56 | | |
57 | | // Because every binary initializes its flags here, we use it as a convenient place |
58 | | // to offer some global flags as well. |
59 | | DEFINE_bool(dump_metrics_json, false, |
60 | | "Dump a JSON document describing all of the metrics which may be emitted " |
61 | | "by this binary."); |
62 | | TAG_FLAG(dump_metrics_json, hidden); |
63 | | |
64 | | DEFINE_bool(enable_process_lifetime_heap_profiling, false, "Enables heap " |
65 | | "profiling for the lifetime of the process. Profile output will be stored in the " |
66 | | "directory specified by -heap_profile_path. Enabling this option will disable the " |
67 | | "on-demand/remote server profile handlers."); |
68 | | TAG_FLAG(enable_process_lifetime_heap_profiling, stable); |
69 | | TAG_FLAG(enable_process_lifetime_heap_profiling, advanced); |
70 | | |
71 | | DEFINE_string(heap_profile_path, "", "Output path to store heap profiles. If not set " \ |
72 | | "profiles are stored in /tmp/<process-name>.<pid>.<n>.heap."); |
73 | | TAG_FLAG(heap_profile_path, stable); |
74 | | TAG_FLAG(heap_profile_path, advanced); |
75 | | |
76 | | DEFINE_int32(svc_queue_length_default, 50, "Default RPC queue length for a service"); |
77 | | TAG_FLAG(svc_queue_length_default, advanced); |
78 | | |
79 | | // Tag a bunch of the flags that we inherit from glog/gflags. |
80 | | |
81 | | //------------------------------------------------------------ |
82 | | // GLog flags |
83 | | //------------------------------------------------------------ |
84 | | // Most of these are considered stable. The ones related to email are |
85 | | // marked unsafe because sending email inline from a server is a pretty |
86 | | // bad idea. |
87 | | DECLARE_string(alsologtoemail); |
88 | | TAG_FLAG(alsologtoemail, hidden); |
89 | | TAG_FLAG(alsologtoemail, unsafe); |
90 | | |
91 | | // --alsologtostderr is deprecated in favor of --stderrthreshold |
92 | | DECLARE_bool(alsologtostderr); |
93 | | TAG_FLAG(alsologtostderr, hidden); |
94 | | TAG_FLAG(alsologtostderr, runtime); |
95 | | |
96 | | DECLARE_bool(colorlogtostderr); |
97 | | TAG_FLAG(colorlogtostderr, stable); |
98 | | TAG_FLAG(colorlogtostderr, runtime); |
99 | | |
100 | | DECLARE_bool(drop_log_memory); |
101 | | TAG_FLAG(drop_log_memory, advanced); |
102 | | TAG_FLAG(drop_log_memory, runtime); |
103 | | |
104 | | DECLARE_string(log_backtrace_at); |
105 | | TAG_FLAG(log_backtrace_at, advanced); |
106 | | |
107 | | DECLARE_string(log_dir); |
108 | | TAG_FLAG(log_dir, stable); |
109 | | |
110 | | DECLARE_string(log_link); |
111 | | TAG_FLAG(log_link, stable); |
112 | | TAG_FLAG(log_link, advanced); |
113 | | |
114 | | DECLARE_bool(log_prefix); |
115 | | TAG_FLAG(log_prefix, stable); |
116 | | TAG_FLAG(log_prefix, advanced); |
117 | | TAG_FLAG(log_prefix, runtime); |
118 | | |
119 | | DECLARE_int32(logbuflevel); |
120 | | TAG_FLAG(logbuflevel, advanced); |
121 | | TAG_FLAG(logbuflevel, runtime); |
122 | | DECLARE_int32(logbufsecs); |
123 | | TAG_FLAG(logbufsecs, advanced); |
124 | | TAG_FLAG(logbufsecs, runtime); |
125 | | |
126 | | DECLARE_int32(logemaillevel); |
127 | | TAG_FLAG(logemaillevel, hidden); |
128 | | TAG_FLAG(logemaillevel, unsafe); |
129 | | |
130 | | DECLARE_string(logmailer); |
131 | | TAG_FLAG(logmailer, hidden); |
132 | | |
133 | | DECLARE_bool(logtostderr); |
134 | | TAG_FLAG(logtostderr, stable); |
135 | | TAG_FLAG(logtostderr, runtime); |
136 | | |
137 | | DECLARE_int32(max_log_size); |
138 | | TAG_FLAG(max_log_size, stable); |
139 | | TAG_FLAG(max_log_size, runtime); |
140 | | |
141 | | DECLARE_int32(minloglevel); |
142 | | TAG_FLAG(minloglevel, stable); |
143 | | TAG_FLAG(minloglevel, advanced); |
144 | | TAG_FLAG(minloglevel, runtime); |
145 | | |
146 | | DECLARE_int32(stderrthreshold); |
147 | | TAG_FLAG(stderrthreshold, stable); |
148 | | TAG_FLAG(stderrthreshold, advanced); |
149 | | TAG_FLAG(stderrthreshold, runtime); |
150 | | |
151 | | DECLARE_bool(stop_logging_if_full_disk); |
152 | | TAG_FLAG(stop_logging_if_full_disk, stable); |
153 | | TAG_FLAG(stop_logging_if_full_disk, advanced); |
154 | | TAG_FLAG(stop_logging_if_full_disk, runtime); |
155 | | |
156 | | DECLARE_int32(v); |
157 | | TAG_FLAG(v, stable); |
158 | | TAG_FLAG(v, advanced); |
159 | | TAG_FLAG(v, runtime); |
160 | | |
161 | | DECLARE_string(vmodule); |
162 | | TAG_FLAG(vmodule, stable); |
163 | | TAG_FLAG(vmodule, advanced); |
164 | | |
165 | | DECLARE_bool(symbolize_stacktrace); |
166 | | TAG_FLAG(symbolize_stacktrace, stable); |
167 | | TAG_FLAG(symbolize_stacktrace, runtime); |
168 | | TAG_FLAG(symbolize_stacktrace, advanced); |
169 | | |
170 | | //------------------------------------------------------------ |
171 | | // GFlags flags |
172 | | //------------------------------------------------------------ |
173 | | DECLARE_string(flagfile); |
174 | | TAG_FLAG(flagfile, stable); |
175 | | |
176 | | DECLARE_string(fromenv); |
177 | | TAG_FLAG(fromenv, stable); |
178 | | TAG_FLAG(fromenv, advanced); |
179 | | |
180 | | DECLARE_string(tryfromenv); |
181 | | TAG_FLAG(tryfromenv, stable); |
182 | | TAG_FLAG(tryfromenv, advanced); |
183 | | |
184 | | DECLARE_string(undefok); |
185 | | TAG_FLAG(undefok, stable); |
186 | | TAG_FLAG(undefok, advanced); |
187 | | |
188 | | DECLARE_int32(tab_completion_columns); |
189 | | TAG_FLAG(tab_completion_columns, stable); |
190 | | TAG_FLAG(tab_completion_columns, hidden); |
191 | | |
192 | | DECLARE_string(tab_completion_word); |
193 | | TAG_FLAG(tab_completion_word, stable); |
194 | | TAG_FLAG(tab_completion_word, hidden); |
195 | | |
196 | | DECLARE_bool(help); |
197 | | TAG_FLAG(help, stable); |
198 | | |
199 | | DECLARE_bool(helpfull); |
200 | | // We hide -helpfull because it's the same as -help for now. |
201 | | TAG_FLAG(helpfull, stable); |
202 | | TAG_FLAG(helpfull, hidden); |
203 | | |
204 | | DECLARE_string(helpmatch); |
205 | | TAG_FLAG(helpmatch, stable); |
206 | | TAG_FLAG(helpmatch, advanced); |
207 | | |
208 | | DECLARE_string(helpon); |
209 | | TAG_FLAG(helpon, stable); |
210 | | TAG_FLAG(helpon, advanced); |
211 | | |
212 | | DECLARE_bool(helppackage); |
213 | | TAG_FLAG(helppackage, stable); |
214 | | TAG_FLAG(helppackage, advanced); |
215 | | |
216 | | DECLARE_bool(helpshort); |
217 | | TAG_FLAG(helpshort, stable); |
218 | | TAG_FLAG(helpshort, advanced); |
219 | | |
220 | | DECLARE_bool(helpxml); |
221 | | TAG_FLAG(helpxml, stable); |
222 | | TAG_FLAG(helpxml, advanced); |
223 | | |
224 | | DECLARE_bool(version); |
225 | | TAG_FLAG(version, stable); |
226 | | |
227 | | namespace yb { |
228 | | namespace { |
229 | | |
230 | 0 | void AppendXMLTag(const char* tag, const string& txt, string* r) { |
231 | 0 | strings::SubstituteAndAppend(r, "<$0>$1</$0>", tag, EscapeForHtmlToString(txt)); |
232 | 0 | } |
233 | | |
234 | 0 | static string DescribeOneFlagInXML(const CommandLineFlagInfo& flag) { |
235 | 0 | unordered_set<string> tags; |
236 | 0 | GetFlagTags(flag.name, &tags); |
237 | |
|
238 | 0 | string r("<flag>"); |
239 | 0 | AppendXMLTag("file", flag.filename, &r); |
240 | 0 | AppendXMLTag("name", flag.name, &r); |
241 | 0 | AppendXMLTag("meaning", flag.description, &r); |
242 | 0 | AppendXMLTag("default", flag.default_value, &r); |
243 | 0 | AppendXMLTag("current", flag.current_value, &r); |
244 | 0 | AppendXMLTag("type", flag.type, &r); |
245 | 0 | AppendXMLTag("tags", JoinStrings(tags, ","), &r); |
246 | 0 | r += "</flag>"; |
247 | 0 | return r; |
248 | 0 | } |
249 | | |
250 | 0 | void DumpFlagsXML() { |
251 | 0 | vector<CommandLineFlagInfo> flags; |
252 | 0 | GetAllFlags(&flags); |
253 | |
|
254 | 0 | cout << "<?xml version=\"1.0\"?>" << endl; |
255 | 0 | cout << "<AllFlags>" << endl; |
256 | 0 | cout << strings::Substitute( |
257 | 0 | "<program>$0</program>", |
258 | 0 | EscapeForHtmlToString(BaseName(google::ProgramInvocationShortName()))) << endl; |
259 | 0 | cout << strings::Substitute( |
260 | 0 | "<usage>$0</usage>", |
261 | 0 | EscapeForHtmlToString(google::ProgramUsage())) << endl; |
262 | |
|
263 | 0 | for (const CommandLineFlagInfo& flag : flags) { |
264 | 0 | cout << DescribeOneFlagInXML(flag) << std::endl; |
265 | 0 | } |
266 | |
|
267 | 0 | cout << "</AllFlags>" << endl; |
268 | 0 | exit(1); |
269 | 0 | } |
270 | | |
271 | 1 | void ShowVersionAndExit() { |
272 | 1 | cout << VersionInfo::GetShortVersionString() << endl; |
273 | 1 | exit(0); |
274 | 1 | } |
275 | | |
276 | | } // anonymous namespace |
277 | | |
278 | 17.4k | int ParseCommandLineFlags(int* argc, char*** argv, bool remove_flags) { |
279 | 17.4k | int ret = google::ParseCommandLineNonHelpFlags(argc, argv, remove_flags); |
280 | | |
281 | 17.4k | if (FLAGS_helpxml) { |
282 | 0 | DumpFlagsXML(); |
283 | 17.4k | } else if (FLAGS_dump_metrics_json) { |
284 | 0 | std::stringstream s; |
285 | 0 | JsonWriter w(&s, JsonWriter::PRETTY); |
286 | 0 | WriteRegistryAsJson(&w); |
287 | 0 | std::cout << s.str() << std::endl; |
288 | 0 | exit(0); |
289 | 17.4k | } else if (FLAGS_version) { |
290 | 1 | ShowVersionAndExit(); |
291 | 17.4k | } else { |
292 | 17.4k | google::HandleCommandLineHelpFlags(); |
293 | 17.4k | } |
294 | | |
295 | 17.4k | if (FLAGS_heap_profile_path.empty()) { |
296 | 17.4k | FLAGS_heap_profile_path = strings::Substitute( |
297 | 17.4k | "/tmp/$0.$1", google::ProgramInvocationShortName(), getpid()); |
298 | 17.4k | } |
299 | | |
300 | | #ifdef TCMALLOC_ENABLED |
301 | | if (FLAGS_enable_process_lifetime_heap_profiling) { |
302 | | HeapProfilerStart(FLAGS_heap_profile_path.c_str()); |
303 | | } |
304 | | #endif |
305 | | |
306 | 17.4k | return ret; |
307 | 17.4k | } |
308 | | |
309 | 3 | bool RefreshFlagsFile(const std::string& filename) { |
310 | | // prog_name is a placeholder that isn't really used by ReadFromFlags. |
311 | | // TODO: Find a better way to refresh flags from the file, ReadFromFlagsFile is going to be |
312 | | // deprecated. |
313 | 3 | const char* prog_name = "yb"; |
314 | 3 | return google::ReadFromFlagsFile(filename, prog_name, false); |
315 | 3 | } |
316 | | |
317 | | } // namespace yb |