On Thu, Apr 10, 2008 at 8:03 PM, Gilles Chanteperdrix
<[EMAIL PROTECTED]> wrote:
>
> On Thu, Apr 10, 2008 at 7:52 PM, jeff koftinoff <[EMAIL PROTECTED]> wrote:
>  > Gilles Chanteperdrix wrote:
>  >
>  > >
>  > > It looks we are wrong. The main thread stack remains allocated
>  > > on-demand, even though the process is mlocked. It must be some recent
>  > > kernel evolution
>  > >
>  >
>  >  Hmm.. interesting.  What is the best way to make sure all my stacks are 
> not
>  > allocated on-demand?
>
>  Find attached the example I used to read the stack size of an mlocked 
> process.
>
>  You can run it on your target to see if you get the same result as me.
>
>  Now, I suspect this on-demand allocation only exists for the main
>  thread stack, I will run further tests to confirm this. So, you may:
>  - decide not to use the main thread for real-time duties
>  - memset a large buffer on stack to force the kernel to commit memory.

Forgot the attachment, but here is a better one, which prints the main
thread stack size as well as a newly created thread stack size. It
shows that, at least in my configuration, only the main thread stack
mapping is grown on-demand.

-- 
                                               Gilles
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <pthread.h>

long get_stack_size(unsigned long stackptr)
{
	char *line = NULL;
	size_t linesz;
	FILE *file;
	int len;

	file = fopen("/proc/self/maps", "r");
	if (!file) {
		perror("fopen(/proc/self/maps)");
		exit(EXIT_FAILURE);
	}

	while ((len = getline(&line, &linesz, file)) != -1) {
		unsigned long map_start, map_end;

		/* Remove trailing \r\n (if any) */
		while (line[len - 1] == '\n' || line[len - 1] == '\r')
			line[--len] = '\0';

		if (sscanf(line, "%lx-%lx", &map_start, &map_end) < 2) {
			perror("sscanf");
			exit(EXIT_FAILURE);
		}

		if ((unsigned) (stackptr - map_start) >= (map_end - map_start))
			continue;

		free(line);
		fclose(file);
		return map_end - map_start;
	}
	if (line)
		free(line);
	fclose(file);

	fprintf(stderr, "Could not find stack in /proc/self/maps\n");
	exit(EXIT_FAILURE);
}

unsigned long threadstack;
void *thread(void *cookie)
{
	unsigned dummy;
	threadstack = (unsigned long) &dummy;

	pause();

	return cookie;
}

int main(void)
{
	pthread_t tid;
	int err;

	if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
		perror("mlockall");
		exit(EXIT_FAILURE);
	}

	if ((err = pthread_create(&tid, NULL, thread, NULL))) {
		fprintf(stderr, "pthread_create: %s\n", strerror(err));
		exit(EXIT_FAILURE);
	}
	sleep(1);

	printf("stack: %ldK\n", get_stack_size((unsigned long) &tid) / 1024);
	printf("thread stack: %ldK\n", get_stack_size(threadstack) / 1024);

	return 0;
}
_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Reply via email to