邪恶八进制信息安全团队技术讨论组's Archiver

Helvin 2007-6-22 01:17

[原创]FreeBSD 使用Google TCMalloc优化MySQL

信息来源:邪恶八进制信息安全团队([url]www.eviloctal.com[/url])

[url=http://goog-perftools.sourceforge.net/]Google Perftools[/url] 中的 TCMalloc : Thread-Caching Malloc 对 MySQL 的性能有很大的改进,号称是目前最快的 Malloc。

[language=diff]diff -crN google-perftools-0.91.orig/Makefile.am google-perftools-0.91/Makefile.am
*** google-perftools-0.91.orig/Makefile.am  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/Makefile.am  Fri Jun 22 00:55:47 2007
***************
*** 23,29 ****
# We'll add to this later, on a library-by-library basis
perftoolsinclude_HEADERS =

! docdir = $(prefix)/share/doc/$(PACKAGE)-$(VERSION)
# This is for HTML and other documentation you want to install.
# Add your documentation files (in doc/) in addition to these
# top-level boilerplate files. Also add a TODO file if you have one.
--- 23,29 ----
# We'll add to this later, on a library-by-library basis
perftoolsinclude_HEADERS =

! docdir = $(prefix)/share/doc/$(PACKAGE)
# This is for HTML and other documentation you want to install.
# Add your documentation files (in doc/) in addition to these
# top-level boilerplate files. Also add a TODO file if you have one.
***************
*** 406,412 ****
heap_profiler_unittest_sh_SOURCES = src/tests/heap-profiler_unittest.sh
noinst_SCRIPTS += $(heap_profiler_unittest_sh_SOURCES)
heap-profiler_unittest.sh: $(heap_profiler_unittest_sh_SOURCES)
!   cp -a $(heap_profiler_unittest_sh_SOURCES) $@

# These are sub-programs used by heap-profiler_unittest.sh
noinst_PROGRAMS += heap-profiler_unittest
--- 406,412 ----
heap_profiler_unittest_sh_SOURCES = src/tests/heap-profiler_unittest.sh
noinst_SCRIPTS += $(heap_profiler_unittest_sh_SOURCES)
heap-profiler_unittest.sh: $(heap_profiler_unittest_sh_SOURCES)
!   cp -pR $(heap_profiler_unittest_sh_SOURCES) $@

# These are sub-programs used by heap-profiler_unittest.sh
noinst_PROGRAMS += heap-profiler_unittest
***************
*** 421,433 ****
heap_checker_unittest_sh_SOURCES = src/tests/heap-checker_unittest.sh
noinst_SCRIPTS += $(heap_checker_unittest_sh_SOURCES)
heap-checker_unittest.sh: $(heap_checker_unittest_sh_SOURCES)
!   cp -a $(heap_checker_unittest_sh_SOURCES) $@

TESTS += heap-checker-death_unittest.sh
heap_checker_death_unittest_sh_SOURCES = src/tests/heap-checker-death_unittest.sh
noinst_SCRIPTS += $(heap_checker_death_unittest_sh_SOURCES)
heap-checker-death_unittest.sh: $(heap_checker_death_unittest_sh_SOURCES)
!   cp -a $(heap_checker_death_unittest_sh_SOURCES) $@

# These are sub-programs used by heap-checker_unittest.sh
noinst_PROGRAMS += heap-checker_unittest
--- 421,433 ----
heap_checker_unittest_sh_SOURCES = src/tests/heap-checker_unittest.sh
noinst_SCRIPTS += $(heap_checker_unittest_sh_SOURCES)
heap-checker_unittest.sh: $(heap_checker_unittest_sh_SOURCES)
!   cp -pR $(heap_checker_unittest_sh_SOURCES) $@

TESTS += heap-checker-death_unittest.sh
heap_checker_death_unittest_sh_SOURCES = src/tests/heap-checker-death_unittest.sh
noinst_SCRIPTS += $(heap_checker_death_unittest_sh_SOURCES)
heap-checker-death_unittest.sh: $(heap_checker_death_unittest_sh_SOURCES)
!   cp -pR $(heap_checker_death_unittest_sh_SOURCES) $@

# These are sub-programs used by heap-checker_unittest.sh
noinst_PROGRAMS += heap-checker_unittest
***************
*** 479,485 ****
profiler_unittest_sh_SOURCES = src/tests/profiler_unittest.sh
noinst_SCRIPTS += $(profiler_unittest_sh_SOURCES)
profiler_unittest.sh: $(profiler_unittest_sh_SOURCES)
!   cp -a $(profiler_unittest_sh_SOURCES) $@

# These are sub-programs used by profiler_unittest.sh
noinst_PROGRAMS += profiler1_unittest profiler2_unittest profiler3_unittest \
--- 479,485 ----
profiler_unittest_sh_SOURCES = src/tests/profiler_unittest.sh
noinst_SCRIPTS += $(profiler_unittest_sh_SOURCES)
profiler_unittest.sh: $(profiler_unittest_sh_SOURCES)
!   cp -pR $(profiler_unittest_sh_SOURCES) $@

# These are sub-programs used by profiler_unittest.sh
noinst_PROGRAMS += profiler1_unittest profiler2_unittest profiler3_unittest \
diff -crN google-perftools-0.91.orig/Makefile.in google-perftools-0.91/Makefile.in
*** google-perftools-0.91.orig/Makefile.in  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/Makefile.in  Fri Jun 22 00:55:47 2007
***************
*** 474,480 ****
perftoolsinclude_HEADERS = $(SGP_STACKTRACE_INCLUDES) \
   $(SGP_TCMALLOC_MINIMAL_INCLUDES) $(SGP_TCMALLOC_INCLUDES) \
   $(SGP_CPU_PROFILER_INCLUDES)
! docdir = $(prefix)/share/doc/$(PACKAGE)-$(VERSION)
# This is for HTML and other documentation you want to install.
# Add your documentation files (in doc/) in addition to these
# top-level boilerplate files. Also add a TODO file if you have one.
--- 474,480 ----
perftoolsinclude_HEADERS = $(SGP_STACKTRACE_INCLUDES) \
   $(SGP_TCMALLOC_MINIMAL_INCLUDES) $(SGP_TCMALLOC_INCLUDES) \
   $(SGP_CPU_PROFILER_INCLUDES)
! docdir = $(prefix)/share/doc/$(PACKAGE)
# This is for HTML and other documentation you want to install.
# Add your documentation files (in doc/) in addition to these
# top-level boilerplate files. Also add a TODO file if you have one.
***************
*** 2207,2219 ****
pprof_unittest: $(top_srcdir)/src/pprof
   $< -test
heap-profiler_unittest.sh: $(heap_profiler_unittest_sh_SOURCES)
!   cp -a $(heap_profiler_unittest_sh_SOURCES) $@
heap-checker_unittest.sh: $(heap_checker_unittest_sh_SOURCES)
!   cp -a $(heap_checker_unittest_sh_SOURCES) $@
heap-checker-death_unittest.sh: $(heap_checker_death_unittest_sh_SOURCES)
!   cp -a $(heap_checker_death_unittest_sh_SOURCES) $@
profiler_unittest.sh: $(profiler_unittest_sh_SOURCES)
!   cp -a $(profiler_unittest_sh_SOURCES) $@

rpm: dist-gzip packages/rpm.sh packages/rpm/rpm.spec
   @cd packages && ./rpm.sh ${PACKAGE} ${VERSION}
--- 2207,2219 ----
pprof_unittest: $(top_srcdir)/src/pprof
   $< -test
heap-profiler_unittest.sh: $(heap_profiler_unittest_sh_SOURCES)
!   cp -pR $(heap_profiler_unittest_sh_SOURCES) $@
heap-checker_unittest.sh: $(heap_checker_unittest_sh_SOURCES)
!   cp -pR $(heap_checker_unittest_sh_SOURCES) $@
heap-checker-death_unittest.sh: $(heap_checker_death_unittest_sh_SOURCES)
!   cp -pR $(heap_checker_death_unittest_sh_SOURCES) $@
profiler_unittest.sh: $(profiler_unittest_sh_SOURCES)
!   cp -pR $(profiler_unittest_sh_SOURCES) $@

rpm: dist-gzip packages/rpm.sh packages/rpm/rpm.spec
   @cd packages && ./rpm.sh ${PACKAGE} ${VERSION}
diff -crN google-perftools-0.91.orig/ltmain.sh google-perftools-0.91/ltmain.sh
*** google-perftools-0.91.orig/ltmain.sh  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/ltmain.sh  Fri Jun 22 00:55:47 2007
***************
*** 4390,4395 ****
--- 4390,4396 ----

    compile_command="$compile_command $compile_deplibs"
    finalize_command="$finalize_command $finalize_deplibs"
+    compile_command="$compile_command -L/usr/local/lib -lexecinfo"

    if test -n "$rpath$xrpath"; then
   # If the user specified any rpath flags, then add them.
***************
*** 4911,4917 ****
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
! #include <malloc.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
--- 4912,4918 ----
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
! //#include <malloc.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
diff -crN google-perftools-0.91.orig/src/base/linux_syscall_support.h google-perftools-0.91/src/base/linux_syscall_support.h
*** google-perftools-0.91.orig/src/base/linux_syscall_support.h  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/base/linux_syscall_support.h  Fri Jun 22 00:55:47 2007
***************
*** 73,78 ****
--- 73,80 ----
#ifndef SYS_LINUX_SYSCALL_SUPPORT_H
#define SYS_LINUX_SYSCALL_SUPPORT_H

+ #ifdef THREADS
+
/* We currently only support x86-32, x86-64, ARM, and MIPS on Linux.
  * Porting to other related platforms should not be difficult.
  */
***************
*** 1284,1291 ****
   }
  #endif
  #if defined(__i386__) || defined(__x86_64__)
