Skip to content

Commit 305175f

Browse files
Update to v1.5.6
- Now daemon does not try to find matching sections every time for already mismatched processes, another optimization update. - Removed annoying even for me output related to cache. - Fixed a bug when PID associated with executable path of process when obtaining info about process.
1 parent 6cc6daf commit 305175f

1 file changed

Lines changed: 71 additions & 53 deletions

File tree

flux

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extract_process_info(){
119119
process_pid="$(xprop -id "$window_id" _NET_WM_PID)"
120120
if [[ "$process_pid" != "_NET_WM_PID: not found." ]]; then
121121
process_pid="${process_pid/* = /}"
122-
# Check if info about process exists in cache
122+
# Check if info about process exists in cache and it is not marked as mismatched
123123
if [[ -z "${cache_process_name["$process_pid"]}" ]]; then
124124
# Extract name of process
125125
process_name="$(<"/proc/$process_pid/comm")"
@@ -147,19 +147,17 @@ extract_process_info(){
147147
process_command="${process_command/%\ /}"
148148
# Add all variables to cache
149149
cache_process_name["$process_pid"]="$process_name"
150-
cache_process_executable["$process_pid"]="$process_pid"
150+
cache_process_executable["$process_pid"]="$process_executable"
151151
cache_process_owner["$process_pid"]="$process_owner"
152152
cache_process_command["$process_pid"]="$process_command"
153153
# Save PID to array to make it easier to remove info from cache in case process does not exist
154154
cached_pids_array+=("$process_pid")
155-
print_verbose "Process '$process_name' with PID $process_pid information is stored to cache."
156155
else
157156
# Set values from cache
158157
process_name="${cache_process_name["$process_pid"]}"
159158
process_executable="${cache_process_executable["$process_pid"]}"
160159
process_owner="${cache_process_owner["$process_pid"]}"
161160
process_command="${cache_process_command["$process_pid"]}"
162-
print_verbose "Process '$process_name' with PID $process_pid information is obtained from cache."
163161
fi
164162
else
165163
process_pid=''
@@ -276,6 +274,11 @@ Changelog for 1.5.4:
276274
Changelog for 1.5.5:
277275
- Fixed a bug when daemon exits with error if `cpu-limit` key is specified to `-1%` (i.e. disabled) along with `fps-limit`.
278276
- Small fixes and improvements.
277+
278+
Changelog for 1.5.6:
279+
- Now daemon does not try to find matching sections every time for already mismatched processes, another optimization update.
280+
- Removed annoying even for me output related to cache.
281+
- Fixed a bug when PID associated with executable path of process when obtaining info about process.
279282
'
280283
exit 0
281284
;;
@@ -390,7 +393,7 @@ Options and values:
390393
shift 1
391394
;;
392395
--version | -V )
393-
echo "flux 1.5.5
396+
echo "flux 1.5.6
394397
A daemon for X11 designed to automatically limit CPU usage of unfocused windows and run commands on focus and unfocus events.
395398
License: GPL-3.0
396399
Repository: https://github.com/itz-me-zappex/flux
@@ -459,7 +462,8 @@ max_cpu_limit="$(( cpu_threads * 100 ))"
459462
unset cpuinfo_line
460463

461464
# Create associative arrays to store values from config
462-
declare -A config_key_name \
465+
declare -A \
466+
config_key_name \
463467
config_key_executable \
464468
config_key_owner \
465469
config_key_cpu_limit \
@@ -638,16 +642,23 @@ done
638642
unset section_from_array
639643

640644
# Declare associative arrays to store info about applied actions
641-
declare -A is_frozen_pid # For marking frozen processes (PIDs)
642-
declare -A freeze_subrocess_pid # For subprocesses to freeze with delay
643-
declare -A is_cpu_limited_pid # For marking CPU-limited processes (PIDs)
644-
declare -A cpulimit_subprocess_pid # For cpulimit subprocesses
645-
declare -A is_fps_limited_section # For marking FPS-limited processes (sections)
646-
declare -A fps_limit_subprocess_pid # For subprocesses to apply FPS-limit with delay
647-
declare -A fps_limited_pid # To print PID of process in case daemon exit
645+
declare -A \
646+
is_frozen_pid \
647+
freeze_subrocess_pid \
648+
is_cpu_limited_pid \
649+
cpulimit_subprocess_pid \
650+
is_fps_limited_section \
651+
fps_limit_subprocess_pid \
652+
fps_limited_pid
648653

649654
# Declare associative arrays to store info about windows to avoid obtaining it every time to speed up code and reduce CPU-usage
650-
declare -A cache_process_name cache_process_executable cache_process_owner cache_process_command cache_section
655+
declare -A \
656+
cache_process_name \
657+
cache_process_executable \
658+
cache_process_owner \
659+
cache_process_command \
660+
cache_section \
661+
cache_mismatch
651662

652663
# Dumbass protection, exit with an error if that is not a X11 session
653664
if [[ "$XDG_SESSION_TYPE" != 'x11' ]]; then
@@ -690,6 +701,7 @@ while read -r window_id; do
690701
cache_process_owner["$cached_pid"]=''
691702
cache_process_command["$cached_pid"]=''
692703
cache_section["$cached_pid"]=''
704+
cache_mismatch["$cached_pid"]=''
693705
cached_pids_to_remove_array+=("$cached_pid")
694706
fi
695707
done
@@ -738,49 +750,55 @@ while read -r window_id; do
738750
if [[ -n "$process_pid" ]]; then
739751
# Find matching section if was not found previously and store it to cache
740752
if [[ -z "${cache_section["$process_pid"]}" ]]; then
741-
# Attempt to find a matching section in config
742-
for section_from_array in "${sections_array[@]}"; do
743-
# Compare process name with specified in section
744-
if [[ -n "${config_key_name["$section_from_array"]}" && "${config_key_name["$section_from_array"]}" != "$process_name" ]]; then
745-
continue
746-
else
747-
name_match='1'
748-
fi
749-
# Compare process executable path with specified in section
750-
if [[ -n "${config_key_executable["$section_from_array"]}" && "${config_key_executable["$section_from_array"]}" != "$process_executable" ]]; then
751-
continue
752-
else
753-
executable_match='1'
754-
fi
755-
# Compare UID of process with specified in section
756-
if [[ -n "${config_key_owner["$section_from_array"]}" && "${config_key_owner["$section_from_array"]}" != "$process_owner" ]]; then
757-
continue
758-
else
759-
owner_match='1'
760-
fi
761-
# Compare process command with specified in section
762-
if [[ -n "${config_key_command["$section_from_array"]}" && "${config_key_command["$section_from_array"]}" != "$process_command" ]]; then
763-
continue
764-
else
765-
command_match='1'
766-
fi
767-
# Mark as matching if all identifiers containing non-zero value
768-
if [[ -n "$name_match" && -n "$executable_match" && -n "$owner_match" && -n "$command_match" ]]; then
769-
section_name="$section_from_array"
770-
cache_section["$process_pid"]="$section_from_array"
771-
break
753+
# Avoid searching for matching section if it was not found previously
754+
if [[ -z "${cache_mismatch["$process_pid"]}" ]]; then
755+
# Attempt to find a matching section in config
756+
for section_from_array in "${sections_array[@]}"; do
757+
# Compare process name with specified in section
758+
if [[ -n "${config_key_name["$section_from_array"]}" && "${config_key_name["$section_from_array"]}" != "$process_name" ]]; then
759+
continue
760+
else
761+
name_match='1'
762+
fi
763+
# Compare process executable path with specified in section
764+
if [[ -n "${config_key_executable["$section_from_array"]}" && "${config_key_executable["$section_from_array"]}" != "$process_executable" ]]; then
765+
continue
766+
else
767+
executable_match='1'
768+
fi
769+
# Compare UID of process with specified in section
770+
if [[ -n "${config_key_owner["$section_from_array"]}" && "${config_key_owner["$section_from_array"]}" != "$process_owner" ]]; then
771+
continue
772+
else
773+
owner_match='1'
774+
fi
775+
# Compare process command with specified in section
776+
if [[ -n "${config_key_command["$section_from_array"]}" && "${config_key_command["$section_from_array"]}" != "$process_command" ]]; then
777+
continue
778+
else
779+
command_match='1'
780+
fi
781+
# Mark as matching if all identifiers containing non-zero value
782+
if [[ -n "$name_match" && -n "$executable_match" && -n "$owner_match" && -n "$command_match" ]]; then
783+
section_name="$section_from_array"
784+
cache_section["$process_pid"]="$section_from_array"
785+
break
786+
fi
787+
unset name_match executable_match owner_match command_match
788+
done
789+
unset section_from_array name_match executable_match owner_match command_match
790+
# Mark process as mismatched if matching section was not found
791+
if [[ -z "$section_name" ]]; then
792+
cache_mismatch["$process_pid"]='1'
772793
fi
773-
unset name_match executable_match owner_match command_match
774-
done
775-
unset section_from_array name_match executable_match owner_match command_match
776-
if [[ -n "$section_name" ]]; then
777-
print_verbose "Matching section '$section_name' for process '$process_name' with PID $process_pid is stored to cache."
778-
else
779-
print_verbose "Process '$process_name' with PID $process_pid does not match with any section."
780794
fi
781795
else # Obtain matching section from cache
782796
section_name="${cache_section["$process_pid"]}"
783-
print_verbose "Matching section '$section_name' for process '$process_name' with PID $process_pid is obtained from cache."
797+
fi
798+
if [[ -n "$section_name" ]]; then
799+
print_verbose "Process '$process_name' with PID $process_pid matches with section '$section_name'."
800+
else
801+
print_verbose "Process '$process_name' with PID $process_pid does not match with any section."
784802
fi
785803
fi
786804
# Check if PID is not the same as previous one

0 commit comments

Comments
 (0)