4 // Copyright (C) 2004 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 // FIXME: This is not portable to Win32
32 using System
.Runtime
.InteropServices
;
36 namespace Beagle
.Util
{
38 public class FileAdvise
{
40 // FIXME: On 64-bit architectures, we need to use "long" not "int" here for
42 [DllImport ("libc", SetLastError
=true)]
43 static extern int posix_fadvise (int fd
, int offset
, int len
, int advice
);
45 // The following are from /usr/include/linux/fadvise.h and will not change
46 private const int AdviseNormal
= 0; // POSIX_FADV_NORMAL
47 private const int AdviseRandom
= 1; // POSIX_FADV_RANDOM
48 private const int AdviseSequential
= 2; // POSIX_FADV_SEQUENTIAL
49 private const int AdviseWillNeed
= 3; // POSIX_FADV_WILLNEED
50 private const int AdviseDontNeed
= 4; // POSIX_FADV_DONTNEED
51 private const int AdviseNoReUse
= 5; // POSIX_FADV_NOREUSE
53 static private int GiveAdvice (FileStream file
, int advice
)
55 int fd
= file
.Handle
.ToInt32 ();
56 return posix_fadvise (fd
, 0, 0, advice
);
59 static public void FlushCache (FileStream file
)
61 GiveAdvice (file
, AdviseDontNeed
);
64 static public void PreLoad (FileStream file
)
66 GiveAdvice (file
, AdviseWillNeed
);
69 static public void IncreaseReadAhead (FileStream file
)
71 GiveAdvice (file
, AdviseSequential
);
74 static public void DisableReadAhead (FileStream file
)
76 GiveAdvice (file
, AdviseRandom
);
79 static public void NormalReadAhead (FileStream file
)
81 GiveAdvice (file
, AdviseNormal
);
84 static public void WillAccessOnlyOnce (FileStream file
)
86 GiveAdvice (file
, AdviseNoReUse
);
89 static public void TestAdvise ()
92 FileStream file
= new FileStream ("/etc/fstab",
93 System
.IO
.FileMode
.Open
,
95 int ret
= GiveAdvice (file
, AdviseNormal
);
97 Log
.Error ("FileAdvise failed: {0}", Mono
.Unix
.Native
.Stdlib
.strerror (Mono
.Unix
.Native
.Stdlib
.GetLastError()));