Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ctxswitch calculation for process with threads #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions proc/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,26 @@ 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 +281,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