2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

デフォルトの malloc を差し替えずに tc_malloc を使う Linux編

Last updated at Posted at 2018-06-09

mallocの速度比較をしたかったのですが、tc_mallocが意地でもmallocを上書きする為測定にならなかったので、こちらの記事を参考にmalloc差し替えを無効化しました。

手順

1 . ソースダウンロード
公式githubから最新のソース gperftools-2.7を落としてきました。
https://github.com/gperftools/gperftools/releases
2. overrideコードの修正。 Linux向けに対応したのは以下。他のOSも同様に見えます。修正箇所はその他を参照ください。
* 関連するlibc_overrideヘッダー(src/libc_override_gcc_and_weak.h, src/libc_override_glibc.h)の上書き処理をコメントアウト
* unittestが通らずエラーになるため、src/tests/tcmalloc_unittest.ccでcfreeを定義
3. ビルド
configureがないので、autogen.shを実行する必要があります。

./autogen.sh
./configure
make
sudo make install

インストールパスは/usr/local配下になります。
ただし、これをやるとtc_malloc自体のスペックも落ちるようです。

その他

修正のdiff -cr結果はこんな感じです。

diff -cr gperftools-gperftools-2.7_ori/src/libc_override_gcc_and_weak.h gperftools-gperftools-2.7/src/libc_override_gcc_and_weak.h
*** gperftools-gperftools-2.7_ori/src/libc_override_gcc_and_weak.h	2018-04-30 15:00:34.000000000 +0900
--- gperftools-gperftools-2.7/src/libc_override_gcc_and_weak.h	2018-06-09 16:05:54.671886411 +0900
***************
*** 206,212 ****
  #endif /* defined(ENABLE_SIZED_DELETE) */
  
  #endif /* defined(ENABLE_ALIGNED_NEW_DELETE) */
! 
  extern "C" {
    void* malloc(size_t size) __THROW               ALIAS(tc_malloc);
    void free(void* ptr) __THROW                    ALIAS(tc_free);
--- 206,212 ----
  #endif /* defined(ENABLE_SIZED_DELETE) */
  
  #endif /* defined(ENABLE_ALIGNED_NEW_DELETE) */
! #if 0
  extern "C" {
    void* malloc(size_t size) __THROW               ALIAS(tc_malloc);
    void free(void* ptr) __THROW                    ALIAS(tc_free);
***************
*** 234,240 ****
    size_t malloc_usable_size(void* p) __THROW      ALIAS(tc_malloc_size);
  #endif
  }   // extern "C"
! 
  #undef ALIAS
  
  // No need to do anything at tcmalloc-registration time: we do it all
--- 234,240 ----
    size_t malloc_usable_size(void* p) __THROW      ALIAS(tc_malloc_size);
  #endif
  }   // extern "C"
! #endif
  #undef ALIAS
  
  // No need to do anything at tcmalloc-registration time: we do it all
diff -cr gperftools-gperftools-2.7_ori/src/libc_override_glibc.h gperftools-gperftools-2.7/src/libc_override_glibc.h
*** gperftools-gperftools-2.7_ori/src/libc_override_glibc.h	2018-04-30 15:00:34.000000000 +0900
--- gperftools-gperftools-2.7/src/libc_override_glibc.h	2018-06-09 15:52:38.397338332 +0900
***************
*** 69,75 ****
  // same implementations.  Since it only matters for redhat, we
  // do it inside the gcc #ifdef, since redhat uses gcc.
  // TODO(csilvers): only do this if we detect we're an old enough glibc?
! 
  #define ALIAS(tc_fn)   __attribute__ ((alias (#tc_fn)))
  extern "C" {
    void* __libc_malloc(size_t size)                ALIAS(tc_malloc);
--- 69,75 ----
  // same implementations.  Since it only matters for redhat, we
  // do it inside the gcc #ifdef, since redhat uses gcc.
  // TODO(csilvers): only do this if we detect we're an old enough glibc?
! #if 0
  #define ALIAS(tc_fn)   __attribute__ ((alias (#tc_fn)))
  extern "C" {
    void* __libc_malloc(size_t size)                ALIAS(tc_malloc);
***************
*** 83,89 ****
    int __posix_memalign(void** r, size_t a, size_t s)  ALIAS(tc_posix_memalign);
  }   // extern "C"
  #undef ALIAS
! 
  #endif  // #if defined(__GNUC__) && !defined(__MACH__)
  
  // No need to write ReplaceSystemAlloc(); one of the #includes above
--- 83,89 ----
    int __posix_memalign(void** r, size_t a, size_t s)  ALIAS(tc_posix_memalign);
  }   // extern "C"
  #undef ALIAS
! #endif
  #endif  // #if defined(__GNUC__) && !defined(__MACH__)
  
  // No need to write ReplaceSystemAlloc(); one of the #includes above
diff -cr gperftools-gperftools-2.7_ori/src/tests/tcmalloc_unittest.cc gperftools-gperftools-2.7/src/tests/tcmalloc_unittest.cc
*** gperftools-gperftools-2.7_ori/src/tests/tcmalloc_unittest.cc	2018-04-30 15:00:34.000000000 +0900
--- gperftools-gperftools-2.7/src/tests/tcmalloc_unittest.cc	2018-06-09 16:11:29.761269583 +0900
***************
*** 97,102 ****
--- 97,103 ----
  #include "system-alloc.h"
  #include "tests/testutil.h"
  
+ # define cfree free         // don't bother to try to test these obsolete fns
  // Windows doesn't define pvalloc and a few other obsolete unix
  // functions; nor does it define posix_memalign (which is not obsolete).
  #if defined(_WIN32)

参考

デフォルトの malloc を差し替えずに tc_malloc を使う

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?