Skip to content

Commit e9e4491

Browse files
cmd/containerd-shim-runhcs-v1: fix UserTime_100Ns incorrectly reporting kernel time
When querying process metrics via containerd's task Pids API, the UserTime_100Ns metric was incorrectly being assigned the value of KernelTime100ns. This updates the assignment to use the correct UserTime100ns field from the underlying HCS ProcessListItem struct. This change includes a unit test verifying the correct mapping of properties from the schema1 ProcessListItem to the runhcsopts.ProcessDetails structure. Signed-off-by: Harshal Patel <hp842484@gmail.com>
1 parent 0e72fa7 commit e9e4491

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎cmd/containerd-shim-runhcs-v1/task_hcs.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func (ht *hcsTask) Pids(ctx context.Context) ([]*runhcsopts.ProcessDetails, erro
613613
pairs[i].MemoryWorkingSetPrivateBytes = p.MemoryWorkingSetPrivateBytes
614614
pairs[i].MemoryWorkingSetSharedBytes = p.MemoryWorkingSetSharedBytes
615615
pairs[i].ProcessID = p.ProcessId
616-
pairs[i].UserTime_100Ns = p.KernelTime100ns
616+
pairs[i].UserTime_100Ns = p.UserTime100ns
617617

618618
if eid, ok := pidMap[int(p.ProcessId)]; ok {
619619
pairs[i].ExecID = eid

‎cmd/containerd-shim-runhcs-v1/task_hcs_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/Microsoft/hcsshim/internal/cow"
15+
"github.com/Microsoft/hcsshim/internal/hcs/schema1"
1416
"github.com/Microsoft/hcsshim/internal/uvm"
1517
"github.com/Microsoft/hcsshim/pkg/annotations"
1618
"github.com/containerd/errdefs"
@@ -550,3 +552,50 @@ func Test_hcsTask_updateWCOWContainerCPUAffinity_XenonNotImplemented(t *testing.
550552
t.Fatalf("expected ErrNotImplemented for hypervisor-isolated container, got %v", err)
551553
}
552554
}
555+
556+
// mockContainer is a minimal mock for cow.Container
557+
type mockContainer struct {
558+
cow.Container
559+
properties *schema1.ContainerProperties
560+
}
561+
562+
func (m *mockContainer) Properties(ctx context.Context, types ...schema1.PropertyType) (*schema1.ContainerProperties, error) {
563+
return m.properties, nil
564+
}
565+
566+
func Test_hcsTask_Pids_UserTime(t *testing.T) {
567+
ht := &hcsTask{
568+
id: "test-task",
569+
c: &mockContainer{
570+
properties: &schema1.ContainerProperties{
571+
ProcessList: []schema1.ProcessListItem{
572+
{
573+
ProcessId: 123,
574+
ImageName: "test.exe",
575+
KernelTime100ns: 1000,
576+
UserTime100ns: 2000,
577+
},
578+
},
579+
},
580+
},
581+
}
582+
583+
ht.init = newTestShimExec("test-task", "test-task", 123)
584+
585+
pids, err := ht.Pids(context.Background())
586+
if err != nil {
587+
t.Fatalf("Pids() failed: %v", err)
588+
}
589+
590+
if len(pids) != 1 {
591+
t.Fatalf("Expected 1 pid, got %d", len(pids))
592+
}
593+
594+
p := pids[0]
595+
if p.UserTime_100Ns == p.KernelTime_100Ns {
596+
t.Errorf("UserTime_100Ns (%d) incorrectly equals KernelTime_100Ns (%d)", p.UserTime_100Ns, p.KernelTime_100Ns)
597+
}
598+
if p.UserTime_100Ns != 2000 {
599+
t.Errorf("Expected UserTime_100Ns=2000, got %d", p.UserTime_100Ns)
600+
}
601+
}

0 commit comments

Comments
 (0)