svn commit: r189612 - in user/lstewart/alq_varlen_8.x: sys/kern tools/test/alq

Lawrence Stewart lstewart at FreeBSD.org
Tue Mar 10 00:51:00 PDT 2009


Author: lstewart
Date: Tue Mar 10 07:50:59 2009
New Revision: 189612
URL: http://svn.freebsd.org/changeset/base/189612

Log:
  More bug and style fixes.

Modified:
  user/lstewart/alq_varlen_8.x/sys/kern/kern_alq.c
  user/lstewart/alq_varlen_8.x/tools/test/alq/Makefile
  user/lstewart/alq_varlen_8.x/tools/test/alq/alqtest.c

Modified: user/lstewart/alq_varlen_8.x/sys/kern/kern_alq.c
==============================================================================
--- user/lstewart/alq_varlen_8.x/sys/kern/kern_alq.c	Tue Mar 10 06:21:52 2009	(r189611)
+++ user/lstewart/alq_varlen_8.x/sys/kern/kern_alq.c	Tue Mar 10 07:50:59 2009	(r189612)
@@ -270,6 +270,7 @@ alq_shutdown(struct alq *alq)
 		alq->aq_flags |= AQ_WANTED;
 		msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
 	}
+
 	ALQ_UNLOCK(alq);
 
 	vn_close(alq->aq_vp, FWRITE, alq->aq_cred, curthread);
@@ -290,6 +291,7 @@ alq_doio(struct alq *alq)
 	int totlen;
 	int iov;
 	int vfslocked;
+	int prev_writehead = alq->aq_writehead;
 
 	KASSERT((ALQ_HAS_PENDING_DATA(alq)),
 		("%s: queue emtpy!", __func__)
@@ -298,43 +300,46 @@ alq_doio(struct alq *alq)
 	vp = alq->aq_vp;
 	td = curthread;
 	totlen = 0;
-	iov = 0;
+	iov = 1;
 
 	bzero(&aiov, sizeof(aiov));
 	bzero(&auio, sizeof(auio));
 
 	/* Start the write from the location of our buffer tail pointer. */
-	aiov[iov].iov_base = alq->aq_entbuf + alq->aq_writetail;
+	aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail;
 
 	if (alq->aq_writetail < alq->aq_writehead) {
 		/* Buffer not wrapped */
-		totlen = aiov[iov].iov_len =  alq->aq_writehead -
-							alq->aq_writetail;
+		totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail;
 	} else {
 		/*
 		 * Buffer wrapped, requires 2 aiov entries:
 		 * - first is from writetail to end of buffer
 		 * - second is from start of buffer to writehead
 		 */
-		aiov[iov].iov_len =  alq->aq_buflen - alq->aq_writetail;
+		aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail;
 		iov++;
-		aiov[iov].iov_base = alq->aq_entbuf;
-		aiov[iov].iov_len =  alq->aq_writehead;
+		aiov[1].iov_base = alq->aq_entbuf;
+		aiov[1].iov_len =  alq->aq_writehead;
 		totlen = aiov[0].iov_len + aiov[1].iov_len;
 	}
 
 	alq->aq_flags |= AQ_FLUSHING;
 
+	/*printf("pre: alq->aq_writehead=%d,alq->aq_writetail=%d,totlen=%d,iov=%d\n", alq->aq_writehead, alq->aq_writetail, totlen, iov);*/
+
 	if (alq->doio_debugcallback != NULL)
 		alq->doio_debugcallback();
 
+	/*printf("flushing %d bytes\n", totlen);*/
+
 	ALQ_UNLOCK(alq);
 
 	auio.uio_iov = &aiov[0];
 	auio.uio_offset = 0;
 	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_iovcnt = iov + 1;
+	auio.uio_iovcnt = iov;
 	auio.uio_resid = totlen;
 	auio.uio_td = td;
 
@@ -359,8 +364,14 @@ alq_doio(struct alq *alq)
 	ALQ_LOCK(alq);
 	alq->aq_flags &= ~AQ_FLUSHING;
 
+	/*printf("finished flushing %d bytes\n", totlen);*/
+
 	/* Adjust writetail as required, taking into account wrapping. */
-	alq->aq_writetail += (iov == 2) ? aiov[1].iov_len : totlen;
+	if (iov == 2)
+		alq->aq_writetail = prev_writehead;
+	else
+		alq->aq_writetail = (alq->aq_writetail + totlen) % alq->aq_buflen;
+
 	alq->aq_freebytes += totlen;
 
 	/*
@@ -371,6 +382,12 @@ alq_doio(struct alq *alq)
 	if (!ALQ_HAS_PENDING_DATA(alq))
 		alq->aq_writehead = alq->aq_writetail = 0;
 
+	/*printf("post: alq->aq_writehead=%d,alq->aq_writetail=%d,totlen=%d,iov=%d\n", alq->aq_writehead, alq->aq_writetail, totlen, iov);*/
+
+	KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen),
+		("%s: aq_writetail < 0 || aq_writetail >= aq_buflen", __func__)
+	);
+
 	if (alq->doio_debugcallback != NULL)
 		alq->doio_debugcallback();
 
@@ -480,7 +497,7 @@ alq_writen(struct alq *alq, void *data, 
 	int copy = len;
 
 	KASSERT((len > 0 && len <= alq->aq_buflen),
-		("%s: len <= 0 || len > alq->aq_buflen", __func__)
+		("%s: len <= 0 || len > aq_buflen", __func__)
 	);
 
 	ALQ_LOCK(alq);
@@ -516,7 +533,7 @@ alq_writen(struct alq *alq, void *data, 
 
 	/* Bail if we're shutting down. */
 	if (alq->aq_flags & AQ_SHUTDOWN) {
-	    ALQ_UNLOCK(alq);
+		ALQ_UNLOCK(alq);
 		return (EWOULDBLOCK);
 	}
 
@@ -540,6 +557,10 @@ alq_writen(struct alq *alq, void *data, 
 		alq->aq_writehead = len - copy;
 	}
 
+	KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen),
+		("%s: aq_writehead < 0 || aq_writehead >= aq_buflen", __func__)
+	);
+
 	alq->aq_freebytes -= len;
 
 	if (((alq->aq_flags & AQ_ACTIVE) == 0) &&
@@ -657,6 +678,10 @@ alq_getn(struct alq *alq, int len, int f
 	if (alq->aq_writehead == alq->aq_buflen)
 		alq->aq_writehead = 0;
 
+	KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen),
+		("%s: aq_writehead < 0 || aq_writehead >= aq_buflen", __func__)
+	);
+
 	return (ale);
 }
 
@@ -696,7 +721,11 @@ alq_flush(struct alq *alq)
 
 	ALD_UNLOCK();
 
-	if (ALQ_HAS_PENDING_DATA(alq))
+	/*
+	 * Pull the lever iff there is data to flush and we're
+	 * not already in the middle of a flush operation.
+	 */
+	if (ALQ_HAS_PENDING_DATA(alq) && (alq->aq_flags & AQ_FLUSHING) == 0)
 		needwakeup = alq_doio(alq);
 
 	ALQ_UNLOCK(alq);

Modified: user/lstewart/alq_varlen_8.x/tools/test/alq/Makefile
==============================================================================
--- user/lstewart/alq_varlen_8.x/tools/test/alq/Makefile	Tue Mar 10 06:21:52 2009	(r189611)
+++ user/lstewart/alq_varlen_8.x/tools/test/alq/Makefile	Tue Mar 10 07:50:59 2009	(r189612)
@@ -6,8 +6,6 @@ KMOD=alqtest
 SRCS=alqtest.c alqtest.h
 CLEANFILES=alqtest.h
 
-SYSDIR=/devel/freebsd_mirror/user/lstewart/alq_varlen_8.x/sys
-
 alqtest.h:
 	@awk -F "\n" '{ if(index($$0, "struct alq {") > 0) p=1; if(p == 1) { print $$0; if($$0 == "};") exit; } }' ${.CURDIR}/../../../sys/kern/kern_alq.c >> ${.TARGET}
 

Modified: user/lstewart/alq_varlen_8.x/tools/test/alq/alqtest.c
==============================================================================
--- user/lstewart/alq_varlen_8.x/tools/test/alq/alqtest.c	Tue Mar 10 06:21:52 2009	(r189611)
+++ user/lstewart/alq_varlen_8.x/tools/test/alq/alqtest.c	Tue Mar 10 07:50:59 2009	(r189612)
@@ -71,17 +71,25 @@ typedef const enum {
 } fgcolor_t;
 
 static int
-sbuf_printf_color(struct sbuf *s, fgcolor_t c, const char *fmt, ...)
+alqtest_printf(struct sbuf *s, fgcolor_t c, const char *fmt, ...)
 {
-	va_list ap;
 	int ret;
+	va_list ap1, ap2;
+
+	va_start(ap1, fmt);
+	va_copy(ap2, ap1);
+
+	printf("\033[%dm", c);
+	vprintf(fmt, ap1);
+	printf("\033[0m");
 
 	sbuf_printf(s, "\033[%dm", c);
-	va_start(ap, fmt);
-	ret = sbuf_vprintf(s, fmt, ap);
-	va_end(ap);
+	ret = sbuf_vprintf(s, fmt, ap2);
 	sbuf_printf(s, "\033[0m");
 
+	va_end(ap2);
+	va_end(ap1);
+
 	return (ret);
 }
 
