public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [google]  support for building Linux kernel with FDO (issue4523061)
@ 2011-05-13  9:10 Rong Xu
  2011-05-13  9:23 ` Xinliang David Li
  2011-05-13 13:58 ` Paolo Bonzini
  0 siblings, 2 replies; 24+ messages in thread
From: Rong Xu @ 2011-05-13  9:10 UTC (permalink / raw)
  To: reply, gcc-patches

This patch add support to build Linux kernel with FDO. 

Building Linux kernel with FDO needs both compiler change and 
kernel changes. This part only contains the gcc changes. I'll 
attach kernel changes to the reveiew thread for reference. 

Source files gcov-io.c gcov-io.h and libgcov.c will be copied to
kernel source and directly used in the kernel build. When building
for kernel, we assume option '-D__KERNEL__' is used.

To enable profile generation, we need to
a.1) set the following config:
     CONFIG_GCOV_KERNEL=y
     CONFIG_GCOV_PROFILE_ALL=y
a.2) set COMPILER_LIB_PATH to the absolute path to the
     parent directory of "gcov-src". "gcov-src" is a directory
     containing the following 4 files:
       gcov-iov.h gcov-io.h gcov-io.c and libgcov.c.
     It usually sits at
${GCC_ROOT}/lib/gcc/x86_64-unknown-linux-gnu/${GCC_VERSION}/
a.3) set CFLAGS_GCOV as "-fprofile-generate" and build the 
     kernel (prof_gen).

To obtain the profile data
b.1) install profile_gen kernel, mount debugfs.
b.2) reset the profile count:
     echo 1 > /sys/kernel/debug/gcov/reset
b.3) run the benchmark
b.4) cp -r /sys/kernel/debug/gcov/<your_kernel_build_dir>/ <dest_dir>

To build FDO optimized kernel:
c.1 ) same as a.1).
c.2 ) same as a.2).
c.3) set set CFLAGS_GCOV as 
     "-fprofile-use -fprofile-correction -Wcoverage-mismatch" and build.

Performance results:
Tested with 2.6.34 and 2.6.36 kernel with stand-alone benchmarks:
tbench(~23%), dbench(~4%), unixbench(1%-2%), no noticable regressions.

This patch has been testd with bootstraps, regression test, standalong
kernel benchmarks (tbench, dbench, membench, unixbench, iozone, specjbb,
and kernbench), and google internal FDO tests.

2011-05-12  Rong Xu  <xur@google.com>

	* gcc/gcov-io.c	(revision 173717): FDO support to build Linux kernel.
	* gcc/gcov-io.h	(revision 173717): FDO support to build Linux kernel.
	* gcc/coverage.c	(revision 173717): set a flag if building for Linux kernel.
	* gcc/tree-profile.c	(revision 173717): don't emit TLS declarations for Linux kernel builds.
	* gcc/libgcov.c	(revision 173717): FDO support to build Linux kernel.

Index: gcc/gcov-io.c
===================================================================
--- gcc/gcov-io.c	(revision 173717)
+++ gcc/gcov-io.c	(working copy)
@@ -32,6 +32,10 @@
 static void gcov_allocate (unsigned);
 #endif
 
+#ifdef __GCOV_KERNEL__
+struct gcov_var gcov_var ATTRIBUTE_HIDDEN;
+#endif
+
 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
 {
 #if !IN_LIBGCOV
@@ -54,6 +58,7 @@
    Return zero on failure, >0 on opening an existing file and <0 on
    creating a new one.  */
 
+#ifndef __GCOV_KERNEL__
 GCOV_LINKAGE int
 #if IN_LIBGCOV
 gcov_open (const char *name)
@@ -148,7 +153,24 @@
 
   return 1;
 }
+#else /* __GCOV_KERNEL__ */
 
+extern _GCOV_FILE *gcov_current_file;
+
+GCOV_LINKAGE int
+gcov_open (const char *name)
+{
+  gcov_var.start = 0;
+  gcov_var.offset = gcov_var.length = 0;
+  gcov_var.overread = -1u;
+  gcov_var.error = 0;
+  gcov_var.file = gcov_current_file;
+  gcov_var.mode = 1;
+
+  return 1;
+}
+#endif /* __GCOV_KERNEL__ */
+
 /* Close the current gcov file. Flushes data to disk. Returns nonzero
    on failure or error flag set.  */
 
@@ -161,7 +183,7 @@
       if (gcov_var.offset && gcov_var.mode < 0)
 	gcov_write_block (gcov_var.offset);
 #endif
-      fclose (gcov_var.file);
+      _GCOV_fclose (gcov_var.file);
       gcov_var.file = 0;
       gcov_var.length = 0;
     }
@@ -217,7 +239,7 @@
 static void
 gcov_write_block (unsigned size)
 {
-  if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
+  if (_GCOV_fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
     gcov_var.error = 1;
   gcov_var.start += size;
   gcov_var.offset -= size;
@@ -413,7 +435,7 @@
 	gcov_allocate (gcov_var.length + words);
       excess = gcov_var.alloc - gcov_var.length;
 #endif
-      excess = fread (gcov_var.buffer + gcov_var.length,
+      excess = _GCOV_fread (gcov_var.buffer + gcov_var.length,
 		      1, excess << 2, gcov_var.file) >> 2;
       gcov_var.length += excess;
       if (gcov_var.length < words)
@@ -554,6 +576,10 @@
 GCOV_LINKAGE void
 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
 {
+#ifdef __GCOV_KERNEL__
+  /* should not reach this point */
+  gcc_assert (0);
+#else /* __GCOV_KERNEL__ */
   gcc_assert (gcov_var.mode > 0);
   base += length;
   if (base - gcov_var.start <= gcov_var.length)
@@ -561,9 +587,10 @@
   else
     {
       gcov_var.offset = gcov_var.length = 0;
-      fseek (gcov_var.file, base << 2, SEEK_SET);
-      gcov_var.start = ftell (gcov_var.file) >> 2;
+      _GCOV_fseek (gcov_var.file, base << 2, SEEK_SET);
+      gcov_var.start = _GCOV_ftell (gcov_var.file) >> 2;
     }
+#endif /* __GCOV_KERNEL__ */
 }
 #endif
 
@@ -576,8 +603,8 @@
   gcc_assert (gcov_var.mode < 0);
   if (gcov_var.offset)
     gcov_write_block (gcov_var.offset);
-  fseek (gcov_var.file, base << 2, SEEK_SET);
-  gcov_var.start = ftell (gcov_var.file) >> 2;
+  _GCOV_fseek (gcov_var.file, base << 2, SEEK_SET);
+  gcov_var.start = _GCOV_ftell (gcov_var.file) >> 2;
 }
 
 /* Truncate the gcov file at the current position.  */
@@ -585,6 +612,10 @@
 GCOV_LINKAGE void
 gcov_truncate (void)
 {
+#ifdef __GCOV_KERNEL__
+  /* should not reach this point */
+  gcc_assert (0);
+#else /* __GCOV_KERNEL__ */
   long offs;
   int filenum;
   gcc_assert (gcov_var.mode < 0);
@@ -594,6 +625,7 @@
   filenum = fileno (gcov_var.file);
   if (offs == -1 || filenum == -1 || ftruncate (filenum, offs))
     gcov_var.error = 1;
+#endif /* __GCOV_KERNEL__ */
 }
 #endif
 
@@ -611,3 +643,105 @@
     return status.st_mtime;
 }
 #endif /* IN_GCOV */
+
+#ifdef __GCOV_KERNEL__
+
+/* File fclose operation in kernel mode.  */
+
+int
+kernel_file_fclose (gcov_kernel_vfile *fp)
+{
+  return 0;
+}
+
+/* File ftell operation in kernel mode. It currently should not
+   be called.  */
+
+long
+kernel_file_ftell (gcov_kernel_vfile *fp)
+{
+  gcc_assert (0);  /* should not reach here */
+  return 0;
+}
+
+/* File fseek operation in kernel mode. It should only be called
+   with OFFSET==0 and WHENCE==0 to a freshly opened file.  */
+
+int
+kernel_file_fseek (gcov_kernel_vfile *fp, long offset, int whence)
+{
+  gcc_assert (offset == 0 && whence == 0 && fp->count == 0);
+  return 0;
+}
+
+/* File ftruncate operation in kernel mode. It currently should not
+   be called.  */
+
+int
+kernel_file_ftruncate (gcov_kernel_vfile *fp, off_t value)
+{
+  gcc_assert (0);  /* should not reach here */
+  return 0;
+}
+
+/* File fread operation in kernel mode. It currently should not
+   be called.  */
+
+int
+kernel_file_fread (void *ptr, size_t size, size_t nitems,
+                  gcov_kernel_vfile *fp)
+{
+  gcc_assert (0);  /* should not reach here */
+  return 0;
+}
+
+/* File fwrite operation in kernel mode. It outputs the data
+   to a buffer in the virual file.  */
+
+int
+kernel_file_fwrite (const void *ptr, size_t size,
+                   size_t nitems, gcov_kernel_vfile *fp)
+{
+  char *vbuf;
+  unsigned vsize, vpos;
+  unsigned len;
+
+  if (!fp) return 0;
+
+  vbuf = fp->buf;
+  vsize = fp->size;
+  vpos = fp->count;
+
+  if (vsize <= vpos)
+    {
+      printk (KERN_ERR
+          "GCOV_KERNEL: something wrong: vbuf=%p vsize=%u vpos=%u\n",
+          vbuf, vsize, vpos);
+      return 0;
+    }
+  len = vsize - vpos;
+  len /= size;
+
+  if (len > nitems)
+    len = nitems;
+
+  memcpy (vbuf+vpos, ptr, size*len);
+  fp->count += len*size;
+
+  if (len != nitems)
+    printk (KERN_ERR
+        "GCOV_KERNEL: something wrong: size=%lu nitems=%lu ret=%d\n",
+        size, nitems, len);
+  return len;
+}
+
+/* File fileno operation in kernel mode. It currently should not
+   be called.  */
+
+int
+kernel_file_fileno (gcov_kernel_vfile *fp)
+{
+  gcc_assert (0);  /* should not reach here */
+  return 0;
+}
+#endif /* GCOV_KERNEL */
Index: gcc/gcov-io.h
===================================================================
--- gcc/gcov-io.h	(revision 173717)
+++ gcc/gcov-io.h	(working copy)
@@ -163,6 +163,88 @@
 #ifndef GCC_GCOV_IO_H
 #define GCC_GCOV_IO_H
 
+#ifdef __KERNEL__
+#ifndef __GCOV_KERNEL__
+#define __GCOV_KERNEL__
+#endif /* __GCOV_KERNEL__ */
+#endif /* __KERNEL__ */
+
+#ifdef __GCOV_KERNEL__
+#define GCOV_LINKAGE /* nothing */
+
+/* We need the definitions for
+    BITS_PER_UNIT and
+    LONG_LONG_TYPE_SIZE
+  They are defined in gcc/defaults.h and gcc/config/<arch_depend_files>
+  (like, gcc/config/i386/i386.h). And it can be overridden by setting 
+  in build scripts. Here I hardcoded the value for x86.  
+  Todo: using a program to auto-generate the vaules in build time.  */
+#define BITS_PER_UNIT 8
+#define LONG_LONG_TYPE_SIZE 64
+
+/* There are many gcc_assertions. Set the vaule to 1 if we want a warning
+   message if the assertion fails.  */
+#ifndef ENABLE_ASSERT_CHECKING
+#define ENABLE_ASSERT_CHECKING 1
+#endif
+
+#include <linux/fs.h>
+#endif /* __GCOV_KERNEL__ */
+
+/* Wrappers to the file operations.  */
+#ifndef __GCOV_KERNEL__
+# define _GCOV_FILE      FILE
+# define _GCOV_fclose    fclose
+# define _GCOV_ftell     ftell
+# define _GCOV_fseek     fseek
+# define _GCOV_ftruncate ftruncate
+# define _GCOV_fread     fread
+# define _GCOV_fwrite    fwrite
+# define _GCOV_fread     fread
+# define _GCOV_fileno    fileno
+#else /* __GCOV_KERNEL__ */
+/* In Linux kernel mode, a virtual file is used for file operations.  */
+struct gcov_info;
+typedef struct {
+  long size; /* size of buf */
+  long count; /* element written into buf */
+  struct gcov_info *info;
+  char buf[0];
+} gcov_kernel_vfile;
+
+# define _GCOV_FILE gcov_kernel_vfile
+
+/* gcc_assert() prints out a warning if the check fails. It
+   will not abort.  */
+#if ENABLE_ASSERT_CHECKING
+# define gcc_assert(EXPR) \
+    ((void)(!(EXPR) ? printk (KERN_WARNING \
+      "GCOV assertion fails: func=%s line=%d\n", \
+      __FUNCTION__, __LINE__), 0 : 0))
+#else
+# define gcc_assert(EXPR) ((void)(0 && (EXPR)))
+#endif
+
+/* Wrappers to the file operations.  */
+# define _GCOV_fclose     kernel_file_fclose
+# define _GCOV_ftell      kernel_file_ftell
+# define _GCOV_fseek      kernel_file_fseek
+# define _GCOV_ftruncate  kernel_file_ftruncate
+# define _GCOV_fread      kernel_file_fread
+# define _GCOV_fwrite     kernel_file_fwrite
+# define _GCOV_fileno     kernel_file_fileno
+
+/* Declarations for virtual files operations.  */
+extern int kernel_file_fclose (gcov_kernel_vfile *);
+extern long kernel_file_ftell (gcov_kernel_vfile *);
+extern int kernel_file_fseek (gcov_kernel_vfile *, long, int);
+extern int kernel_file_ftruncate (gcov_kernel_vfile *, off_t);
+extern int kernel_file_fread (void *, size_t, size_t,
+    gcov_kernel_vfile *);
+extern int kernel_file_fwrite (const void *, size_t, size_t,
+    gcov_kernel_vfile *);
+extern int kernel_file_fileno(gcov_kernel_vfile *);
+#endif /* GCOV_KERNEL */
 #if IN_LIBGCOV
 
 #undef FUNC_ID_WIDTH
@@ -264,7 +346,9 @@
    is not also used in a DSO.  */
 #if IN_LIBGCOV
 
+#ifndef __GCOV_KERNEL__
 #include "tconfig.h"
+#endif /* __GCOV_KERNEL__ */
 
 #define gcov_var __gcov_var
 #define gcov_open __gcov_open
@@ -609,9 +693,9 @@
 /* Optimum number of gcov_unsigned_t's read from or written to disk.  */
 #define GCOV_BLOCK_SIZE (1 << 10)
 
-GCOV_LINKAGE struct gcov_var
+struct gcov_var
 {
-  FILE *file;
+  _GCOV_FILE *file;
   gcov_position_t start;	/* Position of first byte of block */
   unsigned offset;		/* Read/write position within the block.  */
   unsigned length;		/* Read limit in the block.  */
@@ -631,8 +715,16 @@
   size_t alloc;
   gcov_unsigned_t *buffer;
 #endif
-} gcov_var ATTRIBUTE_HIDDEN;
+};
 
+/* In kernel mode, move gcov_var definition to gcov-io.c
+   to avoid dulipcate definitions.  */
+#ifndef __GCOV_KERNEL__
+GCOV_LINKAGE struct gcov_var gcov_var ATTRIBUTE_HIDDEN;
+#else
+extern struct gcov_var gcov_var;
+#endif
+
 /* Functions for reading and writing gcov files. In libgcov you can
    open the file for reading then writing. Elsewhere you can open the
    file either for reading or for writing. When reading a file you may
@@ -679,6 +771,7 @@
 static void gcov_rewrite (void);
 GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
 GCOV_LINKAGE void gcov_truncate (void) ATTRIBUTE_HIDDEN;
+GCOV_LINKAGE unsigned gcov_gcda_file_size (struct gcov_info *);
 #else
 /* Available outside libgcov */
 GCOV_LINKAGE const char *gcov_read_string (void);
@@ -729,7 +822,7 @@
   gcov_var.mode = -1;
   gcov_var.start = 0;
   gcov_var.offset = 0;
-  fseek (gcov_var.file, 0L, SEEK_SET);
+  _GCOV_fseek (gcov_var.file, 0L, SEEK_SET);
 }
 #endif
 
Index: gcc/coverage.c
===================================================================
--- gcc/coverage.c	(revision 173717)
+++ gcc/coverage.c	(working copy)
@@ -1949,6 +1949,10 @@
   *tail = e;
 }
 
+extern bool is_kernel_build;
+
+#define KERNEL_BUILD_PREDEF_STRING "__KERNEL__"
+
 /* Copies the macro def or undef CPP_DEF and saves the copy
    in a list. IS_DEF is a flag indicating if CPP_DEF represents
    a -D or -U.  */
@@ -1961,6 +1965,11 @@
   strcpy (s + 1, cpp_def);
   str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
   num_cpp_defines++;
+
+  /* When -D__KERNEL__ is in the option list, we assume this is
+     compilation for Linux Kernel.  */
+  if (!strcmp(cpp_def, KERNEL_BUILD_PREDEF_STRING))
+    is_kernel_build = is_def;
 }
 
 /* Copies the -imacro/-include FILENAME and saves the copy in a list.  */
Index: gcc/tree-profile.c
===================================================================
--- gcc/tree-profile.c	(revision 173717)
+++ gcc/tree-profile.c	(working copy)
@@ -76,6 +76,10 @@
 static GTY(()) tree ptr_void;
 static GTY(()) tree gcov_info_decl;
 
+/* When -D__KERNEL__ is in the option list, we assume this is a
+   compilation for Linux Kernel.  */ 
+bool is_kernel_build;
+
 /* Do initialization work for the edge profiler.  */
 
 /* Add code:
@@ -102,7 +106,7 @@
 		      ptr_void);
       TREE_PUBLIC (ic_void_ptr_var) = 1;
       DECL_EXTERNAL (ic_void_ptr_var) = 1;
-      if (targetm.have_tls)
+      if (targetm.have_tls && !is_kernel_build)
         DECL_TLS_MODEL (ic_void_ptr_var) =
           decl_default_tls_model (ic_void_ptr_var);
 
@@ -113,7 +117,7 @@
 		      gcov_type_ptr);
       TREE_PUBLIC (ic_gcov_type_ptr_var) = 1;
       DECL_EXTERNAL (ic_gcov_type_ptr_var) = 1;
-      if (targetm.have_tls)
+      if (targetm.have_tls && !is_kernel_build)
         DECL_TLS_MODEL (ic_gcov_type_ptr_var) =
           decl_default_tls_model (ic_gcov_type_ptr_var);
     }
@@ -126,7 +130,7 @@
       TREE_STATIC (ic_void_ptr_var) = 1;
       TREE_PUBLIC (ic_void_ptr_var) = 0;
       DECL_INITIAL (ic_void_ptr_var) = NULL;
-      if (targetm.have_tls)
+      if (targetm.have_tls && !is_kernel_build)
         DECL_TLS_MODEL (ic_void_ptr_var) =
           decl_default_tls_model (ic_void_ptr_var);
 
@@ -138,7 +142,7 @@
       TREE_STATIC (ic_gcov_type_ptr_var) = 1;
       TREE_PUBLIC (ic_gcov_type_ptr_var) = 0;
       DECL_INITIAL (ic_gcov_type_ptr_var) = NULL;
-      if (targetm.have_tls)
+      if (targetm.have_tls && !is_kernel_build)
         DECL_TLS_MODEL (ic_gcov_type_ptr_var) =
           decl_default_tls_model (ic_gcov_type_ptr_var);
     }
@@ -318,7 +322,7 @@
       TREE_PUBLIC (gcov_sample_counter_decl) = 1;
       DECL_EXTERNAL (gcov_sample_counter_decl) = 1;
       DECL_ARTIFICIAL (gcov_sample_counter_decl) = 1;
-      if (targetm.have_tls)
+      if (targetm.have_tls && !is_kernel_build)
         DECL_TLS_MODEL (gcov_sample_counter_decl) =
             decl_default_tls_model (gcov_sample_counter_decl);
       assemble_variable (gcov_sample_counter_decl, 0, 0, 0);
@@ -1479,8 +1483,9 @@
 		      build_pointer_type (gcov_type_node));
       DECL_ARTIFICIAL (dc_gcov_type_ptr_var) = 1;
       DECL_EXTERNAL (dc_gcov_type_ptr_var) = 1;
-      DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
-	decl_default_tls_model (dc_gcov_type_ptr_var);
+      if (!is_kernel_build)
+        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
+	  decl_default_tls_model (dc_gcov_type_ptr_var);
 
       dc_void_ptr_var =
 	build_decl (UNKNOWN_LOCATION, VAR_DECL,
@@ -1488,8 +1493,9 @@
 		    ptr_void);
       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
       DECL_EXTERNAL (dc_void_ptr_var) = 1;
-      DECL_TLS_MODEL (dc_void_ptr_var) =
-	decl_default_tls_model (dc_void_ptr_var);
+      if (!is_kernel_build)
+        DECL_TLS_MODEL (dc_void_ptr_var) =
+	  decl_default_tls_model (dc_void_ptr_var);
     }
 
   add_referenced_var (gcov_info_decl);
Index: gcc/libgcov.c
===================================================================
--- gcc/libgcov.c	(revision 173717)
+++ gcc/libgcov.c	(working copy)
@@ -25,10 +25,29 @@
 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */
 
+/* Assume compiling for Linux Kernel if __KERNEL__ is defined.  */
+#ifdef __KERNEL__
+ /* Define MACROs to be used by kernel compilation.  */
+# define L_gcov
+# define L_gcov_interval_profiler
+# define L_gcov_pow2_profiler
+# define L_gcov_one_value_profiler
+# define L_gcov_indirect_call_profiler
+# define L_gcov_average_profiler
+# define L_gcov_ior_profiler
+
+# define TARGET_VTABLE_USES_DESCRIPTORS 0
+# define HAVE_CC_TLS 0
+# define __GCOV_KERNEL__
+
+# define IN_LIBGCOV 1
+# define IN_GCOV 0
+#else /* __KERNEL__ */
 #include "tconfig.h"
 #include "tsystem.h"
 #include "coretypes.h"
 #include "tm.h"
+#endif /* __KERNEL__ */
 
 #if 1
 #define THREAD_PREFIX __thread
@@ -36,6 +55,7 @@
 #define THREAD_PREFIX
 #endif
 
+#ifndef __GCOV_KERNEL__
 #if defined(inhibit_libc)
 #define IN_LIBGCOV (-1)
 #else
@@ -46,6 +66,8 @@
 #define GCOV_LINKAGE /* nothing */
 #endif
 #endif
+#endif /* __GCOV_KERNEL__ */
+
 #include "gcov-io.h"
 
 #if defined(inhibit_libc)
@@ -73,16 +95,35 @@
 
 #else
 
+#ifndef __GCOV_KERNEL__
 #include <string.h>
 #if GCOV_LOCKED
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/stat.h>
 #endif
+#endif /* __GCOV_KERNEL__ */
 
 #ifdef L_gcov
 #include "gcov-io.c"
 
+/* Utility function for outputing errors.  */
+static int
+gcov_error (const char *fmt, ...)
+{
+  int ret;
+  va_list argp;
+  va_start (argp, fmt);
+#ifdef __GCOV_KERNEL__
+  ret = vprintk (fmt, argp);
+#else
+  ret = vfprintf (stderr, fmt, argp);
+#endif
+  va_end (argp);
+  return ret;
+}
+
+#ifndef __GCOV_KERNEL__
 /* Sampling rate.  */
 extern gcov_unsigned_t __gcov_sampling_rate;
 static int gcov_sampling_rate_initialized = 0;
@@ -100,6 +141,10 @@
 /* Chain of per-object gcov structures.  */
 extern struct gcov_info *__gcov_list;
 
+/* Size of the longest file name. */
+static size_t gcov_max_filename = 0;
+#endif /* __GCOV_KERNEL__ */
+
 /* Unique identifier assigned to each module (object file).  */
 static gcov_unsigned_t gcov_cur_module_id = 0;
 
@@ -121,13 +166,45 @@
    object file included in multiple programs.  */
 static gcov_unsigned_t gcov_crc32;
 
-/* Size of the longest file name. */
-static size_t gcov_max_filename = 0;
-
 /* Dynamic call graph build and form module groups.  */
 void __gcov_compute_module_groups (void) ATTRIBUTE_HIDDEN;
 void __gcov_finalize_dyn_callgraph (void) ATTRIBUTE_HIDDEN;
 
+/* Profile summary for the gdca file, used in sanity check?  */
+static struct gcov_summary all;
+
+/* Profile summary for this program in current exeuction.  */
+static struct gcov_summary this_program;
+
+/* Profile summary for this object in current execuction.  */
+static struct gcov_summary this_object;
+
+/* Merged profile summary for this program.  */
+static struct gcov_summary program;
+
+/* Merged profile summary for this object.  */
+static struct gcov_summary object;
+
+/* Record the position of summary info.  */
+static gcov_position_t summary_pos = 0;
+
+/* Record the postion of eof.  */
+static gcov_position_t eof_pos = 0;
+
+/* Number of chars in prefix to be stripped.  */
+static int gcov_prefix_strip = 0;
+
+/* The length of path prefix.  */
+static size_t prefix_length = 0;
+
+/* gi_filename is current object filename.
+   gi_filename_up points to the stripped filename.  */
+static char *gi_filename, *gi_filename_up;
+
+static int gcov_open_by_filename (char * gi_filename);
+static int gcov_exit_init (void);
+static void gcov_dump_one_gcov (struct gcov_info *gi_ptr);
+
 /* Make sure path component of the given FILENAME exists, create
    missing directories. FILENAME must be writable.
    Returns zero on success, or -1 if an error occurred.  */
@@ -175,14 +252,47 @@
 #endif
 }
 
+/* Open a file with the specified name.  */
+
+static int
+gcov_open_by_filename (char * gi_filename)
+{
+  if (!gcov_open (gi_filename))
+    {
+      /* Open failed likely due to missed directory.
+         Create directory and retry to open file.  */
+      if (create_file_directory (gi_filename))
+        {
+          gcov_error ("profiling:%s:Skip\n", gi_filename);
+          return -1;
+        }
+      if (!gcov_open (gi_filename))
+        {
+          gcov_error ("profiling:%s:Cannot open\n", gi_filename);
+          return -1;
+        }
+    }
+  return 0;
+}
+
+
+/* Determine whether a counter is active.  */
+
+static inline int
+gcov_counter_active (const struct gcov_info *info, unsigned int type)
+{
+  return (1 << type) & info->ctr_mask;
+}
+
+#ifndef __GCOV_KERNEL__
 /* Check if VERSION of the info block PTR matches libgcov one.
    Return 1 on success, or zero in case of versions mismatch.
    If FILENAME is not NULL, its value used for reporting purposes
    instead of value from the info block.  */
 
 static int
-gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
-	      const char *filename)
+gcov_version (struct gcov_info *ptr __attribute__ ((unused)), 
+              gcov_unsigned_t version, const char *filename)
 {
   if (version != GCOV_VERSION)
     {
@@ -192,17 +302,17 @@
       GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
 
       if (filename)
-        fprintf (stderr,
-                 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
-                 filename, e, v);
+        gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
+                   filename, e, v);
       else
-        fprintf (stderr,
-                 "profiling:Version mismatch - expected %.4s got %.4s\n", e, v);
+        gcov_error ("profiling:Version mismatch - expected %.4s got %.4s\n", e, v);
       return 0;
     }
   return 1;
 }
 
+#define GCOV_GET_FILENAME gcov_strip_leading_dirs
+
 /* Strip GCOV_PREFIX_STRIP levels of leading '/' from FILENAME and
    put the result into GI_FILENAME_UP.  */
 
@@ -298,7 +408,7 @@
   FILE *imports_file;
   size_t prefix_length, suffix_length;
 
