ffs_tst.c
资源名称:cli.rar [点击查看]
上传用户:hujq123
上传日期:2016-04-10
资源大小:153k
文件大小:15k
源码类别:
Telnet服务器
开发平台:
Unix_Linux
- /*
- * Copyright (C) 2002 Koninklijke Philips Electronics N.V.,
- * All Rights Reserved.
- *
- * This source code and any compilation or derivative thereof is the
- * proprietary information of Koninklijke Philips Electronics N.V.
- * and is confidential in nature.
- * Under no circumstances is this software to be exposed to or placed
- * under an Open Source License of any type without the expressed
- * written permission of Koninklijke Philips Electronics N.V.
- *
- *###########################################################
- */
- /*!
- * file ffs_tst.c
- *
- * brief -
- *
- */
- /*-----------------------------------------------------------*
- *
- * %version: 2.1.2 %
- * instance: DS_6
- * %date_created: Fri Feb 21 08:04:54 2003 %
- *
- *###########################################################
- */
- /*------------------------------ Includes ------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- //#include <tmlib/dprintf.h>
- #include <psos.h>
- #include <math.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <tmlib/tmFlash.h>
- #include <ops/custom_ops.h>
- #if defined(PROFILING)
- #include <tmlib/profile_api.h>
- #endif
- typedef void (*FfsTstPrintf_t)(void *, char *, ...) ;
- static FfsTstPrintf_t FfsTstPrintf ;
- static void *FfsTstHandle ;
- /*---------------------------- Test parameters -------------------------------*/
- #define MAX_FILE_SIZE (32*1024)
- #define MIN_FILE_SIZE 100
- #define E(command) command
- #define EE(command)
- {
- FfsTstPrintf(FfsTstHandle, " [%d] ----------- %20s == %dn", i, #command, command );
- dump_flash();
- }
- #define FS_ASSERT( assertion, code )
- { if (!(assertion)) {
- fprintf(stderr, "Flash file system assert failed; %s (exiting)n", #code);
- exit(-1);
- } }
- /*----------------------- Directory creating / deletion ----------------------*/
- #define DIR_CREATE(TOP)
- {
- E( mkdir ("/flash/"TOP, 0777 ));
- E( touch ("/flash/"TOP"/a" ));
- E( touch ("/flash/"TOP"/b" ));
- E( touch ("/flash/"TOP"/c" ));
- E( mkdir ("/flash/"TOP"/KLOP", 0777 ));
- E( link ("/flash/"TOP"/a", "/flash/"TOP"/KLOP/aa" ));
- E( link ("/flash/"TOP"/KLOP/aa", "/flash/"TOP ));
- }
- #define DIR_DELETE( TOP)
- {
- E( unlink ("/flash/"TOP"/a" ));
- E( unlink ("/flash/"TOP"/KLOP" ));
- E( unlink ("/flash/"TOP"/a" ));
- E( unlink ("/flash/"TOP"/KLOP/aa" ));
- E( unlink ("/flash/"TOP"/KLOP" ));
- E( unlink ("/flash/"TOP"/aa" ));
- E( unlink ("/flash/"TOP"/b" ));
- E( unlink ("/flash/"TOP"/c" ));
- E( unlink ("/flash/"TOP ));
- }
- /*------------------------ Recursive directory printing ----------------------*/
- Int touch ( String f)
- {
- Int fd= open(f, O_CREAT | O_WRONLY);
- if (fd != -1 && close(fd)) {
- return 0;
- } else {
- return -1;
- }
- }
- void dump_flash() { dump_file_system("/flash", NULL); }
- /*------------------------------- Event handler ------------------------------*/
- static Int nrof_write_errors;
- static void event_handler(FlashEvent);
- static void event_handler(FlashEvent event)
- {
- if (event == FlashWriteError) {
- nrof_write_errors++;
- } else
- if (event == FlashFull) {
- fprintf(stderr,"Flash fulln");
- exit(-1);
- } else
- if (event == FlashDangerousWriteError) {
- fprintf(stderr,"Dangerous write errorn");
- exit(-1);
- }
- }
- /*-------------------------- Cycles handler ----------------------------*/
- typedef struct
- {
- UInt32 lo ;
- UInt32 hi ;
- } Cycles_t ;
- static float WriteBytes ;
- static float ReadBytes ;
- static Cycles_t ElapsedWrite ;
- static Cycles_t ElapsedRead ;
- void CyclesAdd(Cycles_t *pCyclesAccum, Cycles_t *pCycles)
- {
- UInt32 add_lo ;
- UInt32 add_hi ;
- add_hi = pCycles->hi + pCyclesAccum->hi ;
- add_lo = pCycles->lo + pCyclesAccum->lo ;
- if ((add_lo < pCycles->lo) || (add_lo < pCyclesAccum->lo))
- {
- add_hi++ ;
- }
- #if defined(DEBUG)
- FfsTstPrintf(FfsTstHandle, "CyclesAdd #0 %08x %08xn", pCycles->hi, pCycles->lo) ;
- FfsTstPrintf(FfsTstHandle, "CyclesAdd #1 %08x %08xn", pCyclesAccum->hi, pCyclesAccum->lo) ;
- FfsTstPrintf(FfsTstHandle, "CyclesAdd #2 %08x %08xn", add_hi, add_lo) ;
- #endif
- pCyclesAccum->hi = add_hi ;
- pCyclesAccum->lo = add_lo ;
- }
- void CyclesSub(Cycles_t *pCyclesAccum, Cycles_t *pCycles)
- {
- UInt32 sub_lo ;
- UInt32 sub_hi ;
- sub_hi = pCycles->hi - pCyclesAccum->hi ;
- sub_lo = pCycles->lo - pCyclesAccum->lo ;
- if (pCycles->lo < pCyclesAccum->lo)
- {
- sub_hi-- ;
- }
- #if defined(DEBUG)
- FfsTstPrintf(FfsTstHandle, "CyclesSub #0 %08x %08xn", pCycles->hi, pCycles->lo) ;
- FfsTstPrintf(FfsTstHandle, "CyclesSub #1 %08x %08xn", pCyclesAccum->hi, pCyclesAccum->lo) ;
- FfsTstPrintf(FfsTstHandle, "CyclesSub #2 %08x %08xn", sub_hi, sub_lo) ;
- #endif
- pCyclesAccum->hi = sub_hi ;
- pCyclesAccum->lo = sub_lo ;
- }
- void CyclesZero(Cycles_t *pCycles)
- {
- pCycles->hi = 0 ;
- pCycles->lo = 0 ;
- }
- void CyclesStart(Cycles_t *pCycles)
- {
- pCycles->hi = hicycles() ;
- pCycles->lo = cycles() ;
- #if defined(DEBUG)
- FfsTstPrintf(FfsTstHandle, "CyclesStart %08x %08xn", pCycles->hi, pCycles->lo) ;
- #endif
- }
- void CyclesDone(Cycles_t *pCyclesStart)
- {
- Cycles_t Cycles ;
- Cycles.hi = hicycles() ;
- Cycles.lo = cycles() ;
- CyclesSub(pCyclesStart, &Cycles) ;
- #if defined(DEBUG)
- FfsTstPrintf(FfsTstHandle, "CyclesDone %08x %08xn", pCyclesStart->hi, pCyclesStart->lo) ;
- #endif
- }
- float CyclesToSeconds(Cycles_t *pCycles)
- {
- float fCyclesPerSecond ;
- float fLo ;
- float fHi ;
- float fCycles ;
- float fSeconds ;
- fLo = (float) pCycles->lo ;
- fHi = pow(2.0, 32.0) * ((float) pCycles->hi) ;
- fCycles = fLo + fHi ;
- fCyclesPerSecond = (float) Clocks_GetCpuFrequency() ;
- fSeconds = fCycles / fCyclesPerSecond ;
- #if defined(DEBUG)
- FfsTstPrintf(FfsTstHandle, "CyclesToSeconds %10f %10f %10f %10f %10fn",
- fLo, fHi, fCycles, fCyclesPerSecond, fSeconds) ;
- #endif
- return fSeconds ;
- }
- /*-------------------------- File read/write test ----------------------------*/
- Bool write_read_test(Int task, Int count)
- {
- Int i;
- Int fp;
- Int size;
- Int ws;
- Int transfer_size ;
- Int bytes_left ;
- Int errors_found;
- Char *write_data ;
- Char *read_data ;
- Char *data ;
- Char fName[80];
- Cycles_t Elapsed ;
- nrof_write_errors= 0;
- size = (rand() * rand()) % MAX_FILE_SIZE;
- if (size < MIN_FILE_SIZE) {
- size = MIN_FILE_SIZE ;
- }
- write_data = malloc(size) ;
- if (write_data == NULL)
- {
- FfsTstPrintf(FfsTstHandle, "No memory for write buffern") ;
- return False ;
- }
- read_data = malloc(size) ;
- if (read_data == NULL)
- {
- FfsTstPrintf(FfsTstHandle, "No memory for read buffern") ;
- return False ;
- }
- sprintf(fName, "/flash/fst%xf%x.bin", task, count & 0xf);
- for (i = 0; i < size; i++) { write_data[i] = i & 255; }
- for (i = 0; i < size; i++) { read_data [i] = 47; }
- fp = open(fName, O_CREAT | O_BINARY | O_WRONLY, 0777);
- if (fp == -1)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : open for write failed, errno %dn",
- task, count, fName, errno) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- CyclesStart(&Elapsed) ;
- bytes_left = size ;
- data = write_data ;
- while (bytes_left != 0)
- {
- transfer_size = rand() % (size/2) ;
- if (bytes_left < transfer_size)
- {
- transfer_size = bytes_left ;
- }
- ws = write(fp, data, transfer_size);
- if (transfer_size != ws)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : write failed exp %d obs %dn",
- task, count, fName, transfer_size, ws) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- bytes_left -= transfer_size ;
- data += transfer_size ;
- }
- CyclesDone(&Elapsed) ;
- CyclesAdd(&ElapsedWrite, &Elapsed) ;
- WriteBytes += (float) size ;
- if (close(fp) != 0)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : close for write failed, errno %dn",
- task, count, fName, errno) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- fp = open(fName, O_BINARY | O_RDONLY, 0777);
- if (fp == -1)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : open for read failed, errno %dn",
- task, count, fName, errno) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- CyclesStart(&Elapsed) ;
- bytes_left = size ;
- data = read_data ;
- while (bytes_left != 0)
- {
- transfer_size = rand() % (size/2) ;
- if (bytes_left < transfer_size)
- {
- transfer_size = bytes_left ;
- }
- ws = read(fp, data, transfer_size);
- if (transfer_size != ws)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : read failed exp %d obs %dn",
- task, count, fName, transfer_size, ws) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- bytes_left -= transfer_size ;
- data += transfer_size ;
- }
- CyclesDone(&Elapsed) ;
- CyclesAdd(&ElapsedRead, &Elapsed) ;
- ReadBytes += (float) size ;
- if (close(fp) != 0)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : close for read failed, errno %dn",
- task, count, fName, errno) ;
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- errors_found = 0;
- for (i = 0; i < size; i++) {
- if (write_data[i] != read_data[i]) {
- errors_found++;
- if (errors_found < 10) {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : Data error %d, offset %08x. exp %02x obs %02xn",
- task, count, fName, errors_found, i, write_data[i], read_data[i]);
- }
- }
- }
- if (errors_found != 0)
- {
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : %7d bytes failed, errors %dn",
- task, count, fName, size, errors_found);
- free(write_data) ;
- free(read_data) ;
- return False ;
- }
- #if defined(SHOW_PROGRESS)
- FfsTstPrintf(FfsTstHandle, "FfsTst %d %6d %s : %7d bytes passedn",
- task, count, fName, size);
- #endif
- free(write_data) ;
- free(read_data) ;
- return True ;
- }
- /*------------------------------ Main program --------------------------------*/
- void dump_status()
- {
- FfsTstPrintf(FfsTstHandle, "--------------------------------------------------n");
- dump_flash();
- FlashUtil_print();
- FfsTstPrintf(FfsTstHandle, "--------------------------------------------------n");
- }
- void print_heap()
- {
- Pointer x= (Pointer)malloc(0x1030);
- FfsTstPrintf(FfsTstHandle, "0x%08x ", x);
- free(x);
- }
- void ffs_test(FfsTstPrintf_t Printf, void * Handle, int task, int nrof_iterations)
- {
- Int i;
- Int count ;
- Char fName[80];
- #if defined(DEBUG)
- atexit(dump_status);
- #endif
- FfsTstPrintf = Printf ;
- FfsTstHandle = Handle ;
- CyclesZero(&ElapsedWrite) ;
- CyclesZero(&ElapsedRead) ;
- WriteBytes = 0.0 ;
- ReadBytes = 0.0 ;
- FfsTstPrintf(FfsTstHandle, "ffs_test task %d startn", task);
- #if !defined(__FFS_TEST_IN_TW_CLI_FS__)
- dump_flash() ;
- #endif
- for (i = 0; i < nrof_iterations; i++) {
- if (write_read_test(task, i) != True)
- {
- return ;
- }
- }
- for (count = 0 ; count < 16; count++)
- {
- sprintf(fName, "/flash/fst%xf%x.bin", task, count) ;
- unlink(fName) ;
- }
- #if 0
- FfsTstPrintf(FfsTstHandle, "n");
- FfsTstPrintf(FfsTstHandle, "Transfer rates:n") ;
- FfsTstPrintf(FfsTstHandle, " Write %7.2f KB/secondn",
- WriteBytes/CyclesToSeconds(&ElapsedWrite)/1024.0) ;
- FfsTstPrintf(FfsTstHandle, " Read %7.2f KB/secondn",
- ReadBytes/CyclesToSeconds(&ElapsedRead)/1024.0) ;
- FfsTstPrintf(FfsTstHandle, " Write bytes %f seconds %10f cycles %08x %08xn",
- WriteBytes, CyclesToSeconds(&ElapsedWrite),
- ElapsedWrite.hi, ElapsedWrite.lo) ;
- FfsTstPrintf(FfsTstHandle, " Read bytes %f seconds %10f cycles %08x %08xn",
- ReadBytes, CyclesToSeconds(&ElapsedRead),
- ElapsedRead.hi, ElapsedRead.lo) ;
- FfsTstPrintf(FfsTstHandle, "n");
- #endif
- FfsTstPrintf(FfsTstHandle, "ffs_test task %d donen", task);
- }
- int dump_profile(int handle, void *array, int nbytes)
- {
- int bytes_per_line ;
- unsigned char *cp ;
- cp = (unsigned char *) array ;
- bytes_per_line = 32 ;
- while (nbytes-- != 0)
- {
- if (bytes_per_line == 32)
- {
- printf("prof:") ;
- }
- printf(" %02x", *cp++) ;
- bytes_per_line-- ;
- if (bytes_per_line == 0)
- {
- bytes_per_line = 32 ;
- printf("n") ;
- }
- }
- if (bytes_per_line != 32)
- {
- printf("n") ;
- }
- return nbytes ;
- }
- #if !defined(__FFS_TEST_IN_TW_CLI_FS__)
- void main(void)
- {
- Int i;
- Cycles_t Elapsed ;
- tmLibdevErr_t TmErr ;
- #if defined(PROFILING)
- struct profileCaps caps ;
- #endif
- JtagStdio_Init() ;
- printf("n") ;
- printf("startn") ;
- printf("n") ;
- DPsize(1024*32);
- DP(("startn")) ;
- TmErr = TwFlash_InitFileSystem() ;
- if (TmErr != TMLIBDEV_OK)
- {
- printf("Error in TwFlash_InitFileSystem() %08xn", TmErr) ;
- exit(1) ;
- }
- #if defined(PROFILING)
- memset(&caps, 0, sizeof(caps)) ;
- strcpy(caps.version, TMPROF_VERSION) ;
- strcpy(caps.args, "") ;
- printf("profileInitn") ;
- profileInit(&caps, dump_profile, 0) ;
- printf("profileStartn") ;
- profileStart() ;
- #endif
- CyclesStart(&Elapsed) ;
- ffs_test((FfsTstPrintf_t) fprintf, stdout, 0, 100) ;
- CyclesDone(&Elapsed) ;
- printf("elapsed seconds %fn", CyclesToSeconds(&Elapsed)) ;
- printf("elapsed cycles %08x %08xn", Elapsed.hi, Elapsed.lo) ;
- #if defined(PROFILING)
- printf("profileStopn") ;
- profileStop() ;
- printf("profileFlushn") ;
- profileFlush() ;
- #endif
- exit (0);
- }
- #endif /* !defined(__FFS_TEST_IN_TW_CLI_FS__) */