Profiler.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 81 CPU Profiling Routines */
10 
92 #include "squid.h"
93 #include "profiler/Profiler.h"
94 
95 #if USE_XPROF_STATS
96 
97 #include <cassert>
98 #if HAVE_GNUMALLLOC_H
99 #include <gnumalloc.h>
100 #elif HAVE_MALLOC_H
101 #include <malloc.h>
102 #endif
103 #if HAVE_UNISTD_H
104 #include <unistd.h>
105 #endif
106 
107 /* Exported Data */
109 
110 /* Private stuff */
111 
112 /* new stuff */
113 #define MAXSTACKDEPTH 512
114 
116  int timer; /* index into timers array */
117  const char *name;
119 };
120 
122 int cstack_head = 0;
123 
124 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
125 static __inline void
126 #else
127 static inline void
128 #endif
130 {
131  if (head->delta < head->best)
132  head->best = head->delta;
133  if (head->worst < head->delta)
134  head->worst = head->delta;
135  head->summ += head->delta;
136  ++head->count;
137 }
138 
140 static int xprof_inited = 0;
141 
142 static void
144 {
145  if (xprof_inited)
146  return;
147 
148  xprof_Timers = static_cast<TimersArray *>(calloc(XPROF_LAST + 2, sizeof(xprof_stats_node)));
149 
150  xprof_Timers[XPROF_PROF_UNACCOUNTED]->name = "PROF_UNACCOUNTED";
151  xprof_Timers[XPROF_PROF_UNACCOUNTED]->accu.start = get_tick();
152  xp_UNACCOUNTED = &xprof_Timers[XPROF_PROF_UNACCOUNTED]->accu;
153  cstack_head = 0;
154  xprof_inited = 1;
155 }
156 
157 void
159 {
160  hrtime_t tt = get_tick();
161  if (!xprof_inited)
162  xprof_InitLib();
163 
164  /* If nested, stop current stack frame */
165  if (cstack_head > 0) {
166  cstack[cstack_head - 1].accum += get_tick() - cstack[cstack_head - 1].start;
167  cstack[cstack_head - 1].start = -1;
168  }
169 
170  /* Are we at the first level? If so; stop the unaccounted timer */
171  if (cstack_head == 0) {
172  assert(xp_UNACCOUNTED->start != -1);
173  xp_UNACCOUNTED->delta = tt - xp_UNACCOUNTED->start;
174  xp_UNACCOUNTED->start = -1;
175  xprof_update(xp_UNACCOUNTED);
176  }
177 
178  /* Allocate new stack frame */
179  cstack[cstack_head].start = tt;
180  cstack[cstack_head].stop = -1;
181  cstack[cstack_head].accum = 0;
184  ++cstack_head;
186 
187 }
188 
189 void
191 {
192  hrtime_t tt = get_tick();
193  assert(cstack_head > 0);
194  --cstack_head;
195  assert(cstack[cstack_head].timer == type);
196 
197  /* Record timer details */
199  xprof_Timers[type]->accu.delta = cstack[cstack_head].accum;
200  xprof_Timers[type]->name = timer;
201 
202  /* Update */
203  xprof_update(&xprof_Timers[type]->accu);
204 
205  /* Restart previous timer if we're not at the top level */
206  if (cstack_head > 0) {
207  cstack[cstack_head - 1].start = tt;
208  cstack[cstack_head - 1].stop = 0;
209  return;
210  }
211  /* Get here? We're at the top level; unaccounted */
212  xp_UNACCOUNTED->start = tt;
213 }
214 
215 #endif /* USE_XPROF_STATS */
216 
#define assert(EX)
Definition: assert.h:17
struct _callstack_entry cstack[MAXSTACKDEPTH]
Definition: Profiler.cc:121
int type
Definition: errorpage.cc:79
static int xprof_inited
Definition: Profiler.cc:140
xprof_stats_node TimersArray[1]
Definition: Profiler.h:52
void xprof_stop(xprof_type type, const char *timer)
Definition: Profiler.cc:190
hrtime_t count
Definition: Profiler.h:41
hrtime_t delta
Definition: Profiler.h:38
hrtime_t accum
Definition: Profiler.cc:118
static void xprof_update(xprof_stats_data *head)
Definition: Profiler.cc:129
hrtime_t worst
Definition: Profiler.h:40
Definition: Profiler.cc:115
hrtime_t best
Definition: Profiler.h:39
static void xprof_InitLib(void)
Definition: Profiler.cc:143
int timer
Definition: Profiler.cc:116
#define MAXSTACKDEPTH
Definition: Profiler.cc:113
hrtime_t start
Definition: Profiler.cc:118
static xprof_stats_data * xp_UNACCOUNTED
Definition: Profiler.cc:139
hrtime_t start
Definition: Profiler.h:36
hrtime_t stop
Definition: Profiler.cc:118
int cstack_head
Definition: Profiler.cc:122
TimersArray * xprof_Timers
Definition: Profiler.cc:108
void xprof_start(xprof_type type, const char *timer)
Definition: Profiler.cc:158
const char * name
Definition: Profiler.cc:117
int64_t summ
Definition: Profiler.h:43
xprof_type
Definition: xprof_type.h:13
int64_t hrtime_t
Definition: get_tick.h:19
squidaio_request_t * head
Definition: aiops.cc:127
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors