qvm-top 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection
  24. from qubes.qubes import QubesHost
  25. from qubes.qubes import QubesException
  26. from optparse import OptionParser
  27. import sys
  28. def main():
  29. usage = "usage: %prog [options]"
  30. parser = OptionParser (usage)
  31. parser.add_option("--list", dest="list_top",
  32. action="store_true", default=False,
  33. help="n m : One line summary of top n vms with more than m cpu_time %")
  34. (options, args) = parser.parse_args ()
  35. qvm_collection = QubesVmCollection()
  36. qvm_collection.lock_db_for_reading()
  37. qvm_collection.load()
  38. qvm_collection.unlock_db()
  39. fields_to_display = ["name", "cpu", "mem"]
  40. cpu_usages = None
  41. qhost = QubesHost()
  42. (measure_time, cpu_usages) = qhost.measure_cpu_usage(qvm_collection)
  43. vms_list = [vm for vm in qvm_collection.values() if vm.is_running()]
  44. vms_list = sorted(vms_list, key= lambda vm: 1-cpu_usages[vm.get_xid()]['cpu_usage'])
  45. no_vms = len (vms_list)
  46. vms_to_display = vms_list
  47. if options.list_top:
  48. any_shown = False
  49. ndisp = 3
  50. cputh = 0
  51. if len(args) > 0:
  52. ndisp = int(args[0])
  53. if len(args) > 1:
  54. cputh = int(args[1])
  55. for vm in vms_to_display[:ndisp]:
  56. cpu = cpu_usages[vm.get_xid()]['cpu_usage']
  57. if cpu > cputh:
  58. any_shown = True
  59. sys.stdout.write("%d %s, " % (cpu, vm.name))
  60. if any_shown:
  61. sys.stdout.write(" ... | ")
  62. totalMem = 0
  63. dom0mem = 0
  64. for vm in vms_to_display:
  65. if not vm.name == "dom0":
  66. totalMem += vm.get_mem()
  67. else:
  68. dom0mem = vm.get_mem()
  69. totalMem /= 1024.0 * 1024.0
  70. dom0mem /= 1024.0 * 1024.0
  71. sys.stdout.write("%.1f G + %.1f G" % (totalMem, dom0mem))
  72. return
  73. max_width = { 'name': 0, 'cpu': 0, 'mem': 0 }
  74. data_to_display = []
  75. for vm in vms_to_display:
  76. data_row = {}
  77. data_row['name'] = vm.name
  78. max_width['name'] = max(max_width['name'], len(data_row['name']))
  79. data_row['cpu'] = "%.1f" % (cpu_usages[vm.get_xid()]['cpu_usage'])
  80. max_width['cpu'] = max(max_width['cpu'], len(data_row['cpu']))
  81. data_row['mem'] = "%d" % (vm.get_mem() / (1024.0))
  82. max_width['mem'] = max(max_width['mem'], len(data_row['mem']))
  83. data_to_display.append(data_row)
  84. # Display the header
  85. s = ""
  86. for f in fields_to_display:
  87. fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
  88. s += fmt.format('-')
  89. print s
  90. s = ""
  91. for f in fields_to_display:
  92. fmt="{{0:>{0}}} |".format(max_width[f] + 1)
  93. s += fmt.format(f)
  94. print s
  95. s = ""
  96. for f in fields_to_display:
  97. fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
  98. s += fmt.format('-')
  99. print s
  100. # ... and the actual data
  101. for row in data_to_display:
  102. s = ""
  103. for f in fields_to_display:
  104. fmt="{{0:>{0}}} |".format(max_width[f] + 1)
  105. s += fmt.format(row[f])
  106. print s
  107. main()