From d830f574782cbd4e5a826fadb8c648877f73c428 Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Thu, 26 Mar 2020 10:23:03 +0100 Subject: [PATCH 1/2] Count usage of process start-up When a process started between two Update(), count the usage between process creation and the first Update() which see it. --- proc/tracker.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/proc/tracker.go b/proc/tracker.go index bfd89c6..19d602a 100644 --- a/proc/tracker.go +++ b/proc/tracker.go @@ -23,6 +23,9 @@ type ( // procIds is a map from pid to ProcId. This is a convenience // to allow finding the Tracked entry of a parent process. procIds map[int]ID + // firstUpdateAt is the time the first update was run. It allows to + // count first usage of a process started between to Update() + firstUpdateAt time.Time // trackChildren makes Tracker track descendants of procs the // namer wanted tracked. trackChildren bool @@ -164,6 +167,13 @@ func (t *Tracker) track(groupName string, idinfo IDInfo) { thr.ThreadName, thr.Counts, Delta{}, time.Time{}, thr.Wchan} } } + + // If the process started while Tracker was running, all current counter happened + // between the last Update() and the current Update() and should be counted. + if idinfo.StartTime.After(t.firstUpdateAt) { + tproc.lastaccum = Delta(tproc.metrics.Counts) + } + t.tracked[idinfo.ID] = &tproc } @@ -397,6 +407,10 @@ func (t *Tracker) lookupUid(uid int) string { // its metrics for existing tracked procs. Returns nonfatal errors // and the status of all tracked procs, or an error if fatal. func (t *Tracker) Update(iter Iter) (CollectErrors, []Update, error) { + if t.firstUpdateAt.IsZero() { + t.firstUpdateAt = time.Now() + } + newProcs, colErrs, err := t.update(iter) if err != nil { return colErrs, nil, err From 9c9d3a77f8ff104c960fbc6232968ef85a05e50a Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Wed, 6 May 2020 16:56:54 +0200 Subject: [PATCH 2/2] Fix typo Co-authored-by: ncabatoff --- proc/tracker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proc/tracker.go b/proc/tracker.go index 19d602a..ff34d0f 100644 --- a/proc/tracker.go +++ b/proc/tracker.go @@ -24,7 +24,7 @@ type ( // to allow finding the Tracked entry of a parent process. procIds map[int]ID // firstUpdateAt is the time the first update was run. It allows to - // count first usage of a process started between to Update() + // count first usage of a process started between two Update() calls firstUpdateAt time.Time // trackChildren makes Tracker track descendants of procs the // namer wanted tracked.