OSDN Git Service

l10n: vi.po (2257t): Update translation
[git-core/git.git] / trace.c
diff --git a/trace.c b/trace.c
index a194b16..e583dc6 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -85,9 +85,13 @@ void trace_disable(struct trace_key *key)
 static const char err_msg[] = "Could not trace into fd given by "
        "GIT_TRACE environment variable";
 
-static int prepare_trace_line(struct trace_key *key, struct strbuf *buf)
+static int prepare_trace_line(const char *file, int line,
+                             struct trace_key *key, struct strbuf *buf)
 {
        static struct trace_key trace_bare = TRACE_KEY_INIT(BARE);
+       struct timeval tv;
+       struct tm tm;
+       time_t secs;
 
        if (!trace_want(key))
                return 0;
@@ -98,7 +102,20 @@ static int prepare_trace_line(struct trace_key *key, struct strbuf *buf)
        if (trace_want(&trace_bare))
                return 1;
 
-       /* add line prefix here */
+       /* print current timestamp */
+       gettimeofday(&tv, NULL);
+       secs = tv.tv_sec;
+       localtime_r(&secs, &tm);
+       strbuf_addf(buf, "%02d:%02d:%02d.%06ld ", tm.tm_hour, tm.tm_min,
+                   tm.tm_sec, (long) tv.tv_usec);
+
+#ifdef HAVE_VARIADIC_MACROS
+       /* print file:line */
+       strbuf_addf(buf, "%s:%d ", file, line);
+       /* align trace output (column 40 catches most files names in git) */
+       while (buf->len < 40)
+               strbuf_addch(buf, ' ');
+#endif
 
        return 1;
 }
@@ -113,60 +130,146 @@ static void print_trace_line(struct trace_key *key, struct strbuf *buf)
        strbuf_release(buf);
 }
 
-static void trace_vprintf(struct trace_key *key, const char *format, va_list ap)
+static void trace_vprintf_fl(const char *file, int line, struct trace_key *key,
+                            const char *format, va_list ap)
 {
        struct strbuf buf = STRBUF_INIT;
 
-       if (!prepare_trace_line(key, &buf))
+       if (!prepare_trace_line(file, line, key, &buf))
                return;
 
        strbuf_vaddf(&buf, format, ap);
        print_trace_line(key, &buf);
 }
 
-void trace_printf_key(struct trace_key *key, const char *format, ...)
+static void trace_argv_vprintf_fl(const char *file, int line,
+                                 const char **argv, const char *format,
+                                 va_list ap)
+{
+       struct strbuf buf = STRBUF_INIT;
+
+       if (!prepare_trace_line(file, line, NULL, &buf))
+               return;
+
+       strbuf_vaddf(&buf, format, ap);
+
+       sq_quote_argv(&buf, argv, 0);
+       print_trace_line(NULL, &buf);
+}
+
+void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
+                    const struct strbuf *data)
+{
+       struct strbuf buf = STRBUF_INIT;
+
+       if (!prepare_trace_line(file, line, key, &buf))
+               return;
+
+       strbuf_addbuf(&buf, data);
+       print_trace_line(key, &buf);
+}
+
+static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
+
+static void trace_performance_vprintf_fl(const char *file, int line,
+                                        uint64_t nanos, const char *format,
+                                        va_list ap)
+{
+       struct strbuf buf = STRBUF_INIT;
+
+       if (!prepare_trace_line(file, line, &trace_perf_key, &buf))
+               return;
+
+       strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000);
+
+       if (format && *format) {
+               strbuf_addstr(&buf, ": ");
+               strbuf_vaddf(&buf, format, ap);
+       }
+
+       print_trace_line(&trace_perf_key, &buf);
+}
+
+#ifndef HAVE_VARIADIC_MACROS
+
+void trace_printf(const char *format, ...)
 {
        va_list ap;
        va_start(ap, format);
-       trace_vprintf(key, format, ap);
+       trace_vprintf_fl(NULL, 0, NULL, format, ap);
        va_end(ap);
 }
 
-void trace_printf(const char *format, ...)
+void trace_printf_key(struct trace_key *key, const char *format, ...)
 {
        va_list ap;
        va_start(ap, format);
-       trace_vprintf(NULL, format, ap);
+       trace_vprintf_fl(NULL, 0, key, format, ap);
        va_end(ap);
 }
 
-void trace_strbuf(struct trace_key *key, const struct strbuf *data)
+void trace_argv_printf(const char **argv, const char *format, ...)
 {
-       struct strbuf buf = STRBUF_INIT;
+       va_list ap;
+       va_start(ap, format);
+       trace_argv_vprintf_fl(NULL, 0, argv, format, ap);
+       va_end(ap);
+}
 
-       if (!prepare_trace_line(key, &buf))
-               return;
+void trace_strbuf(const char *key, const struct strbuf *data)
+{
+       trace_strbuf_fl(NULL, 0, key, data);
+}
 
