| 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 <wtf/FileSystem.h> |
| 57 | #include <wtf/Noncopyable.h> |
| 58 | #include <wtf/RefPtr.h> |
| 59 | #include <wtf/Vector.h> |
| 60 | #include <wtf/text/WTFString.h> |
| 61 | |
| 62 | namespace WebKit { |
| 63 | |
| 64 | struct SystemMallocStats; |
| 65 | |
| 66 | struct WebMemoryStatistics { |
| 67 | Vector<String> keys; |
| 68 | Vector<size_t> values; |
| 69 | }; |
| 70 | |
| 71 | class WebMemorySampler { |
| 72 | WTF_MAKE_NONCOPYABLE(WebMemorySampler); |
| 73 | public: |
| 74 | static WebMemorySampler* singleton(); |
| 75 | void start(const double interval = 0); |
| 76 | void start(SandboxExtension::Handle&&, const String&, const double interval = 0); |
| 77 | void stop(); |
| 78 | bool isRunning() const; |
| 79 | |
| 80 | private: |
| 81 | WebMemorySampler(); |
| 82 | ~WebMemorySampler(); |
| 83 | |
| 84 | void initializeTempLogFile(); |
| 85 | void initializeSandboxedLogFile(SandboxExtension::Handle&&, const String&); |
| 86 | void (); |
| 87 | void initializeTimers(double); |
| 88 | void sampleTimerFired(); |
| 89 | void stopTimerFired(); |
| 90 | void appendCurrentMemoryUsageToFile(FileSystem::PlatformFileHandle&); |
| 91 | void sendMemoryPressureEvent(); |
| 92 | |
| 93 | SystemMallocStats sampleSystemMalloc() const; |
| 94 | size_t sampleProcessCommittedBytes() const; |
| 95 | WebMemoryStatistics sampleWebKit() const; |
| 96 | String processName() const; |
| 97 | |
| 98 | FileSystem::PlatformFileHandle m_sampleLogFile { FileSystem::invalidPlatformFileHandle }; |
| 99 | String m_sampleLogFilePath; |
| 100 | WebCore::Timer m_sampleTimer; |
| 101 | WebCore::Timer m_stopTimer; |
| 102 | bool m_isRunning; |
| 103 | double m_runningTime; |
| 104 | RefPtr<SandboxExtension> m_sampleLogSandboxExtension; |
| 105 | }; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | #endif |
| 110 | |
| 111 | #endif |
| 112 | |