-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstat.c
More file actions
62 lines (51 loc) · 1.19 KB
/
stat.c
File metadata and controls
62 lines (51 loc) · 1.19 KB
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
/*
* stat.c
*
* Copyright (c) 2014 Virtual Open Systems Sarl.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#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;
}