updated with opencode
This commit is contained in:
@@ -9,5 +9,5 @@ require (
|
|||||||
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
||||||
github.com/tklauser/numcpus v0.6.0 // indirect
|
github.com/tklauser/numcpus v0.6.0 // indirect
|
||||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||||
golang.org/x/sys v0.4.0 // indirect
|
golang.org/x/sys v0.4.0
|
||||||
)
|
)
|
||||||
|
|||||||
+62
-38
@@ -1,55 +1,79 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
/*
|
|
||||||
#include <windows.h>
|
|
||||||
BOOL SetPriority(DWORD pid, int priority) {
|
|
||||||
HANDLE h = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
|
|
||||||
if (!h) return FALSE;
|
|
||||||
BOOL success = SetPriorityClass(h, priority);
|
|
||||||
CloseHandle(h);
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/postfinance/single"
|
"github.com/postfinance/single"
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
"os"
|
"golang.org/x/sys/windows"
|
||||||
"runtime"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MessageBox(lpText string, lpCaption string, uType int) int {
|
const (
|
||||||
var convertedCaption, _ = syscall.UTF16PtrFromString(lpCaption)
|
memThreshold = 512 * 1024 * 1024
|
||||||
var convertedTitle, _ = syscall.UTF16PtrFromString(lpText)
|
selfPriority = windows.IDLE_PRIORITY_CLASS
|
||||||
returnCode, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
|
targetPriority = windows.HIGH_PRIORITY_CLASS
|
||||||
uintptr(0),
|
)
|
||||||
uintptr(unsafe.Pointer(convertedCaption)),
|
|
||||||
uintptr(unsafe.Pointer(convertedTitle)),
|
|
||||||
uintptr(uType),
|
|
||||||
)
|
|
||||||
return int(returnCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
var lockFile, _ = single.New("autoPriority")
|
func setPriority(pid int32, class uint32) error {
|
||||||
|
h, err := windows.OpenProcess(windows.PROCESS_SET_INFORMATION, false, uint32(pid))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open process %d: %w", pid, err)
|
||||||
|
}
|
||||||
|
defer windows.CloseHandle(h)
|
||||||
|
return windows.SetPriorityClass(h, class)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := lockFile.Lock(); err != nil {
|
lock, err := single.New("autoPriority")
|
||||||
MessageBox("The program is already running", "Only one instance of the program can be run\nYou can close it via the Task Manager", 48)
|
if err != nil {
|
||||||
|
log.Fatalf("create lock: %v", err)
|
||||||
|
}
|
||||||
|
if err := lock.Lock(); err != nil {
|
||||||
|
windows.MessageBox(0,
|
||||||
|
windows.StringToUTF16Ptr("The program is already running.\nYou can close it via the Task Manager."),
|
||||||
|
windows.StringToUTF16Ptr("autoPriority"),
|
||||||
|
windows.MB_OK|windows.MB_ICONWARNING,
|
||||||
|
)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
C.SetPriority(C.DWORD(os.Getpid()), 0x00000040)
|
if err := setPriority(int32(os.Getpid()), selfPriority); err != nil {
|
||||||
for range time.Tick(time.Minute) {
|
log.Printf("set self priority: %v", err)
|
||||||
processes, _ := process.Processes()
|
}
|
||||||
for _, proc := range processes {
|
|
||||||
memoryInfo, err := proc.MemoryInfo()
|
sig := make(chan os.Signal, 1)
|
||||||
if err == nil && memoryInfo.RSS >= (0.5*1024*1024*1024) {
|
signal.Notify(sig, os.Interrupt)
|
||||||
C.SetPriority(C.DWORD(proc.Pid), 0x00000080)
|
|
||||||
|
tk := time.NewTicker(time.Minute)
|
||||||
|
defer tk.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sig:
|
||||||
|
log.Println("exit")
|
||||||
|
return
|
||||||
|
case <-tk.C:
|
||||||
|
procs, err := process.Processes()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("list processes: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, p := range procs {
|
||||||
|
m, err := p.MemoryInfo()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m.RSS >= memThreshold {
|
||||||
|
if err := setPriority(p.Pid, targetPriority); err != nil {
|
||||||
|
log.Printf("PID %d: %v", p.Pid, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
runtime.GC()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user