1 /*
2  * Copyright (c) 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 
16 #ifndef ECMASCRIPT_MEM_MEM_CONTROLLER_UTILS_H
17 #define ECMASCRIPT_MEM_MEM_CONTROLLER_UTILS_H
18 
19 #include <chrono>
20 #include <cmath>
21 #include <limits>
22 
23 #include "ecmascript/base/gc_ring_buffer.h"
24 
25 using BytesAndDuration = std::pair<uint64_t, double>;
MakeBytesAndDuration(uint64_t bytes, double duration)26 static inline BytesAndDuration MakeBytesAndDuration(uint64_t bytes, double duration)
27 {
28     return std::make_pair(bytes, duration);
29 }
30 
31 namespace panda::ecmascript {
32 class MemControllerUtils {
33 public:
GetSystemTimeInMs()34     static double GetSystemTimeInMs()
35     {
36         double currentTime =
37             std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
38         return currentTime * MILLISECOND_PER_SECOND;
39     }
40     static constexpr int LENGTH = 10;
41     static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer);
42     static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer,
43                                         const BytesAndDuration &initial, const double timeMs);
44 private:
45     static constexpr int MILLISECOND_PER_SECOND = 1000;
46 };
47 } // namespace panda::ecmascript
48 
49 #endif // ECMASCRIPT_MEM_MEM_CONTROLLER_UTILS_H