memory.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2015 Jörg Thalheim (Mic92)
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import psutil
  22. from libqtile.widget import base
  23. __all__ = ["Memory"]
  24. class Memory(base.ThreadPoolText):
  25. """Displays memory/swap usage
  26. MemUsed: Returns memory in use
  27. MemTotal: Returns total amount of memory
  28. MemFree: Returns amount of memory free
  29. MemPercent: Returns memory in use as a percentage
  30. Buffers: Returns buffer amount
  31. Active: Returns active memory
  32. Inactive: Returns inactive memory
  33. Shmem: Returns shared memory
  34. SwapTotal: Returns total amount of swap
  35. SwapFree: Returns amount of swap free
  36. SwapUsed: Returns amount of swap in use
  37. SwapPercent: Returns swap in use as a percentage
  38. Widget requirements: psutil_.
  39. .. _psutil: https://pypi.org/project/psutil/
  40. """
  41. orientations = base.ORIENTATION_HORIZONTAL
  42. defaults = [
  43. ("format", "{MemUsed: .0f}{mm}/{MemTotal: .0f}{mm}", "Formatting for field names."),
  44. ("update_interval", 1.0, "Update interval for the Memory"),
  45. ("measure_mem", "M", "Measurement for Memory (G, M, B)"),
  46. ("measure_swap", "M", "Measurement for Swap (G, M, B)"),
  47. ]
  48. measures = {"G": 1024 * 1024 * 1024,
  49. "M": 1024 * 1024,
  50. "B": 1024}
  51. def __init__(self, **config):
  52. super().__init__("", **config)
  53. self.add_defaults(Memory.defaults)
  54. self.calc_mem = self.measures[self.measure_mem]
  55. self.calc_swap = self.measures[self.measure_swap]
  56. def poll(self):
  57. mem = psutil.virtual_memory()
  58. swap = psutil.swap_memory()
  59. val = {}
  60. val["MemUsed"] = mem.used / self.calc_mem
  61. val["MemTotal"] = mem.total / self.calc_mem
  62. val["MemFree"] = mem.free / self.calc_mem
  63. # val["MemPercent"] = mem.percent
  64. val["MemPercent"] = (mem.used / mem.total) * 100
  65. val["Buffers"] = mem.buffers / self.calc_mem
  66. val["Active"] = mem.active / self.calc_mem
  67. val["Inactive"] = mem.inactive / self.calc_mem
  68. val["Shmem"] = mem.shared / self.calc_mem
  69. val["SwapTotal"] = swap.total / self.calc_swap
  70. val["SwapFree"] = swap.free / self.calc_swap
  71. val["SwapUsed"] = swap.used / self.calc_swap
  72. val["SwapPercent"] = swap.percent
  73. val["mm"] = self.measure_mem
  74. val["ms"] = self.measure_swap
  75. return self.format.format(**val)