2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebMemorySampler.h"
29 #if ENABLE(MEMORY_SAMPLER)
31 #include <wtf/text/CString.h>
33 using namespace WebCore;
38 WebMemorySampler* WebMemorySampler::shared()
40 static WebMemorySampler* sharedMemorySampler;
41 if (!sharedMemorySampler)
42 sharedMemorySampler = new WebMemorySampler();
43 return sharedMemorySampler;
46 WebMemorySampler::WebMemorySampler()
47 : m_separator(String("\t"))
48 , m_sampleTimer(this, &WebMemorySampler::sampleTimerFired)
49 , m_stopTimer(this, &WebMemorySampler::stopTimerFired)
55 void WebMemorySampler::start(const double interval)
60 initializeTempLogFile();
61 initializeTimers(interval);
64 void WebMemorySampler::start(const SandboxExtension::Handle& sampleLogFileHandle, const String& sampleLogFilePath, const double interval)
69 // If we are on a system without SandboxExtension the handle and filename will be empty
70 if (sampleLogFilePath.isEmpty()) {
75 initializeSandboxedLogFile(sampleLogFileHandle, sampleLogFilePath);
76 initializeTimers(interval);
80 void WebMemorySampler::initializeTimers(double interval)
82 m_sampleTimer.startRepeating(1);
83 printf("Started memory sampler for process %s", processName().utf8().data());
85 m_stopTimer.startOneShot(interval);
86 printf(" for a interval of %g seconds", interval);
88 printf("; Sampler log file stored at: %s\n", m_sampleLogFilePath.utf8().data());
89 m_runningTime = interval;
93 void WebMemorySampler::stop()
99 printf("Stopped memory sampler for process %s\n", processName().utf8().data());
100 // Flush stdout buffer so python script can be guaranteed to read up to this point.
104 if(m_stopTimer.isActive())
107 if (m_sampleLogSandboxExtension) {
108 m_sampleLogSandboxExtension->invalidate();
109 m_sampleLogSandboxExtension = 0;
113 bool WebMemorySampler::isRunning() const
118 void WebMemorySampler::initializeTempLogFile()
120 m_sampleLogFilePath = String((openTemporaryFile(processName().utf8().data(), m_sampleLogFile)).data());
124 void WebMemorySampler::initializeSandboxedLogFile(const SandboxExtension::Handle& sampleLogSandboxHandle, const String& sampleLogFilePath)
126 m_sampleLogSandboxExtension = SandboxExtension::create(sampleLogSandboxHandle);
127 if (m_sampleLogSandboxExtension)
128 m_sampleLogSandboxExtension->consume();
129 m_sampleLogFilePath = sampleLogFilePath;
130 m_sampleLogFile = openFile(m_sampleLogFilePath, OpenForWrite);
134 void WebMemorySampler::writeHeaders()
136 String processDetails = String("Process: ");
137 processDetails.append(processName());
138 processDetails.append(String("\n"));
139 writeToFile(m_sampleLogFile, processDetails.utf8().data(), processDetails.utf8().length());
142 WebMemoryStatistics stats = sampleWebKit();
143 if (!stats.keys.isEmpty()) {
144 for (size_t i = 0; i < stats.keys.size(); ++i) {
145 header.append(m_separator);
146 header.append(stats.keys[i].utf8().data());
149 header.append(String("\n"));
150 writeToFile(m_sampleLogFile, header.utf8().data(), header.utf8().length());
153 void WebMemorySampler::sampleTimerFired(Timer<WebMemorySampler>*)
155 appendCurrentMemoryUsageToFile(m_sampleLogFile);
158 void WebMemorySampler::stopTimerFired(Timer<WebMemorySampler>*)
162 printf("%g seconds elapsed. Stopping memory sampler...\n", m_runningTime);
166 void WebMemorySampler::appendCurrentMemoryUsageToFile(PlatformFileHandle& file)
168 // Collect statistics from allocators and get RSIZE metric
170 WebMemoryStatistics memoryStats = sampleWebKit();
172 if (!memoryStats.values.isEmpty()) {
173 statString.append(m_separator);
174 for (size_t i = 0; i < memoryStats.values.size(); ++i) {
175 statString.append(m_separator);
176 statString.append(String::format("%lu", memoryStats.values[i]));
179 statString.append(String("\n"));
180 writeToFile(m_sampleLogFile, statString.utf8().data(), statString.utf8().length());