The MASM Forum

General => The Campus => Topic started by: xanatose on November 10, 2014, 06:11:43 AM

Title: How does one invalidate the file cache?
Post by: xanatose on November 10, 2014, 06:11:43 AM
Using win32 api. How does one invalidate the file cache? I'm trying to solve a performance bug that occurs only when the files are not in the cache.

Also:
Is there something faster than FindFirstFile and FindNextFile to enumerate files in a directory. I only need the name of the files.
Title: Re: How does one invalidate the file cache?
Post by: TouEnMasm on November 10, 2014, 08:52:58 PM

msdn files cache
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364218(v=vs.85).aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364218(v=vs.85).aspx)
Title: Re: How does one invalidate the file cache?
Post by: MichaelW on November 10, 2014, 09:15:55 PM
For what it's worth, I did a quick test to determine if the FlushFileBuffers function, when passed a handle to a volume, clears the file data from the cache, and my results indicated that it does not.
Title: Re: How does one invalidate the file cache?
Post by: TouEnMasm on November 11, 2014, 06:14:49 PM

The createfile function with the option FILE_FLAG_NO_BUFFERING must do it.

Title: Re: How does one invalidate the file cache?
Post by: MtheK on November 12, 2014, 12:14:49 AM
  That works for me, tho the data must be aligned, else rc87 (ERROR_INVALID_PARAMETER) will occur.
Title: Re: How does one invalidate the file cache?
Post by: Tedd on November 12, 2014, 06:31:51 AM
I wouldn't expect you can invalidate the cache, as such. Writing to it would make it 'dirty,' but the cache would still be valid.
Flushing the buffers does exactly what is says -- it flushes the cache to file, so the file is updated; the cache still remains and is still valid.
FILE_FLAG_NO_BUFFERING avoids the use of cache, which will only serve to slow down all access in your case.
If your issue is an actual bug due to timing and delays caused by files not yet being ready, your solution should be in synchronisation, not hacking in an attempt to hide the issue. If it's not really a bug, and you're just noticing a slow-down on not-yet-cached files, invalidating the cache would have the opposite effect -- you could try pre-loading the next file as you start to process the current one (in a separate thread, or using overlapped file IO.)

FindFirstFile/FindNextFile are a little slow, but I don't know of another generic way to get a directory listing of files. There is a potentially faster method for NTFS using the "Change Journal," but it works on NTFS volumes only.
Again, preparation may be required if you're looking for optimal performance.
Title: Re: How does one invalidate the file cache?
Post by: xanatose on November 13, 2014, 10:46:47 AM
The simulation of the slow down (when the files are not in the cache) was what I needed.

Ended up writing the files to a DVD. Reading them from there and opening/closing the drive after each test.
Not clean, but worked for my tests.

Thanks everyone that responded.