Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / public / SliceBudget.h
blob44450204840a9998b70212818eb18404cb13639a
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/Atomics.h"
12 #include "mozilla/TimeStamp.h"
13 #include "mozilla/Variant.h"
15 #include <stdint.h>
17 #include "jstypes.h"
19 namespace js {
21 struct JS_PUBLIC_API TimeBudget {
22 const mozilla::TimeDuration budget;
23 mozilla::TimeStamp deadline; // Calculated when SliceBudget is constructed.
25 explicit TimeBudget(mozilla::TimeDuration duration) : budget(duration) {}
26 explicit TimeBudget(int64_t milliseconds)
27 : budget(mozilla::TimeDuration::FromMilliseconds(milliseconds)) {}
29 void setDeadlineFromNow();
32 struct JS_PUBLIC_API WorkBudget {
33 const int64_t budget;
35 explicit WorkBudget(int64_t work) : budget(work) {}
38 struct UnlimitedBudget {};
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.
46 * To reduce the number of gettimeofday calls, we only check the time every 1000
47 * operations.
49 class JS_PUBLIC_API SliceBudget {
50 public:
51 using InterruptRequestFlag = mozilla::Atomic<bool, mozilla::Relaxed>;
53 private:
54 static constexpr int64_t UnlimitedCounter = INT64_MAX;
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;
61 // How many steps to count before checking the time and possibly the interrupt
62 // flag.
63 int64_t counter = StepsPerExpensiveCheck;
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;
69 // Configuration
70 mozilla::Variant<TimeBudget, WorkBudget, UnlimitedBudget> budget;
72 // This SliceBudget is considered interrupted from the time isOverBudget()
73 // finds the interrupt flag set.
74 bool interrupted = false;
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;
81 // Whether this slice was given an extended budget, larger than
82 // the predicted idle time.
83 bool extended = false;
85 private:
86 explicit SliceBudget(InterruptRequestFlag* irqPtr)
87 : counter(irqPtr ? StepsPerExpensiveCheck : UnlimitedCounter),
88 interruptRequested(irqPtr),
89 budget(UnlimitedBudget()) {}
91 bool checkOverBudget();
93 public:
94 // Use to create an unlimited budget.
95 static SliceBudget unlimited() { return SliceBudget(nullptr); }
97 // Instantiate as SliceBudget(TimeBudget(n)).
98 explicit SliceBudget(TimeBudget time,
99 InterruptRequestFlag* interrupt = nullptr);
101 explicit SliceBudget(mozilla::TimeDuration duration,
102 InterruptRequestFlag* interrupt = nullptr)
103 : SliceBudget(TimeBudget(duration.ToMilliseconds()), interrupt) {}
105 // Instantiate as SliceBudget(WorkBudget(n)).
106 explicit SliceBudget(WorkBudget work);
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;
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;
124 bool isOverBudget() { return counter <= 0 && checkOverBudget(); }
126 bool isWorkBudget() const { return budget.is<WorkBudget>(); }
127 bool isTimeBudget() const { return budget.is<TimeBudget>(); }
128 bool isUnlimited() const { return budget.is<UnlimitedBudget>(); }
130 mozilla::TimeDuration timeBudgetDuration() const {
131 return budget.as<TimeBudget>().budget;
133 int64_t timeBudget() const { return timeBudgetDuration().ToMilliseconds(); }
134 int64_t workBudget() const { return budget.as<WorkBudget>().budget; }
136 mozilla::TimeStamp deadline() const {
137 return budget.as<TimeBudget>().deadline;
140 int describe(char* buffer, size_t maxlen) const;
143 } // namespace js
145 #endif /* js_SliceBudget_h */