Skip to content

Commit

Permalink
1.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vinifmor authored Aug 12, 2023
2 parents 69f41c8 + 818601c commit a341a58
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 215 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.3.3] 2023-08-12

### Improvements
- optimizer: ignoring more unneeded EA launcher and Wine child processes

### Fixes
- watcher/optimizer: not able to map processes alive with `ps` command version >= 4.0.X

## [1.3.2] 2023-01-30
### Improvements
- replaced some subprocess calls executed in behalf of non-root users by async calls (Python's asyncio native approach)
Expand Down
2 changes: 1 addition & 1 deletion guapow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
__app_name__ = 'guapow'
__version__ = '1.3.2'
__version__ = '1.3.3'
30 changes: 16 additions & 14 deletions guapow/common/system.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
import re
import subprocess
import traceback
from datetime import datetime, timedelta
Expand All @@ -9,6 +10,7 @@

BAD_USER_ENV_VARS = {'LD_PRELOAD'}
T = TypeVar('T')
RE_SEVERAL_SPACES = re.compile(r'\s+')


class ProcessTimedOutError(Exception):
Expand Down Expand Up @@ -124,7 +126,7 @@ def _match(line: str) -> Optional[Tuple[int, str]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
name = line_split[1].strip()
Expand All @@ -134,7 +136,7 @@ def _match(line: str) -> Optional[Tuple[int, str]]:
except ValueError:
return

return await match_syscall(cmd=f'ps -Ao "%p#%c" -ww --no-headers --sort={"-" if last_match else ""}pid',
return await match_syscall(cmd=f'ps -Ao pid,comm -ww --no-headers --sort={"-" if last_match else ""}pid',
match=_match)


Expand All @@ -143,7 +145,7 @@ def _match(line: str) -> Optional[Tuple[int, str]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
for p in patterns:
Expand All @@ -154,7 +156,7 @@ def _match(line: str) -> Optional[Tuple[int, str]]:
except ValueError:
break

return await match_syscall(cmd=f'ps -Ao "%p#%a" -ww --no-headers --sort={"-" if last_match else ""}pid',
return await match_syscall(cmd=f'ps -Ao pid,args -ww --no-headers --sort={"-" if last_match else ""}pid',
match=_match)


Expand All @@ -165,7 +167,7 @@ def _match(line: str) -> Optional[Dict[str, int]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
cmd = line_split[1].strip()
Expand All @@ -178,7 +180,7 @@ def _match(line: str) -> Optional[Dict[str, int]]:
if len(matches) == len(commands):
return matches

await match_syscall(cmd=f'ps -Ao "%p#%a" -ww --no-headers --sort={"-" if last_match else ""}pid', match=_match)
await match_syscall(cmd=f'ps -Ao pid,args -ww --no-headers --sort={"-" if last_match else ""}pid', match=_match)
return matches if matches else None


Expand All @@ -189,7 +191,7 @@ def _match(line: str) -> Optional[Dict[str, int]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
current_name = line_split[1].strip()
Expand All @@ -203,7 +205,7 @@ def _match(line: str) -> Optional[Dict[str, int]]:
if len(matches) == len(names):
return matches

await match_syscall(cmd=f'ps -Ao "%p#%c" -ww --no-headers --sort={"-" if last_match else ""}pid', match=_match)
await match_syscall(cmd=f'ps -Ao pid,comm -ww --no-headers --sort={"-" if last_match else ""}pid', match=_match)
return matches if matches else None


Expand All @@ -215,7 +217,7 @@ def _match(line: str) -> Optional[Dict[int, str]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
try:
Expand All @@ -229,7 +231,7 @@ def _match(line: str) -> Optional[Dict[int, str]]:
if len(matches) == len(pids):
return matches

await match_syscall(cmd='ps -Ao "%p#%a" -ww --no-headers', match=_match)
await match_syscall(cmd='ps -Ao pid,args -ww --no-headers', match=_match)
return matches if matches else None


Expand All @@ -238,7 +240,7 @@ def read_current_pids() -> Set[int]:


async def map_pids_by_ppid() -> Optional[Dict[int, Set[int]]]:
code, output = await async_syscall('ps -Ao "%P#%p" -ww --no-headers')
code, output = await async_syscall('ps -Ao ppid,pid -ww --no-headers')

if code == 0 and output:
all_procs = {}
Expand All @@ -247,7 +249,7 @@ async def map_pids_by_ppid() -> Optional[Dict[int, Set[int]]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) == 2:
try:
Expand Down Expand Up @@ -348,7 +350,7 @@ async def run_async_process(cmd: str, user_id: Optional[int] = None, custom_env:


async def map_processes_by_parent() -> Dict[int, Set[Tuple[int, str]]]:
exitcode, output = await async_syscall(f'ps -Ao "%P#%p#%c" -ww --no-headers')
exitcode, output = await async_syscall(f'ps -Ao ppid,pid,comm -ww --no-headers')

if exitcode == 0 and output:
proc_tree = dict()
Expand All @@ -357,7 +359,7 @@ async def map_processes_by_parent() -> Dict[int, Set[Tuple[int, str]]]:
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 2)
line_split = RE_SEVERAL_SPACES.split(line_strip, 2)

if len(line_split) > 2:
ppid, pid, comm, = (e.strip() for e in line_split)
Expand Down
22 changes: 12 additions & 10 deletions guapow/service/optimizer/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from guapow.common import util
from guapow.common.dto import OptimizationRequest
from guapow.common.model import CustomEnum
from guapow.common.system import async_syscall, map_processes_by_parent, find_process_children
from guapow.common.system import async_syscall, map_processes_by_parent, find_process_children, RE_SEVERAL_SPACES
from guapow.common.users import is_root_user
from guapow.service.optimizer.profile import OptimizationProfile

Expand Down Expand Up @@ -129,8 +129,8 @@ def __init__(self, check_time: float, found_check_time: float, logger: Logger,
@staticmethod
async def map_process_by_pid(mode: LauncherSearchMode, ignore: Set[int]) -> Optional[Dict[int, str]]:
if mode:
mode_str = "a" if mode == LauncherSearchMode.COMMAND else "c"
exitcode, output = await async_syscall(f'ps -Ao "%p#%{mode_str}" -ww --no-headers')
mode_str = "args" if mode == LauncherSearchMode.COMMAND else "comm"
exitcode, output = await async_syscall(f'ps -Ao pid,{mode_str} -ww --no-headers')

if exitcode == 0 and output:
pid_comm = dict()
Expand All @@ -139,7 +139,7 @@ async def map_process_by_pid(mode: LauncherSearchMode, ignore: Set[int]) -> Opti
line_strip = line.strip()

if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
try:
Expand Down Expand Up @@ -261,13 +261,15 @@ def to_ignore(self) -> Set[str]:
if self._to_ignore is None:
self._to_ignore = {"wineserver", "services.exe", "winedevice.exe", "plugplay.exe", "svchost.exe",
"explorer.exe", "rpcss.exe", "tabtip.exe", "wine", "wine64", "wineboot.exe",
"cmd.exe", "conhost.exe", "start.exe", "steam-runtime-l", "proton", "gzip",
"steam.exe", "python", "python3", "OriginWebHelper", "Origin.exe",
"cmd.exe", "conhost.exe", "start.exe", "winemenubuilder", "steam-runtime-l", "proton",
"gzip", "steam.exe", "python", "python3", "OriginWebHelper", "Origin.exe",
"OriginClientSer", "QtWebEngineProc", "EASteamProxy.ex", "ActivationUI.ex",
"EALink.exe", "OriginLegacyCLI", "IGOProxy.exe", "IGOProxy64.exe", "igoproxy64.exe",
"ldconfig", "UPlayBrowser.exe", "UbisoftGameLaun", "upc.exe", "UplayService.ex",
"UplayWebCore.ex", "CrRendererMain", "regsvr32", "CrGpuMain", "CrUtilityMain",
"whql:off", "PnkBstrA.exe"}
"EALink.exe", "OriginLegacyCLI", "IGOProxy.exe", "IGOProxy32.exe", "IGOProxy64.exe",
"igoproxy64.exe", "ldconfig", "UPlayBrowser.exe", "UbisoftGameLaun", "upc.exe",
"UplayService.ex", "UplayWebCore.ex", "CrRendererMain", "regsvr32", "CrGpuMain",
"CrUtilityMain", "whql:off", "PnkBstrA.exe", "EABackgroundSer", "EADesktop.exe",
"EALocalHostSvc.", "EADestager.exe", "EALaunchHelper", "Link2EA.exe",
"ThreadPoolSingl ", "CrBrowserMain", "rundll32.exe", "iexplore.exe", "UnityCrashHandl"}

return self._to_ignore

Expand Down
8 changes: 4 additions & 4 deletions guapow/service/watcher/util.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import asyncio
from typing import Dict, Tuple

from guapow.common.system import async_syscall
from guapow.common.system import async_syscall, RE_SEVERAL_SPACES


async def _map_processes_by_attribute(attr: str) -> Dict[int, str]:
code, output = await async_syscall(f'ps -Ao "%p#%{attr}" -ww --no-headers')
code, output = await async_syscall(f'ps -Ao pid,{attr} -ww --no-headers')

if code == 0:
procs = {}
for line in output.split('\n'):
line_strip = line.strip()
if line_strip:
line_split = line_strip.split('#', 1)
line_split = RE_SEVERAL_SPACES.split(line_strip, 1)

if len(line_split) > 1:
try:
Expand All @@ -24,7 +24,7 @@ async def _map_processes_by_attribute(attr: str) -> Dict[int, str]:


async def map_processes() -> Dict[int, Tuple[str, str]]:
pid_comm, pid_cmd = await asyncio.gather(_map_processes_by_attribute('c'), _map_processes_by_attribute('a'))
pid_comm, pid_cmd = await asyncio.gather(_map_processes_by_attribute("comm"), _map_processes_by_attribute("args"))

if pid_comm and pid_cmd:
res = {}
Expand Down
Loading

0 comments on commit a341a58

Please sign in to comment.