NetBSD: New PID Allocation Code

Submitted by Anonymous
on March 19, 2003 - 1:07pm

David Laight implemented a new algorithm for pid allocation and proc/pgrp lookups. Main benefits include that searches aren't required when doing pid and pgrp lookups by id, smaller footprint, better scalability, and cleaner locking.

As he explained in his announcement mail:

    "The basic idea is to ensure that you allocate a pid number which
    references an empty slot in the lookup table. To do this a FIFO freelist
    is linked through the free slots of the lookup table."

From: David Laight
To: tech-kern
Subject: new pid allocation code
Date: 03/11/2003 14:36:30

Hackers,

The code below implements a different pid allocation and proc/pgrp
lookup algorithm.

The main benefits are:
- pid and pgrp lookup (by id) doesn't require a search
- no dependency on MAXUSERS
- automatically scales well to large numbers of processes
- small data footprint for small systems
- ability to enumerate through all the processes without holding a lock
  for the entire duration, or having very messy locking rules.
  (the allproc list and p_list fields could be depracted later).

- Largely MP clean

The basic idea is to ensure that you allocate a pid number which
references an empty slot in the lookup table.  To do this a FIFO freelist
is linked through the free slots of the lookup table.
To avoid reusing pid numbers, the top bits of the pid are incremented
each time the slot is reused (the last value is kept in the table).
If the table is getting full (ie a pid would be reused in a relatively
small number of forks), then the table size is doubled.
Orphaned pgrps correctly stop the pid being reused, orphaned sessions
keep the pgrp allocated.

Below is the main part of the change, there are other bits lurking
in fork and exit.  If people think this code is ok, I'll sort out a
full diff against 'current' (and fix for sparc64).

(I've been running this code for months!)

	David

The full announcement, which includes the patch, can be found here

Patch

on
March 15, 2003 - 5:14pm

How 'bout a link to the patch instead...

that would be nice

on
March 18, 2003 - 10:56pm

I agree, as stories go, it doesn't read too well.

At a guess, I'd say PID allocation isn't a complex part of any kernel.
By definition it can't be intrusive.

A comparison with other PID allocators and/or some information about
why this is important could have been interesting.

Ciaran O'Riordan

Re: that would be nice

on
March 19, 2003 - 3:23am

Well, the story has had a major overhaul (the teaser has been rewritten, almost in its entirety, the announcement mail now follows the kerneltrap format, etc.), and now there's a link to the patch instead.

    At a guess, I'd say PID allocation isn't a complex part of any kernel.
    By definition it can't be intrusive.

You're right, changing the pid allocator doesn't require changing any other parts of the kernel; but considering our lack of NetBSD stories, I think we should run this one. It's kinda like the scheduler change: It changed the guts of the kernel, but didn't require any big changes elsewhere (disclaimer: IANAKH, correct me if i'm wrong.)

    A comparison with other PID allocators and/or some information about
    why this is important could have been interesting.

As far as I am aware, most kernels use hash tables for this; so this one looks innovative.

> It's kinda like the schedu

on
March 19, 2003 - 4:08pm

> It's kinda like the scheduler change: It changed the guts of the
> kernel, but didn't require any big changes elsewhere

You are correct.
I find the scheduler interesting since it will effect 94% of GNU/Linux
users. I'm not saying that GNU/Linux is more important than NetBSD,
I'm saying that the scheduler is more important than the PID allocator.

> As far as I am aware, most kernels use hash tables for this; so this
> one looks innovative

Now I've learned something :)

Ciaran O'Riordan

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.