summaryrefslogtreecommitdiff
path: root/ckt0.awk
blob: dac07eadd531901d6d60f1e4418b282c9b11f51f (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env awk -f


function log10(n) { return log(n)/log(10.0) }


function merge_meas(est_val, est_unc, meas_val, meas_unc) {

    # Kalman filtering compares variances,
    # convert uncertainties to square units
    est_unc = (0.5*est_unc)^2.0
    meas_unc = (0.5*meas_unc)^2.0

    K = (est_unc)/((est_unc) + (meas_unc))
    est_val = est_val + K*(meas_val - est_val)
    est_unc = (est_unc*meas_unc)/(est_unc + meas_unc)

    # convert est_unc back to original units
    est_unc = 2.0*sqrt(est_unc)

    return sprintf(OFMT OFS OFMT ORS, est_val, est_unc)

}


function sigfig(val, unc) {

    ordmag_val = log10(val)
    ordmag_unc = log10(unc)

    #sigfig_unc = sprintf("%.f", 6)
    sigfig_unc = sprintf("%.f", sqrt((ordmag_val - ordmag_unc)^2.0) + 1.0)
    sigfig_val = sprintf("%.f", (ordmag_val - ordmag_unc + sigfig_unc))

    ofmt_val = "%." sigfig_val "g"
    ofmt_unc = "%." sigfig_unc "g"

    return sprintf(ofmt_val OFS ofmt_unc ORS, val, unc)

}


function ck_boottime() {

    "uname" | getline uname
    close("uname")

    if (uname ~/OpenBSD/) {
        "sysctl kern.boottime" | getline t0_k
        close("sysctl kern.boottime")
        sub("^.*=", "", t0_k)
        split(t0_k, t0_k_arr)
        t0_k_arr[2] = (index("JanFebMarAprMayJunJulAugSepOctNovDec", t0_k_arr[2]) + 2)/3.0
        gsub(":", " ", t0_k_arr[4])
        t0_k = sprintf("%04d %02d %02d %s", t0_k_arr[5], t0_k_arr[2], t0_k_arr[3], t0_k_arr[4])
        t0_k = mktime(t0_k)
    }

    return t0_k

}


function ck_uptime(t0_est, t0_unc) {

    t_sys_meas[1] = systime()
    t_sys_meas[2] = 1.0

    "uptime" | getline t_up_cmd
    close("uptime")

    split(merge_meas(t_sys_meas[1], t_sys_meas[2], systime(), 1.0), t_sys_meas)

    # estimate (predict) uptime
    t_up_est[1] = t_sys_meas[1] - t0_est
    t_up_est[2] = t_sys_meas[2] + t0_unc

    # evaluate measured uptime
    t_up_meas[1] = 0.0
    t_up_meas[2] = systime()

    sub("^.*up ", "", t_up_cmd)
    sub(", load.*$", "", t_up_cmd)
    split(t_up_cmd, t_up_cmd_arr, ",")

    for (i in t_up_cmd_arr) {

        # TODO: add cases for days, months, years, etc.
        if (t_up_cmd_arr[i] ~ /day/) {
            split(t_up_cmd_arr[i], days)
            t_up_meas[1] += 86400.0*(days[1] + 0.0)
            (t_up_meas[2] > 86400.0) ? t_up_meas[2] = 86400.0 : t_up_meas[2] += 0.0
        }

        if (t_up_cmd_arr[i] ~ /hr/) {
            split(t_up_cmd_arr[i], hrs)
            t_up_meas[1] += 3600.0*(hrs[1] + 0.0)
            (t_up_meas[2] > 3600.0) ? t_up_meas[2] = 3600.0 : t_up_meas[2] += 0.0
        }

        if (t_up_cmd_arr[i] ~ /min/) {
            split(t_up_cmd_arr[i], mins)
            t_up_meas[1] += (60.0*(mins[1] + 0.0))
            (t_up_meas[2] > 60.0) ? t_up_meas[2] = 60.0 : t_up_meas[2] += 0.0
        }

        if (t_up_cmd_arr[i] ~ /:/) {
            split(t_up_cmd_arr[i], hrs_min, ":")
            t_up_meas[1] += 3600.0*(hrs_min[1] + 0.0)
            (t_up_meas[2] > 3600.0) ? t_up_meas[2] = 3600.0 : t_up_meas[2] += 0.0
            t_up_meas[1] += 60.0*(hrs_min[2] + 0.0)
            (t_up_meas[2] > 60.0) ? t_up_meas[2] = 60.0 : t_up_meas[2] += 0.0
        }

    }

    # merge predicted and measured uptimes
    split(merge_meas(t_up_est[1], t_up_est[2], t_up_meas[1], t_up_meas[2]), t_up_est)

    # evaluate measured boot time, t0
    t_boot_meas[1] = t_sys_meas[1] - t_up_meas[1]
    t_boot_meas[2] = t_sys_meas[2] + t_up_meas[2]

    # merge previous and updated boot time, t0
    split(merge_meas(t0_est, t0_unc, t_boot_meas[1], t_boot_meas[2]), t_boot_est)

    return sigfig(t_boot_est[1], t_boot_est[2])

}


BEGIN {

    OFMT="%.21g"
    pi = 4.0*atan2(1.0, 1.0)
    c0 = 299792458  # m/sec

    #print("_systime_", systime())
    #print("_boottime_", ck_boottime())

    # check ARGV for previous estimate
    if (ARGC > 0) {
        ARGV[1] ? t0_est[1] = ARGV[1] : t0_est[1] = ck_boottime()
        ARGV[2] ? t0_est[2] = ARGV[2] : t0_est[2] = systime()
    }
    #print("t0_est = ", t0_est[1])
    #print("t_boot_unc = ", t0_est[2])

    # check uptime, update estimate
    print(ck_uptime(t0_est[1], t0_est[2]))

}