File: timetest.c

package info (click to toggle)
android-platform-tools 29.0.6-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 365,224 kB
  • sloc: cpp: 1,049,638; java: 460,532; ansic: 375,452; asm: 301,257; xml: 134,509; python: 92,731; perl: 62,008; sh: 26,753; makefile: 3,210; javascript: 3,172; yacc: 1,403; lex: 455; awk: 368; ruby: 183; sql: 140
file content (113 lines) | stat: -rw-r--r-- 2,812 bytes parent folder | download | duplicates (8)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/limits.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>

#include <string.h>



long long nanotime(void)
{
    struct timespec t;
    
    if(clock_gettime(CLOCK_MONOTONIC, &t)) {
        fprintf(stderr,"clock failure\n");
        exit(1);
    }

    return (((long long) t.tv_sec) * 1000000000LL) +
        ((long long) t.tv_nsec);
}

static struct timespec ts_sub(struct timespec a, struct timespec b)
{
    struct timespec r;
    r.tv_sec = a.tv_sec - b.tv_sec;
    r.tv_nsec = a.tv_nsec - b.tv_nsec;
    if(r.tv_nsec < 0) {
        r.tv_sec--;
        r.tv_nsec += 1000 * 1000 * 1000;
    }
    if(r.tv_sec < 0 && r.tv_nsec > 0) {
        r.tv_sec++;
        r.tv_nsec -= 1000 * 1000 * 1000;
    }
    return r;
}

static struct timespec ts_min(struct timespec a, struct timespec b)
{
    if(a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec))
        return a;
    else
        return b;
}

static struct timespec ts_max(struct timespec a, struct timespec b)
{
    if(a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec))
        return b;
    else
        return a;
}

int main(int argc, char **argv)
{
    long long tnow, tlast;
    struct timespec t1, dtmin, dtminp, dtmax;
    int print_interval = 50000;
    int print_countdown = 1;
    int clock_id = CLOCK_MONOTONIC;
    dtmin.tv_sec = INT_MAX;
    dtmin.tv_nsec = 0;
    dtminp.tv_sec = INT_MAX;
    dtminp.tv_nsec = 0;
    dtmax.tv_sec = 0;
    dtmax.tv_nsec = 0;
    tlast = 0;

    if(argc == 2) {
        clock_id = atoi(argv[1]);
        printf("using clock %d\n", clock_id);
    }
    clock_gettime(clock_id, &t1);
    
    for(;;) {
        struct timespec t, dt;
        clock_gettime(clock_id, &t);
        dt = ts_sub(t, t1);
        t1 = t;
        dtmin = ts_min(dtmin, dt);
        if(dt.tv_sec > 0 || dt.tv_nsec > 0)
            dtminp = ts_min(dtminp, dt);
        if(print_countdown != print_interval)
            dtmax = ts_max(dtmax, dt);
        if(--print_countdown == 0) {
            fprintf(stderr,"%09ld.%09ld, dt %ld.%09ld, min %ld.%09ld, minp %ld.%09ld, max %ld.%09ld\n",
                    t.tv_sec, t.tv_nsec, dt.tv_sec, dt.tv_nsec,
                    dtmin.tv_sec, dtmin.tv_nsec, dtminp.tv_sec, dtminp.tv_nsec,
                    dtmax.tv_sec, dtmax.tv_nsec);
            print_countdown = print_interval;
        }
    }
    for(;;) {
        tnow = nanotime();
        if(tnow < tlast) {
#if 0
            fprintf(stderr,"time went backwards: %lld -> %lld\n",
                    tlast, tnow);
            exit(1);
#endif
            fprintf(stderr,"%lld ROLLBACK\n", tnow);
        } else {
            fprintf(stderr,"%lld\n", tnow);
        }
        tlast = tnow;
    }

    return 0;
}