Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace the list of PIDs running on a specific core?

I'm trying to run a program on a dedicated core in Linux. (I know Jailhouse is a good way to do so, but I have to use off-the-shelf Linux. :-( )

Other processes, such as interrupt handlers, kernel threads, service progresses, may also run on the dedicated core occasionally. I want to disable as many such processes as possible. To do that, I need first pin down the list of processes that may run on the dedicated core.

My question is:

Is there any existing tools that I can use to trace the list of PIDs or processes that run on a specific core over a time interval?

Thank you very much for your time and help in this question!

like image 739
Mike Avatar asked Jun 01 '16 18:06

Mike


1 Answers

TL;DR Dirty hacky solution.

DISCLAIMER: At some point stops working "column: line too long" :-/

Copy this to: core-pids.sh

#!/bin/bash

TARGET_CPU=0

touch lastPIDs
touch CPU_PIDs

while true; do
  ps ax -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
  for i in {1..100}; do printf "#\n" >> lastPIDs; done
  cp CPU_PIDs aux
  paste lastPIDs aux > CPU_PIDs
  column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
  sleep 1
done

Then

chmod +x core-pids.sh
./core-pids.sh

Then open CPU_PIDs.humanfriendly.tsv with your favorite editor, and ¡inspect!

The key is in the "ps -o cpuid,pid" bit, for more detailed info, please comment. :D

Explanation

Infinite loop with

  • ps -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
    • ps ax -o cpuid,pid
      • Show pid's associated to CPU
    • tail -n +2
      • remove headers
    • sort
      • sort by cpuid
    • xargs -n 2
      • remove white spaces at begging
    • grep -E "^$TARGET_CPU"
      • filter by CPU id
    • awk '{print $2}'
      • get pid column
    • > lastPIDs
      • output to file those las pid's for the target CPU id
  • for i in {1..10}; do printf "#\n" >> lastPIDs; done
    • hack for pretty .tsv print with the "columns -t" command
  • cp CPU_PIDs aux
    • CPU_PIDs holds the whole timeline, we copy it to aux file to allow the next command to use it as input and output
  • paste lastPIDs aux > CPU_PIDs
    • Append lastPIDs columns to the whole timeline file CPU_PIDs
  • column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
    • pretty print whole timeline CPU_PIDs file

Attribution

  • stackoverflow answer to: ps utility in linux (procps), how to check which CPU is used
    • by Mikel
  • stackoverflow answer to: Echo newline in Bash prints literal \n
    • by sth
  • stackoverflow answer to: shell variable in a grep regex
    • by David W.
  • superuser answer to: Aligning columns in output from a UNIX command
    • Janne Pikkarainen
  • nixCraft article: HowTo: Unix For Loop 1 to 100 Numbers
like image 77
ElMesa Avatar answered Nov 15 '22 03:11

ElMesa