Debian Codegraph @

s_tui/sources/rapl_power_source.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Copyright (C) 2017-2026 Alex Manuskin, Maor Veitsman
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 """RaplPowerSource is a s-tui Source, used to gather power usage
19 information
20 """
21
22 from __future__ import annotations
23
24 import logging
25 import time
26
27 from s_tui.sources.rapl_read import get_power_reader
28 from s_tui.sources.source import Source
29
30
31 class RaplPowerSource(Source):
32 MICRO_JOULE_IN_JOULE = 1000000.0
33
34 def __init__(self):
35 Source.__init__(self)
36
37 self.name = "Power"
38 self.measurement_unit = "W"
39 self.pallet = (
40 "power light",
41 "power dark",
42 "power light smooth",
43 "power dark smooth",
44 )
45
46 self.reader = get_power_reader()
47 if not self.reader:
48 self.is_available = False
49 logging.debug("Power reading is not available")
50 return
51
52 self.last_probe_time = time.time()
53 self.last_probe = self.reader.read_power()
54 self.max_power = 1
55 self.last_measurement = [0.0] * len(self.last_probe)
56
57 multi_sensors = []
58 for item in self.last_probe:
59 name = item.label
60 sensor_count = multi_sensors.count(name)
61 multi_sensors.append(name)
62 name += "," + str(sensor_count)
63 self.available_sensors.append(name)
64
65 def update(self) -> None:
66 if not self.is_available:
67 return
68 if self.reader is None:
69 logging.warning("RAPL reader is not initialized")
70 return
71 try:
72 current_measurement_value = self.reader.read_power()
73 except OSError as e:
74 logging.warning("Failed to read RAPL power: %s", e)
75 return
76
77 if not current_measurement_value:
78 logging.warning("RAPL read_power() returned empty or None")
79 return
80
81 current_measurement_time = time.time()
82 seconds_passed = current_measurement_time - self.last_probe_time
83
84 if seconds_passed <= 0:
85 logging.warning(
86 "Non-positive time delta between RAPL measurements: %s",
87 seconds_passed,
88 )
89 return
90
91 for m_idx, _ in enumerate(self.last_probe):
92 try:
93 joule_used = (
94 current_measurement_value[m_idx].current
95 - self.last_probe[m_idx].current
96 ) / float(self.MICRO_JOULE_IN_JOULE)
97
98 logging.debug("seconds passed %s", seconds_passed)
99 watts_used = float(joule_used) / float(seconds_passed)
100 logging.debug("watts used %s", watts_used)
101 logging.info(
102 "Joule_Used %f, seconds passed, %f", joule_used, seconds_passed
103 )
104
105 if watts_used > 0:
106 # The information on joules used elapses every once in a
107 # while, this might lead to negative readings.
108 # To prevent this, we keep the last value until the next
109 # update
110 self.last_measurement[m_idx] = watts_used
111 logging.info("Power reading elapsed")
112 except (IndexError, AttributeError) as e:
113 logging.warning("Error reading RAPL sensor %d: %s", m_idx, e)
114
115 self.last_probe = current_measurement_value
116 self.last_probe_time = current_measurement_time
117
118 def get_maximum(self) -> float:
119 return self.max_power
120
121 def get_top(self) -> int:
122 return 1