----- Original Message -----
> From: [email protected]
> To: "Jan Stancek" <[email protected]>
> Cc: [email protected]
> Sent: Thursday, 3 October, 2013 3:13:06 PM
> Subject: Re: [LTP] [PATCH 3/3] pthread_attr_setschedpolicy/2-1.c: give 
> threads a moment to block on mutex
> 
> Hi!
> > > Hmm, so you did hit the small window for race condition between the new
> > > thread signals the main thread that it's executed and the next call to
> > > the mutex_lock on the tested mutex?
> > 
> > Yes, I'm assuming this is reason why I see it sporadically failing on
> > different distros/kernels. I see it happening the most on IBM z/VM (s390)
> > guests which have overcommitted resources, which affects scheduling.
> > 
> > It takes longer, but I could trigger it on x86_64 too.
> 
> I think that I know where the problem may come from. We are starting the
> threads in reverse order by priority, so the lowest one can actually be
> preempted by the higher priority one before the lock is taken, which
> makes sense.
> 
> > > I do not like this solution much, but this is not easy to do properly.
> > > One posibility is to pinpoint the threads on one cpu via the affinity()
> > > interface (open_posix_testsuite/include/affinity.h) then we can wait in
> > > the main thread until the thread with lowest priority is executed and
> > > safely say that the rest is locked on the mutex allready (as they run
> > > with FIFO scheduling).
> > 
> > I was considering this too, but there are some problems:
> > 
> > 1. pthread_setaffinity_np is not portable / sched_setaffinity is Linux only
> 
> This is the reason we have the open_posix_testsuite/include/affinity.h,
> it's not portable but some test cannot be written without it and it's
> implemented on most POSIX OSes, so we keep the unportable chunks in one
> place.
> 
> > 2. linux default sched_rt_runtime_us gives 0.05s to be used by
> >    SCHED_OTHER (non-RT tasks) (Documentation/scheduler/sched-rt-group.txt)
> >    If we set also main thread to use SCHED_FIFO, then this shouldn't be
> >    problem.
> 
> That shouldn't be much of problem, the main thread would pick up once
> the realtime threads are locked on mutexes/cond vars. Or am I mistaken
> here?

(assuming all threads are bound to single CPU)
That's true, but (the way I understand it) non-rt main thread can also run
when scheduler thinks that RT processes are monopolizing CPU for too long.
So you can't be sure that thread is actually on "mutex".

thread_func():
  pthread_mutex_lock(&c_mutex);
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&c_mutex);
--> here scheduler decides it's time to give a chance to non-rt threads
    and runs main thread, condition was already signalled so main thinks
    thread is already blocking on mutex
  pthread_mutex_lock(&mutex);

If we set SCHED_FIFO also on main thread with lowest priority, then
it shouldn't run regardless of value in sched_rt_runtime_us.

Regards,
Jan

> 
> --
> Cyril Hrubis
> [email protected]
> 

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to