Skip to content

Commit

Permalink
chore: optimize memory usage of tcell library on init
Browse files Browse the repository at this point in the history
There are two changes here:

* build `machined` binary with `tcell_minimal` tag (which disables
  loading some parts of the terminfo database), which also affects
  `apid`, `trustd` and `dashboard` processes, as they run from the same
  executable; in `dashboard` explicitly import `linux` terminal we're
  using when the `dashboard` runs on the machine
* pass `TCELL_MINIMIZE=1` environment variable to each Talos process
  which removes 0.5MiB of runewdith allocation for a lookup table

See #7578

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Aug 4, 2023
1 parent 7c86a36 commit fb536af
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ WORKDIR /src/internal/app/machined
ARG GO_BUILDFLAGS
ARG GO_LDFLAGS
ARG GOAMD64
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=amd64 GOAMD64=${GOAMD64} go build ${GO_BUILDFLAGS} -ldflags "${GO_LDFLAGS}" -o /machined
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=amd64 GOAMD64=${GOAMD64} go build ${GO_BUILDFLAGS} -tags tcell_minimal -ldflags "${GO_LDFLAGS}" -o /machined
RUN chmod +x /machined

FROM base AS machined-build-arm64
WORKDIR /src/internal/app/machined
ARG GO_BUILDFLAGS
ARG GO_LDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=arm64 go build ${GO_BUILDFLAGS} -ldflags "${GO_LDFLAGS}" -o /machined
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=arm64 go build ${GO_BUILDFLAGS} -tags tcell_minimal -ldflags "${GO_LDFLAGS}" -o /machined
RUN chmod +x /machined

FROM machined-build-${TARGETARCH} AS machined-build
Expand Down
4 changes: 3 additions & 1 deletion internal/app/machined/pkg/system/services/apid.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ func (o *APID) Runner(r runtime.Runtime) (runner.Runner, error) {
{Type: "bind", Destination: filepath.Dir(constants.APISocketPath), Source: filepath.Dir(constants.APISocketPath), Options: []string{"rbind", "rw"}},
}

env := []string{}
env := []string{
constants.TcellMinimizeEnvironment,
}

for _, value := range environment.Get(r.Config()) {
key, _, _ := strings.Cut(value, "=")
Expand Down
5 changes: 4 additions & 1 deletion internal/app/machined/pkg/system/services/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func (d *Dashboard) Runner(r runtime.Runtime) (runner.Runner, error) {
ProcessArgs: []string{"/sbin/dashboard"},
},
runner.WithLoggingManager(r.Logging()),
runner.WithEnv([]string{"TERM=linux"}),
runner.WithEnv([]string{
"TERM=linux",
constants.TcellMinimizeEnvironment,
}),
runner.WithStdinFile(tty),
runner.WithStdoutFile(tty),
runner.WithCtty(1),
Expand Down
1 change: 1 addition & 0 deletions internal/app/machined/pkg/system/services/trustd.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (t *Trustd) Runner(r runtime.Runtime) (runner.Runner, error) {
}

env := environment.Get(r.Config())
env = append(env, constants.TcellMinimizeEnvironment)

if debug.RaceEnabled {
env = append(env, "GORACE=halt_on_error=1")
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/gdamore/tcell/v2"
_ "github.com/gdamore/tcell/v2/terminfo/l/linux" // linux terminal is used when running on the machine, but not included with tcell_minimal
"github.com/gizak/termui/v3"
"github.com/rivo/tview"
"github.com/siderolabs/gen/maps"
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/mount/switchroot/switchroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func Switch(prefix string, mountpoints *mount.Points) (err error) {
// convention.
log.Println("executing /sbin/init")

envv := []string{}
envv := []string{
constants.TcellMinimizeEnvironment,
}

if debug.RaceEnabled {
envv = append(envv, "GORACE=halt_on_error=1")
Expand Down
3 changes: 3 additions & 0 deletions pkg/machinery/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,9 @@ const (

// GRPCMaxMessageSize is the maximum message size for Talos API.
GRPCMaxMessageSize = 32 * 1024 * 1024

// TcellMinimizeEnvironment is the environment variable to minimize tcell library memory usage (skips rune width calculation).
TcellMinimizeEnvironment = "TCELL_MINIMIZE=1"
)

// See https://linux.die.net/man/3/klogctl
Expand Down

0 comments on commit fb536af

Please sign in to comment.