1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 from builtins import str
6 from builtins import object
9 from collections import namedtuple
11 Stat = namedtuple("Stat", ['name', 'get'])
13 class StatWriter(object):
14 def __init__(self, prefix, interval, stats):
16 self.interval = interval
18 self.previous_stats = []
19 self.update_previous_stats()
21 def update_previous_stats(self):
22 self.previous_stats = [stat.get() for stat in self.stats]
25 def append_by_type(string, name, value):
26 if type(value) is float:
27 string += " %.6f %s" % (value, name)
29 string += " %s %s" % (str(value), name)
32 out = "crunchstat: %s" % self.prefix
33 delta = "-- interval %.4f seconds" % self.interval
34 for i, stat in enumerate(self.stats):
36 diff = value - self.previous_stats[i]
37 delta = append_by_type(delta, stat.name, diff)
38 out = append_by_type(out, stat.name, value)
40 sys.stderr.write("%s %s\n" % (out, delta))
41 self.update_previous_stats()
43 def statlogger(interval, keep, ops):
44 calls = StatWriter("keepcalls", interval, [
45 Stat("put", keep.put_counter.get),
46 Stat("get", keep.get_counter.get)
48 net = StatWriter("net:keep0", interval, [
49 Stat("tx", keep.upload_counter.get),
50 Stat("rx", keep.download_counter.get)
52 cache = StatWriter("keepcache", interval, [
53 Stat("hit", keep.hits_counter.get),
54 Stat("miss", keep.misses_counter.get)
56 fuseops = StatWriter("fuseops", interval, [
57 Stat("write", ops.write_ops_counter.get),
58 Stat("read", ops.read_ops_counter.get)
61 for cur_op in ops.metric_op_names():
62 name = "fuseop:{0}".format(cur_op)
63 fusetimes.append(StatWriter(name, interval, [
64 Stat("count", ops.metric_count_func(cur_op)),
65 Stat("time", ops.metric_sum_func(cur_op))
67 blk = StatWriter("blkio:0:0", interval, [
68 Stat("write", ops.write_counter.get),
69 Stat("read", ops.read_counter.get)
79 for ftime in fusetimes: