blob: 630fa9d29d2a47d13c0f73f7a941bc4064ba1cfe [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"
32#include "pub_core_profile.h"
sewardjde4a1d02002-03-22 01:27:54 +000033
34/* get rid of these, if possible */
35#include <signal.h>
36#include <sys/time.h>
37
sewardjde4a1d02002-03-22 01:27:54 +000038
njn25e49d8e72002-09-23 09:36:25 +000039#define VGP_M_STACK 20
40#define VGP_MAX_CCS 50
41
njn25e49d8e72002-09-23 09:36:25 +000042/* All zeroed initially because they're static */
sewardjde4a1d02002-03-22 01:27:54 +000043static Int vgp_nticks;
njn25e49d8e72002-09-23 09:36:25 +000044
45static Int vgp_counts [VGP_MAX_CCS];
46static Int vgp_entries[VGP_MAX_CCS];
47static Char* vgp_names [VGP_MAX_CCS];
sewardjde4a1d02002-03-22 01:27:54 +000048
49static Int vgp_sp;
njn25e49d8e72002-09-23 09:36:25 +000050static UInt vgp_stack[VGP_M_STACK];
51
52/* These definitions override the panicking ones in vg_profile.c */
53
njn31066fd2005-03-26 00:42:02 +000054void VG_(register_profile_event) ( Int n, Char* name )
njn25e49d8e72002-09-23 09:36:25 +000055{
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);
njn67993252004-11-22 18:02:32 +000063 VG_(tool_panic)("profile event too high");
njn25e49d8e72002-09-23 09:36:25 +000064 }
65 if (vgp_names[n] != NULL) {
njn02bc4b82005-05-15 17:28:26 +000066 VG_(printf)("\nProfile event #%d being registered as '%s'\n"
67 "already registered as '%s'.\n"
nethercote7cc9c232004-01-21 15:08:04 +000068 "Note that tool and core event numbers must not overlap.\n",
njn25e49d8e72002-09-23 09:36:25 +000069 n, name, vgp_names[n]);
njn67993252004-11-22 18:02:32 +000070 VG_(tool_panic)("profile event already registered");
njn25e49d8e72002-09-23 09:36:25 +000071 }
72
73 vgp_names[n] = name;
74}
sewardjde4a1d02002-03-22 01:27:54 +000075
njn31513b42005-06-01 03:09:59 +000076static void tick ( int sigNo )
sewardjde4a1d02002-03-22 01:27:54 +000077{
78 Int cc;
79 vgp_nticks++;
80 cc = vgp_stack[vgp_sp];
njnca82cc02004-11-22 17:18:48 +000081 tl_assert(cc >= 0 && cc < VGP_MAX_CCS);
sewardjde4a1d02002-03-22 01:27:54 +000082 vgp_counts[ cc ]++;
83}
84
njn31066fd2005-03-26 00:42:02 +000085void VG_(init_profiling) ( void )
sewardjde4a1d02002-03-22 01:27:54 +000086{
87 struct itimerval value;
njn25e49d8e72002-09-23 09:36:25 +000088 Int ret;
sewardjde4a1d02002-03-22 01:27:54 +000089
njn31513b42005-06-01 03:09:59 +000090#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
njn25e49d8e72002-09-23 09:36:25 +000096 /* Register core events... tricky macro definition causes
njn31066fd2005-03-26 00:42:02 +000097 VG_(register_profile_event)() to be called once for each core event
njn25e49d8e72002-09-23 09:36:25 +000098 in VGP_CORE_LIST. */
njnca82cc02004-11-22 17:18:48 +000099 tl_assert(VgpUnc == 0);
njn31066fd2005-03-26 00:42:02 +0000100# define VGP_PAIR(n,name) VG_(register_profile_event)(n,name)
njn25e49d8e72002-09-23 09:36:25 +0000101 VGP_CORE_LIST;
102# undef VGP_PAIR
sewardjde4a1d02002-03-22 01:27:54 +0000103
sewardjde4a1d02002-03-22 01:27:54 +0000104 vgp_sp = -1;
njn31066fd2005-03-26 00:42:02 +0000105 VG_(pushcc) ( VgpUnc );
sewardjde4a1d02002-03-22 01:27:54 +0000106
107 value.it_interval.tv_sec = 0;
108 value.it_interval.tv_usec = 10 * 1000;
109 value.it_value = value.it_interval;
110
njn31513b42005-06-01 03:09:59 +0000111 signal(SIGPROF, tick );
sewardjde4a1d02002-03-22 01:27:54 +0000112 ret = setitimer(ITIMER_PROF, &value, NULL);
njn31513b42005-06-01 03:09:59 +0000113 if (ret != 0) VG_(core_panic)("vgp_init_profiling");
sewardjde4a1d02002-03-22 01:27:54 +0000114}
115
njn31066fd2005-03-26 00:42:02 +0000116void VG_(done_profiling) ( void )
sewardjde4a1d02002-03-22 01:27:54 +0000117{
118 Int i;
njn25e49d8e72002-09-23 09:36:25 +0000119 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] );
sewardjde4a1d02002-03-22 01:27:54 +0000127}
128
njn31066fd2005-03-26 00:42:02 +0000129void VG_(pushcc) ( UInt cc )
sewardjde4a1d02002-03-22 01:27:54 +0000130{
njn25e49d8e72002-09-23 09:36:25 +0000131 if (vgp_sp >= VGP_M_STACK-1) {
132 VG_(printf)(
njn02bc4b82005-05-15 17:28:26 +0000133 "\nMaximum profile stack depth (%d) reached for event #%d ('%s').\n"
njn31066fd2005-03-26 00:42:02 +0000134 "This is probably due to a VG_(pushcc)() without a matching\n"
135 "VG_(popcc)(). Make sure they all match.\n"
njn25e49d8e72002-09-23 09:36:25 +0000136 "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]);
njn31513b42005-06-01 03:09:59 +0000139 VG_(core_panic)("Profiling stack overflow");
njn25e49d8e72002-09-23 09:36:25 +0000140 }
sewardjde4a1d02002-03-22 01:27:54 +0000141 vgp_sp++;
142 vgp_stack[vgp_sp] = cc;
143 vgp_entries[ cc ] ++;
144}
145
njn31066fd2005-03-26 00:42:02 +0000146void VG_(popcc) ( UInt cc )
sewardjde4a1d02002-03-22 01:27:54 +0000147{
njn25e49d8e72002-09-23 09:36:25 +0000148 if (vgp_sp <= 0) {
149 VG_(printf)(
njn31066fd2005-03-26 00:42:02 +0000150 "\nProfile stack underflow. This is due to a VG_(popcc)() without\n"
151 "a matching VG_(pushcc)(). Make sure they all match.\n");
njn31513b42005-06-01 03:09:59 +0000152 VG_(core_panic)("Profiling stack underflow");
njn25e49d8e72002-09-23 09:36:25 +0000153 }
154 if (vgp_stack[vgp_sp] != cc) {
155 Int i;
njn31513b42005-06-01 03:09:59 +0000156 VG_(printf)("profiling problem:\n");
njn25e49d8e72002-09-23 09:36:25 +0000157 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 }
sewardjde4a1d02002-03-22 01:27:54 +0000162 vgp_sp--;
163}
164
njn31513b42005-06-01 03:09:59 +0000165/*--------------------------------------------------------------------*/
166/*--- end ---*/
167/*--------------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000168
njn31513b42005-06-01 03:09:59 +0000169