New Option --avg-transtab-entry-size=<number> can be used to tune
  the size of the translation table sectors, either to gain memory
  or to avoid too many retranslations.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15016 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/NEWS b/NEWS
index 4a57638..1e29c4e 100644
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,10 @@
   Useful to reduce memory use or increase the stack size if Valgrind
   segfaults due to stack exhausted.
 
+* New Option --avg-transtab-entry-size=<number> can be used to tune
+  the size of the translation table sectors, either to gain memory
+  or to avoid too many retranslations.
+
 * Valgrind can be built with Intel's ICC compiler. The required
   compiler version is 14.0 or later.
 
diff --git a/coregrind/m_main.c b/coregrind/m_main.c
index a1de69c..0f902f1 100644
--- a/coregrind/m_main.c
+++ b/coregrind/m_main.c
@@ -194,6 +194,8 @@
 "           program counters in max <number> frames) [0]\n"
 "    --num-transtab-sectors=<number> size of translated code cache [%d]\n"
 "           more sectors may increase performance, but use more memory.\n"
+"    --avg-transtab-entry-size=<number> avg size in bytes of a translated\n"
+"           basic block [0, meaning use tool provided default]\n"
 "    --aspace-minaddr=0xPP     avoid mapping memory below 0xPP [guessed]\n"
 "    --valgrind-stacksize=<number> size of valgrind (host) thread's stack\n"
 "                               (in bytes) ["
@@ -694,6 +696,9 @@
       else if VG_BINT_CLO(arg, "--num-transtab-sectors",
                                VG_(clo_num_transtab_sectors),
                                MIN_N_SECTORS, MAX_N_SECTORS) {}
+      else if VG_BINT_CLO(arg, "--avg-transtab-entry-size",
+                               VG_(clo_avg_transtab_entry_size),
+                               50, 5000) {}
       else if VG_BINT_CLO(arg, "--merge-recursive-frames",
                                VG_(clo_merge_recursive_frames), 0,
                                VG_DEEPEST_BACKTRACE) {}
diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c
index b3d136b..f28c3f3 100644
--- a/coregrind/m_transtab.c
+++ b/coregrind/m_transtab.c
@@ -59,6 +59,10 @@
    Will be set by VG_(init_tt_tc) to VG_(clo_num_transtab_sectors). */
 static UInt n_sectors = 0;
 
+/* Average size of a transtab code entry. 0 means to use the tool
+   provided default. */
+UInt VG_(clo_avg_transtab_entry_size) = 0;
+
 /*------------------ CONSTANTS ------------------*/
 /* Number of TC entries in each sector.  This needs to be a prime
    number to work properly, it must be <= 65535 (so that a TT index
@@ -2223,7 +2227,10 @@
                    "(startup of code management)\n");
 
    /* Figure out how big each tc area should be.  */
-   avg_codeszQ   = (VG_(details).avg_translation_sizeB + 7) / 8;
+   if (VG_(clo_avg_transtab_entry_size) == 0)
+      avg_codeszQ   = (VG_(details).avg_translation_sizeB + 7) / 8;
+   else
+      avg_codeszQ   = (VG_(clo_avg_transtab_entry_size) + 7) / 8;
    tc_sector_szQ = N_TTES_PER_SECTOR_USABLE * (1 + avg_codeszQ);
 
    /* Ensure the calculated value is not way crazy. */
