[Groonga-commit] groonga/groonga [master] add cache statistics to status result.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 7月 22日 (木) 17:43:11 JST


Kouhei Sutou	2010-07-22 08:43:11 +0000 (Thu, 22 Jul 2010)

  New Revision: 9d446e2b08e2a072a3d2a3712ed6bc1e00896115

  Log:
    add cache statistics to status result.

  Modified files:
    lib/ctx.c
    lib/ctx.h
    lib/output.h
    lib/proc.c
    resource/admin_html/index.html
    test/unit/http/test-http.rb

  Modified: lib/ctx.c (+15 -0)
===================================================================
--- lib/ctx.c    2010-07-22 08:42:04 +0000 (594c0cf)
+++ lib/ctx.c    2010-07-22 08:43:11 +0000 (3c44081)
@@ -1205,6 +1205,8 @@ typedef struct {
   grn_hash *hash;
   grn_mutex mutex;
   uint32_t max_nentries;
+  uint32_t nfetches;
+  uint32_t nhits;
 } grn_cache;
 
 struct _grn_cache_entry {
@@ -1235,6 +1237,17 @@ grn_cache_max_nentries(void)
   return &grn_gcache.max_nentries;
 }
 
+void
+grn_cache_get_statistics(grn_ctx *ctx, grn_cache_statistics *statistics)
+{
+  MUTEX_LOCK(grn_gcache.mutex);
+  statistics->nentries = GRN_HASH_SIZE(grn_gcache.hash);
+  statistics->max_nentries = grn_gcache.max_nentries;
+  statistics->nfetches = grn_gcache.nfetches;
+  statistics->nhits = grn_gcache.nhits;
+  MUTEX_UNLOCK(grn_gcache.mutex);
+}
+
 static void
 grn_cache_expire_entry(grn_cache_entry *ce)
 {
@@ -1253,6 +1266,7 @@ grn_cache_fetch(grn_ctx *ctx, const char *str, uint32_t str_len)
   grn_obj *obj = NULL;
   if (!ctx->impl || !ctx->impl->db) { return obj; }
   MUTEX_LOCK(grn_gcache.mutex);
+  grn_gcache.nfetches++;
   if (grn_hash_get(&grn_gctx, grn_gcache.hash, str, str_len, (void **)&ce)) {
     if (ce->tv.tv_sec <= grn_db_lastmod(ctx->impl->db)) {
       grn_cache_expire_entry(ce);
@@ -1269,6 +1283,7 @@ grn_cache_fetch(grn_ctx *ctx, const char *str, uint32_t str_len)
       ce0->next->prev = ce;
       ce0->next = ce;
     }
+    grn_gcache.nhits++;
   }
 exit :
   MUTEX_UNLOCK(grn_gcache.mutex);

  Modified: lib/ctx.h (+8 -0)
===================================================================
--- lib/ctx.h    2010-07-22 08:42:04 +0000 (8566feb)
+++ lib/ctx.h    2010-07-22 08:43:11 +0000 (084670f)
@@ -461,6 +461,13 @@ typedef struct {
 
 /**** cache ****/
 
+typedef struct {
+  uint32_t nentries;
+  uint32_t max_nentries;
+  uint32_t nfetches;
+  uint32_t nhits;
+} grn_cache_statistics;
+
 void grn_cache_init(void);
 grn_obj *grn_cache_fetch(grn_ctx *ctx, const char *str, uint32_t str_size);
 void grn_cache_unref(const char *str, uint32_t str_size);
@@ -468,6 +475,7 @@ void grn_cache_update(grn_ctx *ctx, const char *str, uint32_t str_size, grn_obj
 void grn_cache_expire(int32_t size);
 void grn_cache_fin(void);
 uint32_t *grn_cache_max_nentries(void);
+void grn_cache_get_statistics(grn_ctx *ctx, grn_cache_statistics *statistics);
 
 /**** receive handler ****/
 

  Modified: lib/output.h (+4 -0)
===================================================================
--- lib/output.h    2010-07-22 08:42:04 +0000 (082a51c)
+++ lib/output.h    2010-07-22 08:43:11 +0000 (757c907)
@@ -47,6 +47,8 @@ void grn_output_int32(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_typ
                       int32_t value);
 void grn_output_int64(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
                       int64_t value);
+void grn_output_float(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
+                      double value);
 void grn_output_cstr(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
                      const char *value);
 void grn_output_str(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
@@ -66,6 +68,8 @@ void grn_output_bool(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type
   (grn_output_int32(ctx, ctx->impl->outbuf, ctx->impl->output_type, value))
 #define GRN_OUTPUT_INT64(value) \
   (grn_output_int64(ctx, ctx->impl->outbuf, ctx->impl->output_type, value))
+#define GRN_OUTPUT_FLOAT(value) \
+  (grn_output_float(ctx, ctx->impl->outbuf, ctx->impl->output_type, value))
 #define GRN_OUTPUT_CSTR(value)\
   (grn_output_cstr(ctx, ctx->impl->outbuf, ctx->impl->output_type, value))
 #define GRN_OUTPUT_STR(value,value_len)\

  Modified: lib/proc.c (+11 -1)
===================================================================
--- lib/proc.c    2010-07-22 08:42:04 +0000 (6959f1c)
+++ lib/proc.c    2010-07-22 08:43:11 +0000 (de303f9)
@@ -375,8 +375,10 @@ static grn_obj *
 proc_status(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
   grn_timeval now;
+  grn_cache_statistics statistics;
   grn_timeval_now(ctx, &now);
-  GRN_OUTPUT_MAP_OPEN("RESULT", 8);
+  grn_cache_get_statistics(ctx, &statistics);
+  GRN_OUTPUT_MAP_OPEN("RESULT", 12);
   GRN_OUTPUT_CSTR("alloc_count");
   GRN_OUTPUT_INT32(grn_alloc_count());
   GRN_OUTPUT_CSTR("starttime");
@@ -385,6 +387,14 @@ proc_status(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
   GRN_OUTPUT_INT32(now.tv_sec - grn_starttime.tv_sec);
   GRN_OUTPUT_CSTR("version");
   GRN_OUTPUT_CSTR(grn_get_version());
+  GRN_OUTPUT_CSTR("n_queries");
+  GRN_OUTPUT_INT64(statistics.nfetches);
+  GRN_OUTPUT_CSTR("cache_hit_rate");
+  if (statistics.nfetches == 0) {
+    GRN_OUTPUT_FLOAT(0.0);
+  } else {
+    GRN_OUTPUT_FLOAT((double)statistics.nhits / (double)statistics.nfetches * 100.0);
+  }
   GRN_OUTPUT_MAP_CLOSE();
   return NULL;
 }

  Modified: resource/admin_html/index.html (+4 -0)
===================================================================
--- resource/admin_html/index.html    2010-07-22 08:42:04 +0000 (a229073)
+++ resource/admin_html/index.html    2010-07-22 08:43:11 +0000 (fb4ceca)
@@ -41,6 +41,8 @@
             <ul>
               <li>開始時間: <span id="status-starttime"></span></li>
               <li>uptime: <span id="status-uptime"></span></li>
+              <li>クエリ数: <span id="status-n-queries"></span></li>
+              <li>キャッシュヒット率: <span id="status-cache-hit-rate"></span></li>
             </ul>
           </div>
           <div id="database-tab-tablelist">
@@ -610,6 +612,8 @@ GroongaAdmin = {
         var d = b[1];
         $('#status-starttime').text(GroongaAdmin.format_unix_time(d.starttime));
         $('#status-uptime').text(GroongaAdmin.format_duration(d.uptime));
+        $('#status-n-queries').text(d.n_queries);
+        $('#status-cache-hit-rate').text(d.cache_hit_rate);
         GroongaAdmin.change_status_timer(1000);
       },
       error: function() {

  Modified: test/unit/http/test-http.rb (+2 -1)
===================================================================
--- test/unit/http/test-http.rb    2010-07-22 08:42:04 +0000 (fc9334d)
+++ test/unit/http/test-http.rb    2010-07-22 08:43:11 +0000 (afc131d)
@@ -31,7 +31,8 @@ class HTTPTest < Test::Unit::TestCase
   def test_status
     response = get(command_path(:status))
     assert_equal("application/json", response.content_type)
-    assert_equal(["alloc_count", "starttime", "uptime", "version"],
+    assert_equal(["alloc_count", "cache_hit_rate", "n_queries",
+                  "starttime", "uptime", "version"],
                  JSON.parse(response.body)[1].keys.sort)
   end
 




Groonga-commit メーリングリストの案内
Back to archive index