[Patches] patch: add command-line option to control stdout/stderr buffering

Jonathan Thornburg jthorn at aei.mpg.de
Sun May 28 08:35:00 CDT 2006


Hi,

When debugging Cactus code, it's often a problem for me that messages
written to stdout/stderr may be buffered in the stdio system, and so
appear out-of-sequence with respect to each other and with respect to
the actual execution of my code.

In message
  http://www.cactuscode.org/old/pipermail/developers/2006-May/001863.html
I proposed that we should deal with this problem by adding a new flesh
flesh command-line option to control stdout/stderr buffering.

The attached patch implements this:
-b, -buffering [u|l]                : Sets standard output and standard error
                                      to be unbuffered or to use line buffering.

ciao,

-- 
-- Jonathan Thornburg <jthorn at aei.mpg.de>      
   Max-Planck-Institut fuer Gravitationsphysik (Albert-Einstein-Institut),
   Golm, Germany, "Old Europe"     http://www.aei.mpg.de/~jthorn/home.html      
   "Washing one's hands of the conflict between the powerful and the
    powerless means to side with the powerful, not to be neutral."
                                      -- quote by Freire / poster by Oxfam
-------------- next part --------------
Index: CommandLine.c
===================================================================
RCS file: /cactusdevcvs/Cactus/src/main/CommandLine.c,v
retrieving revision 1.59
diff -u -r1.59 CommandLine.c
--- CommandLine.c	11 May 2006 13:55:51 -0000	1.59
+++ CommandLine.c	28 May 2006 13:19:52 -0000
@@ -63,7 +63,7 @@
  ********************************************************************/
 #define CACTUS_COMMANDLINE_OPTIONS                                      \
         "[-h] [-O] [-o paramname] [-L n] [-W n] [-E n] [-r[o|e|oe|eo]] " \
-        "[-S] [-T] [-t name] [-parameter-level <level>] [-v] "          \
+        "[-b[u|l]] [-S] [-T] [-t name] [-parameter-level <level>] [-v] " \
         "<parameter_file_name>"
 
 
@@ -458,6 +458,39 @@
 
 
  /*@@
+   @routine    CCTKi_CommandLineuffering
+   @date       Sun May 28 13:00:02 CEST 2006
+   @author     Jonathan Thornburg
+   @desc
+               Set stdout and stderr to be unbuffered or line-buffered.
+   @enddesc
+
+   @var        argument
+   @vdesc      option argument
+   @vtype      const char *
+   @vio        in
+   @endvar
+@@*/
+void CCTKi_CommandLineBuffering(const char* const argument)
+{
+  if      (!argument || (strcmp(argument,"u") == 0))    /* set unbuffered */
+  {
+    setvbuf(stdout, NULL, _IONBF, 0);
+    setvbuf(stderr, NULL, _IONBF, 0);
+  }
+  else if (strcmp(argument, "l") == 0)                  /* set line-buffered */
+  {
+    setvbuf(stdout, NULL, _IOLBF, 0);
+    setvbuf(stderr, NULL, _IOLBF, 0);
+  }
+  else                                  /* illegal value for this option */
+  {
+    CCTKi_CommandLineUsage();                                     /*NOTREACHED*/
+  }
+}
+
+
+ /*@@
    @routine    CCTKi_CommandLinePrintSchedule
    @date       2005-06-10
    @author     Erik Schnetter
@@ -552,6 +585,8 @@
     "-E, -error-level <n>                : Sets the error level to n.\n"
     "-r, -redirect [o|e|oe|eo]           : Redirects standard output and/or standard\n"
     "                                      error to files.\n"
+    "-b, -buffering [u|l]                : Sets standard output and standard error\n"
+    "                                      to be unbuffered or to use line buffering.\n"
     "-S, -print-schedule                 : Print the schedule tree, then exit.\n"
     "-T, -list-thorns                    : Lists the compiled-in thorns.\n"
     "-t, -test-thorn-compiled <name>     : Tests for the presence of thorn <name>.\n"
Index: ProcessCommandLine.c
===================================================================
RCS file: /cactusdevcvs/Cactus/src/main/ProcessCommandLine.c,v
retrieving revision 1.36
diff -u -r1.36 ProcessCommandLine.c
--- ProcessCommandLine.c	11 May 2006 13:55:51 -0000	1.36
+++ ProcessCommandLine.c	28 May 2006 13:19:52 -0000
@@ -58,7 +58,7 @@
    @desc 
    Processes the command line arguments.
    @enddesc 
-   @calls    CCTKi_CommandLineTestThornCompiled CCTKi_CommandLineDescribeAllParameters CCTKi_CommandLineDescribeParameter CCTKi_CommandLineTestParameters CCTKi_CommandLineLoggingLevel CCTKi_CommandLineWarningLevel CCTKi_CommandLineErrorLevel CCTKi_CommandLineParameterLevel CCTKi_CommandLineRedirectStdout CCTKi_CommandLineListThorns() CCTKi_CommandLineVersion() CCTKi_CommandLineHelp
+   @calls    CCTKi_CommandLineTestThornCompiled CCTKi_CommandLineDescribeAllParameters CCTKi_CommandLineDescribeParameter CCTKi_CommandLineTestParameters CCTKi_CommandLineLoggingLevel CCTKi_CommandLineWarningLevel CCTKi_CommandLineErrorLevel CCTKi_CommandLineParameterLevel CCTKi_CommandLineRedirectStdout CCTKiCommandLineSetLineBuf CCTKi_CommandLineListThorns() CCTKi_CommandLineVersion() CCTKi_CommandLineHelp
    @calledby   
    @history 
  
@@ -122,6 +122,7 @@
         {"error-level",             required_argument, NULL, 'E'},
         {"parameter-level",         required_argument, NULL, 256},
         {"redirect",                optional_argument, NULL, 'r'},
+        {"buffering",               optional_argument, NULL, 'b'},
         {"print-schedule",          no_argument,       NULL, 'S'},
         {"list-thorns",             no_argument,       NULL, 'T'},
         {"test-thorn-compiled",     required_argument, NULL, 't'},
@@ -130,7 +131,7 @@
         {0, 0, 0, 0}
       };
       
-      c = getopt_long_only (argc, argv, "hO::o:x::L:W:E:r::STt:vi",
+      c = getopt_long_only (argc, argv, "hO::o:x::L:W:E:r::b::STt:vi",
                             long_options, &option_index);
       if (c == -1)
         break;
@@ -148,6 +149,7 @@
           case 'E': CCTKi_CommandLineErrorLevel(optarg); break;
           case 256: CCTKi_CommandLineParameterLevel(optarg); break;
           case 'r': CCTKi_CommandLineRedirect(optarg); break;
+          case 'b': CCTKi_CommandLineBuffering(optarg); break;
           case 'S': CCTKi_CommandLinePrintSchedule(); break;
           case 'T': CCTKi_CommandLineListThorns(); break;
           case 'v': CCTKi_CommandLineVersion(); break;
-------------- next part --------------
Index: RunningCactus.tex
===================================================================
RCS file: /cactusdevcvs/Cactus/doc/UsersGuide/RunningCactus.tex,v
retrieving revision 1.117
diff -u -r1.117 RunningCactus.tex
--- RunningCactus.tex	21 Feb 2006 16:50:29 -0000	1.117
+++ RunningCactus.tex	28 May 2006 13:32:57 -0000
@@ -1217,6 +1217,8 @@
 \hline
  -r[o|e|oe|eo] & -redirect=[o|e|oe|eo]\\
 \hline
+ -b[u|l] & -buffering=[u|l]\\
+\hline
  -i & -ignore-next \\
 \hline
     & -parameter-level=<\var{level}> \\
@@ -1275,6 +1277,28 @@
 Redirects the standard output (`\texttt{o}') and/or standard error
 (`\texttt{e}') of each processor to a file.  By default
 the standard outputs from processors other than processor 0 are discarded.
+\item [\texttt{-b[ul]} or \texttt{-buffering=[u|l]}]
+Sets standard output and standard error to both be either
+unbuffered (`\texttt{u}') or line-buffered (`\texttt{l}').
+This may be useful for debugging, as it causes the sequence of output
+to more closely match that of internal execution.
+
+By default you get whatever buffering your operating system uses.
+On most Unix systems this is:
+\begin{itemize}
+\item	For things written directly to a terminal, some approximation
+	to line buffering is usually used.  However, even in this case,
+	standard output and standard error may not be synchronous with
+	respect to each other, i.e.\ standard-error output may appear
+	out-of-place with respect to standard-output output.
+\item	For things written to a file (either via shell \texttt{>} redirection,
+	or via the \texttt{-r}/\texttt{-redirect} option), you usually
+	get full buffering (often in filesystem-block-size chunks).
+\end{itemize}
+Note that there are no guarantees whatsoever about synchronization
+of output between different languages.  In particular, output from any of
+C, C++, Fortran~77, and Fortran~90 may appear out-of-sequence with
+respect to output from any other of these languages.
 \item [\texttt{-i} or \texttt{-ignore-next}]
 Causes the next argument on the command line to be ignored.
 \item [\texttt{-parameter-level=<\var{level}>}]


More information about the Patches mailing list