]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Shuffle simple math functions into SquidMath
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 2 Dec 2009 22:39:11 +0000 (11:39 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 2 Dec 2009 22:39:11 +0000 (11:39 +1300)
This unlinks many depencies pulled in by tools.cc through the more
complicated permissions, and death reporting code.

14 files changed:
src/Makefile.am
src/ProfStats.cc
src/SquidMath.cc [new file with mode: 0644]
src/SquidMath.h [new file with mode: 0644]
src/client_db.cc
src/fs/coss/store_dir_coss.cc
src/fs/ufs/store_dir_ufs.cc
src/helper.cc
src/neighbors.cc
src/protos.h
src/snmp_agent.cc
src/stat.cc
src/store_dir.cc
src/tools.cc

index d9e242c7ce64fbaef8c84431a2e25dace1968114..6871baf219972814690ae1766494c77ca3a3a27b 100644 (file)
@@ -407,6 +407,8 @@ squid_SOURCES = \
        send-announce.cc \
        $(SNMP_SOURCE) \
        squid.h \
+       SquidMath.h \
+       SquidMath.cc \
        SquidNew.cc \
        $(SSL_SOURCE) \
        stat.cc \
@@ -1167,6 +1169,8 @@ tests_testCacheManager_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
@@ -1233,6 +1237,8 @@ tests_testDiskIO_SOURCES = \
        tests/stub_cache_manager.cc
 nodist_tests_testDiskIO_SOURCES= \
        $(SWAP_TEST_GEN_SOURCES) \
+       SquidMath.cc \
+       SquidMath.h \
        swap_log_op.cc
 tests_testDiskIO_LDADD = \
        $(SWAP_TEST_LDADD) \
@@ -1339,6 +1345,8 @@ tests_testEvent_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
@@ -1487,6 +1495,8 @@ tests_testEventLoop_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
@@ -1626,6 +1636,8 @@ tests_test_http_range_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
@@ -1778,6 +1790,8 @@ tests_testHttpRequest_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
@@ -1900,6 +1914,8 @@ tests_testStore_SOURCES= \
 
 nodist_tests_testStore_SOURCES= \
        $(TESTSOURCES) \
+       SquidMath.cc \
+       SquidMath.h \
        swap_log_op.cc
 
 tests_testStore_LDADD= \
@@ -1999,6 +2015,8 @@ tests_testUfs_SOURCES = \
        $(SWAP_TEST_SOURCES)
 nodist_tests_testUfs_SOURCES = \
        $(SWAP_TEST_GEN_SOURCES) \
+       SquidMath.cc \
+       SquidMath.h \
        swap_log_op.cc
 tests_testUfs_LDADD = \
        $(SWAP_TEST_LDADD) \
@@ -2134,6 +2152,8 @@ tests_testURL_SOURCES = \
        Server.cc \
        $(SNMP_SOURCE) \
        $(SSL_SOURCE) \
+       SquidMath.h \
+       SquidMath.cc \
        stat.cc \
        StatHist.cc \
        stmem.cc \
index 60724dc63e29590e109f0a285467a1d790e44e98..5e35ba731ea7ed23689ba80c96f1ba897d409428 100644 (file)
  */
 
 #include "squid.h"
-#include "event.h"
-#include "CacheManager.h"
 
 #ifdef USE_XPROF_STATS
+
+#include "CacheManager.h"
+#include "event.h"
+#include "SquidMath.h"
 #include "Store.h"
 
 /* Private stuff */
@@ -121,7 +123,7 @@ xprof_show_item(StoreEntry * sentry, const char *name, xprof_stats_data * hist)
                       hist->count ? hist->summ / hist->count : 0,
                       hist->worst,
                       hist->count / time_frame,
-                      dpercent((double) hist->summ, (double) hist->delta));
+                      Math::doublePercent((double) hist->summ, (double) hist->delta));
 }
 
 static void
