forked from virtualopensystems/vapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.c
56 lines (45 loc) · 1 KB
/
stat.c
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
/*
* stat.c
*/
#include <inttypes.h>
#include <stdio.h>
#include "stat.h"
#define STAT_PRINT_INTERVAL (3) // in ms
int init_stat(Stat* stat)
{
clock_gettime(CLOCK_MONOTONIC, &stat->start);
clock_gettime(CLOCK_MONOTONIC, &stat->stop);
stat->count = 0;
stat->diff = 0;
return 0;
}
int start_stat(Stat* stat)
{
clock_gettime(CLOCK_MONOTONIC, &stat->start);
return 0;
}
int stop_stat(Stat* stat)
{
clock_gettime(CLOCK_MONOTONIC, &stat->stop);
return 0;
}
int update_stat(Stat* stat, uint32_t count)
{
stat->count += count;
return 0;
}
int print_stat(Stat* stat)
{
struct timespec now;
uint64_t diff;
clock_gettime(CLOCK_MONOTONIC, &now);
diff = (now.tv_sec - stat->start.tv_sec)
+ (now.tv_nsec - stat->start.tv_nsec) / 1000000000;
if (diff > stat->diff) {
if (diff % STAT_PRINT_INTERVAL == 0) {
fprintf(stdout,"%10"PRId64"\r", stat->count / diff);fflush(stdout);
}
stat->diff = diff;
}
return 0;
}