|
| 1 | +#include "MethodCallProfiler.h" |
| 2 | +#include <algorithm> |
| 3 | +#include <mutex> |
| 4 | +#include <sstream> |
| 5 | +#include <unordered_map> |
| 6 | +#include <vector> |
| 7 | +#include "Helpers.h" |
| 8 | + |
| 9 | +using namespace v8; |
| 10 | + |
| 11 | +namespace tns { |
| 12 | + |
| 13 | +std::atomic<bool> MethodCallProfiler::enabled_{false}; |
| 14 | + |
| 15 | +struct MethodProfile { |
| 16 | + std::string className; |
| 17 | + std::string selectorName; |
| 18 | + std::string returnType; |
| 19 | + std::vector<std::string> argTypes; |
| 20 | + uint64_t count = 0; |
| 21 | +}; |
| 22 | + |
| 23 | +static std::mutex sProfileMutex; |
| 24 | +static std::unordered_map<std::string, MethodProfile> sProfiles; |
| 25 | + |
| 26 | +static const char* EncodingToTypeName(BinaryTypeEncodingType type) { |
| 27 | + switch (type) { |
| 28 | + case BinaryTypeEncodingType::VoidEncoding: |
| 29 | + return "void"; |
| 30 | + case BinaryTypeEncodingType::BoolEncoding: |
| 31 | + return "BOOL"; |
| 32 | + case BinaryTypeEncodingType::IdEncoding: |
| 33 | + case BinaryTypeEncodingType::InstanceTypeEncoding: |
| 34 | + case BinaryTypeEncodingType::InterfaceDeclarationReference: |
| 35 | + return "id"; |
| 36 | + case BinaryTypeEncodingType::SelectorEncoding: |
| 37 | + return "SEL"; |
| 38 | + case BinaryTypeEncodingType::ClassEncoding: |
| 39 | + return "Class"; |
| 40 | + case BinaryTypeEncodingType::IntEncoding: |
| 41 | + return "int"; |
| 42 | + case BinaryTypeEncodingType::UIntEncoding: |
| 43 | + return "uint"; |
| 44 | + case BinaryTypeEncodingType::LongEncoding: |
| 45 | + return "long"; |
| 46 | + case BinaryTypeEncodingType::ULongEncoding: |
| 47 | + return "ulong"; |
| 48 | + case BinaryTypeEncodingType::LongLongEncoding: |
| 49 | + return "longlong"; |
| 50 | + case BinaryTypeEncodingType::ULongLongEncoding: |
| 51 | + return "ulonglong"; |
| 52 | + case BinaryTypeEncodingType::FloatEncoding: |
| 53 | + return "float"; |
| 54 | + case BinaryTypeEncodingType::DoubleEncoding: |
| 55 | + return "double"; |
| 56 | + case BinaryTypeEncodingType::CharEncoding: |
| 57 | + return "char"; |
| 58 | + case BinaryTypeEncodingType::UCharEncoding: |
| 59 | + return "uchar"; |
| 60 | + case BinaryTypeEncodingType::ShortEncoding: |
| 61 | + return "short"; |
| 62 | + case BinaryTypeEncodingType::UShortEncoding: |
| 63 | + return "ushort"; |
| 64 | + default: |
| 65 | + return nullptr; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void MethodCallProfiler::Enable() { enabled_.store(true, std::memory_order_relaxed); } |
| 70 | + |
| 71 | +void MethodCallProfiler::Disable() { enabled_.store(false, std::memory_order_relaxed); } |
| 72 | + |
| 73 | +void MethodCallProfiler::Reset() { |
| 74 | + std::lock_guard<std::mutex> lock(sProfileMutex); |
| 75 | + sProfiles.clear(); |
| 76 | +} |
| 77 | + |
| 78 | +void MethodCallProfiler::RecordCall(const std::string& className, const MethodMeta* meta) { |
| 79 | + const char* sel = meta->selectorAsString(); |
| 80 | + std::string key = className + "\t" + sel; |
| 81 | + |
| 82 | + std::lock_guard<std::mutex> lock(sProfileMutex); |
| 83 | + auto it = sProfiles.find(key); |
| 84 | + if (it != sProfiles.end()) { |
| 85 | + it->second.count++; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + MethodProfile profile; |
| 90 | + profile.className = className; |
| 91 | + profile.selectorName = sel; |
| 92 | + profile.count = 1; |
| 93 | + |
| 94 | + const auto* encodings = meta->encodings(); |
| 95 | + const TypeEncoding* enc = encodings->first(); |
| 96 | + const char* retName = EncodingToTypeName(enc->type); |
| 97 | + profile.returnType = retName ? retName : "?"; |
| 98 | + |
| 99 | + int paramCount = encodings->count - 1; |
| 100 | + for (int i = 0; i < paramCount; i++) { |
| 101 | + enc = enc->next(); |
| 102 | + const char* argName = EncodingToTypeName(enc->type); |
| 103 | + profile.argTypes.push_back(argName ? argName : "?"); |
| 104 | + } |
| 105 | + |
| 106 | + sProfiles.emplace(key, std::move(profile)); |
| 107 | +} |
| 108 | + |
| 109 | +static std::vector<const MethodProfile*> GetSortedProfiles(int topN) { |
| 110 | + std::vector<const MethodProfile*> sorted; |
| 111 | + sorted.reserve(sProfiles.size()); |
| 112 | + for (const auto& pair : sProfiles) { |
| 113 | + sorted.push_back(&pair.second); |
| 114 | + } |
| 115 | + std::sort(sorted.begin(), sorted.end(), |
| 116 | + [](const MethodProfile* a, const MethodProfile* b) { return a->count > b->count; }); |
| 117 | + if (topN > 0 && (int)sorted.size() > topN) { |
| 118 | + sorted.resize(topN); |
| 119 | + } |
| 120 | + return sorted; |
| 121 | +} |
| 122 | + |
| 123 | +void MethodCallProfiler::JSStart(const FunctionCallbackInfo<Value>& info) { Enable(); } |
| 124 | + |
| 125 | +void MethodCallProfiler::JSStop(const FunctionCallbackInfo<Value>& info) { Disable(); } |
| 126 | + |
| 127 | +void MethodCallProfiler::JSReset(const FunctionCallbackInfo<Value>& info) { Reset(); } |
| 128 | + |
| 129 | +void MethodCallProfiler::JSReport(const FunctionCallbackInfo<Value>& info) { |
| 130 | + Isolate* isolate = info.GetIsolate(); |
| 131 | + int topN = 50; |
| 132 | + if (info.Length() > 0 && info[0]->IsNumber()) { |
| 133 | + topN = (int)tns::ToNumber(isolate, info[0]); |
| 134 | + } |
| 135 | + |
| 136 | + std::lock_guard<std::mutex> lock(sProfileMutex); |
| 137 | + auto sorted = GetSortedProfiles(topN); |
| 138 | + |
| 139 | + std::ostringstream out; |
| 140 | + out << "Top " << sorted.size() << " method calls:\n"; |
| 141 | + for (size_t i = 0; i < sorted.size(); i++) { |
| 142 | + const auto& p = *sorted[i]; |
| 143 | + out << " " << (i + 1) << ". " << p.className << " -[" << p.selectorName << "] " << p.returnType |
| 144 | + << "("; |
| 145 | + for (size_t j = 0; j < p.argTypes.size(); j++) { |
| 146 | + if (j > 0) out << ", "; |
| 147 | + out << p.argTypes[j]; |
| 148 | + } |
| 149 | + out << ") — " << p.count << " calls\n"; |
| 150 | + } |
| 151 | + |
| 152 | + info.GetReturnValue().Set(tns::ToV8String(isolate, out.str())); |
| 153 | +} |
| 154 | + |
| 155 | +void MethodCallProfiler::JSAOTConfig(const FunctionCallbackInfo<Value>& info) { |
| 156 | + Isolate* isolate = info.GetIsolate(); |
| 157 | + int topN = 50; |
| 158 | + if (info.Length() > 0 && info[0]->IsNumber()) { |
| 159 | + topN = (int)tns::ToNumber(isolate, info[0]); |
| 160 | + } |
| 161 | + |
| 162 | + std::lock_guard<std::mutex> lock(sProfileMutex); |
| 163 | + auto sorted = GetSortedProfiles(topN); |
| 164 | + |
| 165 | + std::ostringstream out; |
| 166 | + out << "[\n"; |
| 167 | + bool first = true; |
| 168 | + for (const auto* p : sorted) { |
| 169 | + bool hasUnknownType = (p->returnType == "?"); |
| 170 | + for (const auto& a : p->argTypes) { |
| 171 | + if (a == "?") hasUnknownType = true; |
| 172 | + } |
| 173 | + if (hasUnknownType) continue; |
| 174 | + |
| 175 | + if (!first) out << ",\n"; |
| 176 | + first = false; |
| 177 | + |
| 178 | + out << " { \"class\": \"" << p->className << "\", \"selector\": \"" << p->selectorName |
| 179 | + << "\", \"ret\": \"" << p->returnType << "\", \"args\": ["; |
| 180 | + for (size_t j = 0; j < p->argTypes.size(); j++) { |
| 181 | + if (j > 0) out << ", "; |
| 182 | + out << "\"" << p->argTypes[j] << "\""; |
| 183 | + } |
| 184 | + out << "] }"; |
| 185 | + } |
| 186 | + out << "\n]"; |
| 187 | + |
| 188 | + info.GetReturnValue().Set(tns::ToV8String(isolate, out.str())); |
| 189 | +} |
| 190 | + |
| 191 | +void MethodCallProfiler::RegisterJSAPI(Isolate* isolate, Local<ObjectTemplate> globalTemplate) { |
| 192 | + Local<ObjectTemplate> profiler = ObjectTemplate::New(isolate); |
| 193 | + profiler->Set(tns::ToV8String(isolate, "start"), FunctionTemplate::New(isolate, JSStart)); |
| 194 | + profiler->Set(tns::ToV8String(isolate, "stop"), FunctionTemplate::New(isolate, JSStop)); |
| 195 | + profiler->Set(tns::ToV8String(isolate, "reset"), FunctionTemplate::New(isolate, JSReset)); |
| 196 | + profiler->Set(tns::ToV8String(isolate, "report"), FunctionTemplate::New(isolate, JSReport)); |
| 197 | + profiler->Set(tns::ToV8String(isolate, "aotConfig"), FunctionTemplate::New(isolate, JSAOTConfig)); |
| 198 | + globalTemplate->Set(tns::ToV8String(isolate, "__native_call_profiler"), profiler); |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace tns |
0 commit comments