diff --git a/src/SquidMath.cc b/src/SquidMath.cc
new file mode 100644 (file)
index 0000000..946d3f1
--- /dev/null
@@ -0,0 +1,32 @@
+#include "config.h"
+#include "SquidMath.h"
+
+int
+Math::intPercent(const int a, const int b)
+{
+    return b ? ((int) (100.0 * a / b + 0.5)) : 0;
+}
+
+double
+Math::doublePercent(const double a, const double b)
+{
+    return b ? (100.0 * a / b) : 0.0;
+}
+
+double
+Math::doubleAverage(const double cur, const double newD, int N, const int max)
+{
+    if (N > max)
+        N = max;
+
+    return (cur * (N - 1.0) + newD) / N;
+}
+
+int
+Math::intAverage(const int cur, const int newI, int n, const int max)
+{
+    if (n > max)
+        n = max;
+
+    return (cur * (n - 1) + newI) / n;
+}
diff --git a/src/SquidMath.h b/src/SquidMath.h
new file mode 100644 (file)
index 0000000..f8db458
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _SQUID_SRC_SQUIDMATH_H
+#define _SQUID_SRC_SQUIDMATH_H
+
+/* Math functions we define locally for Squid */
+namespace Math {
+
+extern int intPercent(int a, int b);
+extern double doublePercent(double, double);
+extern int intAverage(int, int, int, int);
+extern double doubleAverage(double, double, int, int);
+
+}; // namespace Math
+
+#endif /* _SQUID_SRC_SQUIDMATH_H */
index 855ae0db68d6de45cdac1deff4b554fb247784f6..ef08ee34a07846898d2c5d5800644845c0f45baa 100644 (file)
@@ -36,6 +36,7 @@
 #include "event.h"
 #include "CacheManager.h"
 #include "ClientInfo.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "Store.h"
 
@@ -261,15 +262,10 @@ clientdbDump(StoreEntry * sentry)
             if (LOG_UDP_HIT == l)
                 icp_hits += c->Icp.result_hist[l];
 
-            storeAppendPrintf(sentry,
-                              "        %-20.20s %7d %3d%%\n",
-                              log_tags[l],
-                              c->Icp.result_hist[l],
-                              percent(c->Icp.result_hist[l], c->Icp.n_requests));
+            storeAppendPrintf(sentry, "        %-20.20s %7d %3d%%\n",log_tags[l], c->Icp.result_hist[l], Math::intPercent(c->Icp.result_hist[l], c->Icp.n_requests));
         }
 
-        storeAppendPrintf(sentry, "    HTTP Requests %d\n",
-                          c->Http.n_requests);
+        storeAppendPrintf(sentry, "    HTTP Requests %d\n", c->Http.n_requests);
 
         for (l = LOG_TAG_NONE; l < LOG_TYPE_MAX; ++l) {
             if (c->Http.result_hist[l] == 0)
@@ -284,7 +280,7 @@ clientdbDump(StoreEntry * sentry)
                               "        %-20.20s %7d %3d%%\n",
                               log_tags[l],
                               c->Http.result_hist[l],
-                              percent(c->Http.result_hist[l], c->Http.n_requests));
+                              Math::intPercent(c->Http.result_hist[l], c->Http.n_requests));
         }
 
         storeAppendPrintf(sentry, "\n");
@@ -292,9 +288,9 @@ clientdbDump(StoreEntry * sentry)
 
     storeAppendPrintf(sentry, "TOTALS\n");
     storeAppendPrintf(sentry, "ICP : %d Queries, %d Hits (%3d%%)\n",
-                      icp_total, icp_hits, percent(icp_hits, icp_total));
+                      icp_total, icp_hits, Math::intPercent(icp_hits, icp_total));
     storeAppendPrintf(sentry, "HTTP: %d Requests, %d Hits (%3d%%)\n",
-                      http_total, http_hits, percent(http_hits, http_total));
+                      http_total, http_hits, Math::intPercent(http_hits, http_total));
 }
 
 static void
index 0be41406c835153647162f640eb41a5967b6dd0b..959334d0a1cbd5cfb0efe1b064991fe04b1c8154 100644 (file)
@@ -49,6 +49,7 @@
 #include "StoreFScoss.h"
 #include "Parsing.h"
 #include "swap_log_op.h"
+//#include "SquidMath.h"
 
 #define STORE_META_BUFSZ 4096
 
@@ -1005,7 +1006,7 @@ CossSwapDir::statfs(StoreEntry & sentry) const
     /* is this applicable? I Hope not .. */
     storeAppendPrintf(sentry, "Filemap bits in use: %d of %d (%d%%)\n",
                       SD->map->n_files_in_map, SD->map->max_n_files,
-                      percent(SD->map->n_files_in_map, SD->map->max_n_files));
+                      Math::intPercent(SD->map->n_files_in_map, SD->map->max_n_files));
 #endif
 
     //    storeAppendPrintf(&sentry, "Pending operations: %d out of %d\n", io->aq.aq_numpending, MAX_ASYNCOP);
index 9cd380cdb66b697ae710870fadd42e920ac1c4ef..e4f95ebde27cb261fa829d1cddfe6de301220ad7 100644 (file)
@@ -42,6 +42,7 @@
 #include "DiskIO/DiskIOStrategy.h"
 #include "DiskIO/DiskIOModule.h"
 #include "Parsing.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "SwapDir.h"
 #include "swap_log_op.h"
@@ -320,18 +321,18 @@ UFSSwapDir::statfs(StoreEntry & sentry) const
                       100.0 * cur_size / max_size);
     storeAppendPrintf(&sentry, "Filemap bits in use: %d of %d (%d%%)\n",
                       map->n_files_in_map, map->max_n_files,
-                      percent(map->n_files_in_map, map->max_n_files));
+                      Math::intPercent(map->n_files_in_map, map->max_n_files));
     x = storeDirGetUFSStats(path, &totl_kb, &free_kb, &totl_in, &free_in);
 
     if (0 == x) {
         storeAppendPrintf(&sentry, "Filesystem Space in use: %d/%d KB (%d%%)\n",
                           totl_kb - free_kb,
                           totl_kb,
-                          percent(totl_kb - free_kb, totl_kb));
+                          Math::intPercent(totl_kb - free_kb, totl_kb));
         storeAppendPrintf(&sentry, "Filesystem Inodes in use: %d/%d (%d%%)\n",
                           totl_in - free_in,
                           totl_in,
-                          percent(totl_in - free_in, totl_in));
+                          Math::intPercent(totl_in - free_in, totl_in));
     }
 
     storeAppendPrintf(&sentry, "Flags:");
index 244100c2bf42100f055e4ac8e2e5beeed7fdc7d0..cda398af78841ea6b2e4771fe31039d5cddcbb5a 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "squid.h"
 #include "helper.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "Store.h"
 #include "comm.h"
@@ -920,10 +921,7 @@ helperHandleRead(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, voi
 
             srv->dispatch_time = r->dispatch_time;
 
-            hlp->stats.avg_svc_time =
-                intAverage(hlp->stats.avg_svc_time,
-                           tvSubMsec(r->dispatch_time, current_time),
-                           hlp->stats.replies, REDIRECT_AV_FACTOR);
+            hlp->stats.avg_svc_time = Math::intAverage(hlp->stats.avg_svc_time, tvSubMsec(r->dispatch_time, current_time), hlp->stats.replies, REDIRECT_AV_FACTOR);
 
             helperRequestFree(r);
         } else {
@@ -1020,9 +1018,9 @@ helperStatefulHandleRead(int fd, char *buf, size_t len, comm_err_t flag, int xer
         hlp->stats.replies++;
         srv->answer_time = current_time;
         hlp->stats.avg_svc_time =
-            intAverage(hlp->stats.avg_svc_time,
-                       tvSubMsec(srv->dispatch_time, current_time),
-                       hlp->stats.replies, REDIRECT_AV_FACTOR);
+            Math::intAverage(hlp->stats.avg_svc_time,
+                             tvSubMsec(srv->dispatch_time, current_time),
+                             hlp->stats.replies, REDIRECT_AV_FACTOR);
 
         if (called)
             helperStatefulServerDone(srv);
index 06e5f84391af7a50e27ed484c51cd61766a7e1ec..1868b00d9c3818e4f56e14b6cdb27637da930a5e 100644 (file)
@@ -41,6 +41,7 @@
 #include "MemObject.h"
 #include "PeerDigest.h"
 #include "PeerSelectState.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "Store.h"
 #include "icmp/net_db.h"
@@ -920,8 +921,7 @@ neighborUpdateRtt(peer * p, MemObject * mem)
     if (p->options.weighted_roundrobin)
         rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
 
-    p->stats.rtt = intAverage(p->stats.rtt, rtt,
-                              p->stats.pings_acked, rtt_av_factor);
+    p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, p->stats.pings_acked, rtt_av_factor);
 }
 
 #if USE_HTCP
