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/numcpus v0.6.0 // 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
|
||||
|
||||
/*
|
||||
#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 (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/postfinance/single"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
"os"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func MessageBox(lpText string, lpCaption string, uType int) int {
|
||||
var convertedCaption, _ = syscall.UTF16PtrFromString(lpCaption)
|
||||
var convertedTitle, _ = syscall.UTF16PtrFromString(lpText)
|
||||
returnCode, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
|
||||
uintptr(0),
|
||||
uintptr(unsafe.Pointer(convertedCaption)),
|
||||
uintptr(unsafe.Pointer(convertedTitle)),
|
||||
uintptr(uType),
|
||||
)
|
||||
return int(returnCode)
|
||||
}
|
||||
const (
|
||||
memThreshold = 512 * 1024 * 1024
|
||||
selfPriority = windows.IDLE_PRIORITY_CLASS
|
||||
targetPriority = windows.HIGH_PRIORITY_CLASS
|
||||
)
|
||||
|
||||
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() {
|
||||
if err := lockFile.Lock(); err != nil {
|
||||
MessageBox("The program is already running", "Only one instance of the program can be run\nYou can close it via the Task Manager", 48)
|
||||
lock, err := single.New("autoPriority")
|
||||
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)
|
||||
}
|
||||
defer lock.Unlock()
|
||||
|
||||
C.SetPriority(C.DWORD(os.Getpid()), 0x00000040)
|
||||
for range time.Tick(time.Minute) {
|
||||
processes, _ := process.Processes()
|
||||
for _, proc := range processes {
|
||||
memoryInfo, err := proc.MemoryInfo()
|
||||
if err == nil && memoryInfo.RSS >= (0.5*1024*1024*1024) {
|
||||
C.SetPriority(C.DWORD(proc.Pid), 0x00000080)
|
||||
if err := setPriority(int32(os.Getpid()), selfPriority); err != nil {
|
||||
log.Printf("set self priority: %v", err)
|
||||
}
|
||||
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, os.Interrupt)
|
||||
|
||||
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