-4

I want to run my C++ program after flushing the cache, Before running my program I do not know what is there in the cache. Is there some other manner in C++ on Ubuntu via which I may flush my cache before running my program.

EDIT: The motive for flushing the cache is... that each time I run my program I do not want my present data structures to be there in the cache... I mean I want a cold cache... whereby all the accesses are made from the disk.

One way of achieving this is to restart the computer... but considering the number of experiments that I have to run, this is not feasible for me. So, can anyone be kind enough to guide me as to how I can achieve this.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jannat Arora
  • 2,759
  • 8
  • 44
  • 70
  • 3
    How could Microsoft Commerce Server be in any way related to anything on Linux? – ildjarn Jun 29 '12 at 22:50
  • 1
    What does Ubuntu have to do with MS Commerce Server? – K. Brafford Jun 29 '12 at 22:53
  • 2
    What are you trying to achieve? – mfontanini Jun 29 '12 at 22:53
  • 2
    God, why do people keep tagging C++ questions with [tag:c]? – Griwes Jun 29 '12 at 23:28
  • @ildjarn I too know that Microsoft Commerce Server is not related to Linux. But I am running my program on both linux and windows. And it was added in order just to make the intention of the question clear. I think in the community, instead of people wasting time in trying to find flaws in the question..it is highly appreciated to understand the intention of the question and help...From the question you have put up...its clear u understood what I meant...so why not spend time constructively in answering the question. – Jannat Arora Jun 29 '12 at 23:48
  • @mfontanini I am trying to flush the cache...explicitly..hope u understand what cache is? – Jannat Arora Jun 29 '12 at 23:49
  • How could anyone possibly understand the intent of the question when you start it off by linking to something entirely unrelated/irrelevant? There is still a burden on _you_ to ask a good question if you want a good answer. – ildjarn Jun 29 '12 at 23:49
  • @ildjarn I know..there is a huge burden on me...but please let me spend my time in some constructive pursuits...and also each human being has some flaws...no one is picture perfect...also I am confident that I will learn to ask good question...and sorry for the badly asked question – Jannat Arora Jun 29 '12 at 23:52
  • For all those who have given negative votes...can someone be please kind enough to answer the question? – Jannat Arora Jun 29 '12 at 23:55
  • 1
    My question is not "what are you asking", but "what are you trying to achieve". Why in hell are you trying to flush your cache, what is the reason? You should explain what you are trying to do so people can understand and give a proper answer. Apparently, several people think the same way(3 negative votes mean something, right?). – mfontanini Jun 30 '12 at 00:20
  • 1
    @JannatArora: What you are missing is that it is not clear at all what you mean by *cache*, and the closest to a definition is a link to something that is most probably unrelated to the question (or else the question is much more confusing than what I think it already is) – David Rodríguez - dribeas Jun 30 '12 at 02:39
  • @mfontanini The motive for flushing the cache is...that each time I run my program I do not want my present data structures to be there in the cache...I mean I want a cold cache..whereby all the accesses are made from the disk...one way of achieving this is to restart the computer...but considering the number of experiments that I have to run, this is not feasible for me. Hope this helps u in helping me out. – Jannat Arora Jun 30 '12 at 14:44
  • @DavidRodríguez-dribeas By cache I mean the data that is kept in the cache memory..that is the data that gets cached for faster access. – Jannat Arora Jun 30 '12 at 14:45

2 Answers2

2

You have no need to flush the cache from your user-mode (non-kernel-mode) program. The OS (Linux, in the case of ubuntu) provides your application with a fresh virtual address space, with no "leftover stuff" from other programs. Without executing special OS system calls, your program can't even get to memory that's used for other applications. So from a cache perspective, your application start from a clean slate, as far as it's concerned. There are cacheflush() system calls (syntax differs by OS), but unless you're doing something out-of-the-ordinary for typical user-mode applications, you can just forget that the cache even exists; it's just there to speed up your program, and the OS manages it via the CPU's MMU, your app does not need to manage it.

You may have also heard about "memory leaks" (memory allocated to your application that your application forgets to free/delete, which is "lost forever" once your application forgets about it). If you're writing a (potentially) long-running program, leaked memory is definitely a concern. But leaked memory is only an issue for the application that leaks it; in modern virtual-memory environments, if application A leaks memory, it doesn't affect application B. And when application A exits, the OS clears out its virtual address space, and any leaked memory is at that point reclaimed by the system and no longer consumes any system resources whatsoever. In many cases, programmers specifically choose to NOT free/delete a memory allocation, knowing that the OS will automatically reclaim the entire amount of the memory when the application exits. There's nothing wrong with that strategy, as long as the program doesn't keep doing that on a repeating basis, exhausting its virtual address space.

phonetagger
  • 7,701
  • 3
  • 31
  • 55
0

This is a common question.

Firstly you have to understand that the caches are never really empty, just like a register is never really empty, it's always there, and it always has a value. The phrase "Flushing the cache" actually refers to writing the cache contents to memory, also called a memory barrier. see https://en.wikipedia.org/wiki/Memory_barrier

This is not your problem, and so you are using the wrong terminology.

What you really want is to fill the cache with the wrong values. This is harder than it sounds, because you are fighting all the optimisations that normally are your friend. Memcpy'ing a large block of memory (several MB - given the size of todays caches) should normally work though.
However...
You also have file caches and other things that will give your application an unfair advantages. This can be a very complex subject, and is a small project in it's own right.

Tiger4Hire
  • 1,065
  • 5
  • 11