Improve overview and experiment naming
[khatus.git] / x2 / src / awk / lib / cache.awk
CommitLineData
03c229bf
SK
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
10BEGIN {
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
8482fea6
SK
34function cache_update(node, module, key, val, time) {
35 # TODO: Use node value
36 val = cache_maybe_total_to_diff(module, key, val)
37 val = cache_maybe_scale(module, key, val)
38 _cache[module, key] = val
03c229bf 39 time = cache_get_time()
8482fea6 40 _cache_mtime[module, key] = time
03c229bf
SK
41 if (time % GC_Interval == 0) {
42 cache_gc()
43 }
44}
45
8482fea6 46function cache_get(result, module, key, ttl, time, age, is_expired) {
03c229bf 47 time = cache_get_time()
8482fea6
SK
48 _cache_atime[module, key] = time
49 age = time - _cache_mtime[module, key]
03c229bf 50 result["is_expired"] = ttl && age > ttl # ttl = 0 => forever
8482fea6 51 result["value"] = _cache[module, key]
03c229bf
SK
52}
53
54function cache_res_fmt_or_def(result, format, default) {
55 return result["is_expired"] ? default : sprintf(format, result["value"])
56}
57
8482fea6 58function cache_get_fmt_def(module, key, ttl, format, default, result) {
03c229bf 59 default = default ? default : "--"
8482fea6 60 cache_get(result, module, key, ttl)
03c229bf
SK
61 return cache_res_fmt_or_def(result, format, default)
62}
63
8482fea6
SK
64function cache_get_time( module, key, time) {
65 module = "khatus_sensor_datetime"
03c229bf 66 key = "epoch"
8482fea6
SK
67 time = _cache[module, key]
68 _cache_atime[module, key] = time
03c229bf
SK
69 return time
70}
71
8482fea6
SK
72function cache_gc( module_and_key, parts, module, key, unused_for) {
73 for (module_and_key in _cache) {
74 split(module_and_key, parts, SUBSEP)
75 module = parts[1]
03c229bf 76 key = parts[2]
8482fea6
SK
77 val = _cache[module, key]
78 unused_for = cache_get_time() - _cache_atime[module, key]
03c229bf 79 if (unused_for > GC_Interval) {
8482fea6 80 msg_out_log_info(\
03c229bf
SK
81 "cache_gc",
82 sprintf(\
8482fea6
SK
83 "Deleting unused data MODULE=%s KEY=%s VAL=%s",
84 module, key, val\
03c229bf
SK
85 ) \
86 )
8482fea6 87 delete _cache[module, key]
03c229bf
SK
88 }
89 }
90}
91
8482fea6 92function cache_maybe_total_to_diff(module, key, val, key_parts) {
03c229bf 93 split(key, key_parts, Kfs)
8482fea6
SK
94 if (_total_to_diff[module, key_parts[1]]) {
95 _prev[module, key] = _curr[module, key]
96 _curr[module, key] = val
97 return (_curr[module, key] - _prev[module, key])
03c229bf
SK
98 } else {
99 return val
100 }
101}
102
8482fea6 103function cache_maybe_scale(module, key, val, key_parts) {
03c229bf 104 split(key, key_parts, Kfs)
8482fea6
SK
105 if ((module SUBSEP key_parts[1]) in _scale) {
106 return val * _scale[module, key_parts[1]]
03c229bf
SK
107 } else {
108 return val
109 }
110}
This page took 0.052223 seconds and 4 git commands to generate.