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
10 class Memory(Command):
11 help_msg = 'show memory information'
14 def __init__(self, *args, **kwargs):
15 super(Memory, self).__init__(*args, **kwargs)
17 'summary': self.Summary}
19 class Summary(Command):
20 help_msg = 'shows total memory used and how it is getting used'
23 def action(self, params):
27 unreachable = gc.collect()
28 for obj in gc.get_objects():
29 inst_name = type(obj).__name__
30 c = count.get(inst_name, None)
33 s = size.get(inst_name, None)
38 s = sys.getsizeof(obj)
44 total_size = total_size // 1000000
46 'unreachable': unreachable,
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
55 ret['summary'].append(
58 'instances': count.get(class_name, None),
63 return CommandsResponse(STATUS_OK, ret)
66 def cli_resp_formatter(cls, resp):
67 if resp.status == STATUS_ERROR:
68 return Command.cli_resp_formatter(resp)
70 ret = 'Unreachable objects: {0}\n'.format(
71 val.get('unreachable', None)
73 ret += 'Total memory used (MB): {0}\n'.format(
74 val.get('total', None)
76 ret += 'Classes with instances that take-up more than one MB:\n'
77 ret += '{0:<20s} {1:>16s} {2:>16s}\n'.format(
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),