Skip to contentSkip to conversation

tools: ocaml: fix the logging in the ocaml libxl bindings

PATCH In review David Scott 3 participants 5 messages Mar 23, 2011 – Apr 4, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: ocaml: fix the logging in the ocaml libxl bindings

# HG changeset patch
# User David Scott <[email protected]>
# Date 1300913511 0
# Node ID 67525892e2c6ee27c25438d43699397c5a3e0272 # Parent 3831bd253e02aa0536ed32e936777d026abb955e
tools: ocaml: fix the logging in the ocaml libxl bindings.

We now:
1. ensure the caml_logger fields have sensible initial values
2. adopt the policy that, if the messages won't fit into the buffer then they are dropped and clipped is set to 1. The default buffer size is 2KiB which ought to be large enough unless the logging is very spammy (which is arguably a problem in itself)

tools/ocaml/libs/xl/xl_stubs.c +29 -4
diff -r 3831bd253e02 -r 67525892e2c6 tools/ocaml/libs/xl/xl_stubs.c
@@ -28,12 +28,21 @@
 
 #include "libxl.h"
 
+#define MAX_LOG_LEN 2048
+
 struct caml_logger {
 	struct xentoollog_logger logger;
 	int log_offset;
-	char log_buf[2048];
+	char log_buf[MAX_LOG_LEN];
+	int clipped; /* Set to 1 if some log messages won't fit in the buffer */
 };
 
+void log_create(struct caml_logger *logger)
+{
+	logger->log_offset = 0;
+	logger->clipped = 0;
+}	
+
 typedef struct caml_gc {
 	int offset;
 	void *ptrs[64];
@@ -43,16 +52,32 @@
                   int errnoval, const char *context, const char *format, va_list al)
 {
 	struct caml_logger *ologger = (struct caml_logger *) logger;
+	int remaining, written;
 
-	ologger->log_offset += vsnprintf(ologger->log_buf + ologger->log_offset,
-	                                 2048 - ologger->log_offset, format, al);
+	if (ologger->clipped)
+		return; /* if we've already started clipping, drop everything */
+
+	/* If the message doesn't fit in the buffer, we drop it and set clipped to 1.
+	   This shouldn't happen unless the logging is very spammy. */
+
+	remaining = sizeof(ologger->log_buf) - ologger->log_offset;
+	written = vsnprintf(ologger->log_buf + ologger->log_offset, remaining, format, al);
+	if (written < 0)
+		return; /* nothing really we can do */
+	if (written < remaining){
+		ologger->log_offset += written;
+		return;
+	}
+	/* buffer isn't big enough */
+	ologger->clipped = 1;
 }
 
 void log_destroy(struct xentoollog_logger *logger)
 {
 }
 
-#define INIT_STRUCT() libxl_ctx ctx; struct caml_logger lg; struct caml_gc gc; gc.offset = 0;
+
+#define INIT_STRUCT() libxl_ctx ctx; struct caml_logger lg; struct caml_gc gc; gc.offset = 0; log_create(&lg);
 
 #define INIT_CTX()  \
 	lg.logger.vmessage = log_vmessage; \
#1 lore raw

David Scott writes ("[Xen-devel] [PATCH] tools: ocaml: fix the logging in the ocaml libxl bindings"):

Quoted 8 lines

tools: ocaml: fix the logging in the ocaml libxl bindings.

We now:
1. ensure the caml_logger fields have sensible initial values
2. adopt the policy that, if the messages won't fit into the buffer then they are dropped and clipped is set to 1. The default buffer size is 2KiB which ought to be large enough unless the logging is very spammy (which is arguably a problem in itself)

Thanks, but:

* Can you make a version where the line lengths do not exceed 75 ? That allows for a patch +/- column, plus a couple of levels of quoting, to fit in an 80-column window.

* Now that I read this code I'm very confused. Why does log_vmessage not actually output the message anywhere ? Looking at the code, the buffer is printed out only if the call fails (I assume that's what failwith_xl is). That's not right. What about debugging output ?

There is no guarantee that the logger will be called to report any particular number of messages if a libxl call fails. Specifically, it may output zero error messages, or several one, before returning a failure code. And it will frequently output a number of informational messages.

Ian.

author
#2 lore raw

Hi,

Quoted 3 lines

David Scott writes ("[Xen-devel] [PATCH] tools: ocaml: fix the logging in the ocaml libxl bindings"):

Quoted 1 line

tools: ocaml: fix the logging in the ocaml libxl bindings.

[ snip ]

Ian Jackson replied:

Quoted 5 lines

Thanks, but:

* Can you make a version where the line lengths do not exceed 75 ? That allows for a patch +/- column, plus a couple of levels of quoting, to fit in an 80-column window.

Will do. I think I've got into a habit of writing very long lines. I blame my poor choice in editors and email clients :-)

Quoted 11 lines

