-
Notifications
You must be signed in to change notification settings - Fork 7
/
diagnostics.py
76 lines (59 loc) · 1.98 KB
/
diagnostics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
diagnostics.py
Copyright 2019 by Eric Hellman
Distributable under the GNU General Public License Version 3 or newer.
"""
import json
import resource
import sys
from collections import Mapping, Container
from sys import getsizeof
import threading
import cherrypy
from cherrypy.lib.sessions import RamSession
from Page import Page
def thread_info():
return [t.name for t in threading.enumerate()]
def deep_getsizeof(o, ids):
"""Find the memory footprint of a Python object
This is a recursive function that rills down a Python object graph
like a dictionary holding nested ditionaries with lists of lists
and tuples and sets.
The sys.getsizeof function does a shallow size of only. It counts each
object inside a container as pointer only regardless of how big it
really is.
:param o: the object
:param ids:
:return:
https://github.com/the-gigi/deep/blob/master/deeper.py
"""
d = deep_getsizeof
if id(o) in ids:
return 0
r = getsizeof(o)
ids.add(id(o))
if isinstance(o, str):
return r
if isinstance(o, Mapping):
try:
return r + sum(d(k, ids) + d(v, ids) for k, v in list(o.items()))
except RuntimeError:
return 'error'
if isinstance(o, Container):
return r + sum(d(x, ids) for x in o)
return r
class DiagnosticsPage (Page):
""" Python health. """
@cherrypy.tools.json_out()
def index (self, **dummy_kwargs):
""" return stats. """
stats = {}
stats['sessions'] = len(RamSession.cache)
stats['sessions_storage'] = deep_getsizeof(RamSession.cache, set())
stats['allocated_blocks'] = sys.getallocatedblocks()
stats['rusage_self'] = resource.getrusage(resource.RUSAGE_SELF)
stats['rusage_children'] = resource.getrusage(resource.RUSAGE_CHILDREN)
stats['thread_info'] = thread_info()
return stats