Yuji Shimada
2008-Dec-03 04:10 UTC
[Xen-devel] [PATCH] [IOEMU] Add date, time and pid on qemu-dm''s log.
The qemu-dm''s log message doesn''t have date, time, and pid. It
is
difficult to find out the message we want.
When we use guest domain for several months, date, time and pid are
necessary to analyse the log file.
This patch solves the problem without changing the codes which write
messages.
The method is like below:
qemu-dm creates a pipe and starts new thread for logging. qemu-dm
associates a stream with the pipe, and sets the stream to existing
"logfile" variable, stdout and stderr. When qemu-dm writes a message
to them, the thread receives it through the pipe and outputs it with
date, time and pid to the actual log file.
The log is modified like below:
(now)
domid: 6
qemu: the number of cpus is 1
...
(modify)
[2008-12-02 17:00:06 22623] domid: 6
[2008-12-02 17:00:06 22623] qemu: the number of cpus is 1
[2008-12-02 17:00:06 22623] ...
This patch is not available for stubdom. So I will send patch for
stubdom.
Thanks,
--
Yuji Shimada
Signed-off-by: Yuji Shimada <shimada-yxb@necst.nec.co.jp>
diff --git a/vl.c b/vl.c
index 182346a..55d323c 100644
--- a/vl.c
+++ b/vl.c
@@ -132,6 +132,10 @@ int inet_aton(const char *cp, struct in_addr *ia);
#define main qemu_main
#endif /* CONFIG_COCOA */
+#ifndef CONFIG_STUBDOM
+#include <pthread.h>
+#endif
+
#include "disas.h"
#include "exec-all.h"
@@ -7829,6 +7833,107 @@ static BOOL WINAPI qemu_ctrl_handler(DWORD type)
}
#endif
+#ifndef CONFIG_STUBDOM
+FILE *pipe_read, *org_stderr;
+pthread_t log_hd;
+#endif
+
+#ifndef CONFIG_STUBDOM
+#define LOGBUFSIZ 256
+
+void *logging_thread(void *arg)
+{
+ time_t current_tm;
+ struct tm time_str;
+ char log_buffer[LOGBUFSIZ];
+
+ while (fgets(log_buffer, LOGBUFSIZ, pipe_read) != NULL) {
+ time(¤t_tm);
+ localtime_r(¤t_tm, &time_str);
+ fprintf(org_stderr, "[%4d-%.2d-%.2d %.2d:%.2d:%.2d %4d] %s",
+ time_str.tm_year+1900, time_str.tm_mon+1, time_str.tm_mday,
+ time_str.tm_hour, time_str.tm_min, time_str.tm_sec,
+ getpid(), log_buffer);
+ fflush(org_stderr);
+ }
+ fclose(pipe_read);
+ pthread_exit(0);
+ return NULL;
+}
+
+void stop_logging_thread(void)
+{
+ fclose(logfile);
+ logfile = NULL;
+ fclose(stdin);
+ fclose(stderr);
+ pthread_join(log_hd, NULL);
+}
+
+void start_logging_thread(void)
+{
+ pthread_attr_t log_attr;
+ int log_pipes[2];
+ int err;
+ int org_stderr_fd;
+
+ org_stderr_fd = dup(2);
+ if (org_stderr_fd == -1) {
+ fprintf(stderr, "Failed to duplicate stderr.\n");
+ exit(1);
+ }
+
+ org_stderr = fdopen(org_stderr_fd, "w");
+ if (org_stderr == NULL) {
+ fprintf(stderr,
+ "Failed to associate stream with stderr.\n");
+ exit(1);
+ }
+
+ if (pipe(log_pipes) != 0) {
+ fprintf(stderr, "Failed to create pipes.\n");
+ exit(1);
+ }
+
+ pipe_read = fdopen(log_pipes[0], "r");
+ if (pipe_read == NULL) {
+ fprintf(stderr, "Failed to associate stream with reading
pipe.\n");
+ exit(1);
+ }
+
+ logfile = fdopen(log_pipes[1], "w");
+ if (logfile == NULL) {
+ fprintf(stderr, "Failed to associate stream with writing
pipe.\n");
+ exit(1);
+ }
+
+ pthread_attr_init(&log_attr);
+ err = pthread_create(&log_hd, &log_attr, logging_thread, (void
*)NULL);
+ if (err != 0) {
+ fprintf(stderr, "Failed to create thread for logging.\n");
+ exit(1);
+ }
+
+ err = atexit(stop_logging_thread);
+ if (err != 0) {
+ fprintf(stderr, "Failed to set stop_logging_thread
function.\n");
+ exit(1);
+ }
+
+#ifndef CONFIG_SOFTMMU
+ /* must avoid mmap() usage of glibc by setting a buffer "by hand"
*/
+ {
+ static uint8_t logfile_buf[4096];
+ setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
+ }
+#else
+ setvbuf(logfile, NULL, _IOLBF, 0);
+#endif
+ dup2(fileno(logfile), 1);
+ dup2(fileno(logfile), 2);
+}
+#endif /* !CONFIG_STUBDOM */
+
#define MAX_NET_CLIENTS 32
int main(int argc, char **argv)
@@ -7868,7 +7973,11 @@ int main(int argc, char **argv)
const char *pid_file = NULL;
VLANState *vlan;
+#ifdef CONFIG_STUBDOM
logfile = stderr; /* initial value */
+#else
+ start_logging_thread();
+#endif /* CONFIG_STUBDOM */
#if !defined(__sun__) && !defined(CONFIG_STUBDOM)
/* Maximise rlimits. Needed where default constraints are tight (*BSD). */
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel
Ian Jackson
2008-Dec-03 12:13 UTC
Re: [Xen-devel] [PATCH] [IOEMU] Add date, time and pid on qemu-dm''s log.
Yuji Shimada writes ("[Xen-devel] [PATCH] [IOEMU] Add date, time and pid
on qemu-dm''s log."):> The qemu-dm''s log message doesn''t have date, time, and
pid. It is
> difficult to find out the message we want.
I agree that adding timestamps to the messages would be a good idea.
But, I think that this kind of feature is best addressed in qemu
upstream. I don''t think we want to carry this patch in our tree.
Also I have some doubts about whether this is really the right
approach. Most programs deal with this by inventing new logging
functions rather than new processes.
If you really can''t live without the timestamped messages then I think
xend should do the plumbing.
Ian.
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel