Which threads are running on a given CPU

We had a need to see which threads were running on a given CPU, as utilization was not balanced amongst the cores.

We used what is below to do so. In our case, our java PID was 488.

-bash-4.1$ jstack 488 > l.txt; for p in $(ps -eL -o pid,psr,lwp | grep 488 | awk '$2 == 0 {print $NF}'); do
>   PID=$(printf "%x\n" $p)
>   grep "nid=0x${PID}" l.txt
> done | awk '{gsub("[0-9]","",$1); print $1}' | sort | uniq -c | sort -n
      1 "ajp-...--Acceptor-"
      1 "/atg/deployment/file/FileDeploymentServer-ConnectionAcceptor"
      1 "/atg/dynamo/server/SQLRepositoryEventServer_production-ConnectionAcceptor"
      1 "/atg/dynamo/server/SQLRepositoryEventServer_production-SendAsynchronousGSAEventThread"
      1 "/atg/dynamo/server/SQLRepositoryEventServer-SendAsynchronousGSAEventThread"
      1 "/atg/dynamo/service/ClientLockManager-MessageReadingThread"
      1 "/atg/dynamo/service/ClientLockManager_production-MessageReadingThread"
      1 "/atg/dynamo/service/GSACacheClientManager-MessageReadingThread"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-/atg/epub/file/ConfigEmptyDirDeleter-folderCleanup"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-JMS-POLL"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-MSG-LIMBO-POLL"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-null"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-OrderSharderRotationServcie"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-PlaceScheduledOrders"
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-Reset
      1 "/atg/dynamo/service/Scheduler-reusablejobhandler-SitemapWriterService"
      1 "/atg/reporting/datacollection/commerce/ClaimableLogEntryQueueSink-"
      1 "/atg/reporting/datacollection/commerce/OrderLogEntryQueueSink-"
      1 "/atg/reporting/datacollection/search/EnvironmentLogEntryQueueSink-"
      1 "/atg/reporting/datacollection/search/QueryLogEntryQueueSink-"
      1 "Attach
      1 ".data"
      1 "Gang
      1 "IdleRemover"
      1 "JBossLifeThread"
      1 "JCA
      1 "JmDNS.SocketListener"
      1 "MultiThreadedHttpConnectionManager
      1 "net.sf.ehcache.CacheManager@ba"
      1 "New
      1 "QueueItemChangedThread"
      1 "Thread-"
      2 "http-...--Acceptor-"
      2 "RMI
      3 "/atg/dynamo/service/Scheduler-reusablejobhandler-reinitialize
     19 "/atg/dynamo/server/SQLRepositoryEventServer_production-"
     41 "pool--thread-"
     81 "http-...--"
     93 "/atg/dynamo/server/SQLRepositoryEventServer-"
-bash-4.1$

2 comments for “Which threads are running on a given CPU

  1. November 29, 2013 at 5:13 AM

    Hi ,
    19 “/atg/dynamo/server/SQLRepositoryEventServer_production-”
    41 “pool–thread-”
    81 “http-…–”
    93 “/atg/dynamo/server/SQLRepositoryEventServer-”

    What do these last lines refer to ? are 19,41,81 ,93 processor id’s . I think not :-). Please clarify.

  2. November 29, 2013 at 8:32 AM

    Hi Nomstradamus,

    That is the number of that thread type in the java virtual machine that is running on a given processor, in this case, processor 0. This was a really specific example to show which java threads run on which CPU.

    If you are only interested in which processes or threads are running on a given CPU/core, you can just pull the ps part of the ugly command line above.

    Something like, the following, where the first column is the processsor id and the second column is the count of processes/threads on that processor.

    -bash-4.1$ ps -Lp 1671 -o pid,psr,lwp | awk ‘{s[$2]++} END {for (i in s) {printf(“%-5i %i\n”,i,s[i])}}’ | sort -n
    0 1
    0 104
    1 91
    2 77
    3 78
    4 85
    5 94
    6 94
    7 95
    8 12
    9 14
    10 22
    11 13
    12 15
    13 25
    14 25
    15 18
    -bash-4.1$

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.