Change in ...osmo-hlr[master]: src/db.c: integrate SQLite3 with talloc allocator

fixeria gerrit-no-reply at lists.osmocom.org
Thu Jul 25 16:30:27 UTC 2019


fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-hlr/+/14939


Change subject: src/db.c: integrate SQLite3 with talloc allocator
......................................................................

src/db.c: integrate SQLite3 with talloc allocator

This change introduces an optional feature that allows to make
SQLite3 use talloc for all internal allocations. This would
facilitate finding memleaks. OsmoHLR needs to be configured
with '--enable-sqlite-talloc'.

  full talloc report on 'OsmoHLR' (total 292168 bytes in 449 blocks)
    struct osmo_gsup_server        contains    162 bytes in   3 blocks (ref 0)
      ...
    struct db_context              contains 288407 bytes in 420 blocks (ref 0)
      hlr.db                       contains      7 bytes in   1 blocks (ref 0)
      SQLite3                      contains 288192 bytes in 418 blocks (ref 0)
        db.c:95                    contains     48 bytes in   1 blocks (ref 0)
	db.c:95                    contains      2 bytes in   1 blocks (ref 0)
	...

Unfortunately, old SQLite3 versions (such as 3.8.2) somehow become
unstable and fail to initialize the database with talloc. There is
a huge difference in heap usage footprint compared to malloc. At the
same time, the recent versions (at least 3.24.0), work just fine.

Change-Id: Icfe67ed0f063b63e6794f9516da3003d01cf20a7
---
M configure.ac
M src/db.c
2 files changed, 85 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/39/14939/1

diff --git a/configure.ac b/configure.ac
index 6694f80..c59f40f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -59,6 +59,20 @@
 	CPPFLAGS="$CPPFLAGS -fsanitize=address -fsanitize=undefined"
 fi
 
+AC_ARG_ENABLE([sqlite_talloc],
+		AC_HELP_STRING([--enable-sqlite-talloc],
+				[Configure SQLite3 to use talloc memory allocator [default=no]]),
+		[sqlite_talloc="$enableval"],[sqlite_talloc="no"])
+if test "x$sqlite_talloc" = "xyes" ; then
+	# Older versions of SQLite3 (at least 3.8.2) become unstable with talloc.
+	# Feel free to relax to 3.24.0 > VER > 3.8.2 if it works for you.
+	# FIXME: PKG_CHECK_MODULES() may return cached result here!
+	PKG_CHECK_MODULES(SQLITE3, sqlite3 >= 3.24.0)
+	AC_DEFINE([SQLITE_USE_TALLOC], 1, [Use talloc for SQLite3])
+fi
+AC_MSG_CHECKING([whether to use talloc for SQLite3])
+AC_MSG_RESULT([$sqlite_talloc])
+
 AC_ARG_ENABLE(werror,
 	[AS_HELP_STRING(
 		[--enable-werror],
diff --git a/src/db.c b/src/db.c
index 7de61a2..f0bac2f 100644
--- a/src/db.c
+++ b/src/db.c
@@ -18,6 +18,7 @@
  */
 
 #include <osmocom/core/utils.h>
+#include <osmocom/core/talloc.h>
 
 #include <stdbool.h>
 #include <sqlite3.h>
@@ -83,6 +84,63 @@
 	[DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn",
 };
 
+/* Optional feature to make SQLite using talloc */
+#ifdef SQLITE_USE_TALLOC
+
+/* Dedicated talloc context for SQLite */
+static void *db_sqlite_ctx = NULL;
+
+static void *tall_xMalloc(int size)
+{
+	return talloc_size(db_sqlite_ctx, size);
+}
+
+static void tall_xFree(void *ptr)
+{
+	talloc_free(ptr);
+}
+
+static void *tall_xRealloc(void *ptr, int size)
+{
+	return talloc_realloc_fn(db_sqlite_ctx, ptr, size);
+}
+
+static int tall_xSize(void *ptr)
+{
+	return talloc_total_size(ptr);
+}
+
+/* DUMMY: talloc doesn't round up the allocation size */
+static int tall_xRoundup(int size) { return size; }
+
+/* DUMMY: nothing to initialize */
+static int tall_xInit(void *data) { return 0; }
+
+/* DUMMY: nothing to deinitialize */
+static void tall_xShutdown(void *data) {  }
+
+/* Interface between SQLite and talloc memory allocator */
+static const struct sqlite3_mem_methods tall_sqlite_if = {
+	/* Memory allocation function */
+	.xMalloc = &tall_xMalloc,
+	/* Free a prior allocation */
+	.xFree = &tall_xFree,
+	/* Resize an allocation */
+	.xRealloc = &tall_xRealloc,
+	/* Return the size of an allocation */
+	.xSize = &tall_xSize,
+	/* Round up request size to allocation size */
+	.xRoundup = &tall_xRoundup,
+	/* Initialize the memory allocator */
+	.xInit = &tall_xInit,
+	/* Deinitialize the memory allocator */
+	.xShutdown = &tall_xShutdown,
+	/* Argument to xInit() and xShutdown() */
+	.pAppData = NULL,
+};
+
+#endif /* SQLITE_USE_TALLOC */
+
 static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
 {
 	LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
@@ -365,6 +423,19 @@
 	LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
 	LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
 
+#ifdef SQLITE_USE_TALLOC
+	db_sqlite_ctx = talloc_named_const(dbc, 0, "SQLite3");
+
+	/* Configure SQLite3 to use talloc memory allocator */
+	rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &tall_sqlite_if);
+	if (rc == SQLITE_OK) {
+		LOGP(DDB, LOGL_NOTICE, "SQLite3 is configured to use talloc\n");
+	} else {
+		LOGP(DDB, LOGL_NOTICE, "Failed to configure SQLite3 "
+		     "to use talloc, using default memory allocator\n");
+	}
+#endif
+
 	dbc->fname = talloc_strdup(dbc, fname);
 
 	for (i = 0; i < 0xfffff; i++) {

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-hlr/+/14939
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Change-Id: Icfe67ed0f063b63e6794f9516da3003d01cf20a7
Gerrit-Change-Number: 14939
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <axilirator at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190725/ad467bd7/attachment.html>


More information about the gerrit-log mailing list