batchplotter.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  3. import json
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. import os
  7. import sys
  8. from subprocess import Popen, PIPE
  9. from stats import parseData
  10. bar_width = 0.25
  11. index = []
  12. def plot(process_data, title, filename):
  13. fig, ax = plt.subplots(figsize=(10,5))
  14. task_output = []
  15. for p in process_data:
  16. cmd = ["./simusched",p["tasks"]]
  17. cmd.extend(p["args"].split(" "))
  18. process = Popen(cmd, stdout=PIPE)
  19. (out, _) = process.communicate()
  20. process.wait()
  21. out = out.decode("utf-8")
  22. (parsed,idle) = parseData(out.split("\n"))
  23. ap = {}
  24. ap["idle"] = idle
  25. ap["processes"] = parsed
  26. ap["tasks"] = p["tasks"]
  27. ap["args"] = p["args"]
  28. task_output.append(ap)
  29. m_latencies = []
  30. m_ready = []
  31. m_cpu = []
  32. m_cpuio = []
  33. idletime = []
  34. for t in task_output:
  35. m_latencies.append(meanLatency(t))
  36. m_ready.append(meanReady(t))
  37. m_cpu.append(meanCPU(t))
  38. m_cpuio.append(meanCPUIO(t))
  39. idletime.append(t["idle"])
  40. plot_data(0, m_latencies, 'r', 'M. Latency')
  41. plot_data(1, m_ready, 'g', 'M. Ready')
  42. plot_data(2, m_cpu, 'b', 'M. CPU')
  43. plot_data(3, m_cpuio, '#deadbe', 'M. CPU+IO')
  44. plot_data(4, idletime, '#c1c1c1', 'Idle')
  45. ax.set_ylabel('Quantum')
  46. ax.set_title(title + " - %s" % task_output[0]["tasks"])
  47. ax.set_xticks([ i + 1.5 * bar_width for i in index])
  48. ax.set_xticklabels( ["%s" % task_output[i]["args"] for i in range(len(process_data))])
  49. for tick in ax.xaxis.get_major_ticks():
  50. tick.label.set_fontsize(8)
  51. plt.legend(loc="best")
  52. plt.tight_layout()
  53. plt.grid()
  54. plt.savefig(filename)
  55. def meanLatency(pl):
  56. totLatency = sum([ latency(p) for p in pl["processes"]])
  57. mean = totLatency / len(pl["processes"])
  58. return mean
  59. def meanReady(pl):
  60. totReady = sum([ ready(p) for p in pl["processes"]])
  61. mean = totReady / len(pl["processes"])
  62. return mean
  63. def meanCPU(pl):
  64. totCPU = sum([ cpu(p) for p in pl["processes"]])
  65. mean = totCPU / len(pl["processes"])
  66. return mean
  67. def meanCPUIO(pl):
  68. totCPUIO = sum([ cpu_io(p) for p in pl["processes"]])
  69. mean = totCPUIO / len(pl["processes"])
  70. return mean
  71. def latency(p):
  72. return p["primerTick"]-p["cargaTick"]
  73. def ready(p):
  74. return (p["ultimoTick"]-p["primerTick"])-(p["ticksBlock"]+p["ticksCpu"])+(p["primerTick"]-p["cargaTick"])
  75. def cpu(p):
  76. return p["ticksCpu"]
  77. def cpu_io(p):
  78. return p["ticksBlock"]+p["ticksCpu"]
  79. def plot_data(ind, data, color, label):
  80. opacity = 1
  81. plt.bar([i + bar_width*ind for i in index],
  82. data,
  83. bar_width,
  84. alpha=opacity,
  85. color=color,
  86. label=(label))
  87. if len(sys.argv) != 2:
  88. print("1 arg, json file")
  89. sys.exit(1)
  90. if not os.path.isfile(sys.argv[1]):
  91. print("'%s' no es un archivo / no existe" % sys.argv[1])
  92. sys.exit(2)
  93. try:
  94. data = open(sys.argv[1]).read()
  95. j = json.loads(data)
  96. index = [ 1.5*i for i in list(range(len(j["list"]))) ]
  97. plot(j["list"],j["title"], j["filename"])
  98. except Exception as e:
  99. print("Exception")
  100. print(data)
  101. print(e)