Linux IO工具 iotop的替代品iopp

iotop虽然在Linux IO监控方面表现出色,但其对内核和Python版本的要求限制了使用。文章介绍了iopp,一个用C语言编写的轻量级替代品,提供类似的IO监控功能。只需简单编译和运行,即可实时查看进程的IO信息,如读写字节数、I/O次数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iotop无疑在linux IO检测上是个不错的工具,但苦于要求内核版本和Python版本,不少朋友放弃了,我也是。偶然间找到了iopp,用c写的,这个跟iotop是一个作用,nice!给大家分享下


安装方法很简单,首先复制下面源代码保存为iopp.c文件

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
 
#define PROC "/proc"
 
#define GET_VALUE(v) \
		p = strchr(p, ':'); \
		++p; \
		++p; \
		q = strchr(p, '\n'); \
		length = q - p; \
		if (length >= BUFFERLEN) \
		{ \
			printf("ERROR - value is larger than the buffer: %d\n", __LINE__); \
			exit(1); \
		} \
		strncpy(value, p, length); \
		value[length] = '\0'; \
		v = atoll(value);
 
#define BTOKB(b) b >> 10
#define BTOMB(b) b >> 20
 
#define BUFFERLEN 255
#define COMMANDLEN 1024
#define VALUELEN 63
 
#define NUM_STRINGS 8
 
struct io_node
{
	int pid;
	long long rchar;
	long long wchar;
	long long syscr;
	long long syscw;
	long long read_bytes;
	long long write_bytes;
	long long cancelled_write_bytes;
	char command[COMMANDLEN + 1];
	struct io_node *next;
};
 
struct io_node *head = NULL;
int command_flag = 0;
int idle_flag = 0;
int mb_flag = 0;
int kb_flag = 0;
int hr_flag = 0;
 
/* Prototypes */
char *format_b(long long);
struct io_node *get_ion(int);
struct io_node *new_ion(char *);
void upsert_data(struct io_node *);
 
char *
format_b(long long amt)
{
	static char retarray[NUM_STRINGS][16];
	static int	index = 0;
	register char *ret;
	register char tag = 'B';
 
	ret = retarray[index];
	index = (index + 1) % NUM_STRINGS;
 
	if (amt >= 10000) {
		amt = (amt + 512) / 1024;
		tag = 'K';
		if (amt >= 10000) {
			amt = (amt + 512) / 1024;
			tag = 'B';
			if (amt >= 10000) {
				amt = (amt + 512) / 1024;
				tag = 'G';
			}
		}
	}
 
	snprintf(ret, sizeof(retarray[index]) - 1, "%lld%c", amt, tag);
 
	return (ret);
}
 
int
get_cmdline(struct io_node *ion)
{
	int fd;
	int length;
	char filename[BUFFERLEN + 1];
	char buffer[COMMANDLEN + 1];
	char *p;
	char *q;
 
 
	length = snprintf(filename, BUFFERLEN, "%s/%d/cmdline", PROC, ion->pid);
	if (length == BUFFERLEN)
		printf("WARNING - filename length may be too big for buffer: %d\n",
				__LINE__);
	fd = open(filename, O_RDONLY);
	if (fd == -1)
		return 1;
	length = read(fd, buffer, sizeof(buffer) - 1);
	close(fd);
	buffer[length] = '\0';
	if (length == 0)
		return 2;
	if (command_flag == 0)
	{
		/*
		 * The command is near the beg
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值