summaryrefslogtreecommitdiffstats
path: root/tdecachegrind/tdecachegrind/coverage.cpp
blob: 86e6f7ff1e68e57e0d0fea4edc31899e82c7c5c5 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* This file is part of KCachegrind.
   Copyright (C) 2002 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind 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, version 2.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * Function Coverage Analysis
 */

#include "coverage.h"

//#define DEBUG_COVERAGE 1

TraceCostType* Coverage::_costType;

const int Coverage::maxHistogramDepth = maxHistogramDepthValue;
const int Coverage::Rtti = 1;

Coverage::Coverage()
{
}

void Coverage::init()
{
  _self = 0.0;
  _incl  = 0.0;
  _callCount = 0.0;
  // should always be overwritten before usage
  _firstPercentage = 1.0;
  _minDistance = 9999;
  _maxDistance = 0;
  _active = false;
  _inRecursion = false;
  for (int i = 0;i<maxHistogramDepth;i++) {
    _selfHisto[i] = 0.0;
    _inclHisto[i] = 0.0;
  }

  _valid = true;
}

int Coverage::inclusiveMedian()
{
  double maxP = _inclHisto[0];
  int medD = 0;
  for (int i = 1;i<maxHistogramDepth;i++)
    if (_inclHisto[i]>maxP) {
      maxP = _inclHisto[i];
      medD = i;
    }

  return medD;
}

int Coverage::selfMedian()
{
  double maxP = _selfHisto[0];
  int medD = 0;
  for (int i = 1;i<maxHistogramDepth;i++)
    if (_selfHisto[i]>maxP) {
      maxP = _selfHisto[i];
      medD = i;
    }

  return medD;
}

TraceFunctionList Coverage::coverage(TraceFunction* f, CoverageMode m,
                                      TraceCostType* ct)
{
  invalidate(f->data(), Coverage::Rtti);

  _costType = ct;

  // function f takes ownership over c!
  Coverage* c = new Coverage();
  c->setFunction(f);
  c->init();

  TraceFunctionList l;

  if (m == Caller)
    c->addCallerCoverage(l, 1.0, 0);
  else
    c->addCallingCoverage(l, 1.0, 1.0, 0);

  return l;
}

void Coverage::addCallerCoverage(TraceFunctionList& fList,
                                 double pBack, int d)
{
  TraceCallList cList;
  TraceCall* call;
  Coverage* c;

  if (_inRecursion) return;

  double incl;
  incl  = (double) (_function->inclusive()->subCost(_costType));

  if (_active) {
#ifdef DEBUG_COVERAGE
    qDebug("CallerCov: D %d, %s (was active, incl %f, self %f): newP %f", d,
           _function->prettyName().ascii(), _incl, _self, pBack);
#endif
    _inRecursion = true;
  }
  else {
    _active = true;

    // only add cost if this is no recursion

    _incl += pBack;
    _firstPercentage = pBack;

    if (_minDistance > d) _minDistance = d;
    if (_maxDistance < d) _maxDistance = d;
    if (d<maxHistogramDepth) {
      _inclHisto[d] += pBack;
    }
    else {
      _inclHisto[maxHistogramDepth-1] += pBack;
    }

#ifdef DEBUG_COVERAGE
    qDebug("CallerCov: D %d, %s (now active, new incl %f): newP %f",
           d, _function->prettyName().ascii(), _incl, pBack);
#endif
  }

  double callVal, pBackNew;

  cList = _function->callers();
  for (call=cList.first();call;call=cList.next()) {
    if (call->inCycle()>0) continue;
    if (call->isRecursion()) continue;

    if (call->subCost(_costType)>0) {
      TraceFunction* caller = call->caller();

      c = (Coverage*) caller->assoziation(rtti());
      if (!c) {
        c = new Coverage();
        c->setFunction(caller);
      }
      if (!c->isValid()) {
        c->init();
        fList.append(caller);
      }

      if (c->isActive()) continue;
      if (c->inRecursion()) continue;

      callVal = (double) call->subCost(_costType);
      pBackNew = pBack * (callVal / incl);

      // FIXME ?!?

      if (!c->isActive()) {
	  if (d>=0)
	      c->callCount() += (double)call->callCount();
	  else
	      c->callCount() += _callCount;
      }
      else {
        // adjust pNew by sum of geometric series of recursion factor.
        // Thus we can avoid endless recursion here
        pBackNew *= 1.0 / (1.0 - pBackNew / c->firstPercentage());
      }

      // Limit depth
      if (pBackNew > 0.0001)
        c->addCallerCoverage(fList, pBackNew, d+1);
    }
  }

  if (_inRecursion)
    _inRecursion = false;
  else if (_active)
    _active = false;
}

