Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
default-path-handlers.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <sstream>
18 #include <fstream>
19 #include <sys/stat.h>
20 #include <boost/algorithm/string.hpp>
21 #include <boost/bind.hpp>
22 #include <google/malloc_extension.h>
23 #include <gutil/strings/substitute.h>
24 
25 #include "common/logging.h"
26 #include "runtime/mem-tracker.h"
27 #include "util/debug-util.h"
29 #include "util/webserver.h"
30 
31 #include "common/names.h"
32 
33 using namespace google;
34 using namespace impala;
35 using namespace rapidjson;
36 using namespace strings;
37 
38 DECLARE_bool(enable_process_lifetime_heap_profiling);
39 DEFINE_int64(web_log_bytes, 1024 * 1024,
40  "The maximum number of bytes to display on the debug webserver's log page");
41 
42 // Writes the last FLAGS_web_log_bytes of the INFO logfile to a webpage
43 // Note to get best performance, set GLOG_logbuflevel=-1 to prevent log buffering
44 void LogsHandler(const Webserver::ArgumentMap& args, Document* document) {
45  string logfile;
46  impala::GetFullLogFilename(google::INFO, &logfile);
47  Value log_path(logfile.c_str(), document->GetAllocator());
48  document->AddMember("logfile", log_path, document->GetAllocator());
49 
50  struct stat file_stat;
51  if (stat(logfile.c_str(), &file_stat) == 0) {
52  long size = file_stat.st_size;
53  long seekpos = size < FLAGS_web_log_bytes ? 0L : size - FLAGS_web_log_bytes;
54  ifstream log(logfile.c_str(), ios::in);
55  // Note if the file rolls between stat and seek, this could fail (and we could wind up
56  // reading the whole file). But because the file is likely to be small, this is
57  // unlikely to be an issue in practice.
58  log.seekg(seekpos);
59  document->AddMember("num_bytes", FLAGS_web_log_bytes, document->GetAllocator());
60  stringstream ss;
61  ss << log.rdbuf();
62  Value log_json(ss.str().c_str(), document->GetAllocator());
63  document->AddMember("log", log_json, document->GetAllocator());
64  } else {
65  Value error(Substitute("Couldn't open INFO log file: $0", logfile).c_str(),
66  document->GetAllocator());
67  document->AddMember("error", error, document->GetAllocator());
68  }
69 }
70 
71 // Registered to handle "/flags", and produces Json with 'title" and 'contents' members
72 // where the latter is a string with all the command-line flags and their values.
73 void FlagsHandler(const Webserver::ArgumentMap& args, Document* document) {
74  Value title("Command-line Flags", document->GetAllocator());
75  document->AddMember("title", title, document->GetAllocator());
76  Value flags(CommandlineFlagsIntoString().c_str(), document->GetAllocator());
77  document->AddMember("contents", flags, document->GetAllocator());
78 }
79 
80 // Registered to handle "/memz"
81 void MemUsageHandler(MemTracker* mem_tracker, const Webserver::ArgumentMap& args,
82  Document* document) {
83  DCHECK(mem_tracker != NULL);
84  Value mem_limit(PrettyPrinter::Print(mem_tracker->limit(), TUnit::BYTES).c_str(),
85  document->GetAllocator());
86  document->AddMember("mem_limit", mem_limit, document->GetAllocator());
87  Value consumption(
88  PrettyPrinter::Print(mem_tracker->consumption(), TUnit::BYTES).c_str(),
89  document->GetAllocator());
90  document->AddMember("consumption", consumption, document->GetAllocator());
91 
92  stringstream ss;
93 #ifdef ADDRESS_SANITIZER
94  ss << "Memory tracking is not available with address sanitizer builds.";
95 #else
96  char buf[2048];
97  MallocExtension::instance()->GetStats(buf, 2048);
98  ss << string(buf);
99 #endif
100 
101  Value overview(ss.str().c_str(), document->GetAllocator());
102  document->AddMember("overview", overview, document->GetAllocator());
103 
104  if (args.find("detailed") != args.end()) {
105  // Dump all mem trackers.
106  Value detailed(mem_tracker->LogUsage().c_str(), document->GetAllocator());
107  document->AddMember("detailed", detailed, document->GetAllocator());
108  }
109 }
110 
112  Webserver* webserver, MemTracker* process_mem_tracker) {
113  webserver->RegisterUrlCallback("/logs", "logs.tmpl", LogsHandler);
114  webserver->RegisterUrlCallback("/varz", "common-pre.tmpl", FlagsHandler);
115  if (process_mem_tracker != NULL) {
116  webserver->RegisterUrlCallback("/memz","memz.tmpl",
117  bind<void>(&MemUsageHandler, process_mem_tracker, _1, _2));
118  }
119 
120 #ifndef ADDRESS_SANITIZER
121  // Remote (on-demand) profiling is disabled if the process is already being profiled.
122  if (!FLAGS_enable_process_lifetime_heap_profiling) {
123  AddPprofUrlCallbacks(webserver);
124  }
125 #endif
126 }
int64_t consumption() const
Returns the memory consumed in bytes.
Definition: mem-tracker.h:298
void MemUsageHandler(MemTracker *mem_tracker, const Webserver::ArgumentMap &args, Document *document)
void AddDefaultUrlCallbacks(Webserver *webserver, MemTracker *process_mem_tracker=NULL)
void FlagsHandler(const Webserver::ArgumentMap &args, Document *document)
void RegisterUrlCallback(const std::string &path, const std::string &template_filename, const UrlCallback &callback, bool is_on_nav_bar=true)
Only one callback may be registered per URL.
Definition: webserver.cc:412
std::map< std::string, std::string > ArgumentMap
Definition: webserver.h:36
void GetFullLogFilename(google::LogSeverity severity, std::string *filename)
DECLARE_bool(enable_process_lifetime_heap_profiling)
void AddPprofUrlCallbacks(Webserver *webserver)
Adds set of path handlers to support pprof profiling of a remote server.
This class is thread-safe.
Definition: mem-tracker.h:61
DEFINE_int64(web_log_bytes, 1024 *1024,"The maximum number of bytes to display on the debug webserver's log page")
int64_t limit() const
Definition: mem-tracker.h:281
void LogsHandler(const Webserver::ArgumentMap &args, Document *document)
std::string LogUsage(const std::string &prefix="") const
Logs the usage of this tracker and all of its children (recursively).