Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何知道Puma中活动线程的数量_Shell_Monitoring_Puma - Fatal编程技术网

Shell 如何知道Puma中活动线程的数量

Shell 如何知道Puma中活动线程的数量,shell,monitoring,puma,Shell,Monitoring,Puma,我正在尝试查看服务器上活动puma线程的数量 我无法通过ps查看它: $ ps aux | grep puma healthd 2623 0.0 1.8 683168 37700 ? Ssl May02 5:38 puma 2.11.1 (tcp://127.0.0.1:22221) [healthd] root 8029 0.0 0.1 110460 2184 pts/0 S+ 06:34 0:00 grep --color=aut

我正在尝试查看服务器上活动puma线程的数量

我无法通过ps查看它:

$ ps aux | grep puma
healthd   2623  0.0  1.8 683168 37700 ?        Ssl  May02   5:38 puma 2.11.1 (tcp://127.0.0.1:22221) [healthd]  
root      8029  0.0  0.1 110460  2184 pts/0    S+   06:34   0:00 grep --color=auto puma
root     18084  0.0  0.1  56836  2664 ?        Ss   May05   0:00 su -s /bin/bash -c puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb webapp
webapp   18113  0.0  0.8  83280 17324 ?        Ssl  May05   0:04 puma 2.16.0 (unix:///var/run/puma/my_app.sock) [/]                                                               
webapp   18116  3.5  6.2 784992 128924 ?       Sl   May05 182:35 puma: cluster worker 0: 18113 [/] 
在配置中,我有:

threads 8, 32

我希望看到至少8个puma线程?

如果您只是查找进程生成的线程数量,您可以看到在
/proc/[pid of process]/task
下创建的
任务
文件夹的数量,因为每个线程在此路径下创建一个文件夹。因此,计算文件夹的数量就足够了

事实上,
ps
实用程序本身从该路径读取信息,该路径是一个文件
/proc/[PID]/cmdline
,以更可读的方式表示

/proc
非常特殊,因为它也是一个虚拟文件系统。它有时被称为过程信息伪文件系统。它不包含“真实”文件,但包含运行时系统信息(如系统内存、安装的设备、硬件配置等)。因此,它可以被视为内核的控制和信息中心。事实上,相当多的系统实用程序只是对该目录中的文件的调用

您只需使用
puma
或您选择的任何实用程序即可获得流程的PID

ps aux | awk '/[p]uma/{print $1}'
或者更直接地使用它,直接将进程名称作为输入获取PID

pidof -s puma
现在,您已经有了PID来计算流程创建的
task/
文件夹的数量,请使用
find
命令

find /proc/<PID>/task -maxdepth 1 -type d -print | wc -l
find/proc//task-maxdepth 1-type d-print | wc-l

ps aux|grep puma
将为您提供仅针对puma的流程列表。您需要了解特定进程正在运行多少线程。也许这会帮助你:

ps-T-p 2623


您需要提供进程id,您希望为其查找线程数。确保您提供了准确的进程id。

使用
ps
wc
来计数
puma
线程:

ps --no-headers -T -C puma | wc -l
ps --no-headers -T -C bash | wc -l
可以根据需要更换字符串“puma”。例如,计数
bash
线程:

ps --no-headers -T -C puma | wc -l
ps --no-headers -T -C bash | wc -l
在我的系统上,输出:

9

问题中的代码“ps aux | grep puma”存在一些与grep相关的问题:

  • 它返回
    grep--color=auto puma
    ,它根本不是
    puma
    线程
  • 类似地,任何带有字符串“puma”的util或命令,例如名为
    notpuma
    的util将与
    grep
    匹配

  • 要快速回答此问题,请输入 在给定PID上运行的进程,可以使用 以下:

    % ps -h -o nlwp <pid>
    
    进程、线程和轻量级进程之间的区别是什么?

    这个问题已经在许多地方得到了回答 [见,和]。快速、简短、丑陋的版本是:

    • 进程本质上是程序的任何运行实例

    • 线程是进程的执行流。过程 包含多个执行流称为多线程 处理并在其线程(内存、, 打开文件,io,…)。Linux内核不知道是什么
      线程是并且只知道进程。过去,,
      多线程是在用户级别而不是内核级别处理的
      水平。这使得内核很难进行正确的处理
      管理层

    • 进入轻量级流程(LWP)。这基本上是 用线程来回答这个问题。每个线程都被认为是 成为内核级的LWP。两者之间的主要区别 流程和LWP是指LWP共享资源。换句话说,轻量级进程是用户称之为线程的内核

    能否
    ps
    显示有关线程或LWP的信息?

    ps
    命令或过程状态命令提供信息 关于当前正在运行的进程,包括其相应的 LWP或螺纹。为此,它使用
    /proc
    目录 它是一个虚拟文件系统,被视为控制和 内核的信息中心。[见和]

    默认情况下,
    ps
    不会向您提供有关LWP的任何信息, 但是,将选项
    -L
    -m
    添加到命令中通常不起作用 诀窍

    man ps::线程显示

       H      Show threads as if they were processes.
       -L     Show threads, possibly with LWP and NLWP columns.
       m      Show threads after processes.
       -m     Show threads after processes.
       -T     Show threads, possibly with SPID column.
    
       lwp    lightweight process (thread) ID of the dispatchable
              entity (alias spid, tid).  See tid for additional
              information.  Show threads as if they were processes.
       nlwp   number of lwps (threads) in the process.  (alias thcount).
    
    对于单个流程
    puma
    ,pid由
    pgrep puma

    % ps -fL $(pgrep puma)
    UID        PID  PPID   LWP  C NLWP STIME TTY      STAT   TIME CMD
    kvantour  2160  2876  2160  0    4 15:22 pts/39   Sl+    0:00 ./puma
    kvantour  2160  2876  2161 99    4 15:22 pts/39   Rl+    0:14 ./puma
    kvantour  2160  2876  2162 99    4 15:22 pts/39   Rl+    0:14 ./puma
    kvantour  2160  2876  2163 99    4 15:22 pts/39   Rl+    0:14 ./puma
    
    但是,添加
    -m
    选项显然可以提供更好的概述。这 当多个进程以相同的方式运行时,此功能尤其方便 名字

    在本例中,您可以看到PID为2160的进程
    puma
    以4 ID为2160--2163的线程(NLWP)。在
    STAT
    下,您会看到两个不同的值
    Sl+
    和'Rl+'。这里的
    l
    是多线程
    的指示器
    S
    R
    分别代表可中断睡眠(等待事件完成)和运行。所以我们看到4个线程中有3个运行在99%的CPU上,一个线程处于休眠状态。 您还可以看到,当单个线程仅运行14秒时,CPU的总累计时间(44秒)

    获取信息的另一种方法是直接使用格式 带有
    -o
    -o
    的说明符

    man ps::标准格式说明符

       H      Show threads as if they were processes.
       -L     Show threads, possibly with LWP and NLWP columns.
       m      Show threads after processes.
       -m     Show threads after processes.
       -T     Show threads, possibly with SPID column.
    
       lwp    lightweight process (thread) ID of the dispatchable
              entity (alias spid, tid).  See tid for additional
              information.  Show threads as if they were processes.
       nlwp   number of lwps (threads) in the process.  (alias thcount).
    
    因此,您可以使用
    lwp
    spid
    tid
    nlwp
    thcount
    中的任何一种

    如果您只想获取一个名为
    puma
    ,您可以使用:

    % ps -o nlwp $(pgrep puma)
    NLWP
       4
    
    或者如果你不喜欢标题

    % ps -h -o nlwp $(pgrep puma)
       4
    
    您可以通过以下方式获得更多信息:

    % ps -O nlwp $(pgrep puma)
    PID   NLWP S TTY          TIME COMMAND
    19304    4 T pts/39   00:00:00 ./puma
    
    最后,您可以将标志与
    ps aux
    组合起来列出线程

     % ps aux -L
    USER       PID   LWP %CPU NLWP %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    ...
    kvantour  1618  1618  0.0    4  0.0  33260  1436 pts/39   Sl+  15:17   0:00 ./puma
    kvantour  1618  1619 99.8    4  0.0  33260  1436 pts/39   Rl+  15:17   0:14 ./puma
    kvantour  1618  1620 99.8    4  0.0  33260  1436 pts/39   Rl+  15:17   0:14 ./puma
    kvantour  1618  1621 99.8    4  0.0  33260  1436 pts/39   Rl+  15:17   0:14 ./puma
    ...
    
    可以
    top
    显示有关线程的信息
    % top -H -p $(pgrep puma)
    top - 09:52:48 up 17 days, 14 min,  1 user,  load average: 3.31, 3.38, 3.10
    Threads:   4 total,   3 running,   1 sleeping,   0 stopped,   0 zombie
    %Cpu(s): 75.5 us,  0.1 sy,  0.2 ni, 23.6 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
    KiB Mem : 16310772 total,  8041048 free,  3706460 used,  4563264 buff/cache
    KiB Swap:  4194300 total,  4194300 free,        0 used. 11325008 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
      869 kvantour  20   0   33268   1436   1308 R 99.9  0.0  26:03.37 puma
      870 kvantour  20   0   33268   1436   1308 R 99.9  0.0  26:03.30 puma
      872 kvantour  20   0   33268   1436   1308 R 99.9  0.0  26:03.22 puma
      868 kvantour  20   0   33268   1436   1308 S  0.0  0.0   0:00.00 puma
    
    % grep Threads /proc/19304/status
    Threads:        4
    
    #include <omp.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc, char *argv[]) {
    char c = 0;
    #pragma omp parallel shared(c)   {
        int i = 0;
        if (omp_get_thread_num() == 0) {
          printf("Read character from input : ");
          c = getchar();
        } else {
          while (c == 0) i++;
          printf("Total sum is on thread %d : %d\n", omp_get_thread_num(), i);
        }
      }
    }
    
    ps aux | awk '/[p]uma/{print $2}' | xargs ps -h -o nlwp
    
       7
      59
      59
      61
      59
      60
      59
      59
      59