network statistics in SMP

Harti Brandt hartmut.brandt at
Sat Dec 19 14:56:43 UTC 2009

On Sat, 19 Dec 2009, Ulrich Sprlein wrote:

US>On Tue, 15.12.2009 at 13:13:28 -0500, John Baldwin wrote:
US>> On Tuesday 15 December 2009 12:45:13 pm Harti Brandt wrote:
US>> > I see. I was also thinking along these lines, but was not sure whether it 
US>> > is worth the trouble. I suppose this does not help to implement 64-bit 
US>> > counters on 32-bit architectures, though, because you cannot read them 
US>> > reliably without locking to sum them up, right?
US>> Either that or you just accept that you have a small race since it is only stats. :)
US>This might be stupid, but can we not easily *read* 64bit counters
US>on 32bit machines like this:
US>do {
US>    h1 = read_upper_32bits;
US>    l1 = read_lower_32bits;
US>    h2 = read_upper_32bits;
US>    l2 = read_lower_32bits; /* not needed */
US>} while (h1 != h2);
US>sum64 = (h1<<32) + l1;
US>or something like that? If h2 does not change between readings, no
US>wrap-around has occured. If l1 was read in between the readings of h1
US>and h2, the code above is sound. Right?

I suppose this works only if it would be guaranteed that the CPU modifying 
the 64-bit value does this somehow faster than the CPU reading the data:

CPU1                    CPU2
----                    ----
write new h
			read h1 (new h)
			read l1 (old l)
			read h2 (new h)
write new l

It doesn't work too when the CPU first writes L and the H.


More information about the freebsd-arch mailing list