|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 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 | +#include "config.h" |
| 26 | +#include "WasmOpcodeCounter.h" |
| 27 | + |
| 28 | +#if ENABLE(WEBASSEMBLY) && ENABLE(B3_JIT) |
| 29 | +#include "WasmTypeDefinition.h" |
| 30 | +#include <wtf/Atomics.h> |
| 31 | +#include <wtf/DataLog.h> |
| 32 | +#include <wtf/NeverDestroyed.h> |
| 33 | +#include <wtf/Vector.h> |
| 34 | + |
| 35 | +#if PLATFORM(COCOA) |
| 36 | +#include <notify.h> |
| 37 | +#include <unistd.h> |
| 38 | +#endif |
| 39 | + |
| 40 | +namespace JSC { |
| 41 | +namespace Wasm { |
| 42 | + |
| 43 | +WasmOpcodeCounter& WasmOpcodeCounter::singleton() |
| 44 | +{ |
| 45 | + static LazyNeverDestroyed<WasmOpcodeCounter> counter; |
| 46 | + static std::once_flag onceKey; |
| 47 | + std::call_once(onceKey, [&] { |
| 48 | + counter.construct(); |
| 49 | + }); |
| 50 | + return counter; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +template<typename OpcodeType, typename OpcodeTypeDump, typename IsRegisteredOpcodeFunctor> |
| 55 | +void WasmOpcodeCounter::dump(Atomic<uint64_t>* counter, NumberOfRegisteredOpcodes numberOfRegisteredOpcode, CounterSize counterSize, const IsRegisteredOpcodeFunctor& isRegisteredOpcodeFunctor, const char* prefix, const char* suffix) |
| 56 | +{ |
| 57 | + struct Pair { |
| 58 | + OpcodeType opcode; |
| 59 | + uint64_t count; |
| 60 | + }; |
| 61 | + |
| 62 | + Vector<Pair> vector; |
| 63 | + uint64_t usedOpcode = 0; |
| 64 | + for (size_t i = 0; i < counterSize; i++) { |
| 65 | + if (!isRegisteredOpcodeFunctor((OpcodeType)i)) |
| 66 | + continue; |
| 67 | + |
| 68 | + uint64_t count = counter[i].loadFullyFenced(); |
| 69 | + if (count) |
| 70 | + usedOpcode++; |
| 71 | + vector.append({ (OpcodeType)i, count }); |
| 72 | + } |
| 73 | + |
| 74 | + std::sort(vector.begin(), vector.end(), [](Pair& a, Pair& b) { |
| 75 | + return b.count < a.count; |
| 76 | + }); |
| 77 | + |
| 78 | + int pid = 0; |
| 79 | +#if PLATFORM(COCOA) |
| 80 | + pid = getpid(); |
| 81 | +#endif |
| 82 | + float coverage = usedOpcode * 1.0 / numberOfRegisteredOpcode * 100; |
| 83 | + dataLogF("%s<%d> %s use coverage %.2f%%.\n", prefix, pid, suffix, coverage); |
| 84 | + for (Pair& pair : vector) |
| 85 | + dataLogLn(prefix, "<", pid, "> ", OpcodeTypeDump(pair.opcode), ": ", pair.count); |
| 86 | +} |
| 87 | + |
| 88 | +void WasmOpcodeCounter::dump() |
| 89 | +{ |
| 90 | + dump<ExtSIMDOpType, ExtSIMDOpTypeDump>(m_extendedSIMDOpcodeCounter, m_extendedSIMDOpcodeInfo.first, m_extendedSIMDOpcodeInfo.second, isRegisteredWasmExtendedSIMDOpcode, "<WASM.EXT.SIMD.OP.STAT>", "wasm extended SIMD opcode"); |
| 91 | + |
| 92 | + dump<ExtAtomicOpType, ExtAtomicOpTypeDump>(m_extendedAtomicOpcodeCounter, m_extendedAtomicOpcodeInfo.first, m_extendedAtomicOpcodeInfo.second, isRegisteredExtenedAtomicOpcode, "<WASM.EXT.ATOMIC.OP.STAT>", "wasm extended atomic opcode"); |
| 93 | + |
| 94 | + dump<GCOpType, GCOpTypeDump>(m_GCOpcodeCounter, m_GCOpcodeInfo.first, m_GCOpcodeInfo.second, isRegisteredGCOpcode, "<WASM.GC.OP.STAT>", "wasm GC opcode"); |
| 95 | + |
| 96 | + dump<OpType, OpTypeDump>(m_baseOpcodeCounter, m_baseOpcodeInfo.first, m_baseOpcodeInfo.second, isRegisteredBaseOpcode, "<WASM.BASE.OP.STAT>", "wasm base opcode"); |
| 97 | +} |
| 98 | + |
| 99 | +void WasmOpcodeCounter::registerDispatch() |
| 100 | +{ |
| 101 | +#if PLATFORM(COCOA) |
| 102 | + static std::once_flag registerFlag; |
| 103 | + std::call_once(registerFlag, [&]() { |
| 104 | + int pid = getpid(); |
| 105 | + const char* key = "com.apple.WebKit.wasm.op.stat"; |
| 106 | + dataLogF("<WASM.OP.STAT><%d> Registering callback for wasm opcode statistics.\n", pid); |
| 107 | + dataLogF("<WASM.OP.STAT><%d> Use `notifyutil -v -p %s` to dump statistics.\n", pid, key); |
| 108 | + |
| 109 | + int token; |
| 110 | + notify_register_dispatch(key, &token, dispatch_get_main_queue(), ^(int) { |
| 111 | + WasmOpcodeCounter::singleton().dump(); |
| 112 | + }); |
| 113 | + }); |
| 114 | +#endif |
| 115 | +} |
| 116 | + |
| 117 | +void WasmOpcodeCounter::increment(ExtSIMDOpType op) |
| 118 | +{ |
| 119 | + registerDispatch(); |
| 120 | + m_extendedSIMDOpcodeCounter[(uint8_t)op].exchangeAdd(1); |
| 121 | +} |
| 122 | + |
| 123 | +void WasmOpcodeCounter::increment(ExtAtomicOpType op) |
| 124 | +{ |
| 125 | + registerDispatch(); |
| 126 | + m_extendedAtomicOpcodeCounter[(uint8_t)op].exchangeAdd(1); |
| 127 | +} |
| 128 | + |
| 129 | +void WasmOpcodeCounter::increment(GCOpType op) |
| 130 | +{ |
| 131 | + registerDispatch(); |
| 132 | + m_GCOpcodeCounter[(uint8_t)op].exchangeAdd(1); |
| 133 | +} |
| 134 | + |
| 135 | +void WasmOpcodeCounter::increment(OpType op
109A2
) |
| 136 | +{ |
| 137 | + registerDispatch(); |
| 138 | + m_baseOpcodeCounter[(uint8_t)op].exchangeAdd(1); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace JSC |
| 142 | +} // namespace JSC::Wasm |
| 143 | + |
| 144 | +#endif // ENABLE(WEBASSEMBLY) && && ENABLE(B3_JIT) |
0 commit comments