qvm-top 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from qubes.qubes import QubesHost
  24. from qubes.qubes import QubesException
  25. from optparse import OptionParser
  26. import sys
  27. def main():
  28. usage = "usage: %prog [options]"
  29. parser = OptionParser (usage)
  30. parser.add_option("--list", dest="list_top",
  31. action="store_true", default=False,
  32. help="n m : One line summary of top n vms with more than m cpu_time %")
  33. (options, args) = parser.parse_args ()
  34. qvm_collection = QubesVmCollection()
  35. qvm_collection.lock_db_for_reading()
  36. qvm_collection.load()
  37. qvm_collection.unlock_db()
  38. fields_to_display = ["name", "cpu", "mem"]
  39. cpu_usages = None
  40. qhost = QubesHost()
  41. (measure_time, cpu_usages) = qhost.measure_cpu_usage(qvm_collection)
  42. vms_list = [vm for vm in qvm_collection.values() if vm.is_running()]
  43. vms_list = sorted(vms_list, key= lambda vm: 1-cpu_usages[vm.get_xid()]['cpu_usage'])
  44. no_vms = len (vms_list)
  45. vms_to_display = vms_list
  46. if options.list_top:
  47. any_shown = False
  48. ndisp = 3
  49. cputh = 0
  50. if len(args) > 0:
  51. ndisp = int(args[0])
  52. if len(args) > 1:
  53. cputh = int(args[1])
  54. for vm in vms_to_display[:ndisp]:
  55. cpu = cpu_usages[vm.get_xid()]['cpu_usage']
  56. if cpu > cputh:
  57. any_shown = True
  58. sys.stdout.write("%d %s, " % (cpu, vm.name))
  59. if any_shown:
  60. sys.stdout.write(" ... | ")
  61. totalMem = 0
  62. dom0mem = 0
  63. for vm in vms_to_display:
  64. if not vm.name == "dom0":
  65. totalMem += vm.get_mem()
  66. else:
  67. dom0mem = vm.get_mem()
  68. totalMem /= 1024.0 * 1024.0
  69. dom0mem /= 1024.0 * 1024.0
  70. sys.stdout.write("%.1f G + %.1f G" % (totalMem, dom0mem))
  71. return
  72. max_width = { 'name': 0, 'cpu': 0, 'mem': 0 }
  73. data_to_display = []
  74. for vm in vms_to_display:
  75. data_row = {}
  76. data_row['name'] = vm.name
  77. max_width['name'] = max(max_width['name'], len(data_row['name']))
  78. data_row['cpu'] = "%.1f" % (cpu_usages[vm.get_xid()]['cpu_usage'])
  79. max_width['cpu'] = max(max_width['cpu'], len(data_row['cpu']))
  80. data_row['mem'] = "%d" % (vm.get_mem() / (1024.0))
  81. max_width['mem'] = max(max_width['mem'], len(data_row['mem']))
  82. data_to_display.append(data_row)
  83. # Display the header
  84. s = ""
  85. for f in fields_to_display:
  86. fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
  87. s += fmt.format('-')
  88. print s
  89. s = ""
  90. for f in fields_to_display:
  91. fmt="{{0:>{0}}} |".format(max_width[f] + 1)
  92. s += fmt.format(f)
  93. print s
  94. s = ""
  95. for f in fields_to_display:
  96. fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
  97. s += fmt.format('-')
  98. print s
  99. # ... and the actual data
  100. for row in data_to_display:
  101. s = ""
  102. for f in fields_to_display:
  103. fmt="{{0:>{0}}} |".format(max_width[f] + 1)
  104. s += fmt.format(row[f])
  105. print s
  106. main()