1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_SliceBudget_h
8 #define js_SliceBudget_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/TimeStamp.h"
12 #include "mozilla/Variant.h"
20 struct JS_PUBLIC_API TimeBudget
{
22 mozilla::TimeStamp deadline
; // Calculated when SliceBudget is constructed.
24 explicit TimeBudget(int64_t milliseconds
) : budget(milliseconds
) {}
25 explicit TimeBudget(mozilla::TimeDuration duration
)
26 : TimeBudget(duration
.ToMilliseconds()) {}
29 struct JS_PUBLIC_API WorkBudget
{
32 explicit WorkBudget(int64_t work
) : budget(work
) {}
35 struct UnlimitedBudget
{};
38 * This class describes a limit to the amount of work to be performed in a GC
39 * slice, so that we can return to the mutator without pausing for too long. The
40 * budget may be based on a deadline time or an amount of work to be performed,
41 * or may be unlimited.
43 * To reduce the number of gettimeofday calls, we only check the time every 1000
46 class JS_PUBLIC_API SliceBudget
{
47 static const intptr_t UnlimitedCounter
= INTPTR_MAX
;
48 static const intptr_t DefaultStepsPerTimeCheck
= 1000;
50 mozilla::Variant
<TimeBudget
, WorkBudget
, UnlimitedBudget
> budget
;
51 int64_t stepsPerTimeCheck
= DefaultStepsPerTimeCheck
;
55 SliceBudget() : budget(UnlimitedBudget()), counter(UnlimitedCounter
) {}
57 bool checkOverBudget();
60 // Use to create an unlimited budget.
61 static SliceBudget
unlimited() { return SliceBudget(); }
63 // Instantiate as SliceBudget(TimeBudget(n)).
64 explicit SliceBudget(TimeBudget time
,
65 int64_t stepsPerTimeCheck
= DefaultStepsPerTimeCheck
);
67 // Instantiate as SliceBudget(WorkBudget(n)).
68 explicit SliceBudget(WorkBudget work
);
70 explicit SliceBudget(mozilla::TimeDuration time
)
71 : SliceBudget(TimeBudget(time
.ToMilliseconds())) {}
73 // Register having performed the given number of steps (counted against a
74 // work budget, or progress towards the next time or callback check).
75 void step(uint64_t steps
= 1) {
76 MOZ_ASSERT(steps
> 0);
80 // Do enough steps to force an "expensive" (time and/or callback) check on
81 // the next call to isOverBudget. Useful when switching between major phases
82 // of an operation like a cycle collection.
83 void stepAndForceCheck() {
89 bool isOverBudget() { return counter
<= 0 && checkOverBudget(); }
91 bool isWorkBudget() const { return budget
.is
<WorkBudget
>(); }
92 bool isTimeBudget() const { return budget
.is
<TimeBudget
>(); }
93 bool isUnlimited() const { return budget
.is
<UnlimitedBudget
>(); }
95 int64_t timeBudget() const { return budget
.as
<TimeBudget
>().budget
; }
96 int64_t workBudget() const { return budget
.as
<WorkBudget
>().budget
; }
98 int describe(char* buffer
, size_t maxlen
) const;
103 #endif /* js_SliceBudget_h */