1/*
2 * Copyright (C) 2016 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#include "config.h"
27#include "ICStats.h"
28
29namespace JSC {
30
31bool ICEvent::operator<(const ICEvent& other) const
32{
33 if (m_classInfo != other.m_classInfo) {
34 if (!m_classInfo)
35 return true;
36 if (!other.m_classInfo)
37 return false;
38 return strcmp(m_classInfo->className, other.m_classInfo->className) < 0;
39 }
40
41 if (m_propertyName != other.m_propertyName)
42 return codePointCompare(m_propertyName.string(), other.m_propertyName.string()) < 0;
43
44 return m_kind < other.m_kind;
45}
46
47void ICEvent::dump(PrintStream& out) const
48{
49 out.print(m_kind, "(", m_classInfo ? m_classInfo->className : "<null>", ", ", m_propertyName, ")");
50}
51
52void ICEvent::log() const
53{
54 ICStats::instance().add(*this);
55}
56
57Atomic<ICStats*> ICStats::s_instance;
58
59ICStats::ICStats()
60{
61 m_thread = Thread::create(
62 "JSC ICStats",
63 [this] () {
64 LockHolder locker(m_lock);
65 for (;;) {
66 m_condition.waitFor(
67 m_lock, Seconds(1), [this] () -> bool { return m_shouldStop; });
68 if (m_shouldStop)
69 break;
70
71 dataLog("ICStats:\n");
72 auto list = m_spectrum.buildList();
73 for (unsigned i = list.size(); i--;)
74 dataLog(" ", list[i].key, ": ", list[i].count, "\n");
75 }
76 });
77}
78
79ICStats::~ICStats()
80{
81 {
82 LockHolder locker(m_lock);
83 m_shouldStop = true;
84 m_condition.notifyAll();
85 }
86
87 m_thread->waitForCompletion();
88}
89
90void ICStats::add(const ICEvent& event)
91{
92 m_spectrum.add(event);
93}
94
95ICStats& ICStats::instance()
96{
97 for (;;) {
98 ICStats* result = s_instance.load();
99 if (result)
100 return *result;
101
102 ICStats* newStats = new ICStats();
103 if (s_instance.compareExchangeWeak(nullptr, newStats))
104 return *newStats;
105
106 delete newStats;
107 }
108}
109
110} // namespace JSC
111
112namespace WTF {
113
114using namespace JSC;
115
116void printInternal(PrintStream& out, ICEvent::Kind kind)
117{
118 switch (kind) {
119#define ICEVENT_KIND_DUMP(name) case ICEvent::name: out.print(#name); return;
120 FOR_EACH_ICEVENT_KIND(ICEVENT_KIND_DUMP);
121#undef ICEVENT_KIND_DUMP
122 }
123 RELEASE_ASSERT_NOT_REACHED();
124}
125
126} // namespace WTF
127
128
129