-  gcov_suffix = getenv("GCOV_IMPORTS_SUFFIX");
+  gcov_suffix = getenv ("GCOV_IMPORTS_SUFFIX");
   if (!gcov_suffix || !strlen (gcov_suffix))
     gcov_suffix = ".imports";
   suffix_length = strlen (gcov_suffix);
@@ -328,64 +438,26 @@
     }
 }
 
-/* Dump the coverage counts. We merge with existing counts when
-   possible, to avoid growing the .da files ad infinitum. We use this
-   program's checksum to make sure we only accumulate whole program
-   statistics to the correct summary. An object file might be embedded
-   in two separate programs, and we must keep the two program
-   summaries separate.  */
+/* This function allocates the space to store current file name.  */
 
 static void
-gcov_exit (void)
+gcov_alloc_filename (void)
 {
-  struct gcov_info *gi_ptr;
-  struct gcov_summary this_program;
-  struct gcov_summary all;
-  struct gcov_ctr_summary *cs_ptr;
-  const struct gcov_ctr_info *ci_ptr;
-  unsigned t_ix;
-  gcov_unsigned_t c_num;
-  const char *gcov_prefix;
-  int gcov_prefix_strip = 0;
-  size_t prefix_length;
-  char *gi_filename, *gi_filename_up;
-  int dump_module_info = 0;
+  /* Get file name relocation prefix.  Non-absolute values are ignored.  */
+  char *gcov_prefix = 0;
 
-  memset (&all, 0, sizeof (all));
-  /* Find the totals for this execution.  */
-  memset (&this_program, 0, sizeof (this_program));
-  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
-    {
-      ci_ptr = gi_ptr->counts;
-      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
-	{
-	  if (!((1 << t_ix) & gi_ptr->ctr_mask))
-	    continue;
+  prefix_length = 0;
+  gcov_prefix_strip = 0;
 
-	  cs_ptr = &this_program.ctrs[t_ix];
-	  cs_ptr->num += ci_ptr->num;
-	  for (c_num = 0; c_num < ci_ptr->num; c_num++)
-	    {
-      	      cs_ptr->sum_all += ci_ptr->values[c_num];
-	      if (cs_ptr->run_max < ci_ptr->values[c_num])
-		cs_ptr->run_max = ci_ptr->values[c_num];
-	    }
-	  ci_ptr++;
-	}
-      /* The IS_PRIMARY field is overloaded to indicate if this module
-	 is FDO/LIPO.  */
-      dump_module_info |= gi_ptr->mod_info->is_primary;
-    }
-
   {
     /* Check if the level of dirs to strip off specified. */
     char *tmp = getenv ("GCOV_PREFIX_STRIP");
     if (tmp)
       {
-	gcov_prefix_strip = atoi (tmp);
-	/* Do not consider negative values. */
-	if (gcov_prefix_strip < 0)
-	  gcov_prefix_strip = 0;
+        gcov_prefix_strip = atoi (tmp);
+        /* Do not consider negative values. */
+        if (gcov_prefix_strip < 0)
+          gcov_prefix_strip = 0;
       }
   }
   /* Get file name relocation prefix.  Non-absolute values are ignored. */
@@ -396,7 +468,7 @@
 
       /* Remove an unnecessary trailing '/' */
       if (IS_DIR_SEPARATOR (gcov_prefix[prefix_length - 1]))
-	prefix_length--;
+        prefix_length--;
     }
   else
     prefix_length = 0;
@@ -408,259 +480,527 @@
       gcov_prefix = ".";
       prefix_length = 1;
     }
-  /* Allocate and initialize the filename scratch space plus one.  */
-  gi_filename = (char *) alloca (prefix_length + gcov_max_filename + 2);
+
+  /* Aelocate and initialize the filename scratch space.  */
+  gi_filename = (char *) malloc (prefix_length + gcov_max_filename + 2);
   if (prefix_length)
     memcpy (gi_filename, gcov_prefix, prefix_length);
-  gi_filename_up = gi_filename + prefix_length;
+}
 
-  /* Now merge each file.  */
+static void
+gcov_dump_module_info (void)
+{
+  struct gcov_info *gi_ptr;
+
+  __gcov_compute_module_groups ();
+
+  /* Now write out module group info.  */
   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
+  {
+    int error;
+
+    gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip, 
+                             gi_ptr->filename, gi_filename_up);
+    error = gcov_open_by_filename (gi_filename);
+    if (error != 0)
+      continue;
+
+    /* Overwrite the zero word at the of the file.  */
+    gcov_rewrite ();
+    gcov_seek (gi_ptr->eof_pos);
+
+    gcov_write_module_infos (gi_ptr);
+    gcov_truncate ();
+
+    if ((error = gcov_close ()))
+         gcov_error (error  < 0 ?  "profiling:%s:Overflow writing\n" :
+                                   "profiling:%s:Error writing\n",
+                                   gi_filename);
+    gcov_write_import_file (gi_filename, gi_ptr);
+  }
+  __gcov_finalize_dyn_callgraph ();
+}
+
+/* Dump the coverage counts. We merge with existing counts when
+   possible, to avoid growing the .da files ad infinitum. We use this
+   program's checksum to make sure we only accumulate whole program
+   statistics to the correct summary. An object file might be embedded
+   in two separate programs, and we must keep the two program
+   summaries separate.  */
+
+static void
+gcov_exit (void)
+{
+  struct gcov_info *gi_ptr;
+  int dump_module_info;
+
+  dump_module_info = gcov_exit_init ();
+
+
+  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
+    gcov_dump_one_gcov (gi_ptr);
+
+  if (dump_module_info)
+    gcov_dump_module_info ();
+
+  free (gi_filename);
+}
+
+/* Add a new object file onto the bb chain.  Invoked automatically
+   when running an object file's global ctors.  */
+
+void
+__gcov_init (struct gcov_info *info)
+{
+  if (!gcov_sampling_rate_initialized)
     {
-      struct gcov_summary this_object;
-      struct gcov_summary object, program;
-      gcov_type *values[GCOV_COUNTERS];
-      const struct gcov_fn_info *fi_ptr;
-      unsigned fi_stride;
-      unsigned c_ix, f_ix, n_counts;
-      struct gcov_ctr_summary *cs_obj, *cs_tobj, *cs_prg, *cs_tprg, *cs_all;
-      int error = 0;
-      gcov_unsigned_t tag, length;
-      gcov_position_t summary_pos = 0;
-      gcov_position_t eof_pos = 0;
-      const char *fname, *s;
+      const char* env_value_str = getenv ("GCOV_SAMPLING_RATE");
+      if (env_value_str)
+        {
+          int env_value_int = atoi(env_value_str);
+          if (env_value_int >= 1)
+            __gcov_sampling_rate = env_value_int;
+        }
+      gcov_sampling_rate_initialized = 1;
+    }
 
-      fname = gi_ptr->filename;
+  if (!info->version)
+    return;
 
-      memset (&this_object, 0, sizeof (this_object));
-      memset (&object, 0, sizeof (object));
+  if (gcov_version (info, info->version, 0))
+    {
+      const char *ptr = info->filename;
+      gcov_unsigned_t crc32 = gcov_crc32;
+      size_t filename_length = strlen (info->filename);
 
-      gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip,
-			       gi_ptr->filename, gi_filename_up);
+      /* Refresh the longest file name information.  */
+      if (filename_length > gcov_max_filename)
+        gcov_max_filename = filename_length;
 
-      /* Totals for this object file.  */
-      ci_ptr = gi_ptr->counts;
-      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
+      /* Assign the module ID (starting at 1).  */
+      info->mod_info->ident = (++gcov_cur_module_id);
+      gcc_assert (EXTRACT_MODULE_ID_FROM_GLOBAL_ID (GEN_FUNC_GLOBAL_ID (
+                                                       info->mod_info->ident, 0))
+                  == info->mod_info->ident);
+
+      do
 	{
-	  if (!((1 << t_ix) & gi_ptr->ctr_mask))
-	    continue;
+	  unsigned ix;
+	  gcov_unsigned_t value = *ptr << 24;
 
-	  cs_ptr = &this_object.ctrs[t_ix];
-	  cs_ptr->num += ci_ptr->num;
-	  for (c_num = 0; c_num < ci_ptr->num; c_num++)
+	  for (ix = 8; ix--; value <<= 1)
 	    {
-	      cs_ptr->sum_all += ci_ptr->values[c_num];
-	      if (cs_ptr->run_max < ci_ptr->values[c_num])
-		cs_ptr->run_max = ci_ptr->values[c_num];
+	      gcov_unsigned_t feedback;
+
+	      feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
+	      crc32 <<= 1;
+	      crc32 ^= feedback;
 	    }
-	  ci_ptr++;
-	}
+	} while (*ptr++);
 
-      c_ix = 0;
-      for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
-	if ((1 << t_ix) & gi_ptr->ctr_mask)
+      gcov_crc32 = crc32;
+
+      if (!__gcov_list)
+        atexit (gcov_exit);
+
+      info->next = __gcov_list;
+      __gcov_list = info;
+    }
+  info->version = 0;
+}
+
+/* Called before fork or exec - write out profile information gathered so
+   far and reset it to zero.  This avoids duplication or loss of the
+   profile information gathered so far.  */
+
+void
+__gcov_flush (void)
+{
+  const struct gcov_info *gi_ptr;
+
+  gcov_exit ();
+  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
+    {
+      unsigned t_ix;
+      const struct gcov_ctr_info *ci_ptr;
+
+      for (t_ix = 0, ci_ptr = gi_ptr->counts; t_ix != GCOV_COUNTERS; t_ix++)
+        if (gcov_counter_active (gi_ptr, t_ix))
 	  {
-	    values[c_ix] = gi_ptr->counts[c_ix].values;
-            if (t_ix == GCOV_COUNTER_ICALL_TOPNV)
-              gcov_sort_icall_topn_counter (&gi_ptr->counts[c_ix]);
-	    c_ix++;
+	    memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
+	    ci_ptr++;
 	  }
+    }
+}
 
-      /* Calculate the function_info stride. This depends on the
-	 number of counter types being measured.  */
-      fi_stride = offsetof (struct gcov_fn_info, n_ctrs)
-	+ c_ix * sizeof (unsigned);
-      if (__alignof__ (struct gcov_fn_info) > sizeof (unsigned))
-	{
-	  fi_stride += __alignof__ (struct gcov_fn_info) - 1;
-	  fi_stride &= ~(__alignof__ (struct gcov_fn_info) - 1);
-	}
+#else /* __GCOV_KERNEL__ */
 
-      if (!gcov_open (gi_filename))
-	{
-	  /* Open failed likely due to missed directory.
-	     Create directory and retry to open file. */
-          if (create_file_directory (gi_filename))
-	    {
-	      fprintf (stderr, "profiling:%s:Skip\n", gi_filename);
-	      continue;
-	    }
-	  if (!gcov_open (gi_filename))
-	    {
-              fprintf (stderr, "profiling:%s:Cannot open\n", gi_filename);
-	      continue;
-	    }
-	}
+#define GCOV_GET_FILENAME gcov_get_filename
 
-      tag = gcov_read_unsigned ();
-      if (tag)
-	{
-	  /* Merge data from file.  */
-	  if (tag != GCOV_DATA_MAGIC)
-	    {
-	      fprintf (stderr, "profiling:%s:Not a gcov data file\n",
-		       gi_filename);
-	      goto read_fatal;
-	    }
-	  length = gcov_read_unsigned ();
-	  if (!gcov_version (gi_ptr, length, gi_filename))
-	    goto read_fatal;
+/* Copy the filename to the buffer.  */
 
-	  length = gcov_read_unsigned ();
-	  if (length != gi_ptr->stamp)
-	    /* Read from a different compilation. Overwrite the file.  */
-	    goto rewrite;
+static inline void
+gcov_get_filename (int prefix_length __attribute__ ((unused)), 
+                   int gcov_prefix_strip __attribute__ ((unused)), 
+                   const char *filename, char *gi_filename_up)
+{
+    strcpy (gi_filename_up, filename);
+}
 
-	  /* Merge execution counts for each function.  */
-	  for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
-	    {
-	      fi_ptr = (const struct gcov_fn_info *)
-		      ((const char *) gi_ptr->functions + f_ix * fi_stride);
-	      tag = gcov_read_unsigned ();
-	      length = gcov_read_unsigned ();
+/* Sort the profile counters for all indirect call sites. Counters
+   for each call site are allocated in array COUNTERS.  */
 
-	      /* Check function.  */
-	      if (tag != GCOV_TAG_FUNCTION
-	          || length != GCOV_TAG_FUNCTION_LENGTH
-		  || gcov_read_unsigned () != fi_ptr->ident
-		  || gcov_read_unsigned () != fi_ptr->lineno_checksum
-		  || gcov_read_unsigned () != fi_ptr->cfg_checksum)
-		{
-		read_mismatch:;
-		  fprintf (stderr, "profiling:%s:Merge mismatch for %s\n",
-			   gi_filename,
-			   f_ix + 1 ? "function" : "summaries");
-		  goto read_fatal;
-		}
+static void
+gcov_sort_icall_topn_counter (const struct gcov_ctr_info *counters)
+{
+  /* Empty */
+}
 
-	      c_ix = 0;
-	      for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
-		{
-		  gcov_merge_fn merge;
+/* Reserves a buffer to store the name of the file being processed.  */
+static char _kernel_gi_filename[520];
 
-		  if (!((1 << t_ix) & gi_ptr->ctr_mask))
-		    continue;
+/* This function allocates the space to store current file name.  */
 
-		  n_counts = fi_ptr->n_ctrs[c_ix];
-		  merge = gi_ptr->counts[c_ix].merge;
+static void
+gcov_alloc_filename (void)
+{
+  prefix_length = 0;
+  gcov_prefix_strip = 0;
+  gi_filename = _kernel_gi_filename;
+}
 
-		  tag = gcov_read_unsigned ();
-		  length = gcov_read_unsigned ();
-		  if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
-		      || length != GCOV_TAG_COUNTER_LENGTH (n_counts))
-		    goto read_mismatch;
-		  (*merge) (values[c_ix], n_counts);
-		  values[c_ix] += n_counts;
-		  c_ix++;
-		}
-	      if ((error = gcov_is_error ()))
-		goto read_error;
-	    }
+#endif /* __GCOV_KERNEL__ */
 
-	  f_ix = ~0u;
-	  /* Check program & object summary */
-	  while (1)
-	    {
-	      int is_program;
+/* Determine number of active counters in gcov_info INFO,
+   the counter arrays are stored in VALUES if the coming
+   value of VALUES !=0. If FLAG_SORT_ICALL_TOPN_COUNTER !=0,
+   the icall_topn_counter in INFO will be sorted.
+   Return: the number of active counter types.  */
 
-	      eof_pos = gcov_position ();
-	      tag = gcov_read_unsigned ();
-	      if (!tag)
-		break;
+static unsigned int
+gcov_counter_array (const struct gcov_info *info,
+                    gcov_type *values[GCOV_COUNTERS],
+                    int flag_sort_icall_topn_counter)
+{
+  unsigned int i;
+  unsigned int result = 0;
 
-	      length = gcov_read_unsigned ();
-	      is_program = tag == GCOV_TAG_PROGRAM_SUMMARY;
-	      if (length != GCOV_TAG_SUMMARY_LENGTH
-		  || (!is_program && tag != GCOV_TAG_OBJECT_SUMMARY))
-		goto read_mismatch;
-	      gcov_read_summary (is_program ? &program : &object);
-	      if ((error = gcov_is_error ()))
-		goto read_error;
-	      if (is_program && program.checksum == gcov_crc32)
-		{
-		  summary_pos = eof_pos;
-		  goto rewrite;
-		}
-	    }
-	}
-      goto rewrite;
+  for (i = 0; i < GCOV_COUNTERS; i++) {
+    if (gcov_counter_active (info, i))
+      {
+        if (values)
+          values[result] = info->counts[result].values;
+        if (flag_sort_icall_topn_counter &&
+            (i == GCOV_COUNTER_ICALL_TOPNV))
+          gcov_sort_icall_topn_counter (&info->counts[result]);
+        result++;
+      }
+  }
+  return result;
+}
 
-    read_error:;
-      fprintf (stderr, error < 0 ? "profiling:%s:Overflow merging\n"
-	       : "profiling:%s:Error merging\n", gi_filename);
+/* Compute object summary recored in gcov_info INFO. The result is
+   stored in OBJ_SUM. Note that the caller is responsible for
+   zeroing out OBJ_SUM, otherwise the summary is accumulated.  */
 
-    read_fatal:;
-      gcov_close ();
-      continue;
+static void
+gcov_object_summary (struct gcov_info *info,
+                     struct gcov_summary *obj_sum)
+{
+  const struct gcov_ctr_info *ci_ptr;
+  struct gcov_ctr_summary *cs_ptr;
+  gcov_unsigned_t c_num;
+  unsigned t_ix;
 
-    rewrite:;
-      gcov_rewrite ();
-      if (!summary_pos)
-	memset (&program, 0, sizeof (program));
+  /* Totals for this object file.  */
+  ci_ptr = info->counts;
+  for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
+    {
+      if (!gcov_counter_active (info, t_ix))
+        continue;
 
-      /* Merge the summaries.  */
-      f_ix = ~0u;
-      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
-	{
-	  cs_obj = &object.ctrs[t_ix];
-	  cs_tobj = &this_object.ctrs[t_ix];
-	  cs_prg = &program.ctrs[t_ix];
-	  cs_tprg = &this_program.ctrs[t_ix];
-	  cs_all = &all.ctrs[t_ix];
+      cs_ptr = &(obj_sum->ctrs[t_ix]);
+      cs_ptr->num += ci_ptr->num;
+      for (c_num = 0; c_num < ci_ptr->num; c_num++)
+        {
+          cs_ptr->sum_all += ci_ptr->values[c_num];
+          if (cs_ptr->run_max < ci_ptr->values[c_num])
+            cs_ptr->run_max = ci_ptr->values[c_num];
+        }
+      ci_ptr++;
+    }
+}
 
-	  if ((1 << t_ix) & gi_ptr->ctr_mask)
-	    {
-	      if (!cs_obj->runs++)
-		cs_obj->num = cs_tobj->num;
-	      else if (cs_obj->num != cs_tobj->num)
-		goto read_mismatch;
-	      cs_obj->sum_all += cs_tobj->sum_all;
-	      if (cs_obj->run_max < cs_tobj->run_max)
-		cs_obj->run_max = cs_tobj->run_max;
-	      cs_obj->sum_max += cs_tobj->run_max;
+/* Merge with existing gcda file in the same directory to avoid
+   excessive growthe of the files.  */
 
-	      if (!cs_prg->runs++)
-		cs_prg->num = cs_tprg->num;
-	      else if (cs_prg->num != cs_tprg->num)
-		goto read_mismatch;
-	      cs_prg->sum_all += cs_tprg->sum_all;
-	      if (cs_prg->run_max < cs_tprg->run_max)
-		cs_prg->run_max = cs_tprg->run_max;
-	      cs_prg->sum_max += cs_tprg->run_max;
-	    }
-	  else if (cs_obj->num || cs_prg->num)
-	    goto read_mismatch;
+static int
+gcov_merge_gcda_file (struct gcov_info *info,
+                      gcov_type *values[GCOV_COUNTERS],
+                      unsigned fi_stride)
+{
+  struct gcov_ctr_summary *cs_obj, *cs_tobj, *cs_prg, *cs_tprg, *cs_all;
+  unsigned t_ix, f_ix;
 
-	  if (!cs_all->runs && cs_prg->runs)
-	    memcpy (cs_all, cs_prg, sizeof (*cs_all));
-	  else if (!all.checksum
-		   && (!GCOV_LOCKED || cs_all->runs == cs_prg->runs)
-		   && memcmp (cs_all, cs_prg, sizeof (*cs_all)))
-	    {
-	      fprintf (stderr, "profiling:%s:Invocation mismatch - some data files may have been removed%s",
-		       gi_filename, GCOV_LOCKED
-		       ? "" : " or concurrent update without locking support");
-	      all.checksum = ~0u;
-	    }
-	}
+#ifndef __GCOV_KERNEL__
+  const struct gcov_fn_info *fi_ptr;
+  unsigned c_ix, n_counts;
+  int error = 0;
+  gcov_unsigned_t tag, length;
 
+  tag = gcov_read_unsigned ();
+  if (tag)
+    {
+      /* Merge data from file.  */
+      if (tag != GCOV_DATA_MAGIC)
+        {
+          gcov_error ("profiling:%s:Not a gcov data file\n", gi_filename);
+          goto read_fatal;
+        }
+     length = gcov_read_unsigned ();
+     if (!gcov_version (info, length, gi_filename))
+       goto read_fatal;
+
+     length = gcov_read_unsigned ();
+     if (length != info->stamp)
+       /* Read from a different compilation. Overwrite the file.  */
+       goto rewrite;
+
+     /* Merge execution counts for each function.  */
+     for (f_ix = 0; f_ix < info->n_functions; f_ix++)
+       {
+         fi_ptr = (const struct gcov_fn_info *)
+                   ((const char *) info->functions + f_ix * fi_stride);
+         tag = gcov_read_unsigned ();
+         length = gcov_read_unsigned ();
+
+         /* Check function.  */
+         if (tag != GCOV_TAG_FUNCTION
+	     || length != GCOV_TAG_FUNCTION_LENGTH
+             || gcov_read_unsigned () != fi_ptr->ident
+             || gcov_read_unsigned () != fi_ptr->lineno_checksum
+             || gcov_read_unsigned () != fi_ptr->cfg_checksum)
+           goto read_mismatch;
+
+           c_ix = 0;
+           for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
+             {
+               gcov_merge_fn merge;
+
+               if (!((1 << t_ix) & info->ctr_mask))
+                 continue;
+
+               n_counts = fi_ptr->n_ctrs[c_ix];
+               merge = info->counts[c_ix].merge;
+
+               tag = gcov_read_unsigned ();
+               length = gcov_read_unsigned ();
+               if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
+                   || length != GCOV_TAG_COUNTER_LENGTH (n_counts))
+                 goto read_mismatch;
+               (*merge) (values[c_ix], n_counts);
+               values[c_ix] += n_counts;
+               c_ix++;
+             }
+           if ((error = gcov_is_error ()))
+             goto read_error;
+       }
+
+       f_ix = ~0u;
+       /* Check program & object summary.  */
+       while (1)
+         {
+           int is_program;
+
+           eof_pos = gcov_position ();
+           tag = gcov_read_unsigned ();
+           if (!tag)
+             break;
+
+           length = gcov_read_unsigned ();
+           is_program = tag == GCOV_TAG_PROGRAM_SUMMARY;
+           if (length != GCOV_TAG_SUMMARY_LENGTH
+               || (!is_program && tag != GCOV_TAG_OBJECT_SUMMARY))
+             goto read_mismatch;
+           gcov_read_summary (is_program ? &program : &object);
+           if ((error = gcov_is_error ()))
+             goto read_error;
+           if (is_program && program.checksum == gcov_crc32)
+             {
+               summary_pos = eof_pos;
+               goto rewrite;
+             }
+         }
+    }
+
+    goto rewrite;
+
+read_error:;
+    gcov_error (error < 0 ? "profiling:%s:Overflow merging\n"
+                : "profiling:%s:Error merging\n", gi_filename);
+    goto read_fatal;
+
+#endif /* __GCOV_KERNEL__ */
+
+    goto rewrite;
+
+read_mismatch:;
+    gcov_error ("profiling:%s:Merge mismatch for %s\n", gi_filename,
+                 f_ix + 1 ? "function" : "summaries");
+    goto read_fatal; /* work-around the compiler warning */
+
+read_fatal:;
+    gcov_close ();
+    return 1;
+
+rewrite:;
+    gcov_rewrite ();
+    if (!summary_pos)
+      memset (&program, 0, sizeof (program));
+
+    /* Merge the summaries.  */
+    f_ix = ~0u;
+    for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
+      {
+        cs_obj = &object.ctrs[t_ix];
+        cs_tobj = &this_object.ctrs[t_ix];
+        cs_prg = &program.ctrs[t_ix];
+        cs_tprg = &this_program.ctrs[t_ix];
+        cs_all = &all.ctrs[t_ix];
+
+        if ((1 << t_ix) & info->ctr_mask)
+          {
+            if (!cs_obj->runs++)
+              cs_obj->num = cs_tobj->num;
+            else if (cs_obj->num != cs_tobj->num)
+               goto read_mismatch;
+            cs_obj->sum_all += cs_tobj->sum_all;
+            if (cs_obj->run_max < cs_tobj->run_max)
+              cs_obj->run_max = cs_tobj->run_max;
+            cs_obj->sum_max += cs_tobj->run_max;
+
+            if (!cs_prg->runs++)
+              cs_prg->num = cs_tprg->num;
+            else if (cs_prg->num != cs_tprg->num)
+              goto read_mismatch;
+            cs_prg->sum_all += cs_tprg->sum_all;
+            if (cs_prg->run_max < cs_tprg->run_max)
+              cs_prg->run_max = cs_tprg->run_max;
+            cs_prg->sum_max += cs_tprg->run_max;
+          }
+        else if (cs_obj->num || cs_prg->num)
+          goto read_mismatch;
+
+        if (!cs_all->runs && cs_prg->runs)
+          memcpy (cs_all, cs_prg, sizeof (*cs_all));
+        else if (!all.checksum
+                 && (!GCOV_LOCKED || cs_all->runs == cs_prg->runs)
+                 && memcmp (cs_all, cs_prg, sizeof (*cs_all)))
+          {
+            gcov_error ("profiling:%s:Invocation mismatch - "
+                "some data files may have been removed%s",
+            gi_filename, GCOV_LOCKED
+            ? "" : " or concurrent update without locking support");
+            all.checksum = ~0u;
+          }
+      }
+
+  return 0;
+}
+
+/* Calculate the function_info stride. This depends on the
+   number of counter types being measured.
+   NUM_COUNTER_TYPES is number of counter types recorded.
+   Return: the number of bytes for accessing next fn_info
+   (aligned to gcov_fn_info).  */
+
+static unsigned
+gcov_compute_fi_stride (unsigned num_counter_types)
+{
+   unsigned fi_stride;
+
+   fi_stride = offsetof (struct gcov_fn_info, n_ctrs) +
+               num_counter_types * sizeof (unsigned);
+   if (__alignof__ (struct gcov_fn_info) > sizeof (unsigned))
+   {
+     fi_stride += __alignof__ (struct gcov_fn_info) - 1;
+     fi_stride &= ~(__alignof__ (struct gcov_fn_info) - 1);
+   }
+   return fi_stride;
+}
+
+/* This function returns the size of gcda file to be written. Note
+   the size is in units of gcov_type.  */
+
+GCOV_LINKAGE unsigned
+gcov_gcda_file_size (struct gcov_info *gi_ptr)
+{
+  unsigned size;
+  const struct gcov_fn_info *fi_ptr;
+  unsigned f_ix, t_ix, c_ix;
+  unsigned n_counts;
+  unsigned fi_stride;
+  gcov_type *values[GCOV_COUNTERS];
+
+  c_ix = gcov_counter_array (gi_ptr, values, 0);
+  fi_stride = gcov_compute_fi_stride (c_ix);
+
+  /* GCOV_DATA_MAGIC, GCOV_VERSION and time_stamp.  */
+  size = 3;
+
+  /* size for each function.  */
+  for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
+    {
+      fi_ptr = (const struct gcov_fn_info *)
+        ((const char *) gi_ptr->functions + f_ix * fi_stride);
+
+      size += 2 /* tag_length itself */
+              + GCOV_TAG_FUNCTION_LENGTH; /* ident, lineno_cksum, cfg_cksm */
+
       c_ix = 0;
       for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
-	if ((1 << t_ix) & gi_ptr->ctr_mask)
-	  {
-	    values[c_ix] = gi_ptr->counts[c_ix].values;
-	    c_ix++;
-	  }
+        {
+          if (!((1 << t_ix) & gi_ptr->ctr_mask))
+            continue;
 
-      program.checksum = gcov_crc32;
+          n_counts = fi_ptr->n_ctrs[c_ix];
+          size += 2 + GCOV_TAG_COUNTER_LENGTH (n_counts);
+          c_ix++;
+        }
+    }
 
+  /* Object summary.  */
+  size += 2 + GCOV_TAG_SUMMARY_LENGTH;
+
+  /* Program summary.  */
+  size += 2 + GCOV_TAG_SUMMARY_LENGTH;
+
+  size += 1;
+
+  return size*4;
+}
+
+/* Write profile data (including summary and module grouping information,
+   if available, to file.  */
+
+static void
+gcov_write_gcda_file (struct gcov_info *gi_ptr,
+                      unsigned fi_stride)
+{
+      const struct gcov_fn_info *fi_ptr;
+      gcov_type *values[GCOV_COUNTERS];
+      unsigned t_ix, c_ix, f_ix, n_counts;
+      int error = 0;
+
       /* Write out the data.  */
       gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
       gcov_write_unsigned (gi_ptr->stamp);
 
+      gcov_counter_array (gi_ptr, values, 0);
+
       /* Write execution counts for each function.  */
       for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
-	{
+        {
 	  fi_ptr = (const struct gcov_fn_info *)
 		  ((const char *) gi_ptr->functions + f_ix * fi_stride);
 
@@ -689,12 +1029,13 @@
 	      values[c_ix] = c_ptr;
 	      c_ix++;
 	    }
-	}
+        }
 
       /* Object file summary.  */
       gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, &object);
 
       /* Generate whole program statistics.  */
