[Groonga-commit] groonga/groonga [master] Boosted up resolution of grn_timeval.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 9月 10日 (金) 19:07:07 JST


Tasuku SUENAGA a.k.a. gunyarakun	2010-09-10 10:07:07 +0000 (Fri, 10 Sep 2010)

  New Revision: 7000794452bb330374339bf33bf1613745bb2410

  Log:
    Boosted up resolution of grn_timeval.

  Modified files:
    configure.ac
    lib/ctx.c
    lib/ctx.h
    lib/db.c
    lib/expr.c
    lib/proc.c
    lib/ql.c
    lib/scm.c
    modules/suggest/suggest.c
    src/groonga.c

  Modified: configure.ac (+6 -1)
===================================================================
--- configure.ac    2010-09-13 08:43:59 +0000 (aff481e)
+++ configure.ac    2010-09-10 10:07:07 +0000 (fdc5c9b)
@@ -135,7 +135,12 @@ AC_CONFIG_FILES([Makefile
 AC_CHECK_HEADERS(sys/mman.h sys/time.h sys/timeb.h sys/param.h sys/types.h pthread.h sys/resource.h)
 AC_CHECK_HEADERS(netdb.h sys/wait.h sys/socket.h netinet/in.h netinet/tcp.h)
 AC_CHECK_HEADERS(ucontext.h signal.h errno.h execinfo.h sys/sysctl.h)
+AC_CHECK_HEADERS(time.h)
 AC_CHECK_FUNCS(localtime_r gmtime_r)
+AC_CHECK_LIB(rt, clock_gettime, [
+  RT_LIBS="-lrt"
+  AC_DEFINE(HAVE_CLOCK_GETTIME, [1], [use clock_gettime])
+], [])
 AC_SYS_LARGEFILE
 AC_TYPE_OFF_T
 AC_TYPE_SIZE_T
@@ -671,7 +676,7 @@ AC_SUBST(examples_dictionarydir)
 CFLAGS="$CFLAGS $OPT_CFLAGS "
 CFLAGS="$CFLAGS -DMODULES_DIR=\\\"\"\$(modulesdir)\"\\\""
 CFLAGS="$CFLAGS -DGROONGA_LOG_PATH=\\\"\"\$(groonga_log_path)\"\\\""
-LIBS="$LIBS $ZLIB_LIBS $LZO_LIBS $PTHREAD_LIBS $M_LIBS $NSL_LIBS $SOCKET_LIBS $WINDOWS_LIBS"
+LIBS="$LIBS $ZLIB_LIBS $LZO_LIBS $RT_LIBS $PTHREAD_LIBS $M_LIBS $NSL_LIBS $SOCKET_LIBS $WINDOWS_LIBS"
 AC_DEFINE_UNQUOTED(CONFIGURE_OPTIONS, "$ac_configure_args", "specified configure options")
 
 # For Debian package release

  Modified: lib/ctx.c (+22 -13)
===================================================================
--- lib/ctx.c    2010-09-13 08:43:59 +0000 (1d74c55)
+++ lib/ctx.c    2010-09-10 10:07:07 +0000 (9006377)
@@ -62,13 +62,23 @@ int grn_uyield_count = 0;
 grn_rc
 grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
 {
+#ifdef HAVE_CLOCK_GETTIME
+  struct timespec t;
+  if (clock_gettime(CLOCK_MONOTONIC, &t)) {
+    SERR("clock_gettime");
+  } else {
+    tv->tv_sec = (int32_t) t.tv_sec;
+    tv->tv_nsec = t.tv_nsec;
+  }
+  return ctx->rc;
+#else /* HAVE_CLOCK_GETTIME */
 #ifdef WIN32
   time_t t;
   struct _timeb tb;
   time(&t);
   _ftime(&tb);
   tv->tv_sec = (int32_t) t;
-  tv->tv_usec = tb.millitm * 1000;
+  tv->tv_nsec = tb.millitm / 1000 * GRN_TIME_NSEC_PER_SEC;
   return GRN_SUCCESS;
 #else /* WIN32 */
   struct timeval t;
@@ -76,10 +86,11 @@ grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
     SERR("gettimeofday");
   } else {
     tv->tv_sec = (int32_t) t.tv_sec;
-    tv->tv_usec = t.tv_usec;
+    tv->tv_nsec = t.tv_usec / GRN_TIME_USEC_PER_SEC * GRN_TIME_NSEC_PER_SEC;
   }
   return ctx->rc;
 #endif /* WIN32 */
+#endif /* HAVE_CLOCK_GETTIME */
 }
 
 void
@@ -87,7 +98,9 @@ grn_time_now(grn_ctx *ctx, grn_obj *obj)
 {
   grn_timeval tv;
   grn_timeval_now(ctx, &tv);
-  GRN_TIME_SET(ctx, obj, GRN_TIME_PACK(tv.tv_sec, tv.tv_usec));
+  GRN_TIME_SET(ctx, obj, GRN_TIME_PACK(tv.tv_sec,
+                                      tv.tv_nsec / GRN_TIME_NSEC_PER_SEC *
+                                      GRN_TIME_USEC_PER_SEC));
 }
 
 grn_rc
@@ -105,7 +118,8 @@ grn_timeval2str(grn_ctx *ctx, grn_timeval *tv, char *buf)
   if (!ltm) { SERR("localtime"); }
   snprintf(buf, GRN_TIMEVAL_STR_SIZE - 1, GRN_TIMEVAL_STR_FORMAT,
            ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday,
-           ltm->tm_hour, ltm->tm_min, ltm->tm_sec, (int) tv->tv_usec);
+           ltm->tm_hour, ltm->tm_min, ltm->tm_sec,
+           (int)(tv->tv_nsec / GRN_TIME_NSEC_PER_SEC * GRN_TIME_USEC_PER_SEC));
   buf[GRN_TIMEVAL_STR_SIZE - 1] = '\0';
   return ctx->rc;
 }
@@ -156,8 +170,8 @@ grn_str2timeval(const char *str, uint32_t str_len, grn_timeval *tv)
     uv *= 10;
     r2++;
   }
-  if (uv >= 1000000) { return GRN_INVALID_ARGUMENT; }
-  tv->tv_usec = uv;
+  if (uv >= GRN_TIME_NSEC_PER_SEC) { return GRN_INVALID_ARGUMENT; }
+  tv->tv_nsec = uv;
   return GRN_SUCCESS;
 }
 
@@ -291,7 +305,7 @@ grn_ctx_impl_init(grn_ctx *ctx)
   ctx->impl->output = NULL /* grn_ctx_concat_func */;
   ctx->impl->data.ptr = NULL;
   ctx->impl->tv.tv_sec = 0;
-  ctx->impl->tv.tv_usec = 0;
+  ctx->impl->tv.tv_nsec = 0;
   GRN_TEXT_INIT(&ctx->impl->subbuf, 0);
   ctx->impl->edge = NULL;
   grn_loader_init(&ctx->impl->loader);
@@ -1040,12 +1054,7 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
       }
       if (expr) { grn_expr_clear_vars(ctx, expr); }
       if (!ctx->impl->qe_next) {
-        uint64_t et;
-        grn_timeval tv;
-        grn_timeval_now(ctx, &tv);
-        et = (tv.tv_sec - ctx->impl->tv.tv_sec) * GRN_TIME_USEC_PER_SEC
-          + (tv.tv_usec - ctx->impl->tv.tv_usec);
-        GRN_LOG(ctx, GRN_LOG_NONE, "%08x|<%012zu rc=%d", (intptr_t)ctx, et, ctx->rc);
+        LAP("<", "rc=%d", ctx->rc);
       }
       goto exit;
     }

  Modified: lib/ctx.h (+12 -1)
===================================================================
--- lib/ctx.h    2010-09-13 08:43:59 +0000 (4a0746f)
+++ lib/ctx.h    2010-09-10 10:07:07 +0000 (b6e5726)
@@ -386,7 +386,7 @@ GRN_VAR const char *grn_qlog_path;
 
 typedef struct {
   int32_t tv_sec;
-  int32_t tv_usec;
+  int32_t tv_nsec;
 } grn_timeval;
 
 extern grn_timeval grn_starttime;
@@ -397,6 +397,17 @@ extern grn_timeval grn_starttime;
 #ifndef GRN_TIMEVAL_STR_FORMAT
 #define GRN_TIMEVAL_STR_FORMAT "%04d-%02d-%02d %02d:%02d:%02d.%06d"
 #endif /* GRN_TIMEVAL_STR_FORMAT */
+#define GRN_TIME_NSEC_PER_SEC 1000000000
+#define GRN_TIME_NSEC_PER_SEC_F 1000000000.0
+
+#define LAP(prefix,format,...) {\
+  uint64_t et;\
+  grn_timeval tv;\
+  grn_timeval_now(ctx, &tv);\
+  et = (tv.tv_sec - ctx->impl->tv.tv_sec) * GRN_TIME_NSEC_PER_SEC\
+    + (tv.tv_nsec - ctx->impl->tv.tv_nsec);\
+  GRN_LOG(ctx, GRN_LOG_NONE, "%08x|" prefix "%015llu " format, (intptr_t)ctx, et, __VA_ARGS__);\
+}
 
 GRN_API grn_rc grn_timeval_now(grn_ctx *ctx, grn_timeval *tv);
 GRN_API grn_rc grn_timeval2str(grn_ctx *ctx, grn_timeval *tv, char *buf);

  Modified: lib/db.c (+4 -2)
===================================================================
--- lib/db.c    2010-09-13 08:43:59 +0000 (fd55d9b)
+++ lib/db.c    2010-09-10 10:07:07 +0000 (e4b7bb7)
@@ -3839,13 +3839,15 @@ grn_obj_cast(grn_ctx *ctx, grn_obj *src, grn_obj *dest, int addp)
           d = strtod(GRN_TEXT_VALUE(&buf), &end);
           if (!errno && end + 1 == GRN_BULK_CURR(&buf)) {
             v.tv_sec = d;
-            v.tv_usec = ((d - v.tv_sec) * GRN_TIME_USEC_PER_SEC);
+            v.tv_nsec = ((d - v.tv_sec) * GRN_TIME_NSEC_PER_SEC);
           } else {
             rc = GRN_INVALID_ARGUMENT;
           }
           GRN_OBJ_FIN(ctx, &buf);
         }
-        GRN_TIME_SET(ctx, dest, GRN_TIME_PACK((int64_t)v.tv_sec, v.tv_usec));
+        GRN_TIME_SET(ctx, dest, GRN_TIME_PACK((int64_t)v.tv_sec,
+                                              v.tv_nsec / GRN_TIME_NSEC_PER_SEC *
+                                              GRN_TIME_USEC_PER_SEC));
       }
       break;
     case GRN_DB_INT64 :

  Modified: lib/expr.c (+1 -10)
===================================================================
--- lib/expr.c    2010-09-13 08:43:59 +0000 (dc27637)
+++ lib/expr.c    2010-09-10 10:07:07 +0000 (d709838)
@@ -3761,15 +3761,6 @@ grn_view_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr,
   return res;
 }
 
-#define LAP(msg,num) {\
-  uint64_t et;\
-  grn_timeval tv;\
-  grn_timeval_now(ctx, &tv);\
-  et = (tv.tv_sec - ctx->impl->tv.tv_sec) * GRN_TIME_USEC_PER_SEC\
-    + (tv.tv_usec - ctx->impl->tv.tv_usec);\
-  GRN_LOG(ctx, GRN_LOG_NONE, "%08x|:%012llu %s(%d)", (intptr_t)ctx, et, msg, num);\
-}
-
 grn_obj *
 grn_table_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr,
                  grn_obj *res, grn_operator op)
@@ -4026,7 +4017,7 @@ grn_table_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr,
           }
         }
         SI_FREE(si);
-        LAP("filter", grn_table_size(ctx, res));
+        LAP(":", "filter(%d)", grn_table_size(ctx, res));
       }
       GRN_OBJ_FIN(ctx, &res_stack);
       GRN_FREE(sis);

  Modified: lib/proc.c (+6 -15)
===================================================================
--- lib/proc.c    2010-09-13 08:43:59 +0000 (4c0c14f)
+++ lib/proc.c    2010-09-10 10:07:07 +0000 (80879dd)
@@ -45,15 +45,6 @@ const char *grn_document_root = NULL;
 #define DEFAULT_DRILLDOWN_LIMIT           10
 #define DEFAULT_DRILLDOWN_OUTPUT_COLUMNS  "_key, _nsubrecs"
 
-#define LAP(msg,num) {\
-  uint64_t et;\
-  grn_timeval tv;\
-  grn_timeval_now(ctx, &tv);\
-  et = (tv.tv_sec - ctx->impl->tv.tv_sec) * GRN_TIME_USEC_PER_SEC\
-    + (tv.tv_usec - ctx->impl->tv.tv_usec);\
-  GRN_LOG(ctx, GRN_LOG_NONE, "%08x|:%012llu %s(%d)", (intptr_t)ctx, et, msg, num);\
-}
-
 grn_rc
 grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
            const char *match_columns, unsigned match_columns_len,
@@ -112,7 +103,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
     if ((cache = grn_cache_fetch(ctx, cache_key, cache_key_size))) {
       GRN_TEXT_PUT(ctx, outbuf, GRN_TEXT_VALUE(cache), GRN_TEXT_LEN(cache));
       grn_cache_unref(cache_key, cache_key_size);
-      LAP("cache", GRN_TEXT_LEN(cache));
+      LAP(":", "cache(%d)", GRN_TEXT_LEN(cache));
       return ctx->rc;
     }
   }
@@ -168,7 +159,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
       res = table_;
     }
     nhits = res ? grn_table_size(ctx, res) : 0;
-    LAP("select", nhits);
+    LAP(":", "select(%d)", nhits);
     GRN_OUTPUT_ARRAY_OPEN("RESULT", -1);
     if (res) {
       if (scorer && scorer_len) {
@@ -188,7 +179,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
           }
           grn_obj_unlink(ctx, scorer_);
         }
-        LAP("score", nhits);
+        LAP(":", "score(%d)", nhits);
       }
 
       grn_normalize_offset_and_limit(ctx, nhits, &offset, &limit);
@@ -198,7 +189,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
                                        GRN_OBJ_TABLE_NO_KEY, NULL, res))) {
           if ((keys = grn_table_sort_key_from_str(ctx, sortby, sortby_len, res, &nkeys))) {
             grn_table_sort(ctx, res, offset, limit, sorted, keys, nkeys);
-            LAP("sort", limit);
+            LAP(":", "sort(%d)", limit);
             GRN_OBJ_FORMAT_INIT(&format, nhits, 0, limit, offset);
             format.flags =
               GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
@@ -219,7 +210,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
         GRN_OUTPUT_OBJ(res, &format);
         GRN_OBJ_FORMAT_FIN(ctx, &format);
       }
-      LAP("output", limit);
+      LAP(":", "output(%d)", limit);
       if (!ctx->rc && drilldown_len) {
         uint32_t i, ngkeys;
         grn_table_sort_key *gkeys;
@@ -274,7 +265,7 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
               }
               grn_obj_unlink(ctx, g.table);
             }
-            LAP("drilldown", nhits);
+            LAP(":", "drilldown(%d)", nhits);
           }
           grn_table_sort_key_close(ctx, gkeys, ngkeys);
         }

  Modified: lib/ql.c (+11 -11)
===================================================================
--- lib/ql.c    2010-09-13 08:43:59 +0000 (fe4cd24)
+++ lib/ql.c    2010-09-10 10:07:07 +0000 (1e38a27)
@@ -614,7 +614,7 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
             v = (int32_t) FVALUE(cell);
             break;
           case GRN_CELL_TIME :
-            v = (int32_t) cell->u.tv.tv_usec;
+            v = (int32_t) cell->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC * GRN_TIME_USEC_PER_SEC;
             break;
           }
           if (!obj) { if (!(obj = grn_obj_open(ctx, GRN_BULK, 0, 0))) { return NULL; }}
@@ -638,7 +638,7 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
             v = (uint32_t) FVALUE(cell);
             break;
           case GRN_CELL_TIME :
-            v = (uint32_t) cell->u.tv.tv_usec;
+            v = (uint32_t) cell->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC * GRN_TIME_USEC_PER_SEC;
             break;
           }
           if (!obj) { if (!(obj = grn_obj_open(ctx, GRN_BULK, 0, 0))) { return NULL; }}
@@ -659,7 +659,7 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
             v = (int64_t) FVALUE(cell);
             break;
           case GRN_CELL_TIME :
-            v = (int32_t) cell->u.tv.tv_usec;
+            v = (int32_t) cell->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC * GRN_TIME_USEC_PER_SEC;
             break;
           }
           if (!obj) { if (!(obj = grn_obj_open(ctx, GRN_BULK, 0, 0))) { return NULL; }}
@@ -684,7 +684,7 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
             v = FVALUE(cell);
             break;
           case GRN_CELL_TIME :
