blob: 158eb938d0cf0be6287649b838c2f11b9cb986de [file] [log] [blame]
nethercote5912c812004-02-15 15:38:08 +00001/* This file is part of hp2ps, a graph drawer for memory profiles.
2 Copyright (C) 2002 The University Court of the University of Glasgow.
3 This program is governed by the license contained in the file LICENSE. */
4
nethercotec9f36922004-02-14 16:40:02 +00005#include <stdio.h>
nethercotea0b77912004-06-02 20:43:24 +00006#include <stdlib.h>
nethercotec9f36922004-02-14 16:40:02 +00007#include "Main.h"
8#include "Defines.h"
9#include "HpFile.h"
10#include "Error.h"
11#include "Utilities.h"
12
13/* own stuff */
14#include "TraceElement.h"
15
16/*
17 * Compute the total volume for each identifier, and the grand
18 * total of these totals. The identifiers whose totals when
19 * added together amount to less that a threshold percentage
20 * (default 1%) of the grand total are considered to be ``trace
21 * elements'' and they are thrown away.
22 */
23
nethercotec9f36922004-02-14 16:40:02 +000024extern floatish thresholdpercent;
25
26void TraceElement()
27{
28 intish i;
29 intish j;
30 struct chunk* ch;
31 floatish grandtotal;
32 intish min;
33 floatish t;
34 floatish p;
35 struct entry* e;
36 intish *totals;
37
38 totals = (intish *) xmalloc(nidents * sizeof(intish));
39
40 /* find totals */
41
42 for (i = 0; i < nidents; i++) {
43 totals[ i ] = 0;
44 }
45
46 for (i = 0; i < nidents; i++) {
47 for (ch = identtable[i]->chk; ch; ch = ch->next) {
48 for (j = 0; j < ch->nd; j++) {
49 totals[ i ] += ch->d[j].value;
50 }
51 }
52 }
53
54 /* sort on the basis of total */
55
56 for (i = 0; i < nidents-1; i++) {
57 min = i;
58 for (j = i+1; j < nidents; j++) {
59 if (totals[ j ] < totals[ min ]) {
60 min = j;
61 }
62 }
63
64 t = totals[ min ];
65 totals[ min ] = totals[ i ];
66 totals[ i ] = t;
67
68 e = identtable[ min ];
69 identtable[ min ] = identtable[ i ];
70 identtable[ i ] = e;
71 }
72
73
74 /* find the grand total (NB: can get *BIG*!) */
75
76 grandtotal = 0.0;
77
78 for (i = 0; i < nidents; i++) {
79 grandtotal += (floatish) totals[ i ];
80 }
81
82 t = 0.0; /* cumulative percentage */
83
84 for (i = 0; i < nidents; i++) {
85 p = (100.0 * (floatish) totals[i]) / grandtotal;
86 t = t + p;
87 if (t >= THRESHOLD_PERCENT) {
88 break;
89 }
90 }
91
92 /* identifiers from 0 to i-1 should be removed */
93 for (j = 0; i < nidents; i++, j++) {
94 identtable[j] = identtable[i];
95 }
96
97 nidents = j;
98
99 free(totals);
100}