+      program.checksum = gcov_crc32;
       if (eof_pos)
 	gcov_seek (eof_pos);
       gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &program);
@@ -713,146 +1054,77 @@
         gi_ptr->eof_pos = gcov_position ();
 
       if ((error = gcov_close ()))
-	  fprintf (stderr, error  < 0 ?
+	  gcov_error (error  < 0 ?
 		   "profiling:%s:Overflow writing\n" :
 		   "profiling:%s:Error writing\n",
 		   gi_filename);
+}
 
-    }
+/* Do some preparation work before calling the actual dumping
+   routine.
+   Return: 1 when module grouping info needs to be dumped,
+           0 otherwise.  */
 
-  if (!dump_module_info)
-    return;
+static int
+gcov_exit_init (void)
+{
+  struct gcov_info *gi_ptr;
+  int dump_module_info = 0;
 
-   __gcov_compute_module_groups ();
+  dump_module_info = 0;
+  gcov_prefix_strip = 0;
 
-   /* Now write out module group info.  */
-   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
+  memset (&all, 0, sizeof (all));
+
+  /* Find the totals for this execution.  */
+  memset (&this_program, 0, sizeof (this_program));
+  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
     {
-      int error;
-      gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip,
-			       gi_ptr->filename, gi_filename_up);
+      gcov_object_summary (gi_ptr, &this_program);
 
-      if (!gcov_open (gi_filename))
-	{
-#ifdef TARGET_POSIX_IO
-	  /* Open failed likely due to missed directory.
-	     Create directory and retry to open file. */
-          if (create_file_directory (gi_filename))
-	    {
-	      fprintf (stderr, "profiling:%s:Skip\n", gi_filename);
-	      continue;
-	    }
-#endif
-	  if (!gcov_open (gi_filename))
-	    {
-              fprintf (stderr, "profiling:%s:Cannot open\n", gi_filename);
-	      continue;
-	    }
-	}
+      /* The IS_PRIMARY field is overloaded to indicate if this module
+         is FDO/LIPO.  */
+      dump_module_info |= gi_ptr->mod_info->is_primary;
+    }
 
-      /* Overwrite the zero word at the of the file.  */
-      gcov_rewrite ();
-      gcov_seek (gi_ptr->eof_pos);
+  gcov_alloc_filename ();
+  gi_filename_up = gi_filename + prefix_length;
 
-      gcov_write_module_infos (gi_ptr);
-      gcov_truncate ();
-
-      if ((error = gcov_close ()))
-	  fprintf (stderr, error  < 0 ?
-		   "profiling:%s:Overflow writing\n" :
-		   "profiling:%s:Error writing\n",
-		   gi_filename);
-      gcov_write_import_file (gi_filename, gi_ptr);
-    }
-   __gcov_finalize_dyn_callgraph ();
+  return dump_module_info;
 }
 
-/* Add a new object file onto the bb chain.  Invoked automatically
-   when running an object file's global ctors.  */
+/* Dump one entry in the gcov_info list (for one object).  */
 
-void
-__gcov_init (struct gcov_info *info)
+static void
+gcov_dump_one_gcov (struct gcov_info *gi_ptr)
 {
-  if (!gcov_sampling_rate_initialized)
-    {
-      const char* env_value_str = getenv ("GCOV_SAMPLING_RATE");
-      if (env_value_str)
-        {
-          int env_value_int = atoi(env_value_str);
-          if (env_value_int >= 1)
-            __gcov_sampling_rate = env_value_int;
-        }
-      gcov_sampling_rate_initialized = 1;
-    }
-  if (!info->version)
-    return;
-  if (gcov_version (info, info->version, 0))
-    {
-      const char *ptr = info->filename;
-      gcov_unsigned_t crc32 = gcov_crc32;
-      size_t filename_length = strlen(info->filename);
+  gcov_type *values[GCOV_COUNTERS];
+  unsigned fi_stride;
+  unsigned c_ix;
+  int ret;
 
-      /* Refresh the longest file name information */
-      if (filename_length > gcov_max_filename)
-        gcov_max_filename = filename_length;
+  memset (&this_object, 0, sizeof (this_object));
+  memset (&object, 0, sizeof (object));
 
-      /* Assign the module ID (starting at 1).  */
-      info->mod_info->ident = (++gcov_cur_module_id);
-      gcc_assert (EXTRACT_MODULE_ID_FROM_GLOBAL_ID (GEN_FUNC_GLOBAL_ID (
-                                                       info->mod_info->ident, 0))
-                  == info->mod_info->ident);
+  gcov_object_summary (gi_ptr, &this_object);
 
-      do
-	{
-	  unsigned ix;
-	  gcov_unsigned_t value = *ptr << 24;
+  c_ix = gcov_counter_array (gi_ptr, values, 1);
 
-	  for (ix = 8; ix--; value <<= 1)
-	    {
-	      gcov_unsigned_t feedback;
+  fi_stride = gcov_compute_fi_stride (c_ix);
 
-	      feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
-	      crc32 <<= 1;
-	      crc32 ^= feedback;
-	    }
-	}
-      while (*ptr++);
+  GCOV_GET_FILENAME (prefix_length, gcov_prefix_strip, gi_ptr->filename,
+                     gi_filename_up);
 
-      gcov_crc32 = crc32;
+  if (gcov_open_by_filename (gi_filename) == -1)
+    return;
 
-      if (!__gcov_list)
-	atexit (gcov_exit);
+  /* Now merge this file.  */
+  ret = gcov_merge_gcda_file (gi_ptr, values, fi_stride);
+  if (ret != 0 ) return;
 
-      info->next = __gcov_list;
-      __gcov_list = info;
-    }
-  info->version = 0;
+  gcov_write_gcda_file (gi_ptr, fi_stride);
 }
 
-/* Called before fork or exec - write out profile information gathered so
-   far and reset it to zero.  This avoids duplication or loss of the
-   profile information gathered so far.  */
-
-void
-__gcov_flush (void)
-{
-  const struct gcov_info *gi_ptr;
-
-  gcov_exit ();
-  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
-    {
-      unsigned t_ix;
-      const struct gcov_ctr_info *ci_ptr;
-
-      for (t_ix = 0, ci_ptr = gi_ptr->counts; t_ix != GCOV_COUNTERS; t_ix++)
-	if ((1 << t_ix) & gi_ptr->ctr_mask)
-	  {
-	    memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
-	    ci_ptr++;
-	  }
-    }
-}
-
 #endif /* L_gcov */
 
 #ifdef L_gcov_merge_add
@@ -1556,4 +1828,99 @@
   return execve (path, argv, envp);
 }
 #endif
+
+#ifdef __GCOV_KERNEL__
+/*
+ * Provide different implementation for the following functions:
+ *   __gcov_init
+ *   __gcov_exit
+ *
+ * Provide the following dummy merge functions:
+ *   __gcov_merge_add
+ *   __gcov_merge_single
+ *   __gcov_merge_delta
+ *   __gcov_merge_ior
+ *   __gcov_merge_icall_topn
+ *   __gcov_merge_dc
+ *   __gcov_merge_reusedist
+ *
+ * Reuse the following functions:
+ *   __gcov_interval_profiler()
+ *   __gcov_pow2_profiler()
+ *   __gcov_average_profiler()
+ *   __gcov_ior_profiler()
+ *   __gcov_one_value_profiler()
+ *   __gcov_indirect_call_profiler()
+ *     |-> __gcov_one_value_profiler_body()
+ *
+ * For LIPO: (TBD)
+ *  Change slightly for the following functions:
+ *   __gcov_merge_icall_topn
+ *   __gcov_merge_dc
+ *
+ *  Reuse the following functions:
+ *   __gcov_direct_call_profiler()
+ *   __gcov_indirect_call_topn_profiler()
+ *     |-> __gcov_topn_value_profiler_body()
+ *
+ */
+
+/* Current virual gcda file. This is for kernel use only.  */
+gcov_kernel_vfile *gcov_current_file;
+
+/* Set current virutal gcda file. It needs to be set before dumping
+   profile data.  */
+
+void
+gcov_set_vfile (gcov_kernel_vfile *file)
+{
+  gcov_current_file = file;
+}
+
+/* Dump one entry in the gcov_info list (for one object) in kernel.  */
+
+void
+gcov_kernel_dump_one_gcov (struct gcov_info *info)
+{
+  gcc_assert (gcov_current_file);
+
+  gcov_exit_init ();
+
+  gcov_dump_one_gcov (info);
+}
+
+#define DUMMY_FUNC(func) \
+void func (gcov_type *counters  __attribute__ ((unused)), \
+           unsigned n_counters __attribute__ ((unused))) {}
+
+DUMMY_FUNC (__gcov_merge_add)
+EXPORT_SYMBOL (__gcov_merge_add);
+
+DUMMY_FUNC (__gcov_merge_single)
+EXPORT_SYMBOL (__gcov_merge_single);
+
+DUMMY_FUNC (__gcov_merge_delta)
+EXPORT_SYMBOL (__gcov_merge_delta);
+
+DUMMY_FUNC(__gcov_merge_ior)
+EXPORT_SYMBOL (__gcov_merge_ior);
+
+DUMMY_FUNC (__gcov_merge_icall_topn)
+EXPORT_SYMBOL (__gcov_merge_icall_topn);
+
+DUMMY_FUNC (__gcov_merge_dc)
+EXPORT_SYMBOL (__gcov_merge_dc);
+
+DUMMY_FUNC (__gcov_merge_reusedist)
+EXPORT_SYMBOL (__gcov_merge_reusedist);
+
+EXPORT_SYMBOL (__gcov_average_profiler);
+EXPORT_SYMBOL (__gcov_indirect_call_profiler);
+EXPORT_SYMBOL (__gcov_interval_profiler);
+EXPORT_SYMBOL (__gcov_ior_profiler);
+EXPORT_SYMBOL (__gcov_one_value_profiler);
+EXPORT_SYMBOL (__gcov_pow2_profiler);
+
+#endif /* __GCOV_KERNEL__ */
+
 #endif /* inhibit_libc */

