1/*
2 * Copyright (C) 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 *
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 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "GCActivityCallback.h"
31
32#include "Heap.h"
33#include "JSLock.h"
34#include "JSObject.h"
35#include "VM.h"
36
37#include <wtf/RetainPtr.h>
38#include <wtf/WTFThreadData.h>
39
40#if PLATFORM(EFL)
41#include <wtf/MainThread.h>
42#endif
43
44namespace JSC {
45
46bool GCActivityCallback::s_shouldCreateGCTimer = true;
47
48#if USE(CF) || PLATFORM(EFL)
49
50const double timerSlop = 2.0; // Fudge factor to avoid performance cost of resetting timer.
51
52#if USE(CF)
53GCActivityCallback::GCActivityCallback(Heap* heap)
54    : GCActivityCallback(heap->vm(), CFRunLoopGetCurrent())
55{
56}
57
58GCActivityCallback::GCActivityCallback(Heap* heap, CFRunLoopRef runLoop)
59    : GCActivityCallback(heap->vm(), runLoop)
60{
61}
62#elif PLATFORM(EFL)
63GCActivityCallback::GCActivityCallback(Heap* heap)
64    : GCActivityCallback(heap->vm(), WTF::isMainThread())
65{
66}
67#endif
68
69void GCActivityCallback::doWork()
70{
71    Heap* heap = &m_vm->heap;
72    if (!isEnabled())
73        return;
74
75    JSLockHolder locker(m_vm);
76    if (heap->isDeferred()) {
77        scheduleTimer(0);
78        return;
79    }
80
81    doCollection();
82}
83
84#if USE(CF)
85void GCActivityCallback::scheduleTimer(double newDelay)
86{
87    if (newDelay * timerSlop > m_delay)
88        return;
89    double delta = m_delay - newDelay;
90    m_delay = newDelay;
91    CFRunLoopTimerSetNextFireDate(m_timer.get(), CFRunLoopTimerGetNextFireDate(m_timer.get()) - delta);
92}
93
94void GCActivityCallback::cancelTimer()
95{
96    m_delay = s_decade;
97    CFRunLoopTimerSetNextFireDate(m_timer.get(), CFAbsoluteTimeGetCurrent() + s_decade);
98}
99#elif PLATFORM(EFL)
100void GCActivityCallback::scheduleTimer(double newDelay)
101{
102    if (newDelay * timerSlop > m_delay)
103        return;
104
105    stop();
106    m_delay = newDelay;
107
108    ASSERT(!m_timer);
109    m_timer = add(newDelay, this);
110}
111
112void GCActivityCallback::cancelTimer()
113{
114    m_delay = s_hour;
115    stop();
116}
117#endif
118
119void GCActivityCallback::didAllocate(size_t bytes)
120{
121#if PLATFORM(EFL)
122    if (!isEnabled())
123        return;
124
125    ASSERT(WTF::isMainThread());
126#endif
127
128    // The first byte allocated in an allocation cycle will report 0 bytes to didAllocate.
129    // We pretend it's one byte so that we don't ignore this allocation entirely.
130    if (!bytes)
131        bytes = 1;
132    double bytesExpectedToReclaim = static_cast<double>(bytes) * deathRate();
133    double newDelay = lastGCLength() / gcTimeSlice(bytesExpectedToReclaim);
134    scheduleTimer(newDelay);
135}
136
137void GCActivityCallback::willCollect()
138{
139    cancelTimer();
140}
141
142void GCActivityCallback::cancel()
143{
144    cancelTimer();
145}
146
147#else
148
149GCActivityCallback::GCActivityCallback(Heap* heap)
150    : GCActivityCallback(heap->vm())
151{
152}
153
154void GCActivityCallback::doWork()
155{
156}
157
158void GCActivityCallback::didAllocate(size_t)
159{
160}
161
162void GCActivityCallback::willCollect()
163{
164}
165
166void GCActivityCallback::cancel()
167{
168}
169
170#endif
171
172}
173
174