Commit 6a321df3 authored by Mario Hock's avatar Mario Hock

Logging: cleanup of init code; JSON+CSV header

parent e04ffd7b
......@@ -87,77 +87,114 @@ class MeasurementLogger:
"""
## Initialization ##
def __init__(self, begin, num_cpus, nics, comment, filename):
## Attributes
self.num_cpus = num_cpus
self.nics = nics
self.filename = filename
##
## Constants / Characteristics
self.class_names = ("Time", "CPU", "NIC")
self.type_string = "CPUnetLOG:MeasurementLog"
## Run "outsourced" init functions.
self.class_defs = self._init_class_definitions(num_cpus, nics)
self.json_header = self._create_json_header(self.class_names,
self.class_defs.values(),
self.type_string,
comment)
self.csv_header = self._create_csv_header(self.json_header)
## Register special logging functions.
self.log_functions = dict()
self.log_functions["Time"] = self._log_time
self.log_functions["CPU"] = self._log_cpus
self.log_functions["NIC"] = self._log_nics
## General
self.general = dict()
self.general["Classes"] = self.class_names
self.general["Type"] = "CPUnetLOG:MeasurementLog"
self.general["Comment"] = comment
self.general["Begin"] = begin
self.general["End"] = 0 ## TODO
self.general["Duration"] = 0 ## TODO
## Class definitions
self.class_defs = dict()
def _init_class_definitions(self, num_cpus, nics):
class_defs = dict()
# set up "CPU" class
cpu = LoggingClass( name = "CPU",
fields = ("usr", "system", "softirq", "other", "idle"),
siblings = [ "CPU" + str(i) for i in range(1,num_cpus) ],
description = "CPU utilization in percent" )
self.class_defs["CPU"] = cpu
class_defs["CPU"] = cpu
# set up "NIC" class
nic = LoggingClass( name = "NIC",
fields = ("send", "receive"),
siblings = nics,
description = "Network traffic (Bits/s)" )
self.class_defs["NIC"] = nic
class_defs["NIC"] = nic
# set up "Time" class
time = LoggingClass( name = "Time",
fields = ("begin", "end", "duration"),
siblings = None,
description = "Begin, end, and duration (in seconds) of this measurement." )
self.class_defs["Time"] = time
fields = ("begin", "end", "duration"),
siblings = None,
description = "Begin, end, and duration (in seconds) of this measurement." )
class_defs["Time"] = time
## XXX
self._write_json_header()
return class_defs
def _write_json_header(self):
top = dict()
## TODO Move this outside the class?
def _create_json_header(self, class_names, class_defs, type, comment):
top_level = dict()
general = dict()
class_definitions = dict()
## General
top["General"] = self.general
general["Classes"] = class_names
general["Type"] = type
general["Comment"] = comment
#general["Begin"] = begin
#general["End"] = 0 ## TODO ... can't be written at the beginning of the file!!
#general["Duration"] = 0 ## TODO
top_level["General"] = general
## Class definitions
for c in self.class_defs.values():
for c in class_defs:
class_definitions[c.name] = c.values
top["ClassDefinitions"] = class_definitions
top_level["ClassDefinitions"] = class_definitions
pretty_json = json.dumps(top, sort_keys=True, indent=4)
pretty_json = json.dumps(top_level, sort_keys=True, indent=4)
## XXX
print( pretty_json )
return top_level
## TODO Move this outside the class?
def _create_csv_header(self, json_header):
csv_header = list()
general = json_header["General"]
class_definitions = json_header["ClassDefinitions"]
for _class_name in general["Classes"]:
_class = class_definitions[_class_name]
if ( _class["Siblings"] ):
for sibling in _class["Siblings"]:
for field in _class["Fields"]:
csv_header.append( ".".join([sibling, field]) )
else:
for field in _class["Fields"]:
csv_header.append( field )
## XXX
print( ", ".join(csv_header) )
return csv_header
......@@ -200,4 +237,5 @@ class MeasurementLogger:
## Close ##
def close(self):
## TODO
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment