blob: ed10eded2f6da89c1f7953706e092563831cb277 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- Profiling machinery -- not for release builds! ---*/
4/*--- vg_profile.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of Valgrind, an x86 protected-mode emulator
9 designed for debugging and profiling binaries on x86-Unixes.
10
11 Copyright (C) 2000-2002 Julian Seward
12 jseward@acm.org
13 Julian_Seward@muraroa.demon.co.uk
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 02111-1307, USA.
29
30 The GNU General Public License is contained in the file LICENSE.
31*/
32
33#include "vg_include.h"
34
35#ifdef VG_PROFILE
36
37/* get rid of these, if possible */
38#include <signal.h>
39#include <sys/time.h>
40
41#define VGP_PAIR(enumname,str) str
42static const Char* vgp_names[VGP_M_CCS] = { VGP_LIST };
43#undef VGP_PAIR
44
45static Int vgp_nticks;
46static Int vgp_counts[VGP_M_CCS];
47static Int vgp_entries[VGP_M_CCS];
48
49static Int vgp_sp;
50static VgpCC vgp_stack[VGP_M_STACK];
51
52void VGP_(tick) ( int sigNo )
53{
54 Int cc;
55 vgp_nticks++;
56 cc = vgp_stack[vgp_sp];
57 vg_assert(cc >= 0 && cc < VGP_M_CCS);
58 vgp_counts[ cc ]++;
59}
60
61void VGP_(init_profiling) ( void )
62{
63 struct itimerval value;
64 Int i, ret;
65
66 for (i = 0; i < VGP_M_CCS; i++)
67 vgp_counts[i] = vgp_entries[i] = 0;
68
69 vgp_nticks = 0;
70 vgp_sp = -1;
71 VGP_(pushcc) ( VgpRun );
72
73 value.it_interval.tv_sec = 0;
74 value.it_interval.tv_usec = 10 * 1000;
75 value.it_value = value.it_interval;
76
77 signal(SIGPROF, VGP_(tick) );
78 ret = setitimer(ITIMER_PROF, &value, NULL);
79 if (ret != 0) VG_(panic)("vgp_init_profiling");
80}
81
82void VGP_(done_profiling) ( void )
83{
84 Int i;
85 VG_(printf)("Profiling done, %d ticks\n", vgp_nticks);
86 for (i = 0; i < VGP_M_CCS; i++)
87 VG_(printf)("%2d: %4d (%3d %%%%) ticks, %8d entries for %s\n",
88 i, vgp_counts[i],
89 (Int)(1000.0 * (double)vgp_counts[i] / (double)vgp_nticks),
90 vgp_entries[i],
91 vgp_names[i] );
92}
93
94void VGP_(pushcc) ( VgpCC cc )
95{
96 if (vgp_sp >= VGP_M_STACK-1) VG_(panic)("vgp_pushcc");
97 vgp_sp++;
98 vgp_stack[vgp_sp] = cc;
99 vgp_entries[ cc ] ++;
100}
101
102void VGP_(popcc) ( void )
103{
104 if (vgp_sp <= 0) VG_(panic)("vgp_popcc");
105 vgp_sp--;
106}
107
108#endif /* VG_PROFILE */
109
110/*--------------------------------------------------------------------*/
111/*--- end vg_profile.c ---*/
112/*--------------------------------------------------------------------*/