@@ -1478,10 +1478,7 @@ peerCountMcastPeersDone(void *data)
     if (cbdataReferenceValid(psstate->callback_data)) {
         peer *p = (peer *)psstate->callback_data;
         p->mcast.flags.counting = 0;
-        p->mcast.avg_n_members = doubleAverage(p->mcast.avg_n_members,
-                                               (double) psstate->ping.n_recv,
-                                               ++p->mcast.n_times_counted,
-                                               10);
+        p->mcast.avg_n_members = Math::doubleAverage(p->mcast.avg_n_members, (double) psstate->ping.n_recv, ++p->mcast.n_times_counted, 10);
         debugs(15, 1, "Group " << p->host  << ": " << psstate->ping.n_recv  <<
                " replies, "<< std::setw(4)<< std::setprecision(2) <<
                p->mcast.avg_n_members <<" average, RTT " << p->stats.rtt);
@@ -1516,7 +1513,7 @@ peerCountHandleIcpReply(peer * p, peer_t type, protocol_t proto, void *hdrnotuse
     if (p->options.weighted_roundrobin)
         rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
 
-    p->stats.rtt = intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
+    p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
 }
 
 static void
@@ -1689,12 +1686,10 @@ dump_peers(StoreEntry * sentry, peer * peers)
 
             storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n",
                               e->stats.pings_acked,
-                              percent(e->stats.pings_acked, e->stats.pings_sent));
+                              Math::intPercent(e->stats.pings_acked, e->stats.pings_sent));
         }
 
-        storeAppendPrintf(sentry, "IGNORED    : %8d %3d%%\n",
-                          e->stats.ignored_replies,
-                          percent(e->stats.ignored_replies, e->stats.pings_acked));
+        storeAppendPrintf(sentry, "IGNORED    : %8d %3d%%\n", e->stats.ignored_replies, Math::intPercent(e->stats.ignored_replies, e->stats.pings_acked));
 
         if (!e->options.no_query) {
             storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n");
@@ -1703,10 +1698,10 @@ dump_peers(StoreEntry * sentry, peer * peers)
             if (e->options.htcp) {
                 storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n",
                                   e->htcp.counts[0],
-                                  percent(e->htcp.counts[0], e->stats.pings_acked));
+                                  Math::intPercent(e->htcp.counts[0], e->stats.pings_acked));
                 storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n",
                                   e->htcp.counts[1],
-                                  percent(e->htcp.counts[1], e->stats.pings_acked));
+                                  Math::intPercent(e->htcp.counts[1], e->stats.pings_acked));
             } else {
 #endif
 
@@ -1717,7 +1712,7 @@ dump_peers(StoreEntry * sentry, peer * peers)
                     storeAppendPrintf(sentry, "    %12.12s : %8d %3d%%\n",
                                       icp_opcode_str[op],
                                       e->icp.counts[op],
-                                      percent(e->icp.counts[op], e->stats.pings_acked));
+                                      Math::intPercent(e->icp.counts[op], e->stats.pings_acked));
                 }
 
 #if USE_HTCP
@@ -1744,8 +1739,7 @@ dump_peers(StoreEntry * sentry, peer * peers)
             storeAppendPrintf(sentry, "\n");
         }
 
-        storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n",
-                          percent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
+        storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n", Math::intPercent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
     }
 }
 
index 25149234e652e9b5971070b054c38844e615b7c0..d7e7642e9cf13d1733b17e2071f35c04cebba7f8 100644 (file)
@@ -571,8 +571,6 @@ SQUIDCEXTERN void no_suid(void);
 SQUIDCEXTERN void writePidFile(void);
 SQUIDCEXTERN void setSocketShutdownLifetimes(int);
 SQUIDCEXTERN void setMaxFD(void);
-SQUIDCEXTERN int percent(int, int);
-SQUIDCEXTERN double dpercent(double, double);
 SQUIDCEXTERN void squid_signal(int sig, SIGHDLR *, int flags);
 SQUIDCEXTERN pid_t readPidFile(void);
 SQUIDCEXTERN void keepCapabilities(void);
@@ -580,8 +578,6 @@ SQUIDCEXTERN void keepCapabilities(void);
 /* AYJ debugs function to show locations being reset with memset() */
 SQUIDCEXTERN void *xmemset(void *dst, int, size_t);
 
-SQUIDCEXTERN int intAverage(int, int, int, int);
-SQUIDCEXTERN double doubleAverage(double, double, int, int);
 SQUIDCEXTERN void debug_trap(const char *);
 SQUIDCEXTERN void logsFlush(void);
 SQUIDCEXTERN const char *checkNullString(const char *p);
