All of lore.kernel.org
 help / color / mirror / Atom feed
From: John Ogness <john.ogness@linutronix.de>
To: "Ahmed S. Darwish" <a.darwish@linutronix.de>,
	Dipen Patel <dipenp@nvidia.com>
Cc: linux-rt-users@vger.kernel.org
Subject: Re: Multi pthreaded RT application - mlock doubt
Date: Wed, 26 May 2021 12:26:40 +0200	[thread overview]
Message-ID: <87mtshbxjz.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <YK4HT42yr02J1Gp1@lx-t490>

Hi Dipen,

On 2021-05-26, "Ahmed S. Darwish" <a.darwish@linutronix.de> 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

      reply	other threads:[~2021-05-26 10:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01  2:06 Multi pthreaded RT application - mlock doubt Dipen Patel
2021-05-25  3:08 ` Dipen Patel
2021-05-25  7:26 ` Ahmed S. Darwish
2021-05-25 22:24   ` Dipen Patel
2021-05-26  8:31     ` Ahmed S. Darwish
2021-05-26 10:26       ` John Ogness [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mtshbxjz.fsf@jogness.linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=a.darwish@linutronix.de \
    --cc=dipenp@nvidia.com \
    --cc=linux-rt-users@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.