WebMemorySampler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef WebMemorySampler_h
  49. #define WebMemorySampler_h
  50. #if ENABLE(MEMORY_SAMPLER)
  51. #include "SandboxExtension.h"
  52. #include <WebCore/FileSystem.h>
  53. #include <WebCore/Timer.h>
  54. #include <wtf/Noncopyable.h>
  55. #include <wtf/RefPtr.h>
  56. #include <wtf/Vector.h>
  57. #include <wtf/text/WTFString.h>
  58. namespace WebKit {
  59. struct SystemMallocStats;
  60. struct WebMemoryStatistics {
  61. Vector<String> keys;
  62. Vector<size_t> values;
  63. };
  64. class WebMemorySampler {
  65. WTF_MAKE_NONCOPYABLE(WebMemorySampler);
  66. public:
  67. static WebMemorySampler* shared();
  68. void start(const double interval = 0);
  69. void start(const SandboxExtension::Handle&, const String&, const double interval = 0);
  70. void stop();
  71. bool isRunning() const;
  72. private:
  73. WebMemorySampler();
  74. ~WebMemorySampler();
  75. void initializeTempLogFile();
  76. void initializeSandboxedLogFile(const SandboxExtension::Handle&, const String&);
  77. void writeHeaders();
  78. void initializeTimers(double);
  79. void sampleTimerFired(WebCore::Timer<WebMemorySampler>*);
  80. void stopTimerFired(WebCore::Timer<WebMemorySampler>*);
  81. void appendCurrentMemoryUsageToFile(WebCore::PlatformFileHandle&);
  82. void sendMemoryPressureEvent();
  83. SystemMallocStats sampleSystemMalloc() const;
  84. size_t sampleProcessCommittedBytes() const;
  85. WebMemoryStatistics sampleWebKit() const;
  86. String processName() const;
  87. WebCore::PlatformFileHandle m_sampleLogFile;
  88. String m_sampleLogFilePath;
  89. WebCore::Timer<WebMemorySampler> m_sampleTimer;
  90. WebCore::Timer<WebMemorySampler> m_stopTimer;
  91. bool m_isRunning;
  92. double m_runningTime;
  93. RefPtr<SandboxExtension> m_sampleLogSandboxExtension;
  94. };
  95. }
  96. #endif
  97. #endif