Article View: news.software.nntp
Article #18219INN Performance Patch
From: Jeff Garzik
Date: Tue, 03 Jun 1997 00:00
Date: Tue, 03 Jun 1997 00:00
472 lines
13017 bytes
13017 bytes
This is a multi-part message in MIME format.
--------------626D7E96693D3FE26F39FE98
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
This patch adds *two* caches to INN 1.5.1. It adds a pre-commit L1
cache, since they're all the rage, and it adds a history cache modeled
after coneill's history hash patch. Both caches are very effective. On
my system, I almost always have over a 30% hit rate:
History cache: 643191 lookups, 318521 hits
Precommit cache: 606713 lookups, 188485 hits
The L1 precommit cache simply stores hashed msg ids in a 4096-elem array
after an IHAVE or TAKETHIS. The next IHAVEs or CHECKs that search for
the msg id will be refused.
The L2 history cache functions like coneill's -- it hashes each history
lookup into a large (64 * 1024) array to cache the value of the lookup
for later calls.
The main ways this differs from coneill's patch is
- is uses NO memory allocation, hash values are stored
instead of strdup'ing the msg ids. After a short test
(2-3 hours) storing the msg ids as well as hashes, I found
no collisions with duplicate hash values for different
msg ids.
- it fixes the Data->MessageIDLength bug in coneill's patch,
and patches cc.c to initialize Data->* fields correctly.
- reports history and precommit cache hits in syslog and
with 'ctlinnd mode'
There is always room for improvement. MD5 hashes of the msg id (instead
of dbzhash()) would be less prone to possible collisions.
The precommit cache shouldn't be referenced in his.c, but it was
convenient to report precommit cache stats at the same time as reporting
the history cache stats.
Rejected articles might be refused erroneously by the pre-commit cache
with REMEMBER_TRASH is DONT. Not a big deal IMHO.
--
Jeff Garzik
News Administrator, Spinne, Inc.
http://www.spinne.com/usenet/
--------------626D7E96693D3FE26F39FE98
Content-Type: text/plain; charset=us-ascii; name="innd.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="innd.patch"
diff --context -r --ignore-space-change ../inn-plain/innd/cc.c innd/cc.c
*** ../inn-plain/innd/cc.c Tue Dec 17 09:40:40 1996
--- innd/cc.c Tue Jun 3 02:49:26 1997
***************
*** 410,420 ****
ARTDATA Data;
STRING p;
! Data.Posted = Now.time;
Data.Expires = 0;
Data.Feedsite = "?";
if ((p = CCgetid(av[0], &Data.MessageID)) != NULL)
return p;
if (Mode == OMrunning)
ARTcancel(&Data, Data.MessageID, TRUE);
else {
--- 410,422 ----
ARTDATA Data;
STRING p;
! memset ((void *)&Data, 0, sizeof(Data));
! Data.Posted = Data.Arrived = Now.time;
Data.Expires = 0;
Data.Feedsite = "?";
if ((p = CCgetid(av[0], &Data.MessageID)) != NULL)
return p;
+ Data.MessageIDLength = strlen(Data.MessageID);
if (Mode == OMrunning)
ARTcancel(&Data, Data.MessageID, TRUE);
else {
***************
*** 734,739 ****
--- 736,747 ----
}
+ extern unsigned long HIScachehits;
+ extern unsigned long HIScachelookups;
+ extern unsigned long NCprecomhits;
+ extern unsigned long NCprecomlookups;
+
+
/*
** Return our operating mode.
*/
***************
*** 840,845 ****
--- 848,861 ----
p += strlen(strcpy(p, "disabled"));
#endif /* defined(DO_PERL) */
+ *p++ = '\n';
+ sprintf(p, "History cache: %lu lookups, %lu hits", HIScachelookups, HIScachehits);
+ p += strlen(p);
+
+ *p++ = '\n';
+ sprintf(p, "Precommit cache: %lu lookups, %lu hits", NCprecomlookups, NCprecomhits);
+ p += strlen(p);
+
i = strlen(buff);
if (CCreply.Size <= i) {
CCreply.Size = i;
diff --context -r --ignore-space-change ../inn-plain/innd/his.c innd/his.c
*** ../inn-plain/innd/his.c Tue Dec 17 09:40:40 1996
--- innd/his.c Tue Jun 3 02:57:40 1997
***************
*** 1,4 ****
! /* $Revision: 1.23 $
**
** History file routines.
*/
--- 1,4 ----
! /* $Revision: 1.2 $
**
** History file routines.
*/
***************
*** 9,21 ****
--- 9,33 ----
#include "innd.h"
#include "dbz.h"
+ #define HISCACHESIZE (64 * 1024L) /* num. elements in cache array */
+ #define HISCACHELOGTIME 1400L /* seconds between cache hit reports to syslog */
+
+ typedef struct {
+ long MessageIDHash;
+ BOOL Found;
+ } _HIScache;
STATIC char HIShistpath[] = _PATH_HISTORY;
STATIC FILE *HISwritefp;
STATIC int HISreadfd;
STATIC int HISdirty;
STATIC int HISincore = INND_DBZINCORE;
+ STATIC _HIScache HIScache[HISCACHESIZE];
+ unsigned long HIScachehits;
+ unsigned long HIScachelookups;
+ STATIC time_t HIScachelastreported;
+ void NCprecomclear();
/*
** Set up the history files.
***************
*** 46,51 ****
--- 58,71 ----
exit(1);
}
}
+
+ syslog(L_NOTICE, "SERVER history cache reset: %ld entries, %u bytes",
+ HISCACHESIZE, sizeof(HIScache));
+ memset((void *)&HIScache, 0, sizeof(HIScache));
+ HIScachehits = HIScachelookups = 0;
+ HIScachelastreported = Now.time;
+
+ NCprecomclear();
}
***************
*** 65,76 ****
--- 85,101 ----
}
+ extern unsigned long NCprecomhits;
+ extern unsigned long NCprecomlookups;
+
/*
** Close the history files.
*/
void
HISclose()
{
+ unsigned long i, holes = 0;
+
if (HISwritefp != NULL) {
/* Since dbmclose calls dbzsync we could replace this line with
* "HISdirty = 0;". Oh well, it keeps the abstraction clean. */
***************
*** 84,89 ****
--- 109,124 ----
syslog(L_ERROR, "%s cant close history %m", LogName);
HISreadfd = -1;
}
+
+ for (i = 0; i < HISCACHESIZE; i++)
+ if (HIScache[i].MessageIDHash == 0)
+ holes++;
+
+ syslog(L_NOTICE, "SERVER history cache final: %lu lookups, %lu hits, %lu unused slots",
+ HIScachelookups, HIScachehits, holes);
+
+ syslog(L_NOTICE, "SERVER precommit cache final: %lu lookups, %lu hits",
+ NCprecomlookups, NCprecomhits);
}
***************
*** 198,206 ****
--- 233,262 ----
{
datum key;
datum val;
+ long hash_pos, hash_val;
+
+ hash_val = dbzhash(MessageID, strlen(MessageID));
+ hash_pos = hash_val % HISCACHESIZE;
+
+ HIScachelookups++;
+ if (HIScache[hash_pos].MessageIDHash == hash_val) {
+ HIScachehits++;
+
+ if (Now.time > (HIScachelastreported + HISCACHELOGTIME)) {
+ syslog(L_NOTICE, "SERVER history cache: %lu lookups, %lu hits",
+ HIScachelookups, HIScachehits);
+
+ HIScachelastreported = Now.time;
+ }
+
+ return HIScache[hash_pos].Found;
+ }
HISsetkey(MessageID, &key);
val = dbzfetch(key);
+
+ HIScache[hash_pos].MessageIDHash = hash_val;
+ HIScache[hash_pos].Found = (char)(val.dptr != NULL);
return val.dptr != NULL;
}
***************
*** 237,247 ****
char *paths;
{
static char NOPATHS[] = "";
! long offset;
datum key;
datum val;
int i;
HISsetkey(Data->MessageID, &key);
if (paths != NULL && paths[0] != '\0')
HISslashify(paths);
--- 293,304 ----
char *paths;
{
static char NOPATHS[] = "";
! long offset, hash_val, hash_pos;
datum key;
datum val;
int i;
+
HISsetkey(Data->MessageID, &key);
if (paths != NULL && paths[0] != '\0')
HISslashify(paths);
***************
*** 279,284 ****
--- 336,348 ----
return FALSE;
}
+ /* store hashed message id value in lookup cache */
+ hash_val = dbzhash(Data->MessageID, strlen(Data->MessageID));
+ hash_pos = hash_val % HISCACHESIZE;
+
+ HIScache[hash_pos].MessageIDHash = hash_val;
+ HIScache[hash_pos].Found = TRUE;
+
if (++HISdirty >= ICD_SYNC_COUNT)
HISsync();
return TRUE;
diff --context -r --ignore-space-change ../inn-plain/innd/nc.c innd/nc.c
*** ../inn-plain/innd/nc.c Tue Dec 17 09:40:40 1996
--- innd/nc.c Tue Jun 3 02:45:58 1997
***************
*** 105,110 ****
--- 105,131 ----
/*
+ * precommit cache
+ */
+ #define NCPRECOMCACHESIZE 4096L
+
+ STATIC long NCprecomcache[NCPRECOMCACHESIZE];
+ unsigned long NCprecomhits;
+ unsigned long NCprecomlookups;
+ long NCprecompos, NCprecomval;
+
+
+ void
+ NCprecomclear ()
+ {
+ memset((void *)&NCprecomcache, 0, sizeof(NCprecomcache));
+ NCprecomhits = NCprecomlookups = 0;
+ }
+
+
+
+
+ /*
** Clear the work-in-progress entry and wake up anyone who might
** have been waiting for us.
*/
***************
*** 582,588 ****
if (NCbadid(cp, p))
return;
! if (HIShavearticle(p)) {
cp->Refused++;
NCwritetext(cp, NNTP_HAVEIT);
}
--- 603,619 ----
if (NCbadid(cp, p))
return;
! NCprecomval = dbzhash(p, strlen(p));
! NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
! NCprecomlookups++;
!
! if (NCprecomcache[NCprecompos] == NCprecomval) {
! NCprecomhits++;
! cp->Refused++;
! NCwritetext(cp, NNTP_HAVEIT);
! }
!
! else if (HIShavearticle(p)) {
cp->Refused++;
NCwritetext(cp, NNTP_HAVEIT);
}
***************
*** 599,604 ****
--- 630,637 ----
#endif /* defined(NNTP_RESENDIT_LATER) */
}
else {
+ NCprecomcache[NCprecompos] = NCprecomval;
+
NCwritetext(cp, NNTP_SENDIT);
cp->State = CSgetarticle;
}
***************
*** 1486,1492 ****
return;
}
! if (HIShavearticle(p)) {
cp->Refused++;
(void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
NCwritereply(cp, cp->Sendid.Data);
--- 1519,1534 ----
return;
}
! NCprecomval = dbzhash(p, strlen(p));
! NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
! NCprecomlookups++;
!
! if (NCprecomcache[NCprecompos] == NCprecomval) {
! NCprecomhits++;
! cp->Refused++;
! (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
! NCwritereply(cp, cp->Sendid.Data);
! } else if (HIShavearticle(p)) {
cp->Refused++;
(void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
NCwritereply(cp, cp->Sendid.Data);
***************
*** 1521,1526 ****
--- 1563,1575 ----
if (!ARTidok(p)) {
syslog(L_NOTICE, "%s bad_messageid %s", CHANname(cp), MaxLength(p, p));
}
+
+ else {
+ NCprecomval = dbzhash(p, strlen(p));
+ NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
+ NCprecomcache[NCprecompos] = NCprecomval;
+ }
+
msglen = strlen(p) + 5; /* 3 digits + space + id + null */
if (cp->Sendid.Size < msglen) {
if (cp->Sendid.Size > 0) DISPOSE(cp->Sendid.Data);
*** ../inn-plain/dbz/dbz.c Tue Dec 17 09:40:40 1996
--- dbz/dbz.c Mon Jun 2 05:24:59 1997
***************
*** 355,361 ****
extern void CloseOnExec();
/* misc. forwards */
- static long hash();
static void crcinit();
static char *cipoint();
static char *mapcase();
--- 355,360 ----
***************
*** 1426,1432 ****
{
register long h;
! h = hash(kp->dptr, kp->dsize);
if (osp != FRESH && osp->hash == h) {
if (sp != osp)
*sp = *osp;
--- 1425,1431 ----
{
register long h;
! h = dbzhash(kp->dptr, kp->dsize);
if (osp != FRESH && osp->hash == h) {
if (sp != osp)
*sp = *osp;
***************
*** 1716,1725 ****
}
/*
! - hash - Honeyman's nice hashing function
*/
! static long
! hash(name, size)
register char *name;
register int size;
{
--- 1715,1724 ----
}
/*
! - dbzhash - Honeyman's nice hashing function
*/
! long
! dbzhash(name, size)
register char *name;
register int size;
{
*** ../inn-plain/dbz/dbz.h Tue Dec 17 09:40:40 1996
--- dbz/dbz.h Mon Jun 2 05:24:59 1997
***************
*** 24,29 ****
--- 24,30 ----
extern int dbzcancel();
extern int dbzdebug();
extern int dbzwritethrough();
+ extern long dbzhash();
/*
* In principle we could handle unlimited-length keys by operating a chunk
--------------626D7E96693D3FE26F39FE98--
Technical Details
Message-ID:
<33948068.4F305FB8@spinne.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailReferences:
<5mt7et$s52$1@turing.mathworks.com> <5mtdlr$ndu$1@hammer.msfc.nasa.gov> <82bu5ogp8s.fsf@manic.eecs.umich.edu> <5n15ar$9md$1@drum.msfc.nasa.gov>Show Full Headers
Complete Article Headers
Message-ID: <33948068.4F305FB8@spinne.com> Subject: INN Performance Patch Date: Tue, 03 Jun 1997 00:00:00 +0000 From: Jeff Garzik <jeff.garzik@spinne.com> References: <5mt7et$s52$1@turing.mathworks.com> <5mtdlr$ndu$1@hammer.msfc.nasa.gov> <82bu5ogp8s.fsf@manic.eecs.umich.edu> <5n15ar$9md$1@drum.msfc.nasa.gov> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 245907655 Organization: Spinne http://spinne.com Newsgroups: news.software.nntp