index db394fdff316ce88666daaf640aa5faf9da35a52..7b2162786d1904950d19d661946c9ac229250042 100644 (file)
@@ -36,6 +36,7 @@
 #include "cache_snmp.h"
 #include "Store.h"
 #include "mem_node.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 
 /************************************************************************
@@ -371,7 +372,7 @@ snmp_prfSysFn(variable_list * Var, snint * ErrP)
     case PERF_SYS_CPUUSAGE:
         squid_getrusage(&rusage);
         Answer = snmp_var_new_integer(Var->name, Var->name_length,
-                                      (snint) dpercent(rusage_cputime(&rusage), tvSubDsec(squid_start, current_time)),
+                                      (snint) Math::doublePercent(rusage_cputime(&rusage), tvSubDsec(squid_start, current_time)),
                                       ASN_INTEGER);
         break;
 
index 693032fea3797f5fa24e7befae7202d11bfcd41c..030d8d0120824b0420a8d1a76b86a0fad89856ab 100644 (file)
@@ -48,6 +48,7 @@
 #include "client_side_request.h"
 #include "client_side.h"
 #include "MemBuf.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 
 /* these are included because they expose stats calls */
@@ -199,7 +200,7 @@ stat_io_get(StoreEntry * sentry)
                           i ? (1 << (i - 1)) + 1 : 1,
                           1 << i,
                           IOStats.Http.read_hist[i],
-                          percent(IOStats.Http.read_hist[i], IOStats.Http.reads));
+                          Math::intPercent(IOStats.Http.read_hist[i], IOStats.Http.reads));
     }
 
     storeAppendPrintf(sentry, "\n");
@@ -212,7 +213,7 @@ stat_io_get(StoreEntry * sentry)
                           i ? (1 << (i - 1)) + 1 : 1,
                           1 << i,
                           IOStats.Ftp.read_hist[i],
-                          percent(IOStats.Ftp.read_hist[i], IOStats.Ftp.reads));
+                          Math::intPercent(IOStats.Ftp.read_hist[i], IOStats.Ftp.reads));
     }
 
     storeAppendPrintf(sentry, "\n");
@@ -225,7 +226,7 @@ stat_io_get(StoreEntry * sentry)
                           i ? (1 << (i - 1)) + 1 : 1,
                           1 << i,
                           IOStats.Gopher.read_hist[i],
-                          percent(IOStats.Gopher.read_hist[i], IOStats.Gopher.reads));
+                          Math::intPercent(IOStats.Gopher.read_hist[i], IOStats.Gopher.reads));
     }
 
     storeAppendPrintf(sentry, "\n");
@@ -539,16 +540,16 @@ info_get(StoreEntry * sentry)
                       store_swap_size);
 
     storeAppendPrintf(sentry, "\tStorage Swap capacity:\t%4.1f%% used, %4.1f%% free\n",
-                      dpercent(store_swap_size, Store::Root().maxSize()),
-                      dpercent((Store::Root().maxSize() - store_swap_size), Store::Root().maxSize()));
+                      Math::doublePercent(store_swap_size, Store::Root().maxSize()),
+                      Math::doublePercent((Store::Root().maxSize() - store_swap_size), Store::Root().maxSize()));
 
 
     storeAppendPrintf(sentry, "\tStorage Mem size:\t%lu KB\n",
                       (unsigned long)mem_node::StoreMemSize() >> 10);
 
     storeAppendPrintf(sentry, "\tStorage Mem capacity:\t%4.1f%% used, %4.1f%% free\n",
-                      dpercent(mem_node::InUseCount(), store_pages_max),
-                      dpercent((store_pages_max - mem_node::InUseCount()), store_pages_max));
+                      Math::doublePercent(mem_node::InUseCount(), store_pages_max),
+                      Math::doublePercent((store_pages_max - mem_node::InUseCount()), store_pages_max));
 
     storeAppendPrintf(sentry, "\tMean Object Size:\t%0.2f KB\n",
                       n_disk_objects ? (double) store_swap_size / n_disk_objects : 0.0);
@@ -597,7 +598,7 @@ info_get(StoreEntry * sentry)
     storeAppendPrintf(sentry, "\tCPU Time:\t%.3f seconds\n", cputime);
 
     storeAppendPrintf(sentry, "\tCPU Usage:\t%.2f%%\n",
-                      dpercent(cputime, runtime));
+                      Math::doublePercent(cputime, runtime));
 
     storeAppendPrintf(sentry, "\tCPU Usage, 5 minute avg:\t%.2f%%\n",
                       statCPUUsage(5));
