Skip to content

Commit b5cfe39

Browse files
LaurenceChaulaurence.chau
authored andcommitted
fix: ctxswitch calculation for process with threads
1 parent 935906b commit b5cfe39

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

proc/tracker.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,28 @@ func (t *Tracker) handleProc(proc Proc, updateTime time.Time) (*IDInfo, CollectE
252252
}
253253
cerrs.Partial += softerrors
254254

255-
if len(threads) > 0 {
256-
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = 0, 0
257-
for _, thread := range threads {
258-
metrics.Counts.CtxSwitchNonvoluntary += thread.Counts.CtxSwitchNonvoluntary
259-
metrics.Counts.CtxSwitchVoluntary += thread.Counts.CtxSwitchVoluntary
260-
metrics.States.Add(thread.States)
261-
}
262-
}
263-
264255
var newProc *IDInfo
265256
if known {
257+
if len(threads) > 0 {
258+
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = last.metrics.CtxSwitchNonvoluntary, last.metrics.CtxSwitchVoluntary
259+
ctxSwitchNonvoluntaryDiff, ctxSwitchVoluntaryDiff := uint64(0), uint64(0)
260+
for _, thread := range threads {
261+
262+
if lastThread, knownThread := last.threads[thread.ThreadID]; knownThread {
263+
ctxSwitchNonvoluntaryDiff +=
264+
thread.Counts.CtxSwitchNonvoluntary - lastThread.accum.CtxSwitchNonvoluntary
265+
ctxSwitchVoluntaryDiff +=
266+
thread.Counts.CtxSwitchVoluntary - lastThread.accum.CtxSwitchVoluntary
267+
} else {
268+
ctxSwitchNonvoluntaryDiff += thread.Counts.CtxSwitchNonvoluntary
269+
ctxSwitchVoluntaryDiff += thread.Counts.CtxSwitchVoluntary
270+
}
271+
metrics.States.Add(thread.States)
272+
273+
}
274+
metrics.Counts.CtxSwitchNonvoluntary += ctxSwitchNonvoluntaryDiff
275+
metrics.Counts.CtxSwitchVoluntary += ctxSwitchVoluntaryDiff
276+
}
266277
last.update(metrics, updateTime, &cerrs, threads)
267278
} else {
268279
static, err := proc.GetStatic()
@@ -272,6 +283,16 @@ func (t *Tracker) handleProc(proc Proc, updateTime time.Time) (*IDInfo, CollectE
272283
}
273284
return nil, cerrs
274285
}
286+
287+
if len(threads) > 0 {
288+
metrics.Counts.CtxSwitchNonvoluntary, metrics.Counts.CtxSwitchVoluntary = 0, 0
289+
for _, thread := range threads {
290+
metrics.Counts.CtxSwitchNonvoluntary += thread.Counts.CtxSwitchNonvoluntary
291+
metrics.Counts.CtxSwitchVoluntary += thread.Counts.CtxSwitchVoluntary
292+
metrics.States.Add(thread.States)
293+
}
294+
}
295+
275296
newProc = &IDInfo{procID, static, metrics, threads}
276297
if t.debug {
277298
log.Printf("found new proc: %s", newProc)

proc/tracker_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,22 @@ func TestTrackerThreads(t *testing.T) {
134134
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 1, States{}, msi{}, nil},
135135
}, {
136136
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
137-
{ThreadID(ID{p, 0}), "t1", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
138-
{ThreadID(ID{p + 1, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
137+
{ThreadID(ID{p, 0}), "t1", Counts{1, 2, 3, 4, 5, 6, 1, 1}, "", States{}},
138+
{ThreadID(ID{p + 1, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 1, 1}, "", States{}},
139139
}),
140-
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
140+
Update{n, Delta{0, 0, 0, 0, 0, 0, 2, 2}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
141141
[]ThreadUpdate{
142142
{"t1", Delta{}},
143143
{"t2", Delta{}},
144144
},
145145
},
146146
}, {
147147
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
148-
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
149-
{ThreadID(ID{p + 1, 0}), "t2", Counts{2, 2, 2, 2, 2, 2, 0, 0}, "", States{}},
150-
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
148+
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 1, 1}, "", States{}},
149+
{ThreadID(ID{p + 1, 0}), "t2", Counts{2, 2, 2, 2, 2, 2, 1, 1}, "", States{}},
150+
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 1, 1}, "", States{}},
151151
}),
152-
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 3, States{}, msi{},
152+
Update{n, Delta{0, 0, 0, 0, 0, 0, 1, 1}, Memory{}, Filedesc{1, 1}, tm, 3, States{}, msi{},
153153
[]ThreadUpdate{
154154
{"t1", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
155155
{"t2", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
@@ -158,15 +158,26 @@ func TestTrackerThreads(t *testing.T) {
158158
},
159159
}, {
160160
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
161-
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
162-
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
161+
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 1, 1}, "", States{}},
162+
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 1, 1}, "", States{}},
163163
}),
164164
Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
165165
[]ThreadUpdate{
166166
{"t1", Delta{}},
167167
{"t2", Delta{0, 1, 2, 3, 4, 5, 0, 0}},
168168
},
169169
},
170+
}, {
171+
piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
172+
{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 2, 2}, "", States{}},
173+
{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 2, 2}, "", States{}},
174+
}),
175+
Update{n, Delta{0, 0, 0, 0, 0, 0, 2, 2}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
176+
[]ThreadUpdate{
177+
{"t1", Delta{0, 0, 0, 0, 0, 0, 1, 1}},
178+
{"t2", Delta{0, 0, 0, 0, 0, 0, 1, 1}},
179+
},
180+
},
170181
},
171182
}
172183
tr := NewTracker(newNamer(n), false, false, false)

0 commit comments

Comments
 (0)