1. 程式人生 > >使用tcmalloc編譯啟動時宕機

使用tcmalloc編譯啟動時宕機

連結時增加了-ltcmalloc,編好之後伺服器第一次啟動就宕機了,code檔案堆疊如下:

Program terminated with signal SIGABRT, Aborted.
#0  0x0000000000bdfda8 in raise ([email protected]=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x0000000000bdfda8 in raise ([email protected]
=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x0000000000b63538 in abort () at abort.c:89 #2 0x0000000000b58487 in uw_init_context_1 ([email protected]=0x7ffeeca2b170, [email protected]=0x7ffeeca2b420, outer_ra=0xaa23da <GetStackTrace_libgcc(void**, int, int)+42>) at ../../../gcc-5.1.0/libgcc/unwind-dw2.c:1563
#3 0x0000000000b59048 in _Unwind_Backtrace (trace=0xaa25c0 <libgcc_backtrace_helper(_Unwind_Context*, void*)>, trace_argument=0x7ffeeca2b420) at ../../../gcc-5.1.0/libgcc/unwind.inc:283 #4 0x0000000000aa23da in GetStackTrace_libgcc (result=<optimized out>, max_depth=<optimized out>, skip_count=<optimized out
>) at src/stacktrace_libgcc-inl.h:100 #5 0x0000000000aa2ab4 in GetStackTrace ([email protected]=0x2f3d250, [email protected]=30, [email protected]=3) at src/stacktrace.cc:295 #6 0x0000000000a9f256 in tcmalloc::RecordGrowth (growth=1048576) at src/page_heap.cc:618 #7 tcmalloc::PageHeap::GrowHeap (this=0x1757a80 <tcmalloc::Static::pageheap_>, n=<optimized out>) at src/page_heap.cc:644 #8 0x0000000000a9f553 in tcmalloc::PageHeap::New (this=0x1757a80 <tcmalloc::Static::pageheap_>, [email protected]=1) at src/page_heap.cc:154 #9 0x0000000000a99075 in tcmalloc::CentralFreeList::Populate (this=this@entry=0x18dbf00 <tcmalloc::Static::central_cache_+4864>) at src/central_freelist.cc:329 #10 0x0000000000a99278 in tcmalloc::CentralFreeList::FetchFromOneSpansSafe (this=0x18dbf00 <tcmalloc::Static::central_cache_+4864>, N=1, start=0x7ffeeca2b5c0, end=0x7ffeeca2b5c8) at src/central_freelist.cc:284 #11 0x0000000000a99304 in tcmalloc::CentralFreeList::RemoveRange (this=0x18dbf00 <tcmalloc::Static::central_cache_+4864>, [email protected]=0x7ffeeca2b5c0, [email protected]=0x7ffeeca2b5c8, N=1) at src/central_freelist.cc:264 #12 0x0000000000aa1110 in tcmalloc::ThreadCache::FetchFromCentralCache (this=this@entry=0x2f7d240, [email protected]=4, [email protected]=48, oom_handler[email protected]=0xa953d0 <(anonymous namespace)::nop_oom_handler(size_t)>) at src/thread_cache.cc:126 #13 0x0000000000c2870b in tcmalloc::ThreadCache::Allocate (oom_handler=0xa953d0 <(anonymous namespace)::nop_oom_handler(size_t)>, cl=4, size=48, this=<optimized out>) at src/thread_cache.h:380 #14 (anonymous namespace)::do_malloc (size=48) at src/tcmalloc.cc:1367 #15 tcmalloc::do_allocate_full<tcmalloc::malloc_oom> (size=48) at src/tcmalloc.cc:1758 #16 tcmalloc::allocate_full_malloc_oom ([email protected]=48) at src/tcmalloc.cc:1774 #17 0x0000000000c28876 in tcmalloc::dispatch_allocate_full<tcmalloc::malloc_oom> (size=48) at src/tcmalloc.cc:1787 #18 malloc_fast_path<tcmalloc::malloc_oom> ([email protected]=48) at src/tcmalloc.cc:1852 #19 tc_malloc ([email protected]=48) at src/tcmalloc.cc:1880 #20 0x0000000000c1cccb in _dl_get_origin () at ../sysdeps/unix/sysv/linux/dl-origin.c:50 #21 0x0000000000bd58ff in _dl_non_dynamic_init () at dl-support.c:314 #22 0x0000000000bd6a08 in __libc_init_first ([email protected]=4, [email protected]=0x7ffeeca2c7b8, envp=0x7ffeeca2c7e0) at ../csu/init-first.c:79 #23 0x0000000000b5b23d in __libc_start_main (main=0x41d6dc <main(int, char**)>, argc=4, argv=0x7ffeeca2c7b8, init=0xb5b6c0 <__libc_csu_init>, fini=0xb5b750 <__libc_csu_fini>, rtld_fini=0x0, stack_end=0x7ffeeca2c7a8) at libc-start.c:224 #24 0x0000000000400c1d in _start () (gdb)

因為tcmalloc的靜態庫檔案是自己編譯的,有原始碼,於是乎就開始糾結地看起了原始碼。看了很久原始碼並且嘗試除錯,都沒有什麼進展。看堆疊提到了_Unwind_Backtrace,並且想起來在編譯的時候,看到過關於unwind的相關警告:

$ ./configure 
... ...
configure: WARNING: No frame pointers and no libunwind. Using experimental backtrace capturing via libgcc. Expect crashy cpu profiler.

檢視原始碼目錄下的INSTALL檔案(我的 gperftools 版本是2.7),檢視到如下說明:

重新編譯:

$ ./configure --enable-frame-pointers
... ...
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/gperftools/tcmalloc.h
config.status: creating src/windows/gperftools/tcmalloc.h
config.status: creating src/config.h
config.status: src/config.h is unchanged
config.status: executing depfiles commands
config.status: executing libtool commands
$ make

果然沒有警告了,然後拷貝.libs目錄下的 libtcmalloc.a 到靜態編譯目錄,重新編譯伺服器,再次開啟,果然沒有報錯了。