in pplbench/lib/utils.py [0:0]
def create_output_dir(config: SimpleNamespace) -> str:
"""
Create an output directory for storing the benchmark results and write the config file to it.
:param config: the experiment configuration
:returns: a directory name for storing outputs
"""
root_dir = getattr(config, "output_root_dir", os.path.join(".", "outputs"))
if not os.path.isdir(root_dir):
os.mkdir(os.path.join(".", "outputs"))
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime(
"%Y-%m-%d_%H:%M:%S"
)
output_dir = create_subdir(root_dir, timestamp)
# dump the config file in the output directory
with open(os.path.join(output_dir, "config.json"), "w") as fp:
fp.write(SimpleNamespaceEncoder().encode(config))
# redirect C stdout and stderr to files to avoid cluttering user's display
# but keep Python stdout and stderr intact
for sname in ["stdout", "stderr"]:
py_stream = getattr(sys, sname)
# save the current stream's file descriptor
saved_fd = os.dup(py_stream.fileno())
# redirect the current stream's file descriptor to a log file
log_fd = os.open(
os.path.join(output_dir, f"{sname}.txt"), os.O_WRONLY | os.O_CREAT
)
os.dup2(log_fd, py_stream.fileno())
# now restor the Python stream to the saved file descriptor
setattr(sys, sname, io.TextIOWrapper(os.fdopen(saved_fd, "wb")))
return output_dir