4fdc57ac076d83060c280ad2f9b3f14b98a69958
[gentoo.git] /
1 From c7275791a89623ad1edc820a6ef16faae37a18aa Mon Sep 17 00:00:00 2001
2 From: Chris Kühl <chrisk@openismus.com>
3 Date: Sun, 17 Apr 2011 22:31:00 +0000
4 Subject: Fix network totals overflow on 32-bit machines.
5
6 https://bugzilla.gnome.org/show_bug.cgi?id=639212
7 ---
8 diff --git a/src/load-graph.cpp b/src/load-graph.cpp
9 index 57bca17..a6d2a7e 100644
10 --- a/src/load-graph.cpp
11 +++ b/src/load-graph.cpp
12 @@ -368,16 +368,16 @@ get_memory (LoadGraph *g)
13  }
14  
15  static void
16 -net_scale (LoadGraph *g, unsigned din, unsigned dout)
17 +net_scale (LoadGraph *g, guint64 din, guint64 dout)
18  {
19         g->data[0][0] = 1.0f * din / g->net.max;
20         g->data[0][1] = 1.0f * dout / g->net.max;
21  
22 -       unsigned dmax = std::max(din, dout);
23 +        guint64 dmax = std::max(din, dout);
24         g->net.values[g->net.cur] = dmax;
25         g->net.cur = (g->net.cur + 1) % LoadGraph::NUM_POINTS;
26  
27 -       unsigned new_max;
28 +       guint64 new_max;
29         // both way, new_max is the greatest value
30         if (dmax >= g->net.max)
31                 new_max = dmax;
32 @@ -389,7 +389,7 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
33         // Round network maximum
34         //
35  
36 -       const unsigned bak_max(new_max);
37 +       const guint64 bak_max(new_max);
38  
39         if (ProcData::get_instance()->config.network_in_bits) {
40           // TODO: fix logic to give a nice scale with bits
41 @@ -399,7 +399,7 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
42           new_max = 1.1 * new_max;
43           // make sure max is not 0 to avoid / 0
44           // default to 125 bytes == 1kbit
45 -         new_max = std::max(new_max, 125U);
46 +         new_max = std::max(new_max, 125UL);
47  
48         } else {
49           // round up to get some extra space
50 @@ -407,7 +407,7 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
51           new_max = 1.1 * new_max;
52           // make sure max is not 0 to avoid / 0
53           // default to 1 KiB
54 -         new_max = std::max(new_max, 1024U);
55 +         new_max = std::max(new_max, 1024UL);
56  
57           // decompose new_max = coef10 * 2**(base10 * 10)
58           // where coef10 and base10 are integers and coef10 < 2**10
59 @@ -415,16 +415,16 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
60           // e.g: ceil(100.5 KiB) = 101 KiB = 101 * 2**(1 * 10)
61           //      where base10 = 1, coef10 = 101, pow2 = 16
62  
63 -         unsigned pow2 = std::floor(log2(new_max));
64 -         unsigned base10 = pow2 / 10;
65 -         unsigned coef10 = std::ceil(new_max / double(1UL << (base10 * 10)));
66 +         guint64 pow2 = std::floor(log2(new_max));
67 +         guint64 base10 = pow2 / 10.0;
68 +         guint64 coef10 = std::ceil(new_max / double(1UL <<(base10 * 10)));
69           g_assert(new_max <= (coef10 * (1UL << (base10 * 10))));
70  
71           // then decompose coef10 = x * 10**factor10
72           // where factor10 is integer and x < 10
73           // so we new_max has only 1 significant digit
74  
75 -         unsigned factor10 = std::pow(10.0, std::floor(std::log10(coef10)));
76 +         guint64 factor10 = std::pow(10.0, std::floor(std::log10(coef10)));
77           coef10 = std::ceil(coef10 / double(factor10)) * factor10;
78  
79           // then make coef10 divisible by num_bars
80 @@ -432,12 +432,12 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
81             coef10 = coef10 + (g->num_bars() - coef10 % g->num_bars());
82           g_assert(coef10 % g->num_bars() == 0);
83  
84 -         new_max = coef10 * (1UL << (base10 * 10));
85 -         procman_debug("bak %u new_max %u pow2 %u coef10 %u", bak_max, new_max, pow2, coef10);
86 +         new_max = coef10 * (1UL << guint64(base10 * 10));
87 +         procman_debug("bak %lu new_max %lu pow2 %lu coef10 %lu", bak_max, new_max, pow2, coef10);
88         }
89  
90         if (bak_max > new_max) {
91 -         procman_debug("overflow detected: bak=%u new=%u", bak_max, new_max);
92 +         procman_debug("overflow detected: bak=%lu new=%lu", bak_max, new_max);
93           new_max = bak_max;
94         }
95  
96 @@ -446,7 +446,7 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
97         if ((0.8 * g->net.max) < new_max && new_max <= g->net.max)
98                 return;
99  
100 -       const float scale = 1.0f * g->net.max / new_max;
101 +       const double scale = 1.0f * g->net.max / new_max;
102  
103         for (size_t i = 0; i < LoadGraph::NUM_POINTS; i++) {
104                 if (g->data[i][0] >= 0.0f) {
105 @@ -455,7 +455,7 @@ net_scale (LoadGraph *g, unsigned din, unsigned dout)
106                 }
107         }
108  
109 -       procman_debug("rescale dmax = %u max = %u new_max = %u", dmax, g->net.max, new_max);
110 +       procman_debug("rescale dmax = %lu max = %lu new_max = %lu", dmax, g->net.max, new_max);
111  
112         g->net.max = new_max;
113  
114 @@ -471,7 +471,7 @@ get_net (LoadGraph *g)
115         guint32 i;
116         guint64 in = 0, out = 0;
117         GTimeVal time;
118 -       unsigned din, dout;
119 +       guint64 din, dout;
120  
121         ifnames = glibtop_get_netlist(&netlist);
122  
123 @@ -510,9 +510,9 @@ get_net (LoadGraph *g)
124             g->net.time.tv_sec != 0) {
125                 float dtime;
126                 dtime = time.tv_sec - g->net.time.tv_sec +
127 -                       (float) (time.tv_usec - g->net.time.tv_usec) / G_USEC_PER_SEC;
128 -               din   = static_cast<unsigned>((in  - g->net.last_in)  / dtime);
129 -               dout  = static_cast<unsigned>((out - g->net.last_out) / dtime);
130 +                       (double) (time.tv_usec - g->net.time.tv_usec) / G_USEC_PER_SEC;
131 +               din   = static_cast<guint64>((in  - g->net.last_in)  / dtime);
132 +               dout  = static_cast<guint64>((out - g->net.last_out) / dtime);
133         } else {
134                 /* Don't calc anything if new data is less than old (interface
135                    removed, counters reset, ...) or if it is the first time */
136 diff --git a/src/load-graph.h b/src/load-graph.h
137 index d90cf38..9b43c86 100644
138 --- a/src/load-graph.h
139 +++ b/src/load-graph.h
140 @@ -87,7 +87,7 @@ struct LoadGraph {
141                 struct {
142                         guint64 last_in, last_out;
143                         GTimeVal time;
144 -                       unsigned int max;
145 +                       guint64 max;
146                         unsigned values[NUM_POINTS];
147                         size_t cur;
148                 } net;
149 --
150 cgit v0.9