-            v = ((double) cell->u.tv.tv_usec) / 1000000 + cell->u.tv.tv_sec;
+            v = ((double) cell->u.tv.tv_nsec) / GRN_TIME_NSEC_PER_SEC + cell->u.tv.tv_sec;
             break;
           }
           if (!obj) { if (!(obj = grn_obj_open(ctx, GRN_BULK, 0, 0))) { return NULL; }}
@@ -706,7 +706,7 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
                   if (cur >= str + len || *cur != '.') {
                     QLWARN("illegal time format '%s'", str);
                   }
-                  v.tv_usec = grn_atoi(cur + 1, str + len, &cur);
+                  v.tv_nsec = grn_atoi(cur + 1, str + len, &cur);
                   if (cur >= str + len || *cur != '>') {
                     QLWARN("illegal time format '%s'", str);
                   }
@@ -716,22 +716,22 @@ cell2obj(grn_ctx *ctx, grn_cell *cell, grn_obj *column, grn_obj *obj)
                   int len = cell->u.b.size;
                   STR2DBL(str, len, dval);
                   v.tv_sec = (int32_t) dval;
-                  v.tv_usec = (int32_t) ((dval - v.tv_sec) * 1000000);
+                  v.tv_nsec = (int32_t) ((dval - v.tv_sec) * GRN_TIME_NSEC_PER_SEC);
                 }
               }
             }
             break;
           case GRN_CELL_INT :
             v.tv_sec = (int32_t) IVALUE(cell);
-            v.tv_usec = 0;
+            v.tv_nsec = 0;
             break;
           case GRN_CELL_FLOAT :
             v.tv_sec = (int32_t) FVALUE(cell);
-            v.tv_usec = (int32_t) ((FVALUE(cell) - v.tv_sec) * 1000000);
+            v.tv_nsec = (int32_t) ((FVALUE(cell) - v.tv_sec) * GRN_TIME_NSEC_PER_SEC);
             break;
           case GRN_CELL_TIME :
             v.tv_sec = cell->u.tv.tv_sec;
-            v.tv_usec = cell->u.tv.tv_usec;
+            v.tv_nsec = cell->u.tv.tv_nsec;
             break;
           }
           if (!obj) { if (!(obj = grn_obj_open(ctx, GRN_BULK, 0, 0))) { return NULL; }}
@@ -2846,7 +2846,7 @@ disp_j(grn_ctx *ctx, grn_cell *obj, grn_obj *buf)
     case GRN_CELL_TIME :
       {
         double dv= obj->u.tv.tv_sec;
-        dv += obj->u.tv.tv_usec / 1000000.0;
+        dv += obj->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
         grn_text_ftoa(ctx, buf, dv);
       }
       break;
@@ -3055,7 +3055,7 @@ disp_t(grn_ctx *ctx, grn_cell *obj, grn_obj *buf, int *f)
     case GRN_CELL_TIME :
       {
         double dv= obj->u.tv.tv_sec;
-        dv += obj->u.tv.tv_usec / 1000000.0;
+        dv += obj->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
         grn_text_ftoa(ctx, buf, dv);
         *f = 1;
       }

  Modified: lib/scm.c (+17 -17)
===================================================================
--- lib/scm.c    2010-09-13 08:43:59 +0000 (f0adfc4)
+++ lib/scm.c    2010-09-10 10:07:07 +0000 (6e8035a)
@@ -763,7 +763,7 @@ mk_const(grn_ctx *ctx, char *name, unsigned int len)
       if (cur >= name + len || *cur != '.') {
         QLERR("illegal time format '%s'", name);
       }
-      tv.tv_usec = grn_atoi(cur + 1, name + len, &cur);
+      tv.tv_nsec = grn_atoi(cur + 1, name + len, &cur);
       if (cur >= name + len || *cur != '>') {
         QLERR("illegal time format '%s'", name);
       }
@@ -1010,7 +1010,7 @@ grn_obj_inspect(grn_ctx *ctx, grn_cell *obj, grn_obj *buf, int flags)
       GRN_TEXT_PUTS(ctx, buf, "#:<");
       grn_text_itoa(ctx, buf, obj->u.tv.tv_sec);
       GRN_TEXT_PUTS(ctx, buf, ".");
