Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Split ananicy by freq/rare rules in runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
  • Loading branch information
nefelim4ag committed Dec 10, 2017
1 parent 81187f6 commit 5e83682
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions ananicy
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ YN(){
esac
}

INFO_ENABLED=true
WARN_ENABLED=true
ERRO_ENABLED=true
export INFO_ENABLED=true
export WARN_ENABLED=true
export ERRO_ENABLED=true

TMP="$(mktemp -u)"

INFO(){ YN $INFO_ENABLED || return; echo "INFO: $*";}
WARN(){ YN $WARN_ENABLED || return; echo "WARN: $*";}
ERRO(){ YN $ERRO_ENABLED || exit 1; echo "ERRO: $*"; exit 1;}

export WORK_DIR=/run/ananicy/
export DIR_CONFIGS=/etc/ananicy.d/

check_config_dir(){
## Check DIR_CONFIGS
DIR_CONFIGS=/etc/ananicy.d/
INFO "Check $DIR_CONFIGS dir"
[ -d "$DIR_CONFIGS" ] || ERRO "Config dir $DIR_CONFIGS doesn't exist!"

WORK_DIR=/run/ananicy/
mkdir -p "$WORK_DIR"
cd "$WORK_DIR" || exit 1
}
Expand Down Expand Up @@ -158,26 +158,34 @@ compile_rules(){
[ "1" == "$(ls -l RULES_CACHE/ | wc -l)" ] && ERRO "No rule is enabled!"
}

split_rules(){
mv RULES_CACHE RULES_CACHE_OFTEN
mkdir -p RULES_CACHE_RARE
}

## Show cached information
show_rules(){
INFO "Print compiled rules"
sort RULES_CACHE/*.rule | column -t
grep . -R RULES_CACHE_*/*.rule | sort | column -t
}

show_types(){
load_types
INFO "Print compiled types"
sort TYPES_CACHE/*.type | column -t
INFO "Cleanup types cache"
rm -rf
rm -rf TYPES_CACHE
}
## Helper for renice_w()
nice_of_pid(){
# 19 column in stat is a nice
# But in some cases (name of process have a spaces)
# It's break so.. use long solution
[ -f /proc/$1/stat ] || return 1
stat=( $(sed 's/) . /:/g' /proc/$1/stat 2> /dev/null | cut -d':' -f2) )
read -r LINE < /proc/$1/stat
stat=(
$(echo $LINE | sed -e 's/) . /:/g' | cut -d':' -f2)
)
echo ${stat[15]}
}

Expand Down Expand Up @@ -298,31 +306,72 @@ case $1 in
load_types
load_rules
compile_rules
split_rules
INFO "Initialization completed"
systemd-notify --ready
INFO "Start main process"
CHECK_FREQ=${CHECK_FREQ:-0.2s}
export NAME PID NICE IOCLASS IONICE SCHED OOM_SCORE_ADJ
# Thread with frequently matched rules
while [ -d "$WORK_DIR" ]; do
for RULE in RULES_CACHE/*.rule; do
for RULE in RULES_CACHE_OFTEN/*.rule; do
[ ! -d "$WORK_DIR" ] && break
[ ! -f "$RULE" ] && continue
NICE="" IOCLASS="" IONICE="" SCHED="" OOM_SCORE_ADJ=""
source "$RULE"
NAME="${NAME//\"/}"
for PID in $( pgrep -f -w "$NAME" ); do
[ -z "$PID" ] && return
PIDS=( $( pgrep -f -w "$NAME" ) )
for PID in "${PIDS[@]}"; do
[ -z "$PID" ] && break
[ ! -d "/proc/$PID" ] && continue
[ ! -z "$NICE" ] && renice_w
[ ! -z "$IOCLASS" ] && ionice_class_w
[ ! -z "$IONICE" ] && ionice_w
[ ! -z "$SCHED" ] && schedtool_policy_w
[ ! -z "$OOM_SCORE_ADJ" ] && oom_score_adj_w
done
if ((${#PIDS[@]} == 0)); then
mv $RULE RULES_CACHE_RARE/
continue
fi
# Pause before processing next rule
sleep $CHECK_FREQ
done
done
sleep $CHECK_FREQ
done &
# Thread with rarely matched rules
while [ -d "$WORK_DIR" ]; do
for RULE in RULES_CACHE_RARE/*.rule; do
[ ! -d "$WORK_DIR" ] && break
[ ! -f "$RULE" ] && continue
NICE="" IOCLASS="" IONICE="" SCHED="" OOM_SCORE_ADJ=""
source "$RULE"
NAME="${NAME//\"/}"
PIDS=( $( pgrep -f -w "$NAME" ) )
for PID in "${PIDS[@]}"; do
[ -z "$PID" ] && break
[ ! -d "/proc/$PID" ] && continue
[ ! -z "$NICE" ] && renice_w
[ ! -z "$IOCLASS" ] && ionice_class_w
[ ! -z "$IONICE" ] && ionice_w
[ ! -z "$SCHED" ] && schedtool_policy_w
[ ! -z "$OOM_SCORE_ADJ" ] && oom_score_adj_w
done
if ((${#PIDS[@]} > 0)); then
mv $RULE RULES_CACHE_OFTEN/
continue
fi
# Pause before processing next rule
sleep $CHECK_FREQ
sleep $CHECK_FREQ
sleep $CHECK_FREQ
sleep $CHECK_FREQ
sleep $CHECK_FREQ
done
sleep $CHECK_FREQ
sleep $CHECK_FREQ
done &
wait
;;
stop) force_exit ;;
reload)
Expand All @@ -333,6 +382,7 @@ case $1 in
load_types
load_rules
compile_rules
split_rules
force_exit(){ :; }
;;
dump)
Expand Down

0 comments on commit 5e83682

Please sign in to comment.