winlin

add heap checker and profiler demo

@@ -440,7 +440,8 @@ if [ $SRS_GPERF = YES ]; then @@ -440,7 +440,8 @@ if [ $SRS_GPERF = YES ]; then
440 rm -rf ${SRS_OBJS}/gperftools-2.1 && cd ${SRS_OBJS} && 440 rm -rf ${SRS_OBJS}/gperftools-2.1 && cd ${SRS_OBJS} &&
441 unzip -q ../3rdparty/gperftools-2.1.zip && cd gperftools-2.1 && 441 unzip -q ../3rdparty/gperftools-2.1.zip && cd gperftools-2.1 &&
442 ./configure --prefix=`pwd`/_release --enable-frame-pointers && make ${SRS_JOBS} && make install && 442 ./configure --prefix=`pwd`/_release --enable-frame-pointers && make ${SRS_JOBS} && make install &&
443 - cd .. && rm -f gperf && ln -sf gperftools-2.1/_release gperf 443 + cd .. && rm -f gperf && ln -sf gperftools-2.1/_release gperf &&
  444 + rm -f pprof && ln -sf gperf/bin/pprof pprof
444 ) 445 )
445 fi 446 fi
446 # check status 447 # check status
  1 +heap_checker: heap_checker.cc Makefile
  2 + g++ -o heap_checker heap_checker.cc -g -O0 -ansi \
  3 + -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free \
  4 + ../../../objs/gperf/lib/libtcmalloc_and_profiler.a -lpthread
  5 +clean:
  6 + rm -f heap_checker
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +/**
  24 +set the pprof path if not set:
  25 + export PPROF_PATH=`pwd`/../../../objs/pprof
  26 +to check mem leak:
  27 + make && env HEAPCHECK=normal ./heap_checker
  28 +*/
  29 +#include <stdio.h>
  30 +#include <unistd.h>
  31 +#include <signal.h>
  32 +#include <stdlib.h>
  33 +
  34 +void explicit_leak_imp() {
  35 + printf("func leak: do something...\n");
  36 + for (int i = 0; i < 1024; ++i) {
  37 + char* p = new char[1024];
  38 + }
  39 + printf("func leak: memory leaked\n");
  40 +}
  41 +void explicit_leak() {
  42 + explicit_leak_imp();
  43 +}
  44 +
  45 +char* pglobal = NULL;
  46 +void global_leak_imp() {
  47 + printf("global leak: do something...\n");
  48 + for (int i = 0; i < 1024; ++i) {
  49 + pglobal = new char[189];
  50 + }
  51 + printf("global leak: memory leaked\n");
  52 +}
  53 +void global_leak() {
  54 + global_leak_imp();
  55 +}
  56 +
  57 +void handler(int sig) {
  58 + exit(0);
  59 +}
  60 +int main(int argc, char** argv) {
  61 + signal(SIGINT, handler);
  62 +
  63 + global_leak();
  64 + printf("press CTRL+C if you want to abort the program.\n");
  65 + sleep(3);
  66 +
  67 + explicit_leak();
  68 + printf("press CTRL+C if you want to abort the program.\n");
  69 + sleep(3);
  70 +
  71 + return 0;
  72 +}
  73 +
  1 +heap_profiler: heap_profiler.cc Makefile
  2 + g++ -o heap_profiler heap_profiler.cc -g -O0 -ansi \
  3 + -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free \
  4 + -I../../../objs/gperf/include ../../../objs/gperf/lib/libtcmalloc_and_profiler.a -lpthread
  5 +clean:
  6 + rm -f heap_profiler srs.*.heap
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +/**
  24 +set the pprof path if not set:
  25 + export PPROF_PATH=`pwd`/../../../objs/pprof
  26 +to do mem profile:
  27 + make && rm -f srs.*.heap && env ./heap_profiler
  28 + $PPROF_PATH --text heap_profiler ./*.heap
  29 +*/
  30 +#include <stdio.h>
  31 +#include <unistd.h>
  32 +#include <signal.h>
  33 +#include <stdlib.h>
  34 +
  35 +#include <gperftools/heap-profiler.h>
  36 +
  37 +void memory_alloc_profile_imp() {
  38 + for (int i = 0; i < 2; ++i) {
  39 + char* p = new char[110 * 1024 * 1024];
  40 + for (int j = 0; j < 110 * 1024 * 1024; ++j) {
  41 + p[j] = j;
  42 + }
  43 + printf("mem profile, increase 110MB\n");
  44 + printf("press CTRL+C if you want to abort the program.\n");
  45 + sleep(5);
  46 + }
  47 +}
  48 +void memory_alloc_profile() {
  49 + memory_alloc_profile_imp();
  50 +}
  51 +
  52 +void handler(int sig) {
  53 + exit(0);
  54 +}
  55 +int main(int argc, char** argv) {
  56 + signal(SIGINT, handler);
  57 +
  58 + // must start profiler manually.
  59 + HeapProfilerStart("srs");
  60 +
  61 + memory_alloc_profile();
  62 + // not neccessary to call stop.
  63 + //HeapProfilerStop();
  64 +
  65 + return 0;
  66 +}
  67 +