sysstat - pidstat

Linux: sysstat;pidstat 文章目录简介软件包含有的文件任务配置服务sysstat.servicesysstat-collect.timer 和 sysstat-collect.servicesysstat-summary.timer 和 sysstat-summary.service/usr/lib64/sa/sa1sadc 简介 通过systemd提供的timer机制,定时收集stat数据写道/var/log/sa/目录;每天一个sa日志文件。同时每天23点53分,会将sa二进制文件解析成文本文件放到sar文件中。每天的零点7分 阅读详情

# ./pidstat --help
Usage: ./pidstat [ options ] [ <interval> [ <count> ] ] [ -e <program> <args> ]
Options are:
[ -d ] [ -H ] [ -h ] [ -I ] [ -l ] [ -R ] [ -r ] [ -s ] [ -t ] [ -U [ <username> ] ]
[ -u ] [ -V ] [ -v ] [ -w ] [ -C <command> ] [ -G <process_name> ]
[ -p { <pid> [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ]
[ --dec={ 0 | 1 | 2 } ] [ --human ]

报告 Linux 任务(进程)的统计信息:I/O、CPU、内存等。

pidstat manual page


CPU utilization

选项

-u

指标

%usr

在用户级别(应用程序)执行时任务使用的 CPU 百分比,无论是否有 nice priority。

请注意,此字段不包括运行虚拟处理器所花费的时间。

%system

在系统级别(内核)执行时任务使用的 CPU 百分比。

%guest

任务在虚拟机(运行虚拟处理器)中花费的 CPU 百分比。

%wait

任务在等待运行时花费的CPU的百分比。

%CPU

任务使用的 CPU 时间的总百分比。

在 SMP 环境中,如果在命令行中输入了选项 -I,则任务的 CPU 使用率将除以 CPU 的总数。

CPU

任务所属的处理器编号。


I/O Statistics

选项

-d

指标

kB_rd/s

任务导致每秒从磁盘读取的千字节数。

kB_wr/s

任务已导致或将导致每秒写入磁盘的千字节数。

kB_ccwr/s

任务取消了写入磁盘的千字节数。

这可能发生在任务截断一些脏的分页缓存时。在这种情况下,一些被其他任务占去的IO将不会发生。

iodelay

被监测的任务的块I/O延迟,以时钟滴答为单位测量。

这个指标包括等待同步块I/O完成和交换块I/O完成的延迟。


Priority And Scheduling Policy

实时优先级和调度策略信息

选项

-R

指标

prio

被监视任务的实时优先级。

policy

被监控任务的调度策略。


Page Faults And Memory Utilization

选项

-r

指标

minflt/s

该任务每秒钟发生的小故障(minor faults)总数,那些不需要从磁盘加载内存页的故障。

majflt/s

任务每秒发生的主要故障(major faults)总数,需要从磁盘加载内存页面的故障总数。

VSZ

虚拟大小:整个任务的虚拟内存使用量,以千字节为单位。

RSS

Resident Set Size: 任务使用的非交换物理内存,单位是千字节。

%MEM

任务当前使用的可用物理内存的份额。

报告任务及其所有子任务的全局统计信息时,可能会显示以下值:

minflt-nr
该任务及其所有子任务的小故障(minor faults)总数,并在时间间隔内收集。

majflt-nr
任务及其所有子任务发生的重大故障(major faults)总数,并在时间间隔内收集。


Stack Utilization

选项

-R

指标

StkSize

为任务保留的作为堆栈的内存量,以千字节为单位,但不一定使用。

StkRef

作为堆栈使用的内存量,以千字节为单位,由任务引用。


Some Kernel Tables

选项

-v

指标

threads

与当前任务关联的线程数。

fd-nr

与当前任务关联的文件描述符数。


Task Switching Activity

选项

-w

指标

cswch/s

每秒任务执行的自愿上下文切换总数。

当任务因需要不可用的资源而阻塞时,会发生自愿上下文切换。

nvcswch/s

每秒执行的任务的非自愿上下文切换总数。

当任务在其时间片的持续时间内执行,然后被迫放弃处理器时,就会发生非自愿的上下文切换。


其他选项

-H
显示自纪元以来的时间戳,单位为秒。

-h

在单行上水平显示所有活动。这样做的目的是为了使它更容易被其他程序解析。

-I

在SMP环境中,表示任务的CPU使用率(如选项-u所显示的)应除以总的处理器数量。

-l

显示进程命令名称及其所有参数。

-p { pid [,...] | SELF | ALL }

选择要报告统计数据的任务(进程)。 pid是进程识别号。SELF关键字表示要报告pidstat进程本身的统计数据,而ALL关键字表示要报告系统管理的所有任务的统计数据。

-T { TASK | CHILD | ALL }

这个选项指定了pidstat命令所要监控的内容。TASK关键字表示要报告单个任务的统计数据(这是默认选项),而CHILD关键字表示要全局报告所选任务及其所有子任务的统计数据。ALL关键字表示要报告单个任务的统计数据,以及所选任务及其子任务的全局统计数据。


关键代码实现分析

获取CPU的数量

# cat /proc/stat
cpu  72163 45166 125104 12493464 3299 58616 7822 0 0 0
cpu0 15696 9304 26535 1524054 336 15268 3100 0 0 0
cpu1 16266 10600 27959 1523672 301 14475 1519 0 0 0
cpu2 14690 15250 25626 1523984 408 13848 1363 0 0 0
cpu3 15364 8359 28622 1525795 332 14201 1328 0 0 0
cpu4 5987 1093 8115 1590193 503 496 262 0 0 0
cpu5 1587 321 2903 1601235 589 140 112 0 0 0
cpu6 1424 139 2382 1601921 720 117 116 0 0 0
cpu7 1146 97 2957 1602606 106 68 19 0 0 0
intr 9130775 0 2951047 528520 0 0 0 967681 0 0 0 0 3349312 0 0 0 0 28 4 2 1 11 0 0 0 0 0 125872 0 375115 0 0 233763 
     16 17458 7529 31 1469 0 723 0 0 0 0 0 0 0 0 0 0 0 0 3070 0 2191 0 0 0 0 0 0 0 0 0 0 0 0 417 190557 66581 0 0 0
     0 0 0 0 0 0 0 0 0 16 1924 3 19882 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1556 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 5944 0 0 2 0 0 0 0 0 17218 0 0 0 0 0 0 112 123 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 3 0 0 32 0 0 2 0 6 0 39725 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 103 6 0 2 3 47 3 4 0 0 0 0 0 0 4 0 0 1
     1 0 0 1 1 0 0 97 0 1 1 0 0 0 5 1 1 0 0 33 0 15 0 1 2 0 2 0 0 0 12146 0 0 10 2851 1670 3 5 22769 8834 135 0 0 0 0
     0 286 10623 3112 3082 1988 0 0 207 0 0 0 0 0 0 0 0 0 0 0 0 0 935 1 37 0 2944 4756 142048 156 4 2 0 0 0 0 0 0 0 0
      0 0 0 0 20 0 0 3863
ctxt 12167990
btime 1663731264
processes 25119
procs_running 1
procs_blocked 0
softirq 2045855 1587 489182 4796 19123 373425 0 40118 558988 13 558623

或

# cat /sys/devices/system/cpu/
aarch32_el0         cpu2/               cpu7/               hyp_core_ctl/       online              qcom_lpm/
bus_dcvs/           cpu3/               cpufreq/            isolated            pmu_lib/            smt/
c1dcvs/             cpu4/               cpuidle/            kernel_max          possible            uevent
cpu0/               cpu5/               hang_detect_core/   modalias            power/              vulnerabilities/
cpu1/               cpu6/               hotplug/            offline             present

CPU 在不同状态下的统计

/*
 * Structure for CPU statistics.
 * In activity buffer: First structure is for global CPU utilization ("all").
 * Following structures are for each individual CPU (0, 1, etc.)
 *
 * Used by: sadc, sar, sadf, iostat, mpstat, pidstat
 */
struct stats_cpu {
	unsigned long long cpu_user;
	unsigned long long cpu_nice;
	unsigned long long cpu_sys;
	unsigned long long cpu_idle;
	unsigned long long cpu_iowait;
	unsigned long long cpu_steal;
	unsigned long long cpu_hardirq;
	unsigned long long cpu_softirq;
	unsigned long long cpu_guest;
	unsigned long long cpu_guest_nice;
};

读文件 /proc/stat 并解析每个 CPU 以及整体的统计数据。

/*
 ***************************************************************************
 * Read CPU statistics.
 * Remember that this function is used by several sysstat commands!
 *
 * IN:
 * @st_cpu	Buffer where structures containing stats will be saved.
 * @nr_alloc	Total number of structures allocated. Value is >= 1.
 *
 * OUT:
 * @st_cpu	Buffer with statistics.
 *
 * RETURNS:
 * Highest CPU number(*) for which statistics have been read.
 * 1 means CPU "all", 2 means CPU 0, 3 means CPU 1, etc.
 * Or -1 if the buffer was too small and needs to be reallocated.
 *
 * (*)This doesn't account for all processors in the machine in the case
 * where some CPU are offline and located at the end of the list.
 *
 * USED BY:
 * sadc, iostat, mpstat, pidstat
 ***************************************************************************
 */
__nr_t read_stat_cpu(struct stats_cpu *st_cpu, __nr_t nr_alloc)
{
    FILE *fp;
    struct stats_cpu *st_cpu_i;
    struct stats_cpu sc;
    char line[8192];
    int proc_nr;
    __nr_t cpu_read = 0;

    if ((fp = fopen(STAT, "r")) == NULL) {
        fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
        exit(2);
    }

    while (fgets(line, sizeof(line), fp) != NULL) {

    if (!strncmp(line, "cpu ", 4)) {
        /*
         * All the fields don't necessarily exist,
         * depending on the kernel version used.
         */
        memset(st_cpu, 0, STATS_CPU_SIZE);

        /*
         * Read the number of jiffies spent in the different modes
         * (user, nice, etc.) among all proc. CPU usage is not reduced
         * to one processor to avoid rounding problems.
         */
        sscanf(line + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
               &st_cpu->cpu_user,
               &st_cpu->cpu_nice,
               &st_cpu->cpu_sys,
               &st_cpu->cpu_idle,
               &st_cpu->cpu_iowait,
               &st_cpu->cpu_hardirq,
               &st_cpu->cpu_softirq,
               &st_cpu->cpu_steal,
               &st_cpu->cpu_guest,
               &st_cpu->cpu_guest_nice);

        if (!cpu_read) {
        cpu_read = 1;
        }

        if (nr_alloc == 1)
        /* We just want to read stats for CPU "all" */
        break;
    }

    else if (!strncmp(line, "cpu", 3)) {
        /* All the fields don't necessarily exist */
        memset(&sc, 0, STATS_CPU_SIZE);
        /*
         * Read the number of jiffies spent in the different modes
         * (user, nice, etc) for current proc.
         * This is done only on SMP machines.
         */
        sscanf(line + 3, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
               &proc_nr,
               &sc.cpu_user,
               &sc.cpu_nice,
               &sc.cpu_sys,
               &sc.cpu_idle,
               &sc.cpu_iowait,
               &sc.cpu_hardirq,
               &sc.cpu_softirq,
               &sc.cpu_steal,
               &sc.cpu_guest,
               &sc.cpu_guest_nice);

        if (proc_nr + 2 > nr_alloc) {
	        cpu_read = -1;
	        break;
        }

        st_cpu_i = st_cpu + proc_nr + 1;
        *st_cpu_i = sc;

        if (proc_nr + 2 > cpu_read) {
	        cpu_read = proc_nr + 2;
        }
    }
    }

    fclose(fp);
    return cpu_read;
}
# cat /proc/stat
cpu  77817 45815 134672 14012018 3438 65070 8496 0 0 0
cpu0 17105 9399 28830 1710320 370 16932 3366 0 0 0
cpu1 17636 10717 30203 1710156 328 16133 1666 0 0 0
cpu2 15941 15355 27621 1711141 441 15330 1489 0 0 0
cpu3 16674 8572 30980 1712183 366 15796 1442 0 0 0
cpu4 6140 1175 8541 1782828 509 535 279 0 0 0
cpu5 1702 346 3070 1794270 594 149 114 0 0 0
cpu6 1468 151 2454 1795156 721 121 117 0 0 0
cpu7 1147 97 2968 1795960 106 71 20 0 0 0
CPU Statistics

user

nicesysidleiowaithardirqsoftirqstealguestguest_nice
cpu7781745815134672140120183438650708496000
cpu0171059399288301710320370169323366000
cpu11763610717302031710156328161331666000
cpu21594115355276211711141441153301489000
cpu3166748572309801712183366157961442000
cpu46140117585411782828509535279000
cpu5170234630701794270594149114000
cpu6146815124541795156721121117000
cpu7114797296817959601067120000

计算某个 CPU 花费的 jiffies 总数

基于 CPU Statistics 的统计数据,某个 CPU 花费的 jiffies 总数的计算公式:

tot_jiffies[curr] = st_cpu->cpu_user   + st_cpu->cpu_nice    +
                    st_cpu->cpu_sys    + st_cpu->cpu_idle    +
                    st_cpu->cpu_iowait + st_cpu->cpu_hardirq +
                    st_cpu->cpu_steal  + st_cpu->cpu_softirq;

注意:不要添加 cpu_guest 和 cpu_guest_nice, 因为 cpu_user 和 cpu_nice 已经包含它们。


进程的统计信息

#define PID_STAT	PRE "/proc/%u/stat"
#define TASK_STAT	PRE "/proc/%u/task/%u/stat"

/*
 ***************************************************************************
 * Read various stats for given PID.
 *
 * IN:
 * @pid		Process whose stats are to be read.
 * @plist	Pointer on the linked list where PID is saved.
 * @tgid	If !=0, thread whose stats are to be read.
 * @curr	Index in array for current sample statistics.
 *
 * OUT:
 * @thread_nr	Number of threads of the process.
 *
 * RETURNS:
 * 0 if stats have been successfully read, and 1 otherwise.
 ***************************************************************************
 */
int read_pid_stats(pid_t pid, struct st_pid *plist, unsigned int *thread_nr,
		   pid_t tgid, int curr)
{
	if (read_proc_pid_stat(pid, plist, thread_nr, tgid, curr))
		return 1;

	/*
	 * No need to test the return code here: Not finding
	 * the schedstat files shouldn't make pidstat stop.
	 */
	read_proc_pid_sched(pid, plist, tgid, curr);

	if (DISPLAY_CMDLINE(pidflag) && !plist->cmdline[0]) {
		if (read_proc_pid_cmdline(pid, plist, tgid))
			return 1;
	}

	if (read_proc_pid_status(pid, plist, tgid, curr))
		return 1;

	if (DISPLAY_STACK(actflag)) {
		if (read_proc_pid_smap(pid, plist, tgid, curr))
			return 1;
	}

	if (DISPLAY_KTAB(actflag)) {
		if (read_proc_pid_fd(pid, plist, tgid, curr))
			return 1;
	}

	if (DISPLAY_IO(actflag))
		/* Assume that /proc/#/task/#/io exists! */
		return (read_proc_pid_io(pid, plist, tgid, curr));

	return 0;
}
# cat /proc/1/stat
1 (init) S 0 0 0 0 -1 1077936384 30321 850720 959 456 254 483 880 1269 
20 0 2 0 0 11145768960 2036 18446744073709551615 367395913728 367397338632 
549050140256 0 0 0 65536 0 1073779960 0 0 0 17 2 0 0 29 0 0 367397362976 
367397363736 368425443328 549050142404 549050142434 549050142434 549050142695 0

# cat /proc/1/task/1/stat
1 (init) S 0 0 0 0 -1 1077936384 24255 850720 798 456 182 296 880 1269
20 0 2 0 0 11145768960 2036 18446744073709551615 367395913728 367397338632
549050140256 0 0 0 65536 0 1073779960 1 0 0 17 2 0 0 29 0 0 367397362976 
367397363736 368425443328 549050142404 549050142434 549050142434 549050142695 0

# cat /proc/1/task/233/stat
233 (init) S 0 0 0 0 -1 1077936448 5752 850720 161 456 69 188 880 1269
0 -20 2 0 215 11145768960 2036 18446744073709551615 367395913728 367397338632
549050140256 0 0 0 65536 0 1073779960 1 0 0 -1 1 0 0 11 0 0 367397362976
367397363736 368425443328 549050142404 549050142434 549050142434 549050142695 0

内核相关代码:

///kernel_platform/msm-kernel/fs/proc/array.c
static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
			struct pid *pid, struct task_struct *task, int whole);

统计进程相关状态

struct pid_stats {
	unsigned long long read_bytes			__attribute__ ((aligned (8)));
	unsigned long long write_bytes			__attribute__ ((packed));
	unsigned long long cancelled_write_bytes	__attribute__ ((packed));
	unsigned long long blkio_swapin_delays		__attribute__ ((packed));
	unsigned long long minflt			__attribute__ ((packed));
	unsigned long long cminflt			__attribute__ ((packed));
	unsigned long long majflt			__attribute__ ((packed));
	unsigned long long cmajflt			__attribute__ ((packed));
	unsigned long long utime			__attribute__ ((packed));
	long long          cutime			__attribute__ ((packed));
	unsigned long long stime			__attribute__ ((packed));
	long long          cstime			__attribute__ ((packed));
	unsigned long long gtime			__attribute__ ((packed));
	long long          cgtime			__attribute__ ((packed));
	unsigned long long wtime			__attribute__ ((packed));
	unsigned long long vsz				__attribute__ ((packed));
	unsigned long long rss				__attribute__ ((packed));
	unsigned long      nvcsw			__attribute__ ((packed));
	unsigned long      nivcsw			__attribute__ ((packed));
	unsigned long      stack_size			__attribute__ ((packed));
	unsigned long      stack_ref			__attribute__ ((packed));
	unsigned int       processor			__attribute__ ((packed));
	unsigned int       priority			__attribute__ ((packed));
	unsigned int       policy			__attribute__ ((packed));
	unsigned int       threads			__attribute__ ((packed));
	unsigned int       fd_nr			__attribute__ ((packed));
};


#define PID_STAT	PRE "/proc/%u/stat"
#define TASK_STAT	PRE "/proc/%u/task/%u/stat"

int read_proc_pid_stat(pid_t pid, struct st_pid *plist, unsigned int *thread_nr, pid_t tgid, int curr)
{
    rc = sscanf(start,
            "%*s %*d %*d %*d %*d %*d %*u %llu %llu"
            " %llu %llu %llu %llu %lld %lld %*d %*d %u %*u %*d %llu %llu"
            " %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u"
            " %*u %u %u %u %llu %llu %lld\n",
            &pst->minflt, &pst->cminflt, &pst->majflt, &pst->cmajflt,
            &pst->utime,  &pst->stime, &pst->cutime, &pst->cstime,
            thread_nr, &pst->vsz, &pst->rss, &pst->processor,
            &pst->priority, &pst->policy,
            &pst->blkio_swapin_delays, &pst->gtime, &pst->cgtime);

    if (rc < 15)
        return 1;

    if (rc < 17) {
        /* gtime and cgtime fields are unavailable in file */
        pst->gtime = pst->cgtime = 0;
    }

    /* Convert to kB */
    pst->vsz >>= 10;
    pst->rss = PG_TO_KB(pst->rss);
}

/* Total number of jiffies spent on the interval */
/* /proc/stat > tot_jiffies */
deltot_jiffies = get_interval(tot_jiffies[prev], tot_jiffies[curr]);

/* /proc/uptime > uptime_cs */
itv = get_interval(uptime_cs[prev], uptime_cs[curr]);

CPU Utilization

指标计算方式
%usr(pstc->utime- pstp->utime) / (itv * HZ)
%system(pstc->stime - pstp->stime) / (itv * HZ)
%guest (pstc->gtime - pstp->gtime) / (itv * HZ)
%CPU (Solaris mode)((pstc->utime + pstc->stime)-(pstp->utime + pstp->stime)) / deltot_jiffies
%CPU (Irix  mode)((pstc->utime + pstc->stime)-(pstp->utime + pstp->stime)) / (itv * HZ)
CPUpstc->processor

注意:

  • User time already includes guest time
  • Irix mode: 本进程在本进程运行的cpu中(可能是多cpu的系统)占用的百分比
  • Solaris mode: 在irix的基础上除以cpu个数

HZ:

/*
 ***************************************************************************
 * Get number of clock ticks per second.
 ***************************************************************************
 */
void get_HZ(void)
{
	long ticks;

	if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
		perror("sysconf");
	}

	hz = (unsigned long) ticks;
}

