15397: Remove deprecated Python SDK facilities.
[arvados.git] / services / fuse / arvados_fuse / crunchstat.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import sys
6 import time
7
8 from collections import namedtuple
9
10 Stat = namedtuple("Stat", ['name', 'get'])
11
12 class StatWriter(object):
13     def __init__(self, prefix, interval, stats):
14         self.prefix = prefix
15         self.interval = interval
16         self.stats = stats
17         self.previous_stats = []
18         self.update_previous_stats()
19
20     def update_previous_stats(self):
21         self.previous_stats = [stat.get() for stat in self.stats]
22
23     def update(self):
24         def append_by_type(string, name, value):
25             if type(value) is float:
26                 string += " %.6f %s" % (value, name)
27             else:
28                 string += " %s %s" % (str(value), name)
29             return string
30
31         out = "crunchstat: %s" % self.prefix
32         delta = "-- interval %.4f seconds" % self.interval
33         for i, stat in enumerate(self.stats):
34             value = stat.get()
35             diff = value - self.previous_stats[i]
36             delta = append_by_type(delta, stat.name, diff)
37             out = append_by_type(out, stat.name, value)
38
39         sys.stderr.write("%s %s\n" % (out, delta))
40         self.update_previous_stats()
41
42 def statlogger(interval, keep, ops):
43     calls = StatWriter("keepcalls", interval, [
44         Stat("put", keep.put_counter.get),
45         Stat("get", keep.get_counter.get)
46     ])
47     net = StatWriter("net:keep0", interval, [
48         Stat("tx", keep.upload_counter.get),
49         Stat("rx", keep.download_counter.get)
50     ])
51     cache = StatWriter("keepcache", interval, [
52         Stat("hit", keep.hits_counter.get),
53         Stat("miss", keep.misses_counter.get)
54     ])
55     fuseops = StatWriter("fuseops", interval, [
56         Stat("write", ops.write_ops_counter.get),
57         Stat("read", ops.read_ops_counter.get)
58     ])
59     fusetimes = []
60     for cur_op in ops.metric_op_names():
61         name = "fuseop:{0}".format(cur_op)
62         fusetimes.append(StatWriter(name, interval, [
63             Stat("count", ops.metric_count_func(cur_op)),
64             Stat("time", ops.metric_sum_func(cur_op))
65         ]))
66     blk = StatWriter("blkio:0:0", interval, [
67         Stat("write", ops.write_counter.get),
68         Stat("read", ops.read_counter.get)
69     ])
70
71     while True:
72         time.sleep(interval)
73         calls.update()
74         net.update()
75         cache.update()
76         blk.update()
77         fuseops.update()
78         for ftime in fusetimes:
79             ftime.update()
80
81