* Now that I read this code I'm very confused. Why does log_vmessage not actually output the message anywhere ? Looking at the code, the buffer is printed out only if the call fails (I assume that's what failwith_xl is). That's not right. What about debugging output ?

There is no guarantee that the logger will be called to report any particular number of messages if a libxl call fails. Specifically, it may output zero error messages, or several one, before returning a failure code. And it will frequently output a number of informational messages.

That is indeed the current behaviour -- most log messages end up dropped, only those which are in the buffer when the libxl call fails get bubbled up to the user in an exception. The main difference after my patch is that the code nolonger segfaults after calling (IIRC) gettopologyinfo and hopefully won't blow its buffer if the logging ever gets spammy :-)

If you recommend changing the policy to always record/handle all log messages then that sounds fine to me. From the point of view of the ocaml code I can think of the following options (Vincent: do you have any advice?):

1. add the notion of a per-call context to the ocaml and associate logging callbacks with this. The ocaml code can then do whatever it likes on a per-call basis.

2. allow the ocaml to set a global logging callback which would handle logging from all ocaml xl calls in the same process

3. allow the ocaml to set some syslog logging parameters so all ocaml xl calls would just log via syslog

I don't think any of these are particularly complicated. Anyone have any thoughts / recommendations / preferences / suggestions?

Cheers,
Dave

#3 lore raw
Quoted 8 lines · Dave Scott

Hi,

Quoted 3 lines

David Scott writes ("[Xen-devel] [PATCH] tools: ocaml: fix the logging in the ocaml libxl bindings"):

Quoted 1 line

tools: ocaml: fix the logging in the ocaml libxl bindings.

[ snip ]

Ian Jackson replied:

Quoted 8 lines
Quoted 5 lines

Thanks, but:

* Can you make a version where the line lengths do not exceed 75 ? That allows for a patch +/- column, plus a couple of levels of quoting, to fit in an 80-column window.

Will do. I think I've got into a habit of writing very long lines. I blame my poor choice in editors and email clients :-)

I blame your massive widescreen monitor ;-)

Quoted 28 lines
Quoted 7 lines

* Now that I read this code I'm very confused. Why does log_vmessage not actually output the message anywhere ? Looking at the code, the buffer is printed out only if the call fails (I assume that's what failwith_xl is). That's not right. What about debugging output ?

Quoted 5 lines

There is no guarantee that the logger will be called to report any particular number of messages if a libxl call fails. Specifically, it may output zero error messages, or several one, before returning a failure code. And it will frequently output a number of informational messages.

That is indeed the current behaviour -- most log messages end up dropped, only those which are in the buffer when the libxl call fails get bubbled up to the user in an exception. The main difference after my patch is that the code nolonger segfaults after calling (IIRC) gettopologyinfo and hopefully won't blow its buffer if the logging ever gets spammy :-)

If you recommend changing the policy to always record/handle all log messages then that sounds fine to me. From the point of view of the ocaml code I can think of the following options (Vincent: do you have any advice?):

1. add the notion of a per-call context to the ocaml and associate logging callbacks with this. The ocaml code can then do whatever it likes on a per-call basis.

Quoted 8 lines

2. allow the ocaml to set a global logging callback which would handle logging from all ocaml xl calls in the same process

3. allow the ocaml to set some syslog logging parameters so all ocaml xl calls would just log via syslog

I don't think any of these are particularly complicated. Anyone have any thoughts / recommendations / preferences / suggestions?

Somewhat of an aside but what do you think of having libxl contexts with longer lifespans than the current one per libxl call model? e.g. you would open a context in ocaml code, make a series of calls and then close it? Or perhaps even keep it for the lifetime of a domain or a thread of processing context etc?

Ian.

Quoted 9 lines

Cheers,
Dave

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw
In reply to Dave Scott

Dave Scott writes ("RE: [Xen-devel] [PATCH] tools: ocaml: fix the logging in the ocaml libxl bindings"):

Quoted 5 lines

That is indeed the current behaviour -- most log messages end up dropped, only those which are in the buffer when the libxl call fails get bubbled up to the user in an exception. The main difference after my patch is that the code nolonger segfaults after calling (IIRC) gettopologyinfo and hopefully won't blow its buffer if the logging ever gets spammy :-)

Right :-). Fair enough.

Quoted 4 lines

If you recommend changing the policy to always record/handle all log messages then that sounds fine to me. From the point of view of the ocaml code I can think of the following options (Vincent: do you have any advice?):

I think the ocaml bindings to libxl should expose the actual interface of libxl, without messing with the semantics of errors in this way.

I don't know which of these:

Quoted 9 lines

1. add the notion of a per-call context to the ocaml and associate logging callbacks with this. The ocaml code can then do whatever it likes on a per-call basis.

2. allow the ocaml to set a global logging callback which would handle logging from all ocaml xl calls in the same process

3. allow the ocaml to set some syslog logging parameters so all ocaml xl calls would just log via syslog

... would be more sensible from an ocaml point of view.

Ian.