进程调度延时统计

int read_proc_pid_sched(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)

#define PID_SCHED	PRE "/proc/%u/schedstat"
#define TASK_SCHED	PRE "/proc/%u/task/%u/schedstat"

// # cat /proc/1/schedstat
// 4857180564 2486120670 9302
// # cat /proc/1/task/1/schedstat
// 4857180564 2486120670 9302
// # cat /proc/1/task/233/schedstat
// 2655717387 273417638 5066
schedstat :
<task->se.sum_exec_runtime> <task->sched_info.run_delay> <task->sched_info.pcount>
struct pid_stats {
    // task->sched_info.run_delay
    unsigned long long wtime			__attribute__ ((packed));
};
/* Convert ns to jiffies */
pst->wtime = <task->sched_info.run_delay> * HZ / 1000000000
指标计算方式
%wait(pstc->wtime- pstp->wtime) / (itv * HZ)

Page Faults And Memory Utilization

指标计算方式
minflt/s (pstc->minflt - pstc->minflt) / itv
majflt/s (pstc->majflt - pstc->majflt) / itv
VSZpstc->vsz
RSSpstc->rss
%MEMpstc->rss / tlmkb    (tlmkb: /proc/meminfo MemTotal)

Priority And Scheduling Policy

指标计算方式
priopst->priority
policypst->policy