@@ -96,21 +104,31 @@ alqtest_randchar(void)
 	return (char)c;
 }
 
-static void
+static uint32_t
+alqtest_rand(uint32_t lower, uint32_t upper)
+{
+	uint32_t n;
+
+	while ( (n = arc4random() % (upper+1)) < lower);
+
+	return n;
+}
+
+/*static void
 alqtest_doio_callback(void)
 {
 	printf("doing io baby!\n");
-}
+}*/
 
 static int
 alqtest_writen(struct sbuf *s, struct sbuf *debug)
 {
 	struct alq *testalq;
 	const int buflen = 100;
-	int i = 0, ret = 0, errors = 0;
+	int i = 0, n = 0, ret = 0, errors = 0;
 	char buf[buflen+1];
 
-	sbuf_printf(s, "- variable length message writing\n");
+	alqtest_printf(s, 0, "- variable length message writing\n");
 
 	/* test variable length message writing */
 	ret = alq_open(	&testalq,
@@ -121,31 +139,63 @@ alqtest_writen(struct sbuf *s, struct sb
 			0
 	);
 
-	testalq->doio_debugcallback = &alqtest_doio_callback;
+	/*testalq->doio_debugcallback = &alqtest_doio_callback;*/
 
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = alqtest_randchar();
 
-	sbuf_printf(s, "-- msglen==1,buflen=%d\n", buflen);
+	alqtest_printf(s, 0, "-- msglen==1,buflen=%d\n", buflen);
 	alq_writen(testalq, buf, 1, ALQ_WAITOK | ALQ_NOACTIVATE);
 
 	if ((buflen-1 != testalq->aq_freebytes) &&
 		(1 != testalq->aq_writehead) &&
 			(0 != testalq->aq_writetail)) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_freebytes",
 				buflen-1,
 				testalq->aq_freebytes
 		);
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writehead",
 				1,
 				testalq->aq_writehead
 		);
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_writetail",
+				0,
+				testalq->aq_writetail
+		);
+	}
+
+	alq_flush(testalq);
+
+	if ((buflen != testalq->aq_freebytes) &&
+		(0 != testalq->aq_writehead) &&
+			(0 != testalq->aq_writetail)) {
+		errors++;
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_freebytes",
+				buflen,
+				testalq->aq_freebytes
+		);
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_writehead",
+				0,
+				testalq->aq_writehead
+		);
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writetail",
 				0,
@@ -153,26 +203,29 @@ alqtest_writen(struct sbuf *s, struct sb
 		);
 	}
 
-	sbuf_printf(s, "-- msglen==%d,buflen=%d\n", buflen, buflen);
-	alq_writen(testalq, buf, buflen, ALQ_WAITOK);
+	alqtest_printf(s, 0, "-- msglen==%d,buflen=%d\n", buflen, buflen);
+	alq_writen(testalq, buf, buflen, ALQ_WAITOK | ALQ_NOACTIVATE);
 
 	if ((0 != testalq->aq_freebytes) &&
 		(0 != testalq->aq_writehead) &&
 			(0 != testalq->aq_writetail)) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_freebytes",
 				0,
 				testalq->aq_freebytes
 		);
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writehead",
 				0,
 				testalq->aq_writehead
 		);
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writetail",
 				0,
@@ -180,6 +233,106 @@ alqtest_writen(struct sbuf *s, struct sb
 		);
 	}
 