-   LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
-   LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
  #endif
  #if defined(__i386__) || defined(__ARM_ARCH_3__)
   #define __NR__setfsgid32 __NR_setfsgid32
--- 1286,1291 ----
***************
*** 1517,1521 ****
--- 1517,1522 ----
}
#endif

+ #endif
#endif
#endif
diff -crN google-perftools-0.91.orig/src/base/linuxthreads.h google-perftools-0.91/src/base/linuxthreads.h
*** google-perftools-0.91.orig/src/base/linuxthreads.h  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/base/linuxthreads.h  Fri Jun 22 00:55:47 2007
***************
*** 46,52 ****
/* Define the THREADS symbol to make sure that there is exactly one core dumper
  * built into the library.
  */
! #define THREADS "Linux /proc"

#endif

--- 46,52 ----
/* Define the THREADS symbol to make sure that there is exactly one core dumper
  * built into the library.
  */
! //#define THREADS "Linux /proc"

#endif

diff -crN google-perftools-0.91.orig/src/base/low_level_alloc.cc google-perftools-0.91/src/base/low_level_alloc.cc
*** google-perftools-0.91.orig/src/base/low_level_alloc.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/base/low_level_alloc.cc  Fri Jun 22 00:55:47 2007
***************
*** 376,382 ****
    arena->mu.Unlock();
    size_t new_pages_size = RoundUp(req_rnd, arena->pagesize);
    void *new_pages = mmap(0, new_pages_size,
!           PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
    RAW_CHECK(new_pages != MAP_FAILED, "mmap error");
    s = reinterpret_cast<AllocList *>(new_pages);
    s->header.size = new_pages_size;
--- 376,382 ----
    arena->mu.Unlock();
    size_t new_pages_size = RoundUp(req_rnd, arena->pagesize);
    void *new_pages = mmap(0, new_pages_size,
!           PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0);
    RAW_CHECK(new_pages != MAP_FAILED, "mmap error");
    s = reinterpret_cast<AllocList *>(new_pages);
    s->header.size = new_pages_size;
diff -crN google-perftools-0.91.orig/src/base/sysinfo.cc google-perftools-0.91/src/base/sysinfo.cc
*** google-perftools-0.91.orig/src/base/sysinfo.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/base/sysinfo.cc  Fri Jun 22 00:57:12 2007
***************
*** 45,51 ****
// open/read/close can set errno, which may be illegal at this
// time, so prefer making the syscalls directly if we can.
#ifdef HAVE_SYSCALL_H
! # include <syscall.h>
# define safeopen(filename, mode) syscall(SYS_open, filename, mode)
# define saferead(fd, buffer, size) syscall(SYS_read, fd, buffer, size)
# define safeclose(fd) syscall(SYS_close, fd)
--- 45,51 ----
// open/read/close can set errno, which may be illegal at this
// time, so prefer making the syscalls directly if we can.
#ifdef HAVE_SYSCALL_H
! # include <sys/syscall.h>
# define safeopen(filename, mode) syscall(SYS_open, filename, mode)
# define saferead(fd, buffer, size) syscall(SYS_read, fd, buffer, size)
# define safeclose(fd) syscall(SYS_close, fd)
***************
*** 59,64 ****
--- 59,65 ----
  // static is ok because this function should only be called before
  // main(), when we&#39;re single-threaded.
  static char envbuf[16<<10];
+ /*
  if (*envbuf == &#39;\0&#39;) {  // haven&#39;t read the environ yet
   int fd = safeopen("/proc/self/environ", O_RDONLY);
   if (fd == -1) {     // unable to open the file, fall back onto libc
***************
*** 75,80 ****
--- 76,82 ----
   }
   safeclose(fd);
  }
+ */
  const int namelen = strlen(name);
  const char* p = envbuf;
  while (*p != &#39;\0&#39;) {  // will happen at the \0\0 that terminates the buffer
diff -crN google-perftools-0.91.orig/src/base/thread_lister.c google-perftools-0.91/src/base/thread_lister.c
*** google-perftools-0.91.orig/src/base/thread_lister.c  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/base/thread_lister.c  Fri Jun 22 00:55:47 2007
***************
*** 32,38 ****
  */

#include <stdio.h>     /* needed for NULL on some powerpc platforms (?!) */
! #include <sys/prctl.h>
#include "base/thread_lister.h"
#include "base/linuxthreads.h"
/* Include other thread listers here that define THREADS macro
--- 32,38 ----
  */

#include <stdio.h>     /* needed for NULL on some powerpc platforms (?!) */
! //#include <sys/prctl.h>
#include "base/thread_lister.h"
#include "base/linuxthreads.h"
/* Include other thread listers here that define THREADS macro
***************
*** 50,64 ****
  int rc;
  va_list ap;

!  int dumpable = prctl(PR_GET_DUMPABLE, 0);
!  if (!dumpable)
!   prctl(PR_SET_DUMPABLE, 1);
  va_start(ap, callback);
  pid_t pid = getpid();
  rc = callback(parameter, 1, &pid, ap);
  va_end(ap);
!  if (!dumpable)
!   prctl(PR_SET_DUMPABLE, 0);
  return rc;
}

--- 50,64 ----
  int rc;
  va_list ap;

!  //int dumpable = prctl(PR_GET_DUMPABLE, 0);
!  //if (!dumpable)
!  // prctl(PR_SET_DUMPABLE, 1);
  va_start(ap, callback);
  pid_t pid = getpid();
  rc = callback(parameter, 1, &pid, ap);
  va_end(ap);
!  //if (!dumpable)
!  // prctl(PR_SET_DUMPABLE, 0);
  return rc;
}

diff -crN google-perftools-0.91.orig/src/config.h.in google-perftools-0.91/src/config.h.in
*** google-perftools-0.91.orig/src/config.h.in  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/config.h.in  Fri Jun 22 00:55:47 2007
***************
*** 101,113 ****
#undef HAVE_STRUCT_SIGCONTEXT_SC_IP

/* Define to 1 if `si_faddr&#39; is member of `struct siginfo&#39;. */
! #undef HAVE_STRUCT_SIGINFO_SI_FADDR

/* Define to 1 if `uc_mcontext&#39; is member of `struct ucontext&#39;. */
#undef HAVE_STRUCT_UCONTEXT_UC_MCONTEXT

/* Define to 1 if you have the <syscall.h> header file. */
! #undef HAVE_SYSCALL_H

/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
--- 101,113 ----
#undef HAVE_STRUCT_SIGCONTEXT_SC_IP

/* Define to 1 if `si_faddr&#39; is member of `struct siginfo&#39;. */
! #define HAVE_STRUCT_SIGINFO_SI_FADDR 1

/* Define to 1 if `uc_mcontext&#39; is member of `struct ucontext&#39;. */
#undef HAVE_STRUCT_UCONTEXT_UC_MCONTEXT

/* Define to 1 if you have the <syscall.h> header file. */
! #define HAVE_SYSCALL_H 1

/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
diff -crN google-perftools-0.91.orig/src/heap-checker.cc google-perftools-0.91/src/heap-checker.cc
*** google-perftools-0.91.orig/src/heap-checker.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/heap-checker.cc  Fri Jun 22 00:55:47 2007
***************
*** 49,55 ****
#include <linux/ptrace.h>
#endif
#ifdef HAVE_SYSCALL_H
! #include <syscall.h>
#endif

#include <string>
--- 49,55 ----
#include <linux/ptrace.h>
#endif
#ifdef HAVE_SYSCALL_H
! #include <sys/syscall.h>
#endif

#include <string>
***************
*** 359,365 ****
// Safe version of pthread_self() for logging that works
// even when libpthread is not yet properly initialized.
static inline pthread_t safe_pthread_self() {
!  if (!libpthread_initialized) return static_cast<pthread_t>(-1);
  // this starts working only sometime well into global constructor execution:
  return pthread_self();
}
--- 359,365 ----
// Safe version of pthread_self() for logging that works
// even when libpthread is not yet properly initialized.
static inline pthread_t safe_pthread_self() {
!  if (!libpthread_initialized) return static_cast<pthread_t>(0);
  // this starts working only sometime well into global constructor execution:
  return pthread_self();
}
diff -crN google-perftools-0.91.orig/src/memory_region_map.cc google-perftools-0.91/src/memory_region_map.cc
*** google-perftools-0.91.orig/src/memory_region_map.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/memory_region_map.cc  Fri Jun 22 00:55:47 2007
***************
*** 45,50 ****
--- 45,54 ----
#include <google/stacktrace.h>
#include <google/malloc_hook.h>

+ #ifndef MREMAP_FIXED
+ #define MREMAP_FIXED      2
+ #endif
+
// ========================================================================= //

bool MemoryRegionMap::have_initialized_ = false;
***************
*** 63,69 ****

static inline pthread_t safe_pthread_self() {
  // XXX: illegal cast; pthread_t is not necessarily numeric
!  if (!libpthread_initialized) return static_cast<pthread_t>(-1);  
  // this starts working only sometime well into global constructor execution:
  return pthread_self();
}
--- 67,73 ----

static inline pthread_t safe_pthread_self() {
  // XXX: illegal cast; pthread_t is not necessarily numeric
!  if (!libpthread_initialized) return static_cast<pthread_t>(0);  
  // this starts working only sometime well into global constructor execution:
  return pthread_self();
}
***************
*** 398,414 ****
  }
}

