Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Mar 2012 00:36:33 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r233135 - in head/sys: kern sys
Message-ID:  <201203190036.q2J0aXUT042500@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler
Date: Mon Mar 19 00:36:32 2012
New Revision: 233135
URL: http://svn.freebsd.org/changeset/base/233135

Log:
  - Clean up timestamps in msgbuf code.  The timestamps should now be
      inserted after the priority token thus cleaning up the output.
  - Remove the needless double internal do_add_char function.
  - Resolve a possible deadlock if interrupts are
      disabled and getnanotime is called
  
  Reviewed by:	bde  kmacy, avg, sbruno (various versions)
  Approved by:	cperciva
  MFC after:	2 weeks

Modified:
  head/sys/kern/subr_msgbuf.c
  head/sys/sys/msgbuf.h

Modified: head/sys/kern/subr_msgbuf.c
==============================================================================
--- head/sys/kern/subr_msgbuf.c	Mon Mar 19 00:07:10 2012	(r233134)
+++ head/sys/kern/subr_msgbuf.c	Mon Mar 19 00:36:32 2012	(r233135)
@@ -49,7 +49,8 @@
 static u_int msgbuf_cksum(struct msgbuf *mbp);
 
 /*
- *
+ * Timestamps in msgbuf are useful when trying to diagnose when core dumps
+ * or other actions occured.
  */
 static int msgbuf_show_timestamp = 0;
 SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW | CTLFLAG_TUN,
@@ -143,49 +144,20 @@ msgbuf_getcount(struct msgbuf *mbp)
  *
  * The caller should hold the message buffer spinlock.
  */
-static inline void
-__msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
+
+static void
+msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
 {
 	u_int pos;
 
 	/* Make sure we properly wrap the sequence number. */
 	pos = MSGBUF_SEQ_TO_POS(mbp, *seq);
-
-	mbp->msg_cksum += (u_int)c -
+	mbp->msg_cksum += (u_int)(u_char)c -
 	    (u_int)(u_char)mbp->msg_ptr[pos];
-
 	mbp->msg_ptr[pos] = c;
-
 	*seq = MSGBUF_SEQNORM(mbp, *seq + 1);
 }
 
-static inline void
-msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
-{
-
-	if (msgbuf_show_timestamp &&
-	    (mbp->msg_flags & MSGBUF_NEXT_NEW_LINE) != 0) {
-		char buf[32];
-		char const *bufp;
-		struct timespec ts;
-		int err;
-
-		getnanouptime(&ts);
-		err = snprintf(buf, sizeof (buf), "[%jd.%ld] ",
-		    (intmax_t)ts.tv_sec, ts.tv_nsec / 1000);
-
-		for (bufp = buf; *bufp != '\0'; bufp++)
-			__msgbuf_do_addchar(mbp, seq, *bufp);
-
-		mbp->msg_flags &= ~MSGBUF_NEXT_NEW_LINE;
-	}
-
-	__msgbuf_do_addchar(mbp, seq, c);
-
-	if (c == '\n')
-		mbp->msg_flags |= MSGBUF_NEXT_NEW_LINE;
-}
-
 /*
  * Append a character to a message buffer.
  */
@@ -213,7 +185,8 @@ msgbuf_addstr(struct msgbuf *mbp, int pr
 	u_int seq;
 	size_t len, prefix_len;
 	char prefix[MAXPRIBUF];
-	int nl, i;
+	char buf[32];
+	int nl, i, j, needtime;
 
 	len = strlen(str);
 	prefix_len = 0;
@@ -250,6 +223,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pr
 		mbp->msg_flags &= ~MSGBUF_NEEDNL;
 	}
 
+	needtime = 1;
 	for (i = 0; i < len; i++) {
 		/*
 		 * If we just had a newline, and the priority is not -1
@@ -263,6 +237,16 @@ msgbuf_addstr(struct msgbuf *mbp, int pr
 				msgbuf_do_addchar(mbp, &seq, prefix[j]);
 		}
 
+		if (msgbuf_show_timestamp && needtime == 1 &&
+		    (mbp->msg_flags & MSGBUF_NEEDNL) == 0) {
+
+			snprintf(buf, sizeof(buf), "[%jd] ",
+			    (intmax_t)time_uptime);
+			for (j = 0; buf[j] != '\0'; j++)
+				msgbuf_do_addchar(mbp, &seq, buf[j]);
+			needtime = 0;
+		}
+
 		/*
 		 * Don't copy carriage returns if the caller requested
 		 * filtering.

Modified: head/sys/sys/msgbuf.h
==============================================================================
--- head/sys/sys/msgbuf.h	Mon Mar 19 00:07:10 2012	(r233134)
+++ head/sys/sys/msgbuf.h	Mon Mar 19 00:36:32 2012	(r233135)
@@ -46,9 +46,8 @@ struct msgbuf {
 	u_int	   msg_cksum;		/* checksum of contents */
 	u_int	   msg_seqmod;		/* range for sequence numbers */
 	int	   msg_lastpri;		/* saved priority value */
-	u_int   msg_flags;
-#define MSGBUF_NEEDNL		0x01	/* set when newline needed */
-#define MSGBUF_NEXT_NEW_LINE	0x02
+	u_int      msg_flags;
+#define MSGBUF_NEEDNL	0x01	/* set when newline needed */
 	struct mtx msg_lock;		/* mutex to protect the buffer */
 };
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201203190036.q2J0aXUT042500>