This source file includes following definitions.
- getCachedHeapSize
- maybeUpdate
- update
- quantizeMemorySize
#include "config.h"
#include "core/timing/MemoryInfo.h"
#include <limits>
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "wtf/CurrentTime.h"
#include "wtf/MainThread.h"
#include "wtf/MathExtras.h"
namespace WebCore {
class HeapSizeCache {
WTF_MAKE_NONCOPYABLE(HeapSizeCache); WTF_MAKE_FAST_ALLOCATED;
public:
HeapSizeCache()
: m_lastUpdateTime(0)
{
}
void getCachedHeapSize(HeapInfo& info)
{
maybeUpdate();
info = m_info;
}
private:
void maybeUpdate()
{
const double TwentyMinutesInSeconds = 20 * 60;
double now = monotonicallyIncreasingTime();
if (now - m_lastUpdateTime >= TwentyMinutesInSeconds) {
update();
m_lastUpdateTime = now;
}
}
void update()
{
ScriptGCEvent::getHeapSize(m_info);
m_info.usedJSHeapSize = quantizeMemorySize(m_info.usedJSHeapSize);
m_info.totalJSHeapSize = quantizeMemorySize(m_info.totalJSHeapSize);
m_info.jsHeapSizeLimit = quantizeMemorySize(m_info.jsHeapSizeLimit);
}
double m_lastUpdateTime;
HeapInfo m_info;
};
size_t quantizeMemorySize(size_t size)
{
const int numberOfBuckets = 100;
DEFINE_STATIC_LOCAL(Vector<size_t>, bucketSizeList, ());
ASSERT(isMainThread());
if (bucketSizeList.isEmpty()) {
bucketSizeList.resize(numberOfBuckets);
float sizeOfNextBucket = 10000000.0;
const float largestBucketSize = 4000000000.0;
const float scalingFactor = exp(log(largestBucketSize / sizeOfNextBucket) / numberOfBuckets);
size_t nextPowerOfTen = static_cast<size_t>(pow(10, floor(log10(sizeOfNextBucket)) + 1) + 0.5);
size_t granularity = nextPowerOfTen / 1000;
for (int i = 0; i < numberOfBuckets; ++i) {
size_t currentBucketSize = static_cast<size_t>(sizeOfNextBucket);
bucketSizeList[i] = currentBucketSize - (currentBucketSize % granularity);
sizeOfNextBucket *= scalingFactor;
if (sizeOfNextBucket >= nextPowerOfTen) {
if (std::numeric_limits<size_t>::max() / 10 <= nextPowerOfTen) {
nextPowerOfTen = std::numeric_limits<size_t>::max();
} else {
nextPowerOfTen *= 10;
granularity *= 10;
}
}
if (i > 0 && bucketSizeList[i] < bucketSizeList[i - 1])
bucketSizeList[i] = std::numeric_limits<size_t>::max();
}
}
for (int i = 0; i < numberOfBuckets; ++i) {
if (size <= bucketSizeList[i])
return bucketSizeList[i];
}
return bucketSizeList[numberOfBuckets - 1];
}
MemoryInfo::MemoryInfo(LocalFrame* frame)
{
ScriptWrappable::init(this);
if (!frame || !frame->settings())
return;
if (frame->settings()->preciseMemoryInfoEnabled()) {
ScriptGCEvent::getHeapSize(m_info);
} else {
DEFINE_STATIC_LOCAL(HeapSizeCache, heapSizeCache, ());
heapSizeCache.getCachedHeapSize(m_info);
}
}
}