pst->policy:

/* Normally defined in <linux/sched.h> */
#define SCHED_NORMAL	0
#define SCHED_FIFO	1
#define SCHED_RR	2
#define SCHED_BATCH	3
/* SCHED_ISO not yet implemented */
#define SCHED_IDLE	5
#define SCHED_DEADLINE	6

Task Switching Activity

统计进程状态 status

# cat /proc/1/status
Name:   init
Umask:  0000
State:  S (sleeping)
Tgid:   1
Ngid:   0
Pid:    1
PPid:   0
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups: 3009
VmPeak: 10901976 kB
VmSize: 10884540 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     15436 kB
VmRSS:      8144 kB
RssAnon:            2356 kB
RssFile:            5348 kB
RssShmem:            440 kB
VmData:    22528 kB
VmStk:       132 kB
VmExe:      1392 kB
VmLib:      5236 kB
VmPTE:       332 kB
VmSwap:     4348 kB
CoreDumping:    0
THP_enabled:    1
Threads:        2
SigQ:   4/27074
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000080010000
SigIgn: 0000002000000000
SigCgt: 0000004c400094f8
CapInh: 0000000000000000
CapPrm: 000001ffffffffff
CapEff: 000001ffffffffff
CapBnd: 000001ffffffffff
CapAmb: 0000000000000000
NoNewPrivs:     0
Seccomp:        0
Seccomp_filters:        0
Speculation_Store_Bypass:       thread vulnerable
Cpus_allowed:   ff
Cpus_allowed_list:      0-7
Mems_allowed:   1
Mems_allowed_list:      0
voluntary_ctxt_switches:        5998
nonvoluntary_ctxt_switches:     3319
struct pid_stats {
    // voluntary_ctxt_switches
    unsigned long      nvcsw			__attribute__ ((packed));
    // nonvoluntary_ctxt_switches
    unsigned long      nivcsw			__attribute__ ((packed));
    // Threads
    unsigned int       threads			__attribute__ ((packed));
};

