Symmetric Multiprocessing (SMP) development
 help / color / mirror / Atom feed
From: "J.A. Magallon" <jamagallon@able.es>
To: "J.A. Magallon" <jamagallon@able.es>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>,
	"Robert M. Hyatt" <hyatt@cis.uab.edu>,
	Lista Linux-SMP <linux-smp@vger.kernel.org>
Subject: Re: per-thread global variables
Date: Sat, 13 Jul 2002 04:05:44 +0200	[thread overview]
Message-ID: <20020713020544.GE1675@werewolf.able.es> (raw)
In-Reply-To: <20020713011114.GC1675@werewolf.able.es>; from jamagallon@able.es on Sat, Jul 13, 2002 at 03:11:14 +0200


On 2002.07.13 J.A. Magallon wrote:
>
>>
>>Which goes to show they weren't planning for clone like performance. A
>>single unshared page means two seperate mm structs, two sets of page
>>tables and switches causing TLB flushes. All because
>>
>>	movl %esp, %eax
>>	andl $ffff8000, %eax
>>	
>>and casting that is 'hard'
>>

It can be done even arch-independent (__builtin_frame_address is at least
since 2.96):

#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/user.h>

#define MAXT 4

// User definable
#define STACK_PAGES 8
#define PRIV_PAGES 1
//

#define STACK_SIZE (STACK_PAGES*PAGE_SIZE)
#define PRIV_SIZE (PRIV_PAGES*PAGE_SIZE)
#define TOTAL_PAGES (STACK_PAGES+PRIV_PAGES)
#define TOTAL_SIZE (TOTAL_PAGES*PAGE_SIZE)
#define TOTAL_MASK (~(TOTAL_SIZE-1))

void* getpriv();
#define self() (*(int*)getpriv())
#define setself(k) do { *(int*)getpriv() = k; } while(0)

int slave(void *arg);
void f();

int main(int argc,char** argv)
{
	void*	stack[MAXT];
	int		i;

	for (i=0; i<MAXT; i++)
	{
		stack[i] = memalign(TOTAL_SIZE,TOTAL_SIZE);
		clone(slave,stack[i]+STACK_SIZE-1,CLONE_VM|SIGCHLD,(void*)i);
	}
	for (i=0; i<MAXT; i++)
		wait(0);

	for (i=0; i<MAXT; i++)
		free(stack[i]);

	return 0;
}

void* getpriv()
{
	void* frame = __builtin_frame_address(0);
	void* priv = (void*)((unsigned long)frame&TOTAL_MASK);

	return priv+(ptrdiff_t)STACK_SIZE;
}

int slave(void *arg)
{
	setself((int)arg);
	printf("priv=%p\n",getpriv());
	printf("self=%d\n",self());

	f();

	return 0;
}

void f()
{
	printf("f self=%d\n",self());
}

Code for getpriv():

getpriv:
    pushl   %ebp
    subl    $8, %esp
    movl    %ebp, 4(%esp)
    movl    4(%esp), %eax
    andl    $-36864, %eax
    movl    %eax, (%esp)
    movl    (%esp), %eax
    addl    $32768, %eax
    addl    $8, %esp
    popl    %ebp
    ret

So it does not look too slow. I could do also sysconf(_SC_PAGESIZE), but
that adds a function call everywhere. Indeed it will be safer.

And you could fill private area before clone(), so children do not have to
worry about anything like setself(). Yup, it would be nicer...

Thanks for everything !!!

-- 
J.A. Magallon             \   Software is like sex: It's better when it's free
mailto:jamagallon@able.es  \                    -- Linus Torvalds, FSF T-shirt
Linux werewolf 2.4.19-rc1-jam3, Mandrake Linux 8.3 (Cooker) for i586
gcc (GCC) 3.1.1 (Mandrake Linux 8.3 3.1.1-0.7mdk)

  reply	other threads:[~2002-07-13  2:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-12 16:15 per-thread global variables J.A. Magallon
2002-07-12 16:43 ` Alan Cox
2002-07-12 16:34   ` J.A. Magallon
2002-07-12 17:41     ` Alan Cox
2002-07-12 19:17 ` Robert M. Hyatt
2002-07-13  0:12   ` J.A. Magallon
2002-07-13  1:50     ` Alan Cox
2002-07-13  1:11       ` J.A. Magallon
2002-07-13  2:05         ` J.A. Magallon [this message]
2002-07-13  3:20     ` Robert M. Hyatt
2002-07-13  9:43       ` J.A. Magallon
2002-07-13 14:07         ` Robert M. Hyatt

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=20020713020544.GE1675@werewolf.able.es \
    --to=jamagallon@able.es \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=hyatt@cis.uab.edu \
    --cc=linux-smp@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox