From: shane Date: Wed, 21 Oct 2009 03:42:58 +0000 (+0000) Subject: For Windows version of shell, add support for .timer command X-Git-Tag: fts3-refactor~75 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b320ccd782ff82369520d889ff3137019510698b;p=thirdparty%2Fsqlite.git For Windows version of shell, add support for .timer command using the GetProcessTimes() API if available (in the same way getrusage() is used on UNIX.) Ticket 89668ca167. FossilOrigin-Name: 83216fbe904425f5b15d1ae689cea9d13670e979 --- diff --git a/manifest b/manifest index ce09ea343f..d244da5070 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C For\sWindows\sVFS,\smodified\sxGetLastError()\sto\scall\sFormatMessage()\s\nwith\sthe\sFORMAT_MESSAGE_IGNORE_INSERTS\soption.\s\sAdditionally\supdated\nto\sensure\sstrings\sare\sreturned\sas\sUTF8.\s\sTicket\s39c85e8a4e. -D 2009-10-21T02:00:48 +C For\sWindows\sversion\sof\sshell,\sadd\ssupport\sfor\s.timer\scommand\nusing\sthe\sGetProcessTimes()\sAPI\sif\savailable\s(in\sthe\ssame\sway\ngetrusage()\sis\sused\son\sUNIX.)\sTicket\s89668ca167. +D 2009-10-21T03:42:58 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4ca3f1dd6efa2075bcb27f4dc43eef749877740d F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -161,7 +161,7 @@ F src/random.c 676b9d7ac820fe81e6fb2394ac8c10cff7f38628 F src/resolve.c 3ac31c7181fab03732125fdedf7c2091a5c07f1b F src/rowset.c c64dafba1f9fd876836c8db8682966b9d197eb1f F src/select.c cbe366a0ce114856e66f5daf0f848d7c48a88298 -F src/shell.c 270231b3f587f1f86391b9994fdfcd5d472c3fdf +F src/shell.c 40364daed1653851fa727baa38c4afdbc8d17801 F src/sqlite.h.in 2643b0946d2564a05088e656e0740863d55a02ea F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17 F src/sqliteInt.h 3b00a3ce79e60c5a47c342b738c8b75013f3ec84 @@ -761,7 +761,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P b2aa48b52f140ad722001de686c5b2f802e9babd -R 88fe3851909db662467ab0ab67772332 +P 761396f8cb79be34853ba698a65af54874c3c10e +R 58a677fbffcf3d33e3376183088659ba U shane -Z 511d63a6d62dacb90291925f2c8c96c3 +Z 2462a974d1dd735a6b5e18b16e693806 diff --git a/manifest.uuid b/manifest.uuid index e9a11cdf0b..277a595e6b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -761396f8cb79be34853ba698a65af54874c3c10e \ No newline at end of file +83216fbe904425f5b15d1ae689cea9d13670e979 \ No newline at end of file diff --git a/src/shell.c b/src/shell.c index 2186f4f621..93cd8a7c25 100644 --- a/src/shell.c +++ b/src/shell.c @@ -106,9 +106,86 @@ static void endTimer(void){ timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); } } + #define BEGIN_TIMER beginTimer() #define END_TIMER endTimer() #define HAS_TIMER 1 + +#elif (defined(_WIN32) || defined(WIN32)) + +#include + +/* Saved resource information for the beginning of an operation */ +static HANDLE hProcess; +static FILETIME ftKernelBegin; +static FILETIME ftUserBegin; +typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME); +static GETPROCTIMES getProcessTimesAddr = NULL; + +/* True if the timer is enabled */ +static int enableTimer = 0; + +/* +** Check to see if we have timer support. Return 1 if necessary +** support found (or found previously). +*/ +static int hasTimer(void){ + if( getProcessTimesAddr ){ + return 1; + } else { + /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions. + ** See if the version we are running on has it, and if it does, save off + ** a pointer to it and the current process handle. + */ + hProcess = GetCurrentProcess(); + if( hProcess ){ + HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); + if( NULL != hinstLib ){ + getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); + if( NULL != getProcessTimesAddr ){ + return 1; + } + FreeLibrary(hinstLib); + } + } + } + return 0; +} + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer && getProcessTimesAddr ){ + FILETIME ftCreation, ftExit; + getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin); + } +} + +/* Return the difference of two FILETIME structs in seconds */ +static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ + sqlite_int64 i64Start = *((sqlite_int64 *) pStart); + sqlite_int64 i64End = *((sqlite_int64 *) pEnd); + return (double) ((i64End - i64Start) / 10000000.0); +} + +/* +** Print the timing results. +*/ +static void endTimer(void){ + if( enableTimer && getProcessTimesAddr){ + FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; + getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd); + printf("CPU Time: user %f sys %f\n", + timeDiff(&ftUserBegin, &ftUserEnd), + timeDiff(&ftKernelBegin, &ftKernelEnd)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER endTimer() +#define HAS_TIMER hasTimer() + #else #define BEGIN_TIMER #define END_TIMER @@ -1906,12 +1983,13 @@ static char zHelp[] = ".show Show the current values for various settings\n" ".tables ?PATTERN? List names of tables matching a LIKE pattern\n" ".timeout MS Try opening locked tables for MS milliseconds\n" -#if HAS_TIMER - ".timer ON|OFF Turn the CPU timer measurement on or off\n" -#endif ".width NUM NUM ... Set column widths for \"column\" mode\n" ; +static char zTimerHelp[] = + ".timer ON|OFF Turn the CPU timer measurement on or off\n" +; + /* Forward reference */ static int process_input(struct callback_data *p, FILE *in); @@ -2204,6 +2282,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){ if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ fprintf(stderr,"%s",zHelp); + if( HAS_TIMER ){ + fprintf(stderr,"%s",zTimerHelp); + } }else if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){ @@ -2655,15 +2736,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){ if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ open_db(p); sqlite3_busy_timeout(p->db, atoi(azArg[1])); - }else - -#if HAS_TIMER - if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){ + }else if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){ enableTimer = booleanValue(azArg[1]); - }else -#endif - - if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ + }else if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ int j; assert( nArg<=ArraySize(azArg) ); for(j=1; jcolWidth); j++){