--
This patch is available for review at http://codereview.appspot.com/4523061

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13  9:10 [google] support for building Linux kernel with FDO (issue4523061) Rong Xu
@ 2011-05-13  9:23 ` Xinliang David Li
  2011-05-13 10:12   ` Richard Guenther
  2011-05-13 13:58 ` Paolo Bonzini
  1 sibling, 1 reply; 24+ messages in thread
From: Xinliang David Li @ 2011-05-13  9:23 UTC (permalink / raw)
  To: Rong Xu; +Cc: reply, gcc-patches

Ok for google/main.

David

On Thu, May 12, 2011 at 6:03 PM, Rong Xu <xur@google.com> wrote:
> This patch add support to build Linux kernel with FDO.
>
> Building Linux kernel with FDO needs both compiler change and
> kernel changes. This part only contains the gcc changes. I'll
> attach kernel changes to the reveiew thread for reference.
>
> Source files gcov-io.c gcov-io.h and libgcov.c will be copied to
> kernel source and directly used in the kernel build. When building
> for kernel, we assume option '-D__KERNEL__' is used.
>
> To enable profile generation, we need to
> a.1) set the following config:
>     CONFIG_GCOV_KERNEL=y
>     CONFIG_GCOV_PROFILE_ALL=y
> a.2) set COMPILER_LIB_PATH to the absolute path to the
>     parent directory of "gcov-src". "gcov-src" is a directory
>     containing the following 4 files:
>       gcov-iov.h gcov-io.h gcov-io.c and libgcov.c.
>     It usually sits at
> ${GCC_ROOT}/lib/gcc/x86_64-unknown-linux-gnu/${GCC_VERSION}/
> a.3) set CFLAGS_GCOV as "-fprofile-generate" and build the
>     kernel (prof_gen).
>
> To obtain the profile data
> b.1) install profile_gen kernel, mount debugfs.
> b.2) reset the profile count:
>     echo 1 > /sys/kernel/debug/gcov/reset
> b.3) run the benchmark
> b.4) cp -r /sys/kernel/debug/gcov/<your_kernel_build_dir>/ <dest_dir>
>
> To build FDO optimized kernel:
> c.1 ) same as a.1).
> c.2 ) same as a.2).
> c.3) set set CFLAGS_GCOV as
>     "-fprofile-use -fprofile-correction -Wcoverage-mismatch" and build.
>
> Performance results:
> Tested with 2.6.34 and 2.6.36 kernel with stand-alone benchmarks:
> tbench(~23%), dbench(~4%), unixbench(1%-2%), no noticable regressions.
>
> This patch has been testd with bootstraps, regression test, standalong
> kernel benchmarks (tbench, dbench, membench, unixbench, iozone, specjbb,
> and kernbench), and google internal FDO tests.
>
> 2011-05-12  Rong Xu  <xur@google.com>
>
>        * gcc/gcov-io.c (revision 173717): FDO support to build Linux kernel.
>        * gcc/gcov-io.h (revision 173717): FDO support to build Linux kernel.
>        * gcc/coverage.c        (revision 173717): set a flag if building for Linux kernel.
>        * gcc/tree-profile.c    (revision 173717): don't emit TLS declarations for Linux kernel builds.
>        * gcc/libgcov.c (revision 173717): FDO support to build Linux kernel.
>
> Index: gcc/gcov-io.c
> ===================================================================
> --- gcc/gcov-io.c       (revision 173717)
> +++ gcc/gcov-io.c       (working copy)
> @@ -32,6 +32,10 @@
>  static void gcov_allocate (unsigned);
>  #endif
>
> +#ifdef __GCOV_KERNEL__
> +struct gcov_var gcov_var ATTRIBUTE_HIDDEN;
> +#endif
> +
>  static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
>  {
>  #if !IN_LIBGCOV
> @@ -54,6 +58,7 @@
>    Return zero on failure, >0 on opening an existing file and <0 on
>    creating a new one.  */
>
> +#ifndef __GCOV_KERNEL__
>  GCOV_LINKAGE int
>  #if IN_LIBGCOV
>  gcov_open (const char *name)
> @@ -148,7 +153,24 @@
>
>   return 1;
>  }
> +#else /* __GCOV_KERNEL__ */
>
> +extern _GCOV_FILE *gcov_current_file;
> +
> +GCOV_LINKAGE int
> +gcov_open (const char *name)
> +{
> +  gcov_var.start = 0;
> +  gcov_var.offset = gcov_var.length = 0;
> +  gcov_var.overread = -1u;
> +  gcov_var.error = 0;
> +  gcov_var.file = gcov_current_file;
> +  gcov_var.mode = 1;
> +
> +  return 1;
> +}
> +#endif /* __GCOV_KERNEL__ */
> +
>  /* Close the current gcov file. Flushes data to disk. Returns nonzero
>    on failure or error flag set.  */
>
> @@ -161,7 +183,7 @@
>       if (gcov_var.offset && gcov_var.mode < 0)
>        gcov_write_block (gcov_var.offset);
>  #endif
> -      fclose (gcov_var.file);
> +      _GCOV_fclose (gcov_var.file);
>       gcov_var.file = 0;
>       gcov_var.length = 0;
>     }
> @@ -217,7 +239,7 @@
>  static void
>  gcov_write_block (unsigned size)
>  {
> -  if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
> +  if (_GCOV_fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
>     gcov_var.error = 1;
>   gcov_var.start += size;
>   gcov_var.offset -= size;
> @@ -413,7 +435,7 @@
>        gcov_allocate (gcov_var.length + words);
>       excess = gcov_var.alloc - gcov_var.length;
>  #endif
> -      excess = fread (gcov_var.buffer + gcov_var.length,
> +      excess = _GCOV_fread (gcov_var.buffer + gcov_var.length,
>                      1, excess << 2, gcov_var.file) >> 2;
>       gcov_var.length += excess;
>       if (gcov_var.length < words)
> @@ -554,6 +576,10 @@
>  GCOV_LINKAGE void
>  gcov_sync (gcov_position_t base, gcov_unsigned_t length)
>  {
> +#ifdef __GCOV_KERNEL__
> +  /* should not reach this point */
> +  gcc_assert (0);
> +#else /* __GCOV_KERNEL__ */
>   gcc_assert (gcov_var.mode > 0);
>   base += length;
>   if (base - gcov_var.start <= gcov_var.length)
> @@ -561,9 +587,10 @@
>   else
>     {
>       gcov_var.offset = gcov_var.length = 0;
> -      fseek (gcov_var.file, base << 2, SEEK_SET);
> -      gcov_var.start = ftell (gcov_var.file) >> 2;
> +      _GCOV_fseek (gcov_var.file, base << 2, SEEK_SET);
> +      gcov_var.start = _GCOV_ftell (gcov_var.file) >> 2;
>     }
> +#endif /* __GCOV_KERNEL__ */
>  }
>  #endif
>
> @@ -576,8 +603,8 @@
>   gcc_assert (gcov_var.mode < 0);
>   if (gcov_var.offset)
>     gcov_write_block (gcov_var.offset);
> -  fseek (gcov_var.file, base << 2, SEEK_SET);
> -  gcov_var.start = ftell (gcov_var.file) >> 2;
> +  _GCOV_fseek (gcov_var.file, base << 2, SEEK_SET);
> +  gcov_var.start = _GCOV_ftell (gcov_var.file) >> 2;
>  }
>
>  /* Truncate the gcov file at the current position.  */
> @@ -585,6 +612,10 @@
>  GCOV_LINKAGE void
>  gcov_truncate (void)
>  {
> +#ifdef __GCOV_KERNEL__
> +  /* should not reach this point */
> +  gcc_assert (0);
> +#else /* __GCOV_KERNEL__ */
>   long offs;
>   int filenum;
>   gcc_assert (gcov_var.mode < 0);
> @@ -594,6 +625,7 @@
>   filenum = fileno (gcov_var.file);
>   if (offs == -1 || filenum == -1 || ftruncate (filenum, offs))
>     gcov_var.error = 1;
> +#endif /* __GCOV_KERNEL__ */
>  }
>  #endif
>
> @@ -611,3 +643,105 @@
>     return status.st_mtime;
>  }
>  #endif /* IN_GCOV */
> +
> +#ifdef __GCOV_KERNEL__
> +
> +/* File fclose operation in kernel mode.  */
> +
> +int
> +kernel_file_fclose (gcov_kernel_vfile *fp)
> +{
> +  return 0;
> +}
> +
> +/* File ftell operation in kernel mode. It currently should not
> +   be called.  */
> +
> +long
> +kernel_file_ftell (gcov_kernel_vfile *fp)
> +{
> +  gcc_assert (0);  /* should not reach here */
> +  return 0;
> +}
> +
> +/* File fseek operation in kernel mode. It should only be called
> +   with OFFSET==0 and WHENCE==0 to a freshly opened file.  */
> +
> +int
> +kernel_file_fseek (gcov_kernel_vfile *fp, long offset, int whence)
> +{
> +  gcc_assert (offset == 0 && whence == 0 && fp->count == 0);
> +  return 0;
> +}
> +
> +/* File ftruncate operation in kernel mode. It currently should not
> +   be called.  */
> +
> +int
> +kernel_file_ftruncate (gcov_kernel_vfile *fp, off_t value)
> +{
> +  gcc_assert (0);  /* should not reach here */
> +  return 0;
> +}
> +
> +/* File fread operation in kernel mode. It currently should not
> +   be called.  */
> +
> +int
> +kernel_file_fread (void *ptr, size_t size, size_t nitems,
> +                  gcov_kernel_vfile *fp)
> +{
> +  gcc_assert (0);  /* should not reach here */
> +  return 0;
> +}
> +
> +/* File fwrite operation in kernel mode. It outputs the data
> +   to a buffer in the virual file.  */
> +
> +int
> +kernel_file_fwrite (const void *ptr, size_t size,
> +                   size_t nitems, gcov_kernel_vfile *fp)
> +{
> +  char *vbuf;
> +  unsigned vsize, vpos;
> +  unsigned len;
> +
> +  if (!fp) return 0;
> +
> +  vbuf = fp->buf;
> +  vsize = fp->size;
> +  vpos = fp->count;
> +
> +  if (vsize <= vpos)
> +    {
> +      printk (KERN_ERR
> +          "GCOV_KERNEL: something wrong: vbuf=%p vsize=%u vpos=%u\n",
> +          vbuf, vsize, vpos);
> +      return 0;
> +    }
> +  len = vsize - vpos;
> +  len /= size;
> +
> +  if (len > nitems)
> +    len = nitems;
> +
> +  memcpy (vbuf+vpos, ptr, size*len);
> +  fp->count += len*size;
> +
> +  if (len != nitems)
> +    printk (KERN_ERR
> +        "GCOV_KERNEL: something wrong: size=%lu nitems=%lu ret=%d\n",
> +        size, nitems, len);
> +  return len;
> +}
> +
> +/* File fileno operation in kernel mode. It currently should not
> +   be called.  */
> +
> +int
> +kernel_file_fileno (gcov_kernel_vfile *fp)
> +{
> +  gcc_assert (0);  /* should not reach here */
> +  return 0;
> +}
> +#endif /* GCOV_KERNEL */
> Index: gcc/gcov-io.h
> ===================================================================
> --- gcc/gcov-io.h       (revision 173717)
> +++ gcc/gcov-io.h       (working copy)
> @@ -163,6 +163,88 @@
>  #ifndef GCC_GCOV_IO_H
>  #define GCC_GCOV_IO_H
>
> +#ifdef __KERNEL__
> +#ifndef __GCOV_KERNEL__
> +#define __GCOV_KERNEL__
> +#endif /* __GCOV_KERNEL__ */
> +#endif /* __KERNEL__ */
> +
> +#ifdef __GCOV_KERNEL__
> +#define GCOV_LINKAGE /* nothing */
> +
> +/* We need the definitions for
> +    BITS_PER_UNIT and
> +    LONG_LONG_TYPE_SIZE
> +  They are defined in gcc/defaults.h and gcc/config/<arch_depend_files>
> +  (like, gcc/config/i386/i386.h). And it can be overridden by setting
> +  in build scripts. Here I hardcoded the value for x86.
> +  Todo: using a program to auto-generate the vaules in build time.  */
> +#define BITS_PER_UNIT 8
> +#define LONG_LONG_TYPE_SIZE 64
> +
> +/* There are many gcc_assertions. Set the vaule to 1 if we want a warning
> +   message if the assertion fails.  */
> +#ifndef ENABLE_ASSERT_CHECKING
> +#define ENABLE_ASSERT_CHECKING 1
> +#endif
> +
> +#include <linux/fs.h>
> +#endif /* __GCOV_KERNEL__ */
> +
> +/* Wrappers to the file operations.  */
> +#ifndef __GCOV_KERNEL__
> +# define _GCOV_FILE      FILE
> +# define _GCOV_fclose    fclose
> +# define _GCOV_ftell     ftell
> +# define _GCOV_fseek     fseek
> +# define _GCOV_ftruncate ftruncate
> +# define _GCOV_fread     fread
> +# define _GCOV_fwrite    fwrite
> +# define _GCOV_fread     fread
> +# define _GCOV_fileno    fileno
> +#else /* __GCOV_KERNEL__ */
> +/* In Linux kernel mode, a virtual file is used for file operations.  */
> +struct gcov_info;
> +typedef struct {
> +  long size; /* size of buf */
> +  long count; /* element written into buf */
> +  struct gcov_info *info;
> +  char buf[0];
> +} gcov_kernel_vfile;
> +
> +# define _GCOV_FILE gcov_kernel_vfile
> +
> +/* gcc_assert() prints out a warning if the check fails. It
> +   will not abort.  */
> +#if ENABLE_ASSERT_CHECKING
> +# define gcc_assert(EXPR) \
> +    ((void)(!(EXPR) ? printk (KERN_WARNING \
> +      "GCOV assertion fails: func=%s line=%d\n", \
> +      __FUNCTION__, __LINE__), 0 : 0))
> +#else
> +# define gcc_assert(EXPR) ((void)(0 && (EXPR)))
> +#endif
> +
> +/* Wrappers to the file operations.  */
> +# define _GCOV_fclose     kernel_file_fclose
> +# define _GCOV_ftell      kernel_file_ftell
> +# define _GCOV_fseek      kernel_file_fseek
> +# define _GCOV_ftruncate  kernel_file_ftruncate
> +# define _GCOV_fread      kernel_file_fread
> +# define _GCOV_fwrite     kernel_file_fwrite
> +# define _GCOV_fileno     kernel_file_fileno
> +
> +/* Declarations for virtual files operations.  */
> +extern int kernel_file_fclose (gcov_kernel_vfile *);
> +extern long kernel_file_ftell (gcov_kernel_vfile *);
> +extern int kernel_file_fseek (gcov_kernel_vfile *, long, int);
> +extern int kernel_file_ftruncate (gcov_kernel_vfile *, off_t);
> +extern int kernel_file_fread (void *, size_t, size_t,
> +    gcov_kernel_vfile *);
> +extern int kernel_file_fwrite (const void *, size_t, size_t,
> +    gcov_kernel_vfile *);
> +extern int kernel_file_fileno(gcov_kernel_vfile *);
> +#endif /* GCOV_KERNEL */
>  #if IN_LIBGCOV
>
>  #undef FUNC_ID_WIDTH
> @@ -264,7 +346,9 @@
>    is not also used in a DSO.  */
>  #if IN_LIBGCOV
>
> +#ifndef __GCOV_KERNEL__
>  #include "tconfig.h"
> +#endif /* __GCOV_KERNEL__ */
>
>  #define gcov_var __gcov_var
>  #define gcov_open __gcov_open
> @@ -609,9 +693,9 @@
>  /* Optimum number of gcov_unsigned_t's read from or written to disk.  */
>  #define GCOV_BLOCK_SIZE (1 << 10)
>
> -GCOV_LINKAGE struct gcov_var
> +struct gcov_var
>  {
> -  FILE *file;
> +  _GCOV_FILE *file;
>   gcov_position_t start;       /* Position of first byte of block */
>   unsigned offset;             /* Read/write position within the block.  */
>   unsigned length;             /* Read limit in the block.  */
> @@ -631,8 +715,16 @@
>   size_t alloc;
>   gcov_unsigned_t *buffer;
>  #endif
> -} gcov_var ATTRIBUTE_HIDDEN;
> +};
>
> +/* In kernel mode, move gcov_var definition to gcov-io.c
> +   to avoid dulipcate definitions.  */
> +#ifndef __GCOV_KERNEL__
> +GCOV_LINKAGE struct gcov_var gcov_var ATTRIBUTE_HIDDEN;
> +#else
> +extern struct gcov_var gcov_var;
> +#endif
> +
>  /* Functions for reading and writing gcov files. In libgcov you can
>    open the file for reading then writing. Elsewhere you can open the
>    file either for reading or for writing. When reading a file you may
> @@ -679,6 +771,7 @@
>  static void gcov_rewrite (void);
>  GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
>  GCOV_LINKAGE void gcov_truncate (void) ATTRIBUTE_HIDDEN;
> +GCOV_LINKAGE unsigned gcov_gcda_file_size (struct gcov_info *);
>  #else
>  /* Available outside libgcov */
>  GCOV_LINKAGE const char *gcov_read_string (void);
> @@ -729,7 +822,7 @@
>   gcov_var.mode = -1;
>   gcov_var.start = 0;
>   gcov_var.offset = 0;
> -  fseek (gcov_var.file, 0L, SEEK_SET);
> +  _GCOV_fseek (gcov_var.file, 0L, SEEK_SET);
>  }
>  #endif
>
> Index: gcc/coverage.c
> ===================================================================
> --- gcc/coverage.c      (revision 173717)
> +++ gcc/coverage.c      (working copy)
> @@ -1949,6 +1949,10 @@
>   *tail = e;
>  }
>
> +extern bool is_kernel_build;
> +
> +#define KERNEL_BUILD_PREDEF_STRING "__KERNEL__"
> +
>  /* Copies the macro def or undef CPP_DEF and saves the copy
>    in a list. IS_DEF is a flag indicating if CPP_DEF represents
>    a -D or -U.  */
> @@ -1961,6 +1965,11 @@
>   strcpy (s + 1, cpp_def);
>   str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
>   num_cpp_defines++;
> +
> +  /* When -D__KERNEL__ is in the option list, we assume this is
> +     compilation for Linux Kernel.  */
> +  if (!strcmp(cpp_def, KERNEL_BUILD_PREDEF_STRING))
> +    is_kernel_build = is_def;
>  }
>
>  /* Copies the -imacro/-include FILENAME and saves the copy in a list.  */
> Index: gcc/tree-profile.c
> ===================================================================
> --- gcc/tree-profile.c  (revision 173717)
> +++ gcc/tree-profile.c  (working copy)
> @@ -76,6 +76,10 @@
>  static GTY(()) tree ptr_void;
>  static GTY(()) tree gcov_info_decl;
>
> +/* When -D__KERNEL__ is in the option list, we assume this is a
> +   compilation for Linux Kernel.  */
> +bool is_kernel_build;
> +
>  /* Do initialization work for the edge profiler.  */
>
>  /* Add code:
> @@ -102,7 +106,7 @@
>                      ptr_void);
>       TREE_PUBLIC (ic_void_ptr_var) = 1;
>       DECL_EXTERNAL (ic_void_ptr_var) = 1;
> -      if (targetm.have_tls)
> +      if (targetm.have_tls && !is_kernel_build)
>         DECL_TLS_MODEL (ic_void_ptr_var) =
>           decl_default_tls_model (ic_void_ptr_var);
>
> @@ -113,7 +117,7 @@
>                      gcov_type_ptr);
>       TREE_PUBLIC (ic_gcov_type_ptr_var) = 1;
>       DECL_EXTERNAL (ic_gcov_type_ptr_var) = 1;
> -      if (targetm.have_tls)
> +      if (targetm.have_tls && !is_kernel_build)
>         DECL_TLS_MODEL (ic_gcov_type_ptr_var) =
>           decl_default_tls_model (ic_gcov_type_ptr_var);
>     }
> @@ -126,7 +130,7 @@
>       TREE_STATIC (ic_void_ptr_var) = 1;
>       TREE_PUBLIC (ic_void_ptr_var) = 0;
>       DECL_INITIAL (ic_void_ptr_var) = NULL;
> -      if (targetm.have_tls)
> +      if (targetm.have_tls && !is_kernel_build)
>         DECL_TLS_MODEL (ic_void_ptr_var) =
>           decl_default_tls_model (ic_void_ptr_var);
>
> @@ -138,7 +142,7 @@
>       TREE_STATIC (ic_gcov_type_ptr_var) = 1;
>       TREE_PUBLIC (ic_gcov_type_ptr_var) = 0;
>       DECL_INITIAL (ic_gcov_type_ptr_var) = NULL;
> -      if (targetm.have_tls)
> +      if (targetm.have_tls && !is_kernel_build)
>         DECL_TLS_MODEL (ic_gcov_type_ptr_var) =
>           decl_default_tls_model (ic_gcov_type_ptr_var);
>     }
> @@ -318,7 +322,7 @@
>       TREE_PUBLIC (gcov_sample_counter_decl) = 1;
>       DECL_EXTERNAL (gcov_sample_counter_decl) = 1;
>       DECL_ARTIFICIAL (gcov_sample_counter_decl) = 1;
> -      if (targetm.have_tls)
> +      if (targetm.have_tls && !is_kernel_build)
>         DECL_TLS_MODEL (gcov_sample_counter_decl) =
>             decl_default_tls_model (gcov_sample_counter_decl);
>       assemble_variable (gcov_sample_counter_decl, 0, 0, 0);
> @@ -1479,8 +1483,9 @@
>                      build_pointer_type (gcov_type_node));
>       DECL_ARTIFICIAL (dc_gcov_type_ptr_var) = 1;
>       DECL_EXTERNAL (dc_gcov_type_ptr_var) = 1;
> -      DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
> -       decl_default_tls_model (dc_gcov_type_ptr_var);
> +      if (!is_kernel_build)
> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>
>       dc_void_ptr_var =
>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
> @@ -1488,8 +1493,9 @@
>                    ptr_void);
>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
> -      DECL_TLS_MODEL (dc_void_ptr_var) =
> -       decl_default_tls_model (dc_void_ptr_var);
> +      if (!is_kernel_build)
> +        DECL_TLS_MODEL (dc_void_ptr_var) =
> +         decl_default_tls_model (dc_void_ptr_var);
>     }
>
>   add_referenced_var (gcov_info_decl);
> Index: gcc/libgcov.c
> ===================================================================
> --- gcc/libgcov.c       (revision 173717)
> +++ gcc/libgcov.c       (working copy)
> @@ -25,10 +25,29 @@
>  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
>  <http://www.gnu.org/licenses/>.  */
>
> +/* Assume compiling for Linux Kernel if __KERNEL__ is defined.  */
> +#ifdef __KERNEL__
> + /* Define MACROs to be used by kernel compilation.  */
> +# define L_gcov
> +# define L_gcov_interval_profiler
> +# define L_gcov_pow2_profiler
> +# define L_gcov_one_value_profiler
> +# define L_gcov_indirect_call_profiler
> +# define L_gcov_average_profiler
> +# define L_gcov_ior_profiler
> +
> +# define TARGET_VTABLE_USES_DESCRIPTORS 0
> +# define HAVE_CC_TLS 0
> +# define __GCOV_KERNEL__
> +
> +# define IN_LIBGCOV 1
> +# define IN_GCOV 0
> +#else /* __KERNEL__ */
>  #include "tconfig.h"
>  #include "tsystem.h"
>  #include "coretypes.h"
>  #include "tm.h"
> +#endif /* __KERNEL__ */
>
>  #if 1
>  #define THREAD_PREFIX __thread
> @@ -36,6 +55,7 @@
>  #define THREAD_PREFIX
>  #endif
>
> +#ifndef __GCOV_KERNEL__
>  #if defined(inhibit_libc)
>  #define IN_LIBGCOV (-1)
>  #else
> @@ -46,6 +66,8 @@
>  #define GCOV_LINKAGE /* nothing */
>  #endif
>  #endif
> +#endif /* __GCOV_KERNEL__ */
> +
>  #include "gcov-io.h"
>
>  #if defined(inhibit_libc)
> @@ -73,16 +95,35 @@
>
>  #else
>
> +#ifndef __GCOV_KERNEL__
>  #include <string.h>
>  #if GCOV_LOCKED
>  #include <fcntl.h>
>  #include <errno.h>
>  #include <sys/stat.h>
>  #endif
> +#endif /* __GCOV_KERNEL__ */
>
>  #ifdef L_gcov
>  #include "gcov-io.c"
>
> +/* Utility function for outputing errors.  */
> +static int
> +gcov_error (const char *fmt, ...)
> +{
> +  int ret;
> +  va_list argp;
> +  va_start (argp, fmt);
> +#ifdef __GCOV_KERNEL__
> +  ret = vprintk (fmt, argp);
> +#else
> +  ret = vfprintf (stderr, fmt, argp);
> +#endif
> +  va_end (argp);
> +  return ret;
> +}
> +
> +#ifndef __GCOV_KERNEL__
>  /* Sampling rate.  */
>  extern gcov_unsigned_t __gcov_sampling_rate;
>  static int gcov_sampling_rate_initialized = 0;
> @@ -100,6 +141,10 @@
>  /* Chain of per-object gcov structures.  */
>  extern struct gcov_info *__gcov_list;
>
> +/* Size of the longest file name. */
> +static size_t gcov_max_filename = 0;
> +#endif /* __GCOV_KERNEL__ */
> +
>  /* Unique identifier assigned to each module (object file).  */
>  static gcov_unsigned_t gcov_cur_module_id = 0;
>
> @@ -121,13 +166,45 @@
>    object file included in multiple programs.  */
>  static gcov_unsigned_t gcov_crc32;
>
> -/* Size of the longest file name. */
> -static size_t gcov_max_filename = 0;
> -
>  /* Dynamic call graph build and form module groups.  */
>  void __gcov_compute_module_groups (void) ATTRIBUTE_HIDDEN;
>  void __gcov_finalize_dyn_callgraph (void) ATTRIBUTE_HIDDEN;
>
> +/* Profile summary for the gdca file, used in sanity check?  */
> +static struct gcov_summary all;
> +
> +/* Profile summary for this program in current exeuction.  */
> +static struct gcov_summary this_program;
> +
> +/* Profile summary for this object in current execuction.  */
> +static struct gcov_summary this_object;
> +
> +/* Merged profile summary for this program.  */
> +static struct gcov_summary program;
> +
> +/* Merged profile summary for this object.  */
> +static struct gcov_summary object;
> +
> +/* Record the position of summary info.  */
> +static gcov_position_t summary_pos = 0;
> +
> +/* Record the postion of eof.  */
> +static gcov_position_t eof_pos = 0;
> +
> +/* Number of chars in prefix to be stripped.  */
> +static int gcov_prefix_strip = 0;
> +
> +/* The length of path prefix.  */
> +static size_t prefix_length = 0;
> +
> +/* gi_filename is current object filename.
> +   gi_filename_up points to the stripped filename.  */
> +static char *gi_filename, *gi_filename_up;
> +
> +static int gcov_open_by_filename (char * gi_filename);
> +static int gcov_exit_init (void);
> +static void gcov_dump_one_gcov (struct gcov_info *gi_ptr);
> +
>  /* Make sure path component of the given FILENAME exists, create
>    missing directories. FILENAME must be writable.
>    Returns zero on success, or -1 if an error occurred.  */
> @@ -175,14 +252,47 @@
>  #endif
>  }
>
> +/* Open a file with the specified name.  */
> +
> +static int
> +gcov_open_by_filename (char * gi_filename)
> +{
> +  if (!gcov_open (gi_filename))
> +    {
> +      /* Open failed likely due to missed directory.
> +         Create directory and retry to open file.  */
> +      if (create_file_directory (gi_filename))
> +        {
> +          gcov_error ("profiling:%s:Skip\n", gi_filename);
> +          return -1;
> +        }
> +      if (!gcov_open (gi_filename))
> +        {
> +          gcov_error ("profiling:%s:Cannot open\n", gi_filename);
> +          return -1;
> +        }
> +    }
> +  return 0;
> +}
> +
> +
> +/* Determine whether a counter is active.  */
> +
> +static inline int
> +gcov_counter_active (const struct gcov_info *info, unsigned int type)
> +{
> +  return (1 << type) & info->ctr_mask;
> +}
> +
> +#ifndef __GCOV_KERNEL__
>  /* Check if VERSION of the info block PTR matches libgcov one.
>    Return 1 on success, or zero in case of versions mismatch.
>    If FILENAME is not NULL, its value used for reporting purposes
>    instead of value from the info block.  */
>
>  static int
> -gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
> -             const char *filename)
> +gcov_version (struct gcov_info *ptr __attribute__ ((unused)),
> +              gcov_unsigned_t version, const char *filename)
>  {
>   if (version != GCOV_VERSION)
>     {
> @@ -192,17 +302,17 @@
>       GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
>
>       if (filename)
> -        fprintf (stderr,
> -                 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
> -                 filename, e, v);
> +        gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
> +                   filename, e, v);
>       else
> -        fprintf (stderr,
> -                 "profiling:Version mismatch - expected %.4s got %.4s\n", e, v);
> +        gcov_error ("profiling:Version mismatch - expected %.4s got %.4s\n", e, v);
>       return 0;
>     }
>   return 1;
>  }
>
> +#define GCOV_GET_FILENAME gcov_strip_leading_dirs
> +
>  /* Strip GCOV_PREFIX_STRIP levels of leading '/' from FILENAME and
>    put the result into GI_FILENAME_UP.  */
>
> @@ -298,7 +408,7 @@
>   FILE *imports_file;
>   size_t prefix_length, suffix_length;
>
> -  gcov_suffix = getenv("GCOV_IMPORTS_SUFFIX");
> +  gcov_suffix = getenv ("GCOV_IMPORTS_SUFFIX");
>   if (!gcov_suffix || !strlen (gcov_suffix))
>     gcov_suffix = ".imports";
>   suffix_length = strlen (gcov_suffix);
> @@ -328,64 +438,26 @@
>     }
>  }
>
> -/* Dump the coverage counts. We merge with existing counts when
> -   possible, to avoid growing the .da files ad infinitum. We use this
> -   program's checksum to make sure we only accumulate whole program
> -   statistics to the correct summary. An object file might be embedded
> -   in two separate programs, and we must keep the two program
> -   summaries separate.  */
> +/* This function allocates the space to store current file name.  */
>
>  static void
> -gcov_exit (void)
> +gcov_alloc_filename (void)
>  {
> -  struct gcov_info *gi_ptr;
> -  struct gcov_summary this_program;
> -  struct gcov_summary all;
> -  struct gcov_ctr_summary *cs_ptr;
> -  const struct gcov_ctr_info *ci_ptr;
> -  unsigned t_ix;
> -  gcov_unsigned_t c_num;
> -  const char *gcov_prefix;
> -  int gcov_prefix_strip = 0;
> -  size_t prefix_length;
> -  char *gi_filename, *gi_filename_up;
> -  int dump_module_info = 0;
> +  /* Get file name relocation prefix.  Non-absolute values are ignored.  */
> +  char *gcov_prefix = 0;
>
> -  memset (&all, 0, sizeof (all));
> -  /* Find the totals for this execution.  */
> -  memset (&this_program, 0, sizeof (this_program));
> -  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> -    {
> -      ci_ptr = gi_ptr->counts;
> -      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
> -       {
> -         if (!((1 << t_ix) & gi_ptr->ctr_mask))
> -           continue;
> +  prefix_length = 0;
> +  gcov_prefix_strip = 0;
>
> -         cs_ptr = &this_program.ctrs[t_ix];
> -         cs_ptr->num += ci_ptr->num;
> -         for (c_num = 0; c_num < ci_ptr->num; c_num++)
> -           {
> -             cs_ptr->sum_all += ci_ptr->values[c_num];
> -             if (cs_ptr->run_max < ci_ptr->values[c_num])
> -               cs_ptr->run_max = ci_ptr->values[c_num];
> -           }
> -         ci_ptr++;
> -       }
> -      /* The IS_PRIMARY field is overloaded to indicate if this module
> -        is FDO/LIPO.  */
> -      dump_module_info |= gi_ptr->mod_info->is_primary;
> -    }
> -
>   {
>     /* Check if the level of dirs to strip off specified. */
>     char *tmp = getenv ("GCOV_PREFIX_STRIP");
>     if (tmp)
>       {
> -       gcov_prefix_strip = atoi (tmp);
> -       /* Do not consider negative values. */
> -       if (gcov_prefix_strip < 0)
> -         gcov_prefix_strip = 0;
> +        gcov_prefix_strip = atoi (tmp);
> +        /* Do not consider negative values. */
> +        if (gcov_prefix_strip < 0)
> +          gcov_prefix_strip = 0;
>       }
>   }
>   /* Get file name relocation prefix.  Non-absolute values are ignored. */
> @@ -396,7 +468,7 @@
>
>       /* Remove an unnecessary trailing '/' */
>       if (IS_DIR_SEPARATOR (gcov_prefix[prefix_length - 1]))
> -       prefix_length--;
> +        prefix_length--;
>     }
>   else
>     prefix_length = 0;
> @@ -408,259 +480,527 @@
>       gcov_prefix = ".";
>       prefix_length = 1;
>     }
> -  /* Allocate and initialize the filename scratch space plus one.  */
> -  gi_filename = (char *) alloca (prefix_length + gcov_max_filename + 2);
> +
> +  /* Aelocate and initialize the filename scratch space.  */
> +  gi_filename = (char *) malloc (prefix_length + gcov_max_filename + 2);
>   if (prefix_length)
>     memcpy (gi_filename, gcov_prefix, prefix_length);
> -  gi_filename_up = gi_filename + prefix_length;
> +}
>
> -  /* Now merge each file.  */
> +static void
> +gcov_dump_module_info (void)
> +{
> +  struct gcov_info *gi_ptr;
> +
> +  __gcov_compute_module_groups ();
> +
> +  /* Now write out module group info.  */
>   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> +  {
> +    int error;
> +
> +    gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip,
> +                             gi_ptr->filename, gi_filename_up);
> +    error = gcov_open_by_filename (gi_filename);
> +    if (error != 0)
> +      continue;
> +
> +    /* Overwrite the zero word at the of the file.  */
> +    gcov_rewrite ();
> +    gcov_seek (gi_ptr->eof_pos);
> +
> +    gcov_write_module_infos (gi_ptr);
> +    gcov_truncate ();
> +
> +    if ((error = gcov_close ()))
> +         gcov_error (error  < 0 ?  "profiling:%s:Overflow writing\n" :
> +                                   "profiling:%s:Error writing\n",
> +                                   gi_filename);
> +    gcov_write_import_file (gi_filename, gi_ptr);
> +  }
> +  __gcov_finalize_dyn_callgraph ();
> +}
> +
> +/* Dump the coverage counts. We merge with existing counts when
> +   possible, to avoid growing the .da files ad infinitum. We use this
> +   program's checksum to make sure we only accumulate whole program
> +   statistics to the correct summary. An object file might be embedded
> +   in two separate programs, and we must keep the two program
> +   summaries separate.  */
> +
> +static void
> +gcov_exit (void)
> +{
> +  struct gcov_info *gi_ptr;
> +  int dump_module_info;
> +
> +  dump_module_info = gcov_exit_init ();
> +
> +
> +  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> +    gcov_dump_one_gcov (gi_ptr);
> +
> +  if (dump_module_info)
> +    gcov_dump_module_info ();
> +
> +  free (gi_filename);
> +}
> +
> +/* Add a new object file onto the bb chain.  Invoked automatically
> +   when running an object file's global ctors.  */
> +
> +void
> +__gcov_init (struct gcov_info *info)
> +{
> +  if (!gcov_sampling_rate_initialized)
>     {
> -      struct gcov_summary this_object;
> -      struct gcov_summary object, program;
> -      gcov_type *values[GCOV_COUNTERS];
> -      const struct gcov_fn_info *fi_ptr;
> -      unsigned fi_stride;
> -      unsigned c_ix, f_ix, n_counts;
> -      struct gcov_ctr_summary *cs_obj, *cs_tobj, *cs_prg, *cs_tprg, *cs_all;
> -      int error = 0;
> -      gcov_unsigned_t tag, length;
> -      gcov_position_t summary_pos = 0;
> -      gcov_position_t eof_pos = 0;
> -      const char *fname, *s;
> +      const char* env_value_str = getenv ("GCOV_SAMPLING_RATE");
> +      if (env_value_str)
> +        {
> +          int env_value_int = atoi(env_value_str);
> +          if (env_value_int >= 1)
> +            __gcov_sampling_rate = env_value_int;
> +        }
> +      gcov_sampling_rate_initialized = 1;
> +    }
>
> -      fname = gi_ptr->filename;
> +  if (!info->version)
> +    return;
>
> -      memset (&this_object, 0, sizeof (this_object));
> -      memset (&object, 0, sizeof (object));
> +  if (gcov_version (info, info->version, 0))
> +    {
> +      const char *ptr = info->filename;
> +      gcov_unsigned_t crc32 = gcov_crc32;
> +      size_t filename_length = strlen (info->filename);
>
> -      gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip,
> -                              gi_ptr->filename, gi_filename_up);
> +      /* Refresh the longest file name information.  */
> +      if (filename_length > gcov_max_filename)
> +        gcov_max_filename = filename_length;
>
> -      /* Totals for this object file.  */
> -      ci_ptr = gi_ptr->counts;
> -      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
> +      /* Assign the module ID (starting at 1).  */
> +      info->mod_info->ident = (++gcov_cur_module_id);
> +      gcc_assert (EXTRACT_MODULE_ID_FROM_GLOBAL_ID (GEN_FUNC_GLOBAL_ID (
> +                                                       info->mod_info->ident, 0))
> +                  == info->mod_info->ident);
> +
> +      do
>        {
> -         if (!((1 << t_ix) & gi_ptr->ctr_mask))
> -           continue;
> +         unsigned ix;
> +         gcov_unsigned_t value = *ptr << 24;
>
> -         cs_ptr = &this_object.ctrs[t_ix];
> -         cs_ptr->num += ci_ptr->num;
> -         for (c_num = 0; c_num < ci_ptr->num; c_num++)
> +         for (ix = 8; ix--; value <<= 1)
>            {
> -             cs_ptr->sum_all += ci_ptr->values[c_num];
> -             if (cs_ptr->run_max < ci_ptr->values[c_num])
> -               cs_ptr->run_max = ci_ptr->values[c_num];
> +             gcov_unsigned_t feedback;
> +
> +             feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
> +             crc32 <<= 1;
> +             crc32 ^= feedback;
>            }
> -         ci_ptr++;
> -       }
> +       } while (*ptr++);
>
> -      c_ix = 0;
> -      for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
> -       if ((1 << t_ix) & gi_ptr->ctr_mask)
> +      gcov_crc32 = crc32;
> +
> +      if (!__gcov_list)
> +        atexit (gcov_exit);
> +
> +      info->next = __gcov_list;
> +      __gcov_list = info;
> +    }
> +  info->version = 0;
> +}
> +
> +/* Called before fork or exec - write out profile information gathered so
> +   far and reset it to zero.  This avoids duplication or loss of the
> +   profile information gathered so far.  */
> +
> +void
> +__gcov_flush (void)
> +{
> +  const struct gcov_info *gi_ptr;
> +
> +  gcov_exit ();
> +  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> +    {
> +      unsigned t_ix;
> +      const struct gcov_ctr_info *ci_ptr;
> +
> +      for (t_ix = 0, ci_ptr = gi_ptr->counts; t_ix != GCOV_COUNTERS; t_ix++)
> +        if (gcov_counter_active (gi_ptr, t_ix))
>          {
> -           values[c_ix] = gi_ptr->counts[c_ix].values;
> -            if (t_ix == GCOV_COUNTER_ICALL_TOPNV)
> -              gcov_sort_icall_topn_counter (&gi_ptr->counts[c_ix]);
> -           c_ix++;
> +           memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
> +           ci_ptr++;
>          }
> +    }
> +}
>
> -      /* Calculate the function_info stride. This depends on the
> -        number of counter types being measured.  */
> -      fi_stride = offsetof (struct gcov_fn_info, n_ctrs)
> -       + c_ix * sizeof (unsigned);
> -      if (__alignof__ (struct gcov_fn_info) > sizeof (unsigned))
> -       {
> -         fi_stride += __alignof__ (struct gcov_fn_info) - 1;
> -         fi_stride &= ~(__alignof__ (struct gcov_fn_info) - 1);
> -       }
> +#else /* __GCOV_KERNEL__ */
>
> -      if (!gcov_open (gi_filename))
> -       {
> -         /* Open failed likely due to missed directory.
> -            Create directory and retry to open file. */
> -          if (create_file_directory (gi_filename))
> -           {
> -             fprintf (stderr, "profiling:%s:Skip\n", gi_filename);
> -             continue;
> -           }
> -         if (!gcov_open (gi_filename))
> -           {
> -              fprintf (stderr, "profiling:%s:Cannot open\n", gi_filename);
> -             continue;
> -           }
> -       }
> +#define GCOV_GET_FILENAME gcov_get_filename
>
> -      tag = gcov_read_unsigned ();
> -      if (tag)
> -       {
> -         /* Merge data from file.  */
> -         if (tag != GCOV_DATA_MAGIC)
> -           {
> -             fprintf (stderr, "profiling:%s:Not a gcov data file\n",
> -                      gi_filename);
> -             goto read_fatal;
> -           }
> -         length = gcov_read_unsigned ();
> -         if (!gcov_version (gi_ptr, length, gi_filename))
> -           goto read_fatal;
> +/* Copy the filename to the buffer.  */
>
> -         length = gcov_read_unsigned ();
> -         if (length != gi_ptr->stamp)
> -           /* Read from a different compilation. Overwrite the file.  */
> -           goto rewrite;
> +static inline void
> +gcov_get_filename (int prefix_length __attribute__ ((unused)),
> +                   int gcov_prefix_strip __attribute__ ((unused)),
> +                   const char *filename, char *gi_filename_up)
> +{
> +    strcpy (gi_filename_up, filename);
> +}
>
> -         /* Merge execution counts for each function.  */
> -         for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
> -           {
> -             fi_ptr = (const struct gcov_fn_info *)
> -                     ((const char *) gi_ptr->functions + f_ix * fi_stride);
> -             tag = gcov_read_unsigned ();
> -             length = gcov_read_unsigned ();
> +/* Sort the profile counters for all indirect call sites. Counters
> +   for each call site are allocated in array COUNTERS.  */
>
> -             /* Check function.  */
> -             if (tag != GCOV_TAG_FUNCTION
> -                 || length != GCOV_TAG_FUNCTION_LENGTH
> -                 || gcov_read_unsigned () != fi_ptr->ident
> -                 || gcov_read_unsigned () != fi_ptr->lineno_checksum
> -                 || gcov_read_unsigned () != fi_ptr->cfg_checksum)
> -               {
> -               read_mismatch:;
> -                 fprintf (stderr, "profiling:%s:Merge mismatch for %s\n",
> -                          gi_filename,
> -                          f_ix + 1 ? "function" : "summaries");
> -                 goto read_fatal;
> -               }
> +static void
> +gcov_sort_icall_topn_counter (const struct gcov_ctr_info *counters)
> +{
> +  /* Empty */
> +}
>
> -             c_ix = 0;
> -             for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
> -               {
> -                 gcov_merge_fn merge;
> +/* Reserves a buffer to store the name of the file being processed.  */
> +static char _kernel_gi_filename[520];
>
> -                 if (!((1 << t_ix) & gi_ptr->ctr_mask))
> -                   continue;
> +/* This function allocates the space to store current file name.  */
>
> -                 n_counts = fi_ptr->n_ctrs[c_ix];
> -                 merge = gi_ptr->counts[c_ix].merge;
> +static void
> +gcov_alloc_filename (void)
> +{
> +  prefix_length = 0;
> +  gcov_prefix_strip = 0;
> +  gi_filename = _kernel_gi_filename;
> +}
>
> -                 tag = gcov_read_unsigned ();
> -                 length = gcov_read_unsigned ();
> -                 if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
> -                     || length != GCOV_TAG_COUNTER_LENGTH (n_counts))
> -                   goto read_mismatch;
> -                 (*merge) (values[c_ix], n_counts);
> -                 values[c_ix] += n_counts;
> -                 c_ix++;
> -               }
> -             if ((error = gcov_is_error ()))
> -               goto read_error;
> -           }
> +#endif /* __GCOV_KERNEL__ */
>
> -         f_ix = ~0u;
> -         /* Check program & object summary */
> -         while (1)
> -           {
> -             int is_program;
> +/* Determine number of active counters in gcov_info INFO,
> +   the counter arrays are stored in VALUES if the coming
> +   value of VALUES !=0. If FLAG_SORT_ICALL_TOPN_COUNTER !=0,
> +   the icall_topn_counter in INFO will be sorted.
> +   Return: the number of active counter types.  */
>
> -             eof_pos = gcov_position ();
> -             tag = gcov_read_unsigned ();
> -             if (!tag)
> -               break;
> +static unsigned int
> +gcov_counter_array (const struct gcov_info *info,
> +                    gcov_type *values[GCOV_COUNTERS],
> +                    int flag_sort_icall_topn_counter)
> +{
> +  unsigned int i;
> +  unsigned int result = 0;
>
> -             length = gcov_read_unsigned ();
> -             is_program = tag == GCOV_TAG_PROGRAM_SUMMARY;
> -             if (length != GCOV_TAG_SUMMARY_LENGTH
> -                 || (!is_program && tag != GCOV_TAG_OBJECT_SUMMARY))
> -               goto read_mismatch;
> -             gcov_read_summary (is_program ? &program : &object);
> -             if ((error = gcov_is_error ()))
> -               goto read_error;
> -             if (is_program && program.checksum == gcov_crc32)
> -               {
> -                 summary_pos = eof_pos;
> -                 goto rewrite;
> -               }
> -           }
> -       }
> -      goto rewrite;
> +  for (i = 0; i < GCOV_COUNTERS; i++) {
> +    if (gcov_counter_active (info, i))
> +      {
> +        if (values)
> +          values[result] = info->counts[result].values;
> +        if (flag_sort_icall_topn_counter &&
> +            (i == GCOV_COUNTER_ICALL_TOPNV))
> +          gcov_sort_icall_topn_counter (&info->counts[result]);
> +        result++;
> +      }
> +  }
> +  return result;
> +}
>
> -    read_error:;
> -      fprintf (stderr, error < 0 ? "profiling:%s:Overflow merging\n"
> -              : "profiling:%s:Error merging\n", gi_filename);
> +/* Compute object summary recored in gcov_info INFO. The result is
> +   stored in OBJ_SUM. Note that the caller is responsible for
> +   zeroing out OBJ_SUM, otherwise the summary is accumulated.  */
>
> -    read_fatal:;
> -      gcov_close ();
> -      continue;
> +static void
> +gcov_object_summary (struct gcov_info *info,
> +                     struct gcov_summary *obj_sum)
> +{
> +  const struct gcov_ctr_info *ci_ptr;
> +  struct gcov_ctr_summary *cs_ptr;
> +  gcov_unsigned_t c_num;
> +  unsigned t_ix;
>
> -    rewrite:;
> -      gcov_rewrite ();
> -      if (!summary_pos)
> -       memset (&program, 0, sizeof (program));
> +  /* Totals for this object file.  */
> +  ci_ptr = info->counts;
> +  for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
> +    {
> +      if (!gcov_counter_active (info, t_ix))
> +        continue;
>
> -      /* Merge the summaries.  */
> -      f_ix = ~0u;
> -      for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
> -       {
> -         cs_obj = &object.ctrs[t_ix];
> -         cs_tobj = &this_object.ctrs[t_ix];
> -         cs_prg = &program.ctrs[t_ix];
> -         cs_tprg = &this_program.ctrs[t_ix];
> -         cs_all = &all.ctrs[t_ix];
> +      cs_ptr = &(obj_sum->ctrs[t_ix]);
> +      cs_ptr->num += ci_ptr->num;
> +      for (c_num = 0; c_num < ci_ptr->num; c_num++)
> +        {
> +          cs_ptr->sum_all += ci_ptr->values[c_num];
> +          if (cs_ptr->run_max < ci_ptr->values[c_num])
> +            cs_ptr->run_max = ci_ptr->values[c_num];
> +        }
> +      ci_ptr++;
> +    }
> +}
>
> -         if ((1 << t_ix) & gi_ptr->ctr_mask)
> -           {
> -             if (!cs_obj->runs++)
> -               cs_obj->num = cs_tobj->num;
> -             else if (cs_obj->num != cs_tobj->num)
> -               goto read_mismatch;
> -             cs_obj->sum_all += cs_tobj->sum_all;
> -             if (cs_obj->run_max < cs_tobj->run_max)
> -               cs_obj->run_max = cs_tobj->run_max;
> -             cs_obj->sum_max += cs_tobj->run_max;
> +/* Merge with existing gcda file in the same directory to avoid
> +   excessive growthe of the files.  */
>
> -             if (!cs_prg->runs++)
> -               cs_prg->num = cs_tprg->num;
> -             else if (cs_prg->num != cs_tprg->num)
> -               goto read_mismatch;
> -             cs_prg->sum_all += cs_tprg->sum_all;
> -             if (cs_prg->run_max < cs_tprg->run_max)
> -               cs_prg->run_max = cs_tprg->run_max;
> -             cs_prg->sum_max += cs_tprg->run_max;
> -           }
> -         else if (cs_obj->num || cs_prg->num)
> -           goto read_mismatch;
> +static int
> +gcov_merge_gcda_file (struct gcov_info *info,
> +                      gcov_type *values[GCOV_COUNTERS],
> +                      unsigned fi_stride)
> +{
> +  struct gcov_ctr_summary *cs_obj, *cs_tobj, *cs_prg, *cs_tprg, *cs_all;
> +  unsigned t_ix, f_ix;
>
> -         if (!cs_all->runs && cs_prg->runs)
> -           memcpy (cs_all, cs_prg, sizeof (*cs_all));
> -         else if (!all.checksum
> -                  && (!GCOV_LOCKED || cs_all->runs == cs_prg->runs)
> -                  && memcmp (cs_all, cs_prg, sizeof (*cs_all)))
> -           {
> -             fprintf (stderr, "profiling:%s:Invocation mismatch - some data files may have been removed%s",
> -                      gi_filename, GCOV_LOCKED
> -                      ? "" : " or concurrent update without locking support");
> -             all.checksum = ~0u;
> -           }
> -       }
> +#ifndef __GCOV_KERNEL__
> +  const struct gcov_fn_info *fi_ptr;
> +  unsigned c_ix, n_counts;
> +  int error = 0;
> +  gcov_unsigned_t tag, length;
>
> +  tag = gcov_read_unsigned ();
> +  if (tag)
> +    {
> +      /* Merge data from file.  */
> +      if (tag != GCOV_DATA_MAGIC)
> +        {
> +          gcov_error ("profiling:%s:Not a gcov data file\n", gi_filename);
> +          goto read_fatal;
> +        }
> +     length = gcov_read_unsigned ();
> +     if (!gcov_version (info, length, gi_filename))
> +       goto read_fatal;
> +
> +     length = gcov_read_unsigned ();
> +     if (length != info->stamp)
> +       /* Read from a different compilation. Overwrite the file.  */
> +       goto rewrite;
> +
> +     /* Merge execution counts for each function.  */
> +     for (f_ix = 0; f_ix < info->n_functions; f_ix++)
> +       {
> +         fi_ptr = (const struct gcov_fn_info *)
> +                   ((const char *) info->functions + f_ix * fi_stride);
> +         tag = gcov_read_unsigned ();
> +         length = gcov_read_unsigned ();
> +
> +         /* Check function.  */
> +         if (tag != GCOV_TAG_FUNCTION
> +            || length != GCOV_TAG_FUNCTION_LENGTH
> +             || gcov_read_unsigned () != fi_ptr->ident
> +             || gcov_read_unsigned () != fi_ptr->lineno_checksum
> +             || gcov_read_unsigned () != fi_ptr->cfg_checksum)
> +           goto read_mismatch;
> +
> +           c_ix = 0;
> +           for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
> +             {
> +               gcov_merge_fn merge;
> +
> +               if (!((1 << t_ix) & info->ctr_mask))
> +                 continue;
> +
> +               n_counts = fi_ptr->n_ctrs[c_ix];
> +               merge = info->counts[c_ix].merge;
> +
> +               tag = gcov_read_unsigned ();
> +               length = gcov_read_unsigned ();
> +               if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
> +                   || length != GCOV_TAG_COUNTER_LENGTH (n_counts))
> +                 goto read_mismatch;
> +               (*merge) (values[c_ix], n_counts);
> +               values[c_ix] += n_counts;
> +               c_ix++;
> +             }
> +           if ((error = gcov_is_error ()))
> +             goto read_error;
> +       }
> +
> +       f_ix = ~0u;
> +       /* Check program & object summary.  */
> +       while (1)
> +         {
> +           int is_program;
> +
> +           eof_pos = gcov_position ();
> +           tag = gcov_read_unsigned ();
> +           if (!tag)
> +             break;
> +
> +           length = gcov_read_unsigned ();
> +           is_program = tag == GCOV_TAG_PROGRAM_SUMMARY;
> +           if (length != GCOV_TAG_SUMMARY_LENGTH
> +               || (!is_program && tag != GCOV_TAG_OBJECT_SUMMARY))
> +             goto read_mismatch;
> +           gcov_read_summary (is_program ? &program : &object);
> +           if ((error = gcov_is_error ()))
> +             goto read_error;
> +           if (is_program && program.checksum == gcov_crc32)
> +             {
> +               summary_pos = eof_pos;
> +               goto rewrite;
> +             }
> +         }
> +    }
> +
> +    goto rewrite;
> +
> +read_error:;
> +    gcov_error (error < 0 ? "profiling:%s:Overflow merging\n"
> +                : "profiling:%s:Error merging\n", gi_filename);
> +    goto read_fatal;
> +
> +#endif /* __GCOV_KERNEL__ */
> +
> +    goto rewrite;
> +
> +read_mismatch:;
> +    gcov_error ("profiling:%s:Merge mismatch for %s\n", gi_filename,
> +                 f_ix + 1 ? "function" : "summaries");
> +    goto read_fatal; /* work-around the compiler warning */
> +
> +read_fatal:;
> +    gcov_close ();
> +    return 1;
> +
> +rewrite:;
> +    gcov_rewrite ();
> +    if (!summary_pos)
> +      memset (&program, 0, sizeof (program));
> +
> +    /* Merge the summaries.  */
> +    f_ix = ~0u;
> +    for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
> +      {
> +        cs_obj = &object.ctrs[t_ix];
> +        cs_tobj = &this_object.ctrs[t_ix];
> +        cs_prg = &program.ctrs[t_ix];
> +        cs_tprg = &this_program.ctrs[t_ix];
> +        cs_all = &all.ctrs[t_ix];
> +
> +        if ((1 << t_ix) & info->ctr_mask)
> +          {
> +            if (!cs_obj->runs++)
> +              cs_obj->num = cs_tobj->num;
> +            else if (cs_obj->num != cs_tobj->num)
> +               goto read_mismatch;
> +            cs_obj->sum_all += cs_tobj->sum_all;
> +            if (cs_obj->run_max < cs_tobj->run_max)
> +              cs_obj->run_max = cs_tobj->run_max;
> +            cs_obj->sum_max += cs_tobj->run_max;
> +
> +            if (!cs_prg->runs++)
> +              cs_prg->num = cs_tprg->num;
> +            else if (cs_prg->num != cs_tprg->num)
> +              goto read_mismatch;
> +            cs_prg->sum_all += cs_tprg->sum_all;
> +            if (cs_prg->run_max < cs_tprg->run_max)
> +              cs_prg->run_max = cs_tprg->run_max;
> +            cs_prg->sum_max += cs_tprg->run_max;
> +          }
> +        else if (cs_obj->num || cs_prg->num)
> +          goto read_mismatch;
> +
> +        if (!cs_all->runs && cs_prg->runs)
> +          memcpy (cs_all, cs_prg, sizeof (*cs_all));
> +        else if (!all.checksum
> +                 && (!GCOV_LOCKED || cs_all->runs == cs_prg->runs)
> +                 && memcmp (cs_all, cs_prg, sizeof (*cs_all)))
> +          {
> +            gcov_error ("profiling:%s:Invocation mismatch - "
> +                "some data files may have been removed%s",
> +            gi_filename, GCOV_LOCKED
> +            ? "" : " or concurrent update without locking support");
> +            all.checksum = ~0u;
> +          }
> +      }
> +
> +  return 0;
> +}
> +
> +/* Calculate the function_info stride. This depends on the
> +   number of counter types being measured.
> +   NUM_COUNTER_TYPES is number of counter types recorded.
> +   Return: the number of bytes for accessing next fn_info
> +   (aligned to gcov_fn_info).  */
> +
> +static unsigned
> +gcov_compute_fi_stride (unsigned num_counter_types)
> +{
> +   unsigned fi_stride;
> +
> +   fi_stride = offsetof (struct gcov_fn_info, n_ctrs) +
> +               num_counter_types * sizeof (unsigned);
> +   if (__alignof__ (struct gcov_fn_info) > sizeof (unsigned))
> +   {
> +     fi_stride += __alignof__ (struct gcov_fn_info) - 1;
> +     fi_stride &= ~(__alignof__ (struct gcov_fn_info) - 1);
> +   }
> +   return fi_stride;
> +}
> +
> +/* This function returns the size of gcda file to be written. Note
> +   the size is in units of gcov_type.  */
> +
> +GCOV_LINKAGE unsigned
> +gcov_gcda_file_size (struct gcov_info *gi_ptr)
> +{
> +  unsigned size;
> +  const struct gcov_fn_info *fi_ptr;
> +  unsigned f_ix, t_ix, c_ix;
> +  unsigned n_counts;
> +  unsigned fi_stride;
> +  gcov_type *values[GCOV_COUNTERS];
> +
> +  c_ix = gcov_counter_array (gi_ptr, values, 0);
> +  fi_stride = gcov_compute_fi_stride (c_ix);
> +
> +  /* GCOV_DATA_MAGIC, GCOV_VERSION and time_stamp.  */
> +  size = 3;
> +
> +  /* size for each function.  */
> +  for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
> +    {
> +      fi_ptr = (const struct gcov_fn_info *)
> +        ((const char *) gi_ptr->functions + f_ix * fi_stride);
> +
> +      size += 2 /* tag_length itself */
> +              + GCOV_TAG_FUNCTION_LENGTH; /* ident, lineno_cksum, cfg_cksm */
> +
>       c_ix = 0;
>       for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
> -       if ((1 << t_ix) & gi_ptr->ctr_mask)
> -         {
> -           values[c_ix] = gi_ptr->counts[c_ix].values;
> -           c_ix++;
> -         }
> +        {
> +          if (!((1 << t_ix) & gi_ptr->ctr_mask))
> +            continue;
>
> -      program.checksum = gcov_crc32;
> +          n_counts = fi_ptr->n_ctrs[c_ix];
> +          size += 2 + GCOV_TAG_COUNTER_LENGTH (n_counts);
> +          c_ix++;
> +        }
> +    }
>
> +  /* Object summary.  */
> +  size += 2 + GCOV_TAG_SUMMARY_LENGTH;
> +
> +  /* Program summary.  */
> +  size += 2 + GCOV_TAG_SUMMARY_LENGTH;
> +
> +  size += 1;
> +
> +  return size*4;
> +}
> +
> +/* Write profile data (including summary and module grouping information,
> +   if available, to file.  */
> +
> +static void
> +gcov_write_gcda_file (struct gcov_info *gi_ptr,
> +                      unsigned fi_stride)
> +{
> +      const struct gcov_fn_info *fi_ptr;
> +      gcov_type *values[GCOV_COUNTERS];
> +      unsigned t_ix, c_ix, f_ix, n_counts;
> +      int error = 0;
> +
>       /* Write out the data.  */
>       gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
>       gcov_write_unsigned (gi_ptr->stamp);
>
> +      gcov_counter_array (gi_ptr, values, 0);
> +
>       /* Write execution counts for each function.  */
>       for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
> -       {
> +        {
>          fi_ptr = (const struct gcov_fn_info *)
>                  ((const char *) gi_ptr->functions + f_ix * fi_stride);
>
> @@ -689,12 +1029,13 @@
>              values[c_ix] = c_ptr;
>              c_ix++;
>            }
> -       }
> +        }
>
>       /* Object file summary.  */
>       gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, &object);
>
>       /* Generate whole program statistics.  */
> +      program.checksum = gcov_crc32;
>       if (eof_pos)
>        gcov_seek (eof_pos);
>       gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &program);
> @@ -713,146 +1054,77 @@
>         gi_ptr->eof_pos = gcov_position ();
>
>       if ((error = gcov_close ()))
> -         fprintf (stderr, error  < 0 ?
> +         gcov_error (error  < 0 ?
>                   "profiling:%s:Overflow writing\n" :
>                   "profiling:%s:Error writing\n",
>                   gi_filename);
> +}
>
> -    }
> +/* Do some preparation work before calling the actual dumping
> +   routine.
> +   Return: 1 when module grouping info needs to be dumped,
> +           0 otherwise.  */
>
> -  if (!dump_module_info)
> -    return;
> +static int
> +gcov_exit_init (void)
> +{
> +  struct gcov_info *gi_ptr;
> +  int dump_module_info = 0;
>
> -   __gcov_compute_module_groups ();
> +  dump_module_info = 0;
> +  gcov_prefix_strip = 0;
>
> -   /* Now write out module group info.  */
> -   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> +  memset (&all, 0, sizeof (all));
> +
> +  /* Find the totals for this execution.  */
> +  memset (&this_program, 0, sizeof (this_program));
> +  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
>     {
> -      int error;
> -      gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip,
> -                              gi_ptr->filename, gi_filename_up);
> +      gcov_object_summary (gi_ptr, &this_program);
>
> -      if (!gcov_open (gi_filename))
> -       {
> -#ifdef TARGET_POSIX_IO
> -         /* Open failed likely due to missed directory.
> -            Create directory and retry to open file. */
> -          if (create_file_directory (gi_filename))
> -           {
> -             fprintf (stderr, "profiling:%s:Skip\n", gi_filename);
> -             continue;
> -           }
> -#endif
> -         if (!gcov_open (gi_filename))
> -           {
> -              fprintf (stderr, "profiling:%s:Cannot open\n", gi_filename);
> -             continue;
> -           }
> -       }
> +      /* The IS_PRIMARY field is overloaded to indicate if this module
> +         is FDO/LIPO.  */
> +      dump_module_info |= gi_ptr->mod_info->is_primary;
> +    }
>
> -      /* Overwrite the zero word at the of the file.  */
> -      gcov_rewrite ();
> -      gcov_seek (gi_ptr->eof_pos);
> +  gcov_alloc_filename ();
> +  gi_filename_up = gi_filename + prefix_length;
>
> -      gcov_write_module_infos (gi_ptr);
> -      gcov_truncate ();
> -
> -      if ((error = gcov_close ()))
> -         fprintf (stderr, error  < 0 ?
> -                  "profiling:%s:Overflow writing\n" :
> -                  "profiling:%s:Error writing\n",
> -                  gi_filename);
> -      gcov_write_import_file (gi_filename, gi_ptr);
> -    }
> -   __gcov_finalize_dyn_callgraph ();
> +  return dump_module_info;
>  }
>
> -/* Add a new object file onto the bb chain.  Invoked automatically
> -   when running an object file's global ctors.  */
> +/* Dump one entry in the gcov_info list (for one object).  */
>
> -void
> -__gcov_init (struct gcov_info *info)
> +static void
> +gcov_dump_one_gcov (struct gcov_info *gi_ptr)
>  {
> -  if (!gcov_sampling_rate_initialized)
> -    {
> -      const char* env_value_str = getenv ("GCOV_SAMPLING_RATE");
> -      if (env_value_str)
> -        {
> -          int env_value_int = atoi(env_value_str);
> -          if (env_value_int >= 1)
> -            __gcov_sampling_rate = env_value_int;
> -        }
> -      gcov_sampling_rate_initialized = 1;
> -    }
> -  if (!info->version)
> -    return;
> -  if (gcov_version (info, info->version, 0))
> -    {
> -      const char *ptr = info->filename;
> -      gcov_unsigned_t crc32 = gcov_crc32;
> -      size_t filename_length = strlen(info->filename);
> +  gcov_type *values[GCOV_COUNTERS];
> +  unsigned fi_stride;
> +  unsigned c_ix;
> +  int ret;
>
> -      /* Refresh the longest file name information */
> -      if (filename_length > gcov_max_filename)
> -        gcov_max_filename = filename_length;
> +  memset (&this_object, 0, sizeof (this_object));
> +  memset (&object, 0, sizeof (object));
>
> -      /* Assign the module ID (starting at 1).  */
> -      info->mod_info->ident = (++gcov_cur_module_id);
> -      gcc_assert (EXTRACT_MODULE_ID_FROM_GLOBAL_ID (GEN_FUNC_GLOBAL_ID (
> -                                                       info->mod_info->ident, 0))
> -                  == info->mod_info->ident);
> +  gcov_object_summary (gi_ptr, &this_object);
>
> -      do
> -       {
> -         unsigned ix;
> -         gcov_unsigned_t value = *ptr << 24;
> +  c_ix = gcov_counter_array (gi_ptr, values, 1);
>
> -         for (ix = 8; ix--; value <<= 1)
> -           {
> -             gcov_unsigned_t feedback;
> +  fi_stride = gcov_compute_fi_stride (c_ix);
>
> -             feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
> -             crc32 <<= 1;
> -             crc32 ^= feedback;
> -           }
> -       }
> -      while (*ptr++);
> +  GCOV_GET_FILENAME (prefix_length, gcov_prefix_strip, gi_ptr->filename,
> +                     gi_filename_up);
>
> -      gcov_crc32 = crc32;
> +  if (gcov_open_by_filename (gi_filename) == -1)
> +    return;
>
> -      if (!__gcov_list)
> -       atexit (gcov_exit);
> +  /* Now merge this file.  */
> +  ret = gcov_merge_gcda_file (gi_ptr, values, fi_stride);
> +  if (ret != 0 ) return;
>
> -      info->next = __gcov_list;
> -      __gcov_list = info;
> -    }
> -  info->version = 0;
> +  gcov_write_gcda_file (gi_ptr, fi_stride);
>  }
>
> -/* Called before fork or exec - write out profile information gathered so
> -   far and reset it to zero.  This avoids duplication or loss of the
> -   profile information gathered so far.  */
> -
> -void
> -__gcov_flush (void)
> -{
> -  const struct gcov_info *gi_ptr;
> -
> -  gcov_exit ();
> -  for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
> -    {
> -      unsigned t_ix;
> -      const struct gcov_ctr_info *ci_ptr;
> -
> -      for (t_ix = 0, ci_ptr = gi_ptr->counts; t_ix != GCOV_COUNTERS; t_ix++)
> -       if ((1 << t_ix) & gi_ptr->ctr_mask)
> -         {
> -           memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
> -           ci_ptr++;
> -         }
> -    }
> -}
> -
>  #endif /* L_gcov */
>
>  #ifdef L_gcov_merge_add
> @@ -1556,4 +1828,99 @@
>   return execve (path, argv, envp);
>  }
>  #endif
> +
> +#ifdef __GCOV_KERNEL__
> +/*
> + * Provide different implementation for the following functions:
> + *   __gcov_init
> + *   __gcov_exit
> + *
> + * Provide the following dummy merge functions:
> + *   __gcov_merge_add
> + *   __gcov_merge_single
> + *   __gcov_merge_delta
> + *   __gcov_merge_ior
> + *   __gcov_merge_icall_topn
> + *   __gcov_merge_dc
> + *   __gcov_merge_reusedist
> + *
> + * Reuse the following functions:
> + *   __gcov_interval_profiler()
> + *   __gcov_pow2_profiler()
> + *   __gcov_average_profiler()
> + *   __gcov_ior_profiler()
> + *   __gcov_one_value_profiler()
> + *   __gcov_indirect_call_profiler()
> + *     |-> __gcov_one_value_profiler_body()
> + *
> + * For LIPO: (TBD)
> + *  Change slightly for the following functions:
> + *   __gcov_merge_icall_topn
> + *   __gcov_merge_dc
> + *
> + *  Reuse the following functions:
> + *   __gcov_direct_call_profiler()
> + *   __gcov_indirect_call_topn_profiler()
> + *     |-> __gcov_topn_value_profiler_body()
> + *
> + */
> +
> +/* Current virual gcda file. This is for kernel use only.  */
> +gcov_kernel_vfile *gcov_current_file;
> +
> +/* Set current virutal gcda file. It needs to be set before dumping
> +   profile data.  */
> +
> +void
> +gcov_set_vfile (gcov_kernel_vfile *file)
> +{
> +  gcov_current_file = file;
> +}
> +
> +/* Dump one entry in the gcov_info list (for one object) in kernel.  */
> +
> +void
> +gcov_kernel_dump_one_gcov (struct gcov_info *info)
> +{
> +  gcc_assert (gcov_current_file);
> +
> +  gcov_exit_init ();
> +
> +  gcov_dump_one_gcov (info);
> +}
> +
> +#define DUMMY_FUNC(func) \
> +void func (gcov_type *counters  __attribute__ ((unused)), \
> +           unsigned n_counters __attribute__ ((unused))) {}
> +
> +DUMMY_FUNC (__gcov_merge_add)
> +EXPORT_SYMBOL (__gcov_merge_add);
> +
> +DUMMY_FUNC (__gcov_merge_single)
> +EXPORT_SYMBOL (__gcov_merge_single);
> +
> +DUMMY_FUNC (__gcov_merge_delta)
> +EXPORT_SYMBOL (__gcov_merge_delta);
> +
> +DUMMY_FUNC(__gcov_merge_ior)
> +EXPORT_SYMBOL (__gcov_merge_ior);
> +
> +DUMMY_FUNC (__gcov_merge_icall_topn)
> +EXPORT_SYMBOL (__gcov_merge_icall_topn);
> +
> +DUMMY_FUNC (__gcov_merge_dc)
> +EXPORT_SYMBOL (__gcov_merge_dc);
> +
> +DUMMY_FUNC (__gcov_merge_reusedist)
> +EXPORT_SYMBOL (__gcov_merge_reusedist);
> +
> +EXPORT_SYMBOL (__gcov_average_profiler);
> +EXPORT_SYMBOL (__gcov_indirect_call_profiler);
> +EXPORT_SYMBOL (__gcov_interval_profiler);
> +EXPORT_SYMBOL (__gcov_ior_profiler);
> +EXPORT_SYMBOL (__gcov_one_value_profiler);
> +EXPORT_SYMBOL (__gcov_pow2_profiler);
> +
> +#endif /* __GCOV_KERNEL__ */
> +
>  #endif /* inhibit_libc */
>
> --
> This patch is available for review at http://codereview.appspot.com/4523061
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13  9:23 ` Xinliang David Li
@ 2011-05-13 10:12   ` Richard Guenther
  2011-05-13 12:43     ` Jan Hubicka
  0 siblings, 1 reply; 24+ messages in thread
From: Richard Guenther @ 2011-05-13 10:12 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Rong Xu, reply, gcc-patches

On Fri, May 13, 2011 at 6:53 AM, Xinliang David Li <davidxl@google.com> wrote:
> Ok for google/main.

Seems to be very special and not appropriate for GCC trunk.  Instead
an adjusted copy providing the interface GCC needs should belong to
the kernel tree.

Richard.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 10:12   ` Richard Guenther
@ 2011-05-13 12:43     ` Jan Hubicka
  2011-05-13 17:14       ` Andi Kleen
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Hubicka @ 2011-05-13 12:43 UTC (permalink / raw)
  To: Richard Guenther; +Cc: Xinliang David Li, Rong Xu, reply, gcc-patches, andi

> On Fri, May 13, 2011 at 6:53 AM, Xinliang David Li <davidxl@google.com> wrote:
> > Ok for google/main.
> 
> Seems to be very special and not appropriate for GCC trunk.  Instead
> an adjusted copy providing the interface GCC needs should belong to
> the kernel tree.
Andi Kleen also did work in this direction. We probably should synchronize the
efforts.

It seems that there is quite a bit of FDO stuff that should get in some form
to mainline for 4.7.  Perhaps some overview would be helpful.

Honza
> 
> Richard.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google]  support for building Linux kernel with FDO (issue4523061)
  2011-05-13  9:10 [google] support for building Linux kernel with FDO (issue4523061) Rong Xu
  2011-05-13  9:23 ` Xinliang David Li
@ 2011-05-13 13:58 ` Paolo Bonzini
  2011-05-13 18:44   ` Xinliang David Li
  1 sibling, 1 reply; 24+ messages in thread
From: Paolo Bonzini @ 2011-05-13 13:58 UTC (permalink / raw)
  To: Rong Xu; +Cc: reply, gcc-patches

On 05/13/2011 03:03 AM, Rong Xu wrote:
> 	* gcc/coverage.c	(revision 173717): set a flag if building for Linux kernel.
> 	* gcc/tree-profile.c	(revision 173717): don't emit TLS declarations for Linux kernel builds.

I think this should be done without touching at all the profiling 
machinery in GCC.

1) add a new TLS model -ftls-model=none and make the kernel uses it. 
The model would simply force targetm.have_tls to false.

2) as Richi mentioned, gcov-io and libgcov changes then can move to the 
kernel, and GCC needs no change at all here.

BTW, these parts of LIPO:

> +      if (!is_kernel_build)
> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
> +	  decl_default_tls_model (dc_gcov_type_ptr_var);
>
>        dc_void_ptr_var =
>  	build_decl (UNKNOWN_LOCATION, VAR_DECL,
> @@ -1488,8 +1493,9 @@
>  		    ptr_void);
>        DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>        DECL_EXTERNAL (dc_void_ptr_var) = 1;
> -      DECL_TLS_MODEL (dc_void_ptr_var) =
> -	decl_default_tls_model (dc_void_ptr_var);
> +      if (!is_kernel_build)
> +        DECL_TLS_MODEL (dc_void_ptr_var) =
> +	  decl_default_tls_model (dc_void_ptr_var);

Probably are missing a !targetm.have_tls.

Paolo

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 12:43     ` Jan Hubicka
@ 2011-05-13 17:14       ` Andi Kleen
  0 siblings, 0 replies; 24+ messages in thread
From: Andi Kleen @ 2011-05-13 17:14 UTC (permalink / raw)
  To: Jan Hubicka
  Cc: Richard Guenther, Xinliang David Li, Rong Xu, reply, gcc-patches, andi

On Fri, May 13, 2011 at 11:43:42AM +0200, Jan Hubicka wrote:
> > On Fri, May 13, 2011 at 6:53 AM, Xinliang David Li <davidxl@google.com> wrote:
> > > Ok for google/main.
> > 
> > Seems to be very special and not appropriate for GCC trunk.  Instead
> > an adjusted copy providing the interface GCC needs should belong to
> > the kernel tree.
> Andi Kleen also did work in this direction. We probably should synchronize the
> efforts.

Yes, the in kernel gcov code -- which already has large parts of
the gcov interface, but unfortunately for gcc 3 needs to be updated.
This needs to be done inside the kernel tree though.

I did some work on this to update, but my efforts fell a bit short. The 
resulting kernel was actually larger because I was missing some information.

My variant didn't need the TLS changes because I just used atomic
counters.

I also ran into some problems with gcc when the information was not
missing some TUs due to the way the information is collected.

-Andi
-- 
ak@linux.intel.com -- Speaking for myself only.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 13:58 ` Paolo Bonzini
@ 2011-05-13 18:44   ` Xinliang David Li
  2011-05-13 18:59     ` Paolo Bonzini
                       ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: Xinliang David Li @ 2011-05-13 18:44 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Rong Xu, reply, gcc-patches

On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
> On 05/13/2011 03:03 AM, Rong Xu wrote:
>>
>>        * gcc/coverage.c        (revision 173717): set a flag if building
>> for Linux kernel.
>>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>> declarations for Linux kernel builds.
>
> I think this should be done without touching at all the profiling machinery
> in GCC.
>
> 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
> model would simply force targetm.have_tls to false.
>

This is a good idea.


> 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
> kernel, and GCC needs no change at all here.
>

In fact -- reuse gcc code profiling machinery for FDO is the KEY
objective in this effort --- the effort tries to minimize gcc changes
by refactoring gcc code and isolating/abstracting away part of gcc
implementation that is user space program specific without using
runtime hooks.  Aside from the kernel FDO change -- the refactoring
itself actually makes the libgcov code more readable -- the existing
implementation has many huge functions etc.

Kernel source has their implementation of coverage testing -- but it
makes lots of data structure assumptions and hard coded -- it can
easily out of sync with gcc and is considered  unmaintainable.

Rong will have more explanation on the design.

Thanks,

David


> BTW, these parts of LIPO:
>
>> +      if (!is_kernel_build)
>> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>>
>>       dc_void_ptr_var =
>>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>> @@ -1488,8 +1493,9 @@
>>                    ptr_void);
>>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>> -       decl_default_tls_model (dc_void_ptr_var);
>> +      if (!is_kernel_build)
>> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>> +         decl_default_tls_model (dc_void_ptr_var);
>
> Probably are missing a !targetm.have_tls.
>
> Paolo
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 18:44   ` Xinliang David Li
@ 2011-05-13 18:59     ` Paolo Bonzini
  2011-05-13 22:24     ` Rong Xu
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 24+ messages in thread
From: Paolo Bonzini @ 2011-05-13 18:59 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Rong Xu, reply, gcc-patches

On 05/13/2011 06:31 PM, Xinliang David Li wrote:
>> >  1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>> >  model would simply force targetm.have_tls to false.
>> >
>
> This is a good idea.
>
>> >  2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>> >  kernel, and GCC needs no change at all here.
>
> In fact -- reuse gcc code profiling machinery for FDO is the KEY
> objective in this effort

Ok, removing in_kernel_build is already fixing 99% of what is 
undesirable with this patch IMO!

Paolo

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 18:44   ` Xinliang David Li
  2011-05-13 18:59     ` Paolo Bonzini
@ 2011-05-13 22:24     ` Rong Xu
  2011-05-13 22:31     ` Rong Xu
  2011-05-15 19:34     ` Jan Hubicka
  3 siblings, 0 replies; 24+ messages in thread
From: Rong Xu @ 2011-05-13 22:24 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Paolo Bonzini, reply, gcc-patches

Thanks for the comments. This particular patch is not intended for
trunk. But we do want to hear your feedback on our designs.

On Fri, May 13, 2011 at 9:31 AM, Xinliang David Li <davidxl@google.com> wrote:
> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>> On 05/13/2011 03:03 AM, Rong Xu wrote:
>>>
>>>        * gcc/coverage.c        (revision 173717): set a flag if building
>>> for Linux kernel.
>>>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>>> declarations for Linux kernel builds.
>>
>> I think this should be done without touching at all the profiling machinery
>> in GCC.
>>
>> 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>> model would simply force targetm.have_tls to false.
>>
>
> This is a good idea.
>

Agreed. We can use the option together with -fprofile-generate.

>
>> 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>> kernel, and GCC needs no change at all here.
>>
>
> In fact -- reuse gcc code profiling machinery for FDO is the KEY
> objective in this effort --- the effort tries to minimize gcc changes
> by refactoring gcc code and isolating/abstracting away part of gcc
> implementation that is user space program specific without using
> runtime hooks.  Aside from the kernel FDO change -- the refactoring
> itself actually makes the libgcov code more readable -- the existing
> implementation has many huge functions etc.
>
> Kernel source has their implementation of coverage testing -- but it
> makes lots of data structure assumptions and hard coded -- it can
> easily out of sync with gcc and is considered  unmaintainable.
>
> Rong will have more explanation on the design.
>

David has said most I want to say. I just add one more thing: kernel
can be compiled with many versions of gcc where libgcov is not
guaranteed to be compatible. It's will be messy if we put the all
versions of libgcov code into kernel. It's much cleaner to reuse the
libgcov code in gcc. For maintenance, it's better to have a central
place for one functionality.

The functionality in libgcov can be rough divided into the following part:
(1) gcov_init support: kernel need to have it's own version.
(2) runtime profile support: this part is mostly neutral to user-space
mode or kernel mode. Only some LIPO support needs to use a few libc
functions.
(3) dumping the profile file (traverse gcov_list, compute summary,
merging profile, wrtie gcda file etc). I would say most of the
functionalities are neutral. Only the file I/O  part is specific to
user-mode. Kernel's merging function is not necessary as it can be
done offline. We need to refactor gcov_exit() so it can dump one
individual gcov_info.

So I think it's reasonable to factor the libgcov code to provide
kernel support.  There are many place in this patch can be improved in
this regard, but here are the key points of our design that we want to
keep:
(1) The functionality of dumping gcov_info (independent of use-space
mode or kernel mode) should be in gcc.
(2) libgcov source in gcc will be exported to kernel.

We can re-factor current implementation to separate kernel code. I
have another patch that puts all the kernel code into separated files
(and they can easily be moved to kernel.)


> Thanks,
>
> David
>
>
>> BTW, these parts of LIPO:
>>
>>> +      if (!is_kernel_build)
>>> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>>> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>>>
>>>       dc_void_ptr_var =
>>>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>>> @@ -1488,8 +1493,9 @@
>>>                    ptr_void);
>>>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>>>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>>> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>>> -       decl_default_tls_model (dc_void_ptr_var);
>>> +      if (!is_kernel_build)
>>> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>>> +         decl_default_tls_model (dc_void_ptr_var);
>>
>> Probably are missing a !targetm.have_tls.
>>
>> Paolo
>>
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 18:44   ` Xinliang David Li
  2011-05-13 18:59     ` Paolo Bonzini
  2011-05-13 22:24     ` Rong Xu
