sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 3 | /*--- Profiling machinery. m_profile.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 31 | #include "core.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame^] | 32 | #include "pub_core_libcprint.h" |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 33 | #include "pub_core_profile.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
| 35 | /* get rid of these, if possible */ |
| 36 | #include <signal.h> |
| 37 | #include <sys/time.h> |
| 38 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 39 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 40 | #define VGP_M_STACK 20 |
| 41 | #define VGP_MAX_CCS 50 |
| 42 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | /* All zeroed initially because they're static */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | static Int vgp_nticks; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 45 | |
| 46 | static Int vgp_counts [VGP_MAX_CCS]; |
| 47 | static Int vgp_entries[VGP_MAX_CCS]; |
| 48 | static Char* vgp_names [VGP_MAX_CCS]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 49 | |
| 50 | static Int vgp_sp; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 51 | static UInt vgp_stack[VGP_M_STACK]; |
| 52 | |
| 53 | /* These definitions override the panicking ones in vg_profile.c */ |
| 54 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 55 | void VG_(register_profile_event) ( Int n, Char* name ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 56 | { |
| 57 | /* Adjust for negative values */ |
| 58 | n += VgpUnc; |
| 59 | if (n >= VGP_MAX_CCS) { |
| 60 | VG_(printf)("\nProfile event #%d higher than VGP_MAX_CCS of %d.\n" |
| 61 | "If you really need this many profile events, increase\n" |
| 62 | "VGP_MAX_CCS and recompile Valgrind.\n", |
| 63 | n, VGP_MAX_CCS); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 64 | VG_(tool_panic)("profile event too high"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 65 | } |
| 66 | if (vgp_names[n] != NULL) { |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 67 | VG_(printf)("\nProfile event #%d being registered as '%s'\n" |
| 68 | "already registered as '%s'.\n" |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 69 | "Note that tool and core event numbers must not overlap.\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 70 | n, name, vgp_names[n]); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 71 | VG_(tool_panic)("profile event already registered"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | vgp_names[n] = name; |
| 75 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 76 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 77 | static void tick ( int sigNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 78 | { |
| 79 | Int cc; |
| 80 | vgp_nticks++; |
| 81 | cc = vgp_stack[vgp_sp]; |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 82 | tl_assert(cc >= 0 && cc < VGP_MAX_CCS); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 83 | vgp_counts[ cc ]++; |
| 84 | } |
| 85 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 86 | void VG_(init_profiling) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 87 | { |
| 88 | struct itimerval value; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 89 | Int ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 90 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 91 | #ifndef VG_DO_PROFILING |
| 92 | VG_(printf)("valgrind: you must compile with VG_DO_PROFILING defined\n"); |
| 93 | VG_(printf)(" before using --profile=yes. Aborting.\n"); |
| 94 | VG_(exit)(1); |
| 95 | #endif |
| 96 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 97 | /* Register core events... tricky macro definition causes |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 98 | VG_(register_profile_event)() to be called once for each core event |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 99 | in VGP_CORE_LIST. */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 100 | tl_assert(VgpUnc == 0); |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 101 | # define VGP_PAIR(n,name) VG_(register_profile_event)(n,name) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 102 | VGP_CORE_LIST; |
| 103 | # undef VGP_PAIR |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 104 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 105 | vgp_sp = -1; |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 106 | VG_(pushcc) ( VgpUnc ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 107 | |
| 108 | value.it_interval.tv_sec = 0; |
| 109 | value.it_interval.tv_usec = 10 * 1000; |
| 110 | value.it_value = value.it_interval; |
| 111 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 112 | signal(SIGPROF, tick ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | ret = setitimer(ITIMER_PROF, &value, NULL); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 114 | if (ret != 0) VG_(core_panic)("vgp_init_profiling"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 117 | void VG_(done_profiling) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 118 | { |
| 119 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 120 | VG_(printf)("\nProfiling done, %d ticks\n", vgp_nticks); |
| 121 | for (i = 0; i < VGP_MAX_CCS; i++) |
| 122 | if (NULL != vgp_names[i]) |
| 123 | VG_(printf)( |
| 124 | "%2d: %4d (%3d %%%%) ticks, %10d entries for %s\n", |
| 125 | i, vgp_counts[i], |
| 126 | (Int)(1000.0 * (double)vgp_counts[i] / (double)vgp_nticks), |
| 127 | vgp_entries[i], vgp_names[i] ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | } |
| 129 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 130 | void VG_(pushcc) ( UInt cc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 131 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 132 | if (vgp_sp >= VGP_M_STACK-1) { |
| 133 | VG_(printf)( |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 134 | "\nMaximum profile stack depth (%d) reached for event #%d ('%s').\n" |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 135 | "This is probably due to a VG_(pushcc)() without a matching\n" |
| 136 | "VG_(popcc)(). Make sure they all match.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 137 | "Or if you are nesting profiling events very deeply, increase\n" |
| 138 | "VGP_M_STACK and recompile Valgrind.\n", |
| 139 | VGP_M_STACK, cc, vgp_names[cc]); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 140 | VG_(core_panic)("Profiling stack overflow"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 141 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 142 | vgp_sp++; |
| 143 | vgp_stack[vgp_sp] = cc; |
| 144 | vgp_entries[ cc ] ++; |
| 145 | } |
| 146 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 147 | void VG_(popcc) ( UInt cc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 148 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 149 | if (vgp_sp <= 0) { |
| 150 | VG_(printf)( |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 151 | "\nProfile stack underflow. This is due to a VG_(popcc)() without\n" |
| 152 | "a matching VG_(pushcc)(). Make sure they all match.\n"); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 153 | VG_(core_panic)("Profiling stack underflow"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 154 | } |
| 155 | if (vgp_stack[vgp_sp] != cc) { |
| 156 | Int i; |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 157 | VG_(printf)("profiling problem:\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 158 | VG_(printf)("popping %s, stack looks like:\n", vgp_names[cc]); |
| 159 | for (i = vgp_sp; i >= 0; i--) |
| 160 | VG_(printf)("%2d: %s\n", i, vgp_names[vgp_stack[i]]); |
| 161 | VG_(exit)(1); |
| 162 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 163 | vgp_sp--; |
| 164 | } |
| 165 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 166 | /*--------------------------------------------------------------------*/ |
| 167 | /*--- end ---*/ |
| 168 | /*--------------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 169 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 170 | |