Page 1 of 2

#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 4:30 am
by Pype.Clicker
A global guideline through clicker development has always be "avoid arbitrary limits": there are (or should be) only 4 'magic values' when defining your tables: 0, 1, 2 and infinity.

That means i've used avl trees, B+trees and sometimes think of hashtables for storing kernel's basic data such as threads, process, symbols, module info, etc. This even culminates with "KDS" (Kernel Dynamix System), a hierarchically naming service where you can retrieve almost everything related to the system...

Of course this partially affects performance. When you create a new thread, you have to insert it in its process' AVL ... Alternatively, i could have used a simple "thread table" where every thread gets its own entry and where you need to crawl the table, comparing [tt]thread[ i ].pid[/tt] against [tt]the_process[/tt] when you want to display the list of threads belonging to a given process (e.g. because you're about to kill the process ;) )

That's all a matter of trade-offs, but sometimes i get the feeling that i've been too far in complexity for a "microkernel" ...

Is "#define MAX_THREADS 256" or "#define MAX_FILEHANDLES 1024" so evil? I can't tell anymore.

When it came to message passing, i took the decision to have each 'port' in a simple table, addressed by offset, for the sake of performance. While you typically only search threads a few times in their lifespan, you'll do _much more_ search for ports (almost everytime you send a message). I know this is inconsistent with the rest of the design ... and i'm unsure of which approach is the best now. On the 'darkside', it's pretty scary to thing that you could start as many threads as you want, but that the number of communication ports they can open is bounded.

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 4:36 am
by Solar
Isn't one of the very design paradigmas of a microkernel to put clean design over performance?

Even in a monolithic kernel (or a C library... ;D ), I'd avoid arbitrary limits like MAX_THREADS like the plague. In the very least, it makes people depending on whatever that value was set to when the kernel was compiled - meaning, what runs fine on one system might fail on another because the compile-time constants were different. In the very least, allow such constants to be queried at runtime (kernel.getMaxThreads()), and extended if need be (kernel.setMaxThreads() with corresponding realloc()).

That's from a language / designer perspective only. Depending on the actual implementation and the actual impact, tradeoffs might be OK (i.e., getMaxThreads() but no setMaxThreads()).

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 4:47 am
by FlashBurn
I also use these "MAX" defines and also does the haiku kernel. I think when you are programming for a 32bit machine you can have such limitation, because you only have a "small" address range. On a 64bit machine I wouldn?t define such limitations, there you need to use some clever algos ;)

I use the "MAX" defines for processes, threads and msg ports (at the moment), but I only finished the allocation of an entry, not the deallocation.

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 5:07 am
by Solar
Even on a 32bit machine, I'd be reluctant to either arbitrarily limit the number of objects I can have or wasting data structure memory I don't need... But then, I know that Pype doesn't like realloc() that much... 8)

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 5:17 am
by FlashBurn
I don?t really understand what you mean with "wasting data structure memory I don't need". The tables I use are working like a slab allocator, only the needed mem is allocated (in 4KiB chunks). So you can set a high limit (with my tables you also could set a new limit, but this doesn?t work because of the hard coded addresses of the tables).

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 7:00 am
by Solar
Hmmm... if your memory allocation of such structures is dynamic, what do you need the "MAX" defines for anyway?

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 8:37 am
by FlashBurn
Because it uses a memory region and behind this is another region, so I could make it smaller, but not bigger. The memory is continuous, so that I save some mem, the only other thing that is needed are the bitmaps for these regions.

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 11:29 am
by Combuster
I'll take the hardware as the limit. Unless they're obviously LOW they are not worth the hassle to bypass. Effectively, over here
max_threads = entries_in_gdt - 2 (systemdescriptors) - 2 (userspacedescriptors) - 1 (null) = 8187.
Probably the memory runs out before the gdt does 8)

Re:#define MAX_THREADS ... ?

Posted: Thu Jun 29, 2006 12:35 pm
by bluecode
??? _I_ don't need any additional entries in the GDT for a new thread/process (software task-switching).