@@ -628,7 +629,7 @@ info_get(StoreEntry * sentry)
                       ms.bytes_total >> 10);
 
     storeAppendPrintf(sentry, "\tTotal free:            %6d KB %d%%\n",
-                      ms.bytes_free >> 10, percent(ms.bytes_free, ms.bytes_total));
+                      ms.bytes_free >> 10, Math::intPercent(ms.bytes_free, ms.bytes_total));
 
 #elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
@@ -657,12 +658,12 @@ info_get(StoreEntry * sentry)
     t = mp.uordblks + mp.usmblks + mp.hblkhd;
 
     storeAppendPrintf(sentry, "\tTotal in use:          %6d KB %d%%\n",
-                      t >> 10, percent(t, mp.arena + mp.hblkhd));
+                      t >> 10, Math::intPercent(t, mp.arena + mp.hblkhd));
 
     t = mp.fsmblks + mp.fordblks;
 
     storeAppendPrintf(sentry, "\tTotal free:            %6d KB %d%%\n",
-                      t >> 10, percent(t, mp.arena + mp.hblkhd));
+                      t >> 10, Math::intPercent(t, mp.arena + mp.hblkhd));
 
     t = mp.arena + mp.hblkhd;
 
@@ -695,7 +696,7 @@ info_get(StoreEntry * sentry)
 #if !(HAVE_MSTATS && HAVE_GNUMALLOC_H) && HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
     storeAppendPrintf(sentry, "\tTotal accounted:       %6d KB %3d%%\n",
-                      statMemoryAccounted() >> 10, percent(statMemoryAccounted(), t));
+                      statMemoryAccounted() >> 10, Math::intPercent(statMemoryAccounted(), t));
 
 #else
 
@@ -710,10 +711,10 @@ info_get(StoreEntry * sentry)
 
         storeAppendPrintf(sentry, "\tmemPool accounted:     %6d KB %3d%%\n",
                           (int) mp_stats.TheMeter->alloc.level >> 10,
-                          percent(mp_stats.TheMeter->alloc.level, t));
+                          Math::intPercent(mp_stats.TheMeter->alloc.level, t));
         storeAppendPrintf(sentry, "\tmemPool unaccounted:   %6d KB %3d%%\n",
                           (t - (int) mp_stats.TheMeter->alloc.level) >> 10,
-                          percent((t - mp_stats.TheMeter->alloc.level), t));
+                          Math::intPercent((t - mp_stats.TheMeter->alloc.level), t));
 #endif
 
         storeAppendPrintf(sentry, "\tmemPoolAlloc calls: %9.0f\n",
@@ -1004,7 +1005,7 @@ statAvgDump(StoreEntry * sentry, int minutes, int hours)
 
     storeAppendPrintf(sentry, "cpu_time = %f seconds\n", ct);
     storeAppendPrintf(sentry, "wall_time = %f seconds\n", dt);
-    storeAppendPrintf(sentry, "cpu_usage = %f%%\n", dpercent(ct, dt));
+    storeAppendPrintf(sentry, "cpu_usage = %f%%\n", Math::doublePercent(ct, dt));
 }
 
 static void
