batch_tables.py 4.3 KB

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