/**
 * pForward is time on percent used,
 * pBack is given to allow for calculation of call counts
 */
void Coverage::addCallingCoverage(TraceFunctionList& fList,
                                  double pForward, double pBack, int d)
{
  TraceCallList cList;
  TraceCall* call;
  Coverage* c;

  if (_inRecursion) return;

#ifdef DEBUG_COVERAGE
  static const char* spaces = "                                            ";
#endif

  double self, incl;
  incl  = (double) (_function->inclusive()->subCost(_costType));

#ifdef DEBUG_COVERAGE
    qDebug("CngCov:%s - %s (incl %f, self %f): forward %f, back %f",
	   spaces+strlen(spaces)-d,
           _function->prettyName().ascii(), _incl, _self, pForward, pBack);
#endif


  if (_active) {
    _inRecursion = true;

#ifdef DEBUG_COVERAGE
    qDebug("CngCov:%s < %s: STOP (is active)",
	   spaces+strlen(spaces)-d,
           _function->prettyName().ascii());
#endif

  }
  else {
    _active = true;

    // only add cost if this is no recursion
    self = pForward * (_function->subCost(_costType)) / incl;
    _incl += pForward;
    _self += self;
    _firstPercentage = pForward;

    if (_minDistance > d) _minDistance = d;
    if (_maxDistance < d) _maxDistance = d;
     if (d<maxHistogramDepth) {
      _inclHisto[d] += pForward;
      _selfHisto[d] += self;
    }
    else {
      _inclHisto[maxHistogramDepth-1] += pForward;
      _selfHisto[maxHistogramDepth-1] += self;
    }

#ifdef DEBUG_COVERAGE
    qDebug("CngCov:%s < %s (incl %f, self %f)",
	   spaces+strlen(spaces)-d,
           _function->prettyName().ascii(), _incl, _self);
#endif
  }

  double callVal, pForwardNew, pBackNew;

  cList = _function->callings();
  for (call=cList.first();call;call=cList.next()) {
    if (call->inCycle()>0) continue;
    if (call->isRecursion()) continue;

    if (call->subCost(_costType)>0) {
      TraceFunction* calling = call->called();

      c = (Coverage*) calling->assoziation(rtti());
      if (!c) {
        c = new Coverage();
        c->setFunction(calling);
      }
      if (!c->isValid()) {
        c->init();
        fList.append(calling);
      }

      if (c->isActive()) continue;
      if (c->inRecursion()) continue;

      callVal = (double) call->subCost(_costType);
      pForwardNew = pForward * (callVal / incl);
      pBackNew    = pBack * (callVal / 
			     calling->inclusive()->subCost(_costType));

      if (!c->isActive()) {
	  c->callCount() += pBack * call->callCount();

#ifdef DEBUG_COVERAGE
        qDebug("CngCov:%s  > %s: forward %f, back %f, calls %f -> %f, now %f",
	       spaces+strlen(spaces)-d,
               calling->prettyName().ascii(),
	       pForwardNew, pBackNew,
	       (double)call->callCount(),
	       pBack * call->callCount(),
	       c->callCount());
#endif
      }
      else {
        // adjust pNew by sum of geometric series of recursion factor.
        // Thus we can avoid endless recursion here
        double fFactor = 1.0 / (1.0 - pForwardNew / c->firstPercentage());
        double bFactor = 1.0 / (1.0 - pBackNew);
#ifdef DEBUG_COVERAGE
        qDebug("CngCov:%s Recursion - origP %f, actP %f => factor %f, newP %f",
	       spaces+strlen(spaces)-d,
               c->firstPercentage(), pForwardNew,
	       fFactor, pForwardNew * fFactor);
#endif
        pForwardNew *= fFactor;
        pBackNew *= bFactor;

      }

      // Limit depth
      if (pForwardNew > 0.0001)
        c->addCallingCoverage(fList, pForwardNew, pBackNew, d+1);
    }
  }

  if (_inRecursion)
    _inRecursion = false;
  else if (_active)
    _active = false;
}