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
This commit is contained in:
Eitan Adler 2012-02-16 05:11:35 +00:00
parent 9d4261b795
commit f17a6f1b17
2 changed files with 48 additions and 9 deletions

View File

@ -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 <>
@ -46,6 +48,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 *ptr, int size)
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 *ptr, int size)
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_int *seq, int 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.
*/
@ -207,10 +244,10 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr)
* 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 pri, char *str, int filter_cr)
* (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 pri, char *str, int filter_cr)
* 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]);
}

View File

@ -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 */
};