xref: /sqlite-3.40.0/src/os_common.h (revision 8a29dfde)
1 /*
2 ** 2004 May 22
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains macros and a little bit of code that is common to
14 ** all of the platform-specific files (os_*.c) and is #included into those
15 ** files.
16 **
17 ** This file should be #included by the os_*.c files only.  It is not a
18 ** general purpose header file.
19 */
20 
21 /*
22 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24 ** switch.  The following code should catch this problem at compile-time.
25 */
26 #ifdef MEMORY_DEBUG
27 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
28 #endif
29 
30 
31 /*
32  * When testing, this global variable stores the location of the
33  * pending-byte in the database file.
34  */
35 #ifdef SQLITE_TEST
36 unsigned int sqlite3_pending_byte = 0x40000000;
37 #endif
38 
39 #ifdef SQLITE_DEBUG
40 int sqlite3OSTrace = 0;
41 #define OSTRACE1(X)         if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
42 #define OSTRACE2(X,Y)       if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
43 #define OSTRACE3(X,Y,Z)     if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
44 #define OSTRACE4(X,Y,Z,A)   if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
45 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
46 #define OSTRACE6(X,Y,Z,A,B,C) \
47     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
48 #define OSTRACE7(X,Y,Z,A,B,C,D) \
49     if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
50 #else
51 #define OSTRACE1(X)
52 #define OSTRACE2(X,Y)
53 #define OSTRACE3(X,Y,Z)
54 #define OSTRACE4(X,Y,Z,A)
55 #define OSTRACE5(X,Y,Z,A,B)
56 #define OSTRACE6(X,Y,Z,A,B,C)
57 #define OSTRACE7(X,Y,Z,A,B,C,D)
58 #endif
59 
60 /*
61 ** Macros for performance tracing.  Normally turned off.  Only works
62 ** on i486 hardware.
63 */
64 #ifdef SQLITE_PERFORMANCE_TRACE
65 __inline__ unsigned long long int hwtime(void){
66   unsigned long long int x;
67   __asm__("rdtsc\n\t"
68           "mov %%edx, %%ecx\n\t"
69           :"=A" (x));
70   return x;
71 }
72 static unsigned long long int g_start;
73 static unsigned int elapse;
74 #define TIMER_START       g_start=hwtime()
75 #define TIMER_END         elapse=hwtime()-g_start
76 #define TIMER_ELAPSED     elapse
77 #else
78 #define TIMER_START
79 #define TIMER_END
80 #define TIMER_ELAPSED     0
81 #endif
82 
83 /*
84 ** If we compile with the SQLITE_TEST macro set, then the following block
85 ** of code will give us the ability to simulate a disk I/O error.  This
86 ** is used for testing the I/O recovery logic.
87 */
88 #ifdef SQLITE_TEST
89 int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
90 int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
91 int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
92 int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
93 int sqlite3_io_error_benign = 0;         /* True if errors are benign */
94 int sqlite3_diskfull_pending = 0;
95 int sqlite3_diskfull = 0;
96 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
97 #define SimulateIOError(CODE)  \
98   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
99        || sqlite3_io_error_pending-- == 1 )  \
100               { local_ioerr(); CODE; }
101 static void local_ioerr(){
102   IOTRACE(("IOERR\n"));
103   sqlite3_io_error_hit++;
104   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
105 }
106 #define SimulateDiskfullError(CODE) \
107    if( sqlite3_diskfull_pending ){ \
108      if( sqlite3_diskfull_pending == 1 ){ \
109        local_ioerr(); \
110        sqlite3_diskfull = 1; \
111        sqlite3_io_error_hit = 1; \
112        CODE; \
113      }else{ \
114        sqlite3_diskfull_pending--; \
115      } \
116    }
117 #else
118 #define SimulateIOErrorBenign(X)
119 #define SimulateIOError(A)
120 #define SimulateDiskfullError(A)
121 #endif
122 
123 /*
124 ** When testing, keep a count of the number of open files.
125 */
126 #ifdef SQLITE_TEST
127 int sqlite3_open_file_count = 0;
128 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
129 #else
130 #define OpenCounter(X)
131 #endif
132