winlin

add cpu profile

  1 +cpu_profiler: cpu_profiler.cc Makefile
  2 + g++ -o cpu_profiler cpu_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 cpu_profiler ./srs.conf*
  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 cpu profile:
  27 + make && rm -f ./srs.conf* && env CPUPROFILE=./srs.conf ./cpu_profiler
  28 + $PPROF_PATH --text cpu_profiler ./srs.conf*
  29 +to do cpu profile by signal:
  30 + make && rm -f ./srs.conf* && env CPUPROFILE=./srs.conf CPUPROFILESIGNAL=12 ./cpu_profiler
  31 + $PPROF_PATH --text cpu_profiler ./srs.conf*
  32 +*/
  33 +#include <stdio.h>
  34 +#include <unistd.h>
  35 +#include <signal.h>
  36 +#include <stdlib.h>
  37 +
  38 +#include <gperftools/profiler.h>
  39 +
  40 +void cpu_profile_imp() {
  41 + for (int i = 0; i < 2; ++i) {
  42 + for (int j = 0; j < 110 * 1024 * 1024; ++j) {
  43 + }
  44 + printf("cpu profile, loop 110M\n");
  45 + printf("press CTRL+C if you want to abort the program.\n");
  46 + sleep(3);
  47 + }
  48 +}
  49 +void cpu_profile() {
  50 + cpu_profile_imp();
  51 +}
  52 +
  53 +void handler(int sig) {
  54 + exit(0);
  55 +}
  56 +int main(int argc, char** argv) {
  57 + signal(SIGINT, handler);
  58 +
  59 + // must start profiler manually.
  60 + ProfilerStart(NULL);
  61 +
  62 + if (getenv("CPUPROFILESIGNAL")) {
  63 + printf("if specified CPUPROFILESIGNAL, use signal to active it: kill -12 %d\n", getpid());
  64 + sleep(3);
  65 + }
  66 +
  67 + cpu_profile();
  68 + // not neccessary to call stop.
  69 + //ProfilerStop();
  70 +
  71 + return 0;
  72 +}
  73 +