Call GC_stats/verbose_log_printf instead of GC_log_printf if print_stats
authorIvan Maidanski <ivmai@mail.ru>
Thu, 15 Nov 2012 17:47:22 +0000 (21:47 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 15 Nov 2012 17:56:52 +0000 (21:56 +0400)
(code refactoring)

* alloc.c (GC_try_to_collect_inner, GC_stopped_mark,
GC_finish_collection): Use GC_stats_log_printf instead of
GC_log_printf inside "if (GC_print_stats)" blocks.
* include/private/gc_priv.h (GC_COND_LOG_PRINTF): Likewise.
* finalize.c (GC_print_finalization_stats): Use GC_stats_log_printf
instead of GC_log_printf (since the function is invoked only inside
"if (GC_print_stats)" blocks).
* include/private/gc_priv.h (GC_printf, GC_log_printf): Refine
comment.
* include/private/gc_priv.h (GC_stats_log_printf,
GC_verbose_log_printf): New macro (redirected to GC_log_printf).
* reclaim.c (GC_reclaim_all): Use GC_verbose_log_printf instead of
GC_log_printf inside "if (GC_print_stats==VERBOSE)" blocks.
* include/private/gc_priv.h (GC_COND_LOG_PRINTF): Likewise.
* misc.c (GC_LOG_PRINTF_IMPL): New macro (only if GC_ANDROID_LOG,
copy most code from GC_log_printf).
* misc.c (GC_log_printf): Define separately for Android using
GC_LOG_PRINTF_IMPL macro (only if GC_ANDROID_LOG).

alloc.c
finalize.c
include/private/gc_priv.h
misc.c
reclaim.c

diff --git a/alloc.c b/alloc.c
index a497e67..925c53b 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -429,7 +429,7 @@ GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func stop_func)
 #   ifndef SMALL_CONFIG
       if (GC_print_stats) {
         GET_TIME(start_time);
-        GC_log_printf("Initiating full world-stop collection!\n");
+        GC_stats_log_printf("Initiating full world-stop collection!\n");
       }
 #   endif
     GC_promote_black_lists();
@@ -468,8 +468,8 @@ GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func stop_func)
 #   ifndef SMALL_CONFIG
       if (GC_print_stats) {
         GET_TIME(current_time);
-        GC_log_printf("Complete collection took %lu msecs\n",
-                      MS_TIME_DIFF(current_time,start_time));
+        GC_stats_log_printf("Complete collection took %lu msecs\n",
+                            MS_TIME_DIFF(current_time,start_time));
       }
 #   endif
     return(TRUE);
@@ -675,7 +675,7 @@ STATIC GC_bool GC_stopped_mark(GC_stop_func stop_func)
         world_stopped_total_divisor = ++divisor;
 
         GC_ASSERT(divisor != 0);
-        GC_log_printf(
+        GC_stats_log_printf(
                 "World-stopped marking took %lu msecs (%u in average)\n",
                 time_diff, total_time / divisor);
       }
@@ -947,9 +947,10 @@ STATIC void GC_finish_collection(void)
           GC_print_finalization_stats();
 #       endif
 
-        GC_log_printf("Finalize plus initiate sweep took %lu + %lu msecs\n",
-                      MS_TIME_DIFF(finalize_time,start_time),
-                      MS_TIME_DIFF(done_time,finalize_time));
+        GC_stats_log_printf(
+                "Finalize plus initiate sweep took %lu + %lu msecs\n",
+                MS_TIME_DIFF(finalize_time,start_time),
+                MS_TIME_DIFF(done_time,finalize_time));
       }
 #   endif
 }
index 1c5ebaf..32c9394 100644 (file)
@@ -964,13 +964,13 @@ GC_INNER void GC_notify_or_invoke_finalizers(void)
     struct finalizable_object *fo = GC_finalize_now;
     unsigned long ready = 0;
 
-    GC_log_printf(
+    GC_stats_log_printf(
         "%lu finalization table entries; %lu disappearing links alive\n",
         (unsigned long)GC_fo_entries, (unsigned long)GC_dl_entries);
     for (; 0 != fo; fo = fo_next(fo)) ++ready;
-    GC_log_printf("%lu objects are eligible for immediate finalization; "
-                  "%ld links cleared\n",
-                  ready, (long)GC_old_dl_entries - (long)GC_dl_entries);
+    GC_stats_log_printf("%lu objects are eligible for immediate finalization;"
+                        " %ld links cleared\n",
+                        ready, (long)GC_old_dl_entries - (long)GC_dl_entries);
   }
 #endif /* !SMALL_CONFIG */
 
index 6352267..4ea9cf8 100644 (file)
@@ -1889,7 +1889,7 @@ GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */
   /* GC_print_stats should be visible to extra/MacOS.c. */
   extern int GC_print_stats;    /* Nonzero generates basic GC log.      */
                                 /* VERBOSE generates add'l messages.    */
