bindings to flush the page cache of a file after indexing, and other neat things



Hey, Jon.

Attached patch creates Utils/FileAdvise.cs, which wraps posix_fadvise(2)
and allows us to better control the VM.  For example, we can now flush
the page cache of a file after indexing to prevent a full crawl from
wrecking havoc on the system.

I wrap everything with a simple little C# interface:

	static public void FlushCache (FileStream file)
Evict all data from the page cache pertaining to the specified open file
stream.  We would want to do this during a filesystem crawl, after
indexing but before closing the file.

	static public void PreLoad (FileStream file)
The opposite of FlushCache ().  Preload into the page cache as much of
the given file as makes sense.  This preloading is done asynchronously,
in the background.

	static public void IncreaseReadAhead (FileStream file)
	static public void DisableReadAhead (FileStream file)
	static public void NormalReadAhead (FileStream file)
These functions manage the kernel read-ahead of the file from the
backing store.  The first doubles the amount of data that the kernel
will read-ahead, the second turns off read-ahead altogether, and the
third returns the read-ahead amount to the default value.  Calling the
first from a filter, after open but before indexing, which reads the
entire file sequentially would be advantageous.   Calling the second
from a filter, after open but before indexing, might be useful if a
filter reads a file fairly randomly with many seeks.

Seems to work for me in a test driver, but I have not integrated it with
Beagle.

	Robert Love

Add Mono bindings for posix_fadvise(2) so we can f.e. dump the page cache of a
file after indexing.

 FileAdvise.cs |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Makefile.am   |    1
 2 files changed, 89 insertions(+)


Index: Util/FileAdvise.cs
===================================================================
RCS file: Util/FileAdvise.cs
diff -N Util/FileAdvise.cs
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Util/FileAdvise.cs	13 Oct 2004 22:17:37 -0000
@@ -0,0 +1,88 @@
+//
+// FileAdvise.cs
+//
+// Copyright (C) 2004 Novell, Inc.
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+// FIXME: This is not portable to Win32
+
+using System;
+using System.IO;
+using System.Text;
+using System.Runtime.InteropServices;
+using Mono.Posix;
+
+namespace Beagle.Util {
+
+	public class FileAdvise {
+
+		// FIXME: On 64-bit architectures, we need to use "long" not "int" here
+		[DllImport ("libc", SetLastError=true)]
+		static extern int posix_fadvise (int fd, int offset, int len, int advice);
+
+		// The following are from /usr/include/linux/fadvise.h and will not change
+		private const int AdviseNormal = 0;	// POSIX_FADV_NORMAL
+		private const int AdviseRandom = 1;	// POSIX_FADV_RANDOM
+		private const int AdviseSequential = 2;	// POSIX_FADV_SEQUENTIAL
+		private const int AdviseWillNeed = 3;	// POSIX_FADV_WILLNEED
+		private const int AdviseDontNeed = 4;	// POSIX_FADV_DONTNEED
+		private const int AdviseNoReUse = 5;	// POSIX_FADV_NOREUSE
+
+		static private void GiveAdvice (FileStream file, int advice)
+		{
+			int fd = file.Handle.ToInt32();
+			int ret = posix_fadvise (fd, 0, 0, advice);
+			if (ret != 0) {
+				throw new Exception ("Could not give file advice on " + file.Name + ": "
+						     + Syscall.strerror (Marshal.GetLastWin32Error()));
+			}
+		}
+
+		static public void FlushCache (FileStream file)
+		{
+			GiveAdvice (file, AdviseDontNeed);
+		}
+
+		static public void PreLoad (FileStream file)
+		{
+			GiveAdvice (file, AdviseWillNeed);
+		}
+
+		static public void IncreaseReadAhead (FileStream file)
+		{
+			GiveAdvice (file, AdviseSequential);
+		}
+
+		static public void DisableReadAhead (FileStream file)
+		{
+			GiveAdvice (file, AdviseRandom);
+		}
+
+		static public void NormalReadAhead (FileStream file)
+		{
+			GiveAdvice (file, AdviseNormal);
+		}
+
+	}
+
+}
Index: Util/Makefile.am
===================================================================
RCS file: /cvs/gnome/beagle/Util/Makefile.am,v
retrieving revision 1.23
diff -u -r1.23 Makefile.am
--- Util/Makefile.am	11 Oct 2004 15:46:04 -0000	1.23
+++ Util/Makefile.am	13 Oct 2004 22:17:37 -0000
@@ -13,6 +13,7 @@
 
 CSFILES = 			        	\
 	$(srcdir)/camel.cs              	\
+	$(srcdir)/FileAdvise.cs			\
 	$(srcdir)/ExifData.cs			\
 	$(srcdir)/ExtendedAttribute.cs  	\
 	$(srcdir)/FileSystem.cs			\


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]