struct st_pid {
	unsigned long long total_threads;
};

#define PID_STATUS	PRE "/proc/%u/status"
#define TASK_STATUS	PRE "/proc/%u/task/%u/status"
int read_proc_pid_status(pid_t pid, struct st_pid *plist, pid_t tgid, int curr) {
    struct pid_stats *pst = plist->pstats[curr];    
    // ...
    if (!strncmp(line, "Uid:", 4)) {
        sscanf(line + 5, "%u", &plist->uid);
    }
    else if (!strncmp(line, "Threads:", 8)) {
        sscanf(line + 9, "%u", &pst->threads);
    }
    else if (!strncmp(line, "voluntary_ctxt_switches:", 24)) {
        sscanf(line + 25, "%lu", &pst->nvcsw);
    }
    else if (!strncmp(line, "nonvoluntary_ctxt_switches:", 27)) {
        sscanf(line + 28, "%lu", &pst->nivcsw);
    }
}
指标计算方式
cswch/s(pstc->nvcsw -  pstc->nvcsw) / itv
nvcswch/s(pstc->nivcsw -  pstc->nivcsw) / itv
threadspstc->threads

Stack Utilization

统计进程堆栈内存信息

# cat /proc/1/smaps |head -20
558a757000-558a80c000 r--p 00000000 fe:03 2948                           /system/bin/init
Size:                724 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Rss:                 144 kB
Pss:                  51 kB
Shared_Clean:        140 kB
Shared_Dirty:          0 kB
Private_Clean:         4 kB
Private_Dirty:         0 kB
Referenced:          144 kB
......
7fd5ee4000-7fd5f05000 rw-p 00000000 00:00 0                              [stack]
Size:                132 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Rss:                  28 kB
Pss:                  28 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:        28 kB
Referenced:           28 kB
Anonymous:            28 kB
LazyFree:              0 kB
AnonHugePages:         0 kB
ShmemPmdMapped:        0 kB
FilePmdMapped:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                 92 kB
SwapPss:              92 kB
Locked:                0 kB
THPeligible:    0
VmFlags: rd wr mr mw me gd ac
struct pid_stats {
	unsigned long      stack_size			__attribute__ ((packed));
	unsigned long      stack_ref			__attribute__ ((packed));
};

int read_proc_pid_smap(pid_t pid, struct st_pid *plist, pid_t tgid, int curr) {
    struct pid_stats *pst = plist->pstats[curr];
    // ......
    case 1:
        if (strstr(line, "Size:")) {
            sscanf(line + sizeof("Size:"), "%lu", &pst->stack_size);
            state = 2;
        }
        break;
    case 2:
        if (strstr(line, "Referenced:")) {
            sscanf(line + sizeof("Referenced:"), "%lu", &pst->stack_ref);
            state = 3;
        }
}
指标计算方式
StkSizepst->stack_size
StkRefpst->stack_ref

Some Kernel Tables

统计进程打开文件信息

struct pid_stats {
	unsigned int       fd_nr			__attribute__ ((packed));
};

#define PID_FD		PRE "/proc/%u/fd"
#define TASK_FD		PRE "/proc/%u/task/%u/fd"
int read_proc_pid_fd(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
    struct pid_stats *pst = plist->pstats[curr];
    if (tgid) {
        sprintf(filename, TASK_FD, tgid, pid);
    }
    else {
        sprintf(filename, PID_FD, pid);
    }
    dir = opendir(filename)
    /* Count number of entries if fd directory */
    while ((drp = readdir(dir)) != NULL) {
        if (isdigit(drp->d_name[0])) {
            (pst->fd_nr)++;
        }
    }
}
指标计算方式
fd-nrpstc->fd_nr


