From owner-freebsd-arch Thu Dec 27 14:10:31 2001 Delivered-To: freebsd-arch@freebsd.org Received: from prg.traveller.cz (prg.traveller.cz [193.85.2.77]) by hub.freebsd.org (Postfix) with ESMTP id 2397737B416; Thu, 27 Dec 2001 14:10:25 -0800 (PST) Received: from prg.traveller.cz (localhost [127.0.0.1]) by prg.traveller.cz (8.12.1[KQ-CZ](1)/8.12.1/pukvis) with ESMTP id fBRMALvT097735; Thu, 27 Dec 2001 23:10:21 +0100 (CET) Received: from localhost (mime@localhost) by prg.traveller.cz (8.12.1[KQ-CZ](1)/pukvis) with ESMTP id fBRMAKkR097732; Thu, 27 Dec 2001 23:10:20 +0100 (CET) Date: Thu, 27 Dec 2001 23:10:20 +0100 (CET) From: Michal Mertl To: Mike Smith Cc: Matthew Dillon , Alfred Perlstein , John Hanley , Subject: Re: 64 bit counters In-Reply-To: <200112271941.fBRJfCF01253@mass.dis.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Thu, 27 Dec 2001, Mike Smith wrote: > > :Why do I think it never becomes wrong? Generally I'm adding rather small > > :value (<1e6 or so) to 64 bit counter. When one addition overflows less > > :significant 32 bits, several next additions don't overflow. > > : > > :u_int64_t+=u_long is: > > :addl %eax,(u_int64_t) ; this is atomic, right? > > :adcl $0,4(u_int64_t) > > : > > :If interrupt occurs in between these two instructions, then the less > > :significant halt is right and more significant will be fixed after the > > :execution returns to second instruction. Even if the interrupt modified > > :the value, we can expect (read - be sure) it didn't modify the more > > :significant half. > > > > You are absolutely right. The carry will only be set on the addl > > which overflows the 32 bit counter and will not be set on any of > > the other addl's that might be occuring just before, inbetween the > > two instructions, or just after. So only one of those will cause the > > msb to increment. > > > > For UP the addl instruction is atomic. For SMP we still have to lock the > > bus cycle. Something like this: > > > > lock; addl %eax,(u_int64_t) > > lock; adcl $0,4(u_int64_t) > > This is all well and good, but not portable. Aren't all other architectures we support 64 bit? On i386 even present form (32 bit addition) isn't atomic (no lock involved). On i386 for 64 bits the compiler generates the code above (without locks) and on 64 bit platforms I expect it would generate something at least similarly atomic like 32 bit addition on i386 we use today (both stable and current). On i586 we can have 64 bit atomic operations with cmpxchg8b. On Aplha I think the long is 64 bit so counters are already 64 bit. Anyway the code probably should be rewritten to use mutexes or atomic operations or whatever. I'll look at some current sources and maybe I'll be able to understand what are all these locks, mutexes, msleep and so on. In the time being I'll keep my naive patch to add the functionality to stable (it happens to work (probably only in 99.99999% of time and bit less on SMP)). > Sorry folks. 8( Not at all. -- Michal Mertl mime@traveller.cz To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message