Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/js/SliceBudget.h
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 * vim: set ts=8 sts=4 et sw=4 tw=99:
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/TimeStamp.h"
11
12
#include <stdint.h>
13
14
#include "jstypes.h"
15
16
namespace js {
17
18
struct JS_PUBLIC_API(TimeBudget)
19
{
20
    int64_t budget;
21
22
468
    explicit TimeBudget(int64_t milliseconds) { budget = milliseconds; }
23
};
24
25
struct JS_PUBLIC_API(WorkBudget)
26
{
27
    int64_t budget;
28
29
468
    explicit WorkBudget(int64_t work) { budget = work; }
30
};
31
32
/*
33
 * This class records how much work has been done in a given collection slice,
34
 * so that we can return before pausing for too long. Some slices are allowed
35
 * to run for unlimited time, and others are bounded. To reduce the number of
36
 * gettimeofday calls, we only check the time every 1000 operations.
37
 */
38
class JS_PUBLIC_API(SliceBudget)
39
{
40
    static mozilla::TimeStamp unlimitedDeadline;
41
    static const intptr_t unlimitedStartCounter = INTPTR_MAX;
42
43
    bool checkOverBudget();
44
45
    SliceBudget();
46
47
  public:
48
    // Memory of the originally requested budget. If isUnlimited, neither of
49
    // these are in use. If deadline==0, then workBudget is valid. Otherwise
50
    // timeBudget is valid.
51
    TimeBudget timeBudget;
52
    WorkBudget workBudget;
53
54
    mozilla::TimeStamp deadline;
55
    intptr_t counter;
56
57
    static const intptr_t CounterReset = 1000;
58
59
    static const int64_t UnlimitedTimeBudget = -1;
60
    static const int64_t UnlimitedWorkBudget = -1;
61
62
    /* Use to create an unlimited budget. */
63
359
    static SliceBudget unlimited() { return SliceBudget(); }
64
65
    /* Instantiate as SliceBudget(TimeBudget(n)). */
66
    explicit SliceBudget(TimeBudget time);
67
68
    /* Instantiate as SliceBudget(WorkBudget(n)). */
69
    explicit SliceBudget(WorkBudget work);
70
71
377
    void makeUnlimited() {
72
377
        MOZ_ASSERT(unlimitedDeadline);
73
377
        deadline = unlimitedDeadline;
74
377
        counter = unlimitedStartCounter;
75
377
    }
76
77
294k
    void step(intptr_t amt = 1) {
78
294k
        counter -= amt;
79
294k
    }
80
81
309k
    bool isOverBudget() {
82
309k
        if (counter > 0) {
83
308k
            return false;
84
308k
        }
85
1.28k
        return checkOverBudget();
86
1.28k
    }
87
88
100
    bool isWorkBudget() const { return deadline.IsNull(); }
89
100
    bool isTimeBudget() const { return !deadline.IsNull() && !isUnlimited(); }
90
372
    bool isUnlimited() const { return deadline == unlimitedDeadline; }
91
92
    int describe(char* buffer, size_t maxlen) const;
93
94
    static void Init();
95
};
96
97
} // namespace js
98
99
#endif /* js_SliceBudget_h */