SQLite

Check-in [92b9fead35]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:A new version of the slow mutex log that uses gettimeofday() instead of trying to access the hardware timer.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | debug
Files: files | file ages | folders
SHA1: 92b9fead353d50eb41f5ab7e3a887dd4e5c3aa80
User & Date: drh 2016-07-30 03:33:30.306
Context
2016-08-04
14:08
Add extra logging calls to this branch. (check-in: 491f1ef36e user: dan tags: debug)
2016-07-30
03:33
A new version of the slow mutex log that uses gettimeofday() instead of trying to access the hardware timer. (check-in: 92b9fead35 user: drh tags: debug)
2016-07-29
16:32
Turn memory status off by default. (check-in: ea3c7162dc user: drh tags: debug)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/mutex_unix.c.
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

222
223
224


225
226
227
228
229
230
231
static void pthreadMutexFree(sqlite3_mutex *p){
  assert( p->nRef==0 );
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  pthread_mutex_destroy(&p->mutex);
  sqlite3_free(p);
}

#include "hwtime.h"
#ifdef SQLITE_MUTEX_NREF
# define MUTEX_ID(p)  (p->id)
#else
# define MUTEX_ID(p)  0
#endif

/*
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
** to enter a mutex.  If another thread is already within the mutex,
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
** be entered multiple times by the same thread.  In such cases the,
** mutex must be exited an equal number of times before another thread
** can enter.  If the same thread tries to enter any other kind of mutex
** more than once, the behavior is undefined.
*/
static void pthreadMutexEnter(sqlite3_mutex *p){

  sqlite3_uint64 iTimer = sqlite3Hwtime();
  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );



#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  /* If recursive mutexes are not available, then we have to grow
  ** our own.  This implementation assumes that pthread_equal()
  ** is atomic - that it cannot be deceived into thinking self
  ** and p->owner are equal if p->owner changes between two values
  ** that are not equal to self while the comparison is taking place.
  ** This implementation also assumes a coherent cache - that 







|


















>
|


>
>







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
static void pthreadMutexFree(sqlite3_mutex *p){
  assert( p->nRef==0 );
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  pthread_mutex_destroy(&p->mutex);
  sqlite3_free(p);
}

#include <sys/time.h>
#ifdef SQLITE_MUTEX_NREF
# define MUTEX_ID(p)  (p->id)
#else
# define MUTEX_ID(p)  0
#endif

/*
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
** to enter a mutex.  If another thread is already within the mutex,
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
** be entered multiple times by the same thread.  In such cases the,
** mutex must be exited an equal number of times before another thread
** can enter.  If the same thread tries to enter any other kind of mutex
** more than once, the behavior is undefined.
*/
static void pthreadMutexEnter(sqlite3_mutex *p){
  struct timeval x;
  sqlite3_uint64 iBegin, iEnd;
  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );

  gettimeofday(&x, 0);
  iBegin = 1000000*(sqlite3_uint64)x.tv_sec + x.tv_usec;
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  /* If recursive mutexes are not available, then we have to grow
  ** our own.  This implementation assumes that pthread_equal()
  ** is atomic - that it cannot be deceived into thinking self
  ** and p->owner are equal if p->owner changes between two values
  ** that are not equal to self while the comparison is taking place.
  ** This implementation also assumes a coherent cache - that 
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  */
  pthread_mutex_lock(&p->mutex);
#if SQLITE_MUTEX_NREF
  assert( p->nRef>0 || p->owner==0 );
  p->owner = pthread_self();
  p->nRef++;
#endif

  iTimer = sqlite3Hwtime() - iTimer;
  if( iTimer>100000 ){
    sqlite3_mutex *pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
    int id = -1;
    if( p>=pMaster && p<=&pMaster[SQLITE_MUTEX_STATIC_APP3-2] ){
      id = (int)(p - pMaster) + 2;
    }
    sqlite3_log(SQLITE_NOTICE, "slow mutex: %lld cycles on %d/%p",
                iTimer, id, p);
  }
#endif

#ifdef SQLITE_DEBUG
  if( p->trace ){
    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  }







>
|
|





|
|







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  */
  pthread_mutex_lock(&p->mutex);
#if SQLITE_MUTEX_NREF
  assert( p->nRef>0 || p->owner==0 );
  p->owner = pthread_self();
  p->nRef++;
#endif
  gettimeofday(&x, 0);
  iEnd = 1000000*(sqlite3_uint64)x.tv_sec + x.tv_usec;
  if( iEnd > iBegin+500 ){
    sqlite3_mutex *pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
    int id = -1;
    if( p>=pMaster && p<=&pMaster[SQLITE_MUTEX_STATIC_APP3-2] ){
      id = (int)(p - pMaster) + 2;
    }
    sqlite3_log(SQLITE_NOTICE, "slow mutex: %llu uS on %d/%p",
                iEnd - iBegin, id, p);
  }
#endif

#ifdef SQLITE_DEBUG
  if( p->trace ){
    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  }