piprofiler/plugins/DiskWriterPlugin/src/DiskWriterPlugin.cpp
changeset 22 a009639409f5
equal deleted inserted replaced
17:67c6ff54ec25 22:a009639409f5
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  Include Files  
       
    20 
       
    21 #include "DiskWriterPlugin.h"	// CDiskWriterPlugin
       
    22 #include <e32base.h>
       
    23 #include <sysutil.h>
       
    24 //#include <piprofiler/EngineUIDs.h>
       
    25 
       
    26 // constants
       
    27 const TUid KDiskWriterPluginUid = { 0x2001E5BB };   // own UID
       
    28 
       
    29 // engine properties
       
    30 const TUid KEngineStatusPropertyCat={0x2001E5AD};
       
    31 enum TEnginePropertyKeys
       
    32     {
       
    33     EProfilerEngineStatus = 8,
       
    34     EProfilerErrorStatus
       
    35     };
       
    36 
       
    37 /*
       
    38  *
       
    39  *	Class CDiskWriterPlugin implementation
       
    40  *
       
    41  */
       
    42 
       
    43 //  Member Functions
       
    44 CDiskWriterPlugin* CDiskWriterPlugin::NewL(const TUid /*aImplementationUid*/, TAny* /*aInitParams*/)
       
    45     {
       
    46 	LOGTEXT(_L("CDiskWriterPlugin::NewL() - entry"));
       
    47 	CDiskWriterPlugin* self = new (ELeave) CDiskWriterPlugin(KDiskWriterPluginUid);
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop();
       
    51 	LOGTEXT(_L("CDiskWriterPlugin::NewL() - exit"));
       
    52     return self;
       
    53     }
       
    54 
       
    55 CDiskWriterPlugin::CDiskWriterPlugin(const TUid aImplementationUid) :
       
    56 	iWriterType(aImplementationUid.iUid)
       
    57     {
       
    58     LOGTEXT(_L("CDiskWriterPlugin::CDiskWriterPlugin - entry"));
       
    59     isEnabled = EFalse;
       
    60     iWriterId = Id().iUid;
       
    61     LOGTEXT(_L("CDiskWriterPlugin::CDiskWriterPlugin - exit"));
       
    62     }
       
    63 
       
    64 CDiskWriterPlugin::~CDiskWriterPlugin()
       
    65     {
       
    66     LOGTEXT(_L("CDiskWriterPlugin::~CDiskWriterPlugin - entry"));
       
    67 
       
    68     iErrorStatus.Close();
       
    69     
       
    70     if(iWriterHandler)
       
    71         {
       
    72         iWriterHandler->Cancel();
       
    73         delete iWriterHandler;
       
    74         }
       
    75     LOGTEXT(_L("CDiskWriterPlugin::~CDiskWriterPlugin - exit"));
       
    76     }
       
    77 
       
    78 void CDiskWriterPlugin::ConstructL()
       
    79 	{
       
    80 	// second phase constructor, anything that may leave must be constructed here
       
    81 	LOGTEXT(_L("CDiskWriterPlugin::ConstructL() - entry"));
       
    82 	iWriterHandler = CDiskWriterHandler::NewL(this);
       
    83 	User::LeaveIfError(iErrorStatus.Attach(KEngineStatusPropertyCat, EProfilerErrorStatus));
       
    84 	LOGTEXT(_L("CDiskWriterPlugin::ConstructL() - exit"));
       
    85 	}
       
    86 
       
    87 TUid CDiskWriterPlugin::Id() const 
       
    88 	{
       
    89     LOGSTRING2("CDiskWriterPlugin::Id():0x%X", KDiskWriterPluginUid.iUid );
       
    90     return KDiskWriterPluginUid;
       
    91 	}
       
    92 	 
       
    93 void CDiskWriterPlugin::GetWriterVersion(TDes* aDes)
       
    94 	{
       
    95 	_LIT(KDiskWriterVersion, "1.0.0");
       
    96 	aDes->Append(KDiskWriterVersion);
       
    97 	}
       
    98 
       
    99 TInt CDiskWriterPlugin::Start()
       
   100 	{
       
   101 //	if(isEnabled)
       
   102 //		{
       
   103 //		TRAPD(err, iWriterHandler->StartL());
       
   104 //		if( err != KErrNone)
       
   105 //		    {
       
   106 //		    LOGTEXT(_L("Could not start writer plugin"));
       
   107 //		    return err;
       
   108 //		    }
       
   109 //		}
       
   110 	return KErrNone;
       
   111 	}
       
   112 
       
   113 void CDiskWriterPlugin::Stop()
       
   114 	{
       
   115 	// stop writer handler normally
       
   116 	iWriterHandler->Stop();
       
   117 	}
       
   118 
       
   119 TBool CDiskWriterPlugin::GetEnabled()
       
   120 	{
       
   121 	return isEnabled;
       
   122 	}
       
   123 
       
   124 TUint32 CDiskWriterPlugin::GetWriterType()
       
   125 	{
       
   126 	return iWriterType;
       
   127 	}
       
   128 
       
   129 
       
   130 void CDiskWriterPlugin::SetValue( const TWriterPluginValueKeys aKey,
       
   131                                     TDes& aValue )
       
   132     {
       
   133     TRAP_IGNORE( SetValueL( aKey, aValue ) );
       
   134     }
       
   135 
       
   136 
       
   137 void CDiskWriterPlugin::GetValue( const TWriterPluginValueKeys aKey,
       
   138                                     TDes& aValue )
       
   139     {
       
   140     TRAP_IGNORE( GetValueL( aKey, aValue ) );
       
   141     }
       
   142 
       
   143 void CDiskWriterPlugin::SetValueL( const TWriterPluginValueKeys aKey, TDes& aValue )
       
   144     {
       
   145     TInt error(KErrNone);
       
   146     
       
   147     switch( aKey )
       
   148         {
       
   149         case EWriterPluginEnabled:
       
   150             isEnabled = ETrue;
       
   151         	LOGTEXT(_L("CDebOutWriterPlugin::SetValueL - plugin enabled"));
       
   152         	break;
       
   153         case EWriterPluginDisabled:
       
   154             isEnabled = EFalse;
       
   155         	LOGTEXT(_L("CDebOutWriterPlugin::SetValueL - plugin disabled"));	
       
   156             break;
       
   157         case EWriterPluginSettings:	// file name in case of disk writer plugin
       
   158         	iFileName.Zero();
       
   159         	iFileName.Append(aValue);
       
   160         	error = iWriterHandler->TestFile(iFileName);
       
   161         		User::LeaveIfError(error);
       
   162         	break;
       
   163         default:
       
   164         	break;
       
   165         }
       
   166     }
       
   167 
       
   168 void CDiskWriterPlugin::GetValueL( const TWriterPluginValueKeys aKey, TDes& aValue )
       
   169     {
       
   170     switch( aKey )
       
   171         {
       
   172         case EWriterPluginVersion:
       
   173         	GetWriterVersion(&aValue);
       
   174         	break;
       
   175         case EWriterPluginType:
       
   176         	break;
       
   177         case EWriterPluginSettings:	// file name in disk writer case
       
   178         	aValue.Copy(iFileName);
       
   179            default:
       
   180                 break;
       
   181         }
       
   182     }
       
   183 
       
   184 void CDiskWriterPlugin::DoCancel()
       
   185     {
       
   186 	LOGTEXT(_L("CDebOutWriterPlugin::DoCancel - entry"));
       
   187     }
       
   188 
       
   189 void CDiskWriterPlugin::WriteData()
       
   190     {
       
   191     // Activate handler to write data from buffer to output
       
   192     LOGTEXT(_L("CDiskWriterPlugin::WriteData() - entry"));
       
   193     TRAP_IGNORE(iWriterHandler->StartL());
       
   194     LOGTEXT(_L("CDiskWriterPlugin::WriteData() - exit"));
       
   195     }
       
   196 
       
   197 void CDiskWriterPlugin::HandleError(TInt aError)
       
   198     {
       
   199     TInt err(KErrNone);
       
   200     RDebug::Print(_L("CDiskWriterPlugin::HandleError() - error received: %d"), aError);
       
   201     err = iErrorStatus.Set(aError);
       
   202     if(err != KErrNone)
       
   203         {
       
   204         RDebug::Print(_L("CDiskWriterPlugin::HandleError() - error in updating property: %d"), err);
       
   205         }
       
   206     }
       
   207 
       
   208 /*
       
   209  * 
       
   210  * Implementation of class CDiskWriterHandler
       
   211  * 
       
   212  */
       
   213 
       
   214 CDiskWriterHandler* CDiskWriterHandler::NewL(CDiskWriterPlugin* aWriter)
       
   215     {
       
   216 	LOGTEXT(_L("CDiskWriterHandler::NewL() - entry"));
       
   217 	CDiskWriterHandler* self = new (ELeave) CDiskWriterHandler(aWriter);
       
   218     CleanupStack::PushL( self );
       
   219     self->ConstructL();
       
   220     CleanupStack::Pop();
       
   221 	LOGTEXT(_L("CDiskWriterHandler::NewL() - exit"));
       
   222     return self;
       
   223     }
       
   224 
       
   225 CDiskWriterHandler::CDiskWriterHandler(CDiskWriterPlugin* aWriter)  :
       
   226     CActive(EPriorityStandard)
       
   227     {
       
   228     LOGTEXT(_L("CDiskWriterHandler::CDiskWriterHandler - entry"));
       
   229 
       
   230     iWriter = aWriter;
       
   231     
       
   232     // set initial mode to non-stopping
       
   233     iStopping = EFalse;
       
   234     
       
   235     // add the handler to the active scheduler
       
   236     CActiveScheduler::Add(this);
       
   237     
       
   238     LOGTEXT(_L("CDiskWriterHandler::CDiskWriterHandler - exit"));
       
   239     }
       
   240 
       
   241 
       
   242 CDiskWriterHandler::~CDiskWriterHandler()
       
   243     {
       
   244 	LOGTEXT(_L("CDiskWriterHandler::~CDiskWriterHandler - entry"));
       
   245 
       
   246 	LOGTEXT(_L("CDiskWriterHandler::~CDiskWriterHandler - exit"));
       
   247     }    
       
   248     
       
   249 void CDiskWriterHandler::ConstructL()
       
   250 	{
       
   251 	}
       
   252 
       
   253 TInt CDiskWriterHandler::TestFile(const TDesC& totalPrefix) 
       
   254     {
       
   255     TParse parse;
       
   256 
       
   257     TInt err(KErrNone);
       
   258     if((err = parse.Set(totalPrefix, NULL, NULL)) != KErrNone)
       
   259         return err;
       
   260 
       
   261     err = iFs.Connect();
       
   262     if(err != KErrNone)
       
   263         {
       
   264         LOGTEXT(_L("CDiskWriterHandler::TestFile() - Failed to open a session to file server"));
       
   265         return KErrNotFound;
       
   266         }
       
   267     
       
   268     iFs.MkDirAll(parse.FullName());
       
   269     
       
   270     err = iFile.Replace(iFs,parse.FullName(),EFileWrite);
       
   271     if(err != KErrNone)
       
   272         {
       
   273         iFs.Close();
       
   274         return KErrNotFound;
       
   275         }
       
   276     
       
   277     iFileName.Copy(parse.FullName());
       
   278 
       
   279     return KErrNone;
       
   280     }
       
   281 
       
   282 void CDiskWriterHandler::Reset()
       
   283     {
       
   284     // cancel active object
       
   285     Cancel();
       
   286     
       
   287     // start writing new buffer if there is one available
       
   288     TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   289     
       
   290     // empty the rest of the buffers synchronously
       
   291     while(nextBuf != 0)
       
   292         {
       
   293         LOGTEXT(_L("CDiskWriterHandler::Reset - writing to file"));
       
   294         if(nextBuf->iDataSize != 0)
       
   295             {
       
   296             LOGTEXT(_L("CDiskWriterHandler::Reset - writing to file"));
       
   297             WriteMemBufferToFile(*(nextBuf->iBufDes),iStatus);
       
   298             }
       
   299         
       
   300         // empty buffers when profiling stopped
       
   301         iWriter->iStream->AddToFreeBuffers(nextBuf);
       
   302 
       
   303         LOGTEXT(_L("CDiskWriterHandler::Reset - get next full buffer"));
       
   304         // start writing new buffer if there is one available
       
   305         nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   306         LOGSTRING2("CDiskWriterHandler::Reset - got next filled 0x%x",nextBuf);
       
   307         }
       
   308     }
       
   309 
       
   310 void CDiskWriterHandler::HandleFullBuffers()
       
   311     {
       
   312     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - entry"));
       
   313     // previous write operation has finished
       
   314     // release the previous buffer 
       
   315     iWriter->iStream->AddToFreeBuffers(iBufferBeingWritten);
       
   316 
       
   317     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - get next full buffer"));
       
   318     // start writing new buffer if there is one available
       
   319     TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   320 
       
   321     if(nextBuf != 0)
       
   322         {
       
   323         LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - writing to file"));
       
   324         if(nextBuf->iDataSize != 0)
       
   325             {
       
   326             WriteBufferToOutput(nextBuf);
       
   327             }
       
   328         } 
       
   329     LOGTEXT(_L("CDiskWriterHandler::HandleFullBuffers - exit"));
       
   330     }
       
   331 
       
   332 void CDiskWriterHandler::RunL()
       
   333     {
       
   334     LOGTEXT(_L("CDiskWriterHandler::RunL - entry"));
       
   335     // call function to complete full buffer handling
       
   336     HandleFullBuffers();
       
   337     LOGTEXT(_L("CDiskWriterHandler::RunL - exit"));
       
   338     }
       
   339 
       
   340 void CDiskWriterHandler::DoCancel()
       
   341     {
       
   342     
       
   343     }
       
   344 
       
   345 //-----------------------------------------------------------------------------
       
   346 // CPIProfilerTraceCoreLauncher::RunError(TInt aError)
       
   347 // Handle leaves from RunL().
       
   348 //-----------------------------------------------------------------------------
       
   349 TInt CDiskWriterHandler::RunError(TInt aError)
       
   350     {
       
   351     // no reason to continue if disk full or removed
       
   352     iFile.Close();
       
   353     // close handle to file server too
       
   354     iFs.Close();
       
   355     iFileName.Zero();
       
   356     
       
   357     iWriter->HandleError(KErrDiskFull);
       
   358     return aError;
       
   359     }
       
   360 
       
   361 void CDiskWriterHandler::WriteMemBufferToFile(TDesC8& aDes, TRequestStatus& aStatus)
       
   362     {   
       
   363     LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - entry"));
       
   364 
       
   365     TUint sampleSize(aDes.Length());
       
   366     TInt err(KErrNone);
       
   367     TInt drive(0);
       
   368     TDriveInfo info;
       
   369     TBool noDiskSpace(EFalse);
       
   370     
       
   371     err = iFile.Drive(drive,info);
       
   372 
       
   373     // test available disk space 
       
   374     TRAP_IGNORE((noDiskSpace = SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, sampleSize, drive))); 
       
   375     // check first if still space on disk
       
   376     if(noDiskSpace)
       
   377         {
       
   378         // set error to disk full
       
   379         err = KErrDiskFull;
       
   380         LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - disk full, cannot write"));
       
   381         }
       
   382     else
       
   383         {
       
   384         // check if profiling in stopping mode
       
   385         if(iStopping)
       
   386             {
       
   387             // RDebug::Print(_L("CDiskWriterPlugin::WriteMemBufferToFile - data written, length %d, stopping"), aDes.Length());
       
   388             // write to file without status
       
   389             err = iFile.Write(aDes);
       
   390             }
       
   391         else
       
   392             {
       
   393             // RDebug::Print(_L("CDiskWriterPlugin::WriteMemBufferToFile - data written, length %d"), aDes.Length());
       
   394             // write to file with status
       
   395             iFile.Write(aDes,aStatus);
       
   396             }
       
   397         }
       
   398     
       
   399     // check if error in write
       
   400     if(err != KErrNone)
       
   401         {
       
   402         // stop writer handler (and its timer) immediately, DO NOT try write data!
       
   403         Cancel();
       
   404         
       
   405         // no reason to continue if disk full or removed
       
   406         // end of stream detected, file can be closed
       
   407         iFile.Close();
       
   408         // close handle to file server too
       
   409         iFs.Close();
       
   410         iFileName.Zero();
       
   411         
       
   412         // set error status for engine to read
       
   413         iWriter->HandleError(err);
       
   414         }
       
   415     LOGTEXT(_L("CDiskWriterPlugin::WriteMemBufferToFile - exit"));
       
   416     }
       
   417 
       
   418 void CDiskWriterHandler::WriteBufferToOutput(TBapBuf* aBuf)
       
   419     {
       
   420     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - entry"));
       
   421     iBufferBeingWritten = aBuf;
       
   422 
       
   423     // set the data length just to be sure
       
   424     iBufferBeingWritten->iBufDes->SetLength(aBuf->iDataSize);
       
   425 
       
   426     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - writing to file"));
       
   427     WriteMemBufferToFile(*(iBufferBeingWritten->iBufDes), iStatus);
       
   428 
       
   429     // set AO back to active, until filled buffers are emptied 
       
   430     SetActive();
       
   431     
       
   432     LOGTEXT(_L("CDiskWriterPlugin::WriteBufferToOutput - exit"));
       
   433     }
       
   434 
       
   435 void CDiskWriterHandler::StartL()
       
   436 	{
       
   437 	LOGTEXT(_L("CDiskWriterHandler::StartL - entry"));
       
   438     if(!IsActive())
       
   439         {
       
   440         LOGTEXT(_L("CDiskWriterHandler::StartL - is not active"));
       
   441     
       
   442         TBapBuf* nextBuf = iWriter->iStream->GetNextFilledBuffer();
       
   443         LOGSTRING2("CDiskWriterHandler::StartL - got next filled 0x%x",nextBuf);
       
   444     
       
   445         if(nextBuf != 0)
       
   446             {
       
   447             LOGTEXT(_L("CDiskWriterHandler::StartL - writing to file"));
       
   448             WriteBufferToOutput(nextBuf);
       
   449             }
       
   450         }
       
   451     LOGTEXT(_L("CDiskWriterHandler::StartL - exit"));
       
   452 	}
       
   453 
       
   454 void CDiskWriterHandler::Stop()
       
   455 	{
       
   456 	LOGTEXT(_L("CDiskWriterHandler::Stop - entry"));
       
   457 	
       
   458 	// set to stopping mode, needed for emptying the remaining full buffers
       
   459 	iStopping = ETrue;
       
   460 	
       
   461 	// stop the timer
       
   462 	Reset();
       
   463 
       
   464     // end of stream detected, file can be closed
       
   465     iFile.Close();
       
   466     // close handle to file server too
       
   467     iFs.Close();
       
   468     iFileName.Zero();
       
   469     
       
   470     // set mode back to non-stopping
       
   471     iStopping = EFalse;
       
   472     
       
   473     LOGTEXT(_L("CDiskWriterHandler::Stop - exit"));
       
   474 	}
       
   475 
       
   476 // end of file