Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2026 by Thun Lu. All rights reserved. 3 : : * Author: Thun Lu <thun.lu@zohomail.cn> 4 : : * Repo: https://github.com/thun-res/vlink 5 : : * _ __ __ _ __ 6 : : * | | / / / / (_) ____ / /__ 7 : : * | | / / / / / / / __ \ / //_/ 8 : : * | |/ / / /___ / / / / / / / ,< 9 : : * |___/ /_____/ /_/ /_/ /_/ /_/|_| 10 : : * 11 : : * Licensed under the Apache License, Version 2.0 (the "License"); 12 : : * you may not use this file except in compliance with the License. 13 : : * You may obtain a copy of the License at 14 : : * 15 : : * http://www.apache.org/licenses/LICENSE-2.0 16 : : * 17 : : * Unless required by applicable law or agreed to in writing, software 18 : : * distributed under the License is distributed on an "AS IS" BASIS, 19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 : : * See the License for the specific language governing permissions and 21 : : * limitations under the License. 22 : : */ 23 : : 24 : : #include "./impl/calculate_sample.h" 25 : : 26 : : #include <limits> 27 : : #include <mutex> 28 : : 29 : : namespace vlink { 30 : : 31 : : // CalculateSample 32 : 242 : CalculateSample::CalculateSample() noexcept = default; 33 : : 34 : 242 : CalculateSample::~CalculateSample() noexcept = default; 35 : : 36 : 528 : void CalculateSample::update(uint64_t seq, uint64_t guid) noexcept { 37 : : static constexpr int64_t kMaxLossDiff = std::numeric_limits<uint32_t>::max(); 38 : : 39 : 528 : std::lock_guard lock(mtx_); 40 : : 41 : 531 : auto& number = number_map_[guid]; 42 : : 43 : 531 : auto lost = static_cast<int64_t>(seq - number.expected); 44 : : 45 [ + + + + : 531 : if VUNLIKELY (number.expected == 0 || seq < number.expected || lost > kMaxLossDiff || seq == 0) { + + - + + + - + + + ] 46 : 27 : number.first = seq; 47 : 27 : number.expected = seq + 1; 48 : 27 : number.lost = 0; 49 : : 50 : 27 : return; 51 : : } 52 : : 53 : 504 : number.lost += lost; 54 : 504 : number.expected = seq + 1; 55 [ + + ]: 531 : } 56 : : 57 : 68 : uint64_t CalculateSample::get_total() const noexcept { 58 : 68 : std::shared_lock lock(mtx_); 59 : : 60 : 68 : int64_t all_total = 0; 61 : : 62 [ + + ]: 137 : for (const auto& [guid, number] : number_map_) { 63 : 69 : all_total += (number.expected - number.first); 64 : : } 65 : : 66 [ - + ]: 68 : if VUNLIKELY (all_total < 0) { 67 : : return 0; // LCOV_EXCL_LINE GCOVR_EXCL_LINE 68 : : } 69 : : 70 : 68 : return all_total; 71 : 68 : } 72 : : 73 : 71 : uint64_t CalculateSample::get_lost() const noexcept { 74 : 71 : std::shared_lock lock(mtx_); 75 : : 76 : 71 : uint64_t all_lost = 0; 77 : : 78 [ + + ]: 145 : for (const auto& [guid, number] : number_map_) { 79 : 74 : all_lost += number.lost; 80 : : } 81 : : 82 : 142 : return all_lost; 83 : 71 : } 84 : : 85 : : } // namespace vlink