免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8957 | 回复: 3
打印 上一主题 下一主题

[应用] 写了一段测试nandflash可靠性的代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-26 15:06 |只看该作者 |倒序浏览
/*说明:测试nandflash的可靠性,有的时候nandflash写进去的内容会变位比如写进去1,可是存储的值变成了0,为了测试这种情况的频率写了下面的测试用例*/
/*nandflash挂载在/home/test目录下,通过不停的往test目录下写两个文件A,B(随便两个文件二进制文件都可以),写进去的文件名和顺序A0,B0,A1,B1,A2,B2....,每写进入一个文件读出来和A或者B比较*/
/*如果一旦发现写满了就删除文件重写继续写,一直持续下去,如果发现读出来的内容和写进入的内容就报错,然后再写一次,如果还错就退出程序*/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>


  6. #include <sys/statfs.h>  //for statfs
  7. #include <sys/vfs.h>   //for statfs

  8. #include <sys/types.h>
  9. #include <sys/stat.h>  //for stat
  10. #include <sys/time.h>
  11. #include "errno.h"

  12. static int nandflash_wrong=0;


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

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

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

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

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

论坛徽章:
0
2 [报告]
发表于 2011-07-26 23:01 |只看该作者
写后读,是不是会从页高速缓存里取数据,而没有真正读?
fclose会清cache吗?

论坛徽章:
0
3 [报告]
发表于 2011-07-28 14:39 |只看该作者
ls 的担忧是正确的。

我测试flash的时候一般直接 dd dump IN/OUT 然后对比。

论坛徽章:
0
4 [报告]
发表于 2011-10-31 19:32 |只看该作者
楼主精神可嘉。
顺便说一句,Linux源码中现在已经加入了mtdtest去测试nand flash的各种特性,包括读写速度,可靠性测试等等,如果是做这方面的测试,直接利用mtdtest会极大地提高工作效率,而不用自己再重新写这方面的测试程序了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP