sadf.c
来自「linux下查看系统工具原码,如IOSTAT等」· C语言 代码 · 共 2,002 行 · 第 1/4 页
C
2,002 行
/* Print TTY statistics (serial lines) */ if (GET_SERIAL(act)) { int i; struct stats_serial *ssi = st_serial[curr], *ssj = st_serial[!curr]; for (i = 0; i++ < file_hdr.sa_serial; ssi++, ssj++) { if (ssi->line == ~0) continue; if (ssi->line == ssj->line) { render(isdb, pre, PT_NOFLAG, "ttyS%d\trcvin/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->rx, ssi->rx, itv)); render(isdb, pre, PT_NOFLAG, "ttyS%d\txmtin/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->tx, ssi->tx, itv)); render(isdb, pre, PT_NOFLAG, "ttyS%d\tframerr/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->frame, ssi->frame, itv)); render(isdb, pre, PT_NOFLAG, "ttyS%d\tprtyerr/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->parity, ssi->parity, itv)); render(isdb, pre, PT_NOFLAG, "ttyS%d\tbrk/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->brk, ssi->brk, itv)); render(isdb, pre, PT_NEWLIN, "ttyS%d\tovrun/s", "%d", cons(iv, ssi->line, NOVAL), NOVAL, S_VALUE(ssj->overrun, ssi->overrun, itv)); } } } /* Print amount and usage of memory */ if (GET_MEM_AMT(act)) { render(isdb, pre, PT_USEINT, "-\tkbmemfree", NULL, NULL, fsi->frmkb, DNOVAL); render(isdb, pre, PT_USEINT, "-\tkbmemused", NULL, NULL, fsi->tlmkb - fsi->frmkb, DNOVAL); render(isdb, pre, PT_NOFLAG, "-\t%%memused", NULL, NULL, NOVAL, fsi->tlmkb ? SP_VALUE(fsi->frmkb, fsi->tlmkb, fsi->tlmkb) : 0.0); render(isdb, pre, PT_USEINT, "-\tkbbuffers", NULL, NULL, fsi->bufkb, DNOVAL); render(isdb, pre, PT_USEINT, "-\tkbcached", NULL, NULL, fsi->camkb, DNOVAL); render(isdb, pre, PT_USEINT, "-\tkbswpfree", NULL, NULL, fsi->frskb, DNOVAL); render(isdb, pre, PT_USEINT, "-\tkbswpused", NULL, NULL, fsi->tlskb - fsi->frskb, DNOVAL); render(isdb, pre, PT_NOFLAG, "-\t%%swpused", NULL, NULL, NOVAL, fsi->tlskb ? SP_VALUE(fsi->frskb, fsi->tlskb, fsi->tlskb) : 0.0); render(isdb, pre, PT_USEINT | PT_NEWLIN, "-\tkbswpcad", NULL, NULL, fsi->caskb, DNOVAL); } if (GET_IRQ(act) && WANT_PER_PROC(flags) && file_hdr.sa_irqcpu) { int j, k, offset; struct stats_irq_cpu *p, *q, *p0, *q0; for (k = 0; k <= file_hdr.sa_proc; k++) { if (!(cpu_bitmap[k >> 3] & (1 << (k & 0x07)))) continue; for (j = 0; j < file_hdr.sa_irqcpu; p0++, j++) { p0 = st_irq_cpu[curr] + j; /* irq field set only for proc #0 */ /* * A value of ~0 means it is a remaining interrupt * which is no longer used, for example because the * number of interrupts has decreased in /proc/interrupts * or because we are appending data to an old sa file * with more interrupts than are actually available now. */ if (p0->irq == ~0) continue; q0 = st_irq_cpu[!curr] + j; offset = j; if (p0->irq != q0->irq) { if (j) offset = j - 1; q0 = st_irq_cpu[!curr] + offset; if ((p0->irq != q0->irq) && (j + 1 < file_hdr.sa_irqcpu)) offset = j + 1; q0 = st_irq_cpu[!curr] + offset; } if (p0->irq != q0->irq) continue; p = st_irq_cpu[curr] + k * file_hdr.sa_irqcpu + j; q = st_irq_cpu[!curr] + k * file_hdr.sa_irqcpu + offset; render(isdb, pre, PT_NEWLIN, "cpu%d\ti%03d/s", "%d;%d", cons(iv, k, p0->irq), NOVAL, S_VALUE(q->interrupt, p->interrupt, itv)); } } } /* Print values of some kernel tables */ if (GET_KTABLES(act)) { render(isdb, pre, PT_USEINT, "-\tdentunusd", NULL, NULL, fsi->dentry_stat, DNOVAL); render(isdb, pre, PT_USEINT, "-\tfile-sz", NULL, NULL, fsi->file_used, DNOVAL); render(isdb, pre, PT_USEINT, "-\tinode-sz", NULL, NULL, fsi->inode_used, DNOVAL); render(isdb, pre, PT_USEINT, "-\tsuper-sz", NULL, NULL, fsi->super_used, DNOVAL); render(isdb, pre, PT_NOFLAG, "-\t%%super-sz", NULL, NULL, NOVAL, fsi->super_max ? ((double) (fsi->super_used * 100)) / fsi->super_max : 0.0); render(isdb, pre, PT_USEINT, "-\tdquot-sz", NULL, NULL, fsi->dquot_used, DNOVAL); render(isdb, pre, PT_NOFLAG, "-\t%%dquot-sz", NULL, NULL, NOVAL, fsi->dquot_max ? ((double) (fsi->dquot_used * 100)) / fsi->dquot_max : 0.0); render(isdb, pre, PT_USEINT, "-\trtsig-sz", NULL, NULL, fsi->rtsig_queued, DNOVAL); render(isdb, pre, PT_NEWLIN, "-\t%%rtsig-sz", NULL, NULL, NOVAL, fsi->rtsig_max ? ((double) (fsi->rtsig_queued * 100)) / fsi->rtsig_max : 0.0); } /* Print network interface statistics */ if (GET_NET_DEV(act)) { int i, j; struct stats_net_dev *sndi = st_net_dev[curr], *sndj; char *ifc; for (i = 0; i < file_hdr.sa_iface; i++, ++sndi) { if (!strcmp((ifc = sndi->interface), "?")) continue; j = check_iface_reg(&file_hdr, st_net_dev, curr, !curr, i); sndj = st_net_dev[!curr] + j; render(isdb, pre, PT_NOFLAG, "%s\trxpck/s", "%s", cons(sv, ifc, NULL), /* What if the format args are strings? */ NOVAL, S_VALUE(sndj->rx_packets, sndi->rx_packets, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxpck/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_packets, sndi->tx_packets, itv)); render(isdb, pre, PT_NOFLAG, "%s\trxbyt/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_bytes, sndi->rx_bytes, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxbyt/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_bytes, sndi->tx_bytes, itv)); render(isdb, pre, PT_NOFLAG, "%s\trxcmp/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_compressed, sndi->rx_compressed, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxcmp/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_compressed, sndi->tx_compressed, itv)); render(isdb, pre, PT_NEWLIN, "%s\trxmcst/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->multicast, sndi->multicast, itv)); } } /* Print network interface statistics (errors) */ if (GET_NET_EDEV(act)) { int i, j; struct stats_net_dev *sndi = st_net_dev[curr], *sndj; char *ifc; for (i = 0; i < file_hdr.sa_iface; i++, ++sndi) { if (!strcmp((ifc = sndi->interface), "?")) continue; j = check_iface_reg(&file_hdr, st_net_dev, curr, !curr, i); sndj = st_net_dev[!curr] + j; render(isdb, pre, PT_NOFLAG, "%s\trxerr/s", "%s", cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_errors, sndi->rx_errors, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxerr/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_errors, sndi->tx_errors, itv)); render(isdb, pre, PT_NOFLAG, "%s\tcoll/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->collisions, sndi->collisions, itv)); render(isdb, pre, PT_NOFLAG, "%s\trxdrop/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_dropped, sndi->rx_dropped, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxdrop/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_dropped, sndi->tx_dropped, itv)); render(isdb, pre, PT_NOFLAG, "%s\ttxcarr/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_carrier_errors, sndi->tx_carrier_errors, itv)); render(isdb, pre, PT_NOFLAG, "%s\trxfram/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_frame_errors, sndi->rx_frame_errors, itv)); render(isdb, pre, PT_NOFLAG, "%s\trxfifo/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->rx_fifo_errors, sndi->rx_fifo_errors, itv)); render(isdb, pre, PT_NEWLIN, "%s\ttxfifo/s", NULL, cons(sv, ifc, NULL), NOVAL, S_VALUE(sndj->tx_fifo_errors, sndi->tx_fifo_errors, itv)); } } /* Print number of sockets in use */ if (GET_NET_SOCK(act)) { render(isdb, pre, PT_USEINT, "-\ttotsck", NULL, NULL, fsi->sock_inuse, DNOVAL); render(isdb, pre, PT_USEINT, "-\ttcpsck", NULL, NULL, fsi->tcp_inuse, DNOVAL); render(isdb, pre, PT_USEINT, "-\tudpsck", NULL, NULL, fsi->udp_inuse, DNOVAL); render(isdb, pre, PT_USEINT, "-\trawsck", NULL, NULL, fsi->raw_inuse, DNOVAL); render(isdb, pre, PT_USEINT | PT_NEWLIN, "-\tip-frag", NULL, NULL, fsi->frag_inuse, DNOVAL); } /* Print load averages and queue length */ if (GET_QUEUE(act)) { render(isdb, pre, PT_USEINT, "-\trunq-sz", NULL, NULL, fsi->nr_running, DNOVAL); render(isdb, pre, PT_USEINT, "-\tplist-sz", NULL, NULL, fsi->nr_threads, DNOVAL); render(isdb, pre, PT_NOFLAG, "-\tldavg-1", NULL, NULL, NOVAL, (double) fsi->load_avg_1 / 100); render(isdb, pre, PT_NOFLAG, "-\tldavg-5", NULL, NULL, NOVAL, (double) fsi->load_avg_5 / 100); render(isdb, pre, PT_NEWLIN, "-\tldavg-15", NULL, NULL, NOVAL, (double) fsi->load_avg_15 / 100); } /* Print disk statistics */ if (GET_DISK(act)) { int i, j; char *name; double tput, util, await, svctm, arqsz; struct disk_stats *sdi = st_disk[curr], *sdj; for (i = 0; i < file_hdr.sa_nr_disk; i++, ++sdi) { if (!(sdi->major + sdi->minor)) continue; j = check_disk_reg(&file_hdr, st_disk, curr, !curr, i); sdj = st_disk[!curr] + j; name = get_devname(sdi->major, sdi->minor, USE_PRETTY_OPTION(flags)); tput = ((double) (sdi->nr_ios - sdj->nr_ios)) * HZ / itv; util = S_VALUE(sdj->tot_ticks, sdi->tot_ticks, itv); svctm = tput ? util / tput : 0.0; await = (sdi->nr_ios - sdj->nr_ios) ? ((sdi->rd_ticks - sdj->rd_ticks) + (sdi->wr_ticks - sdj->wr_ticks)) / ((double) (sdi->nr_ios - sdj->nr_ios)) : 0.0; arqsz = (sdi->nr_ios - sdj->nr_ios) ? ((sdi->rd_sect - sdj->rd_sect) + (sdi->wr_sect - sdj->wr_sect)) / ((double) (sdi->nr_ios - sdj->nr_ios)) : 0.0; render(isdb, pre, PT_NOFLAG, "%s\ttps", "%s", cons(sv, name, NULL), NOVAL, S_VALUE(sdj->nr_ios, sdi->nr_ios, itv)); render(isdb, pre, PT_NOFLAG, "%s\trd_sec/s", NULL, cons(sv, name, NULL), NOVAL, ll_s_value(sdj->rd_sect, sdi->rd_sect, itv)); render(isdb, pre, PT_NOFLAG, "%s\twr_sec/s", NULL, cons(sv, name, NULL), NOVAL, ll_s_value(sdj->wr_sect, sdi->wr_sect, itv)); render(isdb, pre, PT_NOFLAG, "%s\tavgrq-sz", NULL, cons(sv, name, NULL), NOVAL, arqsz); render(isdb, pre, PT_NOFLAG, "%s\tavgqu-sz", NULL, cons(sv, name, NULL), NOVAL, S_VALUE(sdj->rq_ticks, sdi->rq_ticks, itv) / 1000.0); render(isdb, pre, PT_NOFLAG, "%s\tawait", NULL, cons(sv, name, NULL), NOVAL, await); render(isdb, pre, PT_NOFLAG, "%s\tsvctm", NULL, cons(sv, name, NULL), NOVAL, svctm); render(isdb, pre, PT_NEWLIN, "%s\t%%util", NULL, cons(sv, name, NULL), NOVAL, util / 10.0); } } /* Print NFS client stats */ if (GET_NET_NFS(act)) { render(isdb, pre, PT_NOFLAG, "-\tcall/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_rpccnt, fsi->nfs_rpccnt, itv)); render(isdb, pre, PT_NOFLAG, "-\tretrans/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_rpcretrans, fsi->nfs_rpcretrans, itv)); render(isdb, pre, PT_NOFLAG, "-\tread/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_readcnt, fsi->nfs_readcnt, itv)); render(isdb, pre, PT_NOFLAG, "-\twrite/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_writecnt, fsi->nfs_writecnt, itv)); render(isdb, pre, PT_NOFLAG, "-\taccess/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_accesscnt, fsi->nfs_accesscnt, itv)); render(isdb, pre, PT_NEWLIN, "-\tgetatt/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfs_getattcnt, fsi->nfs_getattcnt, itv)); } /* Print NFS server stats */ if (GET_NET_NFSD(act)) { render(isdb, pre, PT_NOFLAG, "-\tscall/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_rpccnt, fsi->nfsd_rpccnt, itv)); render(isdb, pre, PT_NOFLAG, "-\tbadcall/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_rpcbad, fsi->nfsd_rpcbad, itv)); render(isdb, pre, PT_NOFLAG, "-\tpacket/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_netcnt, fsi->nfsd_netcnt, itv)); render(isdb, pre, PT_NOFLAG, "-\tudp/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_netudpcnt, fsi->nfsd_netudpcnt, itv)); render(isdb, pre, PT_NOFLAG, "-\ttcp/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_nettcpcnt, fsi->nfsd_nettcpcnt, itv)); render(isdb, pre, PT_NOFLAG, "-\thit/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_rchits, fsi->nfsd_rchits, itv)); render(isdb, pre, PT_NOFLAG, "-\tmiss/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_rcmisses, fsi->nfsd_rcmisses, itv)); render(isdb, pre, PT_NOFLAG, "-\tsread/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_readcnt, fsi->nfsd_readcnt, itv)); render(isdb, pre, PT_NOFLAG, "-\tswrite/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_writecnt, fsi->nfsd_writecnt, itv)); render(isdb, pre, PT_NOFLAG, "-\tsaccess/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_accesscnt, fsi->nfsd_accesscnt, itv)); render(isdb, pre, PT_NEWLIN, "-\tsgetatt/s", NULL, NULL, NOVAL, S_VALUE(fsj->nfsd_getattcnt, fsi->nfsd_getattcnt, itv)); }}/* *************************************************************************** * Write system statistics *************************************************************************** */int write_parsable_stats(short curr, unsigned int act, int reset, long *cnt, int use_tm_start, int use_tm_end){ unsigned long long dt, itv, g_itv; char cur_time[26]; /* Check time (1) */ if (!next_slice(file_stats[2].uptime, file_stats[curr].uptime, &file_hdr, reset, interval)) /* Not close enough to desired interval */ return 0; /* Set current timestamp */ set_timestamp(curr, cur_time, 26); /* Check time (2) */ if (use_tm_start && (datecmp(&loc_time, &tm_start) < 0)) /* it's too soon... */ return 0; /* Get interval values */ get_itv_value(&file_stats[curr], &file_stats[!curr], file_hdr.sa_proc, &itv, &g_itv); /* Check time (3) */ if (use_tm_end && (datecmp(&loc_time, &tm_end) > 0)) { /* It's too late... */ *cnt = 0; return 0; } dt = itv / HZ; /* Correct rounding error for dt */ if ((itv % HZ) >= (HZ / 2)) dt++; write_mech_stats(curr, act, dt, itv, g_itv, cur_time); return 1;}/* *************************************************************************** * Display XML activity records *************************************************************************** */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?