I/O Statistics

统计进程 IO 信息

# cat /proc/1/io
rchar: 619196013
wchar: 79644789
syscr: 219124
syscw: 62707
read_bytes: 277807104
write_bytes: 21979136
cancelled_write_bytes: 4096

# cat /proc/1/task/1/io
rchar: 15870554
wchar: 55886697
syscr: 8570
syscw: 7476
read_bytes: 22913024
write_bytes: 217088
cancelled_write_bytes: 0

# cat /proc/1/task/233/io
rchar: 3623029
wchar: 3640600
syscr: 4205
syscw: 3366
read_bytes: 1089536
write_bytes: 8212480
cancelled_write_bytes: 0
struct pid_stats {
    unsigned long long read_bytes			__attribute__ ((aligned (8)));
    unsigned long long write_bytes			__attribute__ ((packed));
    unsigned long long cancelled_write_bytes	__attribute__ ((packed));
};


#define PID_IO      PRE "/proc/%u/io"
#define TASK_IO     PRE "/proc/%u/task/%u/io"
int read_proc_pid_io(pid_t pid, struct st_pid *plist, pid_t tgid, int curr) {

    if (!strncmp(line, "read_bytes:", 11)) {
        sscanf(line + 12, "%llu", &pst->read_bytes);
    }
    else if (!strncmp(line, "write_bytes:", 12)) {
        sscanf(line + 13, "%llu", &pst->write_bytes);
    }
    else if (!strncmp(line, "cancelled_write_bytes:", 22)) {
        sscanf(line + 23, "%llu", &pst->cancelled_write_bytes);
    }

}
指标计算方式
kB_rd/s(pstc->read_bytes - pstp->read_bytes) / itv
kB_wr/s(pstc->write_bytes - pstp->write_bytes) / itv
kB_ccwr/s(pstc->cancelled_write_bytes - pstp->cancelled_write_bytes) / itv
iodelaypstc->blkio_swapin_delays - pstp->blkio_swapin_delays

从模组到集装箱:手把手拆解一个储能BMS的软硬件架构设计与通信组网 本文深入解析储能BMS的软硬件架构设计与通信组网,从电芯采样到系统集成的全链条技术实现。详细介绍了三级硬件架构(AFE、CMU、BMU)的设计要点,以及软件分层实现中的驱动层、应用层核心算法和通信协议栈。通过实战案例分享CAN网络搭建、调试工具链和系统验证测试经验,为储能系统开发提供实用指导。 阅读详情

相关推荐

对族实例进行任意的矩阵变换,需要什么样的族?

问:一个小问题,想向您请教一下。 能不能实现一个这样的操作:创建了一个常规模型的FamilyInstance,然后知道旋转和平移的矩阵,能否根据变换矩阵更新FamilyInstance的位置,绕任意轴旋转任意角度。 请问什么样的实体才能在revit项目文件中任意旋转?貌似一般的实例只能在XY平面内旋转,并且LocationPoint中只有一个Rotation参数。答:您好,因为Revit里面的构件

我爱Revit —— BIM应用及BIM应用软件解决方案专栏 3626

Devfreq Bus Dcvs

一、引言计算机的世界里,CPU任务分为CPU bound和IO bound。而实际场景下往往是两者混合型任务。针对性能的优化,普遍关注点都在CPU上(不论是CPU的频点和CPU的选核), ...

内核工匠 6358

射频测试基础知识总结

表示可以辐射到空间的电磁频率,频率范围从300KHz~110GHz之间,射频简称RF,是高频交流变化电磁波的简称,其每秒变化小于1000次被称为低频电流,超过10000次的称为高频电流,而射频就是这种高频电流。为了确保天线接收的信号能够在接收机的最后一级被正确的恢复,一个好的低噪音放大器需要在放大信号的同时产生尽可能低的噪音以及失真。两种,带内试验的范围也有一定的差别,比如,带内试验主要是检测信号的质量,如功率、频谱宽度、调制质量,而带外测试的是信号的杂散、谐波,主要是看对频带之外的干扰有多大。

cny70的博客 9628