@@ -2254,6 +2261,13 @@
    if (VG_(clo_verbosity) > 2 || VG_(clo_stats)
        || VG_(debugLog_getLevel) () >= 2) {
       VG_(message)(Vg_DebugMsg,
+         "TT/TC: cache: %s--avg-transtab-entry-size=%d, " 
+         "%stool provided default %d\n",
+         VG_(clo_avg_transtab_entry_size) == 0 ? "ignoring " : "using ",
+         VG_(clo_avg_transtab_entry_size),
+         VG_(clo_avg_transtab_entry_size) == 0 ? "using " : "ignoring ",
+         VG_(details).avg_translation_sizeB);
+      VG_(message)(Vg_DebugMsg,
          "TT/TC: cache: %d sectors of %d bytes each = %d total\n", 
           n_sectors, 8 * tc_sector_szQ,
           n_sectors * 8 * tc_sector_szQ );
diff --git a/coregrind/pub_core_options.h b/coregrind/pub_core_options.h
index 60c79e1..53ccfc0 100644
--- a/coregrind/pub_core_options.h
+++ b/coregrind/pub_core_options.h
@@ -310,6 +310,10 @@
 /* Max number of sectors that will be used by the translation code cache. */
 extern UInt VG_(clo_num_transtab_sectors);
 
+/* Average size of a transtab code entry. 0 means to use the tool
+   provided default. */
+extern UInt VG_(clo_avg_transtab_entry_size);
+
 /* Only client requested fixed mapping can be done below 
    VG_(clo_aspacem_minAddr). */
 extern Addr VG_(clo_aspacem_minAddr);
diff --git a/coregrind/pub_core_transtab.h b/coregrind/pub_core_transtab.h
index 67fd9d8..2fd8612 100644
--- a/coregrind/pub_core_transtab.h
+++ b/coregrind/pub_core_transtab.h
@@ -56,7 +56,8 @@
 #define TRANSTAB_BOGUS_GUEST_ADDR ((Addr)1)
 
 
-/* Initialises the TC, using VG_(clo_num_transtab_sectors).
+/* Initialises the TC, using VG_(clo_num_transtab_sectors)
+   and VG_(clo_avg_transtab_entry_size).
    VG_(clo_num_transtab_sectors) must be >= MIN_N_SECTORS
    and <= MAX_N_SECTORS. */
 extern void VG_(init_tt_tc)       ( void );
diff --git a/docs/xml/manual-core.xml b/docs/xml/manual-core.xml
index 21a994e..5437bd5 100644
--- a/docs/xml/manual-core.xml
+++ b/docs/xml/manual-core.xml
@@ -2208,7 +2208,7 @@
     </term>
     <listitem>
       <para>Valgrind translates and instruments your program's machine
-      code in small fragments. The translations are stored in a
+      code in small fragments (basic blocks). The translations are stored in a
       translation cache that is divided into a number of sections
       (sectors). If the cache is full, the sector containing the
       oldest translations is emptied and reused. If these old
@@ -2219,6 +2219,7 @@
       performance by reducing the number of re-translations needed.
       Sectors are allocated on demand.  Once allocated, a sector can
       never be freed, and occupies considerable space, depending on the tool
+      and the value of <option>--avg-transtab-entry-size</option>
       (about 40 MB per sector for Memcheck).  Use the
       option <option>--stats=yes</option> to obtain precise
       information about the memory used by a sector and the allocation
@@ -2226,6 +2227,28 @@
    </listitem>
   </varlistentry>
 
+  <varlistentry id="opt.avg-transtab-entry-size" xreflabel="--avg-transtab-entry-size">
+    <term>
+      <option><![CDATA[--avg-transtab-entry-size=<number> [default: 0,
+      meaning use tool provided default] ]]></option>
+    </term>
+    <listitem>
+      <para>Average size of translated basic block. This average size
+      is used to dimension the size of a sector.
+      Each tool provides a default value to be used.
+      If this default value is too small, the translation sectors
+      will become full too quickly. If this default value is too big,
+      a significant part of the translation sector memory will be unused.
+      Note that the average size of a basic block translation depends
+      on the tool, and might depend on tool options. For example,
+      the memcheck option <option>--track-origins=yes</option>
+      increases the size of the basic block translations.
+      Use <option>--avg-transtab-entry-size</option> to tune the size of the
+      sectors, either to gain memory or to avoid too many retranslations.
+      </para>
+   </listitem>
+  </varlistentry>
+
   <varlistentry id="opt.aspace-minaddr" xreflabel="----aspace-minaddr">
     <term>
       <option><![CDATA[--aspace-minaddr=<address> [default: depends
diff --git a/none/tests/cmdline1.stdout.exp b/none/tests/cmdline1.stdout.exp
index 385f773..564a2e0 100644
--- a/none/tests/cmdline1.stdout.exp
+++ b/none/tests/cmdline1.stdout.exp
@@ -107,6 +107,8 @@
            program counters in max <number> frames) [0]
     --num-transtab-sectors=<number> size of translated code cache [16]
            more sectors may increase performance, but use more memory.
+    --avg-transtab-entry-size=<number> avg size in bytes of a translated
+           basic block [0, meaning use tool provided default]
     --aspace-minaddr=0xPP     avoid mapping memory below 0xPP [guessed]
     --valgrind-stacksize=<number> size of valgrind (host) thread's stack
                                (in bytes) [1048576]
diff --git a/none/tests/cmdline2.stdout.exp b/none/tests/cmdline2.stdout.exp
index 9dbac31..4efc423 100644
--- a/none/tests/cmdline2.stdout.exp
+++ b/none/tests/cmdline2.stdout.exp
@@ -107,6 +107,8 @@
            program counters in max <number> frames) [0]
     --num-transtab-sectors=<number> size of translated code cache [16]
            more sectors may increase performance, but use more memory.
+    --avg-transtab-entry-size=<number> avg size in bytes of a translated
+           basic block [0, meaning use tool provided default]
     --aspace-minaddr=0xPP     avoid mapping memory below 0xPP [guessed]
     --valgrind-stacksize=<number> size of valgrind (host) thread's stack
                                (in bytes) [1048576]