+	alq_flush(testalq);
+
+	if ((buflen != testalq->aq_freebytes) &&
+		(0 != testalq->aq_writehead) &&
+			(0 != testalq->aq_writetail)) {
+		errors++;
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_freebytes",
+				buflen,
+				testalq->aq_freebytes
+		);
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_writehead",
+				0,
+				testalq->aq_writehead
+		);
+		alqtest_printf(	debug,
+				0,
+				"alq->%-15s\texpected=%d\tactual=%d\n",
+				"aq_writetail",
+				0,
+				testalq->aq_writetail
+		);
+	}
+
+#define NMSGS	100
+
+	alqtest_printf(	s,
+			0,
+			"-- nmsgs==%d,buflen=%d,msglen==[1,%d],flags==ALQ_WAITOK|ALQ_NOACTIVATE\n",
+			NMSGS,
+			buflen,
+			buflen
+	);
+
+	for (i = 0; i < NMSGS; i++) {
+		n = alqtest_rand(1,buflen);
+		alqtest_printf(	s,
+				0,
+				"--- msg==%d,msglen==%d\n",
+				i,
+				n
+		);
+		alq_writen(testalq, buf, n, ALQ_WAITOK|ALQ_NOACTIVATE);
+
+		alq_flush(testalq);
+	
+		if ((buflen != testalq->aq_freebytes) &&
+			(0 != testalq->aq_writehead) &&
+				(0 != testalq->aq_writetail)) {
+			errors++;
+			alqtest_printf(	debug,
+					0,
+					"alq->%-15s\texpected=%d\tactual=%d\n",
+					"aq_freebytes",
+					buflen,
+					testalq->aq_freebytes
+			);
+			alqtest_printf(	debug,
+					0,
+					"alq->%-15s\texpected=%d\tactual=%d\n",
+					"aq_writehead",
+					0,
+					testalq->aq_writehead
+			);
+			alqtest_printf(	debug,
+					0,
+					"alq->%-15s\texpected=%d\tactual=%d\n",
+					"aq_writetail",
+					0,
+					testalq->aq_writetail
+			);
+		}
+	}
+
+	alqtest_printf(	s,
+			0,
+			"-- nmsgs==%d,buflen=%d,msglen==[1,%d],flags==ALQ_WAITOK\n",
+			NMSGS,
+			buflen,
+			buflen
+	);
+
+	for (i = 0; i < NMSGS; i++) {
+		n = alqtest_rand(1,buflen);
+		alqtest_printf(	s,
+				0,
+				"--- msg==%d,msglen==%d\n",
+				i,
+				n
+		);
+		alq_writen(testalq, buf, n, ALQ_WAITOK);
+	}
+
+	alq_flush(testalq);
+
 	alq_close(testalq);
 
 	return errors;
@@ -192,7 +345,7 @@ alqtest_open(struct sbuf *s, struct sbuf
 	const int buflen = 100;
 	int ret = 0, errors = 0;
 
-	sbuf_printf(s, "- variable length message queue creation\n");
+	alqtest_printf(s, 0, "- variable length message queue creation\n");
 
 	/* test variable length message queue creation */
 	ret = alq_open(	&testalq,
@@ -205,7 +358,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (0 != testalq->aq_entmax) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_entmax",
 				0,
@@ -215,7 +369,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (0 != testalq->aq_entlen) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_entlen",
 				0,
@@ -225,7 +380,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (buflen != testalq->aq_freebytes) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_freebytes",
 				buflen,
@@ -235,7 +391,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (buflen != testalq->aq_buflen) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_buflen",
 				buflen,
@@ -245,7 +402,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (0 != testalq->aq_writehead) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writehead",
 				0,
@@ -255,7 +413,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (0 != testalq->aq_writetail) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_writetail",
 				0,
@@ -265,7 +424,8 @@ alqtest_open(struct sbuf *s, struct sbuf
 
 	if (0 != testalq->aq_flags) {
 		errors++;
-		sbuf_printf(	debug,
+		alqtest_printf(	debug,
+				0,
 				"alq->%-15s\texpected=%d\tactual=%d\n",
 				"aq_flags",
 				0,
@@ -284,15 +444,15 @@ run_test(struct sbuf *s, const char *tes
 	struct sbuf *debug = NULL;
 
 	if ((debug = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND)) != NULL) {
-		sbuf_printf(s, "########################################\n");
-		sbuf_printf_color(s, GREEN, "%s\n", test_banner);
+		alqtest_printf(s, 0, "########################################\n");
+		alqtest_printf(s, GREEN, "%s\n", test_banner);
 		if (test(s, debug)) {
 			sbuf_finish(debug);
-			sbuf_printf_color(s, RED, "!!ERROR(S) FOUND!!\n");
-			sbuf_printf(s, "%s", sbuf_data(debug));
-			sbuf_printf_color(s, RED, "!!ERROR(S) FOUND!!\n");
+			alqtest_printf(s, RED, "!!ERROR(S) FOUND!!\n");
+			alqtest_printf(s, 0, "%s", sbuf_data(debug));
+			alqtest_printf(s, RED, "!!ERROR(S) FOUND!!\n");
 		}
-		sbuf_printf(s, "########################################\n\n");
+		alqtest_printf(s, 0, "########################################\n\n");
 		sbuf_delete(debug);
 	}
 }
@@ -306,13 +466,13 @@ alqtest_thread(void *arg)
 	/* loop until thread is signalled to exit */
 	while (run_test_thread && runs < NUM_TEST_RUNS) {
 		if ((s = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND)) != NULL) {
-			sbuf_printf(s, "TEST RUN: %ld\n", ++runs);
+			alqtest_printf(s, 0, "TEST RUN: %ld\n", ++runs);
 
 			run_test(s, "alq_open", &alqtest_open);
 			run_test(s, "alq_writen", &alqtest_writen);
 
 			sbuf_finish(s);
-			printf("%s", sbuf_data(s));
+			/*printf("%s", sbuf_data(s));*/
 			sbuf_delete(s);
 		}
 	}


More information about the svn-src-user mailing list