batchplotter.py 3.7 KB

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