Re: Multi pthreaded RT application - mlock doubt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Dipen,

On 2021-05-26, "Ahmed S. Darwish" <a.darwish@xxxxxxxxxxxxx> wrote:
>> Original example shown in above link uses the
>> prove_thread_stack_use_is_safe same way. I just extended it to call it
>> locally and calculate it locally because of the mutli thread.
>
> As I said, the example is wrong, and it is not authoritative. It is just
> an old obsolete wiki that no one maintains anymore.

This example is confusing to say the least. But it will work as expected
with only 1 thread.

>> static void *my_rt_thread(void *args)
>> {
> ...
>> 	int last_majflt = 0, last_minflt = 0;
> ...

Creating threads will cause page faults. Since you are now creating
multiple threads and your thread function immediately calls getrusage(),
the first threads are "seeing" the page faults of the later threads
being created. For example, if you add a sleep() here before calling
getrusage() and give the threads a chance to start up, then you will see
no more page faults.

>>
>>    	getrusage(RUSAGE_SELF, &usage);
>>
>>    	printf("[%d]Pagefaults, Major:%ld, Minor:%ld \n",pthread_self(),
>>    	       usage.ru_majflt - last_majflt,
>>    	       usage.ru_minflt - last_minflt);
>>
>> 	last_majflt = usage.ru_majflt;
>>    	last_minflt = usage.ru_minflt;
>>
>>    	prove_thread_stack_use_is_safe(MY_STACK_SIZE);
>>
>> 	getrusage(RUSAGE_SELF, &usage);
>>
>>    	printf("[%d]After stack usage:Pagefaults, Major:%ld, Minor:%ld \n",pthread_self(),
>>    	       usage.ru_majflt - last_majflt,
>>    	       usage.ru_minflt - last_minflt);
>>
>
> You're just measuring the effect of stack prefaulting, which will
> obviously generate page faults (as expected).

For the main task this would be correct. But the thread stacks come from
the heap, which was already prefaulted.

In general, avoiding page faults for threaded real-time tasks that
dynamically allocate memory is rather complex. If you are using glibc,
you need to understand how it manages memory for threads.

https://sourceware.org/glibc/wiki/MallocInternals

Setting M_ARENA_MAX can help here.

But in general, just make sure your real-time threads have all the
memory they need (allocated and prefaulted and _not_ given back to the
heap) before they start their real-time activity.

John Ogness



[Index of Archives]     [RT Stable]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux