Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 46c9557

Browse files
author
Dongsu Park
committed
systemd: avoid iteration into unit list using UnitState
To avoid an additional iteration of unit states for each unit, set UnitState.ActiveEnterTimestamp accordingly, by calling m.systemd.GetUnitProperty(). Note that we need to retrieve the property not only in the normal loop for the current systemd (>=230), but also in the fallback handling for the older version of systemd (<=229).
1 parent 664c8d1 commit 46c9557

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

systemd/manager.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ func (m *systemdUnitManager) getUnitState(name string) (*unit.UnitState, error)
174174
return nil, err
175175
}
176176
us := unit.UnitState{
177-
LoadState: info["LoadState"].(string),
178-
ActiveState: info["ActiveState"].(string),
179-
SubState: info["SubState"].(string),
177+
LoadState: info["LoadState"].(string),
178+
ActiveState: info["ActiveState"].(string),
179+
SubState: info["SubState"].(string),
180+
ActiveEnterTimestamp: info["ActiveEnterTimestamp"].(uint64),
180181
}
181182
return &us, nil
182183
}
@@ -238,6 +239,9 @@ func (m *systemdUnitManager) GetUnitStates(filter pkg.Set) (map[string]*unit.Uni
238239
if h, ok := m.hashes[dus.Name]; ok {
239240
us.UnitHash = h.String()
240241
}
242+
243+
us.ActiveEnterTimestamp = m.getActiveEnterTimestamp(dus.Name)
244+
241245
states[dus.Name] = us
242246
}
243247

@@ -256,19 +260,11 @@ func (m *systemdUnitManager) GetUnitStates(filter pkg.Set) (map[string]*unit.Uni
256260
if h, ok := m.hashes[name]; ok {
257261
us.UnitHash = h.String()
258262
}
259-
states[name] = us
260-
}
261-
}
262263

263-
// add Active enter time to UnitState
264-
for name, us := range states {
265-
prop, err := m.systemd.GetUnitProperty(name, "ActiveEnterTimestamp")
266-
if err != nil {
267-
return nil, err
268-
}
264+
us.ActiveEnterTimestamp = m.getActiveEnterTimestamp(name)
269265

270-
us.ActiveEnterTimestamp = prop.Value.Value().(uint64)
271-
states[name] = us
266+
states[name] = us
267+
}
272268
}
273269

274270
return states, nil
@@ -328,6 +324,14 @@ func (m *systemdUnitManager) getUnitFilePath(name string) string {
328324
return path.Join(m.unitsDir, name)
329325
}
330326

327+
func (m *systemdUnitManager) getActiveEnterTimestamp(name string) (aTimestamp uint64) {
328+
prop, err := m.systemd.GetUnitProperty(name, "ActiveEnterTimestamp")
329+
if err != nil {
330+
return 0
331+
}
332+
return prop.Value.Value().(uint64)
333+
}
334+
331335
func lsUnitsDir(dir string) ([]string, error) {
332336
filterFunc := func(name string) bool {
333337
if !unit.RecognizedUnitType(name) {

0 commit comments

Comments
 (0)