CPU分析系列--sysstat(mpstat+pidstat)分析系统CPU和I/O负载

sysstat是一个软件包,包含监测系统性能及效率的一组工具,这些工具对于我们收集系统性能数据,比如:CPU 使用率、硬盘和网络吞吐数据,这些数据的收集和分析,有利于我们判断系统是否正常运行. stress是Linux下的一个压力测试工具,可以对cpu、memory、IO以及磁盘进行压力测试,可以指定负载的cpu个数. -c, --cpu N 产生 N 个进程,每个进程都反复不停的计算随机数的平方根 -i, --io N 产生 N 个进程,每个进程反复调用 sync() 将内存上的内容写到硬盘上 -m

qq_40132294的博客 3394

Visual Tracker Benchamark傻瓜式教程(二)

问题  1 1  在调用main.m跑TRE和SRE时,因为会生成相应的.mat数据文件,如果跑完以后发现自己算法生成的.mat文件有问题,那么要把该文件删除,待找到错误原因后,重新生成相应的.mat文件。因为程序中写的是如果已经存在了.mat文件,例如已经存在car4_STC.mat文件,即使该文件中的数据有错误,但是重新跑时会默认该文件中的数据写完,不会重新写入数据 问题  2  b

mingbujingren的专栏 9928

一、5.8GHz射频接收机链路仿真

5.8GHz射频接收机链路仿真 一、5.8GHz射频接收机链路仿真 文章目录5.8GHz射频接收机链路仿真目的一、原理1、仿真工具2、二次下变频超外超接收机二、要求三、内容总结 目的   1、熟悉射频接收机整体结构。   2、学习使用MATLAB工具进行行为级仿真。   3、进行简单的通信链路预算。   4、直观感受接收机各个模块对整体性能的影响。   5、分析增益、线性度、噪声系数等参数。 一、原理 1、仿真工具   MATLAB是一款功能强大的数学软件,针对不同领域提供各种工具箱,帮助设计者快

平乾的博客 6760

高通平台GPU动态调频DCVS . 篇2 . Framework & Procedure

1. 高通平台 GPU DCVS架构 GPU DCVS是基于Linux Devfreq来实现 高通的kgsl(kernel-graphics-support-layer)作为devfreq device msm-adreno-tz 作为devfreq governor 简单的架构如下 如上图所示 首先 KGSL 作为device,msm-adreno-tz 作为governor注册到dev...

Schedule Plus 8748

用Python和CasADi实现高斯伪谱法:一个轨迹优化问题的保姆级教程

本文详细介绍了如何使用Python和CasADi框架实现高斯伪谱法解决轨迹优化问题。通过保姆级教程,从环境准备、问题建模到高斯伪谱法的核心实现,包括配点与权重计算、微分矩阵构造和动力学约束实现,最终完成问题求解与结果可视化。适用于机器人轨迹规划和无人机路径优化等工程场景。

weixin_30776273的博客 293

strncmp的介绍

strncmp函数为字符串比较函数,字符串大小的比较是以ASCII 码表上的顺序来决定,此顺序亦为字符的值。其函数声明为int strncmp ( const char * str1, const char * str2, size_t n );功能是把 str1 和 str2 进行比较,最多比较前n个字节,若str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 小于s2,则返回小于0的值 ...

m0_61949623的博客 559

2026软件测试面试题全解析:从用例设计到接口自动化与性能实战

在软件测试岗位的招聘考察中,面试官越来越看重候选人解决实际问题的工程能力,而不仅仅是背诵概念。从最基础的测试用例设计出发,覆盖功能测试、接口测试、自动化测试、性能测试、数据库与Redis一致性等核心知识体系,是应对中高级岗位面试的关键路径。理解Postman抓包、Python脚本、Pytest框架、JMeter压测指标背后的原理,能帮助测试人员构建完整的质量保障思维。当AI辅助测试成为行业新趋势时,掌握数据校验链路与风险识别意识,更能体现资深测试工程师的竞争力。本文围绕2026年高频软件测试面试题,梳理了从

weixin_28676289的博客 244

用 Claude Code 快速摸清一个陌生代码库

摘要:Claude Code作为AI编码助手,能帮助开发者快速理解陌生代码库。与传统代码搜索不同,它从项目根目录出发,通过分析目录结构、配置文件等技术栈信息,提供系统级的概览。最佳实践是先通过"give me an overview"获取整体架构,再深入数据模型、认证流程等核心模块。它能生成术语表、编码规范和推荐的代码阅读路径,极大降低新成员的学习成本。相比传统方式,Claude Code能主动探索项目上下文,无需人工整理相关文件,使代码理解过程更接近有经验的工程师带教。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 89

sysstat系列:pidstat

sysstat系列:pidstat

zhj_1121的博客 1256

昇腾AI原生创新算子挑战赛(S1赛季)复盘-LessEqual算子

【代码】昇腾AI原生创新算子挑战赛(S1赛季)复盘-LessEqual算子。

william_myq的博客 507

【实践】安装sysstat,使用pidstat命令

安装sysstat权限不够在命令前键入sudo –u admin 赋予文件执行权限chmod +x [firename] 1.下载sysstat-10.0.5.tar.gz到本地 2.通过CRT使用rz命令上传 3.解压zip: unzip sysstat-10.0.5.tar.zip 解压gz: tar -zxvf sysstat-10.0.5.tar.gz 4.cd systa

