1/* 2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 23 * 24 * The MemorySampler class samples a number of internal and external memory 25 * metrics every second while running. Sample data is written to a log file. 26 * Sampling occurs over a duration specified when started. If duration is set 27 * to 0 (default), the memory sampler will run indefinitely until the stop 28 * function is called. MemorySampler allows the option of sampling "in use" 29 * memory (committed memory minus free list byte count) or committed memory for 30 * any allocator which keeps a free list. This includes FastMalloc and the 31 * JavaScriptCore heap at this time. 32 * The following memory metrics are recorded: 33 * 34 * WebCore 35 * - FastMalloc allocations bytes (in use or committed) 36 * JavaScriptCore 37 * - Garbage collector heap bytes (in use or committed) 38 * - Stack bytes (committed only!) 39 * - JIT Code bytes (committed only!) 40 * Malloc zones 41 * - In use bytes for the following zones: 42 * * Default zone (in use or committed) 43 * * DispCon zone (in use or committed) 44 * * Purgable zone (in use or committed) 45 * Task Info 46 * - Resident size memory (RSIZE) 47 */ 48 49#ifndef WebMemorySampler_h 50#define WebMemorySampler_h 51 52#if ENABLE(MEMORY_SAMPLER) 53 54#include "SandboxExtension.h" 55#include <WebCore/Timer.h> 56#include <WebCore/FileSystem.h> 57#include <wtf/Noncopyable.h> 58#include <wtf/RefPtr.h> 59#include <wtf/text/WTFString.h> 60#include <wtf/Vector.h> 61 62namespace WebKit { 63 64struct SystemMallocStats; 65 66struct WebMemoryStatistics 67{ 68 Vector<String> keys; 69 Vector<size_t> values; 70}; 71 72class WebMemorySampler { 73 WTF_MAKE_NONCOPYABLE(WebMemorySampler); 74public: 75 static WebMemorySampler* shared(); 76 void start(const double interval=0); 77 void start(const SandboxExtension::Handle&, const String&, const double interval=0); 78 void stop(); 79 bool isRunning() const; 80 81private: 82 WebMemorySampler(); 83 ~WebMemorySampler(); 84 85 void initializeTempLogFile(); 86 void initializeSandboxedLogFile(const SandboxExtension::Handle&, const String&); 87 void writeHeaders(); 88 void initializeTimers(double); 89 void sampleTimerFired(WebCore::Timer<WebMemorySampler>*); 90 void stopTimerFired(WebCore::Timer<WebMemorySampler>*); 91 void appendCurrentMemoryUsageToFile(WebCore::PlatformFileHandle&); 92 93 SystemMallocStats sampleSystemMalloc() const; 94 size_t sampleProcessCommittedBytes() const; 95 WebMemoryStatistics sampleWebKit() const; 96 String processName() const; 97 98 WebCore::PlatformFileHandle m_sampleLogFile; 99 String m_sampleLogFilePath; 100 String m_separator; 101 WebCore::Timer<WebMemorySampler> m_sampleTimer; 102 WebCore::Timer<WebMemorySampler> m_stopTimer; 103 bool m_isRunning; 104 double m_runningTime; 105 RefPtr<SandboxExtension> m_sampleLogSandboxExtension; 106}; 107 108} 109 110#endif 111 112#endif 113