Commit b211c4bd authored by slewis@apple.com's avatar slewis@apple.com

WebCore: <rdar://problem/8649617> Migrate memory tracking from Safari to WebKit

https://bugs.webkit.org/show_bug.cgi?id=50799 Add Memory Sampler to WebKit
        
Reviewed by Geoff Garen.

Add a sampler for printing off process memory statistics.
Export file mode.

* WebCore.exp.in:

WebKit2: <rdar://problem/8649617> Migrate memory tracking from Safari to WebKit
https://bugs.webkit.org/show_bug.cgi?id=50799 Add Memory Sampler to WebKit
        
Reviewed by Geoff Garen.

Add a sampler for printing off process memory statistics.
This tool can track:
                
        WebCore
           - FastMalloc allocations bytes              (in use or committed)
        JavaScriptCore
           - Garbage collector heap bytes              (in use or committed)
           - Stack bytes                               (committed only!)
           - JIT Code bytes                            (committed only!)
        Malloc zones
           - In use bytes for the following zones:
               * Default zone                          (in use or committed)
               * DispCon zone                          (in use or committed)
               * Purgable zone                         (in use or committed)
        Task Info
           - Resident size memory (RSIZE)
        
        Data collected is dumped to a temporary file.
        
Create a sandbox for a temporary file.
* Shared/SandboxExtension.h:
(WebKit::SandboxExtension::createHandleForTemporaryFile):
* Shared/mac/SandboxExtensionMac.mm:
(WebKit::SandboxExtension::createHandleForTemporaryFile):
        
Memory Sampler files
        
* Shared/WebMemorySampler.cpp: Added.
(WebKit::WebMemorySampler::shared):
(WebKit::WebMemorySampler::WebMemorySampler):
(WebKit::WebMemorySampler::start):
(WebKit::WebMemorySampler::initializeTimers):
(WebKit::WebMemorySampler::stop):
(WebKit::WebMemorySampler::isRunning):
(WebKit::WebMemorySampler::initializeTempLogFile):
(WebKit::WebMemorySampler::initializeSandboxedLogFile):
(WebKit::WebMemorySampler::writeHeaders):
(WebKit::WebMemorySampler::sampleTimerFired):
(WebKit::WebMemorySampler::stopTimerFired):
(WebKit::WebMemorySampler::appendCurrentMemoryUsageToFile):
* Shared/WebMemorySampler.h: Added.
* Shared/mac/WebMemorySampler.mac.mm: Added.
(WebKit::WebMemorySampler::sampleSystemMalloc):
(WebKit::WebMemorySampler::sampleProcessCommittedBytes):
(WebKit::WebMemorySampler::processName):
(WebKit::WebMemorySampler::sampleWebKit):
        
API to start and stop UIProcess and WebProcess sampling.
        
* UIProcess/API/C/WKContext.cpp:
(WKContextStartMemorySampler):
(WKContextStopMemorySampler):
* UIProcess/API/C/WKContext.h:
        
Send messages to WebProcess to start and stop memory sampling.
        
