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