Skip to content

Commit 0ade6ee

Browse files
authored
updates to make processviewer more robust (#3144)
1 parent 96c2526 commit 0ade6ee

File tree

11 files changed

+456
-150
lines changed

11 files changed

+456
-150
lines changed

frontend/app/view/processviewer/processviewer.tsx

Lines changed: 180 additions & 20 deletions
Large diffs are not rendered by default.

frontend/preview/previews/processviewer.preview.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import { useRpcOverride } from "../mock/use-rpc-override";
1010
const PreviewNodeId = "preview-processviewer-node";
1111

1212
const MockProcesses: ProcessInfo[] = [
13-
{ pid: 1, ppid: 0, command: "launchd", user: "root", cpu: 0.0, mem: 4096 * 1024 },
14-
{ pid: 123, ppid: 1, command: "kernel_task", user: "root", cpu: 12.3, mem: 2048 * 1024 * 1024 },
15-
{ pid: 456, ppid: 1, command: "WindowServer", user: "_windowserver", cpu: 5.1, mem: 512 * 1024 * 1024 },
16-
{ pid: 789, ppid: 1, command: "node", user: "mike", cpu: 8.7, mem: 256 * 1024 * 1024 },
17-
{ pid: 1001, ppid: 1, command: "Electron", user: "mike", cpu: 3.2, mem: 400 * 1024 * 1024 },
18-
{ pid: 1234, ppid: 1001, command: "waveterm-helper", user: "mike", cpu: 0.5, mem: 64 * 1024 * 1024 },
19-
{ pid: 2001, ppid: 1, command: "sshd", user: "root", cpu: 0.0, mem: 8 * 1024 * 1024 },
20-
{ pid: 2345, ppid: 1, command: "postgres", user: "postgres", cpu: 1.2, mem: 128 * 1024 * 1024 },
21-
{ pid: 3001, ppid: 1, command: "nginx", user: "_www", cpu: 0.3, mem: 32 * 1024 * 1024 },
22-
{ pid: 3456, ppid: 1, command: "python3", user: "mike", cpu: 2.8, mem: 96 * 1024 * 1024 },
23-
{ pid: 4001, ppid: 1, command: "docker", user: "root", cpu: 0.1, mem: 48 * 1024 * 1024 },
24-
{ pid: 4567, ppid: 4001, command: "containerd", user: "root", cpu: 0.2, mem: 80 * 1024 * 1024 },
25-
{ pid: 5001, ppid: 1, command: "zsh", user: "mike", cpu: 0.0, mem: 6 * 1024 * 1024 },
26-
{ pid: 5678, ppid: 5001, command: "vim", user: "mike", cpu: 0.0, mem: 20 * 1024 * 1024 },
27-
{ pid: 6001, ppid: 1, command: "coreaudiod", user: "_coreaudiod", cpu: 0.4, mem: 16 * 1024 * 1024 },
13+
{ pid: 1, ppid: 0, command: "launchd", user: "root", cpu: 0.0, mem: 4096 * 1024, mempct: 0.01 },
14+
{ pid: 123, ppid: 1, command: "kernel_task", user: "root", cpu: 12.3, mem: 2048 * 1024 * 1024, mempct: 6.25 },
15+
{ pid: 456, ppid: 1, command: "WindowServer", user: "_windowserver", cpu: 5.1, mem: 512 * 1024 * 1024, mempct: 1.56 },
16+
{ pid: 789, ppid: 1, command: "node", user: "mike", cpu: 8.7, mem: 256 * 1024 * 1024, mempct: 0.78 },
17+
{ pid: 1001, ppid: 1, command: "Electron", user: "mike", cpu: 3.2, mem: 400 * 1024 * 1024, mempct: 1.22 },
18+
{ pid: 1234, ppid: 1001, command: "waveterm-helper", user: "mike", cpu: 0.5, mem: 64 * 1024 * 1024, mempct: 0.20 },
19+
{ pid: 2001, ppid: 1, command: "sshd", user: "root", cpu: 0.0, mem: 8 * 1024 * 1024, mempct: 0.02 },
20+
{ pid: 2345, ppid: 1, command: "postgres", user: "postgres", cpu: 1.2, mem: 128 * 1024 * 1024, mempct: 0.39 },
21+
{ pid: 3001, ppid: 1, command: "nginx", user: "_www", cpu: 0.3, mem: 32 * 1024 * 1024, mempct: 0.10 },
22+
{ pid: 3456, ppid: 1, command: "python3", user: "mike", cpu: 2.8, mem: 96 * 1024 * 1024, mempct: 0.29 },
23+
{ pid: 4001, ppid: 1, command: "docker", user: "root", cpu: 0.1, mem: 48 * 1024 * 1024, mempct: 0.15 },
24+
{ pid: 4567, ppid: 4001, command: "containerd", user: "root", cpu: 0.2, mem: 80 * 1024 * 1024, mempct: 0.24 },
25+
{ pid: 5001, ppid: 1, command: "zsh", user: "mike", cpu: 0.0, mem: 6 * 1024 * 1024, mempct: 0.02 },
26+
{ pid: 5678, ppid: 5001, command: "vim", user: "mike", cpu: 0.0, mem: 20 * 1024 * 1024, mempct: 0.06 },
27+
{ pid: 6001, ppid: 1, command: "coreaudiod", user: "_coreaudiod", cpu: 0.4, mem: 16 * 1024 * 1024, mempct: 0.05 },
2828
];
2929

3030
const MockSummary: ProcessSummary = {

frontend/tailwindsetup.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ svg [aria-label="tip"] g path {
9393
color: var(--border-color);
9494
}
9595

96+
.wide-scrollbar::-webkit-scrollbar {
97+
width: 10px;
98+
}
99+
96100
/* Monaco editor scrollbar styling */
97101
.monaco-editor .slider {
98102
background: rgba(255, 255, 255, 0.4);

frontend/types/gotypes.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,14 @@ declare global {
559559

560560
// wshrpc.CommandRemoteProcessListData
561561
type CommandRemoteProcessListData = {
562+
widgetid?: string;
562563
sortby?: string;
563564
sortdesc?: boolean;
564565
start?: number;
565566
limit?: number;
566567
textsearch?: string;
567-
pids?: number[];
568+
lastpidorder?: boolean;
569+
keepalive?: boolean;
568570
};
569571

570572
// wshrpc.CommandRemoteProcessSignalData
@@ -1267,10 +1269,11 @@ declare global {
12671269
command?: string;
12681270
status?: string;
12691271
user?: string;
1270-
mem?: number;
1271-
mempct?: number;
1272-
cpu?: number;
1273-
numthreads?: number;
1272+
mem: number;
1273+
mempct: number;
1274+
cpu: number;
1275+
numthreads: number;
1276+
gone?: boolean;
12741277
};
12751278

12761279
// wshrpc.ProcessListResponse

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/procinfo/procinfo.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ var LinuxStatStatus = map[string]string{
2727
// ProcInfo holds per-process information read from the OS.
2828
// CpuUser and CpuSys are cumulative CPU seconds since process start;
2929
// callers should diff two samples over a known interval to derive a rate.
30+
// CpuUser, CpuSys, and VmRSS are set to -1 when the data is unavailable
31+
// (e.g. permission denied reading another user's process).
3032
type ProcInfo struct {
3133
Pid int32
3234
Ppid int32
3335
Command string
3436
Status string
35-
CpuUser float64 // cumulative user CPU seconds
36-
CpuSys float64 // cumulative system CPU seconds
37-
VmRSS uint64 // resident set size in bytes
37+
CpuUser float64 // cumulative user CPU seconds; -1 if unavailable
38+
CpuSys float64 // cumulative system CPU seconds; -1 if unavailable
39+
VmRSS int64 // resident set size in bytes; -1 if unavailable
3840
Uid uint32
39-
NumThreads int32
41+
NumThreads int32 // -1 if unavailable
4042
}

pkg/util/procinfo/procinfo_darwin.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"unsafe"
1212

1313
"github.com/ebitengine/purego"
14-
goproc "github.com/shirou/gopsutil/v4/process"
1514
"golang.org/x/sys/unix"
1615
)
1716

@@ -88,22 +87,17 @@ func GetProcInfo(ctx context.Context, _ any, pid int32) (*ProcInfo, error) {
8887
Uid: k.Eproc.Ucred.Uid,
8988
}
9089

90+
info.CpuUser = -1
91+
info.CpuSys = -1
92+
info.VmRSS = -1
93+
info.NumThreads = -1
9194
if ti, terr := getDarwinProcTaskInfo(pid); terr == nil {
9295
if darwinTimeScale > 0 {
9396
info.CpuUser = float64(ti.TotalUser) * darwinTimeScale / 1e9
9497
info.CpuSys = float64(ti.TotalSystem) * darwinTimeScale / 1e9
9598
}
96-
info.VmRSS = ti.ResidentSize
99+
info.VmRSS = int64(ti.ResidentSize)
97100
info.NumThreads = ti.Threadnum
98-
} else {
99-
if p, gerr := goproc.NewProcessWithContext(ctx, pid); gerr == nil {
100-
if mi, merr := p.MemoryInfoWithContext(ctx); merr == nil {
101-
info.VmRSS = mi.RSS
102-
}
103-
if nt, nerr := p.NumThreadsWithContext(ctx); nerr == nil {
104-
info.NumThreads = nt
105-
}
106-
}
107101
}
108102

109103
return info, nil

pkg/util/procinfo/procinfo_linux.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ func readStat(pid int32) (*ProcInfo, error) {
9090
return nil, fmt.Errorf("procinfo: parse pid: %w", err)
9191
}
9292

93-
ppid, _ := strconv.ParseInt(rest[1], 10, 32)
94-
utime, _ := strconv.ParseUint(rest[11], 10, 64)
95-
stime, _ := strconv.ParseUint(rest[12], 10, 64)
96-
numThreads, _ := strconv.ParseInt(rest[17], 10, 32)
97-
rssPages, _ := strconv.ParseInt(rest[21], 10, 64)
98-
9993
statusChar := rest[0]
10094
status, ok := LinuxStatStatus[statusChar]
10195
if !ok {
@@ -104,14 +98,30 @@ func readStat(pid int32) (*ProcInfo, error) {
10498

10599
info := &ProcInfo{
106100
Pid: int32(parsedPid),
107-
Ppid: int32(ppid),
108101
Command: comm,
109102
Status: status,
110-
CpuUser: float64(utime) / userHz,
111-
CpuSys: float64(stime) / userHz,
112-
VmRSS: uint64(rssPages * pageSize),
113-
NumThreads: int32(numThreads),
103+
CpuUser: -1,
104+
CpuSys: -1,
105+
VmRSS: -1,
106+
NumThreads: -1,
107+
}
108+
109+
if ppid, err := strconv.ParseInt(rest[1], 10, 32); err == nil {
110+
info.Ppid = int32(ppid)
111+
}
112+
if utime, err := strconv.ParseUint(rest[11], 10, 64); err == nil {
113+
info.CpuUser = float64(utime) / userHz
114114
}
115+
if stime, err := strconv.ParseUint(rest[12], 10, 64); err == nil {
116+
info.CpuSys = float64(stime) / userHz
117+
}
118+
if numThreads, err := strconv.ParseInt(rest[17], 10, 32); err == nil {
119+
info.NumThreads = int32(numThreads)
120+
}
121+
if rssPages, err := strconv.ParseInt(rest[21], 10, 64); err == nil {
122+
info.VmRSS = rssPages * pageSize
123+
}
124+
115125
return info, nil
116126
}
117127

pkg/util/procinfo/procinfo_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ func GetProcInfo(_ context.Context, snap any, pid int32) (*ProcInfo, error) {
9797
Ppid: int32(si.ppid),
9898
NumThreads: int32(si.numThreads),
9999
Command: si.exeName,
100+
CpuUser: -1,
101+
CpuSys: -1,
102+
VmRSS: -1,
100103
}
101104

102105
handle, err := windows.OpenProcess(
@@ -127,7 +130,7 @@ func GetProcInfo(_ context.Context, snap any, pid int32) (*ProcInfo, error) {
127130
uintptr(mc.CB),
128131
)
129132
if r != 0 {
130-
info.VmRSS = uint64(mc.WorkingSetSize)
133+
info.VmRSS = int64(mc.WorkingSetSize)
131134
}
132135

133136
return info, nil

0 commit comments

Comments
 (0)