But I think the virtual address space is pype's hindrance (virtual address space reserved for thread stacks), or? Anyway, how much space do you reserve for one thread?

Re:#define MAX_THREADS ... ?

Posted: Fri Jun 30, 2006 12:55 am
by mystran
max_threads isn't a bad idea, but don't make it a define...

Instead, make it a global variable, and initialize it at startup based on the amount of memory. Doesn't make thing that much more complicated, and means that the number makes sense no matter what amount of memory there is.

Linux forexample sets the maximum number of threads such that at most half of physical memory is used for kernel thread structures, if the maximum number of threads is reached. My own kernel uses the same idea, but with a much more conservative figure (for various reasons).

The max_filehandles case is nastier. The best I can think of is make the limit process specific, with a compile-time configurable default, and allow override at process creation. Typically servers will need a LOT more filehandles than any other random processes.

Re:#define MAX_THREADS ... ?

Posted: Fri Jun 30, 2006 3:08 am
by Pype.Clicker
Isn't one of the very design paradigmas of a microkernel to put clean design over performance?
Ah?? hum ... well, yes, maybe. Now whether Clicker is a microkernel or not is very arguable ... Probably LT would say it is and AT would say it isn't.
But then, I know that Pype doesn't like realloc() that much...
no, indeed. I don't like it in students' programs because it's counterintuitive and make them write nasty bugs. I don't like it in a kernel because i don't like the idea that "oh, you'll usually allocate a new thread in 10?s but if a realloc is needed, it could jump to 1ms or so ...

That's another reason that made me choose AVL trees against other techniques such as associative linked lists or hashes: you know an AVL tree require at most O(log2N) to search/insert/delete. You don't have "pathological" cases where you suddenly need to copy the whole datastructure somewhere else to go on working.
But I think the virtual address space is pype's hindrance (virtual address space reserved for thread stacks), or? Anyway, how much space do you reserve for one thread?
Well, clicker has that somehow-specific design where there is very little "kernel space" ... i mean, true kernel space that is shared by all processes. Threads are indeed there, like a couple of other resources. File handles, on the other hand, would rather be located in a level-0 process-specific memory area (thus you need to be in kernel mode to access it, but what you do there doesn't affect other processes).

Re:#define MAX_THREADS ... ?

Posted: Fri Jun 30, 2006 9:53 am
by mystran
People always forget the less problematic use of realloc():

You need a buffer, of maximum of 1MB, but you might get just 124 bytes. So now, you allocate 1MB. Then you get 124 bytes. At this point if you can call realloc() you don't waste the rest of the 1MB-124B. :)

realloc() in that case is especially cool if the buffer you allocated was at the end of the heap, because you can then allocate a new 1MB buffer, and so on, never actually using more memory than you need.

On the other hand, when you do need a bigger buffer, it makes sense to ask the memory manager whether there would be free space directly after the current block. The fact that realloc() will allocate a replacement block for you if that fails, and copy your data there, is just a convenience hack, and I think that hack is what causes most problems with realloc().

Re:#define MAX_THREADS ... ?

Posted: Sun Jul 02, 2006 4:20 am
by Guest
I'ld recommend what solar told you.
For examples bsd* uses a sysctl() to set & get this value
Linux also uses the same, but for threads & process linux doesn't have a maximum limit, the max limit is your memory.
Thanks

Re:#define MAX_THREADS ... ?

Posted: Sun Jul 02, 2006 4:59 am
by Candy
how about using realloc in combination with c++-style encapsulation? Make a class that controls a series of objects for you, use realloc to resize it and you only need to control one point of pointer duplication (assuming you store and return only pointers, or that references to an object in the storage must be out of scope before it may be resized or might not be used after that).

Re:#define MAX_THREADS ... ?

Posted: Sun Jul 02, 2006 12:24 pm
by zloba
I would have a "thread manager" object which stores the stuff, and as far as the outside world is concerned, it can be implemented in any way - MAX_THREADS, reallocable array, AVL or anything at all. "doesn't matter" (tm)