1 /** 2 * Copyright (c) 2021-2024 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 #ifndef PANDA_RUNTIME_COMPILER_QUEUE_AGED_COUNTER_PRIORITY_H_ 16 #define PANDA_RUNTIME_COMPILER_QUEUE_AGED_COUNTER_PRIORITY_H_ 17 18 #include "runtime/compiler_queue_counter_priority.h" 19 #include "runtime/include/mem/panda_containers.h" 20 #include "runtime/include/method-inl.h" 21 22 namespace ark { 23 24 /** 25 * The aged counter priority queue works mostly as counter priority queue (see description), 26 * but it sorts the methods by its aged hotness counters. 27 * If the aged counter is less then some death value, it is considered as expired and is removed. 28 * Epoch duration and death counter is configured. 29 * This queue is thread unsafe (should be used under lock). 30 */ 31 class CompilerPriorityAgedCounterQueue : public CompilerPriorityCounterQueue { 32 public: CompilerPriorityAgedCounterQueue(mem::InternalAllocatorPtr allocator,uint64_t maxLength,uint64_t deathCounterValue,uint64_t epochDuration)33 explicit CompilerPriorityAgedCounterQueue(mem::InternalAllocatorPtr allocator, uint64_t maxLength, 34 uint64_t deathCounterValue, uint64_t epochDuration) 35 : CompilerPriorityCounterQueue(allocator, maxLength, 0 /* unused */) 36 { 37 deathCounterValue_ = deathCounterValue; 38 epochDuration_ = epochDuration; 39 if (epochDuration_ <= 0) { 40 LOG(FATAL, COMPILATION_QUEUE) << "Incorrect value of epoch duration: " << epochDuration_; 41 } 42 SetQueueName("priority aged counter compilation queue"); 43 } 44 45 protected: UpdateCounterAndCheck(CompilationQueueElement * element)46 bool UpdateCounterAndCheck(CompilationQueueElement *element) override 47 { 48 // Rounding 49 uint64_t currentTime = time::GetCurrentTimeInMillis(); 50 ASSERT(currentTime >= element->GetTimestamp()); 51 uint64_t duration = currentTime - element->GetTimestamp(); 52 uint64_t epochs = duration / epochDuration_; 53 int64_t agedCounter = element->GetContext().GetMethod()->GetHotnessCounter() / std::pow(2, epochs); 54 element->UpdateCounter(agedCounter); 55 // Hotness counter is 0 when method was put into queue and was decremented while waiting in queue. 56 // Thus it is negative and let's compare its absolute value with death_counter_value_ 57 return (static_cast<uint64_t>(std::abs(agedCounter)) < deathCounterValue_); 58 } 59 60 private: 61 uint64_t deathCounterValue_; 62 uint64_t epochDuration_; 63 }; 64 65 } // namespace ark 66 67 #endif // PANDA_RUNTIME_COMPILER_QUEUE_AGED_COUNTER_PRIORITY_H_ 68