procstat - displays linux proc stat (/proc/pid/stat) in human-readable format

本文涉及的产品
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
公网NAT网关,每月750个小时 15CU
简介: procstat.c: /* * Displays linux /proc/pid/stat in human-readable format * * Build: gcc -o procstat procstat.c * Usage: procstat pid * cat /proc/pid/stat | procstat * * Homepage: h

procstat.c:

/*
 * Displays linux /proc/pid/stat in human-readable format
 *
 * Build: gcc -o procstat procstat.c
 * Usage: procstat pid
 *        cat /proc/pid/stat | procstat
 *
 * Homepage: http://www.brokestream.com/procstat.html
 * Version : 2009-03-05
 *
 * Ivan Tikhonov, http://www.brokestream.com, kefeer@netangels.ru
 *
 * 2007-09-19 changed HZ=100 error to warning
 *
 * 2009-03-05 tickspersec are taken from sysconf (Sabuj Pattanayek)
 *
 */


/* Copyright (C) 2009 Ivan Tikhonov

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Ivan Tikhonov, kefeer@brokestream.com

*/

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <linux/limits.h>
#include <sys/times.h>


typedef long long int num;

num pid;
char tcomm[PATH_MAX];
char state;

num ppid;
num pgid;
num sid;
num tty_nr;
num tty_pgrp;

num flags;
num min_flt;
num cmin_flt;
num maj_flt;
num cmaj_flt;
num utime;
num stimev;

num cutime;
num cstime;
num priority;
num nicev;
num num_threads;
num it_real_value;

unsigned long long start_time;

num vsize;
num rss;
num rsslim;
num start_code;
num end_code;
num start_stack;
num esp;
num eip;

num pending;
num blocked;
num sigign;
num sigcatch;
num wchan;
num zero1;
num zero2;
num exit_signal;
num cpu;
num rt_priority;
num policy;

long tickspersec;

FILE *input;

void readone(num *x) { fscanf(input, "%lld ", x); }
void readunsigned(unsigned long long *x) { fscanf(input, "%llu ", x); }
void readstr(char *x) {  fscanf(input, "%s ", x);}
void readchar(char *x) {  fscanf(input, "%c ", x);}

void printone(char *name, num x) {  printf("%20s: %lld\n", name, x);}
void printonex(char *name, num x) {  printf("%20s: %016llx\n", name, x);}
void printunsigned(char *name, unsigned long long x) {  printf("%20s: %llu\n", name, x);}
void printchar(char *name, char x) {  printf("%20s: %c\n", name, x);}
void printstr(char *name, char *x) {  printf("%20s: %s\n", name, x);}
void printtime(char *name, num x) {  printf("%20s: %f\n", name, (((double)x) / tickspersec));}

int gettimesinceboot() {
  FILE *procuptime;
  int sec, ssec;

  procuptime = fopen("/proc/uptime", "r");
  fscanf(procuptime, "%d.%ds", &sec, &ssec);
  fclose(procuptime);
  return (sec*tickspersec)+ssec;
}

void printtimediff(char *name, num x) {
  int sinceboot = gettimesinceboot();
  int running = sinceboot - x;
  time_t rt = time(NULL) - (running / tickspersec);
  char buf[1024];

  strftime(buf, sizeof(buf), "%m.%d %H:%M", localtime(&rt));
  printf("%20s: %s (%u.%us)\n", name, buf, running / tickspersec, running % tickspersec);
}

int main(int argc, char *argv[]) {
  tickspersec = sysconf(_SC_CLK_TCK);
  input = NULL;

  if(argc > 1) {
    chdir("/proc");
    if(chdir(argv[1]) == 0) { input = fopen("stat", "r"); }
    if(!input) {
      perror("open");
      return 1;
    }
  } else {
    input = stdin;
  }


  readone(&pid);
  readstr(tcomm);
  readchar(&state);
  readone(&ppid);
  readone(&pgid);
  readone(&sid);
  readone(&tty_nr);
  readone(&tty_pgrp);
  readone(&flags);
  readone(&min_flt);
  readone(&cmin_flt);
  readone(&maj_flt);
  readone(&cmaj_flt);
  readone(&utime);
  readone(&stimev);
  readone(&cutime);
  readone(&cstime);
  readone(&priority);
  readone(&nicev);
  readone(&num_threads);
  readone(&it_real_value);
  readunsigned(&start_time);
  readone(&vsize);
  readone(&rss);
  readone(&rsslim);
  readone(&start_code);
  readone(&end_code);
  readone(&start_stack);
  readone(&esp);
  readone(&eip);
  readone(&pending);
  readone(&blocked);
  readone(&sigign);
  readone(&sigcatch);
  readone(&wchan);
  readone(&zero1);
  readone(&zero2);
  readone(&exit_signal);
  readone(&cpu);
  readone(&rt_priority);
  readone(&policy);

  {

    printone("pid", pid);
    printstr("tcomm", tcomm);
    printchar("state", state);
    printone("ppid", ppid);
    printone("pgid", pgid);
    printone("sid", sid);
    printone("tty_nr", tty_nr);
    printone("tty_pgrp", tty_pgrp);
    printone("flags", flags);
    printone("min_flt", min_flt);
    printone("cmin_flt", cmin_flt);
    printone("maj_flt", maj_flt);
    printone("cmaj_flt", cmaj_flt);
    printtime("utime", utime);
    printtime("stime", stimev);
    printtime("cutime", cutime);
    printtime("cstime", cstime);
    printone("priority", priority);
    printone("nice", nicev);
    printone("num_threads", num_threads);
    printtime("it_real_value", it_real_value);
    printtimediff("start_time", start_time);
    printone("vsize", vsize);
    printone("rss", rss);
    printone("rsslim", rsslim);
    printone("start_code", start_code);
    printone("end_code", end_code);
    printone("start_stack", start_stack);
    printone("esp", esp);
    printone("eip", eip);
    printonex("pending", pending);
    printonex("blocked", blocked);
    printonex("sigign", sigign);
    printonex("sigcatch", sigcatch);
    printone("wchan", wchan);
    printone("zero1", zero1);
    printone("zero2", zero2);
    printonex("exit_signal", exit_signal);
    printone("cpu", cpu);
    printone("rt_priority", rt_priority);
    printone("policy", policy);
  }

  return 0;
}

