Merge branch '13108-acr-threading-fixes' refs #13108
[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 class Stat(object):
9     def __init__(self, prefix, interval,
10                  egr_name, ing_name,
11                  egr_func, ing_func):
12         self.prefix = prefix
13         self.interval = interval
14         self.egr_name = egr_name
15         self.ing_name = ing_name
16         self.egress = egr_func
17         self.ingress = ing_func
18         self.egr_prev = self.egress()
19         self.ing_prev = self.ingress()
20
21     def update(self):
22         egr = self.egress()
23         ing = self.ingress()
24
25         delta = " -- interval %.4f seconds %d %s %d %s" % (self.interval,
26                                                            egr - self.egr_prev,
27                                                            self.egr_name,
28                                                            ing - self.ing_prev,
29                                                            self.ing_name)
30
31         sys.stderr.write("crunchstat: %s %d %s %d %s%s\n" % (self.prefix,
32                                                              egr,
33                                                              self.egr_name,
34                                                              ing,
35                                                              self.ing_name,
36                                                              delta))
37
38         self.egr_prev = egr
39         self.ing_prev = ing
40
41
42 def statlogger(interval, keep, ops):
43     calls = Stat("keepcalls", interval, "put", "get",
44                  keep.put_counter.get,
45                  keep.get_counter.get)
46     net = Stat("net:keep0", interval, "tx", "rx",
47                keep.upload_counter.get,
48                keep.download_counter.get)
49     cache = Stat("keepcache", interval, "hit", "miss",
50                keep.hits_counter.get,
51                keep.misses_counter.get)
52     fuseops = Stat("fuseops", interval,"write", "read",
53                    ops.write_ops_counter.get,
54                    ops.read_ops_counter.get)
55     blk = Stat("blkio:0:0", interval, "write", "read",
56                ops.write_counter.get,
57                ops.read_counter.get)
58
59     while True:
60         time.sleep(interval)
61         calls.update()
62         net.update()
63         cache.update()
64         fuseops.update()
65         blk.update()
66
67