Chinaunix首页 | 论坛 | 博客
  • 博客访问: 384391
  • 博文数量: 82
  • 博客积分: 1855
  • 博客等级: 上尉
  • 技术积分: 846
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-12 12:28
文章存档

2013年(3)

2012年(8)

2011年(71)

分类: LINUX

2011-11-19 22:03:23

  1. /*说明:测试nandflash的可靠性,有的时候nandflash写进去的内容会变位比如写进去1,可是存储的值变成了0,为了测试这种情况的频率写了下面的测试用例*/
  2. /*nandflash挂载在/home/test目录下,通过不停的往test目录下写两个文件A,B(随便两个文件二进制文件都可以),写进去的文件名和顺序A0,B0,A1,B1,A2,B2....,每写进入一个文件读出来和A或者B比较*/
  3. /*如果一旦发现写满了就删除文件重写继续写,一直持续下去,如果发现读出来的内容和写进入的内容就报错,然后再写一次,如果还错就退出程序*/

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <fcntl.h>


  9. #include <sys/statfs.h> //for statfs
  10. #include <sys/vfs.h> //for statfs

  11. #include <sys/types.h>
  12. #include <sys/stat.h> //for stat
  13. #include <sys/time.h>
  14. #include "errno.h"

  15. static int nandflash_wrong=0;


  16. int check_remain_space(void)
  17. {
  18.         int stat_flag=0;
  19.     int i;
  20.     struct statfs nandflash_stat;
  21.     long hdisk_remainder_space;
  22.     char path_name_check[32];
  23.     struct stat name_buf_check;
  24.     
  25.         stat_flag = statfs("/home/test", &nandflash_stat);//获取nandflash的信息
  26.     if(stat_flag<0)
  27.     {
  28.             printf("get nandflash info error!\n");
  29.             return -1;
  30.     }
  31.     
  32.     hdisk_remainder_space = (float)nandflash_stat.f_bsize * nandflash_stat.f_bfree / 1024;//检测磁盘空间
  33.     if(hdisk_remainder_space<8*1024) //至少保留8M的空间
  34.     {
  35.       for(i=0;i<200;i++)
  36.       {
  37.               sprintf(path_name_check,"/home/test/A%d",i);
  38.               stat(path_name_check,&name_buf_check);
  39.         if(errno!=ENOENT) //文件存在,则删除
  40.         {
  41.                  unlink(path_name_check);//不能重名
  42.         }
  43.               sprintf(path_name_check,"/home/test/B%d",i);
  44.               stat(path_name_check,&name_buf_check);
  45.         if(errno!=ENOENT) //文件存在,则删除
  46.         {
  47.                 unlink(path_name_check);//不能重名
  48.         }
  49.       }
  50.       sleep(20); //删除文件并不是立即删除,所以要等待文件删除之后再写,防止写的速度比删除速度快,导致磁盘被写满的情况而出错
  51.       return 1;
  52.     }
  53.     return 0;
  54. }

  55. int write_read_nandflash(int mark,unsigned int i,int file_size,char *write_buf,char *read_buf)
  56. {
  57.         FILE *fp;
  58.         char path_name[32];
  59.         unsigned long buf_count=0;
  60.         struct stat name_buf;
  61.         
  62.         if( mark == 0 )
  63.         {
  64.                 sprintf(path_name,"/home/test/A%d",i);//生成不同的文件名A0,A1,A2,A3.......
  65.     }
  66.     else
  67.     {
  68.             sprintf(path_name,"/home/test/B%d",i);
  69.                     
  70.     }
  71.    
  72.     
  73.    stat(path_name,&name_buf);
  74.    if(errno!=ENOENT) //文件存在,则删除
  75.    {
  76.             unlink(path_name);//不能重名
  77.             sleep(3);
  78.    }
  79.     if((fp=fopen(path_name,"w"))==NULL)
  80.     {
  81.         printf("fopen failed!\n");
  82.             return -1;
  83.     }

  84.     fwrite(write_buf,file_size,1,fp);
  85.     fclose(fp);
  86.     
  87. // printf("write file name is:%s\n",path_name);
  88.     if((fp=fopen(path_name,"r"))==NULL)
  89.     {
  90.             printf("fopen failed!\n");
  91.             return -1;
  92.     }
  93.     fread(read_buf,file_size,1,fp);
  94.     
  95.     for(buf_count=0;buf_count<(file_size-1);buf_count++)//将master/slave文件写到nandflash中的内容读出来比较
  96.     {
  97.             if(read_buf[buf_count]!=write_buf[buf_count])
  98.             {
  99.                     
  100.                     system("date");//出错的时候打印的系统时间
  101.                     printf("file name is %s:\n",path_name);
  102.                     unlink(path_name);
  103.                     sleep(3);
  104.                     if((fp=fopen(path_name,"w"))==NULL)
  105.             {
  106.                     printf("fopen failed!\n");
  107.                     return -1;
  108.             }
  109.                     nandflash_wrong++;
  110.                     fwrite(write_buf,file_size,1,fp);//出错之后再写一次
  111.                     fclose(fp);
  112.                     
  113.                     if((fp=fopen(path_name,"r"))==NULL)
  114.             {
  115.                     printf("fopen failed!\n");
  116.                     return -1;
  117.             }
  118.                     fread(read_buf,file_size,1,fp);
  119.                     for(buf_count=0;buf_count<(file_size-1);buf_count++)
  120.                     {
  121.                             if(read_buf[buf_count]!=write_buf[buf_count])
  122.                             {
  123.                                     printf("file name is %s:\n",path_name);
  124.                                     printf("write twice failed!\n");
  125.                                     fclose(fp);
  126.                                     return -1;
  127.                             }
  128.                     }
  129.             }
  130.     }
  131.     fclose(fp);
  132.     return 0;
  133. }

  134. int main(void)
  135. {
  136.         int A_size,B_size;
  137.         A_size=B_size=0;
  138.         
  139.         int A_fd,B_fd;
  140.         
  141.         char *A_buf_read=NULL;
  142.         char *B_buf_read=NULL;
  143.         char *A_buf_write=NULL;
  144.         char *B_buf_write=NULL;
  145.         
  146.         unsigned int iteation=0;

  147.     int ret;
  148.     
  149.     A_fd=0;
  150.     A_fd=open("/home/A",O_RDONLY);
  151.         if(A_fd<0)
  152.         {
  153.                 printf("open A faild!\n");
  154.                 return -1;
  155.         }
  156.         A_size=lseek(A_fd,0,SEEK_END);
  157.         lseek(A_fd,0,SEEK_SET);
  158.         A_buf_write=(char *)malloc(A_size);
  159.         
  160.         if(NULL == A_buf_write)
  161.         {
  162.                 printf("A_buf_write malloc failed!\n");
  163.                 close(A_fd);
  164.                 return -1;
  165.         }
  166.         
  167.         A_buf_read=(char *)malloc(A_size);
  168.         if(NULL == A_buf_read)
  169.         {
  170.                 printf("A_buf_read malloc failed!\n");
  171.                 close(A_fd);
  172.                 return -1;
  173.         }
  174.         if(read(A_fd,A_buf_write,A_size)<0)
  175.         {
  176.                 close(A_fd);
  177.                 printf("read A file failed\n");
  178.         }
  179.         
  180.         close(A_fd);
  181.         B_fd=0;
  182.     B_fd=open("/home/B",O_RDONLY);
  183.         if(B_fd<0)
  184.         {
  185.                 printf("open B faild!\n");
  186.                 return -1;
  187.         }
  188.         B_size=lseek(B_fd,0,SEEK_END);
  189.         lseek(B_fd,0,SEEK_SET);
  190.         B_buf_write=(char *)malloc(B_size);
  191.         
  192.         if(NULL == B_buf_write)
  193.         {
  194.                 printf("B_buf_write malloc failed!\n");
  195.                 close(B_fd);
  196.                 return -1;
  197.         }
  198.         
  199.         B_buf_read=(char *)malloc(B_size);
  200.         if(NULL == B_buf_read)
  201.         {
  202.                 printf("B_buf_read malloc failed!\n");
  203.                 close(B_fd);
  204.                 return -1;
  205.         }
  206.         if(read(B_fd,B_buf_write,B_size)<0)
  207.         {
  208.                 printf("read B file failed\n");
  209.                 close(B_fd);
  210.         }
  211.     close(B_fd);
  212.     
  213.     system("date");//测试开始,开始读写nandflash
  214.     while(1)
  215.     {
  216.             
  217.             if((ret=check_remain_space())<0)//检测磁盘空间,小于8M,要删除文件重新从A0,B0,A1,B1,A2,B2。。。。写
  218.             {
  219.                     printf("check space wrong\n");
  220.                     return -1;
  221.             }
  222.             else if(ret==1)
  223.             {
  224.                     iteation=0;
  225.             }
  226.             
  227.             if(write_read_nandflash(0,iteation,A_size,A_buf_write,A_buf_read)<0)//write A to nand and read it to compare
  228.             {
  229.                     printf("write_read nand flash wrong\n");
  230.                     return -1;
  231.             }
  232.             if(write_read_nandflash(1,iteation,B_size,B_buf_write,B_buf_read)<0)//write B
  233.             {
  234.                     printf("write_read nand flash wrong\n");
  235.                     return -1;
  236.             }
  237.             iteation++;
  238.     }
  239.     
  240.     return 0;
  241. }
阅读(6863) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~