kerneltest/f32test/bench/t_notify_perf_util.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32test\bench\t_notify_perf_util.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "t_notify_perf.h"
       
    19 #include "t_server.h"
       
    20 
       
    21 void DoKillThreadsL()
       
    22     {
       
    23     TInt count = 0;
       
    24     while (count < gNotiThreads.Count())
       
    25         {
       
    26         gNotiThreads[count].Kill(KErrGeneral);
       
    27         count++;
       
    28         }
       
    29     gFileThread.Kill(KErrGeneral);
       
    30     }
       
    31 
       
    32 // Safe way of checking in sub threads, it leaves when check fails, Main thread will catch the error and exit.
       
    33 // if use test() directly in sub-threads, the threads may hang if check fails
       
    34 void SafeTestL(TBool aResult, TInt aId, TInt aLine, TText* aFile)
       
    35     {
       
    36     if (!aResult)
       
    37         {
       
    38         if (aId >= 0)
       
    39             {
       
    40             RDebug::Print(_L("NotificationThread%02d: Failed check in %s at line %d"), aId, aFile, aLine);
       
    41             }
       
    42         else if (aId == KNoThreadId)
       
    43             {
       
    44             RDebug::Print(_L("Failed check in %s at line %d"), aFile, aLine);
       
    45             }
       
    46         CTestExecutor::KillAllTestThreads();
       
    47         }
       
    48     }
       
    49 
       
    50 void SafeTestL(TInt aResult, TInt aExpected, TInt aId, TInt aLine, TText* aFile)
       
    51     {
       
    52     if (aResult != aExpected)
       
    53         {
       
    54         if (aId >= 0)
       
    55             {
       
    56             RDebug::Print(_L("NotificationThread%02d: Failed check in %s at line %d, expected %d, got %d"), aId, aFile, aLine, aExpected, aResult);
       
    57             }
       
    58         else if (aId == KNoThreadId)
       
    59             {
       
    60             RDebug::Print(_L("Failed check in %s at line %d, expected %d, got %d"), aFile, aLine, aExpected, aResult);
       
    61             }
       
    62         CTestExecutor::KillAllTestThreads();
       
    63         }
       
    64     }
       
    65 
       
    66 void SetTestPaths()
       
    67     {
       
    68     gTestPath.FillZ();
       
    69     gLogFilePath.FillZ();
       
    70     
       
    71     gTestPath.Append(gDriveToTest);
       
    72     gTestPath.Append(_L(":\\F32-TST\\T_Notify_Perf\\"));
       
    73     
       
    74 #ifndef __WINSCW__
       
    75     gLogFilePath.Append(gDriveToTest);
       
    76 #else
       
    77     gLogFilePath.Append((TChar)'C');  //If emulator lets stick it on C: (\epoc32\wisncw\c\)
       
    78 #endif
       
    79     if (gPerfMeasure)
       
    80         gLogFilePath.Append(_L(":\\F32-TST\\NPTestLog\\"));
       
    81     else
       
    82         gLogFilePath.Append(_L(":\\F32-TST\\Temp\\"));
       
    83     }
       
    84 
       
    85 // Mapping from file operations to notification types
       
    86 TUint OpNotifyMapping(TUint16& aOption, TInt aOperation)
       
    87     {
       
    88     if (aOption & EEnhanced)
       
    89         {
       
    90         switch(aOperation)
       
    91             {       
       
    92             case EOpCreate:
       
    93             case EOpCreateDir:
       
    94                 return TFsNotification::ECreate;
       
    95             case EOpReplace:
       
    96             case EOpRename:
       
    97             case EOpRenameDir:
       
    98                 return TFsNotification::ERename;
       
    99             case EOpChgAttr:
       
   100                 return TFsNotification::EAttribute;
       
   101             case EOpWrite:
       
   102             case EOpResize:
       
   103             case EOpManyChanges:
       
   104             case EOpManyFiles:
       
   105                 return TFsNotification::EFileChange;
       
   106             case EOpDelete:
       
   107             case EOpDeleteDir:
       
   108                 return TFsNotification::EDelete;
       
   109             case EOpMixed:
       
   110                 return (TUint) (TFsNotification::EAllOps & (~TFsNotification::EOverflow));
       
   111             default:
       
   112                 return (TUint) TFsNotification::EAllOps;
       
   113             }
       
   114         }
       
   115     else if (aOption & EOriginal)
       
   116         {
       
   117         switch(aOperation)
       
   118             {   
       
   119             case EOpCreate:
       
   120             case EOpReplace:
       
   121             case EOpRename:
       
   122                 return ENotifyFile;
       
   123             case EOpChgAttr:
       
   124             case EOpResize:
       
   125                 return ENotifyAttributes;
       
   126             case EOpWrite:
       
   127             case EOpManyChanges:
       
   128             case EOpManyFiles:
       
   129                 return ENotifyWrite;
       
   130             case EOpDelete:
       
   131                 return ENotifyEntry;
       
   132             case EOpCreateDir:
       
   133             case EOpRenameDir:
       
   134             case EOpDeleteDir:
       
   135                 return ENotifyDir;
       
   136             case EOpMixed:
       
   137             default:
       
   138                 return ENotifyAll;
       
   139             }
       
   140         }
       
   141     else if (aOption & EPlugin)
       
   142         {
       
   143         switch(aOperation)
       
   144             {
       
   145             case EOpCreate:
       
   146                 return EMdsFileCreated;
       
   147             case EOpReplace:
       
   148                 return EMdsFileReplaced;
       
   149             case EOpRename:
       
   150                 return EMdsFileRenamed;
       
   151             case EOpDelete:
       
   152                 return EMdsFileDeleted;
       
   153             case EOpRenameDir:
       
   154                 return EMdsDirRenamed;
       
   155             default:
       
   156                 // All other operations are not testable
       
   157                 return EMdsFileUnknown;
       
   158             }
       
   159         }
       
   160     return 0;
       
   161     }
       
   162 
       
   163 // generate file names for testing
       
   164 void FileNameGen(TFileName& aName, TInt aNum, TBool aIsFile = ETrue)
       
   165     {
       
   166     aName.FillZ();
       
   167     aName.Copy(gTestPath);
       
   168     if (aIsFile)
       
   169         aName.AppendFormat(_L("%04d.tst"), aNum);
       
   170     else
       
   171         aName.AppendFormat(_L("DIR%04d\\"), aNum);
       
   172     }
       
   173 
       
   174 void ClearTestPathL()
       
   175     {
       
   176     RDebug::Print(_L("Clearing test path..."));
       
   177     RFs fs;
       
   178     User::LeaveIfError(fs.Connect());
       
   179     CFileMan* fm = CFileMan::NewL(fs);
       
   180     TInt r = fm->RmDir(gTestPath);
       
   181     test(r==KErrNone || r==KErrPathNotFound || r==KErrNotFound);
       
   182     r = fs.MkDirAll(gTestPath);
       
   183     test(r==KErrNone || r==KErrAlreadyExists);
       
   184     
       
   185     delete fm;
       
   186     fs.Close();
       
   187     }
       
   188 
       
   189 void DeleteLogFilesL()
       
   190     {
       
   191     RDebug::Print(_L("Clearing test logs if exist..."));
       
   192     RFs fs;
       
   193     User::LeaveIfError(fs.Connect());
       
   194     CFileMan* fm = CFileMan::NewL(fs);
       
   195     
       
   196     TFileName logFiles;
       
   197     logFiles.Copy(gLogFilePath);
       
   198     logFiles.Append('*');
       
   199     logFiles.Append(gLogPostFix);
       
   200     
       
   201     TInt r = fm->Delete(logFiles);
       
   202     test(r==KErrNone || r==KErrPathNotFound || r==KErrNotFound);
       
   203     if (r != KErrNone)
       
   204         {
       
   205         r = fs.MkDirAll(gLogFilePath);
       
   206         test(r==KErrNone || r==KErrAlreadyExists);
       
   207         }
       
   208     
       
   209     delete fm;
       
   210     fs.Close();
       
   211     }
       
   212 
       
   213 // Copy log files from test drive to MMC
       
   214 void CopyLogFilesL()
       
   215     {
       
   216     RFs fs;
       
   217     User::LeaveIfError(fs.Connect());
       
   218     CFileMan* fm = CFileMan::NewL(fs);
       
   219     
       
   220     TFileName path;
       
   221     path.Append(_L("D:\\NPTLogs\\"));
       
   222     TInt r = fs.MkDirAll(path);
       
   223     test(r == KErrNone || r == KErrAlreadyExists);
       
   224     fm->Copy(gLogFilePath, path);
       
   225     
       
   226     delete fm;
       
   227     fs.Close();
       
   228     }
       
   229 
       
   230 // compare the name of two entries
       
   231 TBool CompareEntryName(const TEntry& aEntry1, const TEntry& aEntry2)
       
   232     {
       
   233     return (aEntry1.iName.Compare(aEntry2.iName) == 0);
       
   234     }
       
   235 
       
   236 // start file operations
       
   237 void DoFileOperationL(TThreadParam* aParam)
       
   238     {
       
   239     CFileOperator fileOperator(aParam->iSetting, *(aParam->iLoggerArray), aParam->iSmphFT, aParam->iSmphNT);
       
   240     fileOperator.DoChangesL();
       
   241     }
       
   242 
       
   243 // start monitoring notification
       
   244 void DoNotificationOperationL(TThreadParam* aParam)
       
   245     {
       
   246     CActiveScheduler* sch = new(ELeave) CActiveScheduler(); 
       
   247     CleanupStack::PushL(sch);
       
   248     CActiveScheduler::Install(sch);
       
   249     
       
   250     CNotifyOperator notifyOperator(aParam->iSetting, aParam->iSmphFT, aParam->iSmphNT, aParam->iLogger);
       
   251     aParam->iSmphFT->Signal();
       
   252     notifyOperator.StartOperationL();
       
   253     
       
   254     CleanupStack::PopAndDestroy();
       
   255     }
       
   256 
       
   257 // entry function of file operaton thread
       
   258 TInt FileOperationThread(TAny* aParam)
       
   259     {
       
   260     CTrapCleanup* cleanup;
       
   261     cleanup = CTrapCleanup::New();
       
   262     
       
   263     TRAPD(r, DoFileOperationL(static_cast<TThreadParam*>(aParam)));
       
   264 
       
   265     delete cleanup;
       
   266     
       
   267     return r;
       
   268     }
       
   269 
       
   270 // entry function of notification thread
       
   271 TInt NotificationOperationThread(TAny* aParam)
       
   272     {
       
   273     CTrapCleanup* cleanup;
       
   274     cleanup = CTrapCleanup::New();
       
   275     
       
   276     TRAPD(r, DoNotificationOperationL(static_cast<TThreadParam*>(aParam)));
       
   277     
       
   278     delete cleanup;
       
   279     
       
   280     return r;
       
   281     }
       
   282 
       
   283 TInt KillerThread(TAny*)
       
   284     {
       
   285     CTrapCleanup* cleanup;
       
   286     cleanup = CTrapCleanup::New();
       
   287     
       
   288     TRAPD(r, DoKillThreadsL());
       
   289     
       
   290     delete cleanup;
       
   291     
       
   292     return r;
       
   293     }