1 | /* |
2 | * Copyright (C) 2012-2017 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 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "JSGlobalObject.h" |
29 | #include "Options.h" |
30 | #include <wtf/PrintStream.h> |
31 | |
32 | namespace JSC { |
33 | |
34 | class CodeBlock; |
35 | |
36 | enum CountingVariant { |
37 | CountingForBaseline, |
38 | CountingForUpperTiers |
39 | }; |
40 | |
41 | double applyMemoryUsageHeuristics(int32_t value, CodeBlock*); |
42 | int32_t applyMemoryUsageHeuristicsAndConvertToInt(int32_t value, CodeBlock*); |
43 | |
44 | inline int32_t formattedTotalExecutionCount(float value) |
45 | { |
46 | union { |
47 | int32_t i; |
48 | float f; |
49 | } u; |
50 | u.f = value; |
51 | return u.i; |
52 | } |
53 | |
54 | template<CountingVariant countingVariant> |
55 | class ExecutionCounter { |
56 | public: |
57 | ExecutionCounter(); |
58 | void forceSlowPathConcurrently(); // If you use this, checkIfThresholdCrossedAndSet() may still return false. |
59 | bool checkIfThresholdCrossedAndSet(CodeBlock*); |
60 | bool hasCrossedThreshold() const { return m_counter >= 0; } |
61 | void setNewThreshold(int32_t threshold, CodeBlock*); |
62 | void deferIndefinitely(); |
63 | double count() const { return static_cast<double>(m_totalCount) + m_counter; } |
64 | void dump(PrintStream&) const; |
65 | |
66 | void setNewThresholdForOSRExit(uint32_t activeThreshold, double memoryUsageAdjustedThreshold) |
67 | { |
68 | m_activeThreshold = activeThreshold; |
69 | m_counter = static_cast<int32_t>(-memoryUsageAdjustedThreshold); |
70 | m_totalCount = memoryUsageAdjustedThreshold; |
71 | } |
72 | |
73 | static int32_t maximumExecutionCountsBetweenCheckpoints() |
74 | { |
75 | switch (countingVariant) { |
76 | case CountingForBaseline: |
77 | return Options::maximumExecutionCountsBetweenCheckpointsForBaseline(); |
78 | case CountingForUpperTiers: |
79 | return Options::maximumExecutionCountsBetweenCheckpointsForUpperTiers(); |
80 | default: |
81 | RELEASE_ASSERT_NOT_REACHED(); |
82 | return 0; |
83 | } |
84 | } |
85 | |
86 | template<typename T> |
87 | static T clippedThreshold(JSGlobalObject* globalObject, T threshold) |
88 | { |
89 | int32_t maxThreshold; |
90 | if (Options::randomizeExecutionCountsBetweenCheckpoints()) |
91 | maxThreshold = globalObject->weakRandomInteger() % maximumExecutionCountsBetweenCheckpoints(); |
92 | else |
93 | maxThreshold = maximumExecutionCountsBetweenCheckpoints(); |
94 | if (threshold > maxThreshold) |
95 | threshold = maxThreshold; |
96 | return threshold; |
97 | } |
98 | |
99 | private: |
100 | bool hasCrossedThreshold(CodeBlock*) const; |
101 | bool setThreshold(CodeBlock*); |
102 | void reset(); |
103 | |
104 | public: |
105 | // NB. These are intentionally public because it will be modified from machine code. |
106 | |
107 | // This counter is incremented by the JIT or LLInt. It starts out negative and is |
108 | // counted up until it becomes non-negative. At the start of a counting period, |
109 | // the threshold we wish to reach is m_totalCount + m_counter, in the sense that |
110 | // we will add X to m_totalCount and subtract X from m_counter. |
111 | int32_t m_counter; |
112 | |
113 | // Counts the total number of executions we have seen plus the ones we've set a |
114 | // threshold for in m_counter. Because m_counter's threshold is negative, the |
115 | // total number of actual executions can always be computed as m_totalCount + |
116 | // m_counter. |
117 | float m_totalCount; |
118 | |
119 | // This is the threshold we were originally targeting, without any correction for |
120 | // the memory usage heuristics. |
121 | int32_t m_activeThreshold; |
122 | }; |
123 | |
124 | typedef ExecutionCounter<CountingForBaseline> BaselineExecutionCounter; |
125 | typedef ExecutionCounter<CountingForUpperTiers> UpperTierExecutionCounter; |
126 | |
127 | } // namespace JSC |
128 | |