1/*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Samsung Electronics
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebMemorySampler.h"
28
29#if ENABLE(MEMORY_SAMPLER)
30
31#include "NotImplemented.h"
32#include <JavaScriptCore/MemoryStatistics.h>
33#include <WebCore/JSDOMWindow.h>
34#include <runtime/JSLock.h>
35#include <runtime/Operations.h>
36#include <string.h>
37#include <sys/sysinfo.h>
38#include <wtf/CurrentTime.h>
39#include <wtf/text/WTFString.h>
40
41using namespace WebCore;
42using namespace JSC;
43using namespace WTF;
44
45namespace WebKit {
46
47struct ApplicationMemoryStats {
48    size_t totalProgramSize;
49    size_t residentSetSize;
50    size_t sharedSize;
51    size_t textSize;
52    size_t librarySize;
53    size_t dataStackSize;
54    size_t dirtyPageSize;
55};
56
57static const unsigned int maxBuffer = 128;
58static const unsigned int maxProcessPath = 35;
59
60static inline String nextToken(FILE* file)
61{
62    ASSERT(file);
63    if (!file)
64        return String();
65
66    char buffer[maxBuffer] = {0, };
67    unsigned int index = 0;
68    while (index < maxBuffer) {
69        char ch = fgetc(file);
70        if (ch == EOF || (isASCIISpace(ch) && index)) // Break on non-initial ASCII space.
71            break;
72        if (!isASCIISpace(ch)) {
73            buffer[index] = ch;
74            index++;
75        }
76    }
77
78    return String(buffer);
79}
80
81static inline void appendKeyValuePair(WebMemoryStatistics& stats, const String& key, size_t value)
82{
83    stats.keys.append(key);
84    stats.values.append(value);
85}
86
87static ApplicationMemoryStats sampleMemoryAllocatedForApplication()
88{
89    ApplicationMemoryStats applicationStats;
90    char processPath[maxProcessPath];
91    snprintf(processPath, maxProcessPath, "/proc/self/statm");
92    FILE* statmFileDescriptor = fopen(processPath, "r");
93    if (!statmFileDescriptor)
94        return applicationStats;
95
96    applicationStats.totalProgramSize = nextToken(statmFileDescriptor).toInt();
97    applicationStats.residentSetSize = nextToken(statmFileDescriptor).toInt();
98    applicationStats.sharedSize = nextToken(statmFileDescriptor).toInt();
99    applicationStats.textSize = nextToken(statmFileDescriptor).toInt();
100    applicationStats.librarySize = nextToken(statmFileDescriptor).toInt();
101    applicationStats.dataStackSize = nextToken(statmFileDescriptor).toInt();
102    applicationStats.dirtyPageSize = nextToken(statmFileDescriptor).toInt();
103
104    fclose(statmFileDescriptor);
105
106    return applicationStats;
107}
108
109String WebMemorySampler::processName() const
110{
111    char processPath[maxProcessPath];
112    snprintf(processPath, maxProcessPath, "/proc/self/status");
113    FILE* statusFileDescriptor = fopen(processPath, "r");
114    if (!statusFileDescriptor)
115        return String();
116
117    nextToken(statusFileDescriptor);
118    String processName = nextToken(statusFileDescriptor);
119
120    fclose(statusFileDescriptor);
121
122    return processName;
123}
124
125WebMemoryStatistics WebMemorySampler::sampleWebKit() const
126{
127    WebMemoryStatistics webKitMemoryStats;
128
129    double now = currentTime();
130
131    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Timestamp"), now);
132
133    ApplicationMemoryStats applicationStats = sampleMemoryAllocatedForApplication();
134
135    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Total Program Size"), applicationStats.totalProgramSize);
136    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("RSS"), applicationStats.residentSetSize);
137    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Shared"), applicationStats.sharedSize);
138    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Text"), applicationStats.textSize);
139    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Library"), applicationStats.librarySize);
140    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Data/Stack"), applicationStats.dataStackSize);
141    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Dirty"), applicationStats.dirtyPageSize);
142
143    size_t totalBytesInUse = 0;
144    size_t totalBytesCommitted = 0;
145
146#if ENABLE(GLOBAL_FASTMALLOC_NEW)
147    FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
148    size_t fastMallocBytesInUse = fastMallocStatistics.committedVMBytes - fastMallocStatistics.freeListBytes;
149    size_t fastMallocBytesCommitted = fastMallocStatistics.committedVMBytes;
150    totalBytesInUse += fastMallocBytesInUse;
151    totalBytesCommitted += fastMallocBytesCommitted;
152
153    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Fast Malloc In Use"), fastMallocBytesInUse);
154    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Fast Malloc Committed Memory"), fastMallocBytesCommitted);
155#endif
156
157    size_t jscHeapBytesInUse = JSDOMWindow::commonVM()->heap.size();
158    size_t jscHeapBytesCommitted = JSDOMWindow::commonVM()->heap.capacity();
159    totalBytesInUse += jscHeapBytesInUse;
160    totalBytesCommitted += jscHeapBytesCommitted;
161
162    GlobalMemoryStatistics globalMemoryStats = globalMemoryStatistics();
163    totalBytesInUse += globalMemoryStats.stackBytes + globalMemoryStats.JITBytes;
164    totalBytesCommitted += globalMemoryStats.stackBytes + globalMemoryStats.JITBytes;
165
166    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("JavaScript Heap In Use"), jscHeapBytesInUse);
167    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("JavaScript Heap Commited Memory"), jscHeapBytesCommitted);
168
169    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("JavaScript Stack Bytes"), globalMemoryStats.stackBytes);
170    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("JavaScript JIT Bytes"), globalMemoryStats.JITBytes);
171
172    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Total Memory In Use"), totalBytesInUse);
173    appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Total Committed Memory"), totalBytesCommitted);
174
175    struct sysinfo systemInfo;
176    if (!sysinfo(&systemInfo)) {
177        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("System Total Bytes"), systemInfo.totalram);
178        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Available Bytes"), systemInfo.freeram);
179        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Shared Bytes"), systemInfo.sharedram);
180        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Buffer Bytes"), systemInfo.bufferram);
181        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Total Swap Bytes"), systemInfo.totalswap);
182        appendKeyValuePair(webKitMemoryStats, ASCIILiteral("Available Swap Bytes"), systemInfo.freeswap);
183    }
184
185    return webKitMemoryStats;
186}
187
188void WebMemorySampler::sendMemoryPressureEvent()
189{
190    notImplemented();
191}
192
193}
194#endif
195