aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/subr_msgbuf.c
diff options
context:
space:
mode:
authorEitan Adler <eadler@FreeBSD.org>2012-02-16 05:11:35 +0000
committerEitan Adler <eadler@FreeBSD.org>2012-02-16 05:11:35 +0000
commitf17a6f1b17ee3c02fa804dd88de4fe9cc05a8614 (patch)
tree1c262b124e009fd2461ab6adfbc384b55e424b8f /sys/kern/subr_msgbuf.c
parent9d4261b7957efd4772078b059fb91b3478093595 (diff)
downloadsrc-f17a6f1b17ee3c02fa804dd88de4fe9cc05a8614.tar.gz
src-f17a6f1b17ee3c02fa804dd88de4fe9cc05a8614.zip
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
Notes
Notes: svn path=/head/; revision=231814
Diffstat (limited to 'sys/kern/subr_msgbuf.c')
-rw-r--r--sys/kern/subr_msgbuf.c53
1 files changed, 45 insertions, 8 deletions
diff --git a/sys/kern/subr_msgbuf.c b/sys/kern/subr_msgbuf.c
index cd9c55149775..2d141e36c6fa 100644
--- a/sys/kern/subr_msgbuf.c
+++ b/sys/kern/subr_msgbuf.c
@@ -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 *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]);
}