Re: Do too many files hurt a directory?

From: Nick Landsberg (hukolau_at_NOSPAM.att.net)
Date: 03/26/04


Date: Fri, 26 Mar 2004 18:44:14 GMT

Lew Pitcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Charlie Gibbs wrote:
>
>
>>A process went wild at a customer site the other night, and started
>>creating files with gay abandon. When the staff came in the next
>>morning, the system was extremely sluggish. I dialed in and found
>>that over 280,000 files had been created, all in the same directory.
>>All of the files belong to root (the user kicked off the process's
>>parent under the wrong ID), and most of them are empty or nearly so.
>>I finally managed to delete all of the files (it took the better part
>>of a day), but the system is still sluggish - even a simple ls command
>>takes 10 seconds or more to respond.
>
>
> Depending on the filesystem in question, filename location /may/ be
> implemented as a linear search of the directory. In this case, I'd
> expect that the system would be very sluggish whenever the overloaded
> directory is read.
>
> However, I thought that 'modern' filesystems implemented various
> directory search improvements that eliminated the linear search.
>

I did too (think that modern filesystems were smarter). In any
event, the directory itself is also a special file and once
it is "stretched out of shape" the lookup can get costly
in terms of disk I/O's. Assuming a "short" filenames of
10 chars, that's 2.8 MB of space that it's using up
and (on average) 1.4 MB of stuff to look up if a linear search
is being used. The most expensive operation would be to
look for a non-existant file, too.

>
>>Is a directory somehow stretched out of shape if it has an absurdly
>>high number of files created? If so, what's the best way to recover?
>
>
> Probably by moving the remaining files out of the current directory and
> into a new directory. Once that's done, remove the old directory
> completely, and move the new directory into it's place. This should
> improve things by re-optimizing the directory search.
>
>
>>Currently I have the customer creating a new directory and copying
>>the contents of the old directory into it. Then she can delete the
>>old directory and rename the new one.
>
>
> See above. We seem to think alike <grin>

Yep, that's the ticket! For future reference,
cpio can be used quite effectively for the
copy by actually creating links rather than
copies.

mkdir newdir
cd olddir
find . -print | cpio -pludmav ../newdir
(this preserves the access and modification times,
if that's important to you. The "l" option
only hard links the files into ../newdir
rather than actually copying them)
cd ..
rm -rf olddir
mv newdir olddir

>
>> We've got our fingers crossed -
>>the way the system is running it's going to take a long time - but if
>>it fails, what then? Can fsck clean things up, or is it time for a
>>full reformat a la Windows?
>
>
> Gak! You Always Have Other Options. We'll think of something.
>
>
> - --
> Lew Pitcher, IT Consultant, Enterprise Application Architecture
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed here are my own, not my employer's)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
> Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
>
> iD8DBQFAZG9RagVFX4UWr64RAtrmAJ9yesqGGSqtuzeDnlILBlvkz6B84QCgqlH+
> Cr216c2714BkfA1AKXSUQj8=
> =rQ6G
> -----END PGP SIGNATURE-----

-- 
"It is impossible to make anything foolproof
because fools are so ingenious"
  - A. Bloch