15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/tcmalloc_internals_request_job.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/allocator/allocator_extension.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/child_process_messages.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_child_process_host_iterator.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/process_type.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AboutTcmallocOutputs* AboutTcmallocOutputs::GetInstance() {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Singleton<AboutTcmallocOutputs>::get();
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AboutTcmallocOutputs::AboutTcmallocOutputs() {}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AboutTcmallocOutputs::~AboutTcmallocOutputs() {}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AboutTcmallocOutputs::OnStatsForChildProcess(
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::ProcessId pid, int process_type,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& output) {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string header = GetProcessTypeNameInEnglish(process_type);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringAppendF(&header, " PID %d", static_cast<int>(pid));
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetOutput(header, output);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AboutTcmallocOutputs::SetOutput(const std::string& header,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const std::string& output) {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  outputs_[header] = output;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AboutTcmallocOutputs::DumpToHTMLTable(std::string* data) {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<table width=\"100%\">\n");
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (AboutTcmallocOutputsType::const_iterator oit = outputs_.begin();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       oit != outputs_.end();
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       oit++) {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append("<tr><td bgcolor=\"yellow\">");
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append(oit->first);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append("</td></tr>\n");
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append("<tr><td><pre>\n");
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append(oit->second);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    data->append("</pre></td></tr>\n");
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("</table>\n");
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  outputs_.clear();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TcmallocInternalsRequestJob::TcmallocInternalsRequestJob(
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::URLRequest* request, net::NetworkDelegate* network_delegate)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : net::URLRequestSimpleJob(request, network_delegate) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_TCMALLOC)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RequestTcmallocStatsFromChildRenderProcesses() {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (!it.IsAtEnd()) {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    it.GetCurrentValue()->Send(new ChildProcessMsg_GetTcmallocStats);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    it.Advance();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AboutTcmalloc(std::string* data) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<!DOCTYPE html>\n<html>\n<head>\n");
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append(
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "<meta http-equiv=\"Content-Security-Policy\" "
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "content=\"object-src 'none'; script-src 'none'\">");
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<title>tcmalloc stats</title>");
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("</head><body>");
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Display any stats for which we sent off requests the last time.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<p>Stats as of last page load;");
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("reload to get stats as of this page load.</p>\n");
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("<table width=\"100%\">\n");
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AboutTcmallocOutputs::GetInstance()->DumpToHTMLTable(data);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->append("</body></html>\n");
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Populate the collector with stats from the local browser process
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and send off requests to all the renderer processes.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char buffer[1024 * 32];
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::allocator::GetStats(buffer, sizeof(buffer));
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string browser("Browser");
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AboutTcmallocOutputs::GetInstance()->SetOutput(browser, buffer);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    iter.Send(new ChildProcessMsg_GetTcmallocStats);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &RequestTcmallocStatsFromChildRenderProcesses));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TcmallocInternalsRequestJob::GetData(
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* mime_type,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* charset,
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* data,
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const net::CompletionCallback& callback) const {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mime_type->assign("text/html");
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  charset->assign("UTF8");
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data->clear();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_TCMALLOC)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AboutTcmalloc(data);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return net::OK;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content
122