* UIProcess/WebContext.cpp:
(WebKit::WebContext::WebContext):
(WebKit::WebContext::processDidFinishLaunching):
(WebKit::WebContext::startMemorySampler):
(WebKit::WebContext::stopMemorySampler):
* UIProcess/WebContext.h:
* WebKit2.xcodeproj/project.pbxproj:
* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::startMemorySampler):
(WebKit::WebProcess::stopMemorySampler):
* WebProcess/WebProcess.h:
* WebProcess/WebProcess.messages.in:



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@75306 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent cf46007a
2011-01-07 Stephanie Lewis <slewis@apple.com>
Reviewed by Geoff Garen.
<rdar://problem/8649617> Migrate memory tracking from Safari to WebKit
https://bugs.webkit.org/show_bug.cgi?id=50799 Add Memory Sampler to WebKit
Add a sampler for printing off process memory statistics.
Export file mode.
* WebCore.exp.in:
2011-01-06 Ryosuke Niwa <rniwa@webkit.org>
Reviewed by Adam Barth.
......
......@@ -769,6 +769,7 @@ __ZN7WebCore8FormDataD1Ev
__ZN7WebCore8Gradient12addColorStopEfRKNS_5ColorE
__ZN7WebCore8GradientC1ERKNS_10FloatPointES3_
__ZN7WebCore8IntPointC1ERK8_NSPoint
__ZN7WebCore8openFileERKN3WTF6StringENS_12FileOpenModeE
__ZN7WebCore8PositionC1EN3WTF10PassRefPtrINS_4NodeEEEi
__ZN7WebCore8Settings14setJavaEnabledEb
__ZN7WebCore8Settings15setWebGLEnabledEb
......
2011-01-07 Stephanie Lewis <slewis@apple.com>
Reviewed by Geoff Garen.
<rdar://problem/8649617> Migrate memory tracking from Safari to WebKit
https://bugs.webkit.org/show_bug.cgi?id=50799 Add Memory Sampler to WebKit
Add a sampler for printing off process memory statistics.
This tool can track:
WebCore
- FastMalloc allocations bytes (in use or committed)
JavaScriptCore
- Garbage collector heap bytes (in use or committed)
- Stack bytes (committed only!)
- JIT Code bytes (committed only!)
Malloc zones
- In use bytes for the following zones:
* Default zone (in use or committed)
* DispCon zone (in use or committed)
* Purgable zone (in use or committed)
Task Info
- Resident size memory (RSIZE)
Data collected is dumped to a temporary file.
Create a sandbox for a temporary file.
* Shared/SandboxExtension.h:
(WebKit::SandboxExtension::createHandleForTemporaryFile):
* Shared/mac/SandboxExtensionMac.mm:
(WebKit::SandboxExtension::createHandleForTemporaryFile):
Memory Sampler files
* Shared/WebMemorySampler.cpp: Added.
(WebKit::WebMemorySampler::shared):
(WebKit::WebMemorySampler::WebMemorySampler):
(WebKit::WebMemorySampler::start):
(WebKit::WebMemorySampler::initializeTimers):
(WebKit::WebMemorySampler::stop):
(WebKit::WebMemorySampler::isRunning):
(WebKit::WebMemorySampler::initializeTempLogFile):
(WebKit::WebMemorySampler::initializeSandboxedLogFile):
(WebKit::WebMemorySampler::writeHeaders):
(WebKit::WebMemorySampler::sampleTimerFired):
(WebKit::WebMemorySampler::stopTimerFired):
(WebKit::WebMemorySampler::appendCurrentMemoryUsageToFile):
* Shared/WebMemorySampler.h: Added.
* Shared/mac/WebMemorySampler.mac.mm: Added.
(WebKit::WebMemorySampler::sampleSystemMalloc):
(WebKit::WebMemorySampler::sampleProcessCommittedBytes):
(WebKit::WebMemorySampler::processName):
(WebKit::WebMemorySampler::sampleWebKit):
API to start and stop UIProcess and WebProcess sampling.
* UIProcess/API/C/WKContext.cpp:
(WKContextStartMemorySampler):
(WKContextStopMemorySampler):
* UIProcess/API/C/WKContext.h:
Send messages to WebProcess to start and stop memory sampling.
* UIProcess/WebContext.cpp:
(WebKit::WebContext::WebContext):
(WebKit::WebContext::processDidFinishLaunching):
(WebKit::WebContext::startMemorySampler):
(WebKit::WebContext::stopMemorySampler):
* UIProcess/WebContext.h:
* WebKit2.xcodeproj/project.pbxproj:
* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::startMemorySampler):
(WebKit::WebProcess::stopMemorySampler):
* WebProcess/WebProcess.h:
* WebProcess/WebProcess.messages.in:
2011-01-07 Jessie Berlin <jberlin@apple.com>
Reviewed by Dan Bernstein.
......@@ -30,6 +30,7 @@
#include <wtf/Noncopyable.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/text/WTFString.h>
#if ENABLE(WEB_PROCESS_SANDBOX)
typedef struct __WKSandboxExtension* WKSandboxExtensionRef;
......@@ -69,6 +70,7 @@ public:
static PassRefPtr<SandboxExtension> create(const Handle&);
static void createHandle(const String& path, Type type, Handle&);
static String createHandleForTemporaryFile(const String& prefix, Type type, Handle&);
~SandboxExtension();
bool invalidate();
......@@ -90,6 +92,7 @@ inline void SandboxExtension::Handle::encode(CoreIPC::ArgumentEncoder*) const {
inline bool SandboxExtension::Handle::decode(CoreIPC::ArgumentDecoder*, Handle&) { return true; }
inline PassRefPtr<SandboxExtension> SandboxExtension::create(const Handle&) { return 0; }
inline void SandboxExtension::createHandle(const String& path, Type type, Handle&) { }
inline String SandboxExtension::createHandleForTemporaryFile(const String& prefix, Type type, Handle&) {return String();}
inline SandboxExtension::~SandboxExtension() { }
inline bool SandboxExtension::invalidate() { return true; }
inline bool SandboxExtension::consume() { return true; }
......
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#if ENABLE(MEMORY_SAMPLER)
#include "WebMemorySampler.h"
#include <wtf/text/CString.h>
using namespace WebCore;
namespace WebKit {
WebMemorySampler* WebMemorySampler::shared()
{
static WebMemorySampler* sharedMemorySampler;
if (!sharedMemorySampler)
sharedMemorySampler = new WebMemorySampler();
return sharedMemorySampler;
}
WebMemorySampler::WebMemorySampler()
: m_separator(String("\t"))
, m_sampleTimer(this, &WebMemorySampler::sampleTimerFired)
, m_stopTimer(this, &WebMemorySampler::stopTimerFired)
, m_isRunning(false)
, m_runningTime(0)
{
}
void WebMemorySampler::start(const double interval)
{
if (m_isRunning)
return;
initializeTempLogFile();
initializeTimers(interval);
}
void WebMemorySampler::start(const SandboxExtension::Handle& sampleLogFileHandle, const String& sampleLogFilePath, const double interval)
{
if (m_isRunning)
return;
// If we are on a system without SandboxExtension the handle and filename will be empty
if (sampleLogFilePath.isEmpty()) {
start(interval);
return;
}
initializeSandboxedLogFile(sampleLogFileHandle, sampleLogFilePath);
initializeTimers(interval);
}
void WebMemorySampler::initializeTimers(double interval)
{
m_sampleTimer.startRepeating(1);
printf("Started memory sampler for process %s", processName().utf8().data());
if (interval > 0) {
m_stopTimer.startOneShot(interval);
printf(" for a interval of %g seconds", interval);
}
printf("; Sampler log file stored at: %s\n", m_sampleLogFilePath.utf8().data());
m_runningTime = interval;
m_isRunning = true;
}
void WebMemorySampler::stop()
{
if (!m_isRunning)
return;
m_sampleTimer.stop();
m_sampleLogFile = 0;
printf("Stopped memory sampler for process %s\n", processName().utf8().data());
// Flush stdout buffer so python script can be guaranteed to read up to this point.
fflush(stdout);
m_isRunning = false;
if(m_stopTimer.isActive())
m_stopTimer.stop();
if (m_sampleLogSandboxExtension) {
m_sampleLogSandboxExtension->invalidate();
m_sampleLogSandboxExtension = 0;
}
}
bool WebMemorySampler::isRunning() const
{
return m_isRunning;
}
void WebMemorySampler::initializeTempLogFile()
{
m_sampleLogFilePath = String((openTemporaryFile(processName().utf8().data(), m_sampleLogFile)).data());
writeHeaders();
}
void WebMemorySampler::initializeSandboxedLogFile(const SandboxExtension::Handle& sampleLogSandboxHandle, const String& sampleLogFilePath)
{
m_sampleLogSandboxExtension = SandboxExtension::create(sampleLogSandboxHandle);
if (m_sampleLogSandboxExtension)
m_sampleLogSandboxExtension->consume();
m_sampleLogFilePath = sampleLogFilePath;
m_sampleLogFile = openFile(m_sampleLogFilePath, OpenForWrite);
writeHeaders();
}
void WebMemorySampler::writeHeaders()
{
String processDetails = String("Process: ");
processDetails.append(processName());
processDetails.append(String("\n"));
writeToFile(m_sampleLogFile, processDetails.utf8().data(), processDetails.utf8().length());
String header;
WebMemoryStatistics stats = sampleWebKit();
if (!stats.keys.isEmpty()) {
for (size_t i = 0; i < stats.keys.size(); ++i) {
header.append(m_separator);
header.append(stats.keys[i].utf8().data());
}
}
header.append(String("\n"));
writeToFile(m_sampleLogFile, header.utf8().data(), header.utf8().length());
}
void WebMemorySampler::sampleTimerFired(Timer<WebMemorySampler>*)
{
appendCurrentMemoryUsageToFile(m_sampleLogFile);
}
void WebMemorySampler::stopTimerFired(Timer<WebMemorySampler>*)
{
if (!m_isRunning)
return;
printf("%g seconds elapsed. Stopping memory sampler...\n", m_runningTime);
stop();
}
void WebMemorySampler::appendCurrentMemoryUsageToFile(PlatformFileHandle& file)
{
// Collect statistics from allocators and get RSIZE metric
String statString;
WebMemoryStatistics memoryStats = sampleWebKit();
if (!memoryStats.values.isEmpty()) {
statString.append(m_separator);
for (size_t i = 0; i < memoryStats.values.size(); ++i) {
statString.append(m_separator);
statString.append(String::format("%lu", memoryStats.values[i]));
}
}
statString.append(String("\n"));
writeToFile(m_sampleLogFile, statString.utf8().data(), statString.utf8().length());
}
}
#endif
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The MemorySampler class samples a number of internal and external memory
* metrics every second while running. Sample data is written to a log file.
* Sampling occurs over a duration specified when started. If duration is set
* to 0 (default), the memory sampler will run indefinitely until the stop
* function is called. MemorySampler allows the option of sampling "in use"
* memory (committed memory minus free list byte count) or committed memory for
* any allocator which keeps a free list. This includes FastMalloc and the
* JavaScriptCore heap at this time.
* The following memory metrics are recorded:
*
* WebCore
* - FastMalloc allocations bytes (in use or committed)
* JavaScriptCore
* - Garbage collector heap bytes (in use or committed)
* - Stack bytes (committed only!)
* - JIT Code bytes (committed only!)
* Malloc zones
* - In use bytes for the following zones:
* * Default zone (in use or committed)
* * DispCon zone (in use or committed)
* * Purgable zone (in use or committed)
* Task Info
* - Resident size memory (RSIZE)
*/
#ifndef WebMemorySampler_h
#define WebMemorySampler_h
#if ENABLE(MEMORY_SAMPLER)
#include "SandboxExtension.h"
#include <WebCore/Timer.h>
#include <WebCore/FileSystem.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefPtr.h>
#include <wtf/text/WTFString.h>
#include <wtf/Vector.h>
namespace WebKit {
struct SystemMallocStats;
struct WebMemoryStatistics
{
Vector<String> keys;
Vector<size_t> values;
};
class WebMemorySampler : public Noncopyable {
public:
static WebMemorySampler* shared();
void start(const double interval=0);
void start(const SandboxExtension::Handle&, const String&, const double interval=0);
void stop();
bool isRunning() const;
private:
WebMemorySampler();
~WebMemorySampler();
void initializeTempLogFile();
void initializeSandboxedLogFile(const SandboxExtension::Handle&, const String&);
void writeHeaders();
void initializeTimers(double);
void sampleTimerFired(WebCore::Timer<WebMemorySampler>*);
void stopTimerFired(WebCore::Timer<WebMemorySampler>*);
void appendCurrentMemoryUsageToFile(WebCore::PlatformFileHandle&);
SystemMallocStats sampleSystemMalloc() const;
size_t sampleProcessCommittedBytes() const;
WebMemoryStatistics sampleWebKit() const;
String processName() const;
WebCore::PlatformFileHandle m_sampleLogFile;
String m_sampleLogFilePath;
String m_separator;
WebCore::Timer<WebMemorySampler> m_sampleTimer;
WebCore::Timer<WebMemorySampler> m_stopTimer;
bool m_isRunning;
double m_runningTime;
RefPtr<SandboxExtension> m_sampleLogSandboxExtension;
};
}
#endif
#endif
......@@ -114,6 +114,30 @@ void SandboxExtension::createHandle(const String& path, Type type, Handle& handl
NSString *standardizedPath = [(NSString *)path stringByStandardizingPath];
handle.m_sandboxExtension = WKSandboxExtensionCreate([standardizedPath fileSystemRepresentation], wkSandboxExtensionType(type));
}
String SandboxExtension::createHandleForTemporaryFile(const String& prefix, Type type, Handle& handle)
{
ASSERT(!handle.m_sandboxExtension);
Vector<char> path(PATH_MAX);
if (!confstr(_CS_DARWIN_USER_TEMP_DIR, path.data(), path.size()))
return String();
// Shrink the vector.
path.shrink(strlen(path.data()));
ASSERT(path.last() == '/');
// Append the file name.
path.append(prefix.utf8().data(), prefix.length());
path.append('\0');
handle.m_sandboxExtension = WKSandboxExtensionCreate(fileSystemRepresentation(path.data()).data(), wkSandboxExtensionType(type));
if (!handle.m_sandboxExtension) {
return String();
}
return String(path.data());
}
SandboxExtension::SandboxExtension(const Handle& handle)
: m_sandboxExtension(handle.m_sandboxExtension)
......
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#if ENABLE(MEMORY_SAMPLER)
#include "WebMemorySampler.h"
#include <JavaScriptCore/MemoryStatistics.h>
#include <mach/mach.h>
#include <mach/task.h>
#include <mach/mach_types.h>
#include <malloc/malloc.h>
#include <runtime/JSLock.h>
#include <WebCore/JSDOMWindow.h>
#include <wtf/CurrentTime.h>
using namespace WebCore;
using namespace JSC;
using namespace WTF;
namespace WebKit {
struct SystemMallocStats {
malloc_statistics_t defaultMallocZoneStats;
malloc_statistics_t dispatchContinuationMallocZoneStats;
malloc_statistics_t purgeableMallocZoneStats;
};
SystemMallocStats WebMemorySampler::sampleSystemMalloc() const
{
static const char* defaultMallocZoneName = "DefaultMallocZone";
static const char* dispatchContinuationMallocZoneName = "DispatchContinuations";
static const char* purgeableMallocZoneName = "DefaultPurgeableMallocZone";
SystemMallocStats mallocStats;
vm_address_t* zones;
unsigned count;
// Zero out the structures in case a zone is missing
malloc_statistics_t stats;
stats.blocks_in_use = 0;
stats.size_in_use = 0;
stats.max_size_in_use = 0;
stats.size_allocated = 0;
mallocStats.defaultMallocZoneStats = stats;
mallocStats.dispatchContinuationMallocZoneStats = stats;
mallocStats.purgeableMallocZoneStats = stats;
malloc_get_all_zones(mach_task_self(), 0, &zones, &count);
for (unsigned i = 0; i < count; i++) {
if (const char* name = malloc_get_zone_name(reinterpret_cast<malloc_zone_t*>(zones[i]))) {
stats.blocks_in_use = 0;
stats.size_in_use = 0;
stats.max_size_in_use = 0;
stats.size_allocated = 0;
malloc_zone_statistics(reinterpret_cast<malloc_zone_t*>(zones[i]), &stats);
if (!strcmp(name, defaultMallocZoneName))
mallocStats.defaultMallocZoneStats = stats;
else if (!strcmp(name, dispatchContinuationMallocZoneName))
mallocStats.dispatchContinuationMallocZoneStats = stats;
else if (!strcmp(name, purgeableMallocZoneName))
mallocStats.purgeableMallocZoneStats = stats;
}
}
return mallocStats;
}
size_t WebMemorySampler::sampleProcessCommittedBytes() const
{
task_basic_info_64 taskInfo;
mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
task_info(mach_task_self(), TASK_BASIC_INFO_64, reinterpret_cast<task_info_t>(&taskInfo), &count);
return taskInfo.resident_size;
}
String WebMemorySampler::processName() const
{
NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
if (!appName)
appName = [[NSProcessInfo processInfo] processName];
return String(appName);
}
WebMemoryStatistics WebMemorySampler::sampleWebKit() const
{
size_t totalBytesInUse = 0, totalBytesCommitted = 0;
WebMemoryStatistics webKitMemoryStats;
FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
size_t fastMallocBytesInUse = fastMallocStatistics.committedVMBytes - fastMallocStatistics.freeListBytes;
size_t fastMallocBytesCommitted = fastMallocStatistics.committedVMBytes;
totalBytesInUse += fastMallocBytesInUse;
totalBytesCommitted += fastMallocBytesCommitted;
JSLock lock(SilenceAssertionsOnly);
Heap::Statistics heapMemoryStats = heapStatistics(JSDOMWindow::commonJSGlobalData());
size_t jscHeapBytesInUse = heapMemoryStats.size - heapMemoryStats.free;
size_t jscHeapBytesCommitted = heapMemoryStats.size;
totalBytesInUse += jscHeapBytesInUse;
totalBytesCommitted += jscHeapBytesCommitted;
GlobalMemoryStatistics globalMemoryStats = globalMemoryStatistics();
totalBytesInUse += globalMemoryStats.stackBytes + globalMemoryStats.JITBytes;
totalBytesCommitted += globalMemoryStats.stackBytes + globalMemoryStats.JITBytes;
SystemMallocStats systemStats = sampleSystemMalloc();
size_t defaultMallocZoneBytesInUse = systemStats.defaultMallocZoneStats.size_in_use;
size_t dispatchContinuationMallocZoneBytesInUse = systemStats.dispatchContinuationMallocZoneStats.size_in_use;
size_t purgeableMallocZoneBytesInUse = systemStats.purgeableMallocZoneStats.size_in_use;
size_t defaultMallocZoneBytesCommitted = systemStats.defaultMallocZoneStats.size_allocated;
size_t dispatchContinuationMallocZoneBytesCommitted = systemStats.dispatchContinuationMallocZoneStats.size_allocated;
size_t purgeableMallocZoneBytesCommitted = systemStats.purgeableMallocZoneStats.size_allocated;
totalBytesInUse += defaultMallocZoneBytesInUse + dispatchContinuationMallocZoneBytesInUse + purgeableMallocZoneBytesInUse;
totalBytesCommitted += defaultMallocZoneBytesCommitted + dispatchContinuationMallocZoneBytesCommitted + purgeableMallocZoneBytesCommitted;
size_t residentSize = sampleProcessCommittedBytes();
double now = currentTime();
webKitMemoryStats.keys.append(String("Timestamp"));
webKitMemoryStats.values.append(now);
webKitMemoryStats.keys.append(String("Total Bytes of Memory In Use"));
webKitMemoryStats.values.append(totalBytesInUse);
webKitMemoryStats.keys.append(String("Fast Malloc Zone Bytes"));
webKitMemoryStats.values.append(fastMallocBytesInUse);
webKitMemoryStats.keys.append(String("Default Malloc Zone Bytes"));
webKitMemoryStats.values.append(defaultMallocZoneBytesInUse);
webKitMemoryStats.keys.append(String("Dispatch Continuation Malloc Zone Bytes"));
webKitMemoryStats.values.append(dispatchContinuationMallocZoneBytesInUse);
webKitMemoryStats.keys.append(String("Purgeable Malloc Zone Bytes"));
webKitMemoryStats.values.append(purgeableMallocZoneBytesInUse);
webKitMemoryStats.keys.append(String("JavaScript Heap Bytes"));
webKitMemoryStats.values.append(jscHeapBytesInUse);
webKitMemoryStats.keys.append(String("Total Bytes of Committed Memory"));
webKitMemoryStats.values.append(totalBytesCommitted);
webKitMemoryStats.keys.append(String("Fast Malloc Zone Bytes"));
webKitMemoryStats.values.append(fastMallocBytesCommitted);
webKitMemoryStats.keys.append(String("Default Malloc Zone Bytes"));
webKitMemoryStats.values.append(defaultMallocZoneBytesCommitted);
webKitMemoryStats.keys.append(String("Dispatch Continuation Malloc Zone Bytes"));
webKitMemoryStats.values.append(dispatchContinuationMallocZoneBytesCommitted);
webKitMemoryStats.keys.append(String("Purgeable Malloc Zone Bytes"));
webKitMemoryStats.values.append(purgeableMallocZoneBytesCommitted);
webKitMemoryStats.keys.append(String("JavaScript Heap Bytes"));
webKitMemoryStats.values.append(jscHeapBytesCommitted);
webKitMemoryStats.keys.append(String("JavaScript Stack Bytes"));
webKitMemoryStats.values.append(globalMemoryStats.stackBytes);