From owner-svn-src-all@FreeBSD.ORG Thu Feb 16 05:11:35 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9D338106564A; Thu, 16 Feb 2012 05:11:35 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 877B28FC13; Thu, 16 Feb 2012 05:11:35 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q1G5BZ7R099788; Thu, 16 Feb 2012 05:11:35 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q1G5BZNk099785; Thu, 16 Feb 2012 05:11:35 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201202160511.q1G5BZNk099785@svn.freebsd.org> From: Eitan Adler Date: Thu, 16 Feb 2012 05:11:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r231814 - in head/sys: kern sys X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Feb 2012 05:11:35 -0000 Author: eadler Date: Thu Feb 16 05:11:35 2012 New Revision: 231814 URL: http://svn.freebsd.org/changeset/base/231814 Log: Add a timestamp to the msgbuf output in order to determine when when messages were printed. This can be enabled with the kern.msgbuf_show_timestamp sysctl PR: kern/161553 Reviewed by: avg Submitted by: Arnaud Lacombe Approved by: cperciva MFC after: 1 month 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 Thu Feb 16 05:06:07 2012 (r231813) +++ head/sys/kern/subr_msgbuf.c Thu Feb 16 05:11:35 2012 (r231814) @@ -32,8 +32,10 @@ #include #include #include +#include #include #include +#include /* * Maximum number conversion buffer length: uintmax_t in base 2, plus <> @@ -47,6 +49,14 @@ static u_int msgbuf_cksum(struct msgbuf *mbp); /* + * + */ +static int msgbuf_show_timestamp = 0; +SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW | CTLFLAG_TUN, + &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf"); +TUNABLE_INT("kern.msgbuf_show_timestamp", &msgbuf_show_timestamp); + +/* * Initialize a message buffer of the specified size at the specified * location. This also zeros the buffer area. */ @@ -60,7 +70,7 @@ msgbuf_init(struct msgbuf *mbp, void *pt msgbuf_clear(mbp); mbp->msg_magic = MSG_MAGIC; mbp->msg_lastpri = -1; - mbp->msg_needsnl = 0; + mbp->msg_flags = 0; bzero(&mbp->msg_lock, sizeof(mbp->msg_lock)); mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN); } @@ -95,7 +105,7 @@ msgbuf_reinit(struct msgbuf *mbp, void * mbp->msg_lastpri = -1; /* Assume that the old message buffer didn't end in a newline. */ - mbp->msg_needsnl = 1; + mbp->msg_flags |= MSGBUF_NEEDNL; bzero(&mbp->msg_lock, sizeof(mbp->msg_lock)); mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN); } @@ -134,7 +144,7 @@ msgbuf_getcount(struct msgbuf *mbp) * The caller should hold the message buffer spinlock. */ static inline void -msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c) +__msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c) { u_int pos; @@ -149,6 +159,33 @@ msgbuf_do_addchar(struct msgbuf *mbp, u_ *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. */ @@ -207,10 +244,10 @@ msgbuf_addstr(struct msgbuf *mbp, int pr * did not end with a newline. If that is the case, we need to * insert a newline before this string. */ - if (mbp->msg_lastpri != pri && mbp->msg_needsnl != 0) { + if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) { msgbuf_do_addchar(mbp, &seq, '\n'); - mbp->msg_needsnl = 0; + mbp->msg_flags &= ~MSGBUF_NEEDNL; } for (i = 0; i < len; i++) { @@ -219,7 +256,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pr * (and therefore prefix_len != 0), then we need a priority * prefix for this line. */ - if (mbp->msg_needsnl == 0 && prefix_len != 0) { + if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) { int j; for (j = 0; j < prefix_len; j++) @@ -242,9 +279,9 @@ msgbuf_addstr(struct msgbuf *mbp, int pr * we need to insert a new prefix or insert a newline later. */ if (str[i] == '\n') - mbp->msg_needsnl = 0; + mbp->msg_flags &= ~MSGBUF_NEEDNL; else - mbp->msg_needsnl = 1; + mbp->msg_flags |= MSGBUF_NEEDNL; msgbuf_do_addchar(mbp, &seq, str[i]); } Modified: head/sys/sys/msgbuf.h ============================================================================== --- head/sys/sys/msgbuf.h Thu Feb 16 05:06:07 2012 (r231813) +++ head/sys/sys/msgbuf.h Thu Feb 16 05:11:35 2012 (r231814) @@ -46,7 +46,9 @@ struct msgbuf { u_int msg_cksum; /* checksum of contents */ u_int msg_seqmod; /* range for sequence numbers */ int msg_lastpri; /* saved priority value */ - int msg_needsnl; /* set when newline needed */ + u_int msg_flags; +#define MSGBUF_NEEDNL 0x01 /* set when newline needed */ +#define MSGBUF_NEXT_NEW_LINE 0x02 struct mtx msg_lock; /* mutex to protect the buffer */ };