| 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/. */ |
| 6 |
|
| 7 |
#ifndef js_SliceBudget_h |
| 8 |
#define js_SliceBudget_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/Atomics.h" |
| 12 |
#include "mozilla/TimeStamp.h" |
| 13 |
#include "mozilla/Variant.h" |
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#include "jstypes.h" |
| 18 |
|
| 19 |
namespace JS { |
| 20 |
|
| 21 |
struct JS_PUBLIC_API TimeBudget { |
| 22 |
const mozilla::TimeDuration budget; |
| 23 |
mozilla::TimeStamp deadline; // Calculated when SliceBudget is constructed. |
| 24 |
|
| 25 |
explicit TimeBudget(mozilla::TimeDuration duration) : budget(duration) {} |
| 26 |
explicit TimeBudget(int64_t milliseconds) |
| 27 |
: budget(mozilla::TimeDuration::FromMilliseconds(milliseconds)) {} |
| 28 |
|
| 29 |
void setDeadlineFromNow(); |
| 30 |
}; |
| 31 |
|
| 32 |
struct JS_PUBLIC_API WorkBudget { |
| 33 |
const int64_t budget; |
| 34 |
|
| 35 |
explicit WorkBudget(int64_t work) : budget(work) {} |
| 36 |
}; |
| 37 |
|
| 38 |
struct UnlimitedBudget {}; |
| 39 |
|
| 40 |
/* |
| 41 |
* This class describes a limit to the amount of work to be performed in a GC |
| 42 |
* slice, so that we can return to the mutator without pausing for too long. The |
| 43 |
* budget may be based on a deadline time or an amount of work to be performed, |
| 44 |
* or may be unlimited. |
| 45 |
* |
| 46 |
* To reduce the number of gettimeofday calls, we only check the time every 1000 |
| 47 |
* operations. |
| 48 |
*/ |
| 49 |
class JS_PUBLIC_API SliceBudget { |
| 50 |
public: |
| 51 |
using InterruptRequestFlag = mozilla::Atomic<bool, mozilla::Relaxed>; |
| 52 |
|
| 53 |
private: |
| 54 |
static constexpr int64_t UnlimitedCounter = INT64_MAX; |
| 55 |
|
| 56 |
// Most calls to isOverBudget will only check the counter value. Every N |
| 57 |
// steps, do a more "expensive" check -- look at the current time and/or |
| 58 |
// check the atomic interrupt flag. |
| 59 |
static constexpr int64_t StepsPerExpensiveCheck = 1000; |
| 60 |
|
| 61 |
// How many steps to count before checking the time and possibly the interrupt |
| 62 |
// flag. |
| 63 |
int64_t counter = StepsPerExpensiveCheck; |
| 64 |
|
| 65 |
// External flag to request the current slice to be interrupted |
| 66 |
// (and return isOverBudget() early.) Applies only to time-based budgets. |
| 67 |
InterruptRequestFlag* interruptRequested = nullptr; |
| 68 |
|
| 69 |
// Configuration |
| 70 |
mozilla::Variant<TimeBudget, WorkBudget, UnlimitedBudget> budget; |
| 71 |
|
| 72 |
// This SliceBudget is considered interrupted from the time isOverBudget() |
| 73 |
// finds the interrupt flag set. |
| 74 |
bool interrupted = false; |
| 75 |
|
| 76 |
public: |
| 77 |
// Whether this slice is running in (predicted to be) idle time. |
| 78 |
// Only used for recording in the profile. |
| 79 |
bool idle = false; |
| 80 |
|
| 81 |
// Whether this slice was given an extended budget, larger than |
| 82 |
// the predicted idle time. |
| 83 |
bool extended = false; |
| 84 |
|
| 85 |
private: |
| 86 |
explicit SliceBudget(InterruptRequestFlag* irqPtr) |
| 87 |
: counter(irqPtr ? StepsPerExpensiveCheck : UnlimitedCounter), |
| 88 |
interruptRequested(irqPtr), |
| 89 |
budget(UnlimitedBudget()) {} |
| 90 |
|
| 91 |
bool checkOverBudget(); |
| 92 |
|
| 93 |
public: |
| 94 |
// Use to create an unlimited budget. |
| 95 |
static SliceBudget unlimited() { return SliceBudget(nullptr); } |
| 96 |
|
| 97 |
// Instantiate as SliceBudget(TimeBudget(n)). |
| 98 |
explicit SliceBudget(TimeBudget time, |
| 99 |
InterruptRequestFlag* interrupt = nullptr); |
| 100 |
|
| 101 |
explicit SliceBudget(mozilla::TimeDuration duration, |
| 102 |
InterruptRequestFlag* interrupt = nullptr) |
| 103 |
: SliceBudget(TimeBudget(duration.ToMilliseconds()), interrupt) {} |
| 104 |
|
| 105 |
// Instantiate as SliceBudget(WorkBudget(n)). |
| 106 |
explicit SliceBudget(WorkBudget work); |
| 107 |
|
| 108 |
// Register having performed the given number of steps (counted against a |
| 109 |
// work budget, or progress towards the next time or callback check). |
| 110 |
void step(uint64_t steps = 1) { |
| 111 |
MOZ_ASSERT(steps > 0); |
| 112 |
counter -= steps; |
| 113 |
} |
| 114 |
|
| 115 |
// Force an "expensive" (time) check on the next call to isOverBudget. Useful |
| 116 |
// when switching between major phases of an operation like a cycle |
| 117 |
// collection. |
| 118 |
void forceCheck() { |
| 119 |
if (isTimeBudget()) { |
| 120 |
counter = 0; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
bool isOverBudget() { return counter <= 0 && checkOverBudget(); } |
| 125 |
|
| 126 |
bool isWorkBudget() const { return budget.is<WorkBudget>(); } |
| 127 |
bool isTimeBudget() const { return budget.is<TimeBudget>(); } |
| 128 |
bool isUnlimited() const { return budget.is<UnlimitedBudget>(); } |
| 129 |
|
| 130 |
mozilla::TimeDuration timeBudgetDuration() const { |
| 131 |
return budget.as<TimeBudget>().budget; |
| 132 |
} |
| 133 |
int64_t timeBudget() const { return timeBudgetDuration().ToMilliseconds(); } |
| 134 |
int64_t workBudget() const { return budget.as<WorkBudget>().budget; } |
| 135 |
|
| 136 |
mozilla::TimeStamp deadline() const { |
| 137 |
return budget.as<TimeBudget>().deadline; |
| 138 |
} |
| 139 |
|
| 140 |
int describe(char* buffer, size_t maxlen) const; |
| 141 |
}; |
| 142 |
|
| 143 |
} // namespace JS |
| 144 |
|
| 145 |
#endif /* js_SliceBudget_h */ |