Date: Thu, 16 Feb 2012 05:11:35 +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: r231814 - in head/sys: kern sys Message-ID: <201202160511.q1G5BZNk099785@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
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 <lacombar@gmail.com> 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 <sys/param.h> #include <sys/systm.h> #include <sys/lock.h> +#include <sys/kernel.h> #include <sys/mutex.h> #include <sys/msgbuf.h> +#include <sys/sysctl.h> /* * 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 */ };
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201202160511.q1G5BZNk099785>