// Copyright (c) 2017, Baidu.com, Inc. All Rights Reserved // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. #include "http/default_path_handlers.h" #include #include #include #include #include #include #include "common/logging.h" #include "runtime/mem_tracker.h" #include "util/debug_util.h" #include "util/logging.h" #include "http/web_page_handler.h" namespace palo { // Writes the last config::web_log_bytes of the INFO logfile to a webpage // Note to get best performance, set GLOG_logbuflevel=-1 to prevent log buffering void logs_handler(const WebPageHandler::ArgumentMap& args, std::stringstream* output) { /*std::string logfile; get_full_log_filename(google::INFO, &logfile); (*output) << "

INFO logs

" << std::endl; (*output) << "Log path is: " << logfile << std::endl; struct stat file_stat; if (stat(logfile.c_str(), &file_stat) == 0) { long size = file_stat.st_size; long seekpos = size < config::web_log_bytes ? 0L : size - config::web_log_bytes; std::ifstream log(logfile.c_str(), std::ios::in); // Note if the file rolls between stat and seek, this could fail // (and we could wind up reading the whole file). But because the // file is likely to be small, this is unlikely to be an issue in // practice. log.seekg(seekpos); (*output) << "
Showing last " << config::web_log_bytes << " bytes of log" << std::endl; (*output) << "
" << log.rdbuf() << "
"; } else { (*output) << "
Couldn't open INFO log file: " << logfile; }*/ (*output) << "
Couldn't open INFO log file: "; } // Registered to handle "/flags", and prints out all command-line flags and their values void config_handler(const WebPageHandler::ArgumentMap& args, std::stringstream* output) { (*output) << "

Command-line Flags

"; // (*output) << "
" << google::CommandlineFlagsIntoString() << "
"; } // Registered to handle "/memz", and prints out memory allocation statistics. void mem_usage_handler(MemTracker* mem_tracker, const WebPageHandler::ArgumentMap& args, std::stringstream* output) { if (mem_tracker != NULL) { (*output) << "
"
                  << "Mem Limit: "
                  << PrettyPrinter::print(mem_tracker->limit(), TUnit::BYTES)
                  << std::endl
                  << "Mem Consumption: "
                  << PrettyPrinter::print(mem_tracker->consumption(), TUnit::BYTES)
                  << std::endl
                  << "
"; } else { (*output) << "
"
                  << "No process memory limit set."
                  << "
"; } (*output) << "
";
#ifdef ADDRESS_SANITIZER
    (*output) << "Memory tracking is not available with address sanitizer builds.";
#else
    char buf[2048];
    MallocExtension::instance()->GetStats(buf, 2048);
    // Replace new lines with 
for html std::string tmp(buf); boost::replace_all(tmp, "\n", "
"); (*output) << tmp << "
"; #endif } void add_default_path_handlers(WebPageHandler* web_page_handler, MemTracker* process_mem_tracker) { web_page_handler->register_page("/logs", logs_handler); web_page_handler->register_page("/varz", config_handler); web_page_handler->register_page( "/memz", boost::bind(&mem_usage_handler, process_mem_tracker, _1, _2)); } }