fpizlo@apple.com | a3a6837e | 2016-04-07 02:02:47 +0000 | [diff] [blame] | 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 | |
| 29 | namespace JSC { |
| 30 | |
| 31 | bool 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 | |
| 47 | void ICEvent::dump(PrintStream& out) const |
| 48 | { |
| 49 | out.print(m_kind, "(", m_classInfo ? m_classInfo->className : "<null>", ", ", m_propertyName, ")"); |
| 50 | } |
| 51 | |
| 52 | void ICEvent::log() const |
| 53 | { |
| 54 | ICStats::instance().add(*this); |
| 55 | } |
| 56 | |
| 57 | Atomic<ICStats*> ICStats::s_instance; |
| 58 | |
| 59 | ICStats::ICStats() |
| 60 | { |
| 61 | m_thread = createThread( |
| 62 | "JSC ICStats", |
| 63 | [this] () { |
| 64 | LockHolder locker(m_lock); |
| 65 | for (;;) { |
| 66 | m_condition.waitForSeconds(m_lock, 1, [this] () -> bool { return m_shouldStop; }); |
| 67 | if (m_shouldStop) |
| 68 | break; |
| 69 | |
| 70 | dataLog("ICStats:\n"); |
| 71 | auto list = m_spectrum.buildList(); |
| 72 | for (unsigned i = list.size(); i--;) |
| 73 | dataLog(" ", list[i].key, ": ", list[i].count, "\n"); |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | ICStats::~ICStats() |
| 79 | { |
| 80 | { |
| 81 | LockHolder locker(m_lock); |
| 82 | m_shouldStop = true; |
| 83 | m_condition.notifyAll(); |
| 84 | } |
| 85 | |
| 86 | waitForThreadCompletion(m_thread); |
| 87 | } |
| 88 | |
| 89 | void ICStats::add(const ICEvent& event) |
| 90 | { |
| 91 | m_spectrum.add(event); |
| 92 | } |
| 93 | |
| 94 | ICStats& ICStats::instance() |
| 95 | { |
| 96 | for (;;) { |
| 97 | ICStats* result = s_instance.load(); |
| 98 | if (result) |
| 99 | return *result; |
| 100 | |
| 101 | ICStats* newStats = new ICStats(); |
| 102 | if (s_instance.compareExchangeWeak(nullptr, newStats)) |
| 103 | return *newStats; |
| 104 | |
| 105 | delete newStats; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } // namespace JSC |
| 110 | |
| 111 | namespace WTF { |
| 112 | |
| 113 | using namespace JSC; |
| 114 | |
| 115 | void printInternal(PrintStream& out, ICEvent::Kind kind) |
| 116 | { |
| 117 | switch (kind) { |
| 118 | #define ICEVENT_KIND_DUMP(name) case ICEvent::name: out.print(#name); return; |
| 119 | FOR_EACH_ICEVENT_KIND(ICEVENT_KIND_DUMP); |
| 120 | #undef ICEVENT_KIND_DUMP |
| 121 | } |
| 122 | RELEASE_ASSERT_NOT_REACHED(); |
| 123 | } |
| 124 | |
| 125 | } // namespace WTF |
| 126 | |
| 127 | |