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

本文涉及的产品
公网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
原文地址


相关实践学习
高可用应用架构
欢迎来到“高可用应用架构”课程,本课程是“弹性计算Clouder系列认证“中的阶段四课程。本课程重点向您阐述了云服务器ECS的高可用部署方案,包含了弹性公网IP和负载均衡的概念及操作,通过本课程的学习您将了解在平时工作中,如何利用负载均衡和多台云服务器组建高可用应用架构,并通过弹性公网IP的方式对外提供稳定的互联网接入,使得您的网站更加稳定的同时可以接受更多人访问,掌握在阿里云上构建企业级大流量网站场景的方法。 学习完本课程后,您将能够: 理解高可用架构的含义并掌握基本实现方法 理解弹性公网IP的概念、功能以及应用场景 理解负载均衡的概念、功能以及应用场景 掌握网站高并发时如何处理的基本思路 完成多台Web服务器的负载均衡,从而实现高可用、高并发流量架构
目录
相关文章
|
2月前
|
缓存 监控 Linux
|
7月前
|
Linux 测试技术 开发工具
Linux的进程pid编号极限
整理本文,起源是看到知乎上的一个问题,为什么Linux的进程pid编号极限最大值( process pid max)是131070?
122 0
|
28天前
|
存储 算法 Linux
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
【Linux系统编程】Linux 文件系统探究:深入理解 struct dirent、DIR 和 struct stat结构
40 0
|
18天前
|
Ubuntu Linux 编译器
Linux通过/proc/version文件
`/proc/version`文件在Linux系统中提供当前内核版本详情,属于伪文件系统 `/proc`,展示内核、硬件和进程信息。通过`cat /proc/version`可查看,如`Linux version 5.4.0-80-generic...`,显示内核版本、编译日期等。但此文件不包含发行版信息,查询发行版详情可查看`/etc/os-release`或用`lsb_release`命令。
23 6
|
存储 监控 安全
深度剖析Linux进程的内部机制:一探/proc/pid的奥秘
深度剖析Linux进程的内部机制:一探/proc/pid的奥秘
82 0
|
3月前
|
存储 Linux C语言
Linux获取文件信息的利器stat,fstat,lstat,fstatat
stat系列函数是C语言中的一个系统调用函数,用于获取文件的信息。通过提供文件路径,它能够返回包含文件属性的结构体数据。
38 0
|
3月前
|
存储 Linux Shell
Linux内核追踪(一):proc/sys/debugfs
Linux内核追踪(一):proc/sys/debugfs
60 0
|
4月前
|
安全 Linux Shell
Linux命令(98)之stat
Linux命令(98)之stat
24 0
|
4月前
|
存储 Linux
Linux文件编程(lseek函数和stat函数)
Linux文件编程(lseek函数和stat函数)
36 0
Linux文件编程(lseek函数和stat函数)
|
5月前
|
关系型数据库 MySQL Linux
Linux下MySQL起动报错The server quit without updating PID file
Linux下MySQL起动报错The server quit without updating PID file