阿利在努力 5106

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

sysstat 概览与使用:sar/iostat/mpstat/pidstat(含基础原理)

sysstat工具集概览 sysstat是一组用于监控系统性能的工具,包括sar、iostat、mpstat和pidstat等组件,可采集CPU、内存、I/O、网络等多维度指标。数据主要来源于/proc和/sys等内核接口,通过轻量级轮询实现低开销采集。 关键功能: sar:时间序列系统统计(CPU、内存、网络等) iostat:块设备I/O分析(吞吐、队列等) mpstat:每CPU核心使用率统计 pidstat:进程级资源监控 支持数据导出为CSV/JSON/XML格式,并能生成SVG图表。建议采样周期

love131452098的博客 758

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

sysstat监控工具使用(包含pidstat,iostat,sar)

从http://pagesperso-orange.fr/sebastien.godard/download.html下载sysstat tar zxvf sysstat.tar.gz ./configure make make install   pidstat 2 5 //每隔2秒,显示5次,所有活动进程的CPU使用情况 pidstat -p 3132 2 5 //每

a936676463的专栏 1973

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

linux包之sysstat之mpstat与pidstat命令

概述 [root@localhost ~]# rpm -qa|grep sysstsysstat-9.0.4-22.el6.x86_64mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具。其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中。在多CPUs系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU的信息。下面只介绍...

weixin_34380296的博客 206

全能冠军:Linux系统性能和使用活动监控工具 sysstat、iostat、mpstat、pidstat、sar、 nfsiostat、cifsiostat

下表是包含在sysstat包中的工具 iostat: 输出CPU的统计信息和所有I/O设备的输入输出(I/O)统计信息。 mpstat: 关于CPU的详细信息(单独输出或者分组输出)。 pidstat: 关于运行中的进程/任务、CPU、内存等的统计信息。 sar: 保存并输出不同系统资源(CPU、内存、IO、网络、内核等。。。)的详细信息。 sadc: 系统活动数据收集器,用于收集sar...

qq_24790545的博客 1423

sysstat介绍以及交叉编译

Sysstat 是一个功能强大的 Linux 系统性能监控工具包,提供实时监控和历史数据分析功能,帮助管理员优化系统资源使用和排查性能问题。cifsiostat:监控CIFS/SMB 网络文件系统的 I/O 性能的专用工具,监控其 I/O 活动,分析读写性能、延迟等指标,定位网络或服务器端的性能瓶颈。iostat:监控CPU 使用率和磁盘 I/O 性能,提供设备读写速率、吞吐量、请求队列长度等数据。mpstat:统计多核 CPU 的详细性能,支持按核心分组显示用户态、系统态、空闲时间等指标。

qq_40642828的博客 940

sysstat-master安装

包括pidstat sysstat iostat tapestat等工具。

peterhunter0320的博客 174

安装pidstat方法

3、下载成功后,安装sysstat。2、下载sysstat。

yueguangni的博客 210

将sysstat工具包移植到嵌入式linux设备(cifsiostat iostat mpstat pidstat sadf sar tapestat命令移植)

Linux 下的性能分析,包括CPU使用情况,内存使用情况,IO状态等等与系统相关的状态,少不了要使用 sysstat 工具包中的命令,它包括:cifsiostat iostat mpstat pidstat sadf sar tapestat 七条命令。 sysstat 服务启动后,它会自动每个10分钟收集一次系统状态信息,并将它们存储到 /var/log/sa目录下。每天一...............

Biao 5186

pidstat 命令详解

pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu、内存、线程、设备IO等系统资源的占用情况。pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上次运行该命令以后的统计信息。用户可以通过指定统计的次数和时间来获得所需的统计信息。 pidstat 安装 pidstat 是sysstat软件套件的一部分,sys...

通往架构师的成长学习之路 1万+

在Linux 中使用 pidstat 命令监控进程性能

一、安装 pidstat 命令 检查系统是否已经安装了 pidstat 打开终端,输入以下命令检查是否已经安装了 pidstat: pidstat -V 如果显示版本信息,说明已经安装,可以跳过安装步骤。如果提示找不到命令,那么继续下一步安装。 更新包管理器 在安装 pidstat 前,建议先更新系统的包管理器来获取最新的软件包列表。可以使用以下命令: 对于基于 Debian 或 Ubu...

weixin_32535389的博客 313

sysstat离线安装及使用教程-系统性能监控

安装 离线包下载地址一:http://sebastien.godard.pagesperso-orange.fr/download.html 离线包下载地址二:https://github.com/sysstat/sysstat 安装步骤: unzip sysstat-master.zip cd sysstat-master/ ./configure make make install iosta...

alex_i的博客 4197

sysstat工具的制作和使用

一、sysstat的安装 1、下载路径 http://sebastien.godard.pagesperso-orange.fr/download.html 2、安装 2.1、解压 tar -xvfsysstat-12.4.2.tar.xz 2.2、cd 进入sysstat-12.4.2目录 CC=arm-buildroot-linux-uclibcgnueabihf-gcc ./configure --host=arm --cache-file=/new_home/duyh/dyh/too.

sydyh43的博客 512
上一篇: Linux 磁盘 I/O 性能分析方法概览
下一篇: 【io_uring】使用 io_uring 的高效 IO
zs.w
博客等级 码龄4年 133粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zs.w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值