sl@0: // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Do benchmarking comparisons in asynchronous and synchronous modes. sl@0: // sl@0: // sl@0: sl@0: //! @file f32test\concur\t_cfsbench.cpp sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "t_server.h" sl@0: #include "t_tdebug.h" sl@0: sl@0: // The following #defines are for using older (and less accurate) benchmark sl@0: // timings. They use multiple threads to get the operations simultaneous sl@0: // but this is inherrently inaccurate (it depends whether one of them starts sl@0: // and/or ends before the other how accurate the timings are). If you leave sl@0: // them both commented out then the tests will be done in a single thread sl@0: // using asynchronous file operations, thus avoiding the problem. sl@0: sl@0: // Uncomment the following if you want to test asynchronous file operations sl@0: // using two threads rather than in a single thread. sl@0: sl@0: // #define TEST_ASYNC_IN_THREAD sl@0: sl@0: // Uncomment the following if you want to test using synchronous file sl@0: // operations, using two threads to do both at once. sl@0: sl@0: // #define TEST_SYNC_IN_THREAD sl@0: sl@0: struct TStats sl@0: // sl@0: // Statistics -- size and time of operations. sl@0: // sl@0: { sl@0: TInt64 iSize; sl@0: TInt64 iTime; sl@0: void Init() { iSize = 0; iTime = 0; } sl@0: }; sl@0: sl@0: GLDEF_D RTest test(_L("T_CFSBENCH")); sl@0: GLDEF_D RFs TheFs; sl@0: sl@0: LOCAL_D TFullName gFsName; sl@0: LOCAL_D TFullName gFsName1; sl@0: LOCAL_D TFullName gFsName2; sl@0: LOCAL_D TFullName gOldFsName; sl@0: LOCAL_D TFullName gNewFsName; sl@0: LOCAL_D TBool gNoMedia = ETrue; sl@0: sl@0: #if defined(TEST_SYNC_IN_THREAD) || defined(TEST_ASYNC_IN_THREAD) sl@0: LOCAL_D TInt gThreadNumber = 0; sl@0: #endif sl@0: sl@0: LOCAL_D RMutex gDataLock; sl@0: LOCAL_D TStats gWrStats; sl@0: LOCAL_D TStats gRdStats; sl@0: sl@0: _LIT(KFsFile, "CFAFSDLY"); sl@0: _LIT(KFsName, "DelayFS"); sl@0: sl@0: LOCAL_D const TInt32 KSecond = 1000000; sl@0: LOCAL_D const TInt32 KTimeBM = 20; sl@0: LOCAL_D const TInt32 KNumBuf = 100; sl@0: LOCAL_D const TInt32 KBufLen = 0x100; sl@0: LOCAL_D const TInt32 KMaxThr = 10; sl@0: LOCAL_D const TInt32 KMaxLag = 4; sl@0: sl@0: LOCAL_D TBuf8 gBufferArr[KMaxThr][KNumBuf]; sl@0: LOCAL_D TRequestStatus gStatusArr[KMaxThr][KNumBuf]; sl@0: sl@0: LOCAL_C void AddStats(TStats& aStats, TInt64 aSize, TInt64 aTime) sl@0: /// Add values to the statistics. sl@0: { sl@0: gDataLock.Wait(); sl@0: aStats.iSize += aSize; sl@0: aStats.iTime += aTime; sl@0: gDataLock.Signal(); sl@0: } sl@0: sl@0: LOCAL_C TInt GetSpeed(TStats& aStats) sl@0: /// Calculate and return the data throughput from the statistics, rounded. sl@0: { sl@0: gDataLock.Wait(); sl@0: TInt speed = I64LOW((aStats.iSize + aStats.iTime/2) / aStats.iTime); sl@0: gDataLock.Signal(); sl@0: return speed; sl@0: } sl@0: sl@0: LOCAL_C TInt32 GetSpeed(TInt aOps, TInt64 aDtime) sl@0: /// Calculate and return the throughput from the umber of blocks transferred sl@0: /// and the elapsed time. sl@0: { sl@0: TInt64 dsize = MAKE_TINT64(0, aOps) * MAKE_TINT64(0, KBufLen) * MAKE_TINT64(0, KSecond); sl@0: TInt32 speed = I64LOW((dsize + aDtime/2) / aDtime); sl@0: return speed; sl@0: } sl@0: sl@0: LOCAL_C TBool DriveIsOK(TChar c) sl@0: /// Test that a selected drive leter is OK to write files. sl@0: { sl@0: TInt r; sl@0: TInt drv; sl@0: r=TheFs.CharToDrive(c, drv); sl@0: if (r != KErrNone) sl@0: return EFalse; sl@0: TDriveInfo info; sl@0: r=TheFs.Drive(info,drv); sl@0: test(r==KErrNone); sl@0: return (info.iDriveAtt != 0 && !(info.iDriveAtt & KDriveAttRom)); sl@0: } sl@0: sl@0: LOCAL_C TChar MountTestFileSystem(TInt aDrive) sl@0: // sl@0: // Mount a new CTestFileSystem on the drive under test sl@0: // sl@0: { sl@0: TInt r; sl@0: TBuf<64> b; sl@0: TChar c; sl@0: r=TheFs.DriveToChar(aDrive,c); sl@0: test(r==KErrNone); sl@0: b.Format(_L("Mount test file system on %c:"),(TUint)c); sl@0: test.Next(b); sl@0: sl@0: r=TheFs.AddFileSystem(KFsFile); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: r=TheFs.FileSystemName(gOldFsName,aDrive); sl@0: test(r==KErrNone || r==KErrNotFound); sl@0: sl@0: TDriveInfo drv; sl@0: r = TheFs.Drive(drv, aDrive); sl@0: test(r == KErrNone); sl@0: sl@0: gNoMedia = (drv.iType == EMediaUnknown || drv.iType == EMediaNotPresent); sl@0: sl@0: if (gOldFsName.Length() > 0) sl@0: { sl@0: TTest::Printf(_L("Dismount %C: %S"), (TUint)c, &gOldFsName); sl@0: r=TheFs.DismountFileSystem(gOldFsName,aDrive); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: r=TheFs.MountFileSystem(KFsName,aDrive); sl@0: test(r==KErrNone); sl@0: sl@0: r=TheFs.FileSystemName(gNewFsName,aDrive); sl@0: test(r==KErrNone); sl@0: test(gNewFsName.CompareF(KFsName)==0); sl@0: return c; sl@0: } sl@0: sl@0: LOCAL_C void UnmountFileSystem(TInt aDrive) sl@0: /// Unmount a test filesystem and mount the old one. sl@0: { sl@0: TChar c; sl@0: TInt r=TheFs.DriveToChar(aDrive,c); sl@0: test(r==KErrNone); sl@0: r=TheFs.DismountFileSystem(gNewFsName,aDrive); sl@0: test(r==KErrNone); sl@0: // if there's no media present, don't try to mount it sl@0: if (gNoMedia) sl@0: { sl@0: test.Printf(_L("No media on %C: so don't remount it"), (TUint)c); sl@0: } sl@0: else if (gOldFsName.Length() > 0) sl@0: { sl@0: test.Printf(_L("Mount %C: %S"), (TUint)c, &gOldFsName); sl@0: r=TheFs.MountFileSystem(gOldFsName,aDrive); sl@0: test(r==KErrNone); sl@0: } sl@0: if (r != KErrNone) sl@0: test.Printf(_L("Error %d remounting %S on %C\n"), r, &gOldFsName, (TUint)c); sl@0: } sl@0: sl@0: LOCAL_C void RemountFileSystem(TInt aDrive, TBool aSync) sl@0: /// Unmount and remount the file system on the specified drive in the sl@0: /// selected mode. sl@0: /// @param aDrive Drive number (EDriveC etc.). sl@0: /// @param aSync Mount synchronous if true, asynchronous if not. sl@0: { sl@0: TChar c; sl@0: TInt r=TheFs.DriveToChar(aDrive,c); sl@0: r=TheFs.FileSystemName(gFsName, aDrive); sl@0: test(r==KErrNone || r==KErrNotFound); sl@0: sl@0: if (gFsName.Length() > 0) sl@0: { sl@0: r=TheFs.DismountFileSystem(gFsName, aDrive); sl@0: if(r!=KErrNone) sl@0: { sl@0: test.Printf(_L("Error = %d"),r); sl@0: test(EFalse); sl@0: } sl@0: } sl@0: sl@0: TBufC<16> type = _L("asynchronous"); sl@0: if (aSync) sl@0: type = _L("synchronous"); sl@0: test.Printf(_L("Mount filesystem %c: %-8S as %S\n"), (TUint)c, &gFsName, &type); sl@0: sl@0: #ifdef __CONCURRENT_FILE_ACCESS__ sl@0: r=TheFs.MountFileSystem(gFsName, aDrive, aSync); sl@0: #else sl@0: r=TheFs.MountFileSystem(gFsName, aDrive); sl@0: #endif sl@0: sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: enum TOper sl@0: { sl@0: ERead, sl@0: EWrite sl@0: }; sl@0: sl@0: // --------------------------------------------------------------------------- sl@0: sl@0: #if defined(TEST_SYNC_IN_THREAD) sl@0: sl@0: LOCAL_C TInt testSyncAccess(TAny* aData) sl@0: /// sl@0: /// Test read file handling. sl@0: /// sl@0: /// @param aData pointer to the thread data area sl@0: { sl@0: TThreadData& data = *(TThreadData*)aData; sl@0: TFileName fileName = data.iFile; sl@0: TBool dowrite = (data.iData != NULL); sl@0: sl@0: RFs myFs; sl@0: TInt r = myFs.Connect(); sl@0: TEST(r==KErrNone); sl@0: sl@0: r = myFs.SetSessionPath(gSessionPath); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("SetSessionPath returned %d"), r); sl@0: sl@0: TVolumeInfo vol; sl@0: TInt drv; sl@0: r = myFs.CharToDrive(fileName[0], drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("CharToDrive(%c) returned %d"), fileName[0], r); sl@0: r = myFs.Volume(vol, drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("Volume() returned %d"), r); sl@0: sl@0: TInt maxwrite = TInt(vol.iFree / 2 - KBufLen); sl@0: if (maxwrite < KBufLen*2) sl@0: TTest::Fail(HERE, _L("Not enough space to do test, only %d KB available"), sl@0: TInt(vol.iFree/1024)); sl@0: sl@0: RFile f; sl@0: RTimer timer; sl@0: TTime startTime; sl@0: TTime endTime; sl@0: TTimeIntervalMicroSeconds timeTaken; sl@0: sl@0: TBuf8 buff; sl@0: TRequestStatus tstat; sl@0: sl@0: TInt wrnum = 0; sl@0: TInt rdnum = 0; sl@0: sl@0: timer.CreateLocal(); sl@0: sl@0: if (dowrite) sl@0: { sl@0: // write tests sl@0: sl@0: r = f.Replace(myFs, fileName, EFileStreamText | EFileWrite); sl@0: TEST(r==KErrNone); sl@0: sl@0: // wait for both tasks to have a chance to complete opening the files sl@0: User::After(1000); sl@0: sl@0: buff.Fill('_', KBufLen); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: TInt pos = (wrnum * KBufLen) % maxwrite; sl@0: r = f.Write(pos, buff); sl@0: TEST(r==KErrNone); sl@0: ++wrnum; sl@0: } sl@0: sl@0: endTime.HomeTime(); sl@0: timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: sl@0: TInt64 dtime = timeTaken.Int64(); sl@0: TInt64 dsize = wrnum * KBufLen * TInt64(KSecond); sl@0: TInt32 speed = TInt32((dsize + dtime/2) / dtime); sl@0: AddStats(gWrStats, dsize, dtime); sl@0: sl@0: TTest::Printf(_L("%8d writes in %6d mS = %8d bytes per second\n"), sl@0: wrnum, TInt32(dtime)/1000, speed); sl@0: sl@0: timer.Cancel(); sl@0: f.Close(); sl@0: } sl@0: else sl@0: { sl@0: // read tests sl@0: sl@0: r = f.Open(myFs, fileName, EFileStreamText); sl@0: TEST(r==KErrNone); sl@0: sl@0: // wait for both tasks to have a chance to complete opening the files sl@0: User::After(1000); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: TInt pos = (rdnum * KBufLen) % maxwrite; sl@0: r = f.Read(pos, buff, KBufLen); sl@0: TEST(r==KErrNone); sl@0: ++rdnum; sl@0: } sl@0: sl@0: endTime.HomeTime(); sl@0: timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: sl@0: TInt64 dtime = timeTaken.Int64(); sl@0: TInt64 dsize = rdnum * KBufLen * TInt64(KSecond); sl@0: TInt32 speed = TInt32((dsize + dtime/2) / dtime); sl@0: AddStats(gRdStats, dsize, dtime); sl@0: sl@0: // wait to allow the dust to settle sl@0: User::After(KSecond); sl@0: sl@0: TTest::Printf(_L("%8d reads in %6d mS = %8d bytes per second\n"), sl@0: rdnum, TInt32(dtime)/1000, speed); sl@0: sl@0: timer.Cancel(); sl@0: timer.Close(); sl@0: f.Close(); sl@0: sl@0: // delete file after reading it sl@0: myFs.Delete(fileName); sl@0: } sl@0: sl@0: myFs.Close(); sl@0: return r; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: // --------------------------------------------------------------------------- sl@0: sl@0: #if defined(TEST_ASYNC_IN_THREAD) sl@0: sl@0: LOCAL_C TInt testAsyncAccess(TAny* aData) sl@0: // sl@0: /// Test read file handling. sl@0: /// sl@0: /// @param aData pointer to the thread data area sl@0: { sl@0: TThreadData& data = *(TThreadData*)aData; sl@0: TFileName fileName = data.iFile; sl@0: TBool dowrite = (data.iData != NULL); sl@0: TBuf8* buffer = gBufferArr[data.iNum]; sl@0: TRequestStatus* status = gStatusArr[data.iNum]; sl@0: sl@0: RFs myFs; sl@0: TInt r = myFs.Connect(); sl@0: TEST(r==KErrNone); sl@0: sl@0: r = myFs.SetSessionPath(gSessionPath); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("SetSessionPath returned %d"), r); sl@0: sl@0: TVolumeInfo vol; sl@0: TInt drv; sl@0: r = myFs.CharToDrive(fileName[0], drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("CharToDrive(%c) returned %d"), fileName[0], r); sl@0: r = myFs.Volume(vol, drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("Volume() returned %d"), r); sl@0: sl@0: TInt64 maxwrite = vol.iFree / 2 - KBufLen; sl@0: if (maxwrite < KBufLen*2) sl@0: TTest::Fail(HERE, _L("Not enough space to do test, only %d KB available"), sl@0: TInt(vol.iFree/1024)); sl@0: sl@0: RFile f; sl@0: RTimer timer; sl@0: TRequestStatus tstat; sl@0: TTime startTime; sl@0: TTime endTime; sl@0: TTimeIntervalMicroSeconds timeTaken; sl@0: sl@0: TInt wrnum = 0; sl@0: TInt rdnum = 0; sl@0: TInt opnum = 0; sl@0: TInt opfin = 0; sl@0: TInt i; sl@0: sl@0: timer.CreateLocal(); sl@0: sl@0: if (dowrite) sl@0: { sl@0: r = f.Replace(myFs, fileName, EFileStreamText | EFileWrite); sl@0: TEST(r==KErrNone); sl@0: sl@0: // wait for both tasks to have a chance to complete opening the files sl@0: User::After(1000); sl@0: sl@0: for (i = 0; i < KNumBuf; i++) sl@0: buffer[i].Fill('_', KBufLen); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: TInt pos = TInt((wrnum * KBufLen) % maxwrite); sl@0: TInt bnum = opnum++ % KNumBuf; sl@0: f.Write(pos, buffer[bnum], status[bnum]); sl@0: if (opnum - opfin > KMaxLag) sl@0: { sl@0: while (status[opfin % KNumBuf] == KRequestPending) sl@0: User::WaitForRequest(status[opfin % KNumBuf]); sl@0: opfin++; sl@0: } sl@0: ++wrnum; sl@0: } sl@0: sl@0: while (opfin < opnum) sl@0: { sl@0: while (status[opfin % KNumBuf] == KRequestPending) sl@0: User::WaitForRequest(status[opfin % KNumBuf]); sl@0: opfin++; sl@0: } sl@0: sl@0: endTime.HomeTime(); sl@0: TTimeIntervalMicroSeconds timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: sl@0: TInt64 dtime = timeTaken.Int64(); sl@0: TInt64 dsize = wrnum * KBufLen * TInt64(KSecond); sl@0: TInt32 speed = TInt32((dsize + dtime/2) / dtime); sl@0: AddStats(gWrStats, dsize, dtime); sl@0: sl@0: TTest::Printf(_L("%8d writes in %6d mS = %8d bytes per second\n"), sl@0: wrnum, TInt32(dtime)/1000, speed); sl@0: } sl@0: else sl@0: { sl@0: r = f.Open(myFs, fileName, EFileStreamText); sl@0: TEST(r==KErrNone); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: TInt pos = TInt((rdnum * KBufLen) % maxwrite); sl@0: TInt bnum = opnum++ % KNumBuf; sl@0: f.Read(pos, buffer[bnum], status[bnum]); sl@0: if (opnum - opfin > KMaxLag) sl@0: { sl@0: User::WaitForRequest(status[opfin++ % KNumBuf]); sl@0: } sl@0: ++rdnum; sl@0: } sl@0: sl@0: while (opfin < opnum) sl@0: { sl@0: if (status[opfin % KNumBuf] == KRequestPending) sl@0: User::WaitForRequest(status[opfin % KNumBuf]); sl@0: opfin++; sl@0: } sl@0: sl@0: endTime.HomeTime(); sl@0: timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: TInt64 dtime = timeTaken.Int64(); sl@0: TInt64 dsize = rdnum * KBufLen * TInt64(KSecond); sl@0: TInt32 speed = TInt32((dsize + dtime/2) / dtime); sl@0: AddStats(gRdStats, dsize, dtime); sl@0: sl@0: // wait to allow the dust to settle sl@0: User::After(KSecond); sl@0: sl@0: TTest::Printf(_L("%8d reads in %6d mS = %8d bytes per second\n"), sl@0: rdnum, TInt32(dtime)/1000, speed); sl@0: sl@0: myFs.Delete(fileName); sl@0: } sl@0: sl@0: timer.Cancel(); sl@0: timer.Close(); sl@0: f.Close(); sl@0: myFs.Close(); sl@0: return r; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: // --------------------------------------------------------------------------- sl@0: sl@0: class TFileOps sl@0: /// Do operations on a file. sl@0: { sl@0: public: sl@0: TFileOps(); sl@0: TInt Open(TChar dr, TInt n); sl@0: TInt Close(); sl@0: TInt Reset(); sl@0: TInt Erase(); sl@0: TInt Write(); sl@0: TInt Read(); sl@0: TInt End(); sl@0: public: sl@0: TFileName iName; sl@0: RFile iF; sl@0: TBuf8 iBuffer[KMaxLag]; sl@0: TRequestStatus iStatus[KMaxLag]; sl@0: TInt iPtr; sl@0: TInt iNum; sl@0: TInt iOps; sl@0: TInt iMax; sl@0: TBool iOpen; sl@0: }; sl@0: sl@0: TFileOps::TFileOps() : iPtr(0), iNum(0), iOps(0), iMax(0), iOpen(EFalse) sl@0: { sl@0: for (TInt i = 0; i < KMaxLag; i++) sl@0: { sl@0: iStatus[i] = KErrNone; sl@0: iBuffer[i].Fill(TChar('_'), KBufLen); sl@0: } sl@0: } sl@0: sl@0: TInt TFileOps::Open(TChar aDrvCh, TInt aNum) sl@0: /// Open the file for testing, give error if there is not enough space for it. sl@0: /// @param aDrvCh Drive letter. sl@0: /// @param aNum File number suffix. sl@0: { sl@0: TVolumeInfo vol; sl@0: TInt drv; sl@0: TInt r = TheFs.CharToDrive(aDrvCh, drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("CharToDrive(%c) returned %d"), (TUint)aDrvCh, r); sl@0: r = TheFs.Volume(vol, drv); sl@0: if (r != KErrNone) sl@0: TTest::Fail(HERE, _L("Volume(%c:) returned %d"), (TUint)aDrvCh, r); sl@0: sl@0: iMax = I64LOW(vol.iFree / MAKE_TINT64(0,KBufLen)) / 2 - 1; sl@0: if (iMax < 10) sl@0: TTest::Fail(HERE, _L("Not enough space to do test, only %d KB available"), sl@0: I64LOW(vol.iFree/1024)); sl@0: sl@0: Reset(); sl@0: iName.Format(_L("%c:\\TEST_%d"), (TUint)aDrvCh, aNum); sl@0: r = iF.Replace(TheFs, iName, EFileStreamText | EFileWrite); sl@0: if (r == KErrNone) sl@0: iOpen = ETrue; sl@0: return r; sl@0: } sl@0: sl@0: TInt TFileOps::Close() sl@0: /// Close and delete the file, returning the number of operations done. sl@0: { sl@0: if (!iOpen) sl@0: return 0; sl@0: iF.Close(); sl@0: TheFs.Delete(iName); sl@0: iOpen = EFalse; sl@0: return iNum; sl@0: } sl@0: sl@0: TInt TFileOps::Reset() sl@0: /// Reset all of the counts. sl@0: { sl@0: iPtr = 0; sl@0: iNum = 0; sl@0: iOps = 0; sl@0: return 0; sl@0: } sl@0: sl@0: TInt TFileOps::Write() sl@0: /// If there is a free buffer available, start a write. sl@0: { sl@0: if (!iOpen) sl@0: return 0; sl@0: while (iNum < iOps && iStatus[iNum%KMaxLag] != KRequestPending) sl@0: iNum++; sl@0: if (iOps < KMaxLag || iStatus[iPtr] != KRequestPending) sl@0: { sl@0: TInt pos = iNum%iMax * KBufLen; sl@0: iF.Write(pos, iBuffer[iPtr], iStatus[iPtr]); sl@0: iOps++; sl@0: iPtr++; sl@0: iPtr %= KMaxLag; sl@0: return 1; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: TInt TFileOps::Read() sl@0: /// If there is a free buffer available, start a read. sl@0: { sl@0: if (!iOpen) sl@0: return 0; sl@0: while (iNum < iOps && iStatus[iNum%KMaxLag] != KRequestPending) sl@0: iNum++; sl@0: if (iOps < KMaxLag || iStatus[iPtr] != KRequestPending) sl@0: { sl@0: TInt pos = iNum%iMax * KBufLen; sl@0: iF.Read(pos, iBuffer[iPtr], iStatus[iPtr]); sl@0: iOps++; sl@0: iPtr++; sl@0: iPtr %= KMaxLag; sl@0: return 1; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: TInt TFileOps::End() sl@0: /// Wait until all outstanding operations have ended, then return the number. sl@0: { sl@0: if (!iOpen) sl@0: return 0; sl@0: while (iNum < iOps) sl@0: { sl@0: if (iStatus[iNum%KMaxLag] == KRequestPending) sl@0: User::WaitForRequest(iStatus[iNum%KMaxLag]); sl@0: else sl@0: iNum++; sl@0: } sl@0: if (iOps < iMax) sl@0: iMax = iOps; sl@0: return iNum; sl@0: } sl@0: sl@0: LOCAL_C TInt testAsyncAccess(TChar dc1, TChar dc2) sl@0: // sl@0: // Test one drive against the other. sl@0: // sl@0: { sl@0: TFileOps f1; sl@0: TFileOps f2; sl@0: sl@0: f1.Open(dc1, 1); sl@0: if (dc1 != dc2) sl@0: f2.Open(dc2, 2); sl@0: sl@0: TInt op1 = 0; sl@0: TInt op2 = 0; sl@0: RTimer timer; sl@0: TRequestStatus tstat; sl@0: TTime startTime; sl@0: TTime endTime; sl@0: TTimeIntervalMicroSeconds timeTaken; sl@0: sl@0: timer.CreateLocal(); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: TInt num = f1.Write(); sl@0: num += f2.Write(); sl@0: if (num == 0) sl@0: User::WaitForAnyRequest(); sl@0: } sl@0: sl@0: op1 = f1.End(); sl@0: op2 = f2.End(); sl@0: sl@0: endTime.HomeTime(); sl@0: timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: sl@0: TInt64 dtime = timeTaken.Int64(); sl@0: sl@0: TTest::Printf(_L("%c: %8d writes in %6d mS = %8d bytes per second\n"), sl@0: (TUint)dc1, op1, I64LOW(dtime)/1000, GetSpeed(op1, dtime)); sl@0: sl@0: if (dc1 != dc2) sl@0: TTest::Printf(_L("%c: %8d writes in %6d mS = %8d bytes per second\n"), sl@0: (TUint)dc2, op2, I64LOW(dtime)/1000, GetSpeed(op2, dtime)); sl@0: sl@0: AddStats(gWrStats, MAKE_TINT64(0, op1 + op2) * MAKE_TINT64(0, KBufLen) * MAKE_TINT64(0, KSecond), dtime); sl@0: sl@0: // now the reads! sl@0: sl@0: f1.Reset(); sl@0: f2.Reset(); sl@0: sl@0: timer.After(tstat, KTimeBM * KSecond); sl@0: sl@0: startTime.HomeTime(); sl@0: sl@0: while (tstat == KRequestPending) sl@0: { sl@0: f1.Read(); sl@0: f2.Read(); sl@0: User::WaitForAnyRequest(); sl@0: } sl@0: sl@0: op1 = f1.End(); sl@0: op2 = f2.End(); sl@0: sl@0: endTime.HomeTime(); sl@0: timeTaken=endTime.MicroSecondsFrom(startTime); sl@0: sl@0: dtime = timeTaken.Int64(); sl@0: sl@0: TTest::Printf(_L("%c: %8d reads in %6d mS = %8d bytes per second\n"), sl@0: (TUint)dc1, op1, I64LOW(dtime)/1000, GetSpeed(op1, dtime)); sl@0: sl@0: if (dc1 != dc2) sl@0: TTest::Printf(_L("%c: %8d reads in %6d mS = %8d bytes per second\n"), sl@0: (TUint)dc2, op2, I64LOW(dtime)/1000, GetSpeed(op2, dtime)); sl@0: sl@0: AddStats(gRdStats, MAKE_TINT64(0, op1 + op2) * MAKE_TINT64(0, KBufLen) * MAKE_TINT64(0, KSecond), dtime); sl@0: sl@0: test.Printf(_L("\n")); sl@0: test.Printf(_L("average write throughput = %d bytes/sec\n"), GetSpeed(gWrStats)); sl@0: test.Printf(_L("average read throughput = %d bytes/sec\n"), GetSpeed(gRdStats)); sl@0: test.Printf(_L("\n")); sl@0: gWrStats.Init(); sl@0: gRdStats.Init(); sl@0: sl@0: timer.Cancel(); sl@0: timer.Close(); sl@0: f1.Close(); sl@0: f2.Close(); sl@0: // delay for a second to allow the close to complete before dismounting. sl@0: User::After(1000000); sl@0: return KErrNone; sl@0: } sl@0: sl@0: #if defined(TEST_SYNC_IN_THREAD) || defined(TEST_ASYNC_IN_THREAD) sl@0: sl@0: LOCAL_C TInt CreateThread(TThreadFunction aFunc, TChar c, TOper aOper) sl@0: /// Create a thread to do the appropriate operation on a drive. sl@0: { sl@0: TBuf<2> drive(_L("?")); sl@0: TBuf<64> name; sl@0: drive[0] = TText(c); sl@0: drive.UpperCase(); sl@0: TThreadData& d = TTest::Data(gThreadNumber); sl@0: d.iFile.Format(_L("%S:\\TEST%d.FILE"), &drive, gThreadNumber); sl@0: d.iData = (aOper == EWrite ? &aOper : NULL); sl@0: name.Format(_L("Test_%S_%d"), &drive, gThreadNumber); sl@0: TInt r = TTest::Create(gThreadNumber, aFunc, name); sl@0: ++gThreadNumber; sl@0: return r; sl@0: } sl@0: sl@0: LOCAL_C TInt RunThreads(TThreadFunction aFunc, TChar aDrive1, TChar aDrive2, TOper aOper) sl@0: /// Run threads to test one drive against the other at the same time. sl@0: /// The thread will report any error and return it as a value, the program will sl@0: /// exit at a higher level after cleaning up. sl@0: { sl@0: TInt r; sl@0: gThreadNumber = 0; sl@0: if ((r = CreateThread(aFunc, aDrive1, aOper)) != KErrNone) return r; sl@0: if ((r = CreateThread(aFunc, aDrive2, aOper)) != KErrNone) return r; sl@0: TTest::Printf(); sl@0: r = TTest::Run(); sl@0: TTest::Printf(); sl@0: return r; sl@0: } sl@0: sl@0: LOCAL_C TInt testThreads(TThreadFunction aFunc, TChar c, TChar d) sl@0: /// Run threads testing read and write of the drives both ways round. sl@0: /// The thread will report any error and return it as a value, the program will sl@0: /// exit at a higher level after cleaning up. sl@0: { sl@0: TInt r; sl@0: if ((r = RunThreads(aFunc, c, d, EWrite)) != KErrNone) return r; sl@0: if ((r = RunThreads(aFunc, c, d, ERead)) != KErrNone) return r; sl@0: if ((r = RunThreads(aFunc, d, c, EWrite)) != KErrNone) return r; sl@0: if ((r = RunThreads(aFunc, d, c, ERead)) != KErrNone) return r; sl@0: // display totals; sl@0: test.Printf(_L("average write throughput = %d bytes/sec\n"), GetSpeed(gWrStats)); sl@0: test.Printf(_L("average read throughput = %d bytes/sec\n"), GetSpeed(gRdStats)); sl@0: test.Printf(_L("\n")); sl@0: gWrStats.Init(); sl@0: gRdStats.Init(); sl@0: return r; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: LOCAL_C TInt parseCmd(TChar& aDrvCh1, TChar& aDrvCh2) sl@0: /// Get parameters from the comand line; if there aren't enough then sl@0: /// prompt the user for them and return KErrAbort if ^C is pressed. sl@0: { sl@0: while (aDrvCh1 < 'A' || aDrvCh1 > 'Z') sl@0: { sl@0: test.Printf(_L("Enter drive letter: ")); sl@0: while (aDrvCh1 < 'A' || aDrvCh1 > 'Z') sl@0: { sl@0: if (aDrvCh1 == 0x03) sl@0: return KErrAbort; sl@0: aDrvCh1 = User::UpperCase(test.Getch()); sl@0: } sl@0: if (!DriveIsOK(aDrvCh1)) sl@0: { sl@0: test.Printf(_L("%c: is not a valid drive\n"), (TUint)aDrvCh1); sl@0: aDrvCh1 = 0; sl@0: } sl@0: else sl@0: { sl@0: TInt drv; sl@0: TheFs.CharToDrive(aDrvCh1, drv); sl@0: TheFs.FileSystemName(gFsName1, drv); sl@0: test.Printf(_L("%c: (%S)\n"), (TUint)aDrvCh1, &gFsName1); sl@0: } sl@0: } sl@0: sl@0: while (aDrvCh2 < 'A' || aDrvCh2 > 'Z') sl@0: { sl@0: test.Printf(_L("Enter drive letter: ")); sl@0: while (aDrvCh2 < 'A' || aDrvCh2 > 'Z') sl@0: { sl@0: if (aDrvCh2 == 0x03) sl@0: return KErrAbort; sl@0: aDrvCh2 = User::UpperCase(test.Getch()); sl@0: } sl@0: if (!DriveIsOK(aDrvCh2)) sl@0: { sl@0: test.Printf(_L("%c: is not a valid drive\n"), (TUint)aDrvCh2); sl@0: aDrvCh2 = 0; sl@0: } sl@0: else sl@0: { sl@0: TInt drv; sl@0: TheFs.CharToDrive(aDrvCh2, drv); sl@0: TheFs.FileSystemName(gFsName2, drv); sl@0: test.Printf(_L("%c: (%S)\n"), (TUint)aDrvCh2, &gFsName2); sl@0: } sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: GLDEF_C void CallTestsL() sl@0: // sl@0: // Do all tests sl@0: // sl@0: { sl@0: TInt r = TTest::Init(); sl@0: test(r == KErrNone); sl@0: sl@0: TChar drvch0 = TTest::DefaultDriveChar(); sl@0: TChar drvch1 = 0; sl@0: TChar drvch2 = 0; sl@0: TInt drive0; sl@0: TInt drive1; sl@0: TInt drive2; sl@0: sl@0: const TInt KMaxArgs = 4; sl@0: TPtrC argv[KMaxArgs]; sl@0: TInt argc = TTest::ParseCommandArguments(argv, KMaxArgs); sl@0: if (argc > 1) sl@0: drvch0 = User::UpperCase(argv[1][0]); sl@0: if (argc > 2) sl@0: drvch1 = User::UpperCase(argv[2][0]); sl@0: if (argc > 3) sl@0: drvch2 = User::UpperCase(argv[3][0]); sl@0: sl@0: r = TheFs.CharToDrive(drvch0, drive0); sl@0: test(r == KErrNone); sl@0: sl@0: if (TheFs.IsValidDrive(drive0)) sl@0: MountTestFileSystem(drive0); sl@0: else sl@0: test.Printf(_L("Unable to mount test file system\n")); sl@0: sl@0: r = parseCmd(drvch1, drvch2); sl@0: if (r != KErrNone) sl@0: { sl@0: UnmountFileSystem(drive0); sl@0: User::Panic(_L("USER ABORT"), 0); sl@0: } sl@0: sl@0: r = TheFs.CharToDrive(drvch1, drive1); sl@0: test(r == KErrNone); sl@0: r = TheFs.CharToDrive(drvch2, drive2); sl@0: test(r == KErrNone); sl@0: sl@0: r = TheFs.FileSystemName(gFsName1, drive1); sl@0: test(r == KErrNone || r == KErrNotFound); sl@0: r = TheFs.FileSystemName(gFsName2, drive2); sl@0: test(r == KErrNone || r == KErrNotFound); sl@0: sl@0: gDataLock.CreateLocal(); sl@0: sl@0: if (drive1 == drive2) sl@0: { sl@0: // !!! Disable platform security tests until we get the new APIs sl@0: // if (User::Capability() & KCapabilityRoot) sl@0: // CheckMountLFFS(TheFs, drvch1); sl@0: sl@0: test.Printf(_L("Using drive %c: (%S)\n"), sl@0: (TUint)drvch1, &gFsName1); sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test with drive asynchronous")); sl@0: RemountFileSystem(drive1, EFalse); sl@0: testAsyncAccess(drvch1, drvch1); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test with drive synchronous")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: testAsyncAccess(drvch1, drvch1); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // !!! Disable platform security tests until we get the new APIs sl@0: /* if (User::Capability() & KCapabilityRoot) sl@0: { sl@0: CheckMountLFFS(TheFs, drvch1); sl@0: CheckMountLFFS(TheFs, drvch2); sl@0: } sl@0: */ sl@0: test.Printf(_L("Using drives %c: (%S) and %c: (%S)\n"), sl@0: (TUint)drvch1, &gFsName1, (TUint)drvch2, &gFsName2); sl@0: sl@0: #if !defined(TEST_ASYNC_IN_THREAD) sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with both drives async")); sl@0: RemountFileSystem(drive1, EFalse); sl@0: RemountFileSystem(drive2, EFalse); sl@0: testAsyncAccess(drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with 1st drive sync and 2nd async")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, EFalse); sl@0: testAsyncAccess(drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with 1st drive async and 2nd sync")); sl@0: RemountFileSystem(drive1, EFalse); sl@0: RemountFileSystem(drive2, ETrue); sl@0: testAsyncAccess(drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with both drives sync")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, ETrue); sl@0: testAsyncAccess(drvch1, drvch2); sl@0: } sl@0: sl@0: #else sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with both drives asynchronous")); sl@0: RemountFileSystem(drive1, EFalse); sl@0: RemountFileSystem(drive2, EFalse); sl@0: r = testThreads(testAsyncAccess, drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with one drive sync and one async")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, EFalse); sl@0: r = testThreads(testAsyncAccess, drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test async r/w with both drives synchronous")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, ETrue); sl@0: r = testThreads(testAsyncAccess, drvch1, drvch2); sl@0: } sl@0: #endif sl@0: sl@0: #if defined(TEST_SYNC_IN_THREAD) sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test sync r/w with both drives asynchronous")); sl@0: RemountFileSystem(drive1, EFalse); sl@0: RemountFileSystem(drive2, EFalse); sl@0: r = testThreads(testSyncAccess, drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test sync r/w with one drive sync and one async")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, EFalse); sl@0: r = testThreads(testSyncAccess, drvch1, drvch2); sl@0: } sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: test.Next(_L("Test sync r/w with both drives synchronous")); sl@0: RemountFileSystem(drive1, ETrue); sl@0: RemountFileSystem(drive2, ETrue); sl@0: r = testThreads(testSyncAccess, drvch1, drvch2); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: gDataLock.Close(); sl@0: sl@0: UnmountFileSystem(drive0); sl@0: test(r == 0); sl@0: } sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: // sl@0: // Main entry point sl@0: // sl@0: { sl@0: TInt r; sl@0: sl@0: CTrapCleanup* cleanup; sl@0: cleanup=CTrapCleanup::New(); sl@0: __UHEAP_MARK; sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Starting tests...")); sl@0: sl@0: r=TheFs.Connect(); sl@0: test(r==KErrNone); sl@0: sl@0: // TheFs.SetAllocFailure(gAllocFailOn); sl@0: TTime timerC; sl@0: timerC.HomeTime(); sl@0: sl@0: // Do the tests sl@0: TRAP(r,CallTestsL()); sl@0: sl@0: // reset the debug register sl@0: TheFs.SetDebugRegister(0); sl@0: sl@0: TTime endTimeC; sl@0: endTimeC.HomeTime(); sl@0: TTimeIntervalSeconds timeTakenC; sl@0: r=endTimeC.SecondsFrom(timerC,timeTakenC); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Time taken for test = %d seconds\n"),timeTakenC.Int()); sl@0: // TheFs.SetAllocFailure(gAllocFailOff); sl@0: TheFs.Close(); sl@0: test.End(); sl@0: test.Close(); sl@0: __UHEAP_MARKEND; sl@0: delete cleanup; sl@0: return(KErrNone); sl@0: }