Shift X2 status from legacy to archived
[khatus.git] / lib / cache.awk
1 # Naming convention:
2 # Variables:
3 # - global, builtin : ALLCAPS
4 # - global, public : Camel_Snake_Man_Bear_Pig
5 # - global, private : _snake_case_prefixed_underscore
6 # - local : snake_case
7 # Functions:
8 # - global, public : snake_case
9
10 BEGIN {
11 GC_Interval = GC_Interval ? GC_Interval : 3600 # seconds
12
13 _total_to_diff["khatus_sensor_net_addr_io", "bytes_read" ] = 1
14 _total_to_diff["khatus_sensor_net_addr_io", "bytes_written" ] = 1
15 _total_to_diff["khatus_sensor_disk_io" , "sectors_read" ] = 1
16 _total_to_diff["khatus_sensor_disk_io" , "sectors_written"] = 1
17
18 # (x * y) / z = x * w
19 # ==> w = y / z
20 # (x * bytes_per_sector) / bytes_per_mb = x * scaling_factor
21 # ==> scaling_factor = bytes_per_sector / bytes_per_mb
22 _bytes_per_sector = 512
23 _bytes_per_mb = 1024 * 1024
24 _scale["khatus_sensor_disk_io", "sectors_written"] = _bytes_per_sector / _bytes_per_mb
25 _scale["khatus_sensor_disk_io", "sectors_read" ] = _bytes_per_sector / _bytes_per_mb
26 # (x / y) = x * z
27 # ==> z = 1 / y
28 # x / bytes_per_mb = x * scaling_factor
29 # ==> scaling_factor = 1 / bytes_per_mb
30 _scale["khatus_sensor_net_addr_io", "bytes_written"] = 1 / _bytes_per_mb
31 _scale["khatus_sensor_net_addr_io", "bytes_read" ] = 1 / _bytes_per_mb
32 }
33
34 function cache_update( src, key, val, len_line, len_head, len_val, time) {
35 src = $2
36 key = $3
37 # Not just using $4 for val - because an unstructured value (like name of a
38 # song) might contain a character identical to FS
39 len_line = length($0)
40 len_head = length($1 FS $2 FS $3 FS)
41 len_val = len_line - len_head
42 val = substr($0, len_head + 1, len_val)
43 val = cache_maybe_total_to_diff(src, key, val)
44 val = cache_maybe_scale(src, key, val)
45 _cache[src, key] = val
46 time = cache_get_time()
47 _cache_mtime[src, key] = time
48 if (time % GC_Interval == 0) {
49 cache_gc()
50 }
51 }
52
53 function cache_get(result, src, key, ttl, time, age, is_expired) {
54 time = cache_get_time()
55 _cache_atime[src, key] = time
56 age = time - _cache_mtime[src, key]
57 result["is_expired"] = ttl && age > ttl # ttl = 0 => forever
58 result["value"] = _cache[src, key]
59 }
60
61 function cache_res_fmt_or_def(result, format, default) {
62 return result["is_expired"] ? default : sprintf(format, result["value"])
63 }
64
65 function cache_get_fmt_def(src, key, ttl, format, default, result) {
66 default = default ? default : "--"
67 cache_get(result, src, key, ttl)
68 return cache_res_fmt_or_def(result, format, default)
69 }
70
71 function cache_get_time( src, key, time) {
72 src = "khatus_sensor_datetime"
73 key = "epoch"
74 time = _cache[src, key]
75 _cache_atime[src, key] = time
76 return time
77 }
78
79 function cache_gc( src_and_key, parts, src, key, unused_for) {
80 for (src_and_key in _cache) {
81 split(src_and_key, parts, SUBSEP)
82 src = parts[1]
83 key = parts[2]
84 val = _cache[src, key]
85 unused_for = cache_get_time() - _cache_atime[src, key]
86 if (unused_for > GC_Interval) {
87 msg_out_info(\
88 "cache_gc",
89 sprintf(\
90 "Deleting unused data SRC=%s KEY=%s VAL=%s",
91 src, key, val\
92 ) \
93 )
94 delete _cache[src, key]
95 }
96 }
97 }
98
99 function cache_maybe_total_to_diff(src, key, val, key_parts) {
100 split(key, key_parts, Kfs)
101 if (_total_to_diff[src, key_parts[1]]) {
102 _prev[src, key] = _curr[src, key]
103 _curr[src, key] = val
104 return (_curr[src, key] - _prev[src, key])
105 } else {
106 return val
107 }
108 }
109
110 function cache_maybe_scale(src, key, val, key_parts) {
111 split(key, key_parts, Kfs)
112 if ((src SUBSEP key_parts[1]) in _scale) {
113 return val * _scale[src, key_parts[1]]
114 } else {
115 return val
116 }
117 }
This page took 0.077522 seconds and 4 git commands to generate.