Skip to content
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

Add page-cluster tweak #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var RecommendedCompactionProactiveness = "0"
var RecommendedHugePageDefrag = "0"
var RecommendedPageLockUnfairness = "1"
var RecommendedShMem = "advise"
var RecommendedPageCluster = "1"

//////////////////////
// Default Settings //
Expand All @@ -39,6 +40,7 @@ var DefaultCompactionProactiveness = "20"
var DefaultHugePageDefrag = "1"
var DefaultPageLockUnfairness = "5"
var DefaultShMem = "never"
var DefaultPageCluster = "3"

////////////////
// Unit Files //
Expand All @@ -55,6 +57,7 @@ var UnitMatrix = map[string]string{
"hugepages": "/sys/kernel/mm/transparent_hugepage/enabled",
"shmem_enabled": "/sys/kernel/mm/transparent_hugepage/shmem_enabled",
"defrag": "/sys/kernel/mm/transparent_hugepage/khugepaged/defrag",
"page-cluster": "/proc/sys/vm/page-cluster",
}

var OldSwappinessUnitFile = "/etc/sysctl.d/zzz-custom-swappiness.conf"
Expand Down
53 changes: 53 additions & 0 deletions internal/handler_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ func getDefragStatus() bool {
return false
}

func getPageClusterStatus() bool {
status, err := getUnitStatus("page-cluster")
if err != nil {
CryoUtils.ErrorLog.Println("Unable to get current page-cluster")
return false
}
if status == RecommendedPageCluster {
return true
}
return false
}

// ToggleHugePages Simple one-function toggle for the button to use
func ToggleHugePages() error {
if getHugePagesStatus() {
Expand Down Expand Up @@ -140,6 +152,21 @@ func TogglePageLockUnfairness() error {
return nil
}

func TogglePageCluster() error {
if getPageClusterStatus() {
err := RevertPageCluster()
if err != nil {
return err
}
} else {
err := SetPageCluster()
if err != nil {
return err
}
}
return nil
}

func SetHugePages() error {
CryoUtils.InfoLog.Println("Enabling hugepages...")
// Remove a file accidentally included in a beta for testing
Expand Down Expand Up @@ -271,3 +298,29 @@ func RevertDefrag() error {
}
return nil
}

func SetPageCluster() error {
CryoUtils.InfoLog.Println("Setting page-cluster...")
err := setUnitValue("page-cluster", RecommendedPageCluster)
if err != nil {
return err
}
err = writeUnitFile("page-cluster", RecommendedPageCluster)
if err != nil {
return err
}
return nil
}

func RevertPageCluster() error {
CryoUtils.InfoLog.Println("Disabling page-cluster...")
err := setUnitValue("page-cluster", DefaultPageCluster)
if err != nil {
return err
}
err = removeUnitFile("page-cluster")
if err != nil {
return err
}
return nil
}
25 changes: 21 additions & 4 deletions internal/ui_tab.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package internal

import (
"strconv"
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"strconv"
"strings"
)

// Home tab for "recommended" and "default" buttons
Expand Down Expand Up @@ -176,11 +177,13 @@ func (app *Config) storageTab() *fyne.Container {

// Tab for non-swap, memory-related tweaks.
func (app *Config) memoryTab() *fyne.Container {

app.HugePagesText = canvas.NewText("Huge Pages (THP)", Red)
app.ShMemText = canvas.NewText("Shared Memory in THP", Red)
app.CompactionProactivenessText = canvas.NewText("Compaction Proactiveness", Red)
app.DefragText = canvas.NewText("Defrag", Red)
app.PageLockUnfairnessText = canvas.NewText("Page Lock Unfairness", Red)
app.PageClusterText = canvas.NewText("Page Cluster", Red)

CryoUtils.HugePagesButton = widget.NewButton("Enable HugePages", func() {
renewSudoAuth()
Expand Down Expand Up @@ -227,18 +230,30 @@ func (app *Config) memoryTab() *fyne.Container {
app.refreshPageLockUnfairnessContent()
})

CryoUtils.PageClusterButton = widget.NewButton("Set Page Cluster", func() {
renewSudoAuth()
err := TogglePageCluster()
if err != nil {
presentErrorInUI(err, CryoUtils.MainWindow)
}
app.refreshPageClusterContent()
})

app.refreshHugePagesContent()
app.refreshCompactionProactivenessContent()
app.refreshShMemContent()
app.refreshDefragContent()
app.refreshPageLockUnfairnessContent()
app.refreshPageClusterContent()

app.MemoryBar = container.NewGridWithColumns(5,
app.MemoryBar = container.NewGridWithColumns(3,
container.NewCenter(app.HugePagesText),
container.NewCenter(app.ShMemText),
container.NewCenter(app.CompactionProactivenessText),
container.NewCenter(app.DefragText),
container.NewCenter(app.PageLockUnfairnessText))
container.NewCenter(app.PageLockUnfairnessText),
container.NewCenter(app.PageClusterText))

topBar := container.NewVBox(
container.NewGridWithRows(1),
container.NewGridWithRows(1, container.NewCenter(canvas.NewText("Current Tweak Status:", White))),
Expand All @@ -250,13 +265,15 @@ func (app *Config) memoryTab() *fyne.Container {
compactionProactivenessCard := widget.NewCard("Compaction Proactiveness", "Set compaction proactiveness", app.CompactionProactivenessButton)
defragCard := widget.NewCard("Huge Page Defragmentation", "Toggle huge page defragmentation", app.DefragButton)
pageLockUnfairnessCard := widget.NewCard("Page Lock Unfairness", "Set page lock unfairness", app.PageLockUnfairnessButton)
pageClusterCard := widget.NewCard("Page Cluster", "Set page cluster", app.PageClusterButton)

memoryVBox := container.NewVBox(
hugePagesCard,
shMemCard,
compactionProactivenessCard,
defragCard,
pageLockUnfairnessCard,
pageClusterCard,
)
scroll := container.NewScroll(memoryVBox)
full := container.NewBorder(topBar, nil, nil, nil, scroll)
Expand Down
13 changes: 13 additions & 0 deletions internal/ui_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ func (app *Config) refreshPageLockUnfairnessContent() {
app.PageLockUnfairnessText.Refresh()
}

func (app *Config) refreshPageClusterContent() {
app.InfoLog.Println("Refreshing page cluster data...")
if getPageClusterStatus() {
app.PageClusterButton.Text = "Revert Page Cluster"
app.PageClusterText.Color = Green
} else {
app.PageClusterButton.Text = "Set Page Cluster"
app.PageClusterText.Color = Red
}
app.PageClusterButton.Refresh()
app.PageClusterText.Refresh()
}

func (app *Config) refreshAllContent() {
app.refreshSwapContent()
app.refreshSwappinessContent()
Expand Down
2 changes: 2 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {
CompactionProactivenessText *canvas.Text
DefragText *canvas.Text
PageLockUnfairnessText *canvas.Text
PageClusterText *canvas.Text
SteamAPIResponse map[int]string
MainWindow fyne.Window
SwapResizeProgressBar *widget.ProgressBar
Expand All @@ -44,6 +45,7 @@ type Config struct {
CompactionProactivenessButton *widget.Button
DefragButton *widget.Button
PageLockUnfairnessButton *widget.Button
PageClusterButton *widget.Button
UserPassword string
SwapFileLocation string
}
Expand Down