1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PROFILING_LOADER_H 17 #define PROFILING_LOADER_H 18 19 #include "jit/profiling_data.h" 20 #include "libprofile/aot_profiling_data.h" 21 22 namespace ark { 23 class PANDA_PUBLIC_API ProfilingLoader { 24 public: 25 Expected<PandaString, PandaString> LoadProfile(const PandaString &fileName); 26 GetAotProfilingData()27 pgo::AotProfilingData &GetAotProfilingData() 28 { 29 return aotProfilingData_; 30 } 31 32 template <typename ClassResolverT> CreateProfilingData(const pgo::AotProfilingData::AotMethodProfilingData & methodProfile,mem::InternalAllocatorPtr allocator,ClassResolverT && classResolver)33 ProfilingData *CreateProfilingData(const pgo::AotProfilingData::AotMethodProfilingData &methodProfile, 34 mem::InternalAllocatorPtr allocator, ClassResolverT &&classResolver) 35 { 36 auto inlineCaches = methodProfile.GetInlineCaches(); 37 auto branches = methodProfile.GetBranchData(); 38 auto throws = methodProfile.GetThrowData(); 39 return ProfilingData::Make(allocator, inlineCaches.size(), branches.size(), throws.size(), 40 [&](void *data, void *vcallsMem, void *branchesMem, void *throwsMem) { 41 return new (data) ProfilingData( 42 CreateInlineCaches(vcallsMem, inlineCaches, classResolver), 43 CreateBranchData(branchesMem, branches), CreateThrowData(throwsMem, throws)); 44 }); // CC-OFF(G.FMT.02) project code style 45 } 46 47 private: 48 template <typename ClassResolverT> CreateInlineCaches(void * inlineCachesMem,Span<const pgo::AotProfilingData::AotCallSiteInlineCache> aotInlineCaches,ClassResolverT && classResolver)49 Span<CallSiteInlineCache> CreateInlineCaches( 50 void *inlineCachesMem, Span<const pgo::AotProfilingData::AotCallSiteInlineCache> aotInlineCaches, 51 ClassResolverT &&classResolver) 52 { 53 auto inlineCaches = Span(reinterpret_cast<CallSiteInlineCache *>(inlineCachesMem), aotInlineCaches.size()); 54 for (size_t i = 0; i < aotInlineCaches.size(); i++) { 55 inlineCaches[i].Init(aotInlineCaches[i].pc); 56 for (auto &cls : aotInlineCaches[i].classes) { 57 if (auto *klass = classResolver(cls.first, cls.second)) { 58 inlineCaches[i].UpdateInlineCaches(klass); 59 } 60 } 61 } 62 63 return inlineCaches; 64 } 65 66 Span<BranchData> CreateBranchData(void *branchesMem, 67 Span<const pgo::AotProfilingData::AotBranchData> aotBranchData); 68 Span<ThrowData> CreateThrowData(void *throwsMem, Span<const pgo::AotProfilingData::AotThrowData> aotThrowData); 69 70 PandaVector<PandaString> pandaFiles_; 71 pgo::AotProfilingData aotProfilingData_; 72 }; 73 } // namespace ark 74 #endif // PROFILING_LOADER_H 75