[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/tcmalloc_internals_request_job.h" |
| 6 | |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 7 | #include "base/allocator/allocator_extension.h" |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 8 | #include "content/common/child_process_messages.h" |
| 9 | #include "content/public/browser/browser_child_process_host_iterator.h" |
| 10 | #include "content/public/browser/browser_thread.h" |
| 11 | #include "content/public/browser/render_process_host.h" |
| 12 | #include "content/public/common/process_type.h" |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 13 | #include "net/base/net_errors.h" |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 14 | |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 15 | namespace content { |
| 16 | |
| 17 | // static |
| 18 | AboutTcmallocOutputs* AboutTcmallocOutputs::GetInstance() { |
| 19 | return Singleton<AboutTcmallocOutputs>::get(); |
| 20 | } |
| 21 | |
| 22 | AboutTcmallocOutputs::AboutTcmallocOutputs() {} |
| 23 | |
| 24 | AboutTcmallocOutputs::~AboutTcmallocOutputs() {} |
| 25 | |
| 26 | void AboutTcmallocOutputs::OnStatsForChildProcess( |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 27 | base::ProcessId pid, int process_type, |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 28 | const std::string& output) { |
| 29 | std::string header = GetProcessTypeNameInEnglish(process_type); |
| 30 | base::StringAppendF(&header, " PID %d", static_cast<int>(pid)); |
| 31 | SetOutput(header, output); |
| 32 | } |
| 33 | |
| 34 | void AboutTcmallocOutputs::SetOutput(const std::string& header, |
| 35 | const std::string& output) { |
| 36 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 37 | |
| 38 | outputs_[header] = output; |
| 39 | } |
| 40 | |
| 41 | void AboutTcmallocOutputs::DumpToHTMLTable(std::string* data) { |
| 42 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 | |
| 44 | data->append("<table width=\"100%\">\n"); |
| 45 | for (AboutTcmallocOutputsType::const_iterator oit = outputs_.begin(); |
| 46 | oit != outputs_.end(); |
| 47 | oit++) { |
| 48 | data->append("<tr><td bgcolor=\"yellow\">"); |
| 49 | data->append(oit->first); |
| 50 | data->append("</td></tr>\n"); |
| 51 | data->append("<tr><td><pre>\n"); |
| 52 | data->append(oit->second); |
| 53 | data->append("</pre></td></tr>\n"); |
| 54 | } |
| 55 | data->append("</table>\n"); |
| 56 | outputs_.clear(); |
| 57 | } |
| 58 | |
| 59 | TcmallocInternalsRequestJob::TcmallocInternalsRequestJob( |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 60 | net::URLRequest* request, net::NetworkDelegate* network_delegate) |
| 61 | : net::URLRequestSimpleJob(request, network_delegate) { |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #if defined(USE_TCMALLOC) |
| 65 | void RequestTcmallocStatsFromChildRenderProcesses() { |
| 66 | RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 67 | while (!it.IsAtEnd()) { |
| 68 | it.GetCurrentValue()->Send(new ChildProcessMsg_GetTcmallocStats); |
| 69 | it.Advance(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void AboutTcmalloc(std::string* data) { |
| 74 | data->append("<!DOCTYPE html>\n<html>\n<head>\n"); |
[email protected] | 92d32d88 | 2012-04-30 21:20:05 | [diff] [blame] | 75 | data->append( |
[email protected] | 4db7287 | 2012-11-07 15:34:50 | [diff] [blame] | 76 | "<meta http-equiv=\"Content-Security-Policy\" " |
[email protected] | 6ed944a | 2013-05-31 22:41:58 | [diff] [blame] | 77 | "content=\"object-src 'none'; script-src 'none'\">"); |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 78 | data->append("<title>tcmalloc stats</title>"); |
| 79 | data->append("</head><body>"); |
| 80 | |
| 81 | // Display any stats for which we sent off requests the last time. |
| 82 | data->append("<p>Stats as of last page load;"); |
| 83 | data->append("reload to get stats as of this page load.</p>\n"); |
| 84 | data->append("<table width=\"100%\">\n"); |
| 85 | |
| 86 | AboutTcmallocOutputs::GetInstance()->DumpToHTMLTable(data); |
| 87 | |
| 88 | data->append("</body></html>\n"); |
| 89 | |
| 90 | // Populate the collector with stats from the local browser process |
| 91 | // and send off requests to all the renderer processes. |
| 92 | char buffer[1024 * 32]; |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 93 | base::allocator::GetStats(buffer, sizeof(buffer)); |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 94 | std::string browser("Browser"); |
| 95 | AboutTcmallocOutputs::GetInstance()->SetOutput(browser, buffer); |
| 96 | |
| 97 | for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { |
| 98 | iter.Send(new ChildProcessMsg_GetTcmallocStats); |
| 99 | } |
| 100 | |
| 101 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 102 | &RequestTcmallocStatsFromChildRenderProcesses)); |
| 103 | } |
| 104 | #endif |
| 105 | |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 106 | int TcmallocInternalsRequestJob::GetData( |
| 107 | std::string* mime_type, |
| 108 | std::string* charset, |
| 109 | std::string* data, |
| 110 | const net::CompletionCallback& callback) const { |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 111 | mime_type->assign("text/html"); |
| 112 | charset->assign("UTF8"); |
| 113 | |
| 114 | data->clear(); |
| 115 | #if defined(USE_TCMALLOC) |
| 116 | AboutTcmalloc(data); |
| 117 | #endif |
[email protected] | 36ffc734 | 2012-07-23 17:15:10 | [diff] [blame] | 118 | return net::OK; |
[email protected] | 3d662c3 | 2012-04-25 00:05:17 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace content |