-#else
+#else /* SMALL_CONFIG */
 # define GC_print_stats 0
   /* Will this remove the message character strings from the executable? */
   /* With a particular level of optimizations, it should...              */
@@ -2018,7 +2018,8 @@ GC_API void GC_CALL GC_noop1(word);
 
 /* Logging and diagnostic output:       */
 /* GC_printf is used typically on client explicit print requests.       */
-/* It's recommended to put "\n" at 'format' string end (for atomicity). */
+/* For all GC_X_printf routines, it is recommended to put "\n" at       */
+/* 'format' string end (for output atomicity).                          */
 GC_API_PRIV void GC_printf(const char * format, ...)
                         GC_ATTR_FORMAT_PRINTF(1, 2);
                         /* A version of printf that doesn't allocate,   */
@@ -2028,20 +2029,26 @@ GC_API_PRIV void GC_printf(const char * format, ...)
 GC_API_PRIV void GC_err_printf(const char * format, ...)
                         GC_ATTR_FORMAT_PRINTF(1, 2);
 
+/* Basic logging routine.  Typically, GC_log_printf is called directly  */
+/* only inside various DEBUG_x blocks.                                  */
 #if defined(__cplusplus) && defined(SYMBIAN)
   extern "C" {
 #endif
-/* Logging routine.  Typically called only if GC_print_stats.  It is    */
-/* recommended to put "\n" at 'format' string end (for atomicity).      */
 GC_API_PRIV void GC_log_printf(const char * format, ...)
                         GC_ATTR_FORMAT_PRINTF(1, 2);
 #if defined(__cplusplus) && defined(SYMBIAN)
   }
 #endif
 
-#define GC_COND_LOG_PRINTF if (!GC_print_stats) {} else GC_log_printf
+  /* GC_stats_log_printf should be called only if GC_print_stats.       */
+# define GC_stats_log_printf GC_log_printf
+  /* GC_verbose_log_printf is called only if GC_print_stats is VERBOSE. */
+# define GC_verbose_log_printf GC_log_printf
+
+/* Convenient macros for GC_stats/verbose_log_printf invocation.        */
+#define GC_COND_LOG_PRINTF if (!GC_print_stats) {} else GC_stats_log_printf
 #define GC_VERBOSE_LOG_PRINTF \
-                if (GC_print_stats != VERBOSE) {} else GC_log_printf
+                if (GC_print_stats != VERBOSE) {} else GC_verbose_log_printf
 
 void GC_err_puts(const char *s);
                         /* Write s to stderr, don't buffer, don't add   */
diff --git a/misc.c b/misc.c
index 5e3bad9..77c78ce 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1503,19 +1503,35 @@ void GC_err_printf(const char *format, ...)
     GC_err_puts(buf);
 }
 
-void GC_log_printf(const char *format, ...)
-{
+#ifndef GC_ANDROID_LOG
+
+  void GC_log_printf(const char *format, ...)
+  {
     char buf[BUFSZ + 1];
 
     GC_PRINTF_FILLBUF(buf, format);
-#   ifdef GC_ANDROID_LOG
-      __android_log_write(ANDROID_LOG_INFO, GC_ANDROID_LOG_TAG, buf);
-      if (GC_log == GC_DEFAULT_STDERR_FD)
-        return; /* skip duplicate write to stderr */
-#   endif
     if (WRITE(GC_log, buf, strlen(buf)) < 0)
       ABORT("write to GC log failed");
-}
+  }
+
+#else
+
+# define GC_LOG_PRINTF_IMPL(loglevel, fileLogCond, format) \
+        { \
+          char buf[BUFSZ + 1]; \
+          GC_PRINTF_FILLBUF(buf, format); \
+          __android_log_write(loglevel, GC_ANDROID_LOG_TAG, buf); \
+          if (GC_log != GC_DEFAULT_STDERR_FD && (fileLogCond) \
+              && WRITE(GC_log, buf, strlen(buf)) < 0) \
+            ABORT("write to GC log file failed"); \
+        }
+
+  void GC_log_printf(const char *format, ...)
+  {
+    GC_LOG_PRINTF_IMPL(ANDROID_LOG_INFO, TRUE, format);
+  }
+
+#endif /* GC_ANDROID_LOG */
 
 void GC_err_puts(const char *s)
 {
index 1cf7555..285e146 100644 (file)
--- a/reclaim.c
+++ b/reclaim.c
@@ -727,8 +727,8 @@ GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
 #   ifndef SMALL_CONFIG
       if (GC_print_stats == VERBOSE) {
         GET_TIME(done_time);
-        GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
-                      MS_TIME_DIFF(done_time,start_time));
+        GC_verbose_log_printf("Disposing of reclaim lists took %lu msecs\n",
+                              MS_TIME_DIFF(done_time,start_time));
       }
 #   endif
     return(TRUE);