-      grn_text_itoa(ctx, buf, obj->u.tv.tv_usec);
+      grn_text_itoa(ctx, buf, obj->u.tv.tv_nsec);
       GRN_TEXT_PUTC(ctx, buf, '>');
       break;
     case GRN_QUERY :
@@ -2245,7 +2245,7 @@ grn_ql_eval(grn_ctx *ctx, grn_cell *code, grn_cell *objs)
       if (x->u.tv.tv_sec != y->u.tv.tv_sec) {\
         r = (x->u.tv.tv_sec op y->u.tv.tv_sec);\
       } else {\
-        r = (x->u.tv.tv_usec op y->u.tv.tv_usec);\
+        r = (x->u.tv.tv_nsec op y->u.tv.tv_nsec);\
       }\
     } else {\
       QLERR("can't compare");\
@@ -2261,7 +2261,7 @@ grn_ql_eval(grn_ctx *ctx, grn_cell *code, grn_cell *objs)
   case GRN_CELL_TIME :\
     {\
       double dv= x->u.tv.tv_sec op y->u.tv.tv_sec;\
-      dv += (x->u.tv.tv_usec op y->u.tv.tv_usec) / 1000000.0;\
+      dv += (x->u.tv.tv_nsec op y->u.tv.tv_nsec) / GRN_TIME_NSEC_PER_SEC_F;\
       SETFLOAT(v, dv);\
     }\
     break;\
@@ -2271,7 +2271,7 @@ grn_ql_eval(grn_ctx *ctx, grn_cell *code, grn_cell *objs)
       int64_t sec = x->u.tv.tv_sec op IVALUE(y);\
       if (sec < INT32_MIN || INT32_MAX < sec) { QLERR("time val overflow"); }\
       tv.tv_sec = (int)sec;\
-      tv.tv_usec = x->u.tv.tv_usec;\
+      tv.tv_nsec = x->u.tv.tv_nsec;\
       SETTIME(v, &tv);\
     }\
     break;\
