0

From other things I've read (for one example of many why does it take so long to read the top few lines of my file?_ )

I gather that commands like

head -c 4 bigfile
dd if=bigfile bs=4 count=1

or c++ functions like

std::ifstream file(filePath, std::ios::binary);
char buffer[4];
file.read(buffer, 4);
std::cout << buffer[0] << buffer[1] << buffer[2] << buffer[3] << std::endl;

Should be just as fast for huge files as they are for small files, since from what I gather none of these require loading the entire file they just read the first 4 bytes.

However when I test any of these commands with a 1TB file and a 0.3GB file, with the 0.3GB file they all finish instantly, while with the 1TB file they all take many minutes.

Why is this? Is there a way to actually read the first 4 bytes of a huge file just as quickly as the first 4 bytes of a small file?

My file system is EOS.

7
  • 3
    You wouldn’t happen to have an antivirus running or some other program which scans files you try to read before letting the real caller actually read them, would you? On my system head -c 4 works just as fast on huge files as it does on small ones. Commented Jul 8 at 16:33
  • Hi @StephenKitt , I don't think so. If I run top while doing e.g. head -c 4 bigFile , as soon as I do the head command appears in top and nothing else, and remains in top until a few minutes later when it finishes
    – Jack
    Commented Jul 8 at 16:43
  • I guess if it takes the same amount of time for you for huge files and small files it must just be something to do with my filesystem, I assume it stages the entirety of large files before it reads any part of them or something like that. Thanks
    – Jack
    Commented Jul 8 at 16:57
  • 1
    What file system do you use?
    – choroba
    Commented Jul 8 at 16:59
  • 1
    EOS: yep, that's a userland network file system. It's not head that takes time, it's your file system; and quite likely it's not even the 4 bytes you read, but the metadata that needs to be requested from the MGM instance.. That's a pretty specific tool for a pretty specific (CERN) job. Maybe, if you just happen to need known ranges from files, just going through the XRootD or HTTPS access protocols would be easier. Quite possibly, asking your instance admin whether they offer CIFS/Samba access, S3/Minio or XrdHttp access is clever here. Commented Jul 8 at 17:08

1 Answer 1

0

This is likely the consequence of the organization of files on the storage device. There have been many kinds of filesystems historically (on Unix-like systems alone, not to mention others).

Some filesystems use a B-tree structure or similar. For larger files, the tree gets deeper: more indirect accesses are required to get to the data.

Traditiona Unix filesystems avoided this by a lobsided tree approach. But even those traditional filesystems had extra fast access to very tiny files. Very tiny files (such as most symbolic links) could be stored entirely in the inode structure, not requiring additional data blocks.

The lobsided tree aspect comes into play in that the first bunch of data blocks are directly referenced from the inode (regardless of how large the file is). After the direct datablocks comes an indirect block, which contains pointers to more data blocks. Then there can be a "doubly indirect" block. Fast access to the front of the file is retained; accessing the long tail is expensive.

To understand things further on your system, you have to drill into what filesystem you are using, and how exactly it works.

  • For each file (or other objects), an "inode" structure has to be allocated.

  • A very tiny file's content can be stored entirely in the inode structure, in some spare space that exists. Symbolic links are a kind of file, and usually they are very small. Symbolic links fit entirely into the inode.

  • A slightly larger file requires one or more data blocks in addition to the inode. Thus, it is slower to access. We must not only access the inode, but traverse pointers to data blocks and read them. The pointers to the data blocks are in the inode.

  • An even larger file has indirect blocks. The inode points to indirect blocks

1
  • Jack is using a network file system; and an Exabyte scale, very non-tree-y file system at that – so I'm afraid your considerations simply don't apply here :( Commented Jul 9 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .