Code Monkey home page Code Monkey logo

Comments (7)

mkroening avatar mkroening commented on May 14, 2024

Note that setting HERMIT_CPUS=2 fixes the symptoms, even when having three or more threads.

from hermit-rs.

mkroening avatar mkroening commented on May 14, 2024

I found that the behavior is identical when running on QEMU. One CPU does not preempt, more than one do.

from hermit-rs.

jschwe avatar jschwe commented on May 14, 2024

I don't really have any knowledge about the APIC or the scheduler implementation, but here my two cents on the symptoms:

AFAIK the scheduler in rusty-hermit has some concept of priorities. It seems possible to me that the priority of threads generated via thread::spawn is lower than that of the main thread. In that case, the main thread would always be scheduled (if only a single core is available). Linux uses CFS (Completly Fair Scheduler) by default, so the main thread can't starve the spawned thread on Linux.
You could set a breakpoint / debug print in the scheduler to see if it really is never called.

from hermit-rs.

mkroening avatar mkroening commented on May 14, 2024

Thanks for the fast reply, @jschwe!

Good idea with the priorities! With two cores, the above code also fires the timer handler often, which calls the scheduler, but with one core, it does not. Not firing the interrupt handler is suspicious, but the starving could still be caused by priorities. The timer interrupt seems to operate in one-shot mode and not fire periodically, so if the scheduler would not want to preempt the current thread again in the future because of priorities, this might fit.

Another setup stills seems weird though:

Spawn a second thread, which does not print to Stdout:

use std::thread;

fn main() {
	thread::spawn(|| loop {
		println!(" 2")
	});

	thread::spawn(|| loop {});

	loop {
		println!("1 ")
	}
}

Running this code on uhyve with two cores only prints “ 2”, which I interpret as the first thread running on one core and the second thread and the main thread on the other core with the second thread starving the main thread. When printing in the second thread as well, I can read all three outputs again. So to me, it seems like something is going wrong with the one-shot timer setup when not yielding to the kernel by waiting on Stdout's mutex hidden in println! in this case.

I hope this makes some sense.

from hermit-rs.

jschwe avatar jschwe commented on May 14, 2024

I don't have any further ideas or time to investigate this currently. Pinging @stlankes since it looks like he might have overlooked this.

from hermit-rs.

stlankes avatar stlankes commented on May 14, 2024

RustyHermit uses currently a cooperative scheduler. If you not block a task, the task will never give up the CPU. Your main loop should call std::thread::yield_now.

from hermit-rs.

mkroening avatar mkroening commented on May 14, 2024

All right, that makes sense. Thanks for the clarification!

from hermit-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.