Skip to content
Merged
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
96 changes: 96 additions & 0 deletions desktop/internal/core/process_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package core

import "context"

// MemoryProcess is one row of the process table the client read for processMemory.summarize.
type MemoryProcess struct {
ProcessID int64 `json:"processId"`
ParentProcessID int64 `json:"parentProcessId"`
// ResponsibleProcessID is the process the system holds responsible for this one, nil
// where the system has no such notion.
ResponsibleProcessID *int64 `json:"responsibleProcessId,omitempty"`
// StartTime is when the process started, in microseconds since 1970, nil when it was not
// read. Core ignores a parent link to a process that started later.
StartTime *uint64 `json:"startTime,omitempty"`
Name string `json:"name"`
Bytes uint64 `json:"bytes"`
}

// MemoryRole kinds: what the client started a process for.
const (
MemoryRoleLanguageServer = "languageServer"
MemoryRolePluginService = "pluginService"
)

// MemoryRole names a process the client started for a role. An empty Name shows the
// process name.
type MemoryRole struct {
ProcessID int64 `json:"processId"`
Kind string `json:"kind"`
Name string `json:"name"`
}

// MemoryRequest is the process table of one moment and the roles of the processes the
// client started. HostProcessID must be one of Processes. A nil EntryLimit takes Core's
// default; it is capped at 200.
type MemoryRequest struct {
HostProcessID int64 `json:"hostProcessId"`
Processes []MemoryProcess `json:"processes"`
Roles []MemoryRole `json:"roles,omitempty"`
EntryLimit *int `json:"entryLimit,omitempty"`
}

// Memory kinds: the part of the instance a process counts in, in the order the summary
// lists its categories.
const (
MemoryHost = "host"
MemoryLanguageServer = "languageServer"
MemoryPluginService = "pluginService"
MemorySystemHelper = "systemHelper"
MemoryChildProcess = "childProcess"
)

// MemoryCategory is the sum of one part.
type MemoryCategory struct {
Kind string `json:"kind"`
Bytes uint64 `json:"bytes"`
ProcessCount int `json:"processCount"`
}

// MemoryEntry is one process of the instance other than the host, with the processes Core
// counts under it.
type MemoryEntry struct {
Kind string `json:"kind"`
Name string `json:"name"`
ProcessID int64 `json:"processId"`
Bytes uint64 `json:"bytes"`
ProcessCount int `json:"processCount"`
}

// MemoryOmitted is the count and sum of the entries past the limit.
type MemoryOmitted struct {
Count int `json:"count"`
Bytes uint64 `json:"bytes"`
}

// MemorySummary is Core's account of what the instance holds: the total, every part in a
// fixed order with the empty ones included, and the largest entries.
type MemorySummary struct {
TotalBytes uint64 `json:"totalBytes"`
Categories []MemoryCategory `json:"categories"`
Entries []MemoryEntry `json:"entries"`
Omitted MemoryOmitted `json:"omitted"`
}

// SummarizeProcessMemory has Core attribute every process of the table to the instance or
// leave it out, and sum what each part holds.
func (c *Client) SummarizeProcessMemory(ctx context.Context, request MemoryRequest) (MemorySummary, error) {
request.Processes = nonNil(request.Processes)
var summary MemorySummary
if err := c.Execute(ctx, "processMemory.summarize", request, &summary); err != nil {
return MemorySummary{}, err
}
summary.Categories = nonNil(summary.Categories)
summary.Entries = nonNil(summary.Entries)
return summary, nil
}
93 changes: 93 additions & 0 deletions desktop/internal/core/process_memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package core

import (
"encoding/json"
"errors"
"reflect"
"testing"
)

// memoryFixture is rust/fixtures/process-lifecycle/memory-summary-v1.json of the linked Core.
type memoryFixture struct {
Cases []struct {
Name string `json:"name"`
Request json.RawMessage `json:"request"`
Expected MemorySummary `json:"expected"`
} `json:"cases"`
Invalid []struct {
Name string `json:"name"`
Request json.RawMessage `json:"request"`
} `json:"invalid"`
}

