• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 18812329569

26 Oct 2025 03:26AM UTC coverage: 48.169% (-0.08%) from 48.247%
18812329569

Pull #2694

github

web-flow
Merge 0de7714b2 into 5b66d5fb8
Pull Request #2694: User callback debugging and MavlinkDirect improvements

12 of 79 new or added lines in 4 files covered. (15.19%)

11 existing lines in 7 files now uncovered.

17432 of 36189 relevant lines covered (48.17%)

470.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mavsdk/core/callback_tracker.cpp
1
#include "callback_tracker.h"
2
#include "log.h"
3
#include <algorithm>
4
#include <iomanip>
5
#include <vector>
6

7
namespace mavsdk {
8

NEW
9
CallbackTracker::CallbackTracker() : _last_stats_time(std::chrono::steady_clock::now()) {}
×
10

NEW
11
void CallbackTracker::record_queued(const std::string& filename, int linenumber)
×
12
{
NEW
13
    if (filename.empty()) {
×
NEW
14
        return;
×
15
    }
16

NEW
17
    _total_callback_count++;
×
18

NEW
19
    std::lock_guard<std::mutex> lock(_stats_mutex);
×
NEW
20
    std::string location = filename + ":" + std::to_string(linenumber);
×
NEW
21
    _location_stats[location].count++;
×
NEW
22
}
×
23

NEW
24
void CallbackTracker::record_executed(
×
25
    const std::string& filename, int linenumber, int64_t duration_us)
26
{
NEW
27
    if (filename.empty()) {
×
NEW
28
        return;
×
29
    }
30

NEW
31
    std::lock_guard<std::mutex> lock(_stats_mutex);
×
NEW
32
    std::string location = filename + ":" + std::to_string(linenumber);
×
NEW
33
    _location_stats[location].total_duration_us += duration_us;
×
NEW
34
}
×
35

NEW
36
void CallbackTracker::maybe_print_stats(size_t queue_size)
×
37
{
NEW
38
    auto now = std::chrono::steady_clock::now();
×
NEW
39
    auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - _last_stats_time).count();
×
40

NEW
41
    if (elapsed < STATS_INTERVAL_SECONDS) {
×
NEW
42
        return;
×
43
    }
44

45
    // Calculate callbacks per second
NEW
46
    int callbacks_per_sec = _total_callback_count.load() / elapsed;
×
47

NEW
48
    LogInfo() << "Callback stats: " << callbacks_per_sec
×
NEW
49
              << " callbacks/sec (avg), queue size: " << queue_size;
×
50

51
    // Get a snapshot of the stats and clear them
NEW
52
    std::map<std::string, LocationStats> stats_snapshot;
×
NEW
53
    int64_t total_duration_us = 0;
×
54
    {
NEW
55
        std::lock_guard<std::mutex> lock(_stats_mutex);
×
NEW
56
        stats_snapshot = _location_stats;
×
NEW
57
        _location_stats.clear();
×
58

59
        // Calculate total duration across all callbacks
NEW
60
        for (const auto& [location, stats] : stats_snapshot) {
×
NEW
61
            total_duration_us += stats.total_duration_us;
×
62
        }
NEW
63
    }
×
64

NEW
65
    if (!stats_snapshot.empty()) {
×
NEW
66
        LogInfo() << "Top callback sources (by time):";
×
67

68
        // Convert map to vector and sort by total duration
69
        std::vector<std::pair<std::string, LocationStats>> sorted_stats(
NEW
70
            stats_snapshot.begin(), stats_snapshot.end());
×
NEW
71
        std::sort(sorted_stats.begin(), sorted_stats.end(), [](const auto& a, const auto& b) {
×
NEW
72
            return a.second.total_duration_us > b.second.total_duration_us;
×
73
        });
74

75
        // Print top N sources with count, duration, and percentage
NEW
76
        int count = 0;
×
NEW
77
        for (const auto& [location, stats] : sorted_stats) {
×
NEW
78
            if (count++ >= TOP_N_SOURCES) {
×
NEW
79
                break;
×
80
            }
81

NEW
82
            double avg_duration_us = static_cast<double>(stats.total_duration_us) / stats.count;
×
NEW
83
            double percentage = 0.0;
×
NEW
84
            if (total_duration_us > 0) {
×
NEW
85
                percentage =
×
NEW
86
                    (static_cast<double>(stats.total_duration_us) * 100.0) / total_duration_us;
×
87
            }
88

NEW
89
            LogInfo() << "  " << location << ": " << stats.count << " calls, "
×
NEW
90
                      << stats.total_duration_us << " us total, "
×
NEW
91
                      << static_cast<int>(avg_duration_us) << " us avg, " << std::fixed
×
NEW
92
                      << std::setprecision(1) << percentage << "% of time";
×
93
        }
94

95
        // Print total time spent in callbacks
NEW
96
        LogInfo() << "Total time in callbacks: " << total_duration_us << " us ("
×
NEW
97
                  << (total_duration_us / 1000) << " ms) over " << elapsed << " seconds ("
×
NEW
98
                  << std::fixed << std::setprecision(1)
×
NEW
99
                  << ((static_cast<double>(total_duration_us) / 1000000.0 / elapsed) * 100.0)
×
NEW
100
                  << "% CPU)";
×
NEW
101
    }
×
102

103
    // Reset counters
NEW
104
    _total_callback_count = 0;
×
NEW
105
    _last_stats_time = now;
×
NEW
106
}
×
107

108
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc