persistentstorage/sql/SRC/Server/SqlSrvResourceProfiler.cpp
changeset 0 08ec8eefde2f
child 11 211563e4b919
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2008-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 "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 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <e32debug.h>
       
    18 #include <f32file.h>
       
    19 #include "SqlPanic.h"
       
    20 #include "SqlSrvResourceProfiler.h"
       
    21 #include "SqlResourceProfiler.h"
       
    22 #include "SqliteSymbian.h"
       
    23 #include <hal.h>
       
    24 
       
    25 #ifdef _SQLPROFILER
       
    26 
       
    27 ///////////////////////////////// Heap max alloc /////////////////////////////////////
       
    28 
       
    29 /**
       
    30 If true the max alloc profiling is enabled.
       
    31 @internalComponent
       
    32 */
       
    33 TBool TheSqlSrvProfilerMaxAllocEnabled = EFalse;
       
    34 /**
       
    35 The size of the biggest memory block ever allocated by the SQL server.
       
    36 Set only if compiled with _SQLPROFILER macro.
       
    37 @internalComponent
       
    38 */
       
    39 TInt  TheSqlSrvProfilerMaxAllocSize = 0;
       
    40 
       
    41 ////////////////////////// IPC & SQL tracing related //////////////////////////////////
       
    42 
       
    43 /**
       
    44 If true the tracing is enabled (IPC calls & SQL statements).
       
    45 @internalComponent
       
    46 */
       
    47 static TBool TheSqlSrvProfilerTraceEnabled = EFalse;
       
    48 /**
       
    49 Trace level:
       
    50  - 0: no IPC calls traced (default);
       
    51  - 1: only the 10 most important IPC calls traced - SQL statement execution, ....
       
    52  - 2: all IPC calls traced; 
       
    53 @internalComponent
       
    54 */
       
    55 static TInt TheSqlSrvProfilerTraceLevel = 0;
       
    56 /**
       
    57 If true the SQL statement tracing is enabled.
       
    58 @internalComponent
       
    59 */
       
    60 static TBool TheSqlSrvProfilerSqlTraceEnabled = EFalse;
       
    61 
       
    62 /**
       
    63 When the SQL server boots, TheSqlSrvStartTime will be initialized with the current time then. 
       
    64 @internalComponent
       
    65 */
       
    66 TTime TheSqlSrvStartTime = 0;
       
    67 
       
    68 /**
       
    69 When KSqlSrvProfilerDbName is with non-zero length, then only traces coming from database identified by
       
    70 KSqlSrvProfilerDbName name are printed out. 
       
    71 @internalComponent
       
    72 */
       
    73 //_LIT(KSqlSrvProfilerDbName, "default_avacon.dbSQL");
       
    74 _LIT(KSqlSrvProfilerDbName, "");
       
    75 static TUint TheSqlSrvProfilerHandle = 0;
       
    76 
       
    77 //File "read", "write", "sync" and "set size" counters, incremented inside the OS porting layer.
       
    78 TInt TheSqlSrvProfilerFileRead = 0;
       
    79 TInt TheSqlSrvProfilerFileWrite = 0;
       
    80 TInt TheSqlSrvProfilerFileSync = 0;
       
    81 TInt TheSqlSrvProfilerFileSetSize = 0;
       
    82 
       
    83 static TInt TheSqlSrvProfilerFileRead1 = 0;
       
    84 static TInt TheSqlSrvProfilerFileWrite1 = 0;
       
    85 static TInt TheSqlSrvProfilerFileSync1 = 0;
       
    86 static TInt TheSqlSrvProfilerFileSetSize1 = 0;
       
    87 
       
    88 //Set it to true if you want traces to be stored into a file.
       
    89 static TBool TheSqlSrvProfilerTraceToFile = EFalse;
       
    90 
       
    91 //"Prepared" and "Executed" SQL statement counters
       
    92 static TInt TheSqlSrvProfilerPreparedCnt8;
       
    93 static TInt TheSqlSrvProfilerPreparedCnt16;
       
    94 static TInt TheSqlSrvProfilerExecutedCnt8;
       
    95 static TInt TheSqlSrvProfilerExecutedCnt16;
       
    96 
       
    97 ///////////////////////////////// IPC counters ///////////////////////////////////////
       
    98 
       
    99 /**
       
   100 If true the IPC profiling is enabled.
       
   101 @internalComponent
       
   102 */
       
   103 TBool TheSqlSrvProfilerIpcEnabled = EFalse;
       
   104 /**
       
   105 IPC requests, read and write counters.
       
   106 @internalComponent
       
   107 */
       
   108 TInt  TheSqlSrvProfilerIpc[ESqlIpcLast] = {0};
       
   109 /**
       
   110 IPC read and write - bytes.
       
   111 @internalComponent
       
   112 */
       
   113 TInt64 TheSqlSrvProfilerIpcBytes[ESqlIpcLast] = {0};
       
   114 
       
   115 //////////////////////////////////////////////////////////////////////////////////////
       
   116 
       
   117 /**
       
   118 Starts the specified profiling counter.
       
   119 
       
   120 @leave KErrNotSupported, The requested profiling type is not supported;
       
   121                  		 The function may also leave with some other system-wide error codes.
       
   122 
       
   123 Usage of the IPC call arguments:
       
   124  - Arg 0: [in]  profiling counter type, one of the TSqlResourceProfiler::TSqlCounter enum item values.
       
   125  - Arg 1: [in]  the length of the additional profiling parameters.
       
   126  - Arg 2: [in]  additional profiling parameters.
       
   127 */
       
   128 void TSqlSrvResourceProfiler::StartL(const RMessage2& aMessage)
       
   129 	{
       
   130 	const TSqlResourceProfiler::TSqlCounter KCounterType = static_cast <TSqlResourceProfiler::TSqlCounter> (aMessage.Int0());
       
   131 	TInt err = KErrNone;
       
   132 	switch(KCounterType)
       
   133 		{
       
   134 		case TSqlResourceProfiler::ESqlCounterFileIO:
       
   135 		case TSqlResourceProfiler::ESqlCounterOsCall:
       
   136 		case TSqlResourceProfiler::ESqlCounterOsCallTime:
       
   137 		case TSqlResourceProfiler::ESqlCounterOsCallDetails:
       
   138 			err = sqlite3SymbianProfilerStart(KCounterType);
       
   139 			break;
       
   140 		case TSqlResourceProfiler::ESqlCounterIpc:
       
   141 			TheSqlSrvProfilerIpcEnabled = ETrue;
       
   142 			break;
       
   143 		case TSqlResourceProfiler::ESqlCounterMemory:
       
   144 			err = sqlite3SymbianProfilerStart(KCounterType);
       
   145 			break;
       
   146 		case TSqlResourceProfiler::ESqlCounterMaxAlloc:
       
   147 			TheSqlSrvProfilerMaxAllocEnabled = ETrue;
       
   148 			err = sqlite3SymbianProfilerStart(KCounterType);
       
   149 			break;
       
   150 		case TSqlResourceProfiler::ESqlCounterTrace:
       
   151 		    {
       
   152 			TheSqlSrvProfilerTraceEnabled = ETrue;
       
   153 		    TInt len = aMessage.Int1();
       
   154 		    __SQLPANIC_CLIENT((TUint)len < 64, aMessage, ESqlPanicBadArgument);
       
   155 		    if(len > 0)
       
   156 		        {
       
   157                 TBuf8<64> prmBuf;
       
   158                 aMessage.ReadL(2, prmBuf);
       
   159                 prmBuf.UpperCase();
       
   160                 TPtrC8 ptr(prmBuf);
       
   161                 _LIT8(KLevel0, "L0");
       
   162                 _LIT8(KLevel1, "L1");
       
   163                 _LIT8(KLevel2, "L2");
       
   164                 _LIT8(KSqlStmtTraceOff, "S0");
       
   165                 _LIT8(KSqlStmtTraceOn, "S1");
       
   166                 while(ptr.Length() > 0)
       
   167                     {
       
   168                     TInt pos = ptr.Locate(TChar(';'));
       
   169                     TPtrC8 str = ptr;
       
   170                     if(pos >= 0)
       
   171                         {
       
   172                         str.Set(ptr.Left(pos));
       
   173                         }
       
   174                     if(str == KLevel0)
       
   175                         {
       
   176                         TheSqlSrvProfilerTraceLevel = 0;
       
   177                         }
       
   178                     else if(str == KLevel1)
       
   179                         {
       
   180                         TheSqlSrvProfilerTraceLevel = 1;
       
   181                         }
       
   182                     else if(str == KLevel2)
       
   183                         {
       
   184                         TheSqlSrvProfilerTraceLevel = 2;
       
   185                         }
       
   186                     else if(str == KSqlStmtTraceOff)
       
   187                         {
       
   188                         TheSqlSrvProfilerSqlTraceEnabled = EFalse;
       
   189                         }
       
   190                     else if(str == KSqlStmtTraceOn)
       
   191                         {
       
   192                         TheSqlSrvProfilerSqlTraceEnabled = ETrue;
       
   193                         }
       
   194                     if((TUint)pos > (ptr.Length() - 1))
       
   195                         {
       
   196                         break;
       
   197                         }
       
   198                     ptr.Set(ptr.Mid(pos + 1));
       
   199                     }
       
   200 		        }
       
   201 		    }
       
   202 			break;
       
   203 		default:
       
   204 			err = KErrNotSupported;
       
   205 			break;
       
   206 		}
       
   207 	__SQLLEAVE_IF_ERROR(err);
       
   208 	}
       
   209 	
       
   210 /**
       
   211 Stops the specified profiling counter.
       
   212 
       
   213 @leave KErrNotSupported, The requested profiling type is not supported;
       
   214                  		 The function may also leave with some other system-wide error codes.
       
   215 
       
   216 Usage of the IPC call arguments:
       
   217  - Arg 0: [in]  profiling counter type, one of the TSqlResourceProfiler::TSqlCounter enum item values.
       
   218 */
       
   219 void TSqlSrvResourceProfiler::StopL(const RMessage2& aMessage)
       
   220 	{
       
   221 	const TSqlResourceProfiler::TSqlCounter KCounterType = static_cast <TSqlResourceProfiler::TSqlCounter> (aMessage.Int0());
       
   222 	TInt err = KErrNone;
       
   223 	switch(KCounterType)
       
   224 		{
       
   225 		case TSqlResourceProfiler::ESqlCounterFileIO:
       
   226 		case TSqlResourceProfiler::ESqlCounterOsCall:
       
   227 		case TSqlResourceProfiler::ESqlCounterOsCallTime:
       
   228 		case TSqlResourceProfiler::ESqlCounterOsCallDetails:
       
   229 			err = sqlite3SymbianProfilerStop(KCounterType);
       
   230 			break;
       
   231 		case TSqlResourceProfiler::ESqlCounterIpc:
       
   232 			TheSqlSrvProfilerIpcEnabled = EFalse;
       
   233 			break;
       
   234 		case TSqlResourceProfiler::ESqlCounterMemory:
       
   235 			err = sqlite3SymbianProfilerStop(KCounterType);
       
   236 			break;
       
   237 		case TSqlResourceProfiler::ESqlCounterMaxAlloc:
       
   238 			TheSqlSrvProfilerMaxAllocEnabled = EFalse;
       
   239 			err = sqlite3SymbianProfilerStop(KCounterType);
       
   240 			break;
       
   241         case TSqlResourceProfiler::ESqlCounterTrace:
       
   242             TheSqlSrvProfilerTraceEnabled = EFalse;
       
   243             TheSqlSrvProfilerSqlTraceEnabled = EFalse;
       
   244             TheSqlSrvProfilerTraceLevel = 0;
       
   245             TheSqlSrvProfilerHandle = 0;
       
   246             break;
       
   247 		default:
       
   248 			err = KErrNotSupported;
       
   249 			break;
       
   250 		}
       
   251 	__SQLLEAVE_IF_ERROR(err);
       
   252 	}
       
   253 	
       
   254 /**
       
   255 Resets the specified profiling counter.
       
   256 
       
   257 @leave KErrNotSupported, The requested profiling type is not supported;
       
   258                  		 The function may also leave with some other system-wide error codes.
       
   259 
       
   260 Usage of the IPC call arguments:
       
   261  - Arg 0: [in]  profiling counter type, one of the TSqlResourceProfiler::TSqlCounter enum item values.
       
   262 */
       
   263 void TSqlSrvResourceProfiler::ResetL(const RMessage2& aMessage)
       
   264 	{
       
   265 	const TSqlResourceProfiler::TSqlCounter KCounterType = static_cast <TSqlResourceProfiler::TSqlCounter> (aMessage.Int0());
       
   266 	TInt err = KErrNone;
       
   267 	switch(KCounterType)
       
   268 		{
       
   269 		case TSqlResourceProfiler::ESqlCounterFileIO:
       
   270 		case TSqlResourceProfiler::ESqlCounterOsCall:
       
   271 		case TSqlResourceProfiler::ESqlCounterOsCallTime:
       
   272 		case TSqlResourceProfiler::ESqlCounterOsCallDetails:
       
   273 			err = sqlite3SymbianProfilerReset(KCounterType);
       
   274 			break;
       
   275 		case TSqlResourceProfiler::ESqlCounterIpc:
       
   276 			Mem::FillZ(TheSqlSrvProfilerIpc, sizeof(TheSqlSrvProfilerIpc));
       
   277 			Mem::FillZ(TheSqlSrvProfilerIpcBytes, sizeof(TheSqlSrvProfilerIpcBytes));
       
   278 			break;
       
   279 		case TSqlResourceProfiler::ESqlCounterMemory:
       
   280 			err = sqlite3SymbianProfilerReset(KCounterType);
       
   281 			break;
       
   282 		case TSqlResourceProfiler::ESqlCounterMaxAlloc:
       
   283 			TheSqlSrvProfilerMaxAllocSize = 0;
       
   284 			err = sqlite3SymbianProfilerReset(KCounterType);
       
   285 			break;
       
   286         case TSqlResourceProfiler::ESqlCounterTrace:
       
   287             break;
       
   288 		default:
       
   289 			err = KErrNotSupported;
       
   290 			break;
       
   291 		}
       
   292 	__SQLLEAVE_IF_ERROR(err);
       
   293 	}
       
   294 	
       
   295 /**
       
   296 Retrieves the counter values for the specified profiling counter.
       
   297 
       
   298 @leave KErrNotSupported, The requested profiling type is not supported;
       
   299                  		 The function may also leave with some other system-wide error codes.
       
   300 
       
   301 @see TSqlResourceProfiler
       
   302 
       
   303 Usage of the IPC call arguments:
       
   304  - Arg 0: [in]  profiling counter type, one of the TSqlResourceProfiler::TSqlCounter enum item values.
       
   305  - Arg 1: [in]  the size of the buffer for the profiling counter values.
       
   306  - Arg 2: [out] the buffer for the profiling counter values.
       
   307 */
       
   308 void TSqlSrvResourceProfiler::QueryL(const RMessage2& aMessage)
       
   309 	{
       
   310 	const TSqlResourceProfiler::TSqlCounter KCounterType = static_cast <TSqlResourceProfiler::TSqlCounter> (aMessage.Int0());
       
   311 	const TInt KIpcBufLen = 300;
       
   312 	TBuf8<KIpcBufLen> ipcBuf;
       
   313 	TInt err = KErrNone;
       
   314 	switch(KCounterType)
       
   315 		{
       
   316 		case TSqlResourceProfiler::ESqlCounterFileIO:
       
   317 		case TSqlResourceProfiler::ESqlCounterOsCall:
       
   318 		case TSqlResourceProfiler::ESqlCounterOsCallTime:
       
   319 		case TSqlResourceProfiler::ESqlCounterOsCallDetails:
       
   320 			err = sqlite3SymbianProfilerQuery(KCounterType, ipcBuf);
       
   321 			break;
       
   322 		case TSqlResourceProfiler::ESqlCounterIpc:
       
   323 			ipcBuf.AppendNum(TheSqlSrvProfilerIpc[ESqlIpcRq]);
       
   324 			ipcBuf.Append(TChar(';'));
       
   325 			ipcBuf.AppendNum(TheSqlSrvProfilerIpc[ESqlIpcRead]);
       
   326 			ipcBuf.Append(TChar(';'));
       
   327 			ipcBuf.AppendNum(TheSqlSrvProfilerIpc[ESqlIpcWrite]);
       
   328 			ipcBuf.Append(TChar(';'));
       
   329 			ipcBuf.AppendNum(TheSqlSrvProfilerIpcBytes[ESqlIpcRead]);
       
   330 			ipcBuf.Append(TChar(';'));
       
   331 			ipcBuf.AppendNum(TheSqlSrvProfilerIpcBytes[ESqlIpcWrite]);
       
   332 			ipcBuf.Append(TChar(';'));
       
   333 			break;
       
   334 		case TSqlResourceProfiler::ESqlCounterMemory:
       
   335 			{
       
   336 			TInt totalAllocCells = 0;
       
   337 			TInt totalAllocSize = 0;
       
   338 			TInt totalFreeSpace = 0;
       
   339 			TInt biggestBlockSize = 0;
       
   340 			RHeap& heap = User::Heap();;
       
   341 			totalAllocCells = heap.AllocSize(totalAllocSize);
       
   342 			totalFreeSpace = heap.Available(biggestBlockSize);
       
   343 			ipcBuf.AppendNum(totalAllocCells);
       
   344 			ipcBuf.Append(TChar(';'));
       
   345 			ipcBuf.AppendNum(totalAllocSize);
       
   346 			ipcBuf.Append(TChar(';'));
       
   347 			ipcBuf.AppendNum(totalFreeSpace);
       
   348 			ipcBuf.Append(TChar(';'));
       
   349 			ipcBuf.AppendNum(biggestBlockSize);
       
   350 			ipcBuf.Append(TChar(';'));
       
   351 			err = sqlite3SymbianProfilerQuery(KCounterType, ipcBuf);
       
   352 			}
       
   353 			break;
       
   354 		case TSqlResourceProfiler::ESqlCounterMaxAlloc:
       
   355 			ipcBuf.AppendNum(TheSqlSrvProfilerMaxAllocSize);
       
   356 			ipcBuf.Append(TChar(';'));
       
   357 			err = sqlite3SymbianProfilerQuery(KCounterType, ipcBuf);
       
   358 			break;
       
   359         case TSqlResourceProfiler::ESqlCounterTrace:
       
   360             break;
       
   361 		default:
       
   362 			err = KErrNotSupported;
       
   363 			break;
       
   364 		}
       
   365 	__SQLLEAVE_IF_ERROR(err);
       
   366 	aMessage.WriteL(2, ipcBuf);
       
   367 	}
       
   368 	
       
   369 ////////////////////////// IPC tracing related ////////////////////////////////////////
       
   370 
       
   371 //Max trace line length
       
   372 const TInt KSqlTraceMaxLength = 220;
       
   373 //Trace buffer.
       
   374 static TBuf<KSqlTraceMaxLength> TheSqlTraceBuf;
       
   375 
       
   376 //IPC calls names - begin
       
   377 _LIT(KSqlSrvDbCreate, "DbCreate");
       
   378 _LIT(KSqlSrvDbCreateSecure, "DbCreateSecure");
       
   379 _LIT(KSqlSrvDbOpen, "DbOpen");
       
   380 _LIT(KSqlSrvDbOpenFromHandle, "DbOpenFromHandle");
       
   381 _LIT(KSqlSrvDbClose, "DbClose");
       
   382 _LIT(KSqlSrvDbCopy, "DbCopy");
       
   383 _LIT(KSqlSrvDbDelete, "DbDelete");
       
   384 _LIT(KSqlSrvLastErrorMsg, "LastErrorMsg");
       
   385 _LIT(KSqlSrvDbExec8, "DbExec8");
       
   386 _LIT(KSqlSrvDbExec16, "DbExec16");
       
   387 _LIT(KSqlSrvDbSetIsolationLevel, "DbSetIsolationLevel");
       
   388 _LIT(KSqlSrvDbGetSecurityPolicy, "DbGetSecurityPolicy");
       
   389 _LIT(KSqlSrvDbAttach, "DbAttach");
       
   390 _LIT(KSqlSrvDbAttachFromHandle, "DbAttachFromHandle");
       
   391 _LIT(KSqlSrvDbDetach, "DbDetach");
       
   392 _LIT(KSqlSrvDbScalarFullSelect8, "DbScalarFullSelect8");
       
   393 _LIT(KSqlSrvDbScalarFullSelect16, "DbScalarFullSelect16");
       
   394 _LIT(KSqlSrvDbInTransaction, "DbInTransaction");
       
   395 _LIT(KSqlSrvDbSize, "DbSize");
       
   396 _LIT(KSqlSrvDbSize2, "DbSize2");
       
   397 _LIT(KSqlSrvDbBlobSource, "DbBlobSource");
       
   398 _LIT(KSqlSrvDbLastInsertedRowId, "DbLastInsertedRowId");
       
   399 _LIT(KSqlSrvDbCompact, "DbCompact");
       
   400 _LIT(KSqlSrvDbReserveDriveSpace, "DbReserveDriveSpace");
       
   401 _LIT(KSqlSrvDbFreeReservedSpace, "DbFreeReservedSpace");
       
   402 _LIT(KSqlSrvDbGetReserveAccess, "DbGetReserveAccess");
       
   403 _LIT(KSqlSrvDbReleaseReserveAccess, "DbReleaseReserveAccess");
       
   404 _LIT(KSqlSrvStmtPrepare8, "StmtPrepare8");
       
   405 _LIT(KSqlSrvStmtPrepare16, "StmtPrepare16");
       
   406 _LIT(KSqlSrvStmtClose, "StmtClose");
       
   407 _LIT(KSqlSrvStmtReset, "StmtReset");
       
   408 _LIT(KSqlSrvStmtExec, "StmtExec");
       
   409 _LIT(KSqlSrvStmtAsyncExec, "StmtAsyncExec");
       
   410 _LIT(KSqlSrvStmtBindExec, "StmtBindExec");
       
   411 _LIT(KSqlSrvStmtAsyncBindExec, "StmtAsyncBindExec");
       
   412 _LIT(KSqlSrvStmtNext, "StmtNext");
       
   413 _LIT(KSqlSrvStmtBindNext, "StmtBindNext");
       
   414 _LIT(KSqlSrvStmtColumnNames, "StmtColumnNames");
       
   415 _LIT(KSqlSrvStmtParamNames, "StmtParamNames");
       
   416 _LIT(KSqlSrvStmtColumnSource, "StmtColumnSource");
       
   417 _LIT(KSqlSrvStmtBinParamSink, "StmtBinParamSink");
       
   418 _LIT(KSqlSrvStmtTxtParamSink16, "StmtTxtParamSink16");
       
   419 _LIT(KSqlSrvStmtBufFlat, "StmtBufFlat");
       
   420 _LIT(KSqlSrvStmtColumnValue, "StmtColumnValue");
       
   421 _LIT(KSqlSrvStmtDeclColumnTypes, "StmtDeclColumnTypes");
       
   422 _LIT(KSqlSrvStreamRead, "StreamRead");
       
   423 _LIT(KSqlSrvStreamWrite, "StreamWrite");
       
   424 _LIT(KSqlSrvStreamSize, "StreamSize");
       
   425 _LIT(KSqlSrvStreamSynch, "StreamSynch");
       
   426 _LIT(KSqlSrvStreamClose, "StreamClose");
       
   427 //IPC calls names - end
       
   428 
       
   429 //Gets as an argument the IPC call type in "aCode" parameter.
       
   430 //Returns: 
       
   431 // 0 or positive integer: the IPC call is one of the 10 most important IPC calls (trace level 0).
       
   432 // KErrNotFound         : the IPC call is some of the other possible call types (trace level 1).
       
   433 // KErrNotSupported     : unknown IPC call type.
       
   434 //
       
   435 // aIpcCallName will always be set to IPC call name descriptor.
       
   436 static TInt SqlIpcTraceIdxAndName(TSqlSrvFunction aCode, TPtrC& aIpcCallName)
       
   437     {
       
   438     TInt rc = KErrNotFound;
       
   439     switch(aCode)
       
   440         {
       
   441         case ESqlSrvDbCreate:
       
   442             aIpcCallName.Set(KSqlSrvDbCreate);
       
   443             break;
       
   444         case ESqlSrvDbCreateSecure:
       
   445             aIpcCallName.Set(KSqlSrvDbCreateSecure);
       
   446             break;
       
   447         case ESqlSrvDbOpen:
       
   448             aIpcCallName.Set(KSqlSrvDbOpen);
       
   449             break;
       
   450         case ESqlSrvDbOpenFromHandle:
       
   451             aIpcCallName.Set(KSqlSrvDbOpenFromHandle);
       
   452             break;
       
   453         case ESqlSrvDbClose:
       
   454             aIpcCallName.Set(KSqlSrvDbClose);
       
   455             break;
       
   456         case ESqlSrvDbCopy:
       
   457             aIpcCallName.Set(KSqlSrvDbCopy);
       
   458             break;
       
   459         case ESqlSrvDbDelete:
       
   460             aIpcCallName.Set(KSqlSrvDbDelete);
       
   461             break;
       
   462         case ESqlSrvLastErrorMsg:
       
   463             aIpcCallName.Set(KSqlSrvLastErrorMsg);
       
   464             break;
       
   465         case ESqlSrvDbExec8:
       
   466             aIpcCallName.Set(KSqlSrvDbExec8);
       
   467             rc = 0;
       
   468             break;
       
   469         case ESqlSrvDbExec16:
       
   470             aIpcCallName.Set(KSqlSrvDbExec16);
       
   471             rc = 1;
       
   472             break;
       
   473         case ESqlSrvDbSetIsolationLevel:
       
   474             aIpcCallName.Set(KSqlSrvDbSetIsolationLevel);
       
   475             break;
       
   476         case ESqlSrvDbGetSecurityPolicy:
       
   477             aIpcCallName.Set(KSqlSrvDbGetSecurityPolicy);
       
   478             break;
       
   479         case ESqlSrvDbAttach:
       
   480             aIpcCallName.Set(KSqlSrvDbAttach);
       
   481             break;
       
   482         case ESqlSrvDbAttachFromHandle:
       
   483             aIpcCallName.Set(KSqlSrvDbAttachFromHandle);
       
   484             break;
       
   485         case ESqlSrvDbDetach:
       
   486             aIpcCallName.Set(KSqlSrvDbDetach);
       
   487             break;
       
   488         case ESqlSrvDbScalarFullSelect8:
       
   489             aIpcCallName.Set(KSqlSrvDbScalarFullSelect8);
       
   490             rc = 2;
       
   491             break;
       
   492         case ESqlSrvDbScalarFullSelect16:
       
   493             aIpcCallName.Set(KSqlSrvDbScalarFullSelect16);
       
   494             rc = 3;
       
   495             break;
       
   496         case ESqlSrvDbInTransaction:
       
   497             aIpcCallName.Set(KSqlSrvDbInTransaction);
       
   498             break;
       
   499         case ESqlSrvDbSize:
       
   500             aIpcCallName.Set(KSqlSrvDbSize);
       
   501             break;
       
   502         case ESqlSrvDbSize2:
       
   503             aIpcCallName.Set(KSqlSrvDbSize2);
       
   504             break;
       
   505         case ESqlSrvDbBlobSource:
       
   506             aIpcCallName.Set(KSqlSrvDbBlobSource);
       
   507             break;
       
   508         case ESqlSrvDbLastInsertedRowId:
       
   509             aIpcCallName.Set(KSqlSrvDbLastInsertedRowId);
       
   510             break;
       
   511         case ESqlSrvDbCompact:
       
   512             aIpcCallName.Set(KSqlSrvDbCompact);
       
   513             break;
       
   514         case ESqlSrvDbReserveDriveSpace:
       
   515             aIpcCallName.Set(KSqlSrvDbReserveDriveSpace);
       
   516             break;
       
   517         case ESqlSrvDbFreeReservedSpace:
       
   518             aIpcCallName.Set(KSqlSrvDbFreeReservedSpace);
       
   519             break;
       
   520         case ESqlSrvDbGetReserveAccess:
       
   521             aIpcCallName.Set(KSqlSrvDbGetReserveAccess);
       
   522             break;
       
   523         case ESqlSrvDbReleaseReserveAccess:
       
   524             aIpcCallName.Set(KSqlSrvDbReleaseReserveAccess);
       
   525             break;
       
   526         case ESqlSrvStmtPrepare8:
       
   527             aIpcCallName.Set(KSqlSrvStmtPrepare8);
       
   528             break;
       
   529         case ESqlSrvStmtPrepare16:
       
   530             aIpcCallName.Set(KSqlSrvStmtPrepare16);
       
   531             break;
       
   532         case ESqlSrvStmtClose:
       
   533             aIpcCallName.Set(KSqlSrvStmtClose);
       
   534             break;
       
   535         case ESqlSrvStmtReset:
       
   536             aIpcCallName.Set(KSqlSrvStmtReset);
       
   537             break;
       
   538         case ESqlSrvStmtExec:
       
   539             aIpcCallName.Set(KSqlSrvStmtExec);
       
   540             rc = 4;
       
   541             break;
       
   542         case ESqlSrvStmtAsyncExec:
       
   543             aIpcCallName.Set(KSqlSrvStmtAsyncExec);
       
   544             rc = 5;
       
   545             break;
       
   546         case ESqlSrvStmtBindExec:
       
   547             aIpcCallName.Set(KSqlSrvStmtBindExec);
       
   548             rc = 6;
       
   549             break;
       
   550         case ESqlSrvStmtAsyncBindExec:
       
   551             aIpcCallName.Set(KSqlSrvStmtAsyncBindExec);
       
   552             rc = 7;
       
   553             break;
       
   554         case ESqlSrvStmtNext:
       
   555             aIpcCallName.Set(KSqlSrvStmtNext);
       
   556             rc = 8;
       
   557             break;
       
   558         case ESqlSrvStmtBindNext:
       
   559             aIpcCallName.Set(KSqlSrvStmtBindNext);
       
   560             rc = 9;
       
   561             break;
       
   562         case ESqlSrvStmtColumnNames:
       
   563             aIpcCallName.Set(KSqlSrvStmtColumnNames);
       
   564             break;
       
   565         case ESqlSrvStmtParamNames:
       
   566             aIpcCallName.Set(KSqlSrvStmtParamNames);
       
   567             break;
       
   568         case ESqlSrvStmtColumnSource:
       
   569             aIpcCallName.Set(KSqlSrvStmtColumnSource);
       
   570             break;
       
   571         case ESqlSrvStmtBinParamSink:
       
   572             aIpcCallName.Set(KSqlSrvStmtBinParamSink);
       
   573             break;
       
   574         case ESqlSrvStmtTxtParamSink16:
       
   575             aIpcCallName.Set(KSqlSrvStmtTxtParamSink16);
       
   576             break;
       
   577         case ESqlSrvStmtBufFlat:
       
   578             aIpcCallName.Set(KSqlSrvStmtBufFlat);
       
   579             break;
       
   580         case ESqlSrvStmtColumnValue:
       
   581             aIpcCallName.Set(KSqlSrvStmtColumnValue);
       
   582             break;
       
   583         case ESqlSrvStmtDeclColumnTypes:
       
   584             aIpcCallName.Set(KSqlSrvStmtDeclColumnTypes);
       
   585             break;
       
   586         case ESqlSrvStreamRead:
       
   587             aIpcCallName.Set(KSqlSrvStreamRead);
       
   588             break;
       
   589         case ESqlSrvStreamWrite:
       
   590             aIpcCallName.Set(KSqlSrvStreamWrite);
       
   591             break;
       
   592         case ESqlSrvStreamSize:
       
   593             aIpcCallName.Set(KSqlSrvStreamSize);
       
   594             break;
       
   595         case ESqlSrvStreamSynch:
       
   596             aIpcCallName.Set(KSqlSrvStreamSynch);
       
   597             break;
       
   598         case ESqlSrvStreamClose:
       
   599             aIpcCallName.Set(KSqlSrvStreamClose);
       
   600             break;
       
   601         default:
       
   602             return KErrNotSupported;
       
   603         };
       
   604     __SQLASSERT((TUint)rc < KIpcTraceTypeCount || rc == KErrNotFound, ESqlPanicInternalError);
       
   605     return rc;
       
   606     }
       
   607 
       
   608 //Calculates and returns the time difference between aStartTicks and aEndTicks in microseconds.  
       
   609 static TInt SqlConvertTicks2Us(TUint32 aStartTicks, TUint32 aEndTicks)
       
   610     {
       
   611     static TInt freq = 0;
       
   612     if(freq == 0)
       
   613         {
       
   614         TInt err = HAL::Get(HAL::EFastCounterFrequency, freq);
       
   615         if(err != KErrNone)
       
   616             {
       
   617             SqlPanic((TSqlPanic)err);
       
   618             }
       
   619         }
       
   620     TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
       
   621     if(diffTicks < 0)
       
   622         {
       
   623         diffTicks = KMaxTUint32 + diffTicks + 1;
       
   624         }
       
   625     const TInt KMicroSecIn1Sec = 1000000;
       
   626     TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq;
       
   627     return us;
       
   628     }
       
   629 
       
   630 //Calculates the time since the SQL server boot in microseconds.
       
   631 static TInt64 SqlTimeFromStartUs()
       
   632     {
       
   633     TTime time;
       
   634     time.UniversalTime();
       
   635     TTimeIntervalMicroSeconds us = time.MicroSecondsFrom(TheSqlSrvStartTime);
       
   636     if(us.Int64() < 0)
       
   637         {
       
   638         TheSqlSrvStartTime = time;
       
   639         us = 0;
       
   640         }
       
   641     return us.Int64();
       
   642     }
       
   643 
       
   644 
       
   645 //Tracing data buffer
       
   646 const TInt KSqlSrvProfilePrnBufSize = 230;
       
   647 static TBuf<KSqlSrvProfilePrnBufSize> TheSqlSrvProfileTraceBuf;
       
   648 static TBuf8<KSqlSrvProfilePrnBufSize> TheSqlSrvProfileTraceBuf8;
       
   649 
       
   650 static RFs TheSqlSrvTraceFs;
       
   651 static RFile TheTheSqlSrvTraceFile;
       
   652 _LIT(KSqlSrvTraceFileName, "C:\\SQLTRACE");
       
   653 
       
   654 //Prints out a time stamp
       
   655 static void SqlSrvProfileTimePrintf()
       
   656     {
       
   657     static TInt64 prevTimeDiff = 0;;
       
   658     TInt64 timeDiff = SqlTimeFromStartUs();
       
   659     const TInt64 KTimeInterval = 1000000; 
       
   660     if((timeDiff - prevTimeDiff) >= KTimeInterval || timeDiff < prevTimeDiff)
       
   661         {
       
   662         prevTimeDiff = timeDiff;
       
   663         TTime time;
       
   664         time.UniversalTime();
       
   665         TDateTime dt = time.DateTime();
       
   666         TheSqlSrvProfileTraceBuf8.Format(_L8("%08X¬%012ld¬TME¬%02d:%02d:%02d.%06d¬Prep8=%08d¬Prep16=%08d¬Ex8=%08d¬Ex16=%08d"),
       
   667                 0, timeDiff, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond(),
       
   668                 TheSqlSrvProfilerPreparedCnt8, TheSqlSrvProfilerPreparedCnt16, 
       
   669                 TheSqlSrvProfilerExecutedCnt8, TheSqlSrvProfilerExecutedCnt16);
       
   670         if(TheSqlSrvProfilerTraceToFile)
       
   671             {
       
   672             TheSqlSrvProfileTraceBuf8.Append(_L8("\r\n"));
       
   673             (void)TheTheSqlSrvTraceFile.Write(TheSqlSrvProfileTraceBuf8);
       
   674             }
       
   675         else
       
   676             {
       
   677             TheSqlSrvProfileTraceBuf8.Append(_L8("\n"));
       
   678             RDebug::RawPrint(TheSqlSrvProfileTraceBuf8);
       
   679             }
       
   680         }
       
   681     }
       
   682 
       
   683 //Trace types
       
   684 enum TSqlSrvProfilerTraceType
       
   685     {
       
   686     ESqlSrvProfilerNonSqlTrace,
       
   687     ESqlSrvProfilerMiddleLineSqlTrace,
       
   688     ESqlSrvProfilerLastLineSqlTrace
       
   689     };
       
   690 
       
   691 //Prints out the data that is in TheSqlSrvProfileTraceBuf.
       
   692 // aType = ESqlSrvProfilerNonSqlTrace       - non-SQL trace
       
   693 // aType = ESqlSrvProfilerMiddleLineSqlTrace - not last line of an SQL trace
       
   694 // aType = ESqlSrvProfilerLastLineSqlTrace  - last line of an SQL trace
       
   695 static void SqlSrvProfilePrintf(TSqlSrvProfilerTraceType aType)
       
   696     {
       
   697     SqlSrvProfileTimePrintf();
       
   698     TheSqlSrvProfileTraceBuf8.Copy(TheSqlSrvProfileTraceBuf);
       
   699     if(TheSqlSrvProfilerTraceToFile)
       
   700         {
       
   701         if(aType == 0 || aType == 2)
       
   702             {
       
   703             TheSqlSrvProfileTraceBuf8.Append(_L8("\r\n"));
       
   704             }
       
   705         (void)TheTheSqlSrvTraceFile.Write(TheSqlSrvProfileTraceBuf8);
       
   706         }
       
   707     else
       
   708         {
       
   709         TheSqlSrvProfileTraceBuf8.Append(_L8("\n"));
       
   710         RDebug::RawPrint(TheSqlSrvProfileTraceBuf8);
       
   711         }
       
   712     }
       
   713 
       
   714 //Called at the beginning of CSqlSrvSession::ServiceL().
       
   715 void SqlIpcStart(TUint& aIpcCounter, TUint32& aStartTicks, TUint aDbHandle)
       
   716     {
       
   717     if(TheSqlSrvProfilerTraceEnabled)
       
   718         {
       
   719         if(TheSqlSrvProfilerTraceLevel == 0)
       
   720             {
       
   721             return;
       
   722             }
       
   723         if(KSqlSrvProfilerDbName().Length() > 0 && (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   724             {
       
   725             return;
       
   726             }
       
   727         ++aIpcCounter;
       
   728         TheSqlSrvProfilerFileRead1 = TheSqlSrvProfilerFileRead;
       
   729         TheSqlSrvProfilerFileWrite1 = TheSqlSrvProfilerFileWrite;
       
   730         TheSqlSrvProfilerFileSync1 = TheSqlSrvProfilerFileSync;
       
   731         TheSqlSrvProfilerFileSetSize1 = TheSqlSrvProfilerFileSetSize;
       
   732         aStartTicks = User::FastCounter();
       
   733         }
       
   734     }
       
   735 
       
   736 //Called at the end of CSqlSrvSession::ServiceL().
       
   737 void SqlIpcEnd(TUint aIpcCounter, TUint32 aStartTicks, TSqlSrvFunction aFuncCode, 
       
   738                TUint aDbHandle, TSqlSrvIpcTraceData aIpcTraceData[], TInt aRetCode)
       
   739     {
       
   740     if(TheSqlSrvProfilerTraceEnabled)
       
   741         {
       
   742         if(TheSqlSrvProfilerTraceLevel == 0)
       
   743             {
       
   744             return;
       
   745             }
       
   746         if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   747             {
       
   748             return;
       
   749             }
       
   750         TUint32 endTicks = User::FastCounter();
       
   751         TInt executionTime = SqlConvertTicks2Us(aStartTicks, endTicks);
       
   752         TPtrC ipcCallName;
       
   753         TInt ipcCallIdx = SqlIpcTraceIdxAndName(aFuncCode, ipcCallName); 
       
   754         TInt64 timeFromStart = SqlTimeFromStartUs();
       
   755         TInt64 ttlExecTime = 0;
       
   756         TInt count = 0;
       
   757         if(ipcCallIdx >= 0)
       
   758             {
       
   759             aIpcTraceData[ipcCallIdx].iExecutionTime += executionTime;
       
   760             ttlExecTime = aIpcTraceData[ipcCallIdx].iExecutionTime;
       
   761             count = ++aIpcTraceData[ipcCallIdx].iCount;
       
   762             }
       
   763         if(ipcCallIdx >= 0  || (ipcCallIdx == KErrNotFound && TheSqlSrvProfilerTraceLevel == 2))
       
   764             {
       
   765             TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬IPC¬%08u¬%20.20S¬%012ld¬%010d¬%08d¬%04d¬%04d¬%04d¬%04d¬rc=%d"),  
       
   766                     aDbHandle, 
       
   767                     timeFromStart,
       
   768                     aIpcCounter,
       
   769                     &ipcCallName, 
       
   770                     ttlExecTime, 
       
   771                     executionTime,
       
   772                     count,
       
   773                     TheSqlSrvProfilerFileRead - TheSqlSrvProfilerFileRead1,
       
   774                     TheSqlSrvProfilerFileWrite - TheSqlSrvProfilerFileWrite1,
       
   775                     TheSqlSrvProfilerFileSync - TheSqlSrvProfilerFileSync1,
       
   776                     TheSqlSrvProfilerFileSetSize - TheSqlSrvProfilerFileSetSize1,
       
   777                     aRetCode);
       
   778             SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   779             }
       
   780         }
       
   781     }
       
   782 
       
   783 //Called at the end of CSqlSrvSession::ServiceError().
       
   784 void SqlIpcError(TUint aIpcCounter, TSqlSrvFunction aFuncCode, TUint aDbHandle, TInt aError)
       
   785     {
       
   786     if(TheSqlSrvProfilerTraceEnabled)
       
   787         {
       
   788         if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   789             {
       
   790             return;
       
   791             }
       
   792         TPtrC ipcCallName;
       
   793         (void)SqlIpcTraceIdxAndName(aFuncCode, ipcCallName); 
       
   794         TInt64 timeFromStart = SqlTimeFromStartUs();
       
   795         TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬ERR¬%08u¬%20.20S¬err=%d"), 
       
   796                 aDbHandle, 
       
   797                 timeFromStart,
       
   798                 aIpcCounter,
       
   799                 &ipcCallName,
       
   800                 aError);
       
   801         SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   802         }
       
   803     }
       
   804 
       
   805 //Prints the passed as a parameter 16-bit SQL statement.
       
   806 void SqlPrintSql16(TUint aDbHandle, const TDesC& aSql, TBool aPrepare)
       
   807     {
       
   808     if(!TheSqlSrvProfilerTraceEnabled || !TheSqlSrvProfilerSqlTraceEnabled)
       
   809         {
       
   810         return;
       
   811         }
       
   812     if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   813         {
       
   814         return;
       
   815         }
       
   816     
       
   817     aPrepare ? ++TheSqlSrvProfilerPreparedCnt16 : ++TheSqlSrvProfilerExecutedCnt16;
       
   818     
       
   819     TInt64 timeFromStart = SqlTimeFromStartUs();
       
   820     TPtr line((TUint16*)TheSqlTraceBuf.Ptr(), 0, TheSqlTraceBuf.MaxLength());
       
   821     TInt len = aSql.Length();
       
   822     TInt pos = 0;
       
   823     do
       
   824         {
       
   825         _LIT(KPrepare,  "Prepare16");
       
   826         _LIT(KExec,     "Exec16");
       
   827         _LIT(KEmptyStr,  "");
       
   828         if(pos == 0)
       
   829             {
       
   830             line.Format(_L("%08X¬%012ld¬SQL¬%12.12S¬"), aDbHandle, timeFromStart, aPrepare ? &KPrepare : &KExec);
       
   831             }
       
   832         else
       
   833             {
       
   834             if(!TheSqlSrvProfilerTraceToFile)
       
   835                 {
       
   836                 line.Format(_L("%08X¬%012ld¬SQL¬%12.12S¬"), aDbHandle, timeFromStart, &KEmptyStr);
       
   837                 }
       
   838             }
       
   839         TInt l = Min(len, (line.MaxLength() - line.Length()));
       
   840         TPtrC ptr(aSql.Ptr() + pos, l);
       
   841         pos += l;
       
   842         len -= l;
       
   843         line.Append(ptr);
       
   844         TheSqlSrvProfileTraceBuf.Format(_L("%S"), &line); 
       
   845         SqlSrvProfilePrintf(len > 0 ? ESqlSrvProfilerMiddleLineSqlTrace : ESqlSrvProfilerLastLineSqlTrace);
       
   846         line.Zero();
       
   847         } while(len > 0);
       
   848     }
       
   849  
       
   850 //Prints the passed as a parameter 8-bit SQL statement.
       
   851 void SqlPrintSql8(TUint aDbHandle, const TDesC8& aSql, TBool aPrepare)
       
   852     {
       
   853     if(!TheSqlSrvProfilerTraceEnabled || !TheSqlSrvProfilerSqlTraceEnabled)
       
   854         {
       
   855         return;
       
   856         }
       
   857     if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   858         {
       
   859         return;
       
   860         }
       
   861     
       
   862     aPrepare ? ++TheSqlSrvProfilerPreparedCnt8 : ++TheSqlSrvProfilerExecutedCnt8;
       
   863     
       
   864     TInt64 timeFromStart = SqlTimeFromStartUs();
       
   865     TPtr line((TUint16*)TheSqlTraceBuf.Ptr(), 0, TheSqlTraceBuf.MaxLength());
       
   866     TInt len = aSql.Length();
       
   867     TInt pos = 0;
       
   868     do
       
   869         {
       
   870         _LIT(KPrepare,   "Prepare8");
       
   871         _LIT(KExec,      "Exec8");
       
   872         _LIT(KEmptyStr,  "");
       
   873         if(pos == 0)
       
   874             {
       
   875             line.Format(_L("%08X¬%012ld¬SQL¬%12.12S¬"), aDbHandle, timeFromStart, aPrepare ? &KPrepare : &KExec);
       
   876             }
       
   877         else
       
   878             {
       
   879             if(!TheSqlSrvProfilerTraceToFile)
       
   880                 {
       
   881                 line.Format(_L("%08X¬%012ld¬SQL¬%12.12S¬"), aDbHandle, timeFromStart, &KEmptyStr);
       
   882                 }
       
   883             }
       
   884         TInt l = Min(len, (line.MaxLength() - line.Length()));
       
   885         TPtrC8 ptr(aSql.Ptr() + pos, l);
       
   886         pos += l;
       
   887         len -= l;
       
   888         TPtr p2((TUint16*)line.Ptr() + line.Length(), 0, l);  
       
   889         p2.Copy(ptr);
       
   890         line.SetLength(line.Length() + p2.Length());
       
   891         TheSqlSrvProfileTraceBuf.Format(_L("%S"), &line); 
       
   892         SqlSrvProfilePrintf(len > 0 ? ESqlSrvProfilerMiddleLineSqlTrace : ESqlSrvProfilerLastLineSqlTrace);
       
   893         line.Zero();
       
   894         } while(len > 0);
       
   895     }
       
   896 
       
   897 //Prints the name of the just created database.
       
   898 void SqlPrintDbCreate(TUint aDbHandle, const TDesC& aDbName)
       
   899     {
       
   900     if(TheSqlSrvProfilerTraceEnabled)
       
   901         {
       
   902         if(KSqlSrvProfilerDbName().Length() > 0 && aDbName.FindF(KSqlSrvProfilerDbName) >= 0)
       
   903             {
       
   904             TheSqlSrvProfilerHandle = aDbHandle;
       
   905             }
       
   906         if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   907             {
       
   908             return;
       
   909             }
       
   910         TInt64 timeFromStart = SqlTimeFromStartUs();
       
   911         TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬CRE¬%S"), 
       
   912                 aDbHandle, 
       
   913                 timeFromStart,
       
   914                 &aDbName);
       
   915         SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   916         }
       
   917     }
       
   918 
       
   919 //Prints the name of the just opened database.
       
   920 void SqlPrintDbOpen(TUint aDbHandle, const TDesC& aDbName)
       
   921     {
       
   922     if(TheSqlSrvProfilerTraceEnabled)
       
   923         {
       
   924         if(KSqlSrvProfilerDbName().Length() > 0 && aDbName.FindF(KSqlSrvProfilerDbName) >= 0)
       
   925             {
       
   926             TheSqlSrvProfilerHandle = aDbHandle;
       
   927             }
       
   928         if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   929             {
       
   930             return;
       
   931             }
       
   932         TInt64 timeFromStart = SqlTimeFromStartUs();
       
   933         TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬OPN¬%S"), 
       
   934                 aDbHandle, 
       
   935                 timeFromStart,
       
   936                 &aDbName);
       
   937         SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   938         }
       
   939     }
       
   940 
       
   941 //Prints the handle of the just closed database.
       
   942 void SqlPrintDbClose(TUint aDbHandle)
       
   943     {
       
   944     if(TheSqlSrvProfilerTraceEnabled)
       
   945         {
       
   946         if(KSqlSrvProfilerDbName().Length() > 0 &&  (aDbHandle == 0 || TheSqlSrvProfilerHandle != aDbHandle))
       
   947             {
       
   948             return;
       
   949             }
       
   950         TInt64 timeFromStart = SqlTimeFromStartUs();
       
   951         TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬CSE"),  
       
   952                 aDbHandle, 
       
   953                 timeFromStart);
       
   954         SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   955         if(aDbHandle == TheSqlSrvProfilerHandle)
       
   956             {
       
   957             TheSqlSrvProfilerHandle = 0;
       
   958             }
       
   959         }
       
   960     }
       
   961 
       
   962 //Prints a trace when the SQL server starts
       
   963 void SqlPrintServerStart()
       
   964     {
       
   965     TInt64 timeFromStart = SqlTimeFromStartUs();
       
   966     if(TheSqlSrvProfilerTraceToFile)
       
   967         {
       
   968         TInt err = TheSqlSrvTraceFs.Connect();
       
   969         if(err == KErrNone)
       
   970             {
       
   971             TInt fileNum = 0;
       
   972             err = KErrGeneral;
       
   973             while(++fileNum < 1000 && err != KErrNone)
       
   974                 {
       
   975                 TBuf<80> ftrname;
       
   976                 ftrname.Copy(KSqlSrvTraceFileName);
       
   977                 ftrname.AppendNum(fileNum);
       
   978                 ftrname.Append(_L(".txt"));
       
   979                 err = TheTheSqlSrvTraceFile.Create(TheSqlSrvTraceFs, ftrname, EFileRead | EFileWrite);
       
   980                 if(err == KErrNone)
       
   981                     {
       
   982                     break;
       
   983                     }
       
   984                 }
       
   985             }
       
   986         if(err != KErrNone)
       
   987             {
       
   988             TheSqlSrvTraceFs.Close();
       
   989             TheSqlSrvProfilerTraceToFile = EFalse;
       
   990             RDebug::Print(_L("SQL trace file creation failed with err=%d"), err);
       
   991             }
       
   992         }
       
   993     TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬SRV¬START"), 0, timeFromStart); 
       
   994     SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
   995     }
       
   996 
       
   997 //Prints a trace when the SQL server stops
       
   998 void SqlPrintServerStop()
       
   999     {
       
  1000     TInt64 timeFromStart = SqlTimeFromStartUs();
       
  1001     TheSqlSrvProfileTraceBuf.Format(_L("%08X¬%012ld¬SRV¬STOP"), 0, timeFromStart); 
       
  1002     SqlSrvProfilePrintf(ESqlSrvProfilerNonSqlTrace);
       
  1003     if(TheSqlSrvProfilerTraceToFile)
       
  1004         {
       
  1005         TheTheSqlSrvTraceFile.Close();
       
  1006         TheSqlSrvTraceFs.Close();
       
  1007         }
       
  1008     }
       
  1009 
       
  1010 #else //_SQLPROFILER
       
  1011 
       
  1012 void TSqlSrvResourceProfiler::StartL(const RMessage2&)
       
  1013 	{
       
  1014 	__SQLLEAVE(KErrNotSupported);
       
  1015 	}
       
  1016 	
       
  1017 void TSqlSrvResourceProfiler::StopL(const RMessage2&)
       
  1018 	{
       
  1019 	__SQLLEAVE(KErrNotSupported);
       
  1020 	}
       
  1021 	
       
  1022 void TSqlSrvResourceProfiler::ResetL(const RMessage2&)
       
  1023 	{
       
  1024 	__SQLLEAVE(KErrNotSupported);
       
  1025 	}
       
  1026 	
       
  1027 void TSqlSrvResourceProfiler::QueryL(const RMessage2&)
       
  1028 	{
       
  1029 	__SQLLEAVE(KErrNotSupported);
       
  1030 	}
       
  1031 	
       
  1032 #endif//_SQLPROFILER