@@ -49,9 +49,21 @@ def init_devices(self):
4949 self ._init_devices ()
5050 self ._devices_inited = True
5151
52- @property
53- def name (self ):
54- return self .__class__ .__module__ .split ("." )[- 1 ].split ("_" , 1 )[1 ]
52+ @classmethod
53+ def name (cls ):
54+ return cls .__module__ .split ("." )[- 1 ].split ("_" , 1 )[1 ]
55+
56+ @classmethod
57+ def supports_static_tuning (cls ):
58+ raise NotImplementedError ()
59+
60+ @classmethod
61+ def supports_dynamic_tuning (cls ):
62+ raise NotImplementedError ()
63+
64+ @classmethod
65+ def uses_periodic_tuning (cls ):
66+ raise NotImplementedError ()
5567
5668 #
5769 # Plugin configuration manipulation and helpers.
@@ -74,8 +86,10 @@ def _get_config_options_used_by_dynamic(self):
7486
7587 def _get_effective_options (self , options ):
7688 """Merge provided options with plugin default options."""
77- # TODO: _has_dynamic_options is a hack
7889 effective = self ._get_config_options ().copy ()
90+ if self .supports_dynamic_tuning ():
91+ effective ["dynamic" ] = True
92+ # TODO: _has_dynamic_options is a hack
7993 for key in options :
8094 if key in effective or self ._has_dynamic_options :
8195 effective [key ] = options [key ]
@@ -118,13 +132,13 @@ def destroy_instance(self, instance):
118132
119133 def initialize_instance (self , instance ):
120134 """Initialize an instance."""
121- log .debug ("initializing instance %s (%s)" % (instance .name , self .name ))
135+ log .debug ("initializing instance %s (%s)" % (instance .name , self .name () ))
122136 self ._instance_init (instance )
123137
124138 def destroy_instances (self ):
125139 """Destroy all instances."""
126140 for instance in list (self ._instances .values ()):
127- log .debug ("destroying instance %s (%s)" % (instance .name , self .name ))
141+ log .debug ("destroying instance %s (%s)" % (instance .name , self .name () ))
128142 self ._destroy_instance (instance )
129143 self ._instances .clear ()
130144
@@ -133,10 +147,13 @@ def _destroy_instance(self, instance):
133147 self ._instance_cleanup (instance )
134148
135149 def _instance_init (self , instance ):
136- raise NotImplementedError ()
150+ instance ._static_tuning_enabled = self .supports_static_tuning ()
151+ instance ._dynamic_tuning_enabled = self .supports_dynamic_tuning () \
152+ and self .__class__ in self ._global_cfg .get (consts .CFG_DYNAMIC_PLUGINS ) \
153+ and self ._option_bool (instance .options ["dynamic" ])
137154
138155 def _instance_cleanup (self , instance ):
139- raise NotImplementedError ()
156+ pass
140157
141158 #
142159 # Devices handling
@@ -158,7 +175,7 @@ def _get_matching_devices(self, instance, devices):
158175 else :
159176 udev_devices = self ._get_device_objects (devices )
160177 if udev_devices is None :
161- log .error ("Plugin '%s' does not support the 'devices_udev_regex' option" , self .name )
178+ log .error ("Plugin '%s' does not support the 'devices_udev_regex' option" , self .name () )
162179 return set ()
163180 udev_devices = self ._device_matcher_udev .match_list (instance .devices_udev_regex , udev_devices )
164181 return set ([x .sys_name for x in udev_devices ])
@@ -174,8 +191,8 @@ def assign_free_devices(self, instance):
174191 log .warn ("instance %s: no matching devices available" % instance .name )
175192 else :
176193 name = instance .name
177- if instance .name != self .name :
178- name += " (%s)" % self .name
194+ if instance .name != self .name () :
195+ name += " (%s)" % self .name ()
179196 log .info ("instance %s: assigning devices %s" % (name , ", " .join (to_assign )))
180197 instance .assigned_devices .update (to_assign ) # cannot use |=
181198 self ._assigned_devices |= to_assign
@@ -254,15 +271,15 @@ def instance_apply_tuning(self, instance):
254271 if not instance .active :
255272 return
256273
257- if instance .has_static_tuning :
274+ if instance .static_tuning_enabled :
258275 self ._call_device_script (instance , instance .script_pre ,
259276 "apply" , instance .assigned_devices )
260277 self ._instance_pre_static (instance , True )
261278 self ._instance_apply_static (instance )
262279 self ._instance_post_static (instance , True )
263280 self ._call_device_script (instance , instance .script_post ,
264281 "apply" , instance .assigned_devices )
265- if instance .has_dynamic_tuning and self . _global_cfg . get ( consts . CFG_DYNAMIC_TUNING , consts . CFG_DEF_DYNAMIC_TUNING ) :
282+ if instance .dynamic_tuning_enabled :
266283 self ._instance_init_dynamic (instance )
267284 self ._run_for_each_device (instance , self ._instance_apply_dynamic , instance .assigned_devices )
268285 instance .processed_devices .update (instance .assigned_devices )
@@ -279,7 +296,7 @@ def instance_verify_tuning(self, instance, ignore_missing):
279296 log .error ("BUG: Some devices have not been tuned: %s"
280297 % ", " .join (instance .assigned_devices ))
281298 devices = instance .processed_devices .copy ()
282- if instance .has_static_tuning :
299+ if instance .static_tuning_enabled :
283300 if self ._call_device_script (instance , instance .script_pre , "verify" , devices ) == False :
284301 return False
285302 if self ._instance_verify_static (instance , ignore_missing , devices ) == False :
@@ -296,7 +313,7 @@ def instance_update_tuning(self, instance):
296313 """
297314 if not instance .active :
298315 return
299- if instance .has_dynamic_tuning and self ._global_cfg . get ( consts . CFG_DYNAMIC_TUNING , consts . CFG_DEF_DYNAMIC_TUNING ):
316+ if instance .dynamic_tuning_enabled and self .uses_periodic_tuning ( ):
300317 self ._run_for_each_device (instance , self ._instance_update_dynamic , instance .processed_devices .copy ())
301318
302319 def instance_unapply_tuning (self , instance , rollback = consts .ROLLBACK_SOFT ):
@@ -306,9 +323,10 @@ def instance_unapply_tuning(self, instance, rollback = consts.ROLLBACK_SOFT):
306323 if rollback == consts .ROLLBACK_NONE :
307324 return
308325
309- if instance .has_dynamic_tuning and self . _global_cfg . get ( consts . CFG_DYNAMIC_TUNING , consts . CFG_DEF_DYNAMIC_TUNING ) :
326+ if instance .dynamic_tuning_enabled :
310327 self ._run_for_each_device (instance , self ._instance_unapply_dynamic , instance .processed_devices )
311- if instance .has_static_tuning :
328+ self ._instance_deinit_dynamic (instance )
329+ if instance .static_tuning_enabled :
312330 self ._call_device_script (instance , instance .script_post ,
313331 "unapply" , instance .processed_devices ,
314332 rollback = rollback )
@@ -337,14 +355,17 @@ def _instance_unapply_static(self, instance, rollback = consts.ROLLBACK_SOFT):
337355 def _instance_init_dynamic (self , instance ):
338356 pass
339357
358+ def _instance_deinit_dynamic (self , instance ):
359+ pass
360+
340361 def _instance_apply_dynamic (self , instance , device ):
341362 for option in [opt for opt in self ._options_used_by_dynamic if self ._storage_get (instance , self ._commands [opt ], device ) is None ]:
342363 self ._check_and_save_value (instance , self ._commands [option ], device )
343-
344- self ._instance_update_dynamic (instance , device )
364+ if self . uses_periodic_tuning ():
365+ self ._instance_update_dynamic (instance , device )
345366
346367 def _instance_unapply_dynamic (self , instance , device ):
347- raise NotImplementedError ()
368+ pass
348369
349370 def _instance_update_dynamic (self , instance , device ):
350371 raise NotImplementedError ()
0 commit comments