Files
2026-09-22 09:38:26 +08:00

232 lines
5.5 KiB
C++

/*
* SPDX-FileCopyrightText: 2020-2021 Unisoc (Shanghai) Technologies Co., Ltd.
*
* SPDX-License-Identifier: LicenseRef-Unisoc-General-1.0
*/
#include <stdio.h>
#include <sys/un.h>
#include <linux/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include <malloc.h>
#include <signal.h>
#include <syslog.h>
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "FLASHTEST"
#define TAG_NORMAL "FLASHTEST"
#define FLASH_LOGD(fmt...) \
do { \
syslog(LOG_USER | LOG_DEBUG, fmt); \
} while (0)
//#define FALSETEST_TEST_MODE
int filesize = 0;
int testtime = 0;
int currenttestprogress = 0;
bool isExit = false;
char* logPath = "/mnt/data/flashtest.log";
int testResult = -1;
int readerror = 0;
FILE * fplog = NULL;
int fileNum = 0;
char filePath[64]={0};
int rwCount = 0;
void fillFixDataToBuff(char* buff,int len){
char c = 0;
for(int i = 0;i < len; i++ ){
buff[i] = 48+c;
c++;
if(c > 9){
c = 0;
}
}
}
bool isByteBuffEquals(char*buff1, char*buff2,int len) {
for (int i = 0; i < len; i++) {
if (buff1[i] != buff2[i]) {
return false;
}
}
return true;
}
#ifdef FALSETEST_TEST_MODE
void handler(int)
{
if((!isExit) && (testResult== -1)){
signal(SIGALRM, handler);
alarm(10);
currenttestprogress += 10;
printf("test1 time =%d (seconds).\n",currenttestprogress);
fflush(stdout);
FLASH_LOGD("test time =%d (seconds).\n",currenttestprogress);
char buff[256];
memset(buff,0 ,256);
sprintf(buff,"testTimes = %d(min) readWriteError = %d\n,readWriteTimes = %d\n",currenttestprogress/10,readerror,rwCount);
if(fplog != NULL){
fputs(buff,fplog);
fflush(fplog);
fsync(fileno(fplog));
}
}
if(currenttestprogress >= testtime*10){
isExit = true;
}
}
#else
void handler(int)
{
if((!isExit) && (testResult== -1) ){
signal(SIGALRM, handler);
alarm(60);
currenttestprogress += 60;
printf("test time = %d (min) \n",currenttestprogress/60);
fflush(stdout);
FLASH_LOGD("test time = %d (min) \n",currenttestprogress/60);
char buff[256];
memset(buff,0 ,256);
sprintf(buff,"testTimes = %d(min) readWriteError = %d\n,readWriteTimes = %d\n",currenttestprogress/60,readerror,rwCount);
if(fplog != NULL){
fputs(buff,fplog);
fflush(fplog);
}
}
if(currenttestprogress >= testtime*60*60){
isExit = true;
}
}
#endif
void* readWriteTest(void *){
char* content = (char*)malloc(1025);
memset(content,0 ,1025);
fillFixDataToBuff(content,1024);
char* s = (char*)malloc(1025);
memset(s,0 ,1025);
memset(filePath,0,64);
sprintf(filePath,"/mnt/data/flashtest%d.txt",fileNum%10);
while(!isExit){
if((access(filePath,0))!=-1){
remove(filePath);
}
fileNum++;
FLASH_LOGD("fileNum = %d \n",fileNum);
if(fileNum>99999999){
fileNum = 0;
}
memset(filePath,0,64);
sprintf(filePath,"/mnt/data/flashtest%d.txt",fileNum%10);
FILE * fp =fopen(filePath,"w+");
if(fp != NULL){
int filelen = 0;
while((filelen < (filesize*1024*1024)) && (!isExit)){
fputs(content,fp);
filelen += 1024;
}
fflush(fp);
fsync(fileno(fp));
fclose(fp);
rwCount++;
if(!isExit){
FILE * fpr =fopen(filePath,"r");
if(fpr != NULL){
int count = 0;
while((!isExit) && (count < filesize*1024)){
memset(s,0 ,1025);
fgets(s,1025,fpr);
if(!isByteBuffEquals(s,content,1024)){
readerror++;
}
count++;
}
fclose(fpr);
} else{
isExit = true;
testResult = 1;
readerror++;
}
}
} else{
FLASH_LOGD("open file fail");
isExit = true;
testResult = 1;
}
}
if((access(filePath,0))!=-1){
remove(filePath);
}
free(content);
free(s);
if(testResult == -1){
testResult = 0;
}
return NULL;
}
void* handerTime(void *){
handler(0);
return NULL;
}
int main (int argc, char *argv[]){
printf("Welcome to use FlashTest tool(v1.0.1). \n");
if(argc == 3){
filesize = atoi(argv[1]);
testtime = atoi(argv[2]);
} else{
filesize = 1;
testtime = 24;
}
currenttestprogress =0;
testResult = -1;
readerror = 0;
fileNum = 0;
isExit = false;
fplog =fopen(logPath,"w+");
char buff0[256];
memset(buff0,0 ,256);
sprintf(buff0,"@testtime = %d(h) @filesize= %d(M)\n",testtime,filesize);
printf("@testtime = %d(h) @filesize= %d(M)\n",testtime,filesize);
if(fplog != NULL){
fputs(buff0,fplog);
} else{
printf("file open failed!");
return -1;
}
pthread_t wrthread;
int ret = pthread_create(&wrthread,NULL,readWriteTest,NULL);
handler(0);
void *retval;
pthread_join(wrthread,&retval);
char buff[256];
memset(buff,0 ,256);
printf("testResult=%d\n",testResult);
if(testResult == 0){
sprintf(buff,"testTimes = %d(min) readWriteError=%d readWriteTimes = %d\n testResult:pass",currenttestprogress/60,readerror,rwCount);
printf("testResult:pass");
FLASH_LOGD("flashtest testResult:pass");
} else{
sprintf(buff,"testTimes = %d(min) readWriteError=%d readWriteTimes = %d\n testResult:fail",currenttestprogress/60,readerror,rwCount);
printf("testResult:fail");
FLASH_LOGD("flashtest testResult:fail");
}
if(fplog != NULL){
fputs(buff,fplog);
}
fclose(fplog);
return 0;
}