File: vg_profile.c

package info (click to toggle)
valgrind 1%3A2.4.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,384 kB
  • ctags: 8,831
  • sloc: ansic: 67,990; sh: 4,364; perl: 1,833; makefile: 1,125; asm: 978; cpp: 101
file content (173 lines) | stat: -rw-r--r-- 5,554 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

/*--------------------------------------------------------------------*/
/*--- Profiling machinery.  #include this file into a tool to      ---*/
/*--- enable --profile=yes, but not for release versions of tools, ---*/
/*--- because it uses glibc code.                                  ---*/
/*---                                                 vg_profile.c ---*/
/*--------------------------------------------------------------------*/

/*
   This file is part of Valgrind, an extensible x86 protected-mode
   emulator for monitoring program execution on x86-Unixes.

   Copyright (C) 2000-2004 Julian Seward 
      jseward@acm.org

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307, USA.

   The GNU General Public License is contained in the file COPYING.
*/

#ifndef __VG_PROFILE_C
#define __VG_PROFILE_C

#include "tool.h"

/* get rid of these, if possible */
#include <signal.h>
#include <sys/time.h>

/* Override the empty definitions from tool.h */
#undef  VGP_PUSHCC
#undef  VGP_POPCC
#define VGP_PUSHCC(x)   if (VG_(clo_profile)) VGP_(pushcc)(x)
#define VGP_POPCC(x)    if (VG_(clo_profile)) VGP_(popcc)(x)

#define VGP_M_STACK     20
#define VGP_MAX_CCS     50


/* All zeroed initially because they're static */
static Int   vgp_nticks;

static Int   vgp_counts [VGP_MAX_CCS];
static Int   vgp_entries[VGP_MAX_CCS];
static Char* vgp_names  [VGP_MAX_CCS];

static Int   vgp_sp;
static UInt  vgp_stack[VGP_M_STACK];

/* These definitions override the panicking ones in vg_profile.c */

void VGP_(register_profile_event) ( Int n, Char* name )
{
   /* Adjust for negative values */
   n += VgpUnc;
   if (n >= VGP_MAX_CCS) {
      VG_(printf)("\nProfile event #%d higher than VGP_MAX_CCS of %d.\n"
                  "If you really need this many profile events, increase\n"
                  "VGP_MAX_CCS and recompile Valgrind.\n",
                  n, VGP_MAX_CCS);
      VG_(skin_panic)("profile event too high");
   }
   if (vgp_names[n] != NULL) {
      VG_(printf)("\nProfile event #%d being registered as `%s'\n"
                  "already registered as `%s'.\n"
                  "Note that tool and core event numbers must not overlap.\n",
                  n, name, vgp_names[n]);
      VG_(skin_panic)("profile event already registered");
   }

   vgp_names[n] = name;
}

void VGP_(tick) ( int sigNo )
{
   Int cc;
   vgp_nticks++;
   cc = vgp_stack[vgp_sp];
   sk_assert(cc >= 0 && cc < VGP_MAX_CCS);
   vgp_counts[ cc ]++;
}

void VGP_(init_profiling) ( void )
{
   struct itimerval value;
   Int ret;

   /* Register core events... tricky macro definition causes
      VGP_(register_profile_event)() to be called once for each core event
      in VGP_CORE_LIST. */
   sk_assert(VgpUnc == 0);
#  define VGP_PAIR(n,name) VGP_(register_profile_event)(n,name)
   VGP_CORE_LIST;
#  undef  VGP_PAIR

   vgp_sp = -1;
   VGP_(pushcc) ( VgpUnc );

   value.it_interval.tv_sec  = 0;
   value.it_interval.tv_usec = 10 * 1000;
   value.it_value = value.it_interval;

   signal(SIGPROF, VGP_(tick) );
   ret = setitimer(ITIMER_PROF, &value, NULL);
   if (ret != 0) VG_(skin_panic)("vgp_init_profiling");
}

void VGP_(done_profiling) ( void )
{
   Int i;
   VG_(printf)("\nProfiling done, %d ticks\n", vgp_nticks);
   for (i = 0; i < VGP_MAX_CCS; i++)
      if (NULL != vgp_names[i])
         VG_(printf)(
            "%2d: %4d (%3d %%%%) ticks,  %10d entries   for  %s\n",
            i, vgp_counts[i], 
            (Int)(1000.0 * (double)vgp_counts[i] / (double)vgp_nticks),
            vgp_entries[i], vgp_names[i] );
}

void VGP_(pushcc) ( UInt cc )
{
   if (vgp_sp >= VGP_M_STACK-1) { 
      VG_(printf)(
         "\nMaximum profile stack depth (%d) reached for event #%d (`%s').\n"
         "This is probably due to a VGP_(pushcc)() without a matching\n"
         "VGP_(popcc)().  Make sure they all match.\n"
         "Or if you are nesting profiling events very deeply, increase\n"
         "VGP_M_STACK and recompile Valgrind.\n",
         VGP_M_STACK, cc, vgp_names[cc]);
      VG_(skin_panic)("Profiling stack overflow");
   }
   vgp_sp++;
   vgp_stack[vgp_sp] = cc;
   vgp_entries[ cc ] ++;
}

void VGP_(popcc) ( UInt cc )
{
   if (vgp_sp <= 0) {
      VG_(printf)(
         "\nProfile stack underflow.  This is due to a VGP_(popcc)() without\n"
         "a matching VGP_(pushcc)().  Make sure they all match.\n");
      VG_(skin_panic)("Profiling stack underflow");
   }
   if (vgp_stack[vgp_sp] != cc) {
      Int i;
      VG_(printf)("popping %s, stack looks like:\n", vgp_names[cc]);
      for (i = vgp_sp; i >= 0; i--)
         VG_(printf)("%2d: %s\n", i, vgp_names[vgp_stack[i]]);
      VG_(exit)(1);
   }
   vgp_sp--;
}

#endif /* __VG_PROFILE_C */

/*--------------------------------------------------------------------*/
/*--- end                                             vg_profile.c ---*/
/*--------------------------------------------------------------------*/