#!/usr/bin/env python3
"""Plot virtual and physical memory of several runs collected by collect.sh.

Usage:
    plot-memory-usage.py <output.png> <label>=<run.csv> [<label>=<run.csv> ...]
"""
import sys

import matplotlib.pyplot as plt
import pandas as pd


def main():
    if len(sys.argv) < 3:
        sys.exit(__doc__)
    out = sys.argv[1]
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8), sharex=True)
    for arg in sys.argv[2:]:
        label, path = arg.rsplit("=", 1)
        df = pd.read_csv(path).dropna()
        ax1.plot(df["seconds"], df["virtual_memory_kb"] / 1024 / 1024, linewidth=2, label=label)
        ax2.plot(df["seconds"], df["physical_memory_kb"] / 1024, linewidth=2, label=label)
    ax1.set_ylabel("Virtual memory (GiB)")
    ax2.set_ylabel("Physical memory, VmRSS (MiB)")
    ax2.set_xlabel("Time (seconds)")
    for ax in (ax1, ax2):
        ax.grid(True)
        ax.legend(loc="upper left")
    plt.tight_layout()
    plt.savefig(out, dpi=100)


if __name__ == "__main__":
    main()
