backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / operator / commands / show / memory.py
1 import gc
2 import sys
3
4 from ryu.services.protocols.bgp.operator.command import Command
5 from ryu.services.protocols.bgp.operator.command import CommandsResponse
6 from ryu.services.protocols.bgp.operator.command import STATUS_ERROR
7 from ryu.services.protocols.bgp.operator.command import STATUS_OK
8
9
10 class Memory(Command):
11     help_msg = 'show memory information'
12     command = 'memory'
13
14     def __init__(self, *args, **kwargs):
15         super(Memory, self).__init__(*args, **kwargs)
16         self.subcommands = {
17             'summary': self.Summary}
18
19     class Summary(Command):
20         help_msg = 'shows total memory used and how it is getting used'
21         command = 'summary'
22
23         def action(self, params):
24             count = {}
25             size = {}
26             total_size = 0
27             unreachable = gc.collect()
28             for obj in gc.get_objects():
29                 inst_name = type(obj).__name__
30                 c = count.get(inst_name, None)
31                 if not c:
32                     count[inst_name] = 0
33                 s = size.get(inst_name, None)
34                 if not s:
35                     size[inst_name] = 0
36
37                 count[inst_name] += 1
38                 s = sys.getsizeof(obj)
39                 size[inst_name] += s
40                 total_size += s
41
42             # Total size in MB
43
44             total_size = total_size // 1000000
45             ret = {
46                 'unreachable': unreachable,
47                 'total': total_size,
48                 'summary': []}
49
50             for class_name, s in size.items():
51                 # Calculate size in MB
52                 size_mb = s // 1000000
53                 # We are only interested in class which take-up more than a MB
54                 if size_mb > 0:
55                     ret['summary'].append(
56                         {
57                             'class': class_name,
58                             'instances': count.get(class_name, None),
59                             'size': size_mb
60                         }
61                     )
62
63             return CommandsResponse(STATUS_OK, ret)
64
65         @classmethod
66         def cli_resp_formatter(cls, resp):
67             if resp.status == STATUS_ERROR:
68                 return Command.cli_resp_formatter(resp)
69             val = resp.value
70             ret = 'Unreachable objects: {0}\n'.format(
71                 val.get('unreachable', None)
72             )
73             ret += 'Total memory used (MB): {0}\n'.format(
74                 val.get('total', None)
75             )
76             ret += 'Classes with instances that take-up more than one MB:\n'
77             ret += '{0:<20s} {1:>16s} {2:>16s}\n'.format(
78                 'Class',
79                 '#Instance',
80                 'Size(MB)'
81             )
82
83             for s in val.get('summary', []):
84                 ret += '{0:<20s} {1:>16d} {2:>16d}\n'.format(
85                     s.get('class', None), s.get('instances', None),
86                     s.get('size', None)
87                 )
88
89             return ret