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" |
| 32 | #include "pub_core_profile.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 33 | |
| 34 | /* get rid of these, if possible */ |
| 35 | #include <signal.h> |
| 36 | #include <sys/time.h> |
| 37 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 38 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 39 | #define VGP_M_STACK 20 |
| 40 | #define VGP_MAX_CCS 50 |
| 41 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 42 | /* All zeroed initially because they're static */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 43 | static Int vgp_nticks; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 44 | |
| 45 | static Int vgp_counts [VGP_MAX_CCS]; |
| 46 | static Int vgp_entries[VGP_MAX_CCS]; |
| 47 | static Char* vgp_names [VGP_MAX_CCS]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 48 | |
| 49 | static Int vgp_sp; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 50 | static UInt vgp_stack[VGP_M_STACK]; |
| 51 | |
| 52 | /* These definitions override the panicking ones in vg_profile.c */ |
| 53 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 54 | void VG_(register_profile_event) ( Int n, Char* name ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 55 | { |
| 56 | /* Adjust for negative values */ |
| 57 | n += VgpUnc; |
| 58 | if (n >= VGP_MAX_CCS) { |
| 59 | VG_(printf)("\nProfile event #%d higher than VGP_MAX_CCS of %d.\n" |
| 60 | "If you really need this many profile events, increase\n" |
| 61 | "VGP_MAX_CCS and recompile Valgrind.\n", |
| 62 | n, VGP_MAX_CCS); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 63 | VG_(tool_panic)("profile event too high"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | } |
| 65 | if (vgp_names[n] != NULL) { |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 66 | VG_(printf)("\nProfile event #%d being registered as '%s'\n" |
| 67 | "already registered as '%s'.\n" |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 68 | "Note that tool and core event numbers must not overlap.\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 69 | n, name, vgp_names[n]); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 70 | VG_(tool_panic)("profile event already registered"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | vgp_names[n] = name; |
| 74 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 75 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 76 | static void tick ( int sigNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 77 | { |
| 78 | Int cc; |
| 79 | vgp_nticks++; |
| 80 | cc = vgp_stack[vgp_sp]; |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 81 | tl_assert(cc >= 0 && cc < VGP_MAX_CCS); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 82 | vgp_counts[ cc ]++; |
| 83 | } |
| 84 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 85 | void VG_(init_profiling) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 86 | { |
| 87 | struct itimerval value; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 88 | Int ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 89 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 90 | #ifndef VG_DO_PROFILING |
| 91 | VG_(printf)("valgrind: you must compile with VG_DO_PROFILING defined\n"); |
| 92 | VG_(printf)(" before using --profile=yes. Aborting.\n"); |
| 93 | VG_(exit)(1); |
| 94 | #endif |
| 95 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 96 | /* Register core events... tricky macro definition causes |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 97 | VG_(register_profile_event)() to be called once for each core event |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 98 | in VGP_CORE_LIST. */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 99 | tl_assert(VgpUnc == 0); |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 100 | # define VGP_PAIR(n,name) VG_(register_profile_event)(n,name) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 101 | VGP_CORE_LIST; |
| 102 | # undef VGP_PAIR |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 103 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 104 | vgp_sp = -1; |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 105 | VG_(pushcc) ( VgpUnc ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 106 | |
| 107 | value.it_interval.tv_sec = 0; |
| 108 | value.it_interval.tv_usec = 10 * 1000; |
| 109 | value.it_value = value.it_interval; |
| 110 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 111 | signal(SIGPROF, tick ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 112 | ret = setitimer(ITIMER_PROF, &value, NULL); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 113 | if (ret != 0) VG_(core_panic)("vgp_init_profiling"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 114 | } |
| 115 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 116 | void VG_(done_profiling) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 117 | { |
| 118 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 119 | VG_(printf)("\nProfiling done, %d ticks\n", vgp_nticks); |
| 120 | for (i = 0; i < VGP_MAX_CCS; i++) |
| 121 | if (NULL != vgp_names[i]) |
| 122 | VG_(printf)( |
| 123 | "%2d: %4d (%3d %%%%) ticks, %10d entries for %s\n", |
| 124 | i, vgp_counts[i], |
| 125 | (Int)(1000.0 * (double)vgp_counts[i] / (double)vgp_nticks), |
| 126 | vgp_entries[i], vgp_names[i] ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 129 | void VG_(pushcc) ( UInt cc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 130 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 131 | if (vgp_sp >= VGP_M_STACK-1) { |
| 132 | VG_(printf)( |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 133 | "\nMaximum profile stack depth (%d) reached for event #%d ('%s').\n" |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 134 | "This is probably due to a VG_(pushcc)() without a matching\n" |
| 135 | "VG_(popcc)(). Make sure they all match.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 136 | "Or if you are nesting profiling events very deeply, increase\n" |
| 137 | "VGP_M_STACK and recompile Valgrind.\n", |
| 138 | VGP_M_STACK, cc, vgp_names[cc]); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 139 | VG_(core_panic)("Profiling stack overflow"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 140 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 141 | vgp_sp++; |
| 142 | vgp_stack[vgp_sp] = cc; |
| 143 | vgp_entries[ cc ] ++; |
| 144 | } |
| 145 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 146 | void VG_(popcc) ( UInt cc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 147 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 148 | if (vgp_sp <= 0) { |
| 149 | VG_(printf)( |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 150 | "\nProfile stack underflow. This is due to a VG_(popcc)() without\n" |
| 151 | "a matching VG_(pushcc)(). Make sure they all match.\n"); |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 152 | VG_(core_panic)("Profiling stack underflow"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 153 | } |
| 154 | if (vgp_stack[vgp_sp] != cc) { |
| 155 | Int i; |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 156 | VG_(printf)("profiling problem:\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 157 | VG_(printf)("popping %s, stack looks like:\n", vgp_names[cc]); |
| 158 | for (i = vgp_sp; i >= 0; i--) |
| 159 | VG_(printf)("%2d: %s\n", i, vgp_names[vgp_stack[i]]); |
| 160 | VG_(exit)(1); |
| 161 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 162 | vgp_sp--; |
| 163 | } |
| 164 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 165 | /*--------------------------------------------------------------------*/ |
| 166 | /*--- end ---*/ |
| 167 | /*--------------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 168 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame^] | 169 | |