Skip to contentSkip to conversation

LVM2 tools/lvmcmdline.c liblvm/lvm_base.c lib/ ...

PATCH [email protected] 3 participants 3 messages May 7, 2011 – May 9, 2011

lore
Share & tools
lore
#0 lore raw

CVSROOT: /cvs/lvm2
Module name: LVM2
Changes by: mornfall at sourceware.org 2011-05-07 13:50:13

Modified files:

	tools          : lvmcmdline.c 
	liblvm         : lvm_base.c 
	lib/commands   : toolcontext.c toolcontext.h 
	daemons/clvmd  : lvm-functions.c 

Log message:
When glibc needs buffers for line buffering of input and output buffers, it 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.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/tools/lvmcmdline.c.diff?cvsroot=lvm2&r1=1.141&r2=1.142
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/liblvm/lvm_base.c.diff?cvsroot=lvm2&r1=1.20&r2=1.21
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/lib/commands/toolcontext.c.diff?cvsroot=lvm2&r1=1.118&r2=1.119
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/lib/commands/toolcontext.h.diff?cvsroot=lvm2&r1=1.42&r2=1.43
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/clvmd/lvm-functions.c.diff?cvsroot=lvm2&r1=1.115&r2=1.116

tools/lvmcmdline.c +1 -1
@@ -1282,7 +1282,7 @@
 	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];
liblvm/lvm_base.c +1 -1
@@ -34,7 +34,7 @@
 	/* 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;
 
lib/commands/toolcontext.c +29 -1
@@ -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 @@
 
 /* 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 @@
 	/* 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 @@
 	_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();
lib/commands/toolcontext.h +3 -1
@@ -66,6 +66,7 @@
 	const char *kernel_vsn;
 
 	unsigned rand_seed;
+	char *linebuffer;
 	const char *cmd_line;
 	struct command *command;
 	char **argv;
@@ -109,7 +110,8 @@
  * 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);
daemons/clvmd/lvm-functions.c +1 -1
@@ -919,7 +919,7 @@
 	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;
 	}
#1 lore raw

lib/commands/toolcontext.c line 1189

Quoted 1 line · mornfall at sourceware.org
lib/commands/toolcontext.c +1 -0
+	/* Set in/out stream buffering before glibc */

Please add more explanatory comments inline.

I mentioned this on IRC but there's been a trend towards not giving explanations inline which I'd like to see reversed. Patch headers are becoming too long, containing information about the new version of the code which should instead have been placed inline.

Alasdair

#2 lore raw

Hi,

Quoted 9 lines · Alasdair G Kergon

lib/commands/toolcontext.c line 1189

Quoted 1 line · mornfall at sourceware.org
lib/commands/toolcontext.c +1 -0
+	/* Set in/out stream buffering before glibc */

Please add more explanatory comments inline.

I mentioned this on IRC but there's been a trend towards not giving explanations inline which I'd like to see reversed. Patch headers are becoming too long, containing information about the new version of the code which should instead have been placed inline.

I'd delegate this,

Quoted 4 lines
Quoted 1 line

+ Avoid memlock size mismatch by preallocating stdio line buffers.

If it fixed this, can't the temporary warning message suppression be removed now?

and this to Zden?k, since he's the author of the patch in question and might therefore be better qualified to answer. Zden?k, if you'd prefer to not deal with those, just bounce it back to me.

Yours,
Petr

Signature · 1 line
id' Ash = Ash; id' Dust = Dust; id' _ = undefined