[PATCH] rand_sleep and t.lock2

David Relson relson at osagesoftware.com
Sat Feb 1 15:08:34 CET 2003


Aaron,

The patch for wordlists.c is below.  During testing I had the printf() 
enabled.  In the course of 5 runs of t.lock2, 844 delays were printed, 
representing 686 different millisecond counts ranging from 1 ms to 1998 
ms.  Overall run times were 22, 22, 24, 23, and 23 seconds - some 
variation, though not a whole lot.

Give the changes a test run and let me know how it goes.


--- wordlists.c	31 Jan 2003 02:46:01 -0000	1.45
+++ wordlists.c	1 Feb 2003 13:41:02 -0000	1.46
@@ -24,13 +24,37 @@
  #include "xmalloc.h"
  #include "xstrdup.h"

-#define	MIN_SLEEP	500l		/* .5 milliseconds */
-#define	MAX_SLEEP	2000000l	/* 2 seconds */
+#define	MIN_SLEEP	0.5e+3		/* .5 milliseconds */
+#define	MAX_SLEEP	2.0e+6		/* 2 seconds */

  wordlist_t *good_list;
  wordlist_t *spam_list;
  /*@null@*/ wordlist_t* word_lists=NULL;

+/* Function Prototypes */
+
+static void rand_sleep(double min, double max);
+
+/* Function Definitions */
+
+static void rand_sleep(double min, double max)
+{
+    static bool need_init = true;
+    struct timeval timeval;
+    long delay;
+
+    if (need_init) {
+	need_init = false;
+	gettimeofday(&timeval, NULL);
+	srand(timeval.tv_usec);
+    }
+    delay = min + ((max-min)*rand()/(RAND_MAX+1.0));
+    /* printf("%d\n", delay); */
+    timeval.tv_sec  = delay / 1000000;
+    timeval.tv_usec = delay % 1000000;
+    select(0,NULL,NULL,NULL,&timeval);
+}
+
  /* returns -1 for error, 0 for success */
  static int init_wordlist(/*@out@*/ wordlist_t **list, const char* name, 
const char* path,
  			double weight, bool bad, int override, bool ignore)
@@ -122,15 +146,7 @@
  		    /* F_SETLK can't obtain lock */
  		    case EAGAIN:
  		    case EACCES:
-			{
-			    struct timeval to;
-			    long delay = MIN_SLEEP + (long)
-				((MAX_SLEEP-MIN_SLEEP)*rand()/(RAND_MAX+1.0));
-
-			    to.tv_sec  = delay / 1000000;
-			    to.tv_usec = delay % 1000000;
-			    select(0,NULL,NULL,NULL,&to);
-			}
+			rand_sleep(MIN_SLEEP, MAX_SLEEP);
  			retry = 1;
  			break;
  		    default:

--------------------------------------------------------
David Relson                   Osage Software Systems, Inc.
relson at osagesoftware.com       Ann Arbor, MI 48103
www.osagesoftware.com          tel:  734.821.8800





More information about the bogofilter-dev mailing list