Skip to content

Commit 6afa6b4

Browse files
add unit and byte conversion fields
1 parent 424d9c1 commit 6afa6b4

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

jc/parsers/top_s.py

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,39 @@
4343
"cpu_hardware": float,
4444
"cpu_software": float,
4545
"cpu_steal": float,
46-
"mem_total": float, # [0]
47-
"mem_free": float, # [0]
48-
"mem_used": float, # [0]
49-
"mem_buff_cache": float, # [0]
50-
"swap_total": float, # [0]
51-
"swap_free": float, # [0]
52-
"swap_used": float, # [0]
53-
"mem_available": float, # [0]
46+
"mem_unit": string,
47+
"swap_unit": string,
48+
"mem_total": float,
49+
"mem_total_bytes": integer,
50+
"mem_free": float,
51+
"mem_free_bytes": integer,
52+
"mem_used": float,
53+
"mem_used_bytes": integer,
54+
"mem_buff_cache": float,
55+
"mem_buff_cache_bytes": integer,
56+
"swap_total": float,
57+
"swap_total_bytes": integer,
58+
"swap_free": float,
59+
"swap_free_bytes": integer,
60+
"swap_used": float,
61+
"swap_used_bytes": integer,
62+
"mem_available": float,
63+
"mem_available_bytes": integer,
5464
"processes": [
5565
{
5666
"pid": integer,
5767
"user": string,
5868
"priority": integer,
5969
"nice": integer,
60-
"virtual_mem": float, # [1]
61-
"resident_mem": float, # [1]
62-
"shared_mem": float, # [1]
70+
"virtual_mem": float,
71+
"virtual_mem_unit": string,
72+
"virtual_mem_bytes": integer,
73+
"resident_mem": float,
74+
"resident_mem_unit": string,
75+
"resident_mem_bytes": integer,
76+
"shared_mem": float,
77+
"shared_mem_unit": string,
78+
"shared_mem_bytes": integer,
6379
"status": string,
6480
"percent_cpu": float,
6581
"percent_mem": float,
@@ -80,9 +96,15 @@
8096
"thread_count": integer,
8197
"last_used_processor": integer,
8298
"time": string,
83-
"swap": float, # [1]
84-
"code": float, # [1]
85-
"data": float, # [1]
99+
"swap": float,
100+
"swap_unit": string,
101+
"swap_bytes": integer,
102+
"code": float,
103+
"code_unit": string,
104+
"code_bytes": integer
105+
"data": float,
106+
"data_unit": string,
107+
"data_bytes": integer,
86108
"major_page_fault_count": integer,
87109
"minor_page_fault_count": integer,
88110
"dirty_pages_count": integer,
@@ -101,7 +123,9 @@
101123
]
102124
"major_page_fault_count_delta": integer,
103125
"minor_page_fault_count_delta": integer,
104-
"used": float, # [1]
126+
"used": float,
127+
"used_unit": string,
128+
"used_bytes": integer,
105129
"ipc_namespace_inode": integer,
106130
"mount_namespace_inode": integer,
107131
"net_namespace_inode": integer,
@@ -128,9 +152,6 @@
128152
}
129153
}
130154
131-
[0] Values are in the units output by `top`
132-
[1] Unit suffix stripped during float conversion
133-
134155
Examples:
135156
136157
$ top -b | jc --top-s
@@ -153,7 +174,7 @@
153174

154175
class info():
155176
"""Provides parser metadata (version, author, etc.)"""
156-
version = '1.2'
177+
version = '1.3'
157178
description = '`top -b` command streaming parser'
158179
author = 'Kelly Brazil'
159180
author_email = 'kellyjonbrazil@gmail.com'
@@ -277,12 +298,24 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
277298
'mem_available', 'virtual_mem', 'resident_mem', 'shared_mem', 'swap', 'code', 'data', 'used'
278299
}
279300

280-
for key in proc_data:
301+
bytes_list: Set = {
302+
'mem_total', 'mem_free', 'mem_used', 'mem_available', 'mem_buff_cache',
303+
'swap_total', 'swap_free', 'swap_used', 'virtual_mem', 'resident_mem',
304+
'shared_mem', 'swap', 'code', 'data', 'used'
305+
}
306+
307+
for key in proc_data.copy():
281308
# root truncation warnings
282309
if isinstance(proc_data[key], str) and proc_data[key].endswith('+') and not quiet:
283310
jc.utils.warning_message([f'item[{idx}]["{key}"] was truncated by top'])
284311

285312
# root int and float conversions
313+
if key in bytes_list:
314+
if key.startswith('mem_'):
315+
proc_data[key + '_bytes'] = jc.utils.convert_size_to_int(proc_data[key] + proc_data['mem_unit'])
316+
if key.startswith('swap_'):
317+
proc_data[key + '_bytes'] = jc.utils.convert_size_to_int(proc_data[key] + proc_data['swap_unit'])
318+
286319
if key in int_list:
287320
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
288321

@@ -299,7 +332,8 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
299332
jc.utils.warning_message([f'Unknown field detected at item[{idx}]["processes"]: {old_key}'])
300333

301334
# cleanup values
302-
for key in proc.keys():
335+
proc_copy = proc.copy()
336+
for key in proc_copy.keys():
303337

304338
# set dashes to nulls
305339
if proc[key] == '-':
@@ -318,6 +352,13 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
318352

319353
# do int/float conversions for the process objects
320354
if proc[key]:
355+
if key in bytes_list:
356+
if proc[key][-1] not in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
357+
proc[key + '_unit'] = proc[key][-1]
358+
else:
359+
proc[key + '_unit'] = 'b'
360+
proc[key + '_bytes'] = jc.utils.convert_size_to_int(proc[key], posix_mode=True)
361+
321362
if key in int_list:
322363
proc[key] = jc.utils.convert_to_int(proc[key])
323364

@@ -448,6 +489,7 @@ def parse(
448489
line_list = line.split()
449490
output_line.update(
450491
{
492+
'mem_unit': line_list[0],
451493
'mem_total': line_list[3],
452494
'mem_free': line_list[5],
453495
'mem_used': line_list[7],
@@ -461,6 +503,7 @@ def parse(
461503
line_list = line.split()
462504
output_line.update(
463505
{
506+
'swap_unit': line_list[0],
464507
'swap_total': line_list[2],
465508
'swap_free': line_list[4],
466509
'swap_used': line_list[6],
@@ -489,3 +532,5 @@ def parse(
489532
if process_list:
490533
output_line['processes'] = parse_table(process_list)
491534
yield output_line if raw else _process(output_line, idx=idx, quiet=quiet)
535+
536+
return None

0 commit comments

Comments
 (0)