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.
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:
35 * - FastMalloc allocations bytes (in use or committed)
37 * - Garbage collector heap bytes (in use or committed)
38 * - Stack bytes (committed only!)
39 * - JIT Code bytes (committed only!)
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)
46 * - Resident size memory (RSIZE)
49 #ifndef WebMemorySampler_h
50 #define WebMemorySampler_h
52 #if ENABLE(MEMORY_SAMPLER)
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>
64 struct SystemMallocStats;
66 struct WebMemoryStatistics
69 Vector<size_t> values;
72 class WebMemorySampler {
73 WTF_MAKE_NONCOPYABLE(WebMemorySampler);
75 static WebMemorySampler* shared();
76 void start(const double interval=0);
77 void start(const SandboxExtension::Handle&, const String&, const double interval=0);
79 bool isRunning() const;
85 void initializeTempLogFile();
86 void initializeSandboxedLogFile(const SandboxExtension::Handle&, const String&);
88 void initializeTimers(double);
89 void sampleTimerFired(WebCore::Timer<WebMemorySampler>*);
90 void stopTimerFired(WebCore::Timer<WebMemorySampler>*);
91 void appendCurrentMemoryUsageToFile(WebCore::PlatformFileHandle&);
92 #if !(ENABLE_TIZEN_DAILY_UPVERSIONING)
93 void sendMemoryPressureEvent();
96 SystemMallocStats sampleSystemMalloc() const;
97 size_t sampleProcessCommittedBytes() const;
98 WebMemoryStatistics sampleWebKit() const;
99 String processName() const;
101 WebCore::PlatformFileHandle m_sampleLogFile;
102 String m_sampleLogFilePath;
104 WebCore::Timer<WebMemorySampler> m_sampleTimer;
105 WebCore::Timer<WebMemorySampler> m_stopTimer;
107 double m_runningTime;
108 RefPtr<SandboxExtension> m_sampleLogSandboxExtension;