Merge pull request #479 from chme/dbperf

[db_profile] Use sqlite3_trace_v2 instead of deprecated sqlite3_profile
This commit is contained in:
ejurgensen 2018-01-13 13:16:25 +01:00 committed by GitHub
commit b1c2a3bd8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 15 deletions

View File

@ -355,6 +355,9 @@ FORK_ARG_ENABLE([Chromecast support], [chromecast], [CHROMECAST],
AM_CONDITIONAL([COND_CHROMECAST], [[test "x$enable_chromecast" = "xyes"]])
AM_CONDITIONAL([COND_PROTOBUF_OLD], [[test "x$protobuf_old" = "xyes"]])
dnl DB profiling support
FORK_ARG_ENABLE([DB profiling support], [dbprofile], [DB_PROFILE])
dnl iTunes playlists with libplist
FORK_ARG_DISABLE([iTunes Music Library XML support], [itunes], [ITUNES],
[AS_IF([[test "x$with_libplist" = "xno"]],

View File

@ -5810,28 +5810,51 @@ db_watch_enum_fetchwd(struct watch_enum *we, uint32_t *wd)
#ifdef DB_PROFILE
static void
db_xprofile(void *notused, const char *pquery, sqlite3_uint64 ptime)
static int
db_xprofile(unsigned int trace_type, void *notused, void *ptr, void *ptr_data)
{
sqlite3_stmt *pstmt;
int64_t ms = 0;
sqlite3_stmt *stmt;
const char *pquery;
char *query;
int log_level;
int ret;
DPRINTF(E_DBG, L_DBPERF, "SQL PROFILE query: %s\n", pquery);
DPRINTF(E_DBG, L_DBPERF, "SQL PROFILE time: %" PRIu64 " ms\n", ((uint64_t)ptime / 1000000));
if (trace_type != SQLITE_TRACE_PROFILE)
return 0;
pstmt = ptr;
pquery = sqlite3_sql(pstmt);
ms = *((int64_t *) ptr_data) / 1000000;
if (ms > 1000)
log_level = E_LOG;
else if (ms > 500)
log_level = E_WARN;
else if (ms > 10)
log_level = E_DBG;
else
log_level = E_SPAM;
if (log_level > logger_severity())
return 0;
DPRINTF(log_level, L_DBPERF, "SQL PROFILE query: %s\n", pquery);
DPRINTF(log_level, L_DBPERF, "SQL PROFILE time: %" PRIi64 " ms\n", ms);
if ((strncmp(pquery, "SELECT", 6) != 0)
&& (strncmp(pquery, "UPDATE", 6) != 0)
&& (strncmp(pquery, "DELETE", 6) != 0))
return;
return 0;
/* Disable profiling callback */
sqlite3_profile(hdl, NULL, NULL);
sqlite3_trace_v2(hdl, 0, NULL, NULL);
query = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", pquery);
if (!query)
{
DPRINTF(E_DBG, L_DBPERF, "Query plan: Out of memory\n");
DPRINTF(log_level, L_DBPERF, "Query plan: Out of memory\n");
goto out;
}
@ -5840,30 +5863,30 @@ db_xprofile(void *notused, const char *pquery, sqlite3_uint64 ptime)
sqlite3_free(query);
if (ret != SQLITE_OK)
{
DPRINTF(E_DBG, L_DBPERF, "Query plan: Could not prepare statement: %s\n", sqlite3_errmsg(hdl));
DPRINTF(log_level, L_DBPERF, "Query plan: Could not prepare statement: %s\n", sqlite3_errmsg(hdl));
goto out;
}
DPRINTF(E_DBG, L_DBPERF, "Query plan:\n");
DPRINTF(log_level, L_DBPERF, "Query plan:\n");
while ((ret = db_blocking_step(stmt)) == SQLITE_ROW)
{
DPRINTF(E_DBG, L_DBPERF, "(%d,%d,%d) %s\n",
DPRINTF(log_level, L_DBPERF, "(%d,%d,%d) %s\n",
sqlite3_column_int(stmt, 0), sqlite3_column_int(stmt, 1), sqlite3_column_int(stmt, 2),
sqlite3_column_text(stmt, 3));
}
if (ret != SQLITE_DONE)
DPRINTF(E_DBG, L_DBPERF, "Query plan: Could not step: %s\n", sqlite3_errmsg(hdl));
DPRINTF(E_DBG, L_DBPERF, "---\n");
DPRINTF(log_level, L_DBPERF, "Query plan: Could not step: %s\n", sqlite3_errmsg(hdl));
sqlite3_finalize(stmt);
out:
/* Reenable profiling callback */
sqlite3_profile(hdl, db_xprofile, NULL);
sqlite3_trace_v2(hdl, SQLITE_TRACE_PROFILE, db_xprofile, NULL);
return 0;
}
#endif
@ -6157,7 +6180,7 @@ db_perthread_init(void)
}
#ifdef DB_PROFILE
sqlite3_profile(hdl, db_xprofile, NULL);
sqlite3_trace_v2(hdl, SQLITE_TRACE_PROFILE, db_xprofile, NULL);
#endif
cache_size = cfg_getint(cfg_getsec(cfg, "sqlite"), "pragma_cache_size_library");

View File

@ -260,6 +260,12 @@ logger_reinit(void)
}
int
logger_severity(void)
{
return threshold;
}
/* The functions below are used at init time with a single thread running */
void
logger_domains(void)

View File

@ -48,6 +48,7 @@
#define E_SPAM 5
void
DPRINTF(int severity, int domain, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
@ -65,6 +66,9 @@ logger_alsa(const char *file, int line, const char *function, int err, const cha
void
logger_reinit(void);
int
logger_severity(void);
void
logger_domains(void);