1 /* 2 * Copyright (c) 2023 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 ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H 17 #define ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H 18 19 #include "ecmascript/js_tagged_value.h" 20 #include "ecmascript/js_hclass.h" 21 #include "ecmascript/mem/c_containers.h" 22 #include "ecmascript/pgo_profiler/types/pgo_profile_type.h" 23 #include "ecmascript/platform/mutex.h" 24 #include "utils/span.h" 25 26 namespace panda::ecmascript::pgo { 27 class PGOTypeGenerator { 28 public: GenerateProfileType(const JSThread * thread,JSTaggedType child,ProfileType rootType)29 static ProfileType GenerateProfileType(const JSThread *thread, JSTaggedType child, ProfileType rootType) 30 { 31 std::pair<bool, CString> result = JSHClass::DumpToString(thread, child); 32 uint32_t traceId = ComputeHashCode(result.second); 33 if (result.first) { 34 return ProfileType(rootType.GetAbcId(), traceId, ProfileType::Kind::InvalidId); 35 } 36 return ProfileType(rootType.GetAbcId(), traceId, rootType.GetKind()); 37 } 38 39 private: 40 static constexpr uint32_t INVALID_ID = 0; 41 ComputeHashCode(const CString & string)42 static uint32_t ComputeHashCode(const CString &string) 43 { 44 uint32_t hash = INVALID_ID; 45 Span<const char> sp(string.c_str(), string.size()); 46 for (auto c : sp) { 47 constexpr size_t SHIFT = 5; 48 hash = (hash << SHIFT) - hash + c; 49 } 50 return hash; 51 } 52 }; 53 } // namespace panda::ecmascript::pgo 54 #endif // ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H 55