blob: 2f72aa308d0529bb9490ac728cf880a7142f6860 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njn31513b42005-06-01 03:09:59 +00003/*--- Profiling machinery. m_profile.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
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
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
njn31513b42005-06-01 03:09:59 +000031#include "core.h"
njn36a20fa2005-06-03 03:08:39 +000032#include "pub_core_libcprint.h"
njn31513b42005-06-01 03:09:59 +000033#include "pub_core_profile.h"
sewardjde4a1d02002-03-22 01:27:54 +000034
35/* get rid of these, if possible */
36#include <signal.h>
37#include <sys/time.h>
38
sewardjde4a1d02002-03-22 01:27:54 +000039
njn25e49d8e72002-09-23 09:36:25 +000040#define VGP_M_STACK 20
41#define VGP_MAX_CCS 50
42
njn25e49d8e72002-09-23 09:36:25 +000043/* All zeroed initially because they're static */
sewardjde4a1d02002-03-22 01:27:54 +000044static Int vgp_nticks;
njn25e49d8e72002-09-23 09:36:25 +000045
46static Int vgp_counts [VGP_MAX_CCS];
47static Int vgp_entries[VGP_MAX_CCS];
48static Char* vgp_names [VGP_MAX_CCS];
sewardjde4a1d02002-03-22 01:27:54 +000049
50static Int vgp_sp;
njn25e49d8e72002-09-23 09:36:25 +000051static UInt vgp_stack[VGP_M_STACK];
52
53/* These definitions override the panicking ones in vg_profile.c */
54
njn31066fd2005-03-26 00:42:02 +000055void VG_(register_profile_event) ( Int n, Char* name )
njn25e49d8e72002-09-23 09:36:25 +000056{
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);
njn67993252004-11-22 18:02:32 +000064 VG_(tool_panic)("profile event too high");
njn25e49d8e72002-09-23 09:36:25 +000065 }
66 if (vgp_names[n] != NULL) {
njn02bc4b82005-05-15 17:28:26 +000067 VG_(printf)("\nProfile event #%d being registered as '%s'\n"
68 "already registered as '%s'.\n"
nethercote7cc9c232004-01-21 15:08:04 +000069 "Note that tool and core event numbers must not overlap.\n",
njn25e49d8e72002-09-23 09:36:25 +000070 n, name, vgp_names[n]);
njn67993252004-11-22 18:02:32 +000071 VG_(tool_panic)("profile event already registered");
njn25e49d8e72002-09-23 09:36:25 +000072 }
73
74 vgp_names[n] = name;
75}
sewardjde4a1d02002-03-22 01:27:54 +000076
njn31513b42005-06-01 03:09:59 +000077static void tick ( int sigNo )
sewardjde4a1d02002-03-22 01:27:54 +000078{
79 Int cc;
80 vgp_nticks++;
81 cc = vgp_stack[vgp_sp];
njnca82cc02004-11-22 17:18:48 +000082 tl_assert(cc >= 0 && cc < VGP_MAX_CCS);
sewardjde4a1d02002-03-22 01:27:54 +000083 vgp_counts[ cc ]++;
84}
85
njn31066fd2005-03-26 00:42:02 +000086void VG_(init_profiling) ( void )
sewardjde4a1d02002-03-22 01:27:54 +000087{
88 struct itimerval value;
njn25e49d8e72002-09-23 09:36:25 +000089 Int ret;
sewardjde4a1d02002-03-22 01:27:54 +000090
njn31513b42005-06-01 03:09:59 +000091#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
njn25e49d8e72002-09-23 09:36:25 +000097 /* Register core events... tricky macro definition causes
njn31066fd2005-03-26 00:42:02 +000098 VG_(register_profile_event)() to be called once for each core event
njn25e49d8e72002-09-23 09:36:25 +000099 in VGP_CORE_LIST. */
njnca82cc02004-11-22 17:18:48 +0000100 tl_assert(VgpUnc == 0);
njn31066fd2005-03-26 00:42:02 +0000101# define VGP_PAIR(n,name) VG_(register_profile_event)(n,name)
njn25e49d8e72002-09-23 09:36:25 +0000102 VGP_CORE_LIST;
103# undef VGP_PAIR
sewardjde4a1d02002-03-22 01:27:54 +0000104
sewardjde4a1d02002-03-22 01:27:54 +0000105 vgp_sp = -1;
njn31066fd2005-03-26 00:42:02 +0000106 VG_(pushcc) ( VgpUnc );
sewardjde4a1d02002-03-22 01:27:54 +0000107
108 value.it_interval.tv_sec = 0;
109 value.it_interval.tv_usec = 10 * 1000;
110 value.it_value = value.it_interval;
111
njn31513b42005-06-01 03:09:59 +0000112 signal(SIGPROF, tick );
sewardjde4a1d02002-03-22 01:27:54 +0000113 ret = setitimer(ITIMER_PROF, &value, NULL);
njn31513b42005-06-01 03:09:59 +0000114 if (ret != 0) VG_(core_panic)("vgp_init_profiling");
sewardjde4a1d02002-03-22 01:27:54 +0000115}
116
njn31066fd2005-03-26 00:42:02 +0000117void VG_(done_profiling) ( void )
sewardjde4a1d02002-03-22 01:27:54 +0000118{
119 Int i;
njn25e49d8e72002-09-23 09:36:25 +0000120 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] );
sewardjde4a1d02002-03-22 01:27:54 +0000128}
129
njn31066fd2005-03-26 00:42:02 +0000130void VG_(pushcc) ( UInt cc )
sewardjde4a1d02002-03-22 01:27:54 +0000131{
njn25e49d8e72002-09-23 09:36:25 +0000132 if (vgp_sp >= VGP_M_STACK-1) {
133 VG_(printf)(
njn02bc4b82005-05-15 17:28:26 +0000134 "\nMaximum profile stack depth (%d) reached for event #%d ('%s').\n"
njn31066fd2005-03-26 00:42:02 +0000135 "This is probably due to a VG_(pushcc)() without a matching\n"
136 "VG_(popcc)(). Make sure they all match.\n"
njn25e49d8e72002-09-23 09:36:25 +0000137 "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]);
njn31513b42005-06-01 03:09:59 +0000140 VG_(core_panic)("Profiling stack overflow");
njn25e49d8e72002-09-23 09:36:25 +0000141 }
sewardjde4a1d02002-03-22 01:27:54 +0000142 vgp_sp++;
143 vgp_stack[vgp_sp] = cc;
144 vgp_entries[ cc ] ++;
145}
146
njn31066fd2005-03-26 00:42:02 +0000147void VG_(popcc) ( UInt cc )
sewardjde4a1d02002-03-22 01:27:54 +0000148{
njn25e49d8e72002-09-23 09:36:25 +0000149 if (vgp_sp <= 0) {
150 VG_(printf)(
njn31066fd2005-03-26 00:42:02 +0000151 "\nProfile stack underflow. This is due to a VG_(popcc)() without\n"
152 "a matching VG_(pushcc)(). Make sure they all match.\n");
njn31513b42005-06-01 03:09:59 +0000153 VG_(core_panic)("Profiling stack underflow");
njn25e49d8e72002-09-23 09:36:25 +0000154 }
155 if (vgp_stack[vgp_sp] != cc) {
156 Int i;
njn31513b42005-06-01 03:09:59 +0000157 VG_(printf)("profiling problem:\n");
njn25e49d8e72002-09-23 09:36:25 +0000158 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 }
sewardjde4a1d02002-03-22 01:27:54 +0000163 vgp_sp--;
164}
165
njn31513b42005-06-01 03:09:59 +0000166/*--------------------------------------------------------------------*/
167/*--- end ---*/
168/*--------------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000169
njn31513b42005-06-01 03:09:59 +0000170