]> git.sesse.net Git - nageru/blob - basic_stats.cpp
Reset audio resampler when FFmpeg inputs restart due to errors.
[nageru] / basic_stats.cpp
1 #include "basic_stats.h"
2 #include "metrics.h"
3
4 #include <assert.h>
5 #include <sys/resource.h>
6
7 using namespace std;
8 using namespace std::chrono;
9
10 bool uses_mlock = false;
11
12 BasicStats::BasicStats(bool verbose)
13         : verbose(verbose)
14 {
15         start = steady_clock::now();
16
17         metric_start_time_seconds = get_timestamp_for_metrics();
18         global_metrics.add("frames_output_total", &metric_frames_output_total);
19         global_metrics.add("frames_output_dropped", &metric_frames_output_dropped);
20         global_metrics.add("start_time_seconds", &metric_start_time_seconds, Metrics::TYPE_GAUGE);
21         global_metrics.add("memory_used_bytes", &metrics_memory_used_bytes);
22         global_metrics.add("memory_locked_limit_bytes", &metrics_memory_locked_limit_bytes);
23 }
24
25 void BasicStats::update(int frame_num, int stats_dropped_frames)
26 {
27         steady_clock::time_point now = steady_clock::now();
28         double elapsed = duration<double>(now - start).count();
29
30         metric_frames_output_total = frame_num;
31         metric_frames_output_dropped = stats_dropped_frames;
32
33         if (frame_num % 100 != 0) {
34                 return;
35         }
36
37         if (verbose) {
38                 printf("%d frames (%d dropped) in %.3f seconds = %.1f fps (%.1f ms/frame)",
39                         frame_num, stats_dropped_frames, elapsed, frame_num / elapsed,
40                         1e3 * elapsed / frame_num);
41         }
42
43         // Check our memory usage, to see if we are close to our mlockall()
44         // limit (if at all set).
45         rusage used;
46         if (getrusage(RUSAGE_SELF, &used) == -1) {
47                 perror("getrusage(RUSAGE_SELF)");
48                 assert(false);
49         }
50         metrics_memory_used_bytes = used.ru_maxrss * 1024;
51
52         if (uses_mlock) {
53                 rlimit limit;
54                 if (getrlimit(RLIMIT_MEMLOCK, &limit) == -1) {
55                         perror("getrlimit(RLIMIT_MEMLOCK)");
56                         assert(false);
57                 }
58                 metrics_memory_locked_limit_bytes = limit.rlim_cur;
59
60                 if (verbose) {
61                         if (limit.rlim_cur == 0) {
62                                 printf(", using %ld MB memory (locked)",
63                                                 long(used.ru_maxrss / 1024));
64                         } else {
65                                 printf(", using %ld / %ld MB lockable memory (%.1f%%)",
66                                                 long(used.ru_maxrss / 1024),
67                                                 long(limit.rlim_cur / 1048576),
68                                                 float(100.0 * (used.ru_maxrss * 1024.0) / limit.rlim_cur));
69                         }
70                 }
71         } else {
72                 metrics_memory_locked_limit_bytes = 0.0 / 0.0;
73                 if (verbose) {
74                         printf(", using %ld MB memory (not locked)",
75                                         long(used.ru_maxrss / 1024));
76                 }
77         }
78
79         if (verbose) {
80                 printf("\n");
81         }
82 }
83
84