1# events.json
2
3This file defines the events that dictate how fan control operates.  Each event
4can contain groups, triggers, and actions.
5
6Actions are where fan targets are calculated and set, among other things.
7Triggers specify when an action should run. Groups specify which D-Bus objects
8the triggers and actions should operate on.
9
10Some actions have modifiers, which help calculate a value.
11
12- [Groups](#groups)
13- [Triggers](#triggers)
14- [Actions](#actions)
15- [Modifiers](#modifiers)
16
17## Example
18
19```
20[
21   {
22     "name": "fan(s) missing",
23     "groups": [
24       {
25         "name": "fan inventory",
26         "interface": "xyz.openbmc_project.Inventory.Item",
27         "property": { "name": "Present" }
28       }
29     ],
30     "triggers": [
31       {
32         "class": "init",
33         "method": "get_properties"
34       },
35       {
36         "class": "signal",
37         "signal": "properties_changed"
38       }
39     ],
40     "actions": [
41       {
42         "name": "count_state_before_target",
43         "count": 1,
44         "state": false,
45         "target": 15000
46       }
47     ]
48   }
49]
50```
51
52The above event is an example of a method to set the fan target to 15000 when
53one or more fans is missing. The basic behavior with this config is:
54
55The trigger `init.get_properties` will, on fan control startup, read the
56Present property on the `xyz.openbmc_project.Inventory.Item` interface from
57every D-Bus object in the 'fan inventory' group, update those values in the
58object cache, and then call the `count_state_before_target` action.
59
60The trigger `signal.properties_changed` will watch for changes on the Present
61property on each D-Bus object in the group, update the new value in the object
62cache, and call the `count_state_before_target` action when the value changes.
63
64The `count_state_before_target` action will look at the object cache value of
65the Present property on each member of the group and set the fan target hold to
6615000 when one or more of them is false.  Otherwise, it will clear its fan
67target hold.
68
69## Groups
70
71```
72"groups": [
73    {
74        "name": "<name>",
75        "interface": "<interface>",
76        "property": { "name": "<name>" }
77    }
78    ...
79]
80```
81
82### name
83The name of a group that must be also be defined in
84[groups.json](groups.md).
85
86### interface
87The actions and triggers defined with this group will look at this D-Bus
88interface on the members of this group.
89
90### property: name
91The actions and triggers defined with this group will look at this D-Bus
92property on the members of this group.
93
94## Triggers
95
96There are several classes of triggers, and the JSON configuration is different
97for each.
98
99### init
100Init triggers run when fan control events are enabled on fan control startup.
101After invoking the configured method, any actions configured for this trigger
102will run.
103
104```
105{
106    "class": "init",
107    "method": "<method>"
108}
109```
110
111#### methods
112
1131. `get_properties` - Read the property configured for the group from every
114   member of the group, and store it in fan control's object cache.
115
1162. `name_has_owner` - Populates the service owned state from D-Bus for each
117   group member in fan control's D-Bus service cache map.
118
119### signal
120Signal triggers subscribe to certain D-Bus signals for each member of its
121configured group.  After handling the signal, any configured actions are run.
122
123```
124{
125    "class": "signal",
126    "signal": "<signal>"
127}
128```
129
130#### signal types
1311. `properties_changed` - Subscribes to the PropertiesChanged signal for the
132   D-Bus interface and property specified in the group definition for each
133   group member.  When the signal occurs, the new property value will be added
134   to or updated in the object cache.
135
1362. `interfaces_added` - Subscribes to the InterfacesAdded signal for the D-Bus
137   interface specified in the group definition for each group member.  When the
138   signal occurs, the interface and its properties will be added to the object
139   cache.
140
1413. `interfaces_removed` - Subscribes to the InterfacesRemoved signal for the
142   D-Bus interface specified in the group definition for each group member.
143   When the signal occurs, the interface and properties will be removed from
144   the object cache.
145
1464. `name_owner_changed` - Subscribes to the NameOwnerChanged signal for the
147   services that host the D-bus interface specified in the group definition for
148   each group member.  When the signal occurs, the service owned state will be
149   updated in the service cache map.
150
1515. `member` - Subscribes to the signal listed on each group member.  No caches
152   are updated when the signal occurs.
153
154### timer
155Timer triggers run actions after the configured type of timer expires.
156
157```
158{
159    "class": "timer",
160    "type": "<type>",
161    "interval": "<interval>",
162    "preload_groups": "<true/false>"
163}
164```
165
166#### type
1671. `oneshot` - Starts a timer that runs once.
168
1692. `repeating` - Starts a repeating timer.
170
171#### interval
172The timer length in microseconds
173
174#### preload_groups
175Optional, if set to true, will update the D-Bus properties from the configured
176groups in the object cache after the timer expires but before any actions run.
177
178### parameter
179Parameter triggers run actions after a parameter changes.
180
181```
182{
183    "class": "parameter",
184    "parameter": "<parameter>"
185}
186```
187#### parameter
188The parameter value to watch.
189
190### poweron
191PowerOn triggers run when the power turns on.  Functionally, they behave like
192an init trigger.
193
194```
195{
196    "class": "poweron",
197    "method": "<method>"
198}
199```
200
201#### method
202The methods are the same as with the init trigger.
203
204### poweroff
205
206PowerOff triggers run when the power turns off.  Functionally, they behave like
207an init trigger.
208
209```
210{
211    "class": "poweroff",
212    "method": "<method>"
213}
214```
215
216#### method
217The methods are the same as with the init trigger.
218
219## Actions
220Actions can either operate on the groups listed with the event, or on the
221groups listed within the action's JSON config.
222
223Most actions set fan targets or floors.  This can be done by requesting a
224target value explicitly, or by requesting an increase or decrease delta.
225Targets and floors can also be set with a hold, meaning another action can't
226set a floor/target below the one that is held.
227
228Some actions set or read a key/value pair called a parameter.  These can be
229created, updated, and deleted as necessary.  For example, one action may
230calculate and set a `floor_index` parameter, and another action may then read
231that parameter to help choose a fan floor.
232
233The available actions are:
234
235- [set_net_increase_target](#set_net_increase_target)
236- [set_net_decrease_target](#set_net_decrease_target)
237- [count_state_floor](#count_state_floor)
238- [count_state_before_target](#count_state_before_target)
239- [default_floor_on_missing_owner](#default_floor_on_missing_owner)
240- [mapped_floor](#mapped_floor)
241- [set_target_on_missing_owner](#set_target_on_missing_owner)
242- [override_fan_target](#override_fan_target)
243- [pcie_card_floors](#pcie_card_floors)
244- [set_request_target_base_with_max](#set_request_target_base_with_max)
245- [set_parameter_from_group_max](#set_parameter_from_group_max)
246- [call_actions_based_on_timer](#call_actions_based_on_timer)
247- [get_managed_objects](#get_managed_objects)
248
249### set_net_increase_target
250
251Calculates the net target increase to be requested based on the value of each
252property given within a group. The net target increase is based on the maximum
253difference between the `delta` JSON value and all of the properties of the group.
254The final result is the increase change that's requested to the current target
255of a zone.
256
257The group values can be compared to either a value hardcoded in the JSON, or a
258parameter value.
259
260```
261{
262    "name": "set_net_increase_target",
263    "groups": [{
264        "name": "pcie temps",
265        "interface": "xyz.openbmc_project.Sensor.Value",
266        "property": { "name": "Value" }
267      }],
268    "state": 70.0,
269    "delta": 255
270}
271```
272
273The above config uses a hardcoded state value:
2741. For each member of the 'pcie temps' group:
275  - Read its 'Value' D-Bus property.
276  - If that property value is greater than the 'state' value of 70.0:
277   - Subtracts 70.0 from the property value.
278   - Multiplies that difference by the 'delta' value of 255.
2792. Requests an increase of the largest calculated delta value, if there is
280   one.
281
282```
283{
284    "name": "set_net_increase_target",
285    "groups": [{
286        "name": "proc0 core temps",
287        "interface": "xyz.openbmc_project.Sensor.Value",
288        "property": { "name": "Value" }
289      }],
290    "state_parameter_name": "proc_0_core_dvfs_increase_temp",
291    "delta": 300
292}
293```
294
295The above config uses a parameter as the state value:
2961. For each member of the 'proc 0 core temps' group:
297  - Read its 'Value' D-Bus property.
298  - If that property value is greater than the value of the parameter listed in
299    the 'state_parameter_name' field, in this case
300    'proc_0_core_dvfs_increase_temp':
301   - Subtracts that parameter value from the property value.
302   - Multiplies that difference by the 'delta' value of 300.
3032. Requests an increase of the largest calculated delta value, if there is one.
304
305### set_net_decrease_target
306Calculates the net target decrease to be requested based on the value of each
307property given within a group. The net target decrease is based on the minimum
308difference between the `delta` JSON value and all properties in the group. The
309final result is the decrease change that's requested to the current target of a
310zone.
311
312The group values can be compared to either a value hardcoded in the JSON, or a
313parameter value.
314
315```
316{
317    "name": "set_net_decrease_target",
318    "groups": [{
319        "name": "pcie temps",
320        "interface": "xyz.openbmc_project.Sensor.Value",
321        "property": { "name": "Value" }
322      }],
323    "state": 65.0,
324    "delta": 80
325}
326
327```
328
329The above config uses a hardcoded state value:
3301. For each member of the 'pcie temps' group:
331  - Read its 'Value' D-Bus property.
332  - If that property value is less than the 'state' value of 65.0:
333   - Subtracts the property value from 65.0.
334   - Multiplies that difference by the 'delta' value of 80.
3352. Requests a decrease of the smallest calculated delta value, if there is
336   one.
337
338```
339{
340    "name": "set_net_decrease_target",
341    "groups": [{
342        "name": "proc 0 core temps",
343        "interface": "xyz.openbmc_project.Sensor.Value",
344        "property": { "name": "Value" }
345      }],
346    "state_parameter_name": "proc_0_core_dvfs_decrease_temp",
347    "delta": 50
348}
349```
350
351The above config uses a parameter as the state value:
3521. For each member of the 'proc 0 core temps' group:
353  - Read its 'Value' D-Bus property.
354  - If that property value is less than the value of the parameter listed
355    the 'state_parameter_name' field, in this case
356    'proc_0_core_dvfs_decrease_temp':
357   - Subtracts the property value from the parameter value.
358   - Multiplies that difference by the 'delta' value of 50.
3592. Requests a decrease of the smallest calculated delta value, if there is
360   one.
361
362### count_state_floor
363 Sets the fans to a configured floor when a number of members within the
364 group are at a configured state. Once the number of members at the given
365 state falls below the configured count, the floor hold is released.
366
367```
368{
369    "name": "count_state_floor",
370    "count": 2,
371    "state": false,
372    "floor": 18000
373}
374```
375
376The above config reads the configured D-Bus property on each group member
377configured for the action.  If two or more members have a property value of
378false, a floor hold will be requested with a value of 18000.  Otherwise, the
379floor hold will be released (if it was previously requested).
380
381### count_state_before_target
382Sets the fans to a configured target when a number of members within the group
383are at a configured state. Once the number of members at the given state falls
384below the configured count, active fan target changes are allowed.
385
386```
387{
388    "name": "count_state_before_target",
389    "count": 1,
390    "state": false,
391    "target": 18000
392}
393```
394
395The above config reads the configured D-Bus property on each group member
396configured for the action.  If one or more members have a property value of
397false, a target hold will be requested with a value of 18000.  Otherwise, the
398hold will be released (if it was previously requested).
399
400### default_floor_on_missing_owner
401Sets the fan floor to the defined zone's default fan floor when a service
402associated to a given group has terminated. Once all services are functional
403and providing the sensors, the fan floor is allowed to be set normally again.
404
405There is no additional JSON config for this action.
406
407### mapped_floor
408This action can be used to set a floor value based on 2 or more groups
409having values within certain ranges, where the key group chooses the set
410of tables in which to check the remaining group values.
411
412```
413{
414  "name": "mapped_floor",
415  "key_group": "ambient temp",
416  "default_floor": 5555,
417  "fan_floors": [
418   {
419     "key": 25,
420     "default_floor": 4444,
421     "floor_offset_parameter": "ambient_25_altitude_offset",
422     "floors": [
423       {
424         "parameter": "pcie_floor_index",
425         "floors": [
426           { "value": 1, "floor": 2000 },
427           { "value": 2, "floor": 3000 },
428           { "value": 3, "floor": 4000 },
429           { "value": 4, "floor": 5000 },
430           { "value": 5, "floor": 6000 }
431         ]
432       },
433       {
434         "group": "power save",
435         "floors": [
436            { "value": true, "floor": 1000 }
437         ]
438       }
439     ]
440   }
441}
442```
443
444The above config will use the maximum value of the 'ambient temp' group as the
445key into the 'fan_floors' tables.  There is one of those tables listed, and it
446will be used when the key group has a max value of less than 25.
447
448It will then traverse the contained floors arrays, keeping track of the highest
449valid floor value it finds.  If it doesn't find any valid floor values, it will
450use the `default_floor` value of 4444, though that value is optional.
451
452If no valid tables were found given the key value, the `default_floor` value of
4535555 would be used, though that is optional and if not supplied the code would
454default to the default floor of the zone.
455
456At the end of the analysis, a floor hold will be set with the final floor
457value.
458
459### set_target_on_missing_owner
460Sets the fans to a configured target when any service owner associated to the
461group is missing. Once all services are functional and providing all the
462group data again, active fan target changes are allowed.
463
464```
465{
466    "name": "set_target_on_missing_owner",
467    "groups": [{
468        "name": "fan inventory",
469        "interface": "xyz.openbmc_project.Inventory.Item",
470        "property": { "name": "Present" }
471      }],
472    "target": 18000
473}
474```
475
476The above config will set a target hold of 18000 when the service associated
477with the 'fan inventory' group is lost.
478
479### override_fan_target
480This action locks fans at configured targets when the configured `count` amount
481of fans meet criterion for the particular condition. A locked fan maintains its
482override target until unlocked (or locked at a higher target). Upon unlocking,
483it will either revert to temperature control or activate the next-highest
484target remaining in its list of locks.
485
486```
487{
488    "name": "override_fan_target",
489    "count": 1,
490    "state": false,
491    "fans": [ "fan0", "fan1", "fan2", "fan3" ],
492    "target": 10000
493}
494```
495
496The above config will lock all fans in the fans array at a target of 10000 when
497one or more members in its configured group have a group property value of
498false.
499
500This could be used for example, to lock the rotors of a multirotor fan to a
501high target when one of its rotors has a functional property equal to false.
502
503### pcie_card_floors
504Sets the `pcie_floor_index` parameter based on the current configuration of
505plugged and powered on PCIe cards, using data from the `pcie_cards.json` file.
506
507It chooses the highest index from the active PCIe cards to set in the
508parameter.
509
510It must be configured with the following groups and properties:
511- The PCIe slots with the PowerState property
512- The PCIe cards with the following properties: Function0DeviceId,
513  Function0VendorId, Function0SubsystemId, Function0SubsystemVendorId
514
515```
516{
517    "name": "pcie_card_floors",
518    "use_config_specific_files": true,
519    "settle_time": 2
520}
521```
522
523The `use_config_specific_files` field tells the code to look for the
524'pcie_cards.json' files in the same system specific directories as
525'events.json'.  If missing or false, looks in the base fan control directory.
526
527The `settle_time` field is the amount of time in seconds that needs to pass
528without a call to run() from a group property value changing.  As the
529PCIeDevice attributes are written close together by the host, this allows the
530action to wait until the writes are done before selecting the index.
531
532Additional details are in the [header
533file](../../control/json/actions/pcie_card_floors.hpp)
534
535### set_request_target_base_with_max
536Determines the maximum value from the properties of the group of D-Bus objects
537and sets the requested target base to this value. Only positive integer or
538floating point types are supported as these are the only valid types for a fan
539target to be based off of.
540
541The `requested target base` value is the base value to apply a target delta
542to.  By default, it's the current zone target unless modified by this action.
543
544```
545{
546    "name": "set_request_target_base_with_max",
547    "groups": [{
548        "name": "fan targets",
549        "interface": "xyz.openbmc_project.Fan.Target",
550        "property": { "name": "Target" }
551      }]
552}
553```
554
555The above config will set the requested target base to the maximum Target
556property value of all members of the 'fan targets' group.
557
558### set_parameter_from_group_max
559Sets a parameter value based on the maximum group property value.  The property
560value can be modified before storing it if the JSON specifies a valid modifier
561expression.
562
563 ```
564{
565    "name": "set_parameter_from_group_max",
566    "parameter_name": "proc_0_throttle_temp",
567    "modifier": {
568      "expression": "minus",
569      "value": 4
570    }
571}
572 ```
573
574The above config will first find the max of its groups property values,
575subtract 4, and then store the resulting value in the `proc_0_throttle_temp`
576parameter.
577
578### call_actions_based_on_timer
579This action starts and stops a timer that runs a list of actions whenever the
580timer expires.  A timer can be either `oneshot` or `repeating`.
581
582When all groups have a configured value to compare against, that will
583be compared against all members within each group to start/stop the
584timer. When all group members have a given value and it matches what's
585in the cache, the timer is started and if any do not match, the timer is
586stopped.
587
588When any group does not have a configured value to be compared against,
589the groups' service owned state is used to start/stop the timer. When
590any service providing a group member is not owned, the timer is started
591and if all members' services are owned, the timer is stopped.
592
593Consider the following action config:
594```
595{
596    "name": "call_actions_based_on_timer",
597    "timer": {
598        "interval": 5000000,
599        "type": "oneshot"
600    },
601    "actions": [{
602        "name": "test"
603    }]
604}
605
606```
607If its group configuration has a property value listed, like:
608
609```
610{
611    "name": "fan inventory",
612    "interface": "xyz.openbmc_project.Inventory.Item",
613    "property": { "name": "Present", "value": true }
614}
615```
616Then a oneshot timer of 5000000us will be started when every member of the fan
617inventory group has a value of true.  Otherwise, the timer will be stopped if
618it's running.
619
620If the group configuration has no property value listed, like:
621
622```
623{
624    "name": "fan inventory",
625    "interface": "xyz.openbmc_project.Inventory.Item",
626    "property": { "name": "Present" }
627}
628```
629Then the timer will be started when any service providing a group member isn't
630owned (on D-Bus).  Otherwise, it will stop the timer if it's running.
631
632### get_managed_objects
633This action adds the members of its groups to the object cache by using the
634GetManagedObjects D-Bus method to find and add the results.  When that is done,
635it then runs any actions listed in the JSON.
636
637This allows an action to run with the latest values in the cache without having
638to subscribe to propertiesChanged for them all.
639
640```
641{
642   "name": "get_managed_objects",
643   "groups": [
644     {
645       "name": "proc temps",
646       "interface": "xyz.openbmc_project.Sensor.Value",
647       "property": { "name": "Value" }
648     }
649   ],
650   "actions": [
651     {
652       "name": "set_net_increase_target",
653       "state": 30,
654       "delta": 100
655     }
656   ]
657}
658```
659
660The above config will make the GetManagedObjects call on all services that own
661the configured groups and then add all resulting property values to the object
662cache.  After that, it will call the `set_net_increase_target` action using the
663same groups.
664
665