-       strbuf_addbuf(&buf, data);
-       print_trace_line(key, &buf);
+void trace_performance(uint64_t nanos, const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       trace_performance_vprintf_fl(NULL, 0, nanos, format, ap);
+       va_end(ap);
 }
 
-void trace_argv_printf(const char **argv, const char *format, ...)
+void trace_performance_since(uint64_t start, const char *format, ...)
 {
-       struct strbuf buf = STRBUF_INIT;
        va_list ap;
+       va_start(ap, format);
+       trace_performance_vprintf_fl(NULL, 0, getnanotime() - start,
+                                    format, ap);
+       va_end(ap);
+}
 
-       if (!prepare_trace_line(NULL, &buf))
-               return;
+#else
 
+void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
+                        const char *format, ...)
+{
+       va_list ap;
        va_start(ap, format);
-       strbuf_vaddf(&buf, format, ap);
+       trace_vprintf_fl(file, line, key, format, ap);
        va_end(ap);
+}
 
-       sq_quote_argv(&buf, argv, 0);
-       print_trace_line(NULL, &buf);
+void trace_argv_printf_fl(const char *file, int line, const char **argv,
+                         const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       trace_argv_vprintf_fl(file, line, argv, format, ap);
+       va_end(ap);
 }
 
+void trace_performance_fl(const char *file, int line, uint64_t nanos,
+                             const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       trace_performance_vprintf_fl(file, line, nanos, format, ap);
+       va_end(ap);
+}
+
+#endif /* HAVE_VARIADIC_MACROS */
+
+
 static const char *quote_crnl(const char *path)
 {
        static char new_path[PATH_MAX];
@@ -219,3 +322,107 @@ int trace_want(struct trace_key *key)
 {
        return !!get_trace_fd(key);
 }
+
+#ifdef HAVE_CLOCK_GETTIME
+
+static inline uint64_t highres_nanos(void)
+{
+       struct timespec ts;
+       if (clock_gettime(CLOCK_MONOTONIC, &ts))
+               return 0;
+       return (uint64_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
+
+#elif defined (GIT_WINDOWS_NATIVE)
+
+static inline uint64_t highres_nanos(void)
+{
+       static uint64_t high_ns, scaled_low_ns;
+       static int scale;
+       LARGE_INTEGER cnt;
+
+       if (!scale) {
+               if (!QueryPerformanceFrequency(&cnt))
+                       return 0;
+
+               /* high_ns = number of ns per cnt.HighPart */
+               high_ns = (1000000000LL << 32) / (uint64_t) cnt.QuadPart;
+
+               /*
+                * Number of ns per cnt.LowPart is 10^9 / frequency (or
+                * high_ns >> 32). For maximum precision, we scale this factor
+                * so that it just fits within 32 bit (i.e. won't overflow if
+                * multiplied with cnt.LowPart).
+                */
+               scaled_low_ns = high_ns;
+               scale = 32;
+               while (scaled_low_ns >= 0x100000000LL) {
+                       scaled_low_ns >>= 1;
+                       scale--;
+               }
+       }
+
+       /* if QPF worked on initialization, we expect QPC to work as well */
+       QueryPerformanceCounter(&cnt);
+
+       return (high_ns * cnt.HighPart) +
+              ((scaled_low_ns * cnt.LowPart) >> scale);
+}
+
+#else
+# define highres_nanos() 0
+#endif
+
+static inline uint64_t gettimeofday_nanos(void)
+{
+       struct timeval tv;
+       gettimeofday(&tv, NULL);
+       return (uint64_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
+}
+
+/*
+ * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
+ * (i.e. favoring high precision over wall clock time accuracy).
+ */
+inline uint64_t getnanotime(void)
+{
+       static uint64_t offset;
+       if (offset > 1) {
+               /* initialization succeeded, return offset + high res time */
+               return offset + highres_nanos();
+       } else if (offset == 1) {
+               /* initialization failed, fall back to gettimeofday */
+               return gettimeofday_nanos();
+       } else {
+               /* initialize offset if high resolution timer works */
+               uint64_t now = gettimeofday_nanos();
+               uint64_t highres = highres_nanos();
+               if (highres)
+                       offset = now - highres;
+               else
+                       offset = 1;
+               return now;
+       }
+}
+
+static uint64_t command_start_time;
+static struct strbuf command_line = STRBUF_INIT;
+
+static void print_command_performance_atexit(void)
+{
+       trace_performance_since(command_start_time, "git command:%s",
+                               command_line.buf);
+}
+
+void trace_command_performance(const char **argv)
+{
+       if (!trace_want(&trace_perf_key))
+               return;
+
+       if (!command_start_time)
+               atexit(print_command_performance_atexit);
+
+       strbuf_reset(&command_line);
+       sq_quote_argv(&command_line, argv, 0);
+       command_start_time = getnanotime();
+}