How to programmatically simulate lower processor frequencies?

I am interested in starting a program at a certain frequency (for example, 25 MHz) on my processor with a frequency of 2 GHz. The only method I can think of to do something like this is to use the sleep mode function to the microsecond, but I'm not sure how to calculate how long the thread should sleep in order to match a specific frequency. Any tips or other ideas? I do this in C on OS X86 Linux.

+7
source share
6 answers

There are a couple of issues here. This is the first thing you are trying to imitate. Modern processors operate with a clock frequency of 2 GHz, but with instructions on the pipeline, so an individual instruction can take 10-30 hours. Putting to sleep in a stream, you break the pipeline. Secondly, how much do you want you to pretend. You need to have time at the instruction level, can we fake it by putting some space between the functions.

My last thought is that you most likely do not want to simulate a modern processor operating at a frequency of 25 MHz, but some type of ARM chip on an integrated device. If so, then for most of these chips there are already very good simulators. Compile your code into your own instructions for your target chip, they use an already available simulator, if one is available.


Edit

So, as I understand it now, you want to execute the virtual processor instruction 25M once per second. I could try an adaptive approach. You have a lot of time to “mess around” between instructions. start by placing some interval, sleep is likely to work between each instruction. Note that in the array with maximum accuracy, when all the virtual clocks begin to support a moving average, for example, the last 25, 100 or 1000 cycles. If the average rises above 25 MHz, add more space. If it is too slow, reduce the space.

As I said, it is very difficult to calculate the amount of time that an instruction takes on a modern processor. The first set of instructions may work too fast or slow, but such a method should support it as close as possible to the correct speed, since a typical generator is on a comparable hardware implementation.

+2
source

I just ran the simulation in the queues. For example, you can run 250 thousand cycles, and then sleep for the remainder of the 10 ms interval. You can customize the look of the clock that the simulation sees to make it completely transparent, unless it interacts with some external equipment that needs to be paired with a certain speed (in this case it becomes a much more complicated problem).

+2
source

I would suggest an event-driven architecture: at each step of STEP (1 / hz), command 1 was triggered.

+1
source

To summarize the above, if you are in user mode trying to emulate a virtual processor with a certain frequency, you must implement some kind of manual "scheduling" of a thread that processes CPU instructions either through sleep calls or more complex functions and functions, such as fibers in Windows The caution you should pay attention to is that some sleep calls in sleep mode do not sleep for the exact amount of time you specified, so you may need to add additional code to calibrate the deviation from computer to computer to get closer to the target frequency , Most often, you will not be able to accurately plan for your virtual processor to work at a stable 25 MHz (most likely 22-28 MHz). In any case, I agree with Nathan and the surge in ideas. Good luck with what you always use!

+1
source

For a virtual machine, everything is virtual, including time. For example, within 123 real seconds, you can emulate 5432 virtual seconds of processing. A common way to measure virtual time is to increment (or add something) the "number of cycles" counter each time a virtual command is emulated.

From time to time, you try to synchronize virtual time with real time. If virtual time is too far ahead of real time, you insert a delay to allow real-time catching up. If virtual time is beyond real time, you need to find a reason to slow down. Depending on the emulated architecture, there is nothing you can do; but for some architectures, there are power management features such as thermal management (for example, maybe you can pretend that the virtual processor has warmed up and is running slower to cool down).

You probably also want to have a queue of events where various emulated devices can say, “some event will happen at some particular moment”; so if the emulated CPU is inactive (waiting for the event to occur), you can proceed to the next event. This naturally allows the virtual machine to catch up with it if it runs slowly.

The next step is to identify the places where synchronization takes place, and only synchronize virtual time with real time in these specific places. If the emulated machine performs heavy processing and does nothing that is visible to the external observer, then the external observer cannot say whether virtual time is approaching real time or not. When a virtual machine does what is visible to an external observer (for example, send a network packet, update video / screen, create sound, etc.), you first synchronize virtual time in real time.

Step by step, it uses buffering to decouple when something happens inside the emulator, when they are visible to an external observer. For an (exaggerated) example, imagine that an emulated machine thinks it is 8:23 AM and she wants to send a network packet, but in reality it is only 8:00 AM. A simple solution is to delay the emulation for 23 minutes and then send the packet. This sounds good, but if (after the virtual machine sends the packet), the emulator fights to keep up with the real time (due to other processes running on the real computer or for any other reason) the emulator may lag, and You may have trouble maintaining the illusion that virtual time is the same as real-time. Alternatively, you can pretend that the packet was sent, and put the packet in the buffer and continue emulating other things, and then send the packet later (when it is actually 8:23 in the real world). In this case, if (after the virtual machine sends the packet), the emulator fights to keep up with the real time, you still have 23 minutes of freedom.

+1
source

See Fracas processor emulator for an approach to this. The authors spoke about this at the Heteropar workshop, part of EUROPAR 2010 . They significantly modify the OS scheduler to allow the use of only fractions of the real processor frequency that will be used by user programs.

0
source

All Articles