Skip to content

Commit

Permalink
fix: ctxswitch calculation for process with threads
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceChau authored and laurence.chau committed May 30, 2023
1 parent 935906b commit b5cfe39
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
39 changes: 30 additions & 9 deletions proc/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,28 @@ func (t *Tracker) handleProc(proc Proc, updateTime time.Time) (*IDInfo, CollectE
}
cerrs.Partial += softerrors

if len(threads) > 0 {
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = 0, 0
for _, thread := range threads {
metrics.Counts.CtxSwitchNonvoluntary += thread.Counts.CtxSwitchNonvoluntary
metrics.Counts.CtxSwitchVoluntary += thread.Counts.CtxSwitchVoluntary
metrics.States.Add(thread.States)
}
}

var newProc *IDInfo
if known {
if len(threads) > 0 {
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = last.metrics.CtxSwitchNonvoluntary, last.metrics.CtxSwitchVoluntary
ctxSwitchNonvoluntaryDiff, ctxSwitchVoluntaryDiff := uint64(0), uint64(0)
for _, thread := range threads {

if lastThread, knownThread := last.threads[thread.ThreadID]; knownThread {
ctxSwitchNonvoluntaryDiff +=
thread.Counts.CtxSwitchNonvoluntary - lastThread.accum.CtxSwitchNonvoluntary
ctxSwitchVoluntaryDiff +=
thread.Counts.CtxSwitchVoluntary - lastThread.accum.CtxSwitchVoluntary
} else {
ctxSwitchNonvoluntaryDiff += thread.Counts.CtxSwitchNonvoluntary
ctxSwitchVoluntaryDiff += thread.Counts.CtxSwitchVoluntary
}
metrics.States.Add(thread.States)

}
metrics.Counts.CtxSwitchNonvoluntary += ctxSwitchNonvoluntaryDiff
metrics.Counts.CtxSwitchVoluntary += ctxSwitchVoluntaryDiff
}
last.update(metrics, updateTime, &cerrs, threads)
} else {
static, err := proc.GetStatic()
Expand All @@ -272,6 +283,16 @@ func (t *Tracker) handleProc(proc Proc, updateTime time.Time) (*IDInfo, CollectE
}
return nil, cerrs
}

if len(threads) > 0 {
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = 0, 0
for _, thread := range threads {
metrics.Counts.CtxSwitchNonvoluntary += thread.Counts.CtxSwitchNonvoluntary
metrics.Counts.CtxSwitchVoluntary += thread.Counts.CtxSwitchVoluntary
metrics.States.Add(thread.States)
}
}

newProc = &IDInfo{procID, static, metrics, threads}
if t.debug {
log.Printf("found new proc: %s", newProc)
Expand Down
29 changes: 20 additions & 9 deletions proc/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ func TestTrackerThreads(t *testing.T) {
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 1, States{}, msi{}, nil},
}, {
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
{ThreadID(ID{p, 0}), "t1", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
{ThreadID(ID{p + 1, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
{ThreadID(ID{p, 0}), "t1", Counts{1, 2, 3, 4, 5, 6, 1, 1}, "", States{}},
{ThreadID(ID{p + 1, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 1, 1}, "", States{}},
}),
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
Update{n, Delta{0, 0, 0, 0, 0, 0, 2, 2}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
[]ThreadUpdate{
{"t1", Delta{}},
{"t2", Delta{}},
},
},
}, {
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
{ThreadID(ID{p + 1, 0}), "t2", Counts{2, 2, 2, 2, 2, 2, 0, 0}, "", States{}},
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 1, 1}, "", States{}},
{ThreadID(ID{p + 1, 0}), "t2", Counts{2, 2, 2, 2, 2, 2, 1, 1}, "", States{}},
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 1, 1}, "", States{}},
}),
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 3, States{}, msi{},
Update{n, Delta{0, 0, 0, 0, 0, 0, 1, 1}, Memory{}, Filedesc{1, 1}, tm, 3, States{}, msi{},
[]ThreadUpdate{
{"t1", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
{"t2", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
Expand All @@ -158,15 +158,26 @@ func TestTrackerThreads(t *testing.T) {
},
}, {
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 1, 1}, "", States{}},
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 1, 1}, "", States{}},
}),
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
[]ThreadUpdate{
{"t1", Delta{}},
{"t2", Delta{0, 1, 2, 3, 4, 5, 0, 0}},
},
},
}, {
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 2, 2}, "", States{}},
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 2, 2}, "", States{}},
}),
Update{n, Delta{0, 0, 0, 0, 0, 0, 2, 2}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
[]ThreadUpdate{
{"t1", Delta{0, 0, 0, 0, 0, 0, 1, 1}},
{"t2", Delta{0, 0, 0, 0, 0, 0, 1, 1}},
},
},
},
}
tr := NewTracker(newNamer(n), false, false, false)
Expand Down

0 comments on commit b5cfe39

Please sign in to comment.