Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1KLK2U-00028a-Bb for mharc-grub-devel@gnu.org; Tue, 22 Jul 2008 11:44:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KLK2S-00026m-Hg for grub-devel@gnu.org; Tue, 22 Jul 2008 11:44:32 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KLK2R-000254-6Y for grub-devel@gnu.org; Tue, 22 Jul 2008 11:44:32 -0400 Received: from [199.232.76.173] (port=50291 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KLK2Q-00024s-Sz for grub-devel@gnu.org; Tue, 22 Jul 2008 11:44:30 -0400 Received: from gateway15.websitewelcome.com ([69.93.179.23]:42927) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1KLK2Q-0000xh-AA for grub-devel@gnu.org; Tue, 22 Jul 2008 11:44:30 -0400 Received: (qmail 30118 invoked from network); 22 Jul 2008 15:50:02 -0000 Received: from gator297.hostgator.com (74.53.228.114) by gateway15.websitewelcome.com with SMTP; 22 Jul 2008 15:50:02 -0000 Received: from c-67-185-142-228.hsd1.wa.comcast.net ([67.185.142.228]:56862 helo=localhost) by gator297.hostgator.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1KLK2L-0004vQ-No for grub-devel@gnu.org; Tue, 22 Jul 2008 10:44:25 -0500 Date: Tue, 22 Jul 2008 08:43:47 -0700 From: Colin D Bennett To: grub-devel@gnu.org Message-ID: <20080722084347.53863920@gibibit.com> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.12.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/cjNd_ziiY27fcvorb1_6liJ"; protocol="application/pgp-signature"; micalg=PGP-SHA1 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator297.hostgator.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - gibibit.com X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 3) Subject: [PATCH] File readahead buffering X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Jul 2008 15:44:32 -0000 --Sig_/cjNd_ziiY27fcvorb1_6liJ Content-Type: multipart/mixed; boundary="MP_/OPzjei1.__bOEDwpWZfkVug" --MP_/OPzjei1.__bOEDwpWZfkVug Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This patch speeds up loading a TGA image on my test system from 29 seconds to approximately 1 second. I noticed that on my 1 GHz test system running from an IDE CompactFlash drive, loading a certain TGA image in GRUB takes about 29 seconds. This seems pretty outrageous, so after looking at the code I hacked the TGA reader code to read the entire TGA image file into memory with a single grub_file_read() call. This made it much faster. Therefore, I concluded that the problem was that each of the one- to four-byte reads done by the TGA reader was causing an actual device read. It seems that it is the file subsystem's responsibility to do file buffering, so I implemented it in `kern/file.c'. It turns out that when booting GRUB from IDE, if file buffering is used, GRUB hangs right after the "GRUB" message, early in the boot process. So I added a flag that allows grub_main() to enable buffering when it is safe to do so. It always worked file from an ISO image (generated with grub-mkrescue) in VMware and QEMU, but when I actually installed GRUB to my CompactFlash drive and booted it, it hung after "GRUB" if file buffering was enabled at the start. It may be poor style to expose `grub_file_buffering_enabled' as a global variable in file.h, but after I refactored it as a function, I wondered whether the overhead of creating a whole new function as a 'setter' was wasteful since it would be called only once. Please advise if you prefer it as a function, or if there is a better solution. Regards, Colin --MP_/OPzjei1.__bOEDwpWZfkVug Content-Type: text/x-patch; name=file-buffering.patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=file-buffering.patch =3D=3D=3D modified file 'ChangeLog' --- ChangeLog 2008-07-21 09:40:01 +0000 +++ ChangeLog 2008-07-22 15:42:00 +0000 @@ -1,3 +1,17 @@ +2008-07-22 Colin D Bennett + + * include/grub/file.h (grub_file_buffering_enabled): New global + variable. + + * kern/file.c (BUFFER_SIZE): New constant. + (grub_file_buffering_enabled): New global variable. + (grub_file_open): Allocate file readahead buffer. + (grub_file_close): Free file readahead buffer. + (grub_file_read): Use file readahead buffer. + + * kern/main.c (grub_main): Enable file readahead buffering when it is + safe to do so. + 2008-07-21 Bean =20 * kern/i386/pc/startup.S (gate_a20_try_bios): Change test order for =3D=3D=3D modified file 'include/grub/file.h' --- include/grub/file.h 2007-08-02 17:40:37 +0000 +++ include/grub/file.h 2008-07-22 15:23:31 +0000 @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * Copyright (C) 2002,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +39,19 @@ /* The file size. */ grub_off_t size; =20 + /* The file offset when `buffer' starts, or -1 to indicate that + buffer has no data. */ + grub_off_t buffer_offset; + + /* The number of bytes in `buffer'. */ + grub_size_t buffer_length; + + /* The number of bytes allocated for `buffer'. */ + grub_size_t buffer_capacity; + + /* Readahead buffer. */ + char *buffer; + /* Filesystem-specific data. */ void *data; =20 @@ -48,6 +61,11 @@ }; typedef struct grub_file *grub_file_t; =20 +/* Enable file readahead buffering if nonzero. Buffering is disabled by + default, since it seems to cause problems during the early GRUB boot + process. The grub_main function enables it. */ +extern int grub_file_buffering_enabled; + /* Get a device name from NAME. */ char *EXPORT_FUNC(grub_file_get_device_name) (const char *name); =20 =3D=3D=3D modified file 'kern/file.c' --- kern/file.c 2008-01-25 20:57:40 +0000 +++ kern/file.c 2008-07-22 15:23:31 +0000 @@ -1,7 +1,7 @@ /* file.c - file I/O functions */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2006,2007 Free Software Foundation, Inc. + * Copyright (C) 2002,2006,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,12 @@ #include #include =20 +/* File readahead buffer size in bytes. */ +#define BUFFER_SIZE 1024 + +/* Use readahead buffering for file reads? */ +int grub_file_buffering_enabled =3D 0; + /* Get the device part of the filename NAME. It is enclosed by parentheses= . */ char * grub_file_get_device_name (const char *name) @@ -81,6 +87,10 @@ =20 file->device =3D device; file->offset =3D 0; + file->buffer_offset =3D -1; + file->buffer_length =3D 0; + file->buffer_capacity =3D 0; + file->buffer =3D 0; file->data =3D 0; file->read_hook =3D 0; =20 @@ -97,6 +107,13 @@ if ((file->fs->open) (file, file_name) !=3D GRUB_ERR_NONE) goto fail; =20 + if (grub_file_buffering_enabled) + { + file->buffer =3D grub_malloc (BUFFER_SIZE); + if (file->buffer) + file->buffer_capacity =3D BUFFER_SIZE; + } + return file; =20 fail: @@ -110,6 +127,15 @@ return 0; } =20 +static inline grub_size_t +grub_min (grub_size_t a, grub_size_t b) +{ + if (a < b) + return a; + else + return b; +} + grub_ssize_t grub_file_read (grub_file_t file, char *buf, grub_size_t len) { @@ -124,10 +150,52 @@ =20 if (len =3D=3D 0) return 0; - =20 - res =3D (file->fs->read) (file, buf, len); - if (res > 0) - file->offset +=3D res; + + if (grub_file_buffering_enabled && file->buffer) + { + res =3D 0; /* The while loop uses res to accumulate the # bytes re= ad. */ + while (len) + { + /* If the start of the chunk requested is not in the buffer, + then fill the buffer starting at the requested offset. */ + if (file->buffer_offset =3D=3D -1 + || file->offset < file->buffer_offset + || file->offset >=3D file->buffer_offset + file->buffer_leng= th) + { + grub_ssize_t res2; + + /* Read into the buffer, and then use that data. */ + file->buffer_offset =3D -1; + file->buffer_length =3D grub_min (file->buffer_capacity, + file->size - file->offset); + res2 =3D (file->fs->read) (file, file->buffer, file->buffer_= length); + if (res2 > 0) + { + file->buffer_length =3D res2; + file->buffer_offset =3D file->offset; + } + else + return res; + } + + /* Use as much buffered data as possible. */ + grub_size_t bufstart =3D file->offset - file->buffer_offset; + grub_size_t buflen =3D len; + if (bufstart + len > file->buffer_length) + buflen =3D file->buffer_length - bufstart; + grub_memcpy (buf, file->buffer + bufstart, buflen); + buf +=3D buflen; + res +=3D buflen; + len -=3D buflen; + file->offset +=3D buflen; + } + } + else + { + res =3D (file->fs->read) (file, buf, len); + if (res > 0) + file->offset +=3D res; + } =20 return res; } @@ -135,6 +203,9 @@ grub_err_t grub_file_close (grub_file_t file) { + grub_free (file->buffer); + file->buffer =3D 0; + if (file->fs->close) (file->fs->close) (file); =20 =3D=3D=3D modified file 'kern/main.c' --- kern/main.c 2008-06-19 19:08:57 +0000 +++ kern/main.c 2008-07-22 15:23:31 +0000 @@ -128,6 +128,9 @@ grub_env_export ("prefix"); grub_set_root_dev (); =20 + /* Enable file readahead buffering, now that it is safe to do so. */ + grub_file_buffering_enabled =3D 1; + /* Load the normal mode module. */ grub_load_normal_mode (); =20 --MP_/OPzjei1.__bOEDwpWZfkVug-- --Sig_/cjNd_ziiY27fcvorb1_6liJ Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkiGADcACgkQokx8fzcGbYcVSACdHMucA8n/+nh7cuAB2NcACJpi dfYAnREXzJC3YO2FjE34x6JNkjggfXUw =HCfw -----END PGP SIGNATURE----- --Sig_/cjNd_ziiY27fcvorb1_6liJ--