@@ -1531,7 +1532,7 @@ static double
 statCPUUsage(int minutes)
 {
     assert(minutes < N_COUNT_HIST);
-    return dpercent(CountHist[0].cputime - CountHist[minutes].cputime,
+    return Math::doublePercent(CountHist[0].cputime - CountHist[minutes].cputime,
                     tvSubDsec(CountHist[minutes].timestamp, CountHist[0].timestamp));
 }
 
@@ -1539,7 +1540,7 @@ extern double
 statRequestHitRatio(int minutes)
 {
     assert(minutes < N_COUNT_HIST);
-    return dpercent(CountHist[0].client_http.hits -
+    return Math::doublePercent(CountHist[0].client_http.hits -
                     CountHist[minutes].client_http.hits,
                     CountHist[0].client_http.requests -
                     CountHist[minutes].client_http.requests);
@@ -1549,7 +1550,7 @@ extern double
 statRequestHitMemoryRatio(int minutes)
 {
     assert(minutes < N_COUNT_HIST);
-    return dpercent(CountHist[0].client_http.mem_hits -
+    return Math::doublePercent(CountHist[0].client_http.mem_hits -
                     CountHist[minutes].client_http.mem_hits,
                     CountHist[0].client_http.hits -
                     CountHist[minutes].client_http.hits);
@@ -1559,7 +1560,7 @@ extern double
 statRequestHitDiskRatio(int minutes)
 {
     assert(minutes < N_COUNT_HIST);
-    return dpercent(CountHist[0].client_http.disk_hits -
+    return Math::doublePercent(CountHist[0].client_http.disk_hits -
                     CountHist[minutes].client_http.disk_hits,
                     CountHist[0].client_http.hits -
                     CountHist[minutes].client_http.hits);
@@ -1596,9 +1597,9 @@ statByteHitRatio(int minutes)
 #endif
 
     if (c > s)
-        return dpercent(c - s, c);
+        return Math::doublePercent(c - s, c);
     else
-        return (-1.0 * dpercent(s - c, c));
+        return (-1.0 * Math::doublePercent(s - c, c));
 }
 
 static void
index 18d641ed563563b5e27454ad4b08646c286aa0b7..f95cab81f218fd1670292ca0fe759974ae39b5e1 100644 (file)
@@ -36,6 +36,7 @@
 #include "squid.h"
 #include "Store.h"
 #include "MemObject.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "SwapDir.h"
 #include "swap_log_op.h"
@@ -365,8 +366,8 @@ StoreController::stat(StoreEntry &output) const
     storeAppendPrintf(&output, "Current Store Swap Size: %8lu KB\n",
                       store_swap_size);
     storeAppendPrintf(&output, "Current Capacity       : %d%% used, %d%% free\n",
-                      percent((int) store_swap_size, (int) maxSize()),
-                      percent((int) (maxSize() - store_swap_size), (int) maxSize()));
+                      Math::intPercent((int) store_swap_size, (int) maxSize()),
+                      Math::intPercent((int) (maxSize() - store_swap_size), (int) maxSize()));
     /* FIXME Here we should output memory statistics */
 
     /* now the swapDir */
index 8cb252a09ce20b0a5eaafc43d06cd566a3aa24a0..91c2110e8162c8547ffd69a20672ed0d450e9e4d 100644 (file)
@@ -38,6 +38,7 @@
 #include "fde.h"
 #include "MemBuf.h"
 #include "wordlist.h"
+#include "SquidMath.h"
 #include "SquidTime.h"
 #include "ip/IpIntercept.h"
 
@@ -161,7 +162,7 @@ dumpMallocStats(void)
             (int) (ms.bytes_total >> 10));
     fprintf(debug_log, "\tTotal free:            %6d KB %d%%\n",
             (int) (ms.bytes_free >> 10),
-            percent(ms.bytes_free, ms.bytes_total));
+            Math::intPercent(ms.bytes_free, ms.bytes_total));
 #elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
     struct mallinfo mp;
@@ -195,12 +196,12 @@ dumpMallocStats(void)
     t = mp.uordblks + mp.usmblks + mp.hblkhd;
 
     fprintf(debug_log, "\tTotal in use:          %6d KB %d%%\n",
-            t >> 10, percent(t, mp.arena));
+            t >> 10, Math::intPercent(t, mp.arena));
 
     t = mp.fsmblks + mp.fordblks;
 
     fprintf(debug_log, "\tTotal free:            %6d KB %d%%\n",
-            t >> 10, percent(t, mp.arena));
+            t >> 10, Math::intPercent(t, mp.arena));
 
 #if HAVE_STRUCT_MALLINFO_MXFAST
 
@@ -946,18 +947,6 @@ setMaxFD(void)
 #endif /* RLIMIT_VMEM */
 }
 
-int
-percent(int a, int b)
-{
-    return b ? ((int) (100.0 * a / b + 0.5)) : 0;
-}
-
-double
-dpercent(double a, double b)
-{
-    return b ? (100.0 * a / b) : 0.0;
-}
-
 void
 squid_signal(int sig, SIGHDLR * func, int flags)
 {
@@ -1015,24 +1004,6 @@ squid_signal(int sig, SIGHDLR * func, int flags)
 #endif
 }
 
-double
-doubleAverage(double cur, double newD, int N, int max)
-{
-    if (N > max)
-        N = max;
-
-    return (cur * (N - 1.0) + newD) / N;
-}
-
-int
-intAverage(int cur, int newI, int n, int max)
-{
-    if (n > max)
-        n = max;
-
-    return (cur * (n - 1) + newI) / n;
-}
-
 void
 logsFlush(void)
 {