-
Notifications
You must be signed in to change notification settings - Fork 684
feat: implement zswap support #11199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smira
wants to merge
1
commit into
siderolabs:main
Choose a base branch
from
smira:feat/zswap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
internal/app/machined/pkg/controllers/block/zswap_config.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package block | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"go.uber.org/zap" | ||
|
||
configconfig "github.com/siderolabs/talos/pkg/machinery/config/config" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/runtime" | ||
) | ||
|
||
// ZswapConfigController provides zswap configuration based machine configuration. | ||
type ZswapConfigController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *ZswapConfigController) Name() string { | ||
return "block.ZswapConfigController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *ZswapConfigController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: optional.Some(config.ActiveID), | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *ZswapConfigController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: runtime.KernelParamSpecType, | ||
Kind: controller.OutputShared, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *ZswapConfigController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error { | ||
for { | ||
select { | ||
case <-r.EventCh(): | ||
case <-ctx.Done(): | ||
return nil | ||
} | ||
|
||
// load config if present | ||
cfg, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.ActiveID) | ||
if err != nil && !state.IsNotFoundError(err) { | ||
return fmt.Errorf("error fetching machine configuration") | ||
} | ||
|
||
r.StartTrackingOutputs() | ||
|
||
var zswapCfg configconfig.ZswapConfig | ||
|
||
if cfg != nil { | ||
zswapCfg = cfg.Config().ZswapConfig() | ||
} | ||
|
||
if zswapCfg != nil { // enabled | ||
if err := safe.WriterModify(ctx, r, runtime.NewKernelParamSpec(runtime.NamespaceName, "sys.module.zswap.parameters.enabled"), | ||
func(p *runtime.KernelParamSpec) error { | ||
p.TypedSpec().Value = "Y" | ||
|
||
return nil | ||
}); err != nil { | ||
return fmt.Errorf("error setting zswap config: %w", err) | ||
} | ||
|
||
if err := safe.WriterModify(ctx, r, runtime.NewKernelParamSpec(runtime.NamespaceName, "sys.module.zswap.parameters.max_pool_percent"), | ||
func(p *runtime.KernelParamSpec) error { | ||
p.TypedSpec().Value = strconv.Itoa(zswapCfg.MaxPoolPercent()) | ||
|
||
return nil | ||
}); err != nil { | ||
return fmt.Errorf("error setting zswap config: %w", err) | ||
} | ||
|
||
if err := safe.WriterModify(ctx, r, runtime.NewKernelParamSpec(runtime.NamespaceName, "sys.module.zswap.parameters.shrinker_enabled"), | ||
func(p *runtime.KernelParamSpec) error { | ||
if zswapCfg.ShrinkerEnabled() { | ||
p.TypedSpec().Value = "Y" | ||
} else { | ||
p.TypedSpec().Value = "N" | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
return fmt.Errorf("error setting zswap config: %w", err) | ||
} | ||
} | ||
|
||
if err = safe.CleanupOutputs[*runtime.KernelParamSpec](ctx, r); err != nil { | ||
return fmt.Errorf("error cleaning up volume configuration: %w", err) | ||
} | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
internal/app/machined/pkg/controllers/block/zswap_status.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package block | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/dustin/go-humanize" | ||
"go.uber.org/zap" | ||
|
||
machineruntime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/block" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/runtime" | ||
) | ||
|
||
// ZswapStatusController provides a view of active swap devices. | ||
type ZswapStatusController struct { | ||
V1Alpha1Mode machineruntime.Mode | ||
} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *ZswapStatusController) Name() string { | ||
return "block.ZswapStatusController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *ZswapStatusController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
// not really a dependency, but we refresh zswap status kernel param change | ||
Namespace: runtime.NamespaceName, | ||
Type: runtime.KernelParamStatusType, | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *ZswapStatusController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: block.ZswapStatusType, | ||
Kind: controller.OutputExclusive, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *ZswapStatusController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
// in container mode, no zswap applies | ||
if ctrl.V1Alpha1Mode == machineruntime.ModeContainer { | ||
return nil | ||
} | ||
|
||
// there is no way to watch for zswap status, so we are going to poll every minute | ||
ticker := time.NewTicker(time.Minute) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
case <-ticker.C: | ||
} | ||
|
||
r.StartTrackingOutputs() | ||
|
||
// try to read a single status file to see if zswap is enabled | ||
if _, err := os.ReadFile("/sys/kernel/debug/zswap/pool_total_size"); err == nil { | ||
if err = safe.WriterModify(ctx, r, block.NewZswapStatus(block.NamespaceName, block.ZswapStatusID), | ||
func(zs *block.ZswapStatus) error { | ||
for _, p := range []struct { | ||
name string | ||
out *uint64 | ||
}{ | ||
{"pool_total_size", &zs.TypedSpec().TotalSizeBytes}, | ||
{"stored_pages", &zs.TypedSpec().StoredPages}, | ||
{"pool_limit_hit", &zs.TypedSpec().PoolLimitHit}, | ||
{"reject_reclaim_fail", &zs.TypedSpec().RejectReclaimFail}, | ||
{"reject_alloc_fail", &zs.TypedSpec().RejectAllocFail}, | ||
{"reject_kmemcache_fail", &zs.TypedSpec().RejectKmemcacheFail}, | ||
{"reject_compress_fail", &zs.TypedSpec().RejectCompressFail}, | ||
{"reject_compress_poor", &zs.TypedSpec().RejectCompressPoor}, | ||
{"written_back_pages", &zs.TypedSpec().WrittenBackPages}, | ||
} { | ||
if err := ctrl.readZswapParam(p.name, p.out); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
zs.TypedSpec().TotalSizeHuman = humanize.IBytes(zs.TypedSpec().TotalSizeBytes) | ||
|
||
return nil | ||
}, | ||
); err != nil { | ||
return fmt.Errorf("failed to create zswap status: %w", err) | ||
} | ||
} | ||
|
||
if err := safe.CleanupOutputs[*block.ZswapStatus](ctx, r); err != nil { | ||
return fmt.Errorf("failed to cleanup outputs: %w", err) | ||
} | ||
} | ||
} | ||
|
||
func (ctrl *ZswapStatusController) readZswapParam(name string, out *uint64) error { | ||
content, err := os.ReadFile(filepath.Join("/sys/kernel/debug/zswap", name)) | ||
if err != nil { | ||
return fmt.Errorf("failed to read zswap parameter %q: %w", name, err) | ||
} | ||
|
||
*out, err = strconv.ParseUint(string(bytes.TrimSpace(content)), 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse zswap parameter %q: %w", name, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is cleaning up enough, or do we need to explicitly set
sys.module.zswap.parameters.enabled
toN
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, cleaning up is enough - the kernel param controller reverts the value to the previous setting ('N') if the spec is removed