Build: gcc -o procstat procstat.c

kef@flash:~$ ./procstat 1
                 pid: 1
               tcomm: (init)
               state: S
                ppid: 0
                pgid: 0
                 sid: 0
              tty_nr: 0
            tty_pgrp: -1
               flags: 8388864
             min_flt: 3706
            cmin_flt: 282132
             maj_flt: 19
            cmaj_flt: 212
               utime: 0.030000
               stime: 1.540000
              cutime: 2.840000
              cstime: 2.790000
            priority: 16
                nice: 0
         num_threads: 1
       it_real_value: 67.140000
          start_time: 11.07 09:07 (11158.38s)
               vsize: 1626112
                 rss: 130
              rsslim: 4294967295
          start_code: 134512640
            end_code: 134540142
         start_stack: 2948335296
                 esp: 2948334000
                 eip: 2817780216
             pending: 0000000000000000
             blocked: 0000000000000000
              sigign: 0000000057f0d8fc
            sigcatch: 00000000280b2603
               wchan: 0
               zero1: 0
               zero2: 0
         exit_signal: 0000000000000000
                 cpu: 0
         rt_priority: 0
              policy: 0
kef@flash:~$ cat /proc/1/stat
1 (init) S 0 0 0 0 -1 8388864 3706 282132 19 212 3 154 284 279 16 0 1 6714 115 1626112 130 4294967295 134512640 134540142 2948335296 2948334000 2817780216 0 0 1475401980 671819267 0 0 0 0 0 0 0
原文地址


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
高可用应用架构
欢迎来到“高可用应用架构”课程,本课程是“弹性计算Clouder系列认证“中的阶段四课程。本课程重点向您阐述了云服务器ECS的高可用部署方案,包含了弹性公网IP和负载均衡的概念及操作,通过本课程的学习您将了解在平时工作中,如何利用负载均衡和多台云服务器组建高可用应用架构,并通过弹性公网IP的方式对外提供稳定的互联网接入,使得您的网站更加稳定的同时可以接受更多人访问,掌握在阿里云上构建企业级大流量网站场景的方法。 学习完本课程后,您将能够: 理解高可用架构的含义并掌握基本实现方法 理解弹性公网IP的概念、功能以及应用场景 理解负载均衡的概念、功能以及应用场景 掌握网站高并发时如何处理的基本思路 完成多台Web服务器的负载均衡,从而实现高可用、高并发流量架构
目录
相关文章
|
6月前
|
缓存 监控 Linux
|
6月前
|
Linux
Linux 查看进程PID和线程CPU和内存占用情况
Linux 查看进程PID和线程CPU和内存占用情况
758 0
|
6月前
|
存储 算法 Linux
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
265 0
|
2月前
|
Oracle Java 关系型数据库
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
如果遇到"exec format error"问题,文章建议先检查Linux操作系统是32位还是64位,并确保安装了与系统匹配的JDK版本。如果系统是64位的,但出现了错误,可能是因为下载了错误的JDK版本。文章提供了一个链接,指向Oracle官网上的JDK 17 Linux版本下载页面,并附有截图说明。
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
|
3月前
|
关系型数据库 MySQL Linux
【Linux】通过 PID 获取服务信息 带你玩转 linux
【Linux】通过 PID 获取服务信息 带你玩转 linux
|
4月前
|
存储 运维 安全
Linux命令stat:深入了解文件与文件系统状态
`stat`命令在Linux中用于显示文件和文件系统的详细状态,包括权限、大小、时间戳等。它通过读取inode获取信息,特点是显示全面、易用且支持多种参数,如`-c`自定义格式,`-f`查看文件系统状态,`-L`处理符号链接。例如,`stat example.txt`显示文件详情,`stat -c &quot;%n 的大小是 %s 字节&quot; example.txt`输出文件大小。理解`stat`有助于系统管理和故障排查。
|
5月前
|
Unix Docker 容器
使用docker 启动naocs 报错出现:standard_init_linux.go:241: exec user process caused "exec format error"
```markdown Error in Docker container startup: &quot;standard_init_linux.go:241: exec user process caused \&quot;exec format error\&quot;&quot;. Occurred at 2024-06-29 09:26:19.910, followed by a failed hook with a syslog delivery error at 09:27:20.193. Seeking solutions from experts. ```
|
5月前
|
应用服务中间件 Linux nginx
蓝易云 - Linux使用pid文件结束nginx
以上就是使用pid文件结束nginx进程的方法。
34 0
|
6月前
|
Linux 开发者
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
94 1
|
6月前
|
Ubuntu Linux 编译器
Linux通过/proc/version文件
`/proc/version`文件在Linux系统中提供当前内核版本详情,属于伪文件系统 `/proc`,展示内核、硬件和进程信息。通过`cat /proc/version`可查看,如`Linux version 5.4.0-80-generic...`,显示内核版本、编译日期等。但此文件不包含发行版信息,查询发行版详情可查看`/etc/os-release`或用`lsb_release`命令。
268 6