Results 1 to 3 of 3

Thread: High performance large file reading on OSX

  1. #1
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default High performance large file reading on OSX

    Hello,

    my aim is to read very large files (10GB to 30GB) into main memory (my MacPro has 32GB installed, running OSX 10.5.8).

    This is basically very easy to achieve using Qt:

    Qt Code:
    1. QFile file(filename);
    2. file.open(QIODevice::ReadOnly);
    3. file.readAll();
    4. file.close();
    To copy to clipboard, switch view to plain text mode 

    However, what happens on OSX is that the so called Unified Buffer Cache (UBC) automatically caches all reads and when reading a file larger than 16GB the systems ends up with 16GB of the file in the active memory and 16GB of the same data in the inactive memory (the cache) and starts to swap heavily. This is a well known problem and the UBC can be disabled with a simple additional system call:

    Qt Code:
    1. #include <fcntl.h>
    2. QFile file(filename);
    3. file.open(QIODevice::ReadOnly);
    4. fcntl(file.handle(), F_NOCACHE, 1); // <- disable UBC for that file
    5. file.readAll();
    6. file.close();
    To copy to clipboard, switch view to plain text mode 

    This successfully disables caching and allows loading files up to 30GB into main memory.

    However, the problem now is that reading performance with the cache disabled drastically drops from ~200mb/s to ~25mb/s.

    Does anyone have any hints on how to have the UBC disabled but still achieve high reading throughput?

    Best regards & thanks a lot,
    mikeee7

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: High performance large file reading on OSX

    have you tried mmap, or if you want to be platform independent Boost.Interprocess' equivalent (see e.g. http://boost.cowic.de/rc/pdf/interprocess.pdf, pages 20ff)?
    Last edited by caduel; 14th October 2009 at 13:20. Reason: url to interprocess docs

  3. #3
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: High performance large file reading on OSX

    Hi caduel,

    thanks for your suggestions -- memory mapping the file would be an option that could circumvent the problem. However we have a large existing codebase that is based on QDataStream, QFile and so on for loading those large files which we would like to continue to use.

    The function call
    Qt Code:
    1. fcntl(file.handle(), F_NOCACHE, 1);
    To copy to clipboard, switch view to plain text mode 
    successfully disables caching but why does read performance suffer that much -- is Qt doing something else behind the scenes?

    When using standard C calls for reading a large file, caching can be disabled while retaining the original high read performance:

    Qt Code:
    1. int fd = open(src.toStdString().c_str(), O_RDONLY);
    2. fcntl(fd, F_GLOBAL_NOCACHE, 1);
    3. size_t bufferSize = 1024ul*1024ul*2000ul;
    4. char* buffer = new char[bufferSize];
    5. read(fd, buffer, bufferSize);
    6. close(fd);
    7. delete[] buffer;
    To copy to clipboard, switch view to plain text mode 

    Any ideas why disabling caching slows down Qt but not the original C calls?
    Last edited by mikeee7; 15th October 2009 at 14:44. Reason: spelling error

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 14:04
  2. reading bytes out of a file
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2009, 16:55
  3. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  4. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55
  5. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.