-
Notifications
You must be signed in to change notification settings - Fork 18
/
time_util.h
62 lines (51 loc) · 1.53 KB
/
time_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Created by https://github.com/Consti10 on 31.03.24.
// https://github.com/OpenHD/FPVue_RK3566/tree/openhd
//
#ifndef FPVUE_TIME_UTIL_H
#define FPVUE_TIME_UTIL_H
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
/**
* @return milliseconds
*/
uint64_t get_time_ms() {
struct timespec spec;
if (clock_gettime(1, &spec) == -1) { /* 1 is CLOCK_MONOTONIC */
abort();
}
return spec.tv_sec * 1000 + spec.tv_nsec / 1e6;
}
void print_time_ms(const char* tag,uint64_t ms){
printf("%s %dms\n",tag,(int)ms);
}
struct TSAccumulator{
// In milliseconds
uint64_t min_ms;
uint64_t max_ms;
uint64_t accumulated_ms;
int count;
uint64_t last_print_ms;
};
void accumulate_and_print(const char *tag,uint64_t ms,struct TSAccumulator* tsAccumulator){
if(ms>tsAccumulator->max_ms){
tsAccumulator->max_ms=ms;
}
if(ms<tsAccumulator->min_ms){
tsAccumulator->min_ms=ms;
}
tsAccumulator->accumulated_ms+=ms;
tsAccumulator->count++;
uint64_t elapsed_since_last_print_ms=get_time_ms()- tsAccumulator->last_print_ms;
if(elapsed_since_last_print_ms>1000){
uint64_t average=tsAccumulator->accumulated_ms/tsAccumulator->count;
printf("%s min:%ld max:%ld avg:%ld (ms)\n",tag,tsAccumulator->min_ms,tsAccumulator->max_ms,average);
tsAccumulator->min_ms=UINT64_MAX;
tsAccumulator->max_ms=0;
tsAccumulator->count=0;
tsAccumulator->accumulated_ms=0;
tsAccumulator->last_print_ms=get_time_ms();
}
}
#endif //FPVUE_TIME_UTIL_H