From a0864754aa8911e1d9caffaf0d290fbc023dd86e Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sat, 29 Aug 2015 17:26:47 -0700 Subject: [PATCH] SourceLayout: shuffle kb_t to ByteCounter in libbase Also, move kb_incr() logic to ByteCounter operator += There are no logic changes in this patch. --- src/ClientInfo.h | 8 +++++--- src/PeerDigest.h | 3 +-- src/StatCounters.h | 29 +++++++++++++++-------------- src/base/ByteCounter.h | 31 +++++++++++++++++++++++++++++++ src/base/Makefile.am | 1 + src/client_db.cc | 8 ++++---- src/client_side.cc | 6 +++--- src/clients/Client.cc | 2 +- src/clients/FtpClient.cc | 14 +++++++------- src/gopher.cc | 8 ++++---- src/http.cc | 10 +++++----- src/icp_v2.cc | 12 ++++++------ src/peer_digest.cc | 13 +++++-------- src/servers/FtpServer.cc | 2 +- src/tests/stub_tools.cc | 1 - src/tools.cc | 8 -------- src/tools.h | 1 - src/tunnel.cc | 16 ++++++++-------- src/typedefs.h | 5 ----- src/whois.cc | 4 ++-- 20 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 src/base/ByteCounter.h diff --git a/src/ClientInfo.h b/src/ClientInfo.h index 365d36eb03..c84f7d3f78 100644 --- a/src/ClientInfo.h +++ b/src/ClientInfo.h @@ -9,12 +9,14 @@ #ifndef SQUID__SRC_CLIENTINFO_H #define SQUID__SRC_CLIENTINFO_H +#include "base/ByteCounter.h" #include "cbdata.h" #include "enums.h" #include "hash.h" #include "ip/Address.h" #include "LogTags.h" #include "typedefs.h" + #include #if USE_DELAY_POOLS @@ -31,9 +33,9 @@ public: struct { int result_hist[LOG_TYPE_MAX]; int n_requests; - kb_t kbytes_in; - kb_t kbytes_out; - kb_t hit_kbytes_out; + ByteCounter kbytes_in; + ByteCounter kbytes_out; + ByteCounter hit_kbytes_out; } Http, Icp; struct { diff --git a/src/PeerDigest.h b/src/PeerDigest.h index 44e7e41757..ab37dce501 100644 --- a/src/PeerDigest.h +++ b/src/PeerDigest.h @@ -12,7 +12,6 @@ #if USE_CACHE_DIGESTS #include "cbdata.h" -/* for CacheDigestGuessStats */ #include "StatCounters.h" class Version @@ -106,7 +105,7 @@ public: struct { int msgs; - kb_t kbytes; + ByteCounter kbytes; } sent, recv; } stats; }; diff --git a/src/StatCounters.h b/src/StatCounters.h index 62a5443a2f..6d5f86fc94 100644 --- a/src/StatCounters.h +++ b/src/StatCounters.h @@ -9,6 +9,7 @@ #ifndef STATCOUNTERS_H_ #define STATCOUNTERS_H_ +#include "base/ByteCounter.h" #include "StatHist.h" #if USE_CACHE_DIGESTS @@ -39,9 +40,9 @@ public: int mem_hits; int disk_hits; int errors; - kb_t kbytes_in; - kb_t kbytes_out; - kb_t hit_kbytes_out; + ByteCounter kbytes_in; + ByteCounter kbytes_out; + ByteCounter hit_kbytes_out; StatHist missSvcTime; StatHist nearMissSvcTime; StatHist nearHitSvcTime; @@ -54,8 +55,8 @@ public: struct { int requests; int errors; - kb_t kbytes_in; - kb_t kbytes_out; + ByteCounter kbytes_in; + ByteCounter kbytes_out; } all , http, ftp, other; } server; @@ -70,12 +71,12 @@ public: int hits_recv; int replies_queued; int replies_dropped; - kb_t kbytes_sent; - kb_t q_kbytes_sent; - kb_t r_kbytes_sent; - kb_t kbytes_recv; - kb_t q_kbytes_recv; - kb_t r_kbytes_recv; + ByteCounter kbytes_sent; + ByteCounter q_kbytes_sent; + ByteCounter r_kbytes_sent; + ByteCounter kbytes_recv; + ByteCounter q_kbytes_recv; + ByteCounter r_kbytes_recv; StatHist querySvcTime; StatHist replySvcTime; int query_timeouts; @@ -97,9 +98,9 @@ public: struct { int times_used; - kb_t kbytes_sent; - kb_t kbytes_recv; - kb_t memory; + ByteCounter kbytes_sent; + ByteCounter kbytes_recv; + ByteCounter memory; int msgs_sent; int msgs_recv; #if USE_CACHE_DIGESTS diff --git a/src/base/ByteCounter.h b/src/base/ByteCounter.h new file mode 100644 index 0000000000..44362cc032 --- /dev/null +++ b/src/base/ByteCounter.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#ifndef SQUID_SRC_BYTECOUNTER_H +#define SQUID_SRC_BYTECOUNTER_H + +/// counter for accumulating byte values +class ByteCounter +{ +public: + ByteCounter() : bytes(0), kb(0) {} + + ByteCounter &operator +=(size_t v) { + bytes += v; + kb += (bytes >> 10); + bytes &= 0x3FF; + return *this; + } + +public: + size_t bytes; + size_t kb; +}; + +#endif /* SQUID_SRC_BYTECOUNTER_H */ + diff --git a/src/base/Makefile.am b/src/base/Makefile.am index cd03165a17..4a43969a41 100644 --- a/src/base/Makefile.am +++ b/src/base/Makefile.am @@ -19,6 +19,7 @@ libbase_la_SOURCES = \ AsyncJobCalls.h \ AsyncCallQueue.cc \ AsyncCallQueue.h \ + ByteCounter.h \ CbcPointer.h \ CbDataList.h \ CharacterSet.h \ diff --git a/src/client_db.cc b/src/client_db.cc index 87817b3661..af4fcf7e8e 100644 --- a/src/client_db.cc +++ b/src/client_db.cc @@ -158,17 +158,17 @@ clientdbUpdate(const Ip::Address &addr, const LogTags <ype, AnyP::ProtocolType if (p == AnyP::PROTO_HTTP) { ++ c->Http.n_requests; ++ c->Http.result_hist[ltype.oldType]; - kb_incr(&c->Http.kbytes_out, size); + c->Http.kbytes_out += size; if (ltype.isTcpHit()) - kb_incr(&c->Http.hit_kbytes_out, size); + c->Http.hit_kbytes_out += size; } else if (p == AnyP::PROTO_ICP) { ++ c->Icp.n_requests; ++ c->Icp.result_hist[ltype.oldType]; - kb_incr(&c->Icp.kbytes_out, size); + c->Icp.kbytes_out += size; if (LOG_UDP_HIT == ltype.oldType) - kb_incr(&c->Icp.hit_kbytes_out, size); + c->Icp.hit_kbytes_out += size; } c->last_seen = squid_curtime; diff --git a/src/client_side.cc b/src/client_side.cc index 84ef380593..a0b054306e 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1611,10 +1611,10 @@ clientUpdateSocketStats(const LogTags &logType, size_t size) if (size == 0) return; - kb_incr(&statCounter.client_http.kbytes_out, size); + statCounter.client_http.kbytes_out += size; if (logType.isTcpHit()) - kb_incr(&statCounter.client_http.hit_kbytes_out, size); + statCounter.client_http.hit_kbytes_out += size; } /** @@ -3122,7 +3122,7 @@ ConnStateData::clientReadRequest(const CommIoCbParams &io) return; case Comm::OK: - kb_incr(&(statCounter.client_http.kbytes_in), rd.size); + statCounter.client_http.kbytes_in += rd.size; if (!receivedFirstByte_) receivedFirstByte(); // may comm_close or setReplyToError diff --git a/src/clients/Client.cc b/src/clients/Client.cc index abfcff2c99..3a2c234491 100644 --- a/src/clients/Client.cc +++ b/src/clients/Client.cc @@ -349,7 +349,7 @@ Client::sentRequestBody(const CommIoCbParams &io) if (io.size > 0) { fd_bytes(io.fd, io.size, FD_WRITE); - kb_incr(&(statCounter.server.all.kbytes_out), io.size); + statCounter.server.all.kbytes_out += io.size; // kids should increment their counters } diff --git a/src/clients/FtpClient.cc b/src/clients/FtpClient.cc index b496f75825..7720ae1b1f 100644 --- a/src/clients/FtpClient.cc +++ b/src/clients/FtpClient.cc @@ -331,8 +331,8 @@ Ftp::Client::readControlReply(const CommIoCbParams &io) debugs(9, 3, "FD " << io.fd << ", Read " << io.size << " bytes"); if (io.size > 0) { - kb_incr(&(statCounter.server.all.kbytes_in), io.size); - kb_incr(&(statCounter.server.ftp.kbytes_in), io.size); + statCounter.server.all.kbytes_in += io.size; + statCounter.server.ftp.kbytes_in += io.size; } if (io.flag == Comm::ERR_CLOSING) @@ -808,8 +808,8 @@ Ftp::Client::writeCommandCallback(const CommIoCbParams &io) if (io.size > 0) { fd_bytes(io.fd, io.size, FD_WRITE); - kb_incr(&(statCounter.server.all.kbytes_out), io.size); - kb_incr(&(statCounter.server.ftp.kbytes_out), io.size); + statCounter.server.all.kbytes_out += io.size; + statCounter.server.ftp.kbytes_out += io.size; } if (io.flag == Comm::ERR_CLOSING) @@ -894,8 +894,8 @@ Ftp::Client::dataRead(const CommIoCbParams &io) debugs(9, 3, "FD " << io.fd << " Read " << io.size << " bytes"); if (io.size > 0) { - kb_incr(&(statCounter.server.all.kbytes_in), io.size); - kb_incr(&(statCounter.server.ftp.kbytes_in), io.size); + statCounter.server.all.kbytes_in += io.size; + statCounter.server.ftp.kbytes_in += io.size; } if (io.flag == Comm::ERR_CLOSING) @@ -1020,7 +1020,7 @@ void Ftp::Client::sentRequestBody(const CommIoCbParams &io) { if (io.size > 0) - kb_incr(&(statCounter.server.ftp.kbytes_out), io.size); + statCounter.server.ftp.kbytes_out += io.size; ::Client::sentRequestBody(io); } diff --git a/src/gopher.cc b/src/gopher.cc index a482624ec0..3dcaf9cb8a 100644 --- a/src/gopher.cc +++ b/src/gopher.cc @@ -752,8 +752,8 @@ gopherReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm delayId.bytesIn(len); #endif - kb_incr(&(statCounter.server.all.kbytes_in), len); - kb_incr(&(statCounter.server.other.kbytes_in), len); + statCounter.server.all.kbytes_in += len; + statCounter.server.other.kbytes_in += len; } debugs(10, 5, HERE << conn << " read len=" << len); @@ -826,8 +826,8 @@ gopherSendComplete(const Comm::ConnectionPointer &conn, char *buf, size_t size, if (size > 0) { fd_bytes(conn->fd, size, FD_WRITE); - kb_incr(&(statCounter.server.all.kbytes_out), size); - kb_incr(&(statCounter.server.other.kbytes_out), size); + statCounter.server.all.kbytes_out += size; + statCounter.server.other.kbytes_out += size; } if (errflag) { diff --git a/src/http.cc b/src/http.cc index 20c5eb5194..33ea69a334 100644 --- a/src/http.cc +++ b/src/http.cc @@ -1204,8 +1204,8 @@ HttpStateData::readReply(const CommIoCbParams &io) delayId.bytesIn(rd.size); #endif - kb_incr(&(statCounter.server.all.kbytes_in), rd.size); - kb_incr(&(statCounter.server.http.kbytes_in), rd.size); + statCounter.server.all.kbytes_in += rd.size; + statCounter.server.http.kbytes_in += rd.size; ++ IOStats.Http.reads; int bin = 0; @@ -1593,8 +1593,8 @@ HttpStateData::wroteLast(const CommIoCbParams &io) if (io.size > 0) { fd_bytes(io.fd, io.size, FD_WRITE); - kb_incr(&(statCounter.server.all.kbytes_out), io.size); - kb_incr(&(statCounter.server.http.kbytes_out), io.size); + statCounter.server.all.kbytes_out += io.size; + statCounter.server.http.kbytes_out += io.size; } if (io.flag == Comm::ERR_CLOSING) @@ -2458,7 +2458,7 @@ void HttpStateData::sentRequestBody(const CommIoCbParams &io) { if (io.size > 0) - kb_incr(&statCounter.server.http.kbytes_out, io.size); + statCounter.server.http.kbytes_out += io.size; Client::sentRequestBody(io); } diff --git a/src/icp_v2.cc b/src/icp_v2.cc index 4773c8fccd..27b3726ec2 100644 --- a/src/icp_v2.cc +++ b/src/icp_v2.cc @@ -777,14 +777,14 @@ icpCount(void *buf, int which, size_t len, int delay) if (SENT == which) { ++statCounter.icp.pkts_sent; - kb_incr(&statCounter.icp.kbytes_sent, len); + statCounter.icp.kbytes_sent += len; if (ICP_QUERY == icp->opcode) { ++statCounter.icp.queries_sent; - kb_incr(&statCounter.icp.q_kbytes_sent, len); + statCounter.icp.q_kbytes_sent += len; } else { ++statCounter.icp.replies_sent; - kb_incr(&statCounter.icp.r_kbytes_sent, len); + statCounter.icp.r_kbytes_sent += len; /* this is the sent-reply service time */ statCounter.icp.replySvcTime.count(delay); } @@ -793,14 +793,14 @@ icpCount(void *buf, int which, size_t len, int delay) ++statCounter.icp.hits_sent; } else if (RECV == which) { ++statCounter.icp.pkts_recv; - kb_incr(&statCounter.icp.kbytes_recv, len); + statCounter.icp.kbytes_recv += len; if (ICP_QUERY == icp->opcode) { ++statCounter.icp.queries_recv; - kb_incr(&statCounter.icp.q_kbytes_recv, len); + statCounter.icp.q_kbytes_recv += len; } else { ++statCounter.icp.replies_recv; - kb_incr(&statCounter.icp.r_kbytes_recv, len); + statCounter.icp.r_kbytes_recv += len; /* statCounter.icp.querySvcTime set in clientUpdateCounters */ } diff --git a/src/peer_digest.cc b/src/peer_digest.cc index 970b50e50a..80ae96be51 100644 --- a/src/peer_digest.cc +++ b/src/peer_digest.cc @@ -853,8 +853,8 @@ peerDigestPDFinish(DigestFetchState * fetch, int pcb_valid, int err) pd->times.received = squid_curtime; pd->times.req_delay = fetch->resp_time; - kb_incr(&pd->stats.sent.kbytes, (size_t) fetch->sent.bytes); - kb_incr(&pd->stats.recv.kbytes, (size_t) fetch->recv.bytes); + pd->stats.sent.kbytes += fetch->sent.bytes; + pd->stats.recv.kbytes += fetch->recv.bytes; pd->stats.sent.msgs += fetch->sent.msg; pd->stats.recv.msgs += fetch->recv.msg; @@ -902,12 +902,9 @@ peerDigestFetchFinish(DigestFetchState * fetch, int err) } /* update global stats */ - kb_incr(&statCounter.cd.kbytes_sent, (size_t) fetch->sent.bytes); - - kb_incr(&statCounter.cd.kbytes_recv, (size_t) fetch->recv.bytes); - + statCounter.cd.kbytes_sent += fetch->sent.bytes; + statCounter.cd.kbytes_recv += fetch->recv.bytes; statCounter.cd.msgs_sent += fetch->sent.msg; - statCounter.cd.msgs_recv += fetch->recv.msg; delete fetch; @@ -1028,7 +1025,7 @@ peerDigestSetCBlock(PeerDigest * pd, const char *buf) pd->cd = cacheDigestCreate(cblock.capacity, cblock.bits_per_entry); if (cblock.mask_size >= freed_size) - kb_incr(&statCounter.cd.memory, cblock.mask_size - freed_size); + statCounter.cd.memory += (cblock.mask_size - freed_size); } assert(pd->cd); diff --git a/src/servers/FtpServer.cc b/src/servers/FtpServer.cc index 8ce0bd1033..9c60b33775 100644 --- a/src/servers/FtpServer.cc +++ b/src/servers/FtpServer.cc @@ -172,7 +172,7 @@ Ftp::Server::readUploadData(const CommIoCbParams &io) if (io.flag == Comm::OK && bodyPipe != NULL) { if (io.size > 0) { - kb_incr(&(statCounter.client_http.kbytes_in), io.size); + statCounter.client_http.kbytes_in += io.size; char *const current_buf = uploadBuf + uploadAvailSize; if (io.buf != current_buf) diff --git a/src/tests/stub_tools.cc b/src/tests/stub_tools.cc index 3090f35f58..fff72e09ed 100644 --- a/src/tests/stub_tools.cc +++ b/src/tests/stub_tools.cc @@ -68,7 +68,6 @@ void setMaxFD(void) STUB void setSystemLimits(void) STUB void squid_signal(int sig, SIGHDLR * func, int flags) STUB void logsFlush(void) STUB -void kb_incr(kb_t * k, size_t v) STUB void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm) STUB void parseEtcHosts(void) STUB int getMyPort(void) STUB_RETVAL(0) diff --git a/src/tools.cc b/src/tools.cc index 84ed023156..5523e45531 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -960,14 +960,6 @@ logsFlush(void) fflush(debug_log); } -void -kb_incr(kb_t * k, size_t v) -{ - k->bytes += v; - k->kb += (k->bytes >> 10); - k->bytes &= 0x3FF; -} - void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm) { diff --git a/src/tools.h b/src/tools.h index afd055c7e5..09dd88eea6 100644 --- a/src/tools.h +++ b/src/tools.h @@ -22,7 +22,6 @@ extern int DebugSignal; /// Default is APP_SHORTNAME ('squid'). extern SBuf service_name; -void kb_incr(kb_t *, size_t); void parseEtcHosts(void); int getMyPort(void); void setUmask(mode_t mask); diff --git a/src/tunnel.cc b/src/tunnel.cc index b5467a9242..dbb8904079 100644 --- a/src/tunnel.cc +++ b/src/tunnel.cc @@ -368,8 +368,8 @@ TunnelStateData::readServer(char *, size_t len, Comm::Flag errcode, int xerrno) if (len > 0) { server.bytesIn(len); - kb_incr(&(statCounter.server.all.kbytes_in), len); - kb_incr(&(statCounter.server.other.kbytes_in), len); + statCounter.server.all.kbytes_in += len; + statCounter.server.other.kbytes_in += len; } if (keepGoingAfterRead(len, errcode, xerrno, server, client)) @@ -389,8 +389,8 @@ TunnelStateData::readConnectResponseDone(char *, size_t len, Comm::Flag errcode, if (len > 0) { connectRespBuf->appended(len); server.bytesIn(len); - kb_incr(&(statCounter.server.all.kbytes_in), len); - kb_incr(&(statCounter.server.other.kbytes_in), len); + statCounter.server.all.kbytes_in += len; + statCounter.server.other.kbytes_in += len; } if (keepGoingAfterRead(len, errcode, xerrno, server, client)) @@ -526,7 +526,7 @@ TunnelStateData::readClient(char *, size_t len, Comm::Flag errcode, int xerrno) if (len > 0) { client.bytesIn(len); - kb_incr(&(statCounter.client_http.kbytes_in), len); + statCounter.client_http.kbytes_in += len; } if (keepGoingAfterRead(len, errcode, xerrno, client, server)) @@ -620,8 +620,8 @@ TunnelStateData::writeServerDone(char *, size_t len, Comm::Flag flag, int xerrno } /* Valid data */ - kb_incr(&(statCounter.server.all.kbytes_out), len); - kb_incr(&(statCounter.server.other.kbytes_out), len); + statCounter.server.all.kbytes_out += len; + statCounter.server.other.kbytes_out += len; client.dataSent(len); /* If the other end has closed, so should we */ @@ -689,7 +689,7 @@ TunnelStateData::writeClientDone(char *, size_t len, Comm::Flag flag, int xerrno } /* Valid data */ - kb_incr(&(statCounter.client_http.kbytes_out), len); + statCounter.client_http.kbytes_out += len; server.dataSent(len); /* If the other end has closed, so should we */ diff --git a/src/typedefs.h b/src/typedefs.h index dd09773023..0022fbe06d 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -17,11 +17,6 @@ typedef signed int sdirno; typedef uint32_t nfmark_t; typedef unsigned char tos_t; -typedef struct { - size_t bytes; - size_t kb; -} kb_t; - typedef struct _CommWriteStateData CommWriteStateData; #if SQUID_SNMP diff --git a/src/whois.cc b/src/whois.cc index 25b5972c91..4a830f6074 100644 --- a/src/whois.cc +++ b/src/whois.cc @@ -139,8 +139,8 @@ WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t if (!dataWritten) setReplyToOK(entry); - kb_incr(&(statCounter.server.all.kbytes_in), aBufferLength); - kb_incr(&(statCounter.server.http.kbytes_in), aBufferLength); + statCounter.server.all.kbytes_in += aBufferLength; + statCounter.server.http.kbytes_in += aBufferLength; /* No range support, we always grab it all */ dataWritten = true; -- 2.47.2