From: Amos Jeffries Date: Sun, 27 Feb 2011 09:44:25 +0000 (+1300) Subject: Bug 3164: Total memory info display 32-bit overflows X-Git-Tag: SQUID_3_1_12~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd4771cdb19b74f6fa9f99abaf039fe7eae415ba;p=thirdparty%2Fsquid.git Bug 3164: Total memory info display 32-bit overflows Use double instead of long. This removes any Squid-caused overflow wraps. Overflows may still appear since the underlying library is 32-bit. --- diff --git a/include/util.h b/include/util.h index 0bf570b4c2..85be84e35c 100644 --- a/include/util.h +++ b/include/util.h @@ -150,7 +150,7 @@ extern void gb_flush(gb_t *); /* internal, do not use this */ /* * Returns the amount of known allocated memory */ -int statMemoryAccounted(void); +double statMemoryAccounted(void); /* Windows Port */ /* win32lib.c */ diff --git a/lib/stub_memaccount.c b/lib/stub_memaccount.c index c49afc4a98..49b0a84bf2 100644 --- a/lib/stub_memaccount.c +++ b/lib/stub_memaccount.c @@ -5,8 +5,8 @@ /* Stub function for programs not implementing statMemoryAccounted */ #include "config.h" #include "util.h" -int +double statMemoryAccounted(void) { - return -1; + return -1.0; } diff --git a/lib/util.c b/lib/util.c index 2cee8433b8..c3196cc6bd 100644 --- a/lib/util.c +++ b/lib/util.c @@ -365,9 +365,9 @@ malloc_number(void *p) static void xmalloc_show_trace(void *p, int sign) { - int statMemoryAccounted(); - static size_t last_total = 0, last_accounted = 0, last_mallinfo = 0; - size_t accounted = statMemoryAccounted(); + static double last_accounted = 0; + static size_t last_total = 0, last_mallinfo = 0; + double accounted = statMemoryAccounted(); size_t mi = 0; size_t sz; #if HAVE_MALLINFO @@ -381,10 +381,10 @@ xmalloc_show_trace(void *p, int sign) xmalloc_count += sign > 0; if (xmalloc_trace) { - fprintf(stderr, "%c%8p size=%5d/%d acc=%5d/%d mallinfo=%5d/%d %s:%d %s", + fprintf(stderr, "%c%8p size=%5d/%d acc=%5.0f/%.0f mallinfo=%5d/%d %s:%d %s", sign > 0 ? '+' : '-', p, (int) xmalloc_total - last_total, (int) xmalloc_total, - (int) accounted - last_accounted, (int) accounted, + accounted - last_accounted, accounted, (int) mi - last_mallinfo, (int) mi, xmalloc_file, xmalloc_line, xmalloc_func); diff --git a/src/stat.cc b/src/stat.cc index 83af8ca517..a68b883137 100644 --- a/src/stat.cc +++ b/src/stat.cc @@ -445,14 +445,6 @@ info_get(StoreEntry * sentry) struct rusage rusage; double cputime; double runtime; -#if HAVE_MSTATS && HAVE_GNUMALLOC_H - - struct mstats ms; -#elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO - - struct mallinfo mp; - long t; -#endif runtime = tvSubDsec(squid_start, current_time); @@ -624,72 +616,73 @@ info_get(StoreEntry * sentry) #if HAVE_MSTATS && HAVE_GNUMALLOC_H - ms = mstats(); + + struct mstats ms = mstats(); storeAppendPrintf(sentry, "Memory usage for %s via mstats():\n",APP_SHORTNAME); - storeAppendPrintf(sentry, "\tTotal space in arena: %6ld KB\n", - (long)(ms.bytes_total >> 10)); + storeAppendPrintf(sentry, "\tTotal space in arena: %6.0f KB\n", + static_cast(ms.bytes_total / 1024)); - storeAppendPrintf(sentry, "\tTotal free: %6ld KB %d%%\n", - (long)(ms.bytes_free >> 10), Math::intPercent(ms.bytes_free, ms.bytes_total)); + storeAppendPrintf(sentry, "\tTotal free: %6.0f KB %.0f%%\n", + static_cast(ms.bytes_free / 1024), + Math::doublePercent(static_cast(ms.bytes_free), static_cast(ms.bytes_total))); #elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO - mp = mallinfo(); + struct mallinfo mp = mallinfo(); storeAppendPrintf(sentry, "Memory usage for %s via mallinfo():\n",APP_SHORTNAME); - storeAppendPrintf(sentry, "\tTotal space in arena: %6ld KB\n", - (long)(mp.arena >> 10)); + storeAppendPrintf(sentry, "\tTotal space in arena: %6.0f KB\n", + static_cast(mp.arena / 1024)); - storeAppendPrintf(sentry, "\tOrdinary blocks: %6ld KB %6ld blks\n", - (long)(mp.uordblks >> 10), (long)mp.ordblks); + storeAppendPrintf(sentry, "\tOrdinary blocks: %6.0f KB %6.0f blks\n", + static_cast(mp.uordblks / 1024), static_cast(mp.ordblks)); - storeAppendPrintf(sentry, "\tSmall blocks: %6ld KB %6ld blks\n", - (long)(mp.usmblks >> 10), (long)mp.smblks); + storeAppendPrintf(sentry, "\tSmall blocks: %6.0f KB %6.0f blks\n", + static_cast(mp.usmblks / 1024), static_cast(mp.smblks)); - storeAppendPrintf(sentry, "\tHolding blocks: %6ld KB %6ld blks\n", - (long)(mp.hblkhd >> 10), (long)mp.hblks); + storeAppendPrintf(sentry, "\tHolding blocks: %6.0f KB %6.0f blks\n", + static_cast(mp.hblkhd / 1024), static_cast(mp.hblks)); - storeAppendPrintf(sentry, "\tFree Small blocks: %6ld KB\n", - (long)(mp.fsmblks >> 10)); + storeAppendPrintf(sentry, "\tFree Small blocks: %6.0f KB\n", + static_cast(mp.fsmblks / 1024)); - storeAppendPrintf(sentry, "\tFree Ordinary blocks: %6ld KB\n", - (long)(mp.fordblks >> 10)); + storeAppendPrintf(sentry, "\tFree Ordinary blocks: %6.0f KB\n", + static_cast(mp.fordblks / 1024)); - t = mp.uordblks + mp.usmblks + mp.hblkhd; + double t = mp.uordblks + mp.usmblks + mp.hblkhd; - storeAppendPrintf(sentry, "\tTotal in use: %6ld KB %d%%\n", - (long)(t >> 10), Math::intPercent(t, mp.arena + mp.hblkhd)); + storeAppendPrintf(sentry, "\tTotal in use: %6.0f KB %.0f%%\n", + (t / 1024), Math::doublePercent(t, static_cast(mp.arena + mp.hblkhd))); t = mp.fsmblks + mp.fordblks; - storeAppendPrintf(sentry, "\tTotal free: %6ld KB %d%%\n", - (long)(t >> 10), Math::intPercent(t, mp.arena + mp.hblkhd)); + storeAppendPrintf(sentry, "\tTotal free: %6.0f KB %.0f%%\n", + (t / 1024), Math::doublePercent(t, static_cast(mp.arena + mp.hblkhd))); t = mp.arena + mp.hblkhd; - storeAppendPrintf(sentry, "\tTotal size: %6ld KB\n", - (long)(t >> 10)); + storeAppendPrintf(sentry, "\tTotal size: %6.0f KB\n", (t / 1024)); #if HAVE_STRUCT_MALLINFO_MXFAST - storeAppendPrintf(sentry, "\tmax size of small blocks:\t%d\n", mp.mxfast); + storeAppendPrintf(sentry, "\tmax size of small blocks:\t%.0f\n", static_cast(mp.mxfast)); - storeAppendPrintf(sentry, "\tnumber of small blocks in a holding block:\t%ld\n", - (long)mp.nlblks); + storeAppendPrintf(sentry, "\tnumber of small blocks in a holding block:\t%6.0f\n", + static_cast(mp.nlblks)); - storeAppendPrintf(sentry, "\tsmall block rounding factor:\t%ld\n", (long)mp.grain); + storeAppendPrintf(sentry, "\tsmall block rounding factor:\t%.0f\n", static_cast(mp.grain)); - storeAppendPrintf(sentry, "\tspace (including overhead) allocated in ord. blks:\t%ld\n", - (long)mp.uordbytes); + storeAppendPrintf(sentry, "\tspace (including overhead) allocated in ord. blks:\t%.0f\n", + static_cast(mp.uordbytes)); - storeAppendPrintf(sentry, "\tnumber of ordinary blocks allocated:\t%ld\n", - (long)mp.allocated); + storeAppendPrintf(sentry, "\tnumber of ordinary blocks allocated:\t%.0f\n", + static_cast(mp.allocated)); - storeAppendPrintf(sentry, "\tbytes used in maintaining the free tree:\t%ld\n", - (long)mp.treeoverhead); + storeAppendPrintf(sentry, "\tbytes used in maintaining the free tree:\t%.0f\n", + static_cast(mp.treeoverhead)); #endif /* HAVE_STRUCT_MALLINFO_MXFAST */ #endif /* HAVE_MALLINFO */ @@ -698,13 +691,13 @@ info_get(StoreEntry * sentry) #if !(HAVE_MSTATS && HAVE_GNUMALLOC_H) && HAVE_MALLINFO && HAVE_STRUCT_MALLINFO - storeAppendPrintf(sentry, "\tTotal accounted: %6ld KB %3d%%\n", - (long)(statMemoryAccounted() >> 10), Math::intPercent(statMemoryAccounted(), t)); + storeAppendPrintf(sentry, "\tTotal accounted: %6.0f KB %.0f%%\n", + (statMemoryAccounted() / 1024), Math::doublePercent(statMemoryAccounted(), t)); #else - storeAppendPrintf(sentry, "\tTotal accounted: %6ld KB\n", - (long)(statMemoryAccounted() >> 10)); + storeAppendPrintf(sentry, "\tTotal accounted: %6.0f KB\n", + (statMemoryAccounted() / 1024)); #endif { @@ -712,15 +705,15 @@ info_get(StoreEntry * sentry) memPoolGetGlobalStats(&mp_stats); #if !(HAVE_MSTATS && HAVE_GNUMALLOC_H) && HAVE_MALLINFO && HAVE_STRUCT_MALLINFO - storeAppendPrintf(sentry, "\tmemPool accounted: %6ld KB %3d%%\n", - (long)(mp_stats.TheMeter->alloc.level >> 10), - Math::intPercent(mp_stats.TheMeter->alloc.level, t)); + storeAppendPrintf(sentry, "\tmemPool accounted: %6.0f KB %.0f%%\n", + static_cast(mp_stats.TheMeter->alloc.level / 1024), + Math::doublePercent(static_cast(mp_stats.TheMeter->alloc.level), t)); - int iFree = 0; + double iFree = 0; if (t >= mp_stats.TheMeter->alloc.level) - iFree = Math::intPercent((t - mp_stats.TheMeter->alloc.level), t); - storeAppendPrintf(sentry, "\tmemPool unaccounted: %6ld KB %3d%%\n", - (long)((t - mp_stats.TheMeter->alloc.level) >> 10), iFree); + iFree = Math::doublePercent((t - static_cast(mp_stats.TheMeter->alloc.level)), t); + storeAppendPrintf(sentry, "\tmemPool unaccounted: %6.0f KB %.0f%%\n", + static_cast((t - mp_stats.TheMeter->alloc.level) / 1024), iFree); #endif storeAppendPrintf(sentry, "\tmemPoolAlloc calls: %9.0f\n", @@ -1774,8 +1767,8 @@ statGraphDump(StoreEntry * e) #endif /* STAT_GRAPHS */ -int +double statMemoryAccounted(void) { - return memPoolsTotalAllocated(); + return static_cast(memPoolsTotalAllocated()); }