From 57239a1adddd8cba2f1af1d7ff29a447f621632b Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Mon, 18 Feb 2008 14:34:12 +0200 Subject: [PATCH] Keep some simple statistics on how the place cache is working. Check the sql statements before finalizing. --- src/osm-db.c | 69 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/src/osm-db.c b/src/osm-db.c index 9e31ac1..98e41a1 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -71,7 +71,12 @@ static GTimer *dbt; static GtkProgressBar *dbpw=NULL; /* Cache hash tables */ -static GHashTable *place_cache; +struct osm_place_cache { + GHashTable *cache; + guint hit; + guint miss; +}; +static struct osm_place_cache pcache; static guint way_dist_range=OSM_RANGE_WAY; @@ -141,7 +146,6 @@ if (w!=NULL) gboolean osm_db_prepare(sqlite3 *db) { -/* Place */ /* Select nearest place inside lat,lon+-range */ if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist," " ilat,ilon,places.nid,isin_p,isin_c " @@ -224,19 +228,34 @@ return TRUE; void osm_deinit(void) { -if (osmdb!=NULL) { - sqlite3_finalize(sql.select_way_ref); - sqlite3_finalize(sql.select_way_name); - sqlite3_finalize(sql.select_way_next_seg); - sqlite3_finalize(sql.select_way_prev_seg); - sqlite3_finalize(sql.select_way_name_search); - sqlite3_finalize(sql.select_way2); - sqlite3_finalize(sql.select_place); - sqlite3_finalize(sql.select_near_place); +if (osmdb) { + if (sql.select_way_ref) + sqlite3_finalize(sql.select_way_ref); + if (sql.select_way_name) + sqlite3_finalize(sql.select_way_name); + if (sql.select_way_next_seg) + sqlite3_finalize(sql.select_way_next_seg); + if (sql.select_way_prev_seg) + sqlite3_finalize(sql.select_way_prev_seg); + if (sql.select_way_name_search) + sqlite3_finalize(sql.select_way_name_search); + if (sql.select_way2) + sqlite3_finalize(sql.select_way2); + if (sql.select_place) + sqlite3_finalize(sql.select_place); + if (sql.select_near_place) + sqlite3_finalize(sql.select_near_place); + if (sql.select_node_next) + sqlite3_finalize(sql.select_node_next); + if (sql.select_node_prev) + sqlite3_finalize(sql.select_node_prev); } osmdb=NULL; osm_db_ok=FALSE; -g_hash_table_destroy(place_cache); +memset(&sql, 0, sizeof(sql)); +g_hash_table_destroy(pcache.cache); +pcache.hit=0; +pcache.miss=0; g_timer_destroy(dbt); } @@ -244,7 +263,7 @@ gboolean osm_init(sqlite3 **db) { osm_db_ok=FALSE; -place_cache=g_hash_table_new(g_direct_hash, g_direct_equal); +pcache.cache=g_hash_table_new(g_direct_hash, g_direct_equal); dbt=g_timer_new(); if (!db || !*db) { @@ -253,6 +272,7 @@ if (!db || !*db) { } osmdb=*db; +memset(&sql, 0, sizeof(sql)); if (osm_db_prepare(osmdb)==FALSE) { g_printerr("Failed to prepare OSM SQL statements:"); g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb)); @@ -344,21 +364,33 @@ return g_slice_new0(osm_place); static osm_place * osm_place_cache_lookup(guint32 id) { -return g_hash_table_lookup(place_cache, GINT_TO_POINTER(id)); +osm_place *r; +r=g_hash_table_lookup(pcache.cache, GINT_TO_POINTER(id)); +if (r) + pcache.hit++; +else + pcache.miss++; + +g_debug("OSM: Cache %d/%d", pcache.hit, pcache.miss); +return r; } static void osm_place_cache_add(osm_place *p) { if (osm_place_cache_lookup(p->id)==NULL) - g_hash_table_insert(place_cache, GINT_TO_POINTER(p->id), p); + g_hash_table_insert(pcache.cache, GINT_TO_POINTER(p->id), p); } static void osm_place_cache_gc(void) { -gint r; -r=g_hash_table_foreach_remove(place_cache, osm_place_remove, NULL); +guint r; + +r=g_hash_table_foreach_remove(pcache.cache, osm_place_remove, NULL); +g_debug("OSM: Cache cleared (%d)", r); +pcache.hit=0; +pcache.miss=0; } static void @@ -383,14 +415,13 @@ osm_place *n; n=*nr; n=osm_place_cache_lookup(id); if (n) { - g_print("*P!\n"); osm_place_update_distance(n, lat, lon); return TRUE; } n=NULL; /* XXX: better place for this */ -if (g_hash_table_size(place_cache)>OSM_PLACE_CACHE_MAX_ITEMS) +if (g_hash_table_size(pcache.cache)>OSM_PLACE_CACHE_MAX_ITEMS) osm_place_cache_gc(); sqlite3_clear_bindings(sql.select_place); -- 2.39.5