LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-06-2006, 10:52 AM   #1
brian_p_sully
LQ Newbie
 
Registered: Jan 2003
Posts: 8

Rep: Reputation: 0
Linux Memory Management - Buffer Cache


I know that Linux dynamically and efficiently allocates physical memory to the buffer cache as it is needed/not needed. This vastly increases performance.

However I am trying to do some worst case testing of my own software on my SuSE 9 Servers. Part of my software does disk I/O. During the first run of my software Linux will load the files I use into the buffer cache and leave them there, thus subsequent runs of my program will run much faster. This makes comparisons of my different runs inaccurate and incomplete.

I've tried to get around this by rebooting and that works. My test suite now has so many scenarios I have to reboot like 60 times a day. This doesn't work well so I've given it up.

I now try creating a file (sized to equal the amount of physical memory in my machine, 2GB) in between runs. Linux sees this as newer data and caches it. I then close and erase the file. Linux frees the cache. However the buffer cache nevers goes to 0, this may be due to somethings in cache from the OS or maybe the kernel doesn't let the cache fill up with just one file. Either way writing a 2GB file between each run is kind of slow and does a real workout on my hard drives.

Is there another way of forcing the buffer cache empty? Some sort of special sync command or parameter somewhere I can set?

I've seen some other people ask similar questions but never saw any real answer, just people asking why you would want to do that. Thanks.
 
Old 01-06-2006, 11:57 AM   #2
Keruskerfuerst
Member
 
Registered: Oct 2005
Location: Lat: 482356N, Lon: 104145E
Distribution: Gentoo 2006
Posts: 719

Rep: Reputation: 31
You may ask this question to kernel developers.
http://www.tux.org/lkml/
 
Old 01-06-2006, 12:10 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
I know Solaris has the "lockfs -f" command to do that, not sure what the equivalent would be with Linux (if any).
 
Old 01-06-2006, 02:33 PM   #4
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
the thing is -- the buffer cache (like so many things Linux/Unix) is not actually used to cache buffers any longer
since i think more than 5 years Linux buffer cache is actually only used for filesystem metadata (ie inodes) (i think)
 
Old 01-06-2006, 02:47 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
try running this as a mem wiper.
i have posted this before (you can cntrl c it to stop it whenever you like if you don't want it hitting the wall)
it will be alot faster than your 2g file
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

extern int errno;

#define MAXMB (4<<10)
#define MB (1<<20)
#define PG (4<<10)

int main(argc,argv)
int argc;
char *argv[];
{
        char *m;
        int i,j;
        sleep(3);
        for(i=0;i<MAXMB;i++) {
                m=malloc(MB);
                printf("%d MB ...",i+1);
                if(errno || (m==NULL)) break;
                for(j=0;j<MB;j+=PG) m[j]='A';
                printf(" WooHoo\n");
        }
        printf("\n\nAllocated %d MB\n",i);
        exit(0);
}
just compile it like
gcc -o name_of_executable name_of_file.c
 
Old 01-07-2006, 03:33 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by foo_bar_foo
the thing is -- the buffer cache (like so many things Linux/Unix) is not actually used to cache buffers any longer
since i think more than 5 years Linux buffer cache is actually only used for filesystem metadata (ie inodes) (i think)
The point is file content is still cached on Linux/Unix these days.
Whether it is done in the buffer cache or in the page cache is secondary.
 
Old 01-08-2006, 03:59 PM   #7
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
Quote:
Original Poster
However the buffer cache nevers goes to 0
Is there another way of forcing the buffer cache empty?
Quote:
jlliagre
The point is file content is still cached on Linux/Unix these days.
Whether it is done in the buffer cache or in the page cache is secondary.

hmmmmmmmmmmmmmmmm
 
Old 01-08-2006, 05:06 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
hmmmmmmmmmmmmmmmm
I understand your doubt, but I am assuming the open poster is confusing the buffer cache with the page cache, given the description of file content filling the cache.
By the way you are also confusing the buffer cache, which holds meta-data blocks (also stored on the page cache), with the Inode cache, which stores recently used Inodes.
There is also a fourth cache, the directory cache, storing a filename to Inode map.

That said, I don't know of a method that clears all these caches in order to have reproductible measurements, but if one dedicate a partition for this testing files, I'm quite sure unmounting then remounting it would invalidate all associated cached data.
 
Old 01-09-2006, 10:56 AM   #9
brian_p_sully
LQ Newbie
 
Registered: Jan 2003
Posts: 8

Original Poster
Rep: Reputation: 0
The memory wiper routine works as long as you actually use the memory, mallocing or callocing it didn't do anything. Once I added a memset after that then the memory was acutally "used" by the kernel and the buffer cache is reduced.
 
Old 01-09-2006, 11:12 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,353

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
Only real way would appear to be reboot - maybe use the program to get rough numbers, and reboot to get solid numbers.
Maybe have a look at kexec - should help out a bit in the time required. If you still need 60 a day though it's going to be a PITA.
 
Old 01-09-2006, 12:16 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Once I added a memset after that then the memory was acutally "used" by the kernel and the buffer cache is reduced.
Are you still convinced it's the buffer cache ?

Did you try unmounting/remounting the partition ?

Last edited by jlliagre; 01-09-2006 at 02:01 PM.
 
Old 01-09-2006, 12:45 PM   #12
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
Quote:
Originally Posted by brian_p_sully
The memory wiper routine works as long as you actually use the memory, mallocing or callocing it didn't do anything. Once I added a memset after that then the memory was acutally "used" by the kernel and the buffer cache is reduced.
that's interesting -- that implies that pointers in the page array can be restored aftr they are booted out.
like a page cache cache.
 
Old 01-09-2006, 06:01 PM   #13
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,401

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
It seems to me that your benchmarking should assume "normal buffering is taking place," and you should just leave things at that. You should run a few dozen repetitions, and treat the first run as the "outlier" because this run is paying the price of loading the buffer-cache. In other words, the first run of the day is the one that is considered "worst case."
 
Old 08-15-2006, 11:27 AM   #14
tkejlboom
LQ Newbie
 
Registered: Apr 2006
Posts: 9

Rep: Reputation: 0
similar problem

I had a similar problem. After a copied large volumes of data from my USB harddrive, I couldn't unmount it. I finally stumbled across a solution in an unrelated problem from a random google search. I restarted FAM using /etc/init.d/fam restart. It didn't eliminate the Disk Cache, but it reduced it from 3GB to 600MB and i could umount. Would that be sufficient for your tests?
 
Old 01-10-2010, 04:14 AM   #15
DreamDreams
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
I guess this is what you're looking for:

echo "3" > /proc/sys/vm/drop_caches
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I clear the memory cache on my linux server? robgo777 Linux - Software 2 01-09-2005 01:30 AM
Regarding Memory management in Linux alfredhitchcock Programming 4 09-07-2004 03:20 AM
file buffer cache jwstric2 Linux - General 2 06-30-2004 04:25 PM
Linux Memory Management beatlelane Red Hat 5 10-04-2003 12:39 AM
linux memory management kb8doa Linux - Newbie 1 06-18-2003 10:36 AM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration