views:

184

answers:

4

I want to test a program's memory management capabilities, for example (say, program name is director)

  1. What happens if some other processes take up too much memory, and there is too less memory for director to run? How does director behave?
  2. What happens if too many of the CPU cycles are used by some other program while director is running?
  3. What happens if memory used by the other programs is freed after sometime? How does director claim the memory and start working at full capabilities. etc.

I'll be doing these experiments on a Unix machine. One way is to limit the amount of memory available to the process using ulimit, but there is no good way to have control over the CPU cycle utilization.

I have another idea. What if I write some program in C or C++ that acts as a dynamic memory and CPU filler, i.e. does nothing useful but eats up memory and/or CPU cycles anyways?

  • I need some ideas on how such a program should be structured. I need to have dynamic(runtime) control over memory used and CPU used.
  • I think that creating a lot of threads would be a good way to clog up the CPU cycles. Is that right?

Is there a better approach that I can use?

Any ideas/suggestions/comments are welcome.

+1  A: 

If you have a single core this is enough to put stress on a CPU:

while ( true ) {
   x++;
} 

If you have lots of cores then you need a thread per core.

You make it variably hungry by adding a few sleeps.

As for memory, just allocate lots.

djna
@djna: The main part is dynamic control, I need to be able to dynamically increase or decrease the CPU and memory utilization.
Lazer
A: 

Dynamic memory control, you could just allocate or free buffers of a certain size to use or free more or less memory. As for CPU utilization, you will have to get an OS function to check this and periodically check it and see if you need to do useful work.

DeadMG
+1  A: 

There are several problems with such a design:

  1. In a virtual memory system, memory size is effectively unlimited. (Well, it's limited by your hard disk...) In practice, systems usually run out of address space well before they run out of backing store -- and address space is a per-process resource.
  2. Any reasonable (non realtime) operating system is going to limit how much CPU time and memory your process can use relative to other processes.
  3. It's already been done.

More importantly, I don't see why you would ever want to do this.

Billy ONeal
@Billy: The process I am trying to experiment with has some complex memory management features, which are supposed to behave in a certain way in high stress (meaning low memory, CPU cycles) situations. That is why I am trying to do this. I have tried to explain this in the question itself.
Lazer
@Lazer: Then you're going to have to use something like Application Verifier. Simply consuming memory on the system will not put memory pressure on the target process, because memory to your C or C++ program is not the same as physical memory.
Billy ONeal
Thanks for the Obrut link, btw. Something very similar to what I want but it only works on Windows.
Lazer
@Lazer: You should probably specify your platform in the question then ;)
Billy ONeal
@Billy: Is there a good Application Verifier utility for Unix? Or even a good C/C++ library for the same?
Lazer
@Billy: I very much did. Read the line after point 3.
Lazer
"I don't see why you would ever want this" should be an automatic -1, but I'm not feeling so petty at this time...
dash-tom-bang
Some might quibble with point 2. For example, any RTOS should provide me with the capability of specifying exactly which process/thread runs when. If I can't guarantee that a process will be allowed to run without interruption, it isn't hard Real Time. As for why, it is quite common in government jobs to have spare RAM and CPU utilization requirements (so they know they have room for minor software mods). Of course on modern consumer OS's you are right that it doesn't make much sense.
T.E.D.
@Lazer: Oops. Sorry. By Application Verifier, I meant Microsoft's Application Verifier -- Valgrind has similar functionality for POSIX boxes. @dash-tom-bang: Well, honestly, I don't. If you want to simulate low memory, that I understand. But simulating "slow" I don't. @T.E.D.: Most of us aren't programming on an RTOS. But it's a good point; +1 to comment and added "Consumer" to my description.
Billy ONeal
+8  A: 
composer
thanks @Henj, the desciption is exactly what I needed. I'll check if I can change the allocation at runtime. Or maybe modify the source to do so.
Lazer
@Lazer: if it doesn't support run-time adjustment, you can do that standard *NIX way. Start bunch of processes each eating some of CPU and memory. To free some resources, kill some of the stress process. To make them busy again - start the corresponding stress processes again.
Dummy00001