@ 2011-05-13 22:31     ` Rong Xu
  2011-05-13 22:33       ` Rong Xu
  2011-05-15 19:34     ` Jan Hubicka
  3 siblings, 1 reply; 24+ messages in thread
From: Rong Xu @ 2011-05-13 22:31 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Paolo Bonzini, reply, gcc-patches

Thanks for the comments. This particular patch is not intended for
trunk. But we do want to hear your feedback on our designs.

On Fri, May 13, 2011 at 9:31 AM, Xinliang David Li <davidxl@google.com> wrote:
> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>> On 05/13/2011 03:03 AM, Rong Xu wrote:
>>>
>>>        * gcc/coverage.c        (revision 173717): set a flag if building
>>> for Linux kernel.
>>>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>>> declarations for Linux kernel builds.
>>
>> I think this should be done without touching at all the profiling machinery
>> in GCC.
>>
>> 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>> model would simply force targetm.have_tls to false.
>>
>
> This is a good idea.
>

Agreed. We can use the option together with -fprofile-generate.

>
>> 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>> kernel, and GCC needs no change at all here.
>>
>
> In fact -- reuse gcc code profiling machinery for FDO is the KEY
> objective in this effort --- the effort tries to minimize gcc changes
> by refactoring gcc code and isolating/abstracting away part of gcc
> implementation that is user space program specific without using
> runtime hooks.  Aside from the kernel FDO change -- the refactoring
> itself actually makes the libgcov code more readable -- the existing
> implementation has many huge functions etc.
>
> Kernel source has their implementation of coverage testing -- but it
> makes lots of data structure assumptions and hard coded -- it can
> easily out of sync with gcc and is considered  unmaintainable.
>
> Rong will have more explanation on the design.
>

David has said most I want to say. I just add one more thing: kernel
can be compiled with many versions of gcc where libgcov is not
guaranteed to be compatible. It's will be messy if we put the all
versions of libgcov code into kernel. It's much cleaner to reuse the
libgcov code in gcc. For maintenance, it's better to have a central
place for one functionality.

The functionality in libgcov can be rough divided into the following part:
(1) gcov_init support: kernel need to have it's own version.
(2) runtime profile support: this part is mostly neutral to user-space
mode or kernel mode. Only some LIPO support needs to use a few libc
functions.
(3) dumping the profile file (traverse gcov_list, compute summary,
merging profile, wrtie gcda file etc). I would say most of the
functionalities are neutral. Only the file I/O  part is specific to
user-mode. Kernel's merging function is not necessary as it can be
done offline. We need to refactor gcov_exit() so it can dump one
individual gcov_info.

So I think it's reasonable to factor the libgcov code to provide
kernel support.  There are many place in this patch can be improved in
this regard, but here are the key points of our design that we want to
keep:
(1) The functionality of dumping gcov_info (independent of use-space
mode or kernel mode) should be in gcc.
(2) libgcov source in gcc will be exported to kernel.

We can re-factor current implementation to separate kernel code. I
have another patch that puts all the kernel code into separated files
(and they can easily be moved to kernel.)


> Thanks,
>
> David
>
>
>> BTW, these parts of LIPO:
>>
>>> +      if (!is_kernel_build)
>>> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>>> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>>>
>>>       dc_void_ptr_var =
>>>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>>> @@ -1488,8 +1493,9 @@
>>>                    ptr_void);
>>>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>>>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>>> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>>> -       decl_default_tls_model (dc_void_ptr_var);
>>> +      if (!is_kernel_build)
>>> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>>> +         decl_default_tls_model (dc_void_ptr_var);
>>
>> Probably are missing a !targetm.have_tls.
>>
>> Paolo
>>
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 22:31     ` Rong Xu
@ 2011-05-13 22:33       ` Rong Xu
  0 siblings, 0 replies; 24+ messages in thread
From: Rong Xu @ 2011-05-13 22:33 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Paolo Bonzini, reply, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 4411 bytes --]

Here is the kernel part of change (for kernel 2.6.36) -- just for the
reference.

On Fri, May 13, 2011 at 11:45 AM, Rong Xu <xur@google.com> wrote:
> Thanks for the comments. This particular patch is not intended for
> trunk. But we do want to hear your feedback on our designs.
>
> On Fri, May 13, 2011 at 9:31 AM, Xinliang David Li <davidxl@google.com> wrote:
>> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>>> On 05/13/2011 03:03 AM, Rong Xu wrote:
>>>>
>>>>        * gcc/coverage.c        (revision 173717): set a flag if building
>>>> for Linux kernel.
>>>>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>>>> declarations for Linux kernel builds.
>>>
>>> I think this should be done without touching at all the profiling machinery
>>> in GCC.
>>>
>>> 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>>> model would simply force targetm.have_tls to false.
>>>
>>
>> This is a good idea.
>>
>
> Agreed. We can use the option together with -fprofile-generate.
>
>>
>>> 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>>> kernel, and GCC needs no change at all here.
>>>
>>
>> In fact -- reuse gcc code profiling machinery for FDO is the KEY
>> objective in this effort --- the effort tries to minimize gcc changes
>> by refactoring gcc code and isolating/abstracting away part of gcc
>> implementation that is user space program specific without using
>> runtime hooks.  Aside from the kernel FDO change -- the refactoring
>> itself actually makes the libgcov code more readable -- the existing
>> implementation has many huge functions etc.
>>
>> Kernel source has their implementation of coverage testing -- but it
>> makes lots of data structure assumptions and hard coded -- it can
>> easily out of sync with gcc and is considered  unmaintainable.
>>
>> Rong will have more explanation on the design.
>>
>
> David has said most I want to say. I just add one more thing: kernel
> can be compiled with many versions of gcc where libgcov is not
> guaranteed to be compatible. It's will be messy if we put the all
> versions of libgcov code into kernel. It's much cleaner to reuse the
> libgcov code in gcc. For maintenance, it's better to have a central
> place for one functionality.
>
> The functionality in libgcov can be rough divided into the following part:
> (1) gcov_init support: kernel need to have it's own version.
> (2) runtime profile support: this part is mostly neutral to user-space
> mode or kernel mode. Only some LIPO support needs to use a few libc
> functions.
> (3) dumping the profile file (traverse gcov_list, compute summary,
> merging profile, wrtie gcda file etc). I would say most of the
> functionalities are neutral. Only the file I/O  part is specific to
> user-mode. Kernel's merging function is not necessary as it can be
> done offline. We need to refactor gcov_exit() so it can dump one
> individual gcov_info.
>
> So I think it's reasonable to factor the libgcov code to provide
> kernel support.  There are many place in this patch can be improved in
> this regard, but here are the key points of our design that we want to
> keep:
> (1) The functionality of dumping gcov_info (independent of use-space
> mode or kernel mode) should be in gcc.
> (2) libgcov source in gcc will be exported to kernel.
>
> We can re-factor current implementation to separate kernel code. I
> have another patch that puts all the kernel code into separated files
> (and they can easily be moved to kernel.)
>
>
>> Thanks,
>>
>> David
>>
>>
>>> BTW, these parts of LIPO:
>>>
>>>> +      if (!is_kernel_build)
>>>> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>>>> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>>>>
>>>>       dc_void_ptr_var =
>>>>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>>>> @@ -1488,8 +1493,9 @@
>>>>                    ptr_void);
>>>>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>>>>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>>>> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>>>> -       decl_default_tls_model (dc_void_ptr_var);
>>>> +      if (!is_kernel_build)
>>>> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>>>> +         decl_default_tls_model (dc_void_ptr_var);
>>>
>>> Probably are missing a !targetm.have_tls.
>>>
>>> Paolo
>>>
>>
>

[-- Attachment #2: 0001-core-gcov-enable-FDO-profile-generation-in-GCC.patch --]
[-- Type: text/x-patch, Size: 39910 bytes --]

From 6b41dbf977a3e001a77100f092532b879115d8e0 Mon Sep 17 00:00:00 2001
From: Rong Xu <xur@google.com>
Date: Wed, 26 Jan 2011 14:33:10 -0800
Subject: [PATCH] core-gcov: enable FDO profile generation in GCC

This change enables profile generation for Feedback Directed
Optimization (FDO) in GCC. It only works with gcc version
4.4.3 or later. Compiling with gcc 3.4 gives the same behavior
as in the original gcov implementation.

This change focuses on enabling FDO and it does not maintain
the same feature set as the original gcov implementation:
1) .gcno and .c files will no longer be linked to profiles
   directory.
2) all .gcda files will have a .tmp_ prefix, the same prefix
    when feeding c source files to the compiler.

To enable profile generation, we need to
1) set the following config:
   CONFIG_GCOV_KERNEL=y
   CONFIG_GCOV_PROFILE_ALL=y
2) set COMPILER_LIB_PATH to the absolute path to the
   parent directory of "gcov-src". "gcov-src" is a directory
   containing the following 4 files:
     gcov-iov.h gcov-io.h gcov-io.c and libgcov.c.
   It usually sits at
${GCC_ROOT}/lib/gcc/x86_64-unknown-linux-gnu/${GCC_VERSION}/.

The profiles will be stored in /sys/debug/debug/gcov/.

Google-Bug-Id: 2405721
Effort: core-gcov
Change-Id: I3e43e06771abf168775d6082e999fd0334845b60
---
 kernel/gcov/Makefile            |    7 +-
 kernel/gcov/base.c              |   24 ++-
 kernel/gcov/fs.c                |  278 ++++++++++++++++++++----
 kernel/gcov/gcc.c               |  461 +++++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c           |  447 -------------------------------------
 kernel/gcov/gcc_version_check.h |   16 ++
 kernel/gcov/gcov.h              |   13 +
 7 files changed, 745 insertions(+), 501 deletions(-)
 create mode 100644 kernel/gcov/gcc.c
 delete mode 100644 kernel/gcov/gcc_3_4.c
 create mode 100644 kernel/gcov/gcc_version_check.h

diff --git a/kernel/gcov/Makefile b/kernel/gcov/Makefile
index 3f76100..c2c3e04 100644
--- a/kernel/gcov/Makefile
+++ b/kernel/gcov/Makefile
@@ -1,3 +1,6 @@
-EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
+EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' -iquote "$(COMPILER_LIB_PATH)"
 
-obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc_3_4.o
+# don't profile itself (maybe needed for code coverage test?)
+GCOV_PROFILE := n
+
+obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc.o
diff --git a/kernel/gcov/base.c b/kernel/gcov/base.c
index 9b22d03..0d4d0cc 100644
--- a/kernel/gcov/base.c
+++ b/kernel/gcov/base.c
@@ -13,6 +13,9 @@
  *		 Paul Larson
  */
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #define pr_fmt(fmt)	"gcov: " fmt
 
 #include <linux/init.h>
@@ -20,7 +23,7 @@
 #include <linux/mutex.h>
 #include "gcov.h"
 
-static struct gcov_info *gcov_info_head;
+static struct gcov_info *__gcov_list;
 static int gcov_events_enabled;
 static DEFINE_MUTEX(gcov_lock);
 
@@ -45,14 +48,20 @@ void __gcov_init(struct gcov_info *info)
 	 * Add new profiling data structure to list and inform event
 	 * listener.
 	 */
-	info->next = gcov_info_head;
-	gcov_info_head = info;
+	info->next = __gcov_list;
+	__gcov_list = info;
+
 	if (gcov_events_enabled)
 		gcov_event(GCOV_ADD, info);
 	mutex_unlock(&gcov_lock);
 }
 EXPORT_SYMBOL(__gcov_init);
 
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+
+#include "gcov-src/libgcov.c"
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
 /*
  * These functions may be referenced by gcc-generated profiling code but serve
  * no function for kernel profiling.
@@ -80,6 +89,7 @@ void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
 	/* Unused. */
 }
 EXPORT_SYMBOL(__gcov_merge_delta);
+#endif /* GCC_VERSION */
 
 /**
  * gcov_enable_events - enable event reporting through gcov_event()
@@ -96,7 +106,7 @@ void gcov_enable_events(void)
 	mutex_lock(&gcov_lock);
 	gcov_events_enabled = 1;
 	/* Perform event callback for previously registered entries. */
-	for (info = gcov_info_head; info; info = info->next)
+	for (info = __gcov_list; info; info = info->next)
 		gcov_event(GCOV_ADD, info);
 	mutex_unlock(&gcov_lock);
 }
@@ -120,12 +130,12 @@ static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
 	mutex_lock(&gcov_lock);
 	prev = NULL;
 	/* Remove entries located in module from linked list. */
-	for (info = gcov_info_head; info; info = info->next) {
+	for (info = __gcov_list; info; info = info->next) {
 		if (within(info, mod->module_core, mod->core_size)) {
 			if (prev)
 				prev->next = info->next;
 			else
-				gcov_info_head = info->next;
+				__gcov_list = info->next;
 			if (gcov_events_enabled)
 				gcov_event(GCOV_REMOVE, info);
 		} else
@@ -146,3 +156,5 @@ static int __init gcov_init(void)
 }
 device_initcall(gcov_init);
 #endif /* CONFIG_MODULES */
+
+#endif /* GCC_VERSION */
diff --git a/kernel/gcov/fs.c b/kernel/gcov/fs.c
index f83972b..6507532 100644
--- a/kernel/gcov/fs.c
+++ b/kernel/gcov/fs.c
@@ -14,6 +14,9 @@
  *		 Yi CDL Yang
  */
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #define pr_fmt(fmt)	"gcov: " fmt
 
 #include <linux/init.h>
@@ -24,7 +27,12 @@
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/mutex.h>
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+#include <linux/vmalloc.h>
+#include <asm/uaccess.h>
+#else /* GCC_VERSION_EQUAL(3, 4) */
 #include <linux/seq_file.h>
+#endif
 #include "gcov.h"
 
 /**
@@ -86,6 +94,185 @@ static int __init gcov_persist_setup(char *str)
 __setup("gcov_persist=", gcov_persist_setup);
 
 /*
+ * Return a profiling data set associated with the given node. This is
+ * either a data set for a loaded object file or a data set copy in case
+ * all associated object files have been unloaded.
+ */
+static struct gcov_info *get_node_info(struct gcov_node *node)
+{
+	if (node->num_loaded > 0)
+		return node->loaded_info[0];
+
+	return node->unloaded_info;
+}
+
+static struct gcov_node *get_node_by_name(const char *name);
+static void reset_node(struct gcov_node *node);
+static void remove_node(struct gcov_node *node);
+
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+extern void gcov_set_vfile (gcov_kernel_vfile *);
+extern unsigned gcov_gcda_file_size (struct gcov_info *);
+extern void gcov_kernel_dump_one_gcov (struct gcov_info *);
+
+/*
+ * open() implementation for gcov data files. Create a copy of the profiling
+ * data set and initialize the object and vfile interface.
+ */
+static int gcda_file_open(struct inode *inode, struct file *file)
+{
+	struct gcov_node *node = inode->i_private;
+	gcov_kernel_vfile *kvf;
+	struct gcov_info *info;
+	int rc = -ENOMEM;
+	unsigned vfile_size, alloc_size;
+
+	mutex_lock(&node_lock);
+	/*
+	 * Read from a profiling data copy to minimize reference tracking
+	 * complexity and concurrent access.
+	 */
+	info = gcov_info_dup(get_node_info(node));
+	if (!info)
+		goto out_unlock;
+
+	vfile_size = gcov_gcda_file_size (info);
+	alloc_size = vfile_size + sizeof(gcov_kernel_vfile);
+	kvf = (gcov_kernel_vfile *) vmalloc (alloc_size);
+
+	if (kvf == NULL) {
+		pr_err("Cannot allocate memory (%d) for gcov.\n", alloc_size);
+		goto err_free_info;
+	}
+
+	kvf->size = vfile_size;
+	kvf->info = info;
+	kvf->count = 0;
+
+	file->private_data = kvf;
+	gcov_set_vfile (kvf);
+
+	gcov_kernel_dump_one_gcov (info);
+	rc = 0;
+
+	goto out_unlock;
+
+err_free_info:
+	gcov_info_free(info);
+
+out_unlock:
+	mutex_unlock(&node_lock);
+	return rc;
+
+}
+
+/*
+ * read() implementation for gcov data files. Copy the profile data
+ * stored in virtual file to user space buffer.
+ */
+static ssize_t gcda_file_read(struct file *filp, char __user *ubuf,
+			      size_t count, loff_t *ppos)
+{
+	gcov_kernel_vfile *kvf;
+	char *buf;
+	int ret;
+	int copied;
+	int n;
+	unsigned buf_size, buf_cnt;
+
+	if (!count)
+		return 0;
+
+	kvf = filp->private_data;
+	buf = kvf->buf;
+	if (!buf)
+		return 0;
+
+	buf_size = kvf->size;
+	buf_cnt = kvf->count;
+
+	if (buf_cnt == *ppos)
+		return 0;
+
+	if (buf_cnt < *ppos) {
+		pr_err("Buffer overflows: buf_cnt=%u, pos=%u\n",
+			buf_cnt, (unsigned)*ppos);
+		return -EFAULT;
+	}
+
+	n = count;
+	if (buf_cnt - *ppos < n)
+		n = buf_cnt - *ppos;
+
+	ret = copy_to_user(ubuf, buf + *ppos, n);
+
+	/* Not all data copied. Invalid user space address? */
+	if (ret != 0)
+		return -EFAULT;
+
+	copied = n-ret;
+	*ppos += copied;
+
+	return copied;
+}
+
+/*
+ * release() implementation for gcov data files. Release resources allocated
+ * by open().
+ */
+static int gcda_file_release(struct inode *inode, struct file *file)
+{
+	struct gcov_info *info;
+	gcov_kernel_vfile *kvf;
+
+	kvf = file->private_data;
+	info = kvf->info;
+	gcov_info_free(info);
+	vfree (kvf);
+
+	return 0;
+}
+
+/*
+ * write() implementation for gcov data files. Reset profiling data for the
+ * associated file. If the object file has been unloaded (i.e. this is
+ * a "ghost" node), remove the debug fs node as well.
+ */
+static ssize_t gcda_file_write(struct file *file, const char __user *addr,
+			        size_t len, loff_t *pos)
+{
+	gcov_kernel_vfile *kvf;
+	struct gcov_info *info;
+	struct gcov_node *node;
+
+	kvf = file->private_data;
+	info = kvf->info;
+	mutex_lock(&node_lock);
+	node = get_node_by_name(info->filename);
+	if (node) {
+		/* Reset counts or remove node for unloaded modules.  */
+		if (node->num_loaded)
+			remove_node(node);
+		else
+			reset_node(node);
+	}
+	/* Reset counts for open file.  */
+	gcov_info_reset(info);
+	mutex_unlock(&node_lock);
+
+	return len;
+}
+
+static const struct file_operations gcda_file_fops = {
+	.open		= gcda_file_open,
+	.read		= gcda_file_read,
+	.write		= gcda_file_write,
+	.release	= gcda_file_release,
+};
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
+
+/*
  * seq_file.start() implementation for gcov data files. Note that the
  * gcov_iterator interface is designed to be more restrictive than seq_file
  * (no start from arbitrary position, etc.), to simplify the iterator
@@ -138,19 +325,6 @@ static const struct seq_operations gcov_seq_ops = {
 };
 
 /*
- * Return a profiling data set associated with the given node. This is
- * either a data set for a loaded object file or a data set copy in case
- * all associated object files have been unloaded.
- */
-static struct gcov_info *get_node_info(struct gcov_node *node)
-{
-	if (node->num_loaded > 0)
-		return node->loaded_info[0];
-
-	return node->unloaded_info;
-}
-
-/*
  * Return a newly allocated profiling data set which contains the sum of
  * all profiling data associated with the given node.
  */
@@ -232,39 +406,6 @@ static int gcov_seq_release(struct inode *inode, struct file *file)
 }
 
 /*
- * Find a node by the associated data file name. Needs to be called with
- * node_lock held.
- */
-static struct gcov_node *get_node_by_name(const char *name)
-{
-	struct gcov_node *node;
-	struct gcov_info *info;
-
-	list_for_each_entry(node, &all_head, all) {
-		info = get_node_info(node);
-		if (info && (strcmp(info->filename, name) == 0))
-			return node;
-	}
-
-	return NULL;
-}
-
-/*
- * Reset all profiling data associated with the specified node.
- */
-static void reset_node(struct gcov_node *node)
-{
-	int i;
-
-	if (node->unloaded_info)
-		gcov_info_reset(node->unloaded_info);
-	for (i = 0; i < node->num_loaded; i++)
-		gcov_info_reset(node->loaded_info[i]);
-}
-
-static void remove_node(struct gcov_node *node);
-
-/*
  * write() implementation for gcov data files. Reset profiling data for the
  * corresponding file. If all associated object files have been unloaded,
  * remove the debug fs node as well.
@@ -408,6 +549,26 @@ static const struct file_operations gcov_data_fops = {
 	.write		= gcov_seq_write,
 };
 
+#endif /* GCC_VERSION */
+
+/*
+ * Find a node by the associated data file name. Needs to be called with
+ * node_lock held.
+ */
+static struct gcov_node *get_node_by_name(const char *name)
+{
+	struct gcov_node *node;
+	struct gcov_info *info;
+
+	list_for_each_entry(node, &all_head, all) {
+		info = get_node_info(node);
+		if (info && (strcmp(info->filename, name) == 0))
+			return node;
+	}
+
+	return NULL;
+}
+
 /* Basic initialization of a new node. */
 static void init_node(struct gcov_node *node, struct gcov_info *info,
 		      const char *name, struct gcov_node *parent)
@@ -445,8 +606,13 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 	init_node(node, info, name, parent);
 	/* Differentiate between gcov data file nodes and directory nodes. */
 	if (info) {
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+		node->dentry = debugfs_create_file(node->name, 0600,
+					parent->dentry, node, &gcda_file_fops);
+#else /* GCC_VERSION_EQUAL(3, 4) */
 		node->dentry = debugfs_create_file(deskew(node->name), 0600,
 					parent->dentry, node, &gcov_data_fops);
+#endif
 	} else
 		node->dentry = debugfs_create_dir(node->name, parent->dentry);
 	if (!node->dentry) {
@@ -454,8 +620,13 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 		kfree(node);
 		return NULL;
 	}
+
+#if GCC_VERSION_EQUAL(3, 4)
+	/* keep the original file name and don't link gcno and .c */
 	if (info)
 		add_links(node, parent->dentry);
+#endif
+
 	list_add(&node->list, &parent->children);
 	list_add(&node->all, &all_head);
 
@@ -496,6 +667,19 @@ static void release_node(struct gcov_node *node)
 	kfree(node);
 }
 
+/*
+ * Reset all profiling data associated with the specified node.
+ */
+static void reset_node(struct gcov_node *node)
+{
+	int i;
+
+	if (node->unloaded_info)
+		gcov_info_reset(node->unloaded_info);
+	for (i = 0; i < node->num_loaded; i++)
+		gcov_info_reset(node->loaded_info[i]);
+}
+
 /* Release node and empty parents. Needs to be called with node_lock held. */
 static void remove_node(struct gcov_node *node)
 {
@@ -787,3 +971,5 @@ err_remove:
 	return rc;
 }
 device_initcall(gcov_fs_init);
+
+#endif /* Not a supported gcc version. */
diff --git a/kernel/gcov/gcc.c b/kernel/gcov/gcc.c
new file mode 100644
index 0000000..784ac44
--- /dev/null
+++ b/kernel/gcov/gcc.c
@@ -0,0 +1,461 @@
+/*
+ *  This code provides functions to handle gcc's profiling data format
+ *  introduced with gcc 3.4. Future versions of gcc may change the gcov
+ *  format (as happened before), so all format-specific information needs
+ *  to be kept modular and easily exchangeable.
+ *
+ *  This file is based on gcc-internal definitions. Functions and data
+ *  structures are defined to be compatible with gcc counterparts.
+ *  For a better understanding, refer to gcc source: gcc/gcov-io.h.
+ *
+ *    Copyright IBM Corp. 2009
+ *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *
+ *    Uses gcc-internal data definitions.
+ */
+
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/seq_file.h>
+#include <linux/vmalloc.h>
+#include "gcov.h"
+
+/* Symbolic links to be created for each profiling data file. */
+const struct gcov_link gcov_link[] = {
+	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
+	{ 0, NULL},
+};
+
+/*
+ * Determine whether a counter is active. Based on gcc magic. Doesn't change
+ * at run-time.
+ */
+static int counter_active(struct gcov_info *info, unsigned int type)
+{
+	return (1 << type) & info->ctr_mask;
+}
+
+/* Determine number of active counters. Based on gcc magic. */
+static unsigned int num_counter_active(struct gcov_info *info)
+{
+	unsigned int i;
+	unsigned int result = 0;
+
+	for (i = 0; i < GCOV_COUNTERS; i++) {
+		if (counter_active(info, i))
+			result++;
+	}
+	return result;
+}
+
+/**
+ * gcov_info_reset - reset profiling data to zero
+ * @info: profiling data set
+ */
+void gcov_info_reset(struct gcov_info *info)
+{
+	unsigned int active = num_counter_active(info);
+	unsigned int i;
+
+	for (i = 0; i < active; i++) {
+		memset(info->counts[i].values, 0,
+		       info->counts[i].num * sizeof(gcov_type));
+	}
+}
+
+/**
+ * gcov_info_is_compatible - check if profiling data can be added
+ * @info1: first profiling data set
+ * @info2: second profiling data set
+ *
+ * Returns non-zero if profiling data can be added, zero otherwise.
+ */
+int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
+{
+	return (info1->stamp == info2->stamp);
+}
+
+/**
+ * gcov_info_add - add up profiling data
+ * @dest: profiling data set to which data is added
+ * @source: profiling data set which is added
+ *
+ * Adds profiling counts of @source to @dest.
+ */
+void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
+{
+	unsigned int i;
+	unsigned int j;
+
+	for (i = 0; i < num_counter_active(dest); i++) {
+		for (j = 0; j < dest->counts[i].num; j++) {
+			dest->counts[i].values[j] +=
+				source->counts[i].values[j];
+		}
+	}
+}
+
+/* Get size of function info entry. Based on gcc magic. */
+static size_t get_fn_size(struct gcov_info *info)
+{
+	size_t size;
+
+	size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
+	       sizeof(unsigned int);
+	if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
+		size = ALIGN(size, __alignof__(struct gcov_fn_info));
+	return size;
+}
+
+/**
+ * gcov_info_dup - duplicate profiling data set
+ * @info: profiling data set to duplicate
+ *
+ * Return newly allocated duplicate on success, %NULL on error.
+ */
+struct gcov_info *gcov_info_dup(struct gcov_info *info)
+{
+	struct gcov_info *dup;
+	unsigned int i;
+	unsigned int active;
+
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
+	if (!dup)
+		return NULL;
+
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+	memcpy (dup, info, sizeof (struct gcov_info));
+	dup->next               = 0;
+	dup->mod_info           = 0;
+#else /* GCC_VERSION_EQUAL(3, 4) */
+	dup->version		= info->version;
+	dup->stamp		= info->stamp;
+	dup->n_functions	= info->n_functions;
+	dup->ctr_mask		= info->ctr_mask;
+#endif
+
+	/* Duplicate filename. */
+	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
+	if (!dup->filename)
+		goto err_free;
+	/* Duplicate table of functions. */
+	dup->functions = kmemdup(info->functions, info->n_functions *
+				 get_fn_size(info), GFP_KERNEL);
+	if (!dup->functions)
+		goto err_free;
+	/* Duplicate counter arrays. */
+	for (i = 0; i < active ; i++) {
+		struct gcov_ctr_info *ctr = &info->counts[i];
+		size_t size = ctr->num * sizeof(gcov_type);
+
+		dup->counts[i].num = ctr->num;
+		dup->counts[i].merge = ctr->merge;
+		dup->counts[i].values = vmalloc(size);
+		if (!dup->counts[i].values)
+			goto err_free;
+		memcpy(dup->counts[i].values, ctr->values, size);
+	}
+	return dup;
+
+err_free:
+	gcov_info_free(dup);
+	return NULL;
+}
+
+/**
+ * gcov_info_free - release memory for profiling data set duplicate
+ * @info: profiling data set duplicate to free
+ */
+void gcov_info_free(struct gcov_info *info)
+{
+	unsigned int active = num_counter_active(info);
+	unsigned int i;
+
+	for (i = 0; i < active ; i++)
+		vfree(info->counts[i].values);
+	kfree(info->functions);
+	kfree(info->filename);
+	kfree(info);
+}
+
+#if GCC_VERSION_EQUAL(3, 4)
+/* Get address of function info entry. Based on gcc magic. */
+static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
+{
+	return (struct gcov_fn_info *)
+		((char *) info->functions + fn * get_fn_size(info));
+}
+
+/**
+ * struct type_info - iterator helper array
+ * @ctr_type: counter type
+ * @offset: index of the first value of the current function for this type
+ *
+ * This array is needed to convert the in-memory data format into the in-file
+ * data format:
+ *
+ * In-memory:
+ *   for each counter type
+ *     for each function
+ *       values
+ *
+ * In-file:
+ *   for each function
+ *     for each counter type
+ *       values
+ *
+ * See gcc source gcc/gcov-io.h for more information on data organization.
+ */
+struct type_info {
+	int ctr_type;
+	unsigned int offset;
+};
+
+/**
+ * struct gcov_iterator - specifies current file position in logical records
+ * @info: associated profiling data
+ * @record: record type
+ * @function: function number
+ * @type: counter type
+ * @count: index into values array
+ * @num_types: number of counter types
+ * @type_info: helper array to get values-array offset for current function
+ */
+struct gcov_iterator {
+	struct gcov_info *info;
+
+	int record;
+	unsigned int function;
+	unsigned int type;
+	unsigned int count;
+
+	int num_types;
+	struct type_info type_info[0];
+};
+
+static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
+{
+	return get_fn_info(iter->info, iter->function);
+}
+
+static struct type_info *get_type(struct gcov_iterator *iter)
+{
+	return &iter->type_info[iter->type];
+}
+
+/**
+ * gcov_iter_new - allocate and initialize profiling data iterator
+ * @info: profiling data set to be iterated
+ *
+ * Return file iterator on success, %NULL otherwise.
+ */
+struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
+{
+	struct gcov_iterator *iter;
+
+	iter = kzalloc(sizeof(struct gcov_iterator) +
+		       num_counter_active(info) * sizeof(struct type_info),
+		       GFP_KERNEL);
+	if (iter)
+		iter->info = info;
+
+	return iter;
+}
+
+/**
+ * gcov_iter_free - release memory for iterator
+ * @iter: file iterator to free
+ */
+void gcov_iter_free(struct gcov_iterator *iter)
+{
+	kfree(iter);
+}
+
+/**
+ * gcov_iter_get_info - return profiling data set for given file iterator
+ * @iter: file iterator
+ */
+struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
+{
+	return iter->info;
+}
+
+/**
+ * gcov_iter_start - reset file iterator to starting position
+ * @iter: file iterator
+ */
+void gcov_iter_start(struct gcov_iterator *iter)
+{
+	int i;
+
+	iter->record = 0;
+	iter->function = 0;
+	iter->type = 0;
+	iter->count = 0;
+	iter->num_types = 0;
+	for (i = 0; i < GCOV_COUNTERS; i++) {
+		if (counter_active(iter->info, i)) {
+			iter->type_info[iter->num_types].ctr_type = i;
+			iter->type_info[iter->num_types++].offset = 0;
+		}
+	}
+}
+
+/* Mapping of logical record number to actual file content. */
+#define RECORD_FILE_MAGIC	0
+#define RECORD_GCOV_VERSION	1
+#define RECORD_TIME_STAMP	2
+#define RECORD_FUNCTION_TAG	3
+#define RECORD_FUNCTON_TAG_LEN	4
+#define RECORD_FUNCTION_IDENT	5
+#define RECORD_FUNCTION_CHECK	6
+#define RECORD_COUNT_TAG	7
+#define RECORD_COUNT_LEN	8
+#define RECORD_COUNT		9
+
+/**
+ * gcov_iter_next - advance file iterator to next logical record
+ * @iter: file iterator
+ *
+ * Return zero if new position is valid, non-zero if iterator has reached end.
+ */
+int gcov_iter_next(struct gcov_iterator *iter)
+{
+	switch (iter->record) {
+	case RECORD_FILE_MAGIC:
+	case RECORD_GCOV_VERSION:
+	case RECORD_FUNCTION_TAG:
+	case RECORD_FUNCTON_TAG_LEN:
+	case RECORD_FUNCTION_IDENT:
+	case RECORD_COUNT_TAG:
+		/* Advance to next record */
+		iter->record++;
+		break;
+	case RECORD_COUNT:
+		/* Advance to next count */
+		iter->count++;
+		/* fall through */
+	case RECORD_COUNT_LEN:
+		if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
+			iter->record = 9;
+			break;
+		}
+		/* Advance to next counter type */
+		get_type(iter)->offset += iter->count;
+		iter->count = 0;
+		iter->type++;
+		/* fall through */
+	case RECORD_FUNCTION_CHECK:
+		if (iter->type < iter->num_types) {
+			iter->record = 7;
+			break;
+		}
+		/* Advance to next function */
+		iter->type = 0;
+		iter->function++;
+		/* fall through */
+	case RECORD_TIME_STAMP:
+		if (iter->function < iter->info->n_functions)
+			iter->record = 3;
+		else
+			iter->record = -1;
+		break;
+	}
+	/* Check for EOF. */
+	if (iter->record == -1)
+		return -EINVAL;
+	else
+		return 0;
+}
+
+/**
+ * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
+ * @seq: seq_file handle
+ * @v: value to be stored
+ *
+ * Number format defined by gcc: numbers are recorded in the 32 bit
+ * unsigned binary form of the endianness of the machine generating the
+ * file.
+ */
+static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
+{
+	return seq_write(seq, &v, sizeof(v));
+}
+
+/**
+ * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
+ * @seq: seq_file handle
+ * @v: value to be stored
+ *
+ * Number format defined by gcc: numbers are recorded in the 32 bit
+ * unsigned binary form of the endianness of the machine generating the
+ * file. 64 bit numbers are stored as two 32 bit numbers, the low part
+ * first.
+ */
+static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
+{
+	u32 data[2];
+
+	data[0] = (v & 0xffffffffUL);
+	data[1] = (v >> 32);
+	return seq_write(seq, data, sizeof(data));
+}
+
+/**
+ * gcov_iter_write - write data for current pos to seq_file
+ * @iter: file iterator
+ * @seq: seq_file handle
+ *
+ * Return zero on success, non-zero otherwise.
+ */
+int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
+{
+	int rc = -EINVAL;
+
+	switch (iter->record) {
+	case RECORD_FILE_MAGIC:
+		rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
+		break;
+	case RECORD_GCOV_VERSION:
+		rc = seq_write_gcov_u32(seq, iter->info->version);
+		break;
+	case RECORD_TIME_STAMP:
+		rc = seq_write_gcov_u32(seq, iter->info->stamp);
+		break;
+	case RECORD_FUNCTION_TAG:
+		rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
+		break;
+	case RECORD_FUNCTON_TAG_LEN:
+		rc = seq_write_gcov_u32(seq, 2);
+		break;
+	case RECORD_FUNCTION_IDENT:
+		rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
+		break;
+	case RECORD_FUNCTION_CHECK:
+		rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
+		break;
+	case RECORD_COUNT_TAG:
+		rc = seq_write_gcov_u32(seq,
+			GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
+		break;
+	case RECORD_COUNT_LEN:
+		rc = seq_write_gcov_u32(seq,
+				get_func(iter)->n_ctrs[iter->type] * 2);
+		break;
+	case RECORD_COUNT:
+		rc = seq_write_gcov_u64(seq,
+			iter->info->counts[iter->type].
+				values[iter->count + get_type(iter)->offset]);
+		break;
+	}
+	return rc;
+}
+#endif /* GCC_VERSION_EQUAL(3, 4) */
+#endif /* Not a supported gcc version. */
diff --git a/kernel/gcov/gcc_3_4.c b/kernel/gcov/gcc_3_4.c
deleted file mode 100644
index ae5bb42..0000000
--- a/kernel/gcov/gcc_3_4.c
+++ /dev/null
@@ -1,447 +0,0 @@
-/*
- *  This code provides functions to handle gcc's profiling data format
- *  introduced with gcc 3.4. Future versions of gcc may change the gcov
- *  format (as happened before), so all format-specific information needs
- *  to be kept modular and easily exchangeable.
- *
- *  This file is based on gcc-internal definitions. Functions and data
- *  structures are defined to be compatible with gcc counterparts.
- *  For a better understanding, refer to gcc source: gcc/gcov-io.h.
- *
- *    Copyright IBM Corp. 2009
- *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
- *
- *    Uses gcc-internal data definitions.
- */
-
-#include <linux/errno.h>
-#include <linux/slab.h>
-#include <linux/string.h>
-#include <linux/seq_file.h>
-#include <linux/vmalloc.h>
-#include "gcov.h"
-
-/* Symbolic links to be created for each profiling data file. */
-const struct gcov_link gcov_link[] = {
-	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
-	{ 0, NULL},
-};
-
-/*
- * Determine whether a counter is active. Based on gcc magic. Doesn't change
- * at run-time.
- */
-static int counter_active(struct gcov_info *info, unsigned int type)
-{
-	return (1 << type) & info->ctr_mask;
-}
-
-/* Determine number of active counters. Based on gcc magic. */
-static unsigned int num_counter_active(struct gcov_info *info)
-{
-	unsigned int i;
-	unsigned int result = 0;
-
-	for (i = 0; i < GCOV_COUNTERS; i++) {
-		if (counter_active(info, i))
-			result++;
-	}
-	return result;
-}
-
-/**
- * gcov_info_reset - reset profiling data to zero
- * @info: profiling data set
- */
-void gcov_info_reset(struct gcov_info *info)
-{
-	unsigned int active = num_counter_active(info);
-	unsigned int i;
-
-	for (i = 0; i < active; i++) {
-		memset(info->counts[i].values, 0,
-		       info->counts[i].num * sizeof(gcov_type));
-	}
-}
-
-/**
- * gcov_info_is_compatible - check if profiling data can be added
- * @info1: first profiling data set
- * @info2: second profiling data set
- *
- * Returns non-zero if profiling data can be added, zero otherwise.
- */
-int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
-{
-	return (info1->stamp == info2->stamp);
-}
-
-/**
- * gcov_info_add - add up profiling data
- * @dest: profiling data set to which data is added
- * @source: profiling data set which is added
- *
- * Adds profiling counts of @source to @dest.
- */
-void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
-{
-	unsigned int i;
-	unsigned int j;
-
-	for (i = 0; i < num_counter_active(dest); i++) {
-		for (j = 0; j < dest->counts[i].num; j++) {
-			dest->counts[i].values[j] +=
-				source->counts[i].values[j];
-		}
-	}
-}
-
-/* Get size of function info entry. Based on gcc magic. */
-static size_t get_fn_size(struct gcov_info *info)
-{
-	size_t size;
-
-	size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
-	       sizeof(unsigned int);
-	if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
-		size = ALIGN(size, __alignof__(struct gcov_fn_info));
-	return size;
-}
-
-/* Get address of function info entry. Based on gcc magic. */
-static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
-{
-	return (struct gcov_fn_info *)
-		((char *) info->functions + fn * get_fn_size(info));
-}
-
-/**
- * gcov_info_dup - duplicate profiling data set
- * @info: profiling data set to duplicate
- *
- * Return newly allocated duplicate on success, %NULL on error.
- */
-struct gcov_info *gcov_info_dup(struct gcov_info *info)
-{
-	struct gcov_info *dup;
-	unsigned int i;
-	unsigned int active;
-
-	/* Duplicate gcov_info. */
-	active = num_counter_active(info);
-	dup = kzalloc(sizeof(struct gcov_info) +
-		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
-	if (!dup)
-		return NULL;
-	dup->version		= info->version;
-	dup->stamp		= info->stamp;
-	dup->n_functions	= info->n_functions;
-	dup->ctr_mask		= info->ctr_mask;
-	/* Duplicate filename. */
-	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
-	if (!dup->filename)
-		goto err_free;
-	/* Duplicate table of functions. */
-	dup->functions = kmemdup(info->functions, info->n_functions *
-				 get_fn_size(info), GFP_KERNEL);
-	if (!dup->functions)
-		goto err_free;
-	/* Duplicate counter arrays. */
-	for (i = 0; i < active ; i++) {
-		struct gcov_ctr_info *ctr = &info->counts[i];
-		size_t size = ctr->num * sizeof(gcov_type);
-
-		dup->counts[i].num = ctr->num;
-		dup->counts[i].merge = ctr->merge;
-		dup->counts[i].values = vmalloc(size);
-		if (!dup->counts[i].values)
-			goto err_free;
-		memcpy(dup->counts[i].values, ctr->values, size);
-	}
-	return dup;
-
-err_free:
-	gcov_info_free(dup);
-	return NULL;
-}
-
-/**
- * gcov_info_free - release memory for profiling data set duplicate
- * @info: profiling data set duplicate to free
- */
-void gcov_info_free(struct gcov_info *info)
-{
-	unsigned int active = num_counter_active(info);
-	unsigned int i;
-
-	for (i = 0; i < active ; i++)
-		vfree(info->counts[i].values);
-	kfree(info->functions);
-	kfree(info->filename);
-	kfree(info);
-}
-
-/**
- * struct type_info - iterator helper array
- * @ctr_type: counter type
- * @offset: index of the first value of the current function for this type
- *
- * This array is needed to convert the in-memory data format into the in-file
- * data format:
- *
- * In-memory:
- *   for each counter type
- *     for each function
- *       values
- *
- * In-file:
- *   for each function
- *     for each counter type
- *       values
- *
- * See gcc source gcc/gcov-io.h for more information on data organization.
- */
-struct type_info {
-	int ctr_type;
-	unsigned int offset;
-};
-
-/**
- * struct gcov_iterator - specifies current file position in logical records
- * @info: associated profiling data
- * @record: record type
- * @function: function number
- * @type: counter type
- * @count: index into values array
- * @num_types: number of counter types
- * @type_info: helper array to get values-array offset for current function
- */
-struct gcov_iterator {
-	struct gcov_info *info;
-
-	int record;
-	unsigned int function;
-	unsigned int type;
-	unsigned int count;
-
-	int num_types;
-	struct type_info type_info[0];
-};
-
-static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
-{
-	return get_fn_info(iter->info, iter->function);
-}
-
-static struct type_info *get_type(struct gcov_iterator *iter)
-{
-	return &iter->type_info[iter->type];
-}
-
-/**
- * gcov_iter_new - allocate and initialize profiling data iterator
- * @info: profiling data set to be iterated
- *
- * Return file iterator on success, %NULL otherwise.
- */
-struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
-{
-	struct gcov_iterator *iter;
-
-	iter = kzalloc(sizeof(struct gcov_iterator) +
-		       num_counter_active(info) * sizeof(struct type_info),
-		       GFP_KERNEL);
-	if (iter)
-		iter->info = info;
-
-	return iter;
-}
-
-/**
- * gcov_iter_free - release memory for iterator
- * @iter: file iterator to free
- */
-void gcov_iter_free(struct gcov_iterator *iter)
-{
-	kfree(iter);
-}
-
-/**
- * gcov_iter_get_info - return profiling data set for given file iterator
- * @iter: file iterator
- */
-struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
-{
-	return iter->info;
-}
-
-/**
- * gcov_iter_start - reset file iterator to starting position
- * @iter: file iterator
- */
-void gcov_iter_start(struct gcov_iterator *iter)
-{
-	int i;
-
-	iter->record = 0;
-	iter->function = 0;
-	iter->type = 0;
-	iter->count = 0;
-	iter->num_types = 0;
-	for (i = 0; i < GCOV_COUNTERS; i++) {
-		if (counter_active(iter->info, i)) {
-			iter->type_info[iter->num_types].ctr_type = i;
-			iter->type_info[iter->num_types++].offset = 0;
-		}
-	}
-}
-
-/* Mapping of logical record number to actual file content. */
-#define RECORD_FILE_MAGIC	0
-#define RECORD_GCOV_VERSION	1
-#define RECORD_TIME_STAMP	2
-#define RECORD_FUNCTION_TAG	3
-#define RECORD_FUNCTON_TAG_LEN	4
-#define RECORD_FUNCTION_IDENT	5
-#define RECORD_FUNCTION_CHECK	6
-#define RECORD_COUNT_TAG	7
-#define RECORD_COUNT_LEN	8
-#define RECORD_COUNT		9
-
-/**
- * gcov_iter_next - advance file iterator to next logical record
- * @iter: file iterator
- *
- * Return zero if new position is valid, non-zero if iterator has reached end.
- */
-int gcov_iter_next(struct gcov_iterator *iter)
-{
-	switch (iter->record) {
-	case RECORD_FILE_MAGIC:
-	case RECORD_GCOV_VERSION:
-	case RECORD_FUNCTION_TAG:
-	case RECORD_FUNCTON_TAG_LEN:
-	case RECORD_FUNCTION_IDENT:
-	case RECORD_COUNT_TAG:
-		/* Advance to next record */
-		iter->record++;
-		break;
-	case RECORD_COUNT:
-		/* Advance to next count */
-		iter->count++;
-		/* fall through */
-	case RECORD_COUNT_LEN:
-		if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
-			iter->record = 9;
-			break;
-		}
-		/* Advance to next counter type */
-		get_type(iter)->offset += iter->count;
-		iter->count = 0;
-		iter->type++;
-		/* fall through */
-	case RECORD_FUNCTION_CHECK:
-		if (iter->type < iter->num_types) {
-			iter->record = 7;
-			break;
-		}
-		/* Advance to next function */
-		iter->type = 0;
-		iter->function++;
-		/* fall through */
-	case RECORD_TIME_STAMP:
-		if (iter->function < iter->info->n_functions)
-			iter->record = 3;
-		else
-			iter->record = -1;
-		break;
-	}
-	/* Check for EOF. */
-	if (iter->record == -1)
-		return -EINVAL;
-	else
-		return 0;
-}
-
-/**
- * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
- * @seq: seq_file handle
- * @v: value to be stored
- *
- * Number format defined by gcc: numbers are recorded in the 32 bit
- * unsigned binary form of the endianness of the machine generating the
- * file.
- */
-static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
-{
-	return seq_write(seq, &v, sizeof(v));
-}
-
-/**
- * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
- * @seq: seq_file handle
- * @v: value to be stored
- *
- * Number format defined by gcc: numbers are recorded in the 32 bit
- * unsigned binary form of the endianness of the machine generating the
- * file. 64 bit numbers are stored as two 32 bit numbers, the low part
- * first.
- */
-static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
-{
-	u32 data[2];
-
-	data[0] = (v & 0xffffffffUL);
-	data[1] = (v >> 32);
-	return seq_write(seq, data, sizeof(data));
-}
-
-/**
- * gcov_iter_write - write data for current pos to seq_file
- * @iter: file iterator
- * @seq: seq_file handle
- *
- * Return zero on success, non-zero otherwise.
- */
-int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
-{
-	int rc = -EINVAL;
-
-	switch (iter->record) {
-	case RECORD_FILE_MAGIC:
-		rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
-		break;
-	case RECORD_GCOV_VERSION:
-		rc = seq_write_gcov_u32(seq, iter->info->version);
-		break;
-	case RECORD_TIME_STAMP:
-		rc = seq_write_gcov_u32(seq, iter->info->stamp);
-		break;
-	case RECORD_FUNCTION_TAG:
-		rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
-		break;
-	case RECORD_FUNCTON_TAG_LEN:
-		rc = seq_write_gcov_u32(seq, 2);
-		break;
-	case RECORD_FUNCTION_IDENT:
-		rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
-		break;
-	case RECORD_FUNCTION_CHECK:
-		rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
-		break;
-	case RECORD_COUNT_TAG:
-		rc = seq_write_gcov_u32(seq,
-			GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
-		break;
-	case RECORD_COUNT_LEN:
-		rc = seq_write_gcov_u32(seq,
-				get_func(iter)->n_ctrs[iter->type] * 2);
-		break;
-	case RECORD_COUNT:
-		rc = seq_write_gcov_u64(seq,
-			iter->info->counts[iter->type].
-				values[iter->count + get_type(iter)->offset]);
-		break;
-	}
-	return rc;
-}
diff --git a/kernel/gcov/gcc_version_check.h b/kernel/gcov/gcc_version_check.h
new file mode 100644
index 0000000..ebfd040
--- /dev/null
+++ b/kernel/gcov/gcc_version_check.h
@@ -0,0 +1,16 @@
+/* This file defines macros to check gcc version number. */
+
+#ifndef GCC_VERSION_CHECH_H
+#define GCC_VERSION_CHECH_H
+
+#define GCC_VERSION_GREATER_OR_EQUAL(major, minor, patch_level) \
+                                ((__GNUC__ > major) || \
+                                 ((__GNUC__ == major) && \
+                                  (__GNUC_MINOR__ > minor)) || \
+                                 ((__GNUC__ == major) && \
+                                  (__GNUC_MINOR__ == minor) && \
+                                  (__GNUC_PATCHLEVEL__ >= patch_level)))
+#define GCC_VERSION_EQUAL(major, minor) ((__GNUC__ == major) && \
+                                         (__GNUC_MINOR__ == minor))
+
+#endif /* ! GCC_VERSION_CHECH_H */
diff --git a/kernel/gcov/gcov.h b/kernel/gcov/gcov.h
index 060073e..37d76ea 100644
--- a/kernel/gcov/gcov.h
+++ b/kernel/gcov/gcov.h
@@ -14,8 +14,19 @@
 #ifndef GCOV_H
 #define GCOV_H GCOV_H
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #include <linux/types.h>
 
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+
+#define IN_LIBGCOV 1
+#define IN_GCOV 0
+/* this gcov-io.h file is copied from gcc source tree */
+#include "gcov-src/gcov-io.h"
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
 /*
  * Profiling data types used for gcc 3.4 and above - these are defined by
  * gcc and need to be kept as close to the original definition as possible to
@@ -88,6 +99,7 @@ struct gcov_info {
 	unsigned int			ctr_mask;
 	struct gcov_ctr_info		counts[0];
 };
+#endif /* GCC_VERSION */
 
 /* Base interface. */
 enum gcov_action {
@@ -125,4 +137,5 @@ struct gcov_link {
 };
 extern const struct gcov_link gcov_link[];
 
+#endif /* GCC_VERSION */
 #endif /* GCOV_H */
-- 
1.7.3.1


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-13 18:44   ` Xinliang David Li
                       ` (2 preceding siblings ...)
  2011-05-13 22:31     ` Rong Xu
@ 2011-05-15 19:34     ` Jan Hubicka
  2011-05-17  2:41       ` Xinliang David Li
                         ` (2 more replies)
  3 siblings, 3 replies; 24+ messages in thread
From: Jan Hubicka @ 2011-05-15 19:34 UTC (permalink / raw)
  To: Xinliang David Li; +Cc: Paolo Bonzini, Rong Xu, reply, gcc-patches

> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
> > On 05/13/2011 03:03 AM, Rong Xu wrote:
> >>
> >>        * gcc/coverage.c        (revision 173717): set a flag if building
> >> for Linux kernel.
> >>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
> >> declarations for Linux kernel builds.
> >
> > I think this should be done without touching at all the profiling machinery
> > in GCC.
> >
> > 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
> > model would simply force targetm.have_tls to false.
> >
> 
> This is a good idea.
> 
> 
> > 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
> > kernel, and GCC needs no change at all here.
> >
> 
> In fact -- reuse gcc code profiling machinery for FDO is the KEY
> objective in this effort --- the effort tries to minimize gcc changes
> by refactoring gcc code and isolating/abstracting away part of gcc
> implementation that is user space program specific without using
> runtime hooks.  Aside from the kernel FDO change -- the refactoring
> itself actually makes the libgcov code more readable -- the existing
> implementation has many huge functions etc.
> 
> Kernel source has their implementation of coverage testing -- but it
> makes lots of data structure assumptions and hard coded -- it can
> easily out of sync with gcc and is considered  unmaintainable.

Yep,
I think it does make sense to share the implementation, but we need to find
resonable way to do so.  I guess we could separate out the i/o bits
into interface generic enough to cover the needs, move libgcov into its
own directory (just like libgcc is these days) and add an configury option
that sets the interface.  The kernel's interface can then be implemented
in a single file instead of tons of ifdefs and I guess can sit either in kernel
or gcc tree...

Honza
> 
> Rong will have more explanation on the design.
> 
> Thanks,
> 
> David
> 
> 
> > BTW, these parts of LIPO:
> >
> >> +      if (!is_kernel_build)
> >> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
> >> +         decl_default_tls_model (dc_gcov_type_ptr_var);
> >>
> >>       dc_void_ptr_var =
> >>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
> >> @@ -1488,8 +1493,9 @@
> >>                    ptr_void);
> >>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
> >>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
> >> -      DECL_TLS_MODEL (dc_void_ptr_var) =
> >> -       decl_default_tls_model (dc_void_ptr_var);
> >> +      if (!is_kernel_build)
> >> +        DECL_TLS_MODEL (dc_void_ptr_var) =
> >> +         decl_default_tls_model (dc_void_ptr_var);
> >
> > Probably are missing a !targetm.have_tls.
> >
> > Paolo
> >

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-15 19:34     ` Jan Hubicka
@ 2011-05-17  2:41       ` Xinliang David Li
  2011-05-17  5:28       ` Andi Kleen
  2011-05-17  7:10       ` Rong Xu
  2 siblings, 0 replies; 24+ messages in thread
From: Xinliang David Li @ 2011-05-17  2:41 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: Paolo Bonzini, Rong Xu, reply, gcc-patches

Agreed.

David

On Sun, May 15, 2011 at 3:27 AM, Jan Hubicka <hubicka@ucw.cz> wrote:
>> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>> > On 05/13/2011 03:03 AM, Rong Xu wrote:
>> >>
>> >>        * gcc/coverage.c        (revision 173717): set a flag if building
>> >> for Linux kernel.
>> >>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>> >> declarations for Linux kernel builds.
>> >
>> > I think this should be done without touching at all the profiling machinery
>> > in GCC.
>> >
>> > 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>> > model would simply force targetm.have_tls to false.
>> >
>>
>> This is a good idea.
>>
>>
>> > 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>> > kernel, and GCC needs no change at all here.
>> >
>>
>> In fact -- reuse gcc code profiling machinery for FDO is the KEY
>> objective in this effort --- the effort tries to minimize gcc changes
>> by refactoring gcc code and isolating/abstracting away part of gcc
>> implementation that is user space program specific without using
>> runtime hooks.  Aside from the kernel FDO change -- the refactoring
>> itself actually makes the libgcov code more readable -- the existing
>> implementation has many huge functions etc.
>>
>> Kernel source has their implementation of coverage testing -- but it
>> makes lots of data structure assumptions and hard coded -- it can
>> easily out of sync with gcc and is considered  unmaintainable.
>
> Yep,
> I think it does make sense to share the implementation, but we need to find
> resonable way to do so.  I guess we could separate out the i/o bits
> into interface generic enough to cover the needs, move libgcov into its
> own directory (just like libgcc is these days) and add an configury option
> that sets the interface.  The kernel's interface can then be implemented
> in a single file instead of tons of ifdefs and I guess can sit either in kernel
> or gcc tree...
>
> Honza
>>
>> Rong will have more explanation on the design.
>>
>> Thanks,
>>
>> David
>>
>>
>> > BTW, these parts of LIPO:
>> >
>> >> +      if (!is_kernel_build)
>> >> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>> >> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>> >>
>> >>       dc_void_ptr_var =
>> >>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>> >> @@ -1488,8 +1493,9 @@
>> >>                    ptr_void);
>> >>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>> >>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>> >> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>> >> -       decl_default_tls_model (dc_void_ptr_var);
>> >> +      if (!is_kernel_build)
>> >> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>> >> +         decl_default_tls_model (dc_void_ptr_var);
>> >
>> > Probably are missing a !targetm.have_tls.
>> >
>> > Paolo
>> >
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-15 19:34     ` Jan Hubicka
  2011-05-17  2:41       ` Xinliang David Li
@ 2011-05-17  5:28       ` Andi Kleen
  2011-05-17  5:30         ` Rong Xu
  2011-05-17  6:35         ` Xinliang David Li
  2011-05-17  7:10       ` Rong Xu
  2 siblings, 2 replies; 24+ messages in thread
From: Andi Kleen @ 2011-05-17  5:28 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: Xinliang David Li, Paolo Bonzini, Rong Xu, reply, gcc-patches

Jan Hubicka <hubicka@ucw.cz> writes:
> Yep,
> I think it does make sense to share the implementation, but we need to find
> resonable way to do so. 

I doubt this will be very popular with the kernel community, which
prefers self contained code.

Also the interface doesn't change that often or does it?

The current kernel code is for gcc 3. That could be simply
replaced with a modern gcc 4 interface.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  5:28       ` Andi Kleen
@ 2011-05-17  5:30         ` Rong Xu
  2011-05-17  7:03           ` Andi Kleen
  2011-05-17  6:35         ` Xinliang David Li
  1 sibling, 1 reply; 24+ messages in thread
From: Rong Xu @ 2011-05-17  5:30 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Jan Hubicka, Xinliang David Li, Paolo Bonzini, reply, gcc-patches

On Mon, May 16, 2011 at 3:38 PM, Andi Kleen <andi@firstfloor.org> wrote:
> Jan Hubicka <hubicka@ucw.cz> writes:
>> Yep,
>> I think it does make sense to share the implementation, but we need to find
>> resonable way to do so.
>
> I doubt this will be very popular with the kernel community, which
> prefers self contained code.
>
> Also the interface doesn't change that often or does it?

Hard to say, we changed the gcov_info data structure several times.

>
> The current kernel code is for gcc 3. That could be simply
> replaced with a modern gcc 4 interface.

You cannot replace. You have to keep the code for every gcc versions
that being used.

>
> -Andi
>
> --
> ak@linux.intel.com -- Speaking for myself only
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  5:28       ` Andi Kleen
  2011-05-17  5:30         ` Rong Xu
@ 2011-05-17  6:35         ` Xinliang David Li
  2011-05-17  6:36           ` Jan Hubicka
  1 sibling, 1 reply; 24+ messages in thread
From: Xinliang David Li @ 2011-05-17  6:35 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Jan Hubicka, Paolo Bonzini, Rong Xu, reply, gcc-patches

On Mon, May 16, 2011 at 3:38 PM, Andi Kleen <andi@firstfloor.org> wrote:
> Jan Hubicka <hubicka@ucw.cz> writes:
>> Yep,
>> I think it does make sense to share the implementation, but we need to find
>> resonable way to do so.
>
> I doubt this will be very popular with the kernel community, which
> prefers self contained code.

Won't be surprised if there are objections ..

>
> Also the interface doesn't change that often or does it?

As FDO gains popularity,  the profile coverage code will change faster
than people may think.  To quote some potential ones -- lightweight
ipo, path profiling, call trace profiling etc.

>
> The current kernel code is for gcc 3. That could be simply
> replaced with a modern gcc 4 interface.

Rong's approach will let kernel get coverage + FDO support almost for
free -- there is no need for kernel to maintain something is basically
not maintainable.

Thanks,

David

>
> -Andi
>
> --
> ak@linux.intel.com -- Speaking for myself only
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  6:35         ` Xinliang David Li
@ 2011-05-17  6:36           ` Jan Hubicka
  0 siblings, 0 replies; 24+ messages in thread
From: Jan Hubicka @ 2011-05-17  6:36 UTC (permalink / raw)
  To: Xinliang David Li
  Cc: Andi Kleen, Jan Hubicka, Paolo Bonzini, Rong Xu, reply, gcc-patches

> On Mon, May 16, 2011 at 3:38 PM, Andi Kleen <andi@firstfloor.org> wrote:
> > Jan Hubicka <hubicka@ucw.cz> writes:
> >> Yep,
> >> I think it does make sense to share the implementation, but we need to find
> >> resonable way to do so.
> >
> > I doubt this will be very popular with the kernel community, which
> > prefers self contained code.
> 
> Won't be surprised if there are objections ..
> 
> >
> > Also the interface doesn't change that often or does it?
> 
> As FDO gains popularity,  the profile coverage code will change faster
> than people may think.  To quote some potential ones -- lightweight
> ipo, path profiling, call trace profiling etc.
> 
> >
> > The current kernel code is for gcc 3. That could be simply
> > replaced with a modern gcc 4 interface.
> 
> Rong's approach will let kernel get coverage + FDO support almost for
> free -- there is no need for kernel to maintain something is basically
> not maintainable.

One other plus for "embeddable gcov" is that Linux kernel is not the only
potential consumer of this infrastructure.  So from sole gcov maintainer POV, I
think adding such support for interface an dmaking libgcov building in its own
directory is a positive thing.  I doubt I will however have time to implement
it myself this stage1 - the list of high priority things is just too long.
Patches would be welcome, however.  If kernel developers decides to maintain
its own variant of libgcov, it is not big problem with me either and I am
trying to keep the interface stable unless there are good reasons to break it.

It is however clear that we will break libgcov interface this GCC release (we
already did) and given new interest in it, probably in releases to come, too.

Honza

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  5:30         ` Rong Xu
@ 2011-05-17  7:03           ` Andi Kleen
  2011-05-17  7:11             ` Rong Xu
  0 siblings, 1 reply; 24+ messages in thread
From: Andi Kleen @ 2011-05-17  7:03 UTC (permalink / raw)
  To: Rong Xu; +Cc: Jan Hubicka, Xinliang David Li, Paolo Bonzini, reply, gcc-patches

Rong Xu <xur@google.com> writes:

>> The current kernel code is for gcc 3. That could be simply
>> replaced with a modern gcc 4 interface.
>
> You cannot replace. You have to keep the code for every gcc versions
> that being used.

I don't think it's a problem to not support gcc 3 gcov/profiling
anymore.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-15 19:34     ` Jan Hubicka
  2011-05-17  2:41       ` Xinliang David Li
  2011-05-17  5:28       ` Andi Kleen
@ 2011-05-17  7:10       ` Rong Xu
  2 siblings, 0 replies; 24+ messages in thread
From: Rong Xu @ 2011-05-17  7:10 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: Xinliang David Li, Paolo Bonzini, reply, gcc-patches

On Sun, May 15, 2011 at 3:27 AM, Jan Hubicka <hubicka@ucw.cz> wrote:
>> On Fri, May 13, 2011 at 5:54 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>> > On 05/13/2011 03:03 AM, Rong Xu wrote:
>> >>
>> >>        * gcc/coverage.c        (revision 173717): set a flag if building
>> >> for Linux kernel.
>> >>        * gcc/tree-profile.c    (revision 173717): don't emit TLS
>> >> declarations for Linux kernel builds.
>> >
>> > I think this should be done without touching at all the profiling machinery
>> > in GCC.
>> >
>> > 1) add a new TLS model -ftls-model=none and make the kernel uses it. The
>> > model would simply force targetm.have_tls to false.
>> >
>>
>> This is a good idea.
>>
>>
>> > 2) as Richi mentioned, gcov-io and libgcov changes then can move to the
>> > kernel, and GCC needs no change at all here.
>> >
>>
>> In fact -- reuse gcc code profiling machinery for FDO is the KEY
>> objective in this effort --- the effort tries to minimize gcc changes
>> by refactoring gcc code and isolating/abstracting away part of gcc
>> implementation that is user space program specific without using
>> runtime hooks.  Aside from the kernel FDO change -- the refactoring
>> itself actually makes the libgcov code more readable -- the existing
>> implementation has many huge functions etc.
>>
>> Kernel source has their implementation of coverage testing -- but it
>> makes lots of data structure assumptions and hard coded -- it can
>> easily out of sync with gcc and is considered  unmaintainable.
>
> Yep,
> I think it does make sense to share the implementation, but we need to find
> resonable way to do so.  I guess we could separate out the i/o bits
> into interface generic enough to cover the needs, move libgcov into its
> own directory (just like libgcc is these days) and add an configury option

Honza, Thanks for the comments. Can you elaborate what the options
will be look like?

-Rong

> that sets the interface.  The kernel's interface can then be implemented
> in a single file instead of tons of ifdefs and I guess can sit either in kernel
> or gcc tree...
>
> Honza
>>
>> Rong will have more explanation on the design.
>>
>> Thanks,
>>
>> David
>>
>>
>> > BTW, these parts of LIPO:
>> >
>> >> +      if (!is_kernel_build)
>> >> +        DECL_TLS_MODEL (dc_gcov_type_ptr_var) =
>> >> +         decl_default_tls_model (dc_gcov_type_ptr_var);
>> >>
>> >>       dc_void_ptr_var =
>> >>        build_decl (UNKNOWN_LOCATION, VAR_DECL,
>> >> @@ -1488,8 +1493,9 @@
>> >>                    ptr_void);
>> >>       DECL_ARTIFICIAL (dc_void_ptr_var) = 1;
>> >>       DECL_EXTERNAL (dc_void_ptr_var) = 1;
>> >> -      DECL_TLS_MODEL (dc_void_ptr_var) =
>> >> -       decl_default_tls_model (dc_void_ptr_var);
>> >> +      if (!is_kernel_build)
>> >> +        DECL_TLS_MODEL (dc_void_ptr_var) =
>> >> +         decl_default_tls_model (dc_void_ptr_var);
>> >
>> > Probably are missing a !targetm.have_tls.
>> >
>> > Paolo
>> >
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  7:03           ` Andi Kleen
@ 2011-05-17  7:11             ` Rong Xu
  2011-10-14  3:44               ` vulcansh
  0 siblings, 1 reply; 24+ messages in thread
From: Rong Xu @ 2011-05-17  7:11 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Jan Hubicka, Xinliang David Li, Paolo Bonzini, reply, gcc-patches

That will be good.
But you never know, we internally have fixed some bugs that filed to
us because people use kernel's old gcov code (many versions guarded by
ifdef) for their tests.

-Rong

On Mon, May 16, 2011 at 4:23 PM, Andi Kleen <andi@firstfloor.org> wrote:
> Rong Xu <xur@google.com> writes:
>
>>> The current kernel code is for gcc 3. That could be simply
>>> replaced with a modern gcc 4 interface.
>>
>> You cannot replace. You have to keep the code for every gcc versions
>> that being used.
>
> I don't think it's a problem to not support gcc 3 gcov/profiling
> anymore.
>
> -Andi
>
> --
> ak@linux.intel.com -- Speaking for myself only
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-05-17  7:11             ` Rong Xu
@ 2011-10-14  3:44               ` vulcansh
  2011-10-14  6:51                 ` Xinliang David Li
  0 siblings, 1 reply; 24+ messages in thread
From: vulcansh @ 2011-10-14  3:44 UTC (permalink / raw)
  To: gcc-patches



Rong Xu wrote:
> 
> That will be good.
> But you never know, we internally have fixed some bugs that filed to
> us because people use kernel's old gcov code (many versions guarded by
> ifdef) for their tests.
> 
> -Rong
> 

Has there been any progress one this patch?  What version of gcc is this
patch for?  I am interested in something that works with gcc 4.7.

-Steve

-- 
View this message in context: http://old.nabble.com/-google---support-for-building-Linux-kernel-with-FDO-%28issue4523061%29-tp31607746p32649731.html
Sent from the gcc - patches mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-10-14  3:44               ` vulcansh
@ 2011-10-14  6:51                 ` Xinliang David Li
  2011-10-14 22:59                   ` vulcansh
  0 siblings, 1 reply; 24+ messages in thread
From: Xinliang David Li @ 2011-10-14  6:51 UTC (permalink / raw)
  To: vulcansh; +Cc: gcc-patches, Rong Xu, Tian

This patch is for google/main which is 4.7 based, but the validated
version is in google_46 branch (which is based on 4.6).

By the way (given that you are from intel),  do you know if linux
kernel can be built with icc with PGO turned on? Our intern Xiaotian
has tried to use icc (12.0) to built kernel, and had some problems.
The bootable kernel built with icc + gcc (for those failed with icc)
does not perform quite well.

Thanks,

David


On Thu, Oct 13, 2011 at 7:02 PM, vulcansh <steven.t.hampson@intel.com> wrote:
>
>
> Rong Xu wrote:
>>
>> That will be good.
>> But you never know, we internally have fixed some bugs that filed to
>> us because people use kernel's old gcov code (many versions guarded by
>> ifdef) for their tests.
>>
>> -Rong
>>
>
> Has there been any progress one this patch?  What version of gcc is this
> patch for?  I am interested in something that works with gcc 4.7.
>
> -Steve
>
> --
> View this message in context: http://old.nabble.com/-google---support-for-building-Linux-kernel-with-FDO-%28issue4523061%29-tp31607746p32649731.html
> Sent from the gcc - patches mailing list archive at Nabble.com.
>
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
  2011-10-14  6:51                 ` Xinliang David Li
@ 2011-10-14 22:59                   ` vulcansh
       [not found]                     ` <CAAkRFZ+54idDZZz2xDb4K+132pSva-1LB-3Zeb__ma_O20Kt4w@mail.gmail.com>
  0 siblings, 1 reply; 24+ messages in thread
From: vulcansh @ 2011-10-14 22:59 UTC (permalink / raw)
  To: gcc-patches


I found this thread through a Google search so I didn't have much context. 
Can you point me to the google/main source repo for gcc and the kernel?  Are
there plans to migrate this patch to the gcc and Linux mainline?

Sorry, but I have never used the PGO option in icc.

Steve


Xinliang David Li wrote:
> 
> This patch is for google/main which is 4.7 based, but the validated
> version is in google_46 branch (which is based on 4.6).
> 
> By the way (given that you are from intel),  do you know if linux
> kernel can be built with icc with PGO turned on? Our intern Xiaotian
> has tried to use icc (12.0) to built kernel, and had some problems.
> The bootable kernel built with icc + gcc (for those failed with icc)
> does not perform quite well.
> 

-- 
View this message in context: http://old.nabble.com/-google---support-for-building-Linux-kernel-with-FDO-%28issue4523061%29-tp31607746p32655618.html
Sent from the gcc - patches mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [google] support for building Linux kernel with FDO (issue4523061)
       [not found]                     ` <CAAkRFZ+54idDZZz2xDb4K+132pSva-1LB-3Zeb__ma_O20Kt4w@mail.gmail.com>
@ 2011-10-15  4:50                       ` Rong Xu
  0 siblings, 0 replies; 24+ messages in thread
From: Rong Xu @ 2011-10-15  4:50 UTC (permalink / raw)
  To: gcc-patches; +Cc: vulcansh, Xinliang David Li

You can access gcc side of patch from the following two branches:
svn://gcc.gnu.org/svn/gcc/branches/google/mainorsvn://gcc.gnu.org/svn/gcc/branches/google/gcc-4_6_branch
I have the patch for 2.6.36 kernel and 2.6.34 kernel and I believe I
attached 2.6.36 patch to one of the early review emails. Let me know
you cannot find it.

We do plan to submit to trunk for both gcc and kernel changes. But we
don't expect it happens shortly.

-Rong> From: vulcansh <steven.t.hampson@intel.com>
> Date: Fri, Oct 14, 2011 at 2:53 PM
> Subject: Re: [google] support for building Linux kernel with FDO (issue4523061)
> To: gcc-patches@gcc.gnu.org
>
>
>
> I found this thread through a Google search so I didn't have much context.
> Can you point me to the google/main source repo for gcc and the kernel?  Are
> there plans to migrate this patch to the gcc and Linux mainline?
>
> Sorry, but I have never used the PGO option in icc.
>
> Steve
>
>
> Xinliang David Li wrote:
> >
> > This patch is for google/main which is 4.7 based, but the validated
> > version is in google_46 branch (which is based on 4.6).
> >
> > By the way (given that you are from intel),  do you know if linux
> > kernel can be built with icc with PGO turned on? Our intern Xiaotian
> > has tried to use icc (12.0) to built kernel, and had some problems.
> > The bootable kernel built with icc + gcc (for those failed with icc)
> > does not perform quite well.
> >
>
> --
> View this message in context:
> http://old.nabble.com/-google---support-for-building-Linux-kernel-with-FDO-%28issue4523061%29-tp31607746p32655618.html
> Sent from the gcc - patches mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2011-10-15  0:15 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-13  9:10 [google] support for building Linux kernel with FDO (issue4523061) Rong Xu
2011-05-13  9:23 ` Xinliang David Li
2011-05-13 10:12   ` Richard Guenther
2011-05-13 12:43     ` Jan Hubicka
2011-05-13 17:14       ` Andi Kleen
2011-05-13 13:58 ` Paolo Bonzini
2011-05-13 18:44   ` Xinliang David Li
2011-05-13 18:59     ` Paolo Bonzini
2011-05-13 22:24     ` Rong Xu
2011-05-13 22:31     ` Rong Xu
2011-05-13 22:33       ` Rong Xu
2011-05-15 19:34     ` Jan Hubicka
2011-05-17  2:41       ` Xinliang David Li
2011-05-17  5:28       ` Andi Kleen
2011-05-17  5:30         ` Rong Xu
2011-05-17  7:03           ` Andi Kleen
2011-05-17  7:11             ` Rong Xu
2011-10-14  3:44               ` vulcansh
2011-10-14  6:51                 ` Xinliang David Li
2011-10-14 22:59                   ` vulcansh
     [not found]                     ` <CAAkRFZ+54idDZZz2xDb4K+132pSva-1LB-3Zeb__ma_O20Kt4w@mail.gmail.com>
2011-10-15  4:50                       ` Rong Xu
2011-05-17  6:35         ` Xinliang David Li
2011-05-17  6:36           ` Jan Hubicka
2011-05-17  7:10       ` Rong Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).