Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement execution throttling when over 100% #20

Closed
gfeun opened this issue Mar 19, 2020 · 4 comments
Closed

Implement execution throttling when over 100% #20

gfeun opened this issue Mar 19, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@gfeun
Copy link
Contributor

gfeun commented Mar 19, 2020

A great issue to write 😄

So now that the simulation runs faster than the simulated CPU, it would be nice to have the possibility to throttle at 100% speed

Similar to how application target a specific number of Frame Per Second, we could target CPU Freq instructions per second, not more.

This would be implemented in the execute runner function.

Not a priority but nice to have.

@urish urish added the enhancement New feature or request label Mar 19, 2020
@urish
Copy link
Contributor

urish commented Mar 19, 2020

Indeed!

Actually, I have some prototype for this already, this is what the code looks like:

async execute(callback: (cpu: CPU) => void) {
  this.stopped = false;
  let workUnitCycles = 500000;
  let nextTick = this.cpu.cycles + workUnitCycles;
  for (;;) {
    avrInstruction(this.cpu);
    this.timer.tick();
    this.usart.tick();
    if (this.cpu.cycles >= nextTick) {
      const speed = this.performance.update();
      callback(this.cpu);
      if (speed < 0.95) {
        workUnitCycles = Math.min(500000, Math.floor(workUnitCycles * 1.05));
      } else if (speed < 0.98) {
        workUnitCycles = Math.min(500000, Math.floor(workUnitCycles * 1.01));
      } else if (speed > 1.1) {
        workUnitCycles = Math.max(10000, Math.floor(workUnitCycles / 1.05));
      } else if (speed > 1.02) {
        workUnitCycles = Math.max(10000, Math.floor(workUnitCycles / 1.01));
      }
      await new Promise(resolve => setTimeout(resolve, 0));
      if (this.stopped) {
        break;
      }
      nextTick += workUnitCycles;
    }
  }
}

@gfeun
Copy link
Contributor Author

gfeun commented Mar 19, 2020

Ah right, that make sense. I like the gradual threshold approach.

@arcostasi
Copy link

This week I studied a little about execution throttling when over 100%
My friend Uri gave me the path of the stones, at the beginning I got a lot complicated, but then I simplified it and I thought the operation improved, but then I decided to complicate it a little more and here is another way.
arcostasi/avr8js-electron-playground@a45fc06

@urish
Copy link
Contributor

urish commented Dec 18, 2020

Thanks for sharing it @arcostasi

@urish urish closed this as completed Dec 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants