|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the Initial |
| 3 | + * Developer's Public License Version 1.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl. |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed AS IS, |
| 9 | + * WITHOUT WARRANTY OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing rights |
| 11 | + * and limitations under the License. |
| 12 | + * |
| 13 | + * The Original Code was created by Adriano dos Santos Fernandes |
| 14 | + * for the Firebird Open Source RDBMS project. |
| 15 | + * |
| 16 | + * Copyright (c) 2023 Adriano dos Santos Fernandes <[email protected]> |
| 17 | + * and all contributors signed below. |
| 18 | + * |
| 19 | + * All Rights Reserved. |
| 20 | + * Contributor(s): ______________________________________. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef COMMON_PERFORMANCE_STOP_WATCH_H |
| 24 | +#define COMMON_PERFORMANCE_STOP_WATCH_H |
| 25 | + |
| 26 | +#include "../common/utils_proto.h" |
| 27 | + |
| 28 | + |
| 29 | +namespace Firebird |
| 30 | +{ |
| 31 | + |
| 32 | +// This class is a wrapper to fb_utils::query_performance_counter() with a way to let |
| 33 | +// the caller amortize timings expended in that function calls. |
| 34 | +// This class is not thread-safe. |
| 35 | +class PerformanceStopWatch |
| 36 | +{ |
| 37 | +public: |
| 38 | + PerformanceStopWatch() = default; |
| 39 | + PerformanceStopWatch(const PerformanceStopWatch&) = delete; |
| 40 | + PerformanceStopWatch& operator=(const PerformanceStopWatch&) = delete; |
| 41 | + |
| 42 | +public: |
| 43 | + SINT64 queryTicks() |
| 44 | + { |
| 45 | + const auto initialTicks = fb_utils::query_performance_counter(); |
| 46 | + |
| 47 | + if ((initialTicks - lastMeasuredTicks) * 1000 / fb_utils::query_performance_frequency() > |
| 48 | + OVERHEAD_CALC_FREQUENCY_MS) |
| 49 | + { |
| 50 | + const auto currentTicks = lastMeasuredTicks = fb_utils::query_performance_counter(); |
| 51 | + lastOverhead = currentTicks - initialTicks; |
| 52 | + accumulatedOverhead += lastOverhead + lastOverhead; |
| 53 | + return currentTicks; |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + accumulatedOverhead += lastOverhead; |
| 58 | + return initialTicks; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + SINT64 getAccumulatedOverhead() const |
| 63 | + { |
| 64 | + return accumulatedOverhead; |
| 65 | + } |
| 66 | + |
| 67 | + SINT64 getElapsedTicksAndAdjustOverhead(SINT64 currentTicks, SINT64 previousTicks, |
| 68 | + SINT64 previousAccumulatedOverhead) |
| 69 | + { |
| 70 | + const SINT64 overhead = MAX(accumulatedOverhead - previousAccumulatedOverhead, 0); |
| 71 | + const SINT64 elapsedTicks = currentTicks - previousTicks - overhead; |
| 72 | + |
| 73 | + if (elapsedTicks >= 0) |
| 74 | + return elapsedTicks; |
| 75 | + |
| 76 | + accumulatedOverhead += elapsedTicks; |
| 77 | + |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | +public: |
| 82 | + static constexpr SINT64 OVERHEAD_CALC_FREQUENCY_MS = 30 * 1000; |
| 83 | + |
| 84 | +private: |
| 85 | + SINT64 lastMeasuredTicks = 0; |
| 86 | + SINT64 lastOverhead = 0; |
| 87 | + SINT64 accumulatedOverhead = 0; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace Firebird |
| 91 | + |
| 92 | + |
| 93 | +#endif // COMMON_PERFORMANCE_STOP_WATCH_H |
0 commit comments