def test_read_memory_stats(self): """Tests updating memory stats from cgroups.""" self.assertEqual( metrics.read_memory_stats('treadmill/apps/appname'), { 'memory.failcnt': 2, 'memory.limit_in_bytes': 2, 'memory.max_usage_in_bytes': 2, 'memory.memsw.failcnt': 2, 'memory.memsw.limit_in_bytes': 2, 'memory.stat': { 'active_anon': 0, 'active_file': 0, 'cache': 0, 'hierarchical_memory_limit': 0, 'hierarchical_memsw_limit': 0, 'inactive_anon': 0, 'inactive_file': 0, 'mapped_file': 0, 'pgpgin': 0, 'pgpgout': 0, 'rss': 0, 'swap': 0, 'total_active_anon': 0, 'total_active_file': 0, 'total_cache': 0, 'total_inactive_anon': 0, 'total_inactive_file': 0, 'total_mapped_file': 0, 'total_pgpgin': 0, 'total_pgpgout': 0, 'total_rss': 0, 'total_swap': 0, 'total_unevictable': 0, 'unevictable': 0 } })
def test_read_memory_stats(self): """Tests updating memory stats from cgroups.""" self.assertEquals(metrics.read_memory_stats('treadmill/apps/appname'), (10, 12, 13))
def psmem_cmd(fast, app, verbose, percent): """Reports memory utilization details for given container.""" if app.find('#') == -1: raise click.BadParameter('Specify full instance name: xxx#nnn') app = app.replace('#', '-') cgroup = None apps = os.listdir('/sys/fs/cgroup/memory/treadmill/apps') for entry in apps: if app in entry: cgroup = os.path.join('/treadmill/apps', entry) if not cgroup: raise click.BadParameter('Could not find corresponding cgroup') pids = cgutils.pids_in_cgroup('memory', cgroup) use_pss = not fast memusage = psmem.get_memory_usage(pids, verbose, use_pss=use_pss) proc_columns = ([ 'ppid', 'name', 'threads', 'private', 'shared', 'total' ]) proc_table = prettytable.PrettyTable(proc_columns) proc_table.align = 'l' proc_table.set_style(prettytable.PLAIN_COLUMNS) proc_table.left_padding_width = 0 proc_table.right_padding_width = 2 total = sum([info['total'] for info in memusage.values()]) readable = lambda value: utils.bytes_to_readable(value, power='B') percentage = lambda value, total: "{:.1%}".format(value / total) cols = proc_columns[3:] for proc, info in sorted(memusage.items()): row_base = [proc, info['name'], info['threads']] row_values = None if percent: row_values = [percentage(info[col], total) for col in cols] else: row_values = [readable(info[col]) for col in cols] proc_table.add_row(row_base + row_values) proc_table.add_row(['', '', '', '', '', '']) proc_table.add_row(['Total:', '', '', '', '', readable(total)]) print(proc_table) metric = metrics.read_memory_stats(cgroup) total_table = prettytable.PrettyTable(['memory', 'value']) total_table.align['memory'] = 'l' total_table.align['value'] = 'r' total_table.set_style(prettytable.PLAIN_COLUMNS) total_table.header = False # Actual memory usage is without the disk cache memory_usage = readable(metric['memory.usage_in_bytes'] - metric['memory.stats']['cache']) total_table.add_row(['usage', memory_usage]) total_table.add_row([ '', percentage(metric['memory.usage_in_bytes'], metric['memory.limit_in_bytes']) ]) total_table.add_row( ['diskcache', readable(metric['memory.stats']['cache'])]) total_table.add_row( ['softlimit', readable(metric['memory.soft_limit_in_bytes'])]) total_table.add_row( ['hardlimit', readable(metric['memory.limit_in_bytes'])]) print('') print(total_table)
def psmem_cmd(fast, app, verbose, percent): """Reports memory utilization details for given container. """ if app.find('#') == -1: raise click.BadParameter('Specify full instance name: xxx#nnn') app = app.replace('#', '-') cgroup = None apps = os.listdir('/sys/fs/cgroup/memory/treadmill/apps') for entry in apps: if app in entry: cgroup = os.path.join('/treadmill/apps', entry) if not cgroup: raise click.BadParameter('Could not find corresponding cgroup') pids = cgutils.pids_in_cgroup('memory', cgroup) use_pss = not fast memusage = psmem.get_memory_usage(pids, verbose, use_pss=use_pss) total = sum([info['total'] for info in memusage]) def _readable(value): return utils.bytes_to_readable(value, power='B') def _percentage(value, total): return '{:.1%}'.format(value / total) to_format = ['private', 'shared', 'total'] for info in memusage: for key, val in info.items(): if key in to_format: if percent: info[key] = _percentage(val, total) else: info[key] = _readable(val) proc_table = PsmemProcPrettyFormatter() print(proc_table.format(memusage)) metric = metrics.read_memory_stats(cgroup) total_list = [] # Actual memory usage is without the disk cache total_list.append({ 'memory-type': 'usage', 'value': _readable(metric['memory.usage_in_bytes'] - metric['memory.stat']['cache']) }) total_list.append({ 'memory-type': '', 'value': _percentage(metric['memory.usage_in_bytes'], metric['memory.limit_in_bytes']) }) total_list.append({ 'memory-type': 'diskcache', 'value': _readable(metric['memory.stat']['cache']) }) total_list.append({ 'memory-type': 'softlimit', 'value': _readable(metric['memory.soft_limit_in_bytes']) }) total_list.append({ 'memory-type': 'hardlimit', 'value': _readable(metric['memory.limit_in_bytes']) }) total_table = PsmemTotalPrettyFormatter() print('') print(total_table.format(total_list))