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