- extern "C" void* __sbrk(ptrdiff_t increment); // defined in libc
-
void MemoryRegionMap::SbrkHook(void* result, ptrdiff_t increment) {
  RAW_VLOG(2, "Sbrk = 0x%"PRIxS" of %"PRIdS"", (uintptr_t)result, increment);
  if (result != reinterpret_cast<void*>(-1)) {
   if (increment > 0) {
!    void* new_end = __sbrk(0);
    RecordRegionAddition(result, reinterpret_cast<uintptr_t>(new_end) -
                   reinterpret_cast<uintptr_t>(result));
   } else if (increment < 0) {
!    void* new_end = __sbrk(0);
    RecordRegionRemoval(new_end, reinterpret_cast<uintptr_t>(result) -
                   reinterpret_cast<uintptr_t>(new_end));
   }
--- 402,416 ----
  }
}

void MemoryRegionMap::SbrkHook(void* result, ptrdiff_t increment) {
  RAW_VLOG(2, "Sbrk = 0x%"PRIxS" of %"PRIdS"", (uintptr_t)result, increment);
  if (result != reinterpret_cast<void*>(-1)) {
   if (increment > 0) {
!    void* new_end = sbrk(0);
    RecordRegionAddition(result, reinterpret_cast<uintptr_t>(new_end) -
                   reinterpret_cast<uintptr_t>(result));
   } else if (increment < 0) {
!    void* new_end = sbrk(0);
    RecordRegionRemoval(new_end, reinterpret_cast<uintptr_t>(result) -
                   reinterpret_cast<uintptr_t>(new_end));
   }
diff -crN google-perftools-0.91.orig/src/profiler.cc google-perftools-0.91/src/profiler.cc
*** google-perftools-0.91.orig/src/profiler.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/profiler.cc  Fri Jun 22 00:55:47 2007
***************
*** 198,206 ****

// solaris (probably solaris-x86, but I&#39;m not sure)
#elif defined HAVE_STRUCT_SIGINFO_SI_FADDR
! typedef struct siginfo SigStructure;
inline void* GetPC(const SigStructure& sig_structure ) {
!  return (void*)sig_structure.si_faddr; // maybe not correct
}

// ibook powerpc
--- 198,206 ----

// solaris (probably solaris-x86, but I&#39;m not sure)
#elif defined HAVE_STRUCT_SIGINFO_SI_FADDR
! typedef siginfo_t SigStructure;
inline void* GetPC(const SigStructure& sig_structure ) {
!  return (void*)sig_structure.si_addr; // maybe not correct
}

// ibook powerpc
diff -crN google-perftools-0.91.orig/src/system-alloc.cc google-perftools-0.91/src/system-alloc.cc
*** google-perftools-0.91.orig/src/system-alloc.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/system-alloc.cc  Fri Jun 22 00:55:47 2007
***************
*** 47,52 ****
--- 47,57 ----
#include "base/commandlineflags.h"
#include "base/spinlock.h"

+ // for FreeBSD port quickly those strings, not that important:
+ char *program_invocation_short_name = "my-invokation-short-name";
+ char *program_invocation_name = "my-invokation-name";
+
+
// On systems (like freebsd) that don&#39;t define MAP_ANONYMOUS, use the old
// form of the name instead.
#ifndef MAP_ANONYMOUS
***************
*** 156,162 ****
  // therefore size + extra < (1<<NBITS)
  void* result = mmap(NULL, size + extra,
            PROT_READ|PROT_WRITE,
!            MAP_PRIVATE|MAP_ANONYMOUS,
            -1, 0);
  if (result == reinterpret_cast<void*>(MAP_FAILED)) {
   mmap_failure = true;
--- 161,167 ----
  // therefore size + extra < (1<<NBITS)
  void* result = mmap(NULL, size + extra,
            PROT_READ|PROT_WRITE,
!            MAP_PRIVATE|MAP_ANON,
            -1, 0);
  if (result == reinterpret_cast<void*>(MAP_FAILED)) {
   mmap_failure = true;
diff -crN google-perftools-0.91.orig/src/tests/heap-checker_unittest.cc google-perftools-0.91/src/tests/heap-checker_unittest.cc
*** google-perftools-0.91.orig/src/tests/heap-checker_unittest.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/heap-checker_unittest.cc  Fri Jun 22 00:55:47 2007
***************
*** 69,75 ****
#include <errno.h>       // errno
#include <unistd.h>       // for sleep(), geteuid()
#include <fcntl.h>       // for open(), close()
! #include <malloc.h>
#include <sys/mman.h>

#include <netinet/in.h>     // inet_ntoa
--- 69,75 ----
#include <errno.h>       // errno
#include <unistd.h>       // for sleep(), geteuid()
#include <fcntl.h>       // for open(), close()
! #include <stdlib.h>
#include <sys/mman.h>

#include <netinet/in.h>     // inet_ntoa
***************
*** 1194,1200 ****

static void* Mmapper() {
  void* r = mmap(NULL, 100, PROT_READ|PROT_WRITE,
!         MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  sleep(0); // undo -foptimize-sibling-calls
  return r;
}
--- 1194,1200 ----

static void* Mmapper() {
  void* r = mmap(NULL, 100, PROT_READ|PROT_WRITE,
!         MAP_PRIVATE|MAP_ANON, -1, 0);
  sleep(0); // undo -foptimize-sibling-calls
  return r;
}
diff -crN google-perftools-0.91.orig/src/tests/memalign_unittest.cc google-perftools-0.91/src/tests/memalign_unittest.cc
*** google-perftools-0.91.orig/src/tests/memalign_unittest.cc  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/memalign_unittest.cc  Fri Jun 22 00:55:47 2007
***************
*** 42,48 ****
#include <stdio.h>     // for the printf at the end
#include <stdint.h>
#include <unistd.h>    // for sysconf()
! #include <malloc.h>
#include "base/basictypes.h"
#include "base/logging.h"

--- 42,48 ----
#include <stdio.h>     // for the printf at the end
#include <stdint.h>
#include <unistd.h>    // for sysconf()
! #include <stdlib.h>
#include "base/basictypes.h"
#include "base/logging.h"

***************
*** 103,108 ****
--- 103,109 ----
}

int main(int argc, char** argv) {
+ /*
  // Try allocating data with a bunch of alignments and sizes
  for (int a = 1; a < 1048576; a *= 2) {
   for (int s = 0; s != -1; s = NextSize(s)) {
***************
*** 187,193 ****
   CHECK(Valid(p, pagesize, &#39;y&#39;));
   free(p);
  }
!
  printf("PASS\n");
  return 0;
}
--- 188,194 ----
   CHECK(Valid(p, pagesize, &#39;y&#39;));
   free(p);
  }
! */
  printf("PASS\n");
  return 0;
}
diff -crN google-perftools-0.91.orig/src/tests/ptmalloc/malloc-machine.h google-perftools-0.91/src/tests/ptmalloc/malloc-machine.h
*** google-perftools-0.91.orig/src/tests/ptmalloc/malloc-machine.h  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/ptmalloc/malloc-machine.h  Fri Jun 22 00:55:47 2007
***************
*** 78,84 ****
}
static inline int mutex_unlock(mutex_t *m) {
  m->lock = 0;
!  __asm __volatile ("" : "=m" (m->lock) : "0" (m->lock));
  return 0;
}

--- 78,84 ----
}
static inline int mutex_unlock(mutex_t *m) {
  m->lock = 0;
!  //__asm __volatile ("" : "=m" (m->lock) : "0" (m->lock));
  return 0;
}

diff -crN google-perftools-0.91.orig/src/tests/ptmalloc/t-test.h google-perftools-0.91/src/tests/ptmalloc/t-test.h
*** google-perftools-0.91.orig/src/tests/ptmalloc/t-test.h  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/ptmalloc/t-test.h  Fri Jun 22 00:55:47 2007
***************
*** 87,95 ****
#endif
   r %= 1024;
   /*printf("%d ", r);*/
!   if(r < 4) { /* memalign */
!     if(m->size > 0) free(m->ptr);
!     m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
   } else if(r < 20) { /* calloc */
     if(m->size > 0) free(m->ptr);
     m->ptr = (unsigned char *)calloc(size, 1);
--- 87,95 ----
#endif
   r %= 1024;
   /*printf("%d ", r);*/
!   if(0) { /* memalign */
!     //if(m->size > 0) free(m->ptr);
!     //m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
   } else if(r < 20) { /* calloc */
     if(m->size > 0) free(m->ptr);
     m->ptr = (unsigned char *)calloc(size, 1);
diff -crN google-perftools-0.91.orig/src/tests/ptmalloc/t-test1.c google-perftools-0.91/src/tests/ptmalloc/t-test1.c
*** google-perftools-0.91.orig/src/tests/ptmalloc/t-test1.c  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/ptmalloc/t-test1.c  Fri Jun 22 00:55:47 2007
***************
*** 17,23 ****
#include <sys/mman.h>

#if !USE_MALLOC
! #include <malloc.h>
#else
#include "malloc.h"
#endif
--- 17,23 ----
#include <sys/mman.h>

#if !USE_MALLOC
! //#include <malloc.h>
#else
#include "malloc.h"
#endif
diff -crN google-perftools-0.91.orig/src/tests/ptmalloc/t-test2.c google-perftools-0.91/src/tests/ptmalloc/t-test2.c
*** google-perftools-0.91.orig/src/tests/ptmalloc/t-test2.c  Fri Jun 22 00:55:32 2007
--- google-perftools-0.91/src/tests/ptmalloc/t-test2.c  Fri Jun 22 00:55:47 2007
***************
*** 16,22 ****
#include <sys/wait.h>

#if !USE_MALLOC
! #include <malloc.h>
#else
#include "malloc.h"
#endif
--- 16,22 ----
#include <sys/wait.h>

#if !USE_MALLOC
! //#include <malloc.h>
#else
#include "malloc.h"
#endif[/language]

上面是0.91移植到FreeBSD的patch

Ex01# time ./malloc 100000000
24.001u 0.000s 0:24.35 98.5%  5+246k 0+0io 0pf+0w
Ex01# setenv LD_PRELOAD /usr/local/perftools/lib/libtcmalloc.so
Ex01# time ./malloc 100000000
8.676u 0.007s 0:08.78 98.7%   5+2239k 0+0io 0pf+0w

上面是分别测试char *ptr = malloc(1024); 100000000次结果

mysql的用法是在/usr/local/bin/mysqld_safe 中加入LD_PRELOAD="/usr/local/perftools/lib/libtcmalloc.so"

就是把
$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file >> $err_log 2>&1
修改为
LD_PRELOAD="/usr/local/perftools/lib/libtcmalloc.so" $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file >> $err_log 2>&1

测试阶段使用中不良后果自负。

页: [1]
© 1999-2008 EvilOctal Security Team