#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
bool check_buf(char *p, char b,int len)
{
int i;
for (i=0; i<len; i++) {
if (p[i] != b){
printf("check_buf: error p[%d]=%02x\n", i, p[i]);
return false;
}
}
return true;
}
int main(int argc, char *argv[])
{
struct stat st;
const static char *stress_abs_file = "/mnt/data/stress-test-file";
char cmd_dd[128];
char cmd_rm[128];
int default_count = 10; //默认10M
int default_bs = 1; //默认1M
int fd, idx, ret, total_cnt, cur_cnt;
char buf[1024];
memset(cmd_dd, 0, sizeof(cmd_dd));
sprintf(cmd_dd, "dd if=/usr/share/mydata of=%s bs=%dM count=%d", stress_abs_file, default_bs, default_count);
memset(cmd_rm, 0, sizeof(cmd_rm));
sprintf(cmd_rm, "rm %s", stress_abs_file);
cur_cnt = 0;
total_cnt = 0;
if ((fd=open("/mnt/data/total_cnt", O_RDWR|O_BINARY|O_CREAT)) >= 0) {
if (read(fd, (char*)&total_cnt, sizeof(total_cnt))>=0){
printf("read last total_cnt=%d\n", total_cnt);
} else {
printf("read last total_cnt failure.\n");
}
close(fd);
} else {
printf("open /mnt/data/total_cnt failure.\n");
}
printf("****************************Start: total_cnt=%d, cur_cnt=%d***********************************\n", total_cnt, cur_cnt);
while (1) {
system("free");
system("df -h");
if (access(stress_abs_file, F_OK) == 0) {
ret = system(cmd_rm);
if (WIFEXITED(ret)) { //正常退出(指调用system函数后的返回值)
if (WEXITSTATUS(ret) == 0) {//正常退出 (指调用system函数内的shell的返回值)
;
} else {
printf("system-shell: %s error exit!!!\n", cmd_rm);
goto RET;
}
} else {
printf("system: %s error exit!!!\n", cmd_rm);
goto RET;
}
}
ret = system(cmd_dd);
if (WIFEXITED(ret)) { //正常退出(指调用system函数后的返回值)
if (WEXITSTATUS(ret) == 0) {//正常退出 (指调用system函数内的shell的返回值)
;
} else {
printf("system-shell: %s error exit!!!\n", cmd_dd);
system("free");
system("df -h");
system("ls -la /mnt/data/");
goto RET;
}
} else {
printf("system: %s error exit!!!\n", cmd_dd);
goto RET;
}
if (stat(stress_abs_file, &st)!=-1 && S_ISREG(st.st_mode)) {
if ((fd=open(stress_abs_file, O_RDWR|O_BINARY)) > -1) {
printf("file size = %dM.\n", ((int)st.st_size+1)/(1024*1024));
idx = 0;
memset(buf, 0xff, sizeof(buf));
while ((ret=read(fd, buf, sizeof(buf))) > 0){
if (!check_buf(buf, 0x5a, ret)) {
printf("Error: abort stress test!!!\n");
goto RET;
}
idx += ret;
if (idx >= (int)st.st_size){
total_cnt++;
cur_cnt++;
}
}
close(fd); //之前忘了关闭文件句柄,导致物理内存和/dev/mtdblock7一直申请,没有释放空间
} else {
printf("system: open %s error\n", cmd_dd);
}
} else {
printf("system: exec %s error\n", cmd_dd);
}
}
RET:
close(fd);
if ((fd=open("/mnt/data/total_cnt", O_RDWR|O_BINARY|O_CREAT)) >= 0) {
if (write(fd, (char*)&total_cnt, sizeof(total_cnt))>=0) {
printf("write total_cnt=%d\n", total_cnt);
} else {
printf("write total_cnt failure.\n");
}
close(fd);
} else {
printf("open /mnt/data/total_cnt failure.\n");
}
printf("****************************End: total_cnt=%d, cur_cnt=%d***********************************\n", total_cnt, cur_cnt);
return 0;
}