batch_tables.py 4.3 KB

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