1111from typing import Iterable , List , Union , Type , Sized
1212from carbontracker .loggerutil import Logger
1313import os
14+ from carbontracker .components .cpu .sim_cpu import SimulatedCPUHandler
15+ from carbontracker .components .gpu .sim_gpu import SimulatedGPUHandler
1416
1517COMPONENTS = [
1618 {
@@ -37,25 +39,64 @@ def error_by_name(name) -> Exception:
3739 raise exceptions .ComponentNameError ()
3840
3941
40- def handlers_by_name (name ) -> List [Type [Handler ]]:
42+ def handlers_by_name (
43+ name ,
44+ sim_cpu = None ,
45+ sim_cpu_tdp = None ,
46+ sim_cpu_util = None ,
47+ sim_gpu = None ,
48+ sim_gpu_watts = None ,
49+ sim_gpu_util = None
50+ ):
4151 for comp in COMPONENTS :
4252 if comp ["name" ] == name :
53+ if name == "cpu" and sim_cpu and sim_cpu_tdp :
54+ return [lambda pids , devices_by_pid : SimulatedCPUHandler (
55+ sim_cpu ,
56+ float (sim_cpu_tdp ),
57+ float (sim_cpu_util ) if sim_cpu_util is not None else 0.5
58+ )]
59+ elif name == "gpu" and sim_gpu and sim_gpu_watts :
60+ return [lambda pids , devices_by_pid : SimulatedGPUHandler (
61+ sim_gpu ,
62+ float (sim_gpu_watts ),
63+ float (sim_gpu_util ) if sim_gpu_util is not None else 0.5
64+ )]
4365 return comp ["handlers" ]
4466 raise exceptions .ComponentNameError ()
4567
4668
4769class Component :
48- def __init__ (self , name : str , pids : Iterable [int ], devices_by_pid : bool , logger : Logger ):
70+ def __init__ (
71+ self ,
72+ name : str ,
73+ pids : Iterable [int ],
74+ devices_by_pid : bool ,
75+ logger : Logger ,
76+ sim_cpu = None ,
77+ sim_cpu_tdp = None ,
78+ sim_cpu_util = None ,
79+ sim_gpu = None ,
80+ sim_gpu_watts = None ,
81+ sim_gpu_util = None
82+ ):
4983 self .name = name
5084 if name not in component_names ():
5185 raise exceptions .ComponentNameError (
5286 f"No component found with name '{ self .name } '."
5387 )
5488 self ._handler = self ._determine_handler (
55- pids = pids , devices_by_pid = devices_by_pid
89+ pids = pids ,
90+ devices_by_pid = devices_by_pid ,
91+ sim_cpu = sim_cpu ,
92+ sim_cpu_tdp = sim_cpu_tdp ,
93+ sim_cpu_util = sim_cpu_util ,
94+ sim_gpu = sim_gpu ,
95+ sim_gpu_watts = sim_gpu_watts ,
96+ sim_gpu_util = sim_gpu_util
5697 )
5798 self .power_usages : List [List [float ]] = []
58- self .cur_epoch : int = - 1 # Sentry
99+ self .cur_epoch : int = - 1
59100 self .logger = logger
60101
61102 @property
@@ -65,11 +106,27 @@ def handler(self) -> Handler:
65106 return self ._handler
66107
67108 def _determine_handler (
68- self , pids : Iterable [int ], devices_by_pid : bool
109+ self ,
110+ pids : Iterable [int ],
111+ devices_by_pid : bool ,
112+ sim_cpu = None ,
113+ sim_cpu_tdp = None ,
114+ sim_cpu_util = None ,
115+ sim_gpu = None ,
116+ sim_gpu_watts = None ,
117+ sim_gpu_util = None
69118 ) -> Union [Handler , None ]:
70- handlers = handlers_by_name (self .name )
119+ handlers = handlers_by_name (
120+ self .name ,
121+ sim_cpu = sim_cpu ,
122+ sim_cpu_tdp = sim_cpu_tdp ,
123+ sim_cpu_util = sim_cpu_util ,
124+ sim_gpu = sim_gpu ,
125+ sim_gpu_watts = sim_gpu_watts ,
126+ sim_gpu_util = sim_gpu_util
127+ )
71128 for h in handlers :
72- handler = h (pids = pids , devices_by_pid = devices_by_pid )
129+ handler = h (pids = pids , devices_by_pid = devices_by_pid ) if callable ( h ) else h ( pids = pids , devices_by_pid = devices_by_pid )
73130 if handler .available ():
74131 return handler
75132 return None
@@ -158,16 +215,47 @@ def shutdown(self):
158215
159216
160217def create_components (
161- components : str , pids : Iterable [int ], devices_by_pid : bool , logger : Logger
218+ components : str ,
219+ pids : Iterable [int ],
220+ devices_by_pid : bool ,
221+ logger : Logger ,
222+ sim_cpu = None ,
223+ sim_cpu_tdp = None ,
224+ sim_cpu_util = None ,
225+ sim_gpu = None ,
226+ sim_gpu_watts = None ,
227+ sim_gpu_util = None
162228) -> List [Component ]:
163229 components = components .strip ().replace (" " , "" ).lower ()
164230 if components == "all" :
165231 return [
166- Component (name = comp_name , pids = pids , devices_by_pid = devices_by_pid , logger = logger )
232+ Component (
233+ name = comp_name ,
234+ pids = pids ,
235+ devices_by_pid = devices_by_pid ,
236+ logger = logger ,
237+ sim_cpu = sim_cpu ,
238+ sim_cpu_tdp = sim_cpu_tdp ,
239+ sim_cpu_util = sim_cpu_util ,
240+ sim_gpu = sim_gpu ,
241+ sim_gpu_watts = sim_gpu_watts ,
242+ sim_gpu_util = sim_gpu_util
243+ )
167244 for comp_name in component_names ()
168245 ]
169246 else :
170247 return [
171- Component (name = comp_name , pids = pids , devices_by_pid = devices_by_pid , logger = logger )
248+ Component (
249+ name = comp_name ,
250+ pids = pids ,
251+ devices_by_pid = devices_by_pid ,
252+ logger = logger ,
253+ sim_cpu = sim_cpu ,
254+ sim_cpu_tdp = sim_cpu_tdp ,
255+ sim_cpu_util = sim_cpu_util ,
256+ sim_gpu = sim_gpu ,
257+ sim_gpu_watts = sim_gpu_watts ,
258+ sim_gpu_util = sim_gpu_util
259+ )
172260 for comp_name in components .split ("," )
173261 ]
0 commit comments