|
|
Created:
11 years, 4 months ago by rsc Modified:
11 years, 3 months ago Reviewers:
CC:
dvyukov, iant, minux1, golang-dev Visibility:
Public. |
Descriptionsync/atomic: document that users must deal with 64-bit alignment
Update issue 599.
Patch Set 1 #Patch Set 2 : diff -r 5f9e99e3f2ea https://code.google.com/p/go/ #Patch Set 3 : diff -r 5f9e99e3f2ea https://code.google.com/p/go/ #
Total comments: 1
Patch Set 4 : diff -r 11de5a00682e https://code.google.com/p/go/ #Patch Set 5 : diff -r 11de5a00682e https://code.google.com/p/go/ #MessagesTotal messages: 15
Hello dvyukov, iant (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://code.google.com/p/go/
Sign in to reply to this message.
LGTM Can we provide any guarantees about int64 global variables?
Sign in to reply to this message.
We can fix it in the library. It's possible to implement atomic operations on non-aligned vars. All LOCK prefixed operations are atomic regardless of alignment. It will incur one additional branch for aligned vars, for non-aligned var it will LOCK prefixed instructions + there is huge overhead for LOCK prefixed operations that occupy 2 cache lines (it can be 5000+ cycles vs 15 cycles). I don't know about LL/SC on ARM. On Sun, Dec 23, 2012 at 10:59 AM, <iant@golang.org> wrote: > LGTM > > Can we provide any guarantees about int64 global variables? > > > https://codereview.appspot.com/7001056/
Sign in to reply to this message.
https://codereview.appspot.com/7001056/diff/5001/src/pkg/sync/atomic/doc.go File src/pkg/sync/atomic/doc.go (right): https://codereview.appspot.com/7001056/diff/5001/src/pkg/sync/atomic/doc.go#n... src/pkg/sync/atomic/doc.go:41: // BUG(rsc): On ARM, the 64-bit functions use instructions unavailable before ARM 11. while you're at it, please remove this BUG, as it's no longer true for Linux/ARM (though it's still true for FreeBSD/ARM).
Sign in to reply to this message.
On 2012/12/23 10:05:24, dvyukov wrote: > We can fix it in the library. It's possible to implement atomic > operations on non-aligned vars. All LOCK prefixed operations are > atomic regardless of alignment. It will incur one additional branch > for aligned vars, for non-aligned var it will LOCK prefixed > instructions + there is huge overhead for LOCK prefixed operations > that occupy 2 cache lines (it can be 5000+ cycles vs 15 cycles). > I don't know about LL/SC on ARM. ARM ARM specifies LDREXD/STREXD must have 64-bit alignment, or alignment fault will result.
Sign in to reply to this message.
I'm happy to add global variables to the list of aligned things. We align all >=8-byte variables to 8-byte boundaries. "We can fix it" + "huge overhead" doesn't sound like a fix. I think it's fine to tell people who use sync/atomic that they have to work a little bit for it. Especially since it might not be possible on ARM or other future platforms. Russ
Sign in to reply to this message.
On Sun, Dec 23, 2012 at 11:21 PM, Russ Cox <rsc@golang.org> wrote: > I'm happy to add global variables to the list of aligned things. We align > all >=8-byte variables to 8-byte boundaries. > > "We can fix it" + "huge overhead" doesn't sound like a fix. I think it's > fine to tell people who use sync/atomic that they have to work a little bit > for it. Especially since it might not be possible on ARM or other future > platforms. It's possible on any platform with global mutex hashtable. But it will be slower and not "lock-free". What I afraid is that it can lead to extremely hard to diagnose issues and the comment won't actually help people. Perhaps add a runtime check for alignment.
Sign in to reply to this message.
On Monday, December 24, 2012, Dmitry Vyukov wrote: > On Sun, Dec 23, 2012 at 11:21 PM, Russ Cox <rsc@golang.org <javascript:;>> > wrote: > > I'm happy to add global variables to the list of aligned things. We align > > all >=8-byte variables to 8-byte boundaries. > > > > "We can fix it" + "huge overhead" doesn't sound like a fix. I think it's > > fine to tell people who use sync/atomic that they have to work a little > bit > > for it. Especially since it might not be possible on ARM or other future > > platforms. > > It's possible on any platform with global mutex hashtable. But it will > be slower and not "lock-free". using that defeated the very purpose of using atomic instructions. (we already uses that for 64-bit arm atomics in runtime unconditionally, and we should fix that to use native instr. or kernel helper if avail. i want to move part or all of core atomics package into runtime, as their functionality is currently duplicated in runtime. what do you think?) > > What I afraid is that it can lead to extremely hard to diagnose issues > and the comment won't actually help people. Perhaps add a runtime > check for alignment. > agreed. we should panic if the address is not properly aligned. the problem is, should we panic on x86 where unaligned atomics is tolerated? i think we should, to provide an uniform api and help people who develop on 386 for arm.
Sign in to reply to this message.
On Mon, Dec 24, 2012 at 12:21 PM, minux <minux.ma@gmail.com> wrote: >> > I'm happy to add global variables to the list of aligned things. We >> > align >> > all >=8-byte variables to 8-byte boundaries. >> > >> > "We can fix it" + "huge overhead" doesn't sound like a fix. I think it's >> > fine to tell people who use sync/atomic that they have to work a little >> > bit >> > for it. Especially since it might not be possible on ARM or other future >> > platforms. >> >> It's possible on any platform with global mutex hashtable. But it will >> be slower and not "lock-free". > > using that defeated the very purpose of using atomic instructions. It's a difficult question, because hardware uses mutexes in implement atomic ops anyway :) However, of course if we add software mutexes, it will bring blocking to OS level. > (we already uses that for 64-bit arm atomics in runtime unconditionally, > and we should fix that to use native instr. or kernel helper if avail. > i want to move part or all of core atomics package into runtime, > as their functionality is currently duplicated in runtime. what do you > think?) When I proposed it some time ago, Russ said that he does not want to unify them because one is C called and another is Go called. It's possible with an additional level of abstraction :) Maybe Russ has changed his mind... >> What I afraid is that it can lead to extremely hard to diagnose issues >> and the comment won't actually help people. Perhaps add a runtime >> check for alignment. > > agreed. we should panic if the address is not properly aligned. > the problem is, should we panic on x86 where unaligned atomics > is tolerated? i think we should, to provide an uniform api and help > people who develop on 386 for arm. 64-bit atomic load/store are not atomic on 386/amd64 (when unaligned). That was the original issue than I hit with GC on 386 with GOMAXPROCS>1. So there is the same problem.
Sign in to reply to this message.
On Monday, December 24, 2012, Dmitry Vyukov wrote: > > 64-bit atomic load/store are not atomic on 386/amd64 (when unaligned). > That was the original issue than I hit with GC on 386 with > GOMAXPROCS>1. So there is the same problem. > i was confused by your saying "All LOCK prefixed operations are atomic regardless of alignment", so you mean the instruction itself is atomic whereas the memory access is not?
Sign in to reply to this message.
On Mon, Dec 24, 2012 at 1:23 PM, minux <minux.ma@gmail.com> wrote: > > On Monday, December 24, 2012, Dmitry Vyukov wrote: >> >> 64-bit atomic load/store are not atomic on 386/amd64 (when unaligned). >> That was the original issue than I hit with GC on 386 with >> GOMAXPROCS>1. So there is the same problem. > > i was confused by your saying "All LOCK prefixed operations are > atomic regardless of alignment", so you mean the instruction itself > is atomic whereas the memory access is not? 64-bit atomic load on amd64/386 does not use LOCK prefixed instructions.
Sign in to reply to this message.
On Monday, December 24, 2012, Dmitry Vyukov wrote: > On Mon, Dec 24, 2012 at 1:23 PM, minux <minux.ma@gmail.com <javascript:;>> > wrote: > > On Monday, December 24, 2012, Dmitry Vyukov wrote: > >> 64-bit atomic load/store are not atomic on 386/amd64 (when unaligned). > >> That was the original issue than I hit with GC on 386 with > >> GOMAXPROCS>1. So there is the same problem. > > i was confused by your saying "All LOCK prefixed operations are > > atomic regardless of alignment", so you mean the instruction itself > > is atomic whereas the memory access is not? > 64-bit atomic load on amd64/386 does not use LOCK prefixed instructions. > but cas does use lock prefix. i think we can use cas to provide atomic loads just like we do on ARM? is that what you have in mind when saying we can fix the library?
Sign in to reply to this message.
On Mon, Dec 24, 2012 at 1:37 PM, minux <minux.ma@gmail.com> wrote: > > On Monday, December 24, 2012, Dmitry Vyukov wrote: >> >> On Mon, Dec 24, 2012 at 1:23 PM, minux <minux.ma@gmail.com> wrote: >> > On Monday, December 24, 2012, Dmitry Vyukov wrote: >> >> 64-bit atomic load/store are not atomic on 386/amd64 (when unaligned). >> >> That was the original issue than I hit with GC on 386 with >> >> GOMAXPROCS>1. So there is the same problem. >> > i was confused by your saying "All LOCK prefixed operations are >> > atomic regardless of alignment", so you mean the instruction itself >> > is atomic whereas the memory access is not? >> 64-bit atomic load on amd64/386 does not use LOCK prefixed instructions. > > but cas does use lock prefix. i think we can use cas to provide > atomic loads just like we do on ARM? is that what you have in mind > when saying we can fix the library? Yes, that's what I meant. It can hundreds of times slower. I think on ARM one needs just LDX, not the whole CAS. However I understand that there are issues with portability.
Sign in to reply to this message.
I added the global variable note and removed the ARM 64-bit note. Will submit now. We can talk about relaxing the constraint in a followup if there is a compelling reason, but we'd need to believe that it will be possible to relax on every architecture, present and future.
Sign in to reply to this message.
*** Submitted as https://code.google.com/p/go/source/detail?r=b2bc77ca3160 *** sync/atomic: document that users must deal with 64-bit alignment Update issue 599. R=dvyukov, iant, minux.ma CC=golang-dev https://codereview.appspot.com/7001056
Sign in to reply to this message.
|