// typedMemoryRequest decodes raw into the client's request and reports whether encoding it
// again gives the same document, so the client sends what the fixture holds.
func typedMemoryRequest(t *testing.T, raw json.RawMessage) (MemoryRequest, bool) {
t.Helper()
var request MemoryRequest
if json.Unmarshal(raw, &request) != nil {
return MemoryRequest{}, false
}
encoded, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
var want, got any
if err := json.Unmarshal(raw, &want); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(encoded, &got); err != nil {
t.Fatal(err)
}
return request, reflect.DeepEqual(got, want)
}

// TestProcessMemorySummarizeFollowsTheContractFixture replays every summary: Core attributes
// and sums, the client only carries the table and the roles.
func TestProcessMemorySummarizeFollowsTheContractFixture(t *testing.T) {
var fixture memoryFixture
if err := json.Unmarshal(readLifecycleFixture(t, "memory-summary-v1.json"), &fixture); err != nil {
t.Fatal(err)
}
if len(fixture.Cases) == 0 || len(fixture.Invalid) == 0 {
t.Fatal("the fixture has no cases")
}
ctx := debugContext(t)
for _, test := range fixture.Cases {
t.Run(test.Name, func(t *testing.T) {
request, exact := typedMemoryRequest(t, test.Request)
if !exact {
t.Fatalf("the client cannot send the request %s as it is", test.Request)
}
got, err := NewClient().SummarizeProcessMemory(ctx, request)
if err != nil {
t.Fatal(err)
}
test.Expected.Categories = nonNil(test.Expected.Categories)
test.Expected.Entries = nonNil(test.Expected.Entries)
if !reflect.DeepEqual(got, test.Expected) {
t.Fatalf("summary = %+v, want %+v", got, test.Expected)
}
})
}
// Some invalid requests, such as negative bytes, cannot be written with the client's
// types; the ones that can are rejected as invalid.
sent := 0
for _, test := range fixture.Invalid {
request, exact := typedMemoryRequest(t, test.Request)
if !exact {
continue
}
sent++
t.Run(test.Name, func(t *testing.T) {
_, err := NewClient().SummarizeProcessMemory(ctx, request)
var coreErr *Error
if !errors.As(err, &coreErr) || coreErr.Code != CodeInvalidRequest {
t.Fatalf("error = %v, want %s", err, CodeInvalidRequest)
}
})
}
if sent == 0 {
t.Fatal("no invalid request could be sent")
}
}
1 change: 0 additions & 1 deletion desktop/internal/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ var english = Messages{
Encoding: Shared(English, "desktop.status.encoding"),
Problems: func(count int) string { return statusProblems(English, count) },
Memory: Shared(English, "desktop.status.memory"),
MemoryValue: func(megabytes int) string { return statusMemory(English, megabytes) },
FPS: Shared(English, "desktop.status.fPS"),
FPSValue: func(fps int) string { return statusFPS(English, fps) },
NoChanges: Shared(English, "desktop.status.noChanges"),
Expand Down
2 changes: 1 addition & 1 deletion desktop/internal/i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestTerminalAndStatusValuesPreserveExistingWording(t *testing.T) {
{Chinese, "本地", "本地 (2)", "会话已结束,退出状态 -1。", "1 个更改", "2 个更改"},
} {
messages := For(test.language)
if messages.Terminal.Tab(1) != test.local || messages.Terminal.Tab(2) != test.second || messages.Terminal.SessionEndedStatus(-1) != test.ended || messages.Status.Changes(1) != test.one || messages.Status.Changes(2) != test.many || messages.Status.MemoryValue(12) != "12 MB" || messages.Status.FPSValue(60) != "60 FPS" {
if messages.Terminal.Tab(1) != test.local || messages.Terminal.Tab(2) != test.second || messages.Terminal.SessionEndedStatus(-1) != test.ended || messages.Status.Changes(1) != test.one || messages.Status.Changes(2) != test.many || messages.Status.FPSValue(60) != "60 FPS" {
t.Fatalf("language %s: terminal or status wording changed", test.language.Code())
}
}
Expand Down
4 changes: 1 addition & 3 deletions desktop/internal/i18n/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,7 @@ type StatusMessages struct {
// "3 problems". One problem has its own wording.
Problems func(count int) string
Memory string
// MemoryValue is the memory readout, for example "312 MB".
MemoryValue func(megabytes int) string
FPS string
FPS string
// FPSValue is the frame rate readout, for example "60 FPS".
FPSValue func(fps int) string
NoChanges string
Expand Down
4 changes: 0 additions & 4 deletions desktop/internal/i18n/runtime_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func terminalTab(language Language, number int) string {
return SharedFormat(language, "desktop.terminal.localNumber", map[string]string{"number": strconv.Itoa(number)})
}

func statusMemory(language Language, value int) string {
return SharedFormat(language, "desktop.status.memoryValue", map[string]string{"value": strconv.Itoa(value)})
}

func statusFPS(language Language, value int) string {
return SharedFormat(language, "desktop.status.fpsValue", map[string]string{"value": strconv.Itoa(value)})
}
Expand Down
56 changes: 38 additions & 18 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion desktop/internal/i18n/zh.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ var chinese = Messages{
Encoding: Shared(Chinese, "desktop.status.encoding"),
Problems: func(count int) string { return statusProblems(Chinese, count) },
Memory: Shared(Chinese, "desktop.status.memory"),
MemoryValue: func(megabytes int) string { return statusMemory(Chinese, megabytes) },
FPS: Shared(Chinese, "desktop.status.fPS"),
FPSValue: func(fps int) string { return statusFPS(Chinese, fps) },
NoChanges: Shared(Chinese, "desktop.status.noChanges"),
Expand Down
23 changes: 0 additions & 23 deletions desktop/internal/pluginservice/processsample.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,3 @@ func parseWindowsTable(output []byte, root int) (memoryBytes uint64, cpuSeconds
}
return memoryBytes, float64(ticks) / 1e7, true
}

// SampleProcessMemory reads the process table once and sums the resident memory of the tree under
// each pid, in bytes. A pid that is gone is left out. Like SampleProcessTree it runs a short-lived
// command, so it is called off the window thread.
func SampleProcessMemory(ctx context.Context, pids []int) map[int]uint64 {
if len(pids) == 0 {
return nil
}
output, err := processTable(ctx)
if err != nil {
return nil
}
memory := make(map[int]uint64, len(pids))
for _, pid := range pids {
if pid <= 0 {
continue
}
if bytes, ok := treeMemory(output, pid); ok {
memory[pid] = bytes
}
}
return memory
}
7 changes: 0 additions & 7 deletions desktop/internal/pluginservice/processsample_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,3 @@ func sampleProcessTree(ctx context.Context, pid int) (ProcessSample, bool) {
}
return parsePSTable(output, pid)
}

func processTable(ctx context.Context) ([]byte, error) { return psCommand(ctx) }

func treeMemory(output []byte, pid int) (uint64, bool) {
sample, ok := parsePSTable(output, pid)
return sample.MemoryBytes, ok
}
9 changes: 0 additions & 9 deletions desktop/internal/pluginservice/processsample_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,3 @@ func sampleProcessTree(ctx context.Context, pid int) (ProcessSample, bool) {
}
return sample, true
}

func processTable(ctx context.Context) ([]byte, error) {
return exec.CommandContext(ctx, "powershell", "-NoProfile", "-NonInteractive", "-Command", windowsTableCommand).Output()
}

func treeMemory(output []byte, pid int) (uint64, bool) {
memory, _, ok := parseWindowsTable(output, pid)
return memory, ok
}
Loading
Loading