@@ -2279,17 +2279,17 @@ grn_ql_eval(grn_ctx *ctx, grn_cell *code, grn_cell *objs)
     {\
       grn_timeval tv;\
       double sec = x->u.tv.tv_sec op (int)FVALUE(y);\
-      int32_t usec = x->u.tv.tv_usec op (int)((FVALUE(y) - (int)FVALUE(y)) * 1000000);\
+      int32_t nsec = x->u.tv.tv_nsec op (int)((FVALUE(y) - (int)FVALUE(y)) * GRN_TIME_NSEC_PER_SEC);\
       if (sec < INT32_MIN || INT32_MAX < sec) { QLERR("time val overflow"); }\
       tv.tv_sec = (int)sec;\
-      if (usec < 0) {\
+      if (nsec < 0) {\
         tv.tv_sec--;\
-        usec += 1000000;\
-      } else if (usec >= 1000000) {\
+        nsec += GRN_TIME_NSEC_PER_SEC;\
+      } else if (nsec >= GRN_TIME_NSEC_PER_SEC) {\
         tv.tv_sec++;\
-        usec -= 1000000;\
+        nsec -= GRN_TIME_NSEC_PER_SEC;\
       }\
-      tv.tv_usec = usec;\
+      tv.tv_nsec = nsec;\
       SETTIME(v, &tv);\
     }\
     break;\
@@ -2565,7 +2565,7 @@ nf_neq(grn_ctx *ctx, grn_cell *args, grn_ql_co *co)
         if (x->u.tv.tv_sec != y->u.tv.tv_sec) {
           r = (x->u.tv.tv_sec == y->u.tv.tv_sec);
         } else {
-          r = (x->u.tv.tv_usec == y->u.tv.tv_usec);
+          r = (x->u.tv.tv_nsec == y->u.tv.tv_nsec);
         }
       } else {
         QLERR("can't compare");
@@ -2868,11 +2868,11 @@ nf_timestr(grn_ctx *ctx, grn_cell *args, grn_ql_co *co)
     /* fallthru */
   case GRN_CELL_INT :
     tv.tv_sec = IVALUE(x);
-    tv.tv_usec = 0;
+    tv.tv_nsec = 0;
     break;
   case GRN_CELL_FLOAT :
     tv.tv_sec = (int32_t) FVALUE(x);
-    tv.tv_usec = (int32_t) ((FVALUE(x) - tv.tv_sec) * 1000000);
+    tv.tv_nsec = (int32_t) ((FVALUE(x) - tv.tv_sec) * GRN_TIME_NSEC_PER_SEC);
     break;
   case GRN_CELL_TIME :
     memcpy(&tv, &x->u.tv, sizeof(grn_timeval));
@@ -2900,7 +2900,7 @@ nf_tonumber(grn_ctx *ctx, grn_cell *args, grn_ql_co *co)
   case GRN_CELL_TIME :
     {
       double dv= x->u.tv.tv_sec;
-      dv += x->u.tv.tv_usec / 1000000.0;
+      dv += x->u.tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
       GRN_CELL_NEW(ctx, v);
       SETFLOAT(v, dv);
     }
@@ -2933,13 +2933,13 @@ nf_totime(grn_ctx *ctx, grn_cell *args, grn_ql_co *co)
     break;
   case GRN_CELL_INT :
     tv.tv_sec = (int32_t) IVALUE(x);
-    tv.tv_usec = 0;
+    tv.tv_nsec = 0;
     GRN_CELL_NEW(ctx, v);
     SETTIME(v, &tv);
     break;
   case GRN_CELL_FLOAT :
     tv.tv_sec = (int32_t) FVALUE(x);
-    tv.tv_usec = (int32_t) ((FVALUE(x) - tv.tv_sec) * 1000000);
+    tv.tv_nsec = (int32_t) ((FVALUE(x) - tv.tv_sec) * GRN_TIME_NSEC_PER_SEC);
     GRN_CELL_NEW(ctx, v);
     SETTIME(v, &tv);
     break;

  Modified: modules/suggest/suggest.c (+6 -15)
===================================================================
--- modules/suggest/suggest.c    2010-09-13 08:43:59 +0000 (637e8cf)
+++ modules/suggest/suggest.c    2010-09-10 10:07:07 +0000 (d7dc1e3)
@@ -28,15 +28,6 @@
 #define CORRECT  2
 #define SUGGEST  4
 
-#define LAP(msg,num) {\
-  uint64_t et;\
-  grn_timeval tv;\
-  grn_timeval_now(ctx, &tv);\
-  et = (tv.tv_sec - ctx->impl->tv.tv_sec) * GRN_TIME_USEC_PER_SEC\
-    + (tv.tv_usec - ctx->impl->tv.tv_usec);\
-  GRN_LOG(ctx, GRN_LOG_NONE, "%08x|:%012llu %s(%d)", (intptr_t)ctx, et, msg, num);\
-}
-
 static int
 grn_parse_suggest_types(const char *nptr, const char *end)
 {
@@ -151,7 +142,7 @@ output(grn_ctx *ctx, grn_obj *table, grn_obj *res, grn_id tid,
     }
     if ((keys = grn_table_sort_key_from_str(ctx, sortby_val, sortby_len, res, &nkeys))) {
       grn_table_sort(ctx, res, offset, limit, sorted, keys, nkeys);
-      LAP("sort", limit);
+      LAP(":", "sort(%d)", limit);
       GRN_OBJ_FORMAT_INIT(&format, grn_table_size(ctx, res), 0, limit, offset);
       format.flags =
         GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
@@ -263,7 +254,7 @@ correct(grn_ctx *ctx, grn_obj *table, grn_obj *query, grn_obj *sortby,
                               GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, table, NULL))) {
     grn_id tid = grn_table_get(ctx, table, TEXT_VALUE_LEN(query));
     int32_t orig_score, max_score = cooccur_search(ctx, table, tid, res, CORRECT);
-    LAP("cooccur", max_score);
+    LAP(":", "cooccur(%d)", max_score);
     if (GRN_TEXT_LEN(query) && max_score < 100) {
       grn_obj *key, *index;
       if ((key = grn_obj_column(ctx, table, CONST_STR_LEN("_key")))) {
@@ -275,7 +266,7 @@ correct(grn_ctx *ctx, grn_obj *table, grn_obj *query, grn_obj *sortby,
           grn_ii_select(ctx, (grn_ii *)index, TEXT_VALUE_LEN(query),
                         (grn_hash *)res, GRN_OP_OR, &optarg);
           grn_obj_unlink(ctx, index);
-          LAP("similar", grn_table_size(ctx, res));
+          LAP(":", "similar(%d)", grn_table_size(ctx, res));
           {
             grn_hash_cursor *hc = grn_hash_cursor_open(ctx, (grn_hash *)res, NULL,
                                                        0, NULL, 0, 0, -1, 0);
@@ -298,7 +289,7 @@ correct(grn_ctx *ctx, grn_obj *table, grn_obj *query, grn_obj *sortby,
               grn_hash_cursor_close(ctx, hc);
             }
           }
-          LAP("filter", grn_table_size(ctx, res));
+          LAP(":", "filter(%d)", grn_table_size(ctx, res));
           {
             /* exec _score -= edit_distance(_key, "query string") for all records */
             grn_obj *var;
@@ -454,8 +445,8 @@ func_suggest_preparer(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *us
           grn_obj_get_value(ctx, events_type, *ep, &pre_type);
           grn_obj_get_value(ctx, events_time, *ep, &pre_time);
           grn_obj_get_value(ctx, events_item, *ep, &pre_item);
-          if (GRN_TIME_VALUE(&pre_time) + 60000000 < post_time) {
-            r = (int)((post_time - GRN_TIME_VALUE(&pre_time))/1000000);
+          if (GRN_TIME_VALUE(&pre_time) + 60 * GRN_TIME_USEC_PER_SEC < post_time) {
+            r = (int)((post_time - GRN_TIME_VALUE(&pre_time))/GRN_TIME_USEC_PER_SEC);
             break;
           }
           key = key_ + GRN_RECORD_VALUE(&pre_item);

  Modified: src/groonga.c (+6 -6)
===================================================================
--- src/groonga.c    2010-09-13 08:43:59 +0000 (ec0f7b9)
+++ src/groonga.c    2010-09-10 10:07:07 +0000 (8aae2f7)
@@ -481,11 +481,11 @@ print_return_code(grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj
       grn_timeval tv;
       grn_timeval_now(ctx, &tv);
       dv = ctx->impl->tv.tv_sec;
-      dv += ctx->impl->tv.tv_usec / 1000000.0;
+      dv += ctx->impl->tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
       GRN_TEXT_PUTC(ctx, head, ',');
       grn_text_ftoa(ctx, head, dv);
       dv = (tv.tv_sec - ctx->impl->tv.tv_sec);
-      dv += (tv.tv_usec - ctx->impl->tv.tv_usec) / 1000000.0;
+      dv += (tv.tv_nsec - ctx->impl->tv.tv_nsec) / GRN_TIME_NSEC_PER_SEC_F;
       GRN_TEXT_PUTC(ctx, head, ',');
       grn_text_ftoa(ctx, head, dv);
     }
@@ -515,10 +515,10 @@ print_return_code(grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj
       grn_timeval tv;
       grn_timeval_now(ctx, &tv);
       dv = ctx->impl->tv.tv_sec;
-      dv += ctx->impl->tv.tv_usec / 1000000.0;
+      dv += ctx->impl->tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
       grn_text_ftoa(ctx, head, dv);
       dv = (tv.tv_sec - ctx->impl->tv.tv_sec);
-      dv += (tv.tv_usec - ctx->impl->tv.tv_usec) / 1000000.0;
+      dv += (tv.tv_nsec - ctx->impl->tv.tv_nsec) / GRN_TIME_NSEC_PER_SEC_F;
       GRN_TEXT_PUTC(ctx, head, '\t');
       grn_text_ftoa(ctx, head, dv);
     }
@@ -564,10 +564,10 @@ print_return_code(grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj
           grn_timeval tv;
           grn_timeval_now(ctx, &tv);
           dv = ctx->impl->tv.tv_sec;
-          dv += ctx->impl->tv.tv_usec / 1000000.0;
+          dv += ctx->impl->tv.tv_nsec / GRN_TIME_NSEC_PER_SEC_F;
           grn_text_ftoa(ctx, head, dv);
           dv = (tv.tv_sec - ctx->impl->tv.tv_sec);
-          dv += (tv.tv_usec - ctx->impl->tv.tv_usec) / 1000000.0;
+          dv += (tv.tv_nsec - ctx->impl->tv.tv_nsec) / GRN_TIME_NSEC_PER_SEC_F;
           GRN_TEXT_PUTS(ctx, head, "\" ELAPSED=\"");
           grn_text_ftoa(ctx, head, dv);
           GRN_TEXT_PUTS(ctx, head, "\">");




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