]> sourceware.org Git - lvm2.git/commitdiff
When glibc needs buffers for line buffering of input and output buffers, it
authorPetr Rockai <prockai@redhat.com>
Sat, 7 May 2011 13:50:11 +0000 (13:50 +0000)
committerPetr Rockai <prockai@redhat.com>
Sat, 7 May 2011 13:50:11 +0000 (13:50 +0000)
allocates these buffers in such way it adds memory page for each such buffer
and size of unlock memory check will mismatch by 1 or 2 pages.

This happens when we print or read lines without '\n' so these buffers are
used. To avoid this extra allocation, use setvbuf to set these bufffers ahead.

Signed-off-by: Zdenek Kabelac <zkabelac@redhat.com>
Reviewed-by: Milan Broz <mbroz@redhat.com>
Reviewed-by: Petr Rockai <prockai@redhat.com>
daemons/clvmd/lvm-functions.c
lib/commands/toolcontext.c
lib/commands/toolcontext.h
liblvm/lvm_base.c
tools/lvmcmdline.c

index 166cd7f13649c35d0e5eecdd423f19f0f0d28861..8e68ed983bb7610eb16d54f17a3aede6fae45213 100644 (file)
@@ -919,7 +919,7 @@ int init_clvm(int using_gulm, char **argv)
        init_syslog(LOG_DAEMON);
        openlog("clvmd", LOG_PID, LOG_DAEMON);
 
-       if (!(cmd = create_toolcontext(1, NULL))) {
+       if (!(cmd = create_toolcontext(1, NULL, 0))) {
                log_error("Failed to allocate command context");
                return 0;
        }
index 9b3e27f4e7ebf944f7620731765c4b002456dbe1..918362cef692106e98d592357ab478cf38e24d95 100644 (file)
@@ -58,6 +58,8 @@
 #  include <malloc.h>
 #endif
 
+static const size_t linebuffer_size = 4096;
+
 static int _get_env_vars(struct cmd_context *cmd)
 {
        const char *e;
@@ -1148,7 +1150,8 @@ static void _init_globals(struct cmd_context *cmd)
 
 /* Entry point */
 struct cmd_context *create_toolcontext(unsigned is_long_lived,
-                                      const char *system_dir)
+                                      const char *system_dir,
+                                      unsigned set_buffering)
 {
        struct cmd_context *cmd;
 
@@ -1183,6 +1186,22 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
        /* FIXME Make this configurable? */
        reset_lvm_errno(1);
 
+       /* Set in/out stream buffering before glibc */
+       if (set_buffering) {
+               /* Allocate 2 buffers */
+               if (!(cmd->linebuffer = dm_malloc(2 * linebuffer_size))) {
+                       log_error("Failed to allocate line buffer.");
+                       goto out;
+               }
+               if ((setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size) ||
+                    setvbuf(stdout, cmd->linebuffer + linebuffer_size,
+                            _IOLBF, linebuffer_size))) {
+                       log_sys_error("setvbuf", "");
+                       goto out;
+               }
+               /* Buffers are used for lines without '\n' */
+       }
+
        /*
         * Environment variable LVM_SYSTEM_DIR overrides this below.
         */
@@ -1419,6 +1438,15 @@ void destroy_toolcontext(struct cmd_context *cmd)
        _destroy_tag_configs(cmd);
        if (cmd->libmem)
                dm_pool_destroy(cmd->libmem);
+
+       if (cmd->linebuffer) {
+               /* Reset stream buffering to defaults */
+               setlinebuf(stdin);
+               fflush(stdout);
+               setlinebuf(stdout);
+               dm_free(cmd->linebuffer);
+       }
+
        dm_free(cmd);
 
        release_log_memory();
index 4628c7c2563935ccc007c2b805f4ab5a5a12afb8..1c3368e6949ebb2639704d2d1be05b6c66cacd3d 100644 (file)
@@ -66,6 +66,7 @@ struct cmd_context {
        const char *kernel_vsn;
 
        unsigned rand_seed;
+       char *linebuffer;
        const char *cmd_line;
        struct command *command;
        char **argv;
@@ -109,7 +110,8 @@ struct cmd_context {
  * The environment variable LVM_SYSTEM_DIR always takes precedence.
  */
 struct cmd_context *create_toolcontext(unsigned is_long_lived,
-                                      const char *system_dir);
+                                      const char *system_dir,
+                                      unsigned set_buffering);
 void destroy_toolcontext(struct cmd_context *cmd);
 int refresh_toolcontext(struct cmd_context *cmd);
 int refresh_filters(struct cmd_context *cmd);
index 9e1a8ecd87241d81085f0428c2354cc26e694338..f44b786cb12834bed79e4addef182d29cd579bdb 100644 (file)
@@ -34,7 +34,7 @@ lvm_t lvm_init(const char *system_dir)
        /* create context */
        /* FIXME: split create_toolcontext */
        /* FIXME: make all globals configurable */
-       cmd = create_toolcontext(0, system_dir);
+       cmd = create_toolcontext(0, system_dir, 1);
        if (!cmd)
                return NULL;
 
index 197cd1431859d96409e0dfb9e3b6ceabed183d2f..3e13a27d65980b28a1a71d8ba687f98733871f58 100644 (file)
@@ -1282,7 +1282,7 @@ struct cmd_context *init_lvm(void)
        if (!udev_init_library_context())
                stack;
 
-       if (!(cmd = create_toolcontext(0, NULL)))
+       if (!(cmd = create_toolcontext(0, NULL, 1)))
                return_NULL;
 
        _cmdline.arg_props = &_arg_props[0];
This page took 0.045144 seconds and 5 git commands to generate.