Skip to content

Commit

Permalink
memory-monitor: Simplify cgroup task handling in the handler script.
Browse files Browse the repository at this point in the history
Simplify the process of extracting PIDs from a cgroup. Instead of using
a temporary file and filtering tasks to find unique PIDs based on TIDs,
the script now directly reads the `cgroup.procs` file to retrieve the
PIDs. This approach reduces complexity, removes the need for
intermediate processing, and improves the efficiency of PID retrieval.
The redundant temporary task list creation has been removed to
streamline the code.

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Oct 16, 2024
1 parent 472daee commit 0a6aedf
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions pkg/memory-monitor/src/monitor/memory-monitor-handler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,11 @@ find_pids_of_cgroup() {
path=$1
tempfile=$2

# Get a copy of the list of tasks in the cgroup, not to block the cgroup while handling
tmp_tasks=$(mktemp)
cat "$path"/tasks > "$tmp_tasks"

# List all tasks, filter out unique PIDs
while read -r tid; do
if [ -z "$tid" ]; then
continue
fi
# Find the main PID for each TID
pid=$(awk '/^Tgid:/ {print $2}' "/proc/$tid/status" 2>/dev/null)
if [ -n "$pid" ]; then
# Get the PIDs of the cgroup
pids=$(cat "$path"/cgroup.procs)
for pid in $pids; do
echo "$pid" >> "$tempfile"
fi
done < "$tmp_tasks"

rm "$tmp_tasks"
done

# Recurse into subdirectories
for subdir in "$path"/*/; do
Expand Down

0 comments on commit 0a6aedf

Please sign in to comment.