Closed Bug 661596 Opened 13 years ago Closed 13 years ago

Prevent NSPR from falling back to PR_Atomic functions on Mac when compiler intrinsics are available

Categories

(NSPR :: NSPR, defect)

4.8.9
All
macOS
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: justin.lebar+bug, Assigned: justin.lebar+bug)

References

Details

Attachments

(1 file, 2 obsolete files)

I was profiling on Mac and I noticed many calls to PR_Atomic{Increment,Decrement}.  We'd attempted to get rid of these in bug 592557.

It appears that in the js engine (and perhaps elsewhere) DARWIN is not defined.  This causes pratom.h to decide that __sync_add_and_fetch isn't defined, and we then fall back to the slow path.

Filing under the js component until I can verify that this happens when building other parts of the code too.
This happens outside js, too.  Moving to core/general.

I think it's Mac and Linux, but I haven't tested Linux yet.
Assignee: general → nobody
Component: JavaScript Engine → General
QA Contact: general → general
Hardware: x86 → All
Attached patch WIP v1 (obsolete) — Splinter Review
Works on Mac.  Need to test on Linux.
To be fair, we're not falling back to the method which uses locks; there's pr/include/md/_darwin.h, which points to an assembly version of atomic increment for darwin.

But it's silly -- we should be using the compiler intrinsic, saving a function call, and letting the compiler optimize around the atomic increment.
Summary: Prevent NSPR from falling back to slow PR_Atomic functions → Prevent NSPR from falling back to PR_Atomic functions
Linux and Windows seem to work fine as-is.  I'll see if I can test Android.
(In reply to comment #4)
> Linux and Windows seem to work fine as-is.  I'll see if I can test Android.

Android's gcc 4.4 and 4.4.3 don't have __sync_* built-in functions.  It is required gcc 4.5.2 (for ARM).

If I should write inline assembler for atomic for arm gcc 4.4.x platform, please file a new bug for NSPR.
Looks like the atomics for ARM were done in bug 405992.
Summary: Prevent NSPR from falling back to PR_Atomic functions → Prevent NSPR from falling back to PR_Atomic functions on Mac when compiler intrinsics are available
Attached patch Patch v1 (obsolete) — Splinter Review
Attachment #537148 - Flags: review?(ted.mielczarek)
Attachment #536952 - Attachment is obsolete: true
Comment on attachment 537148 [details] [diff] [review]
Patch v1

Josh, do you think those #undef DARWINs were there for a reason?  We include Cocoa.h plenty of times without undef'ing DARWIN.

The only harm of leaving the undefs in is that we'll call the PR_Atomic functions from these files instead of using the macros.  If you want to leave them, that's OK with me.
Attachment #537148 - Flags: review?(joshmoz)
Comment on attachment 537148 [details] [diff] [review]
Patch v1

We should fix the NSPR pratom.h header.  NSPR public headers should be
self-sufficient and should not require the client to define any macro.

We can change pratom.h to test defined(__APPLE__) instead of defined(DARWIN).
We'd similarly want to change LINUX to __LINUX__ in that case.
...actually, you'd probably want to change all of the OS defines, then, right?

Maybe we can add a header which defines DARWIN, LINUX, etc.  It appears there are many such defines.
Ah, I see; not all of these headers are publicly-consumable.  The include/md folder is private?  If so, looks like we just need to modify pratom.h.
(In reply to comment #5)
> (In reply to comment #4)
> > Linux and Windows seem to work fine as-is.  I'll see if I can test Android.
> 
> Android's gcc 4.4 and 4.4.3 don't have __sync_* built-in functions.  It is
> required gcc 4.5.2 (for ARM).

So just to check, gcc 4.4.x doesn't define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4?  Or does it define it and export a busted __sync_add_and_fetch?
(In reply to comment #13)
> (In reply to comment #5)
> > (In reply to comment #4)
> > > Linux and Windows seem to work fine as-is.  I'll see if I can test Android.
> > 
> > Android's gcc 4.4 and 4.4.3 don't have __sync_* built-in functions.  It is
> > required gcc 4.5.2 (for ARM).
> 
> So just to check, gcc 4.4.x doesn't define
> __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4?  Or does it define it and export a
> busted __sync_add_and_fetch?

gcc 4.4/4.4.3 for *arm* doesn't defined it and doesn't support __sync_* atomic. You can see more details by https://wiki.linaro.org/WorkingGroups/ToolChain/AtomicMemoryOperations.

If __sync_* functions are supported by gcc, __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* is defined by cpp.  I think that you should use __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* instead of LINUX.
> I think that you should use __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* instead of LINUX.

The problem, AIUI, is that on some architectures GCC defines __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* but exports __sync_* intrinsics which aren't actually threadsafe.  So we can only use the intrinsics on architectures where we know they're safe.
Assignee: nobody → justin.lebar+bug
Comment on attachment 537148 [details] [diff] [review]
Patch v1

I don't know why those DARWIN undefs are there, I'm fine with getting rid of them.
Attachment #537148 - Flags: review?(joshmoz) → review+
Attached patch NSPR Patch v1Splinter Review
Attachment #537552 - Flags: review?(wtc)
Attachment #537148 - Attachment is obsolete: true
Attachment #537148 - Flags: review?(ted.mielczarek)
Comment on attachment 537552 [details] [diff] [review]
NSPR Patch v1

r=wtc.  Note that it is fine to test the LINUX macro because
it is defined by NSPR (in mozilla/nsprpub/pr/include/md/_linux.cfg).
Attachment #537552 - Flags: review?(wtc) → review+
I'm not clear what the process is for making changes to NSPR.  Do I check this patch in to m-c, or somewhere else?
NSPR changes need to land in NSPR CVS. wtc or I can land them for you. Getting them into m-c requires one of us to also tag the NSPR repository, and then someone to update the NSPR snapshot in m-c.
(In reply to comment #20)
> NSPR changes need to land in NSPR CVS. wtc or I can land them for you.
> Getting them into m-c requires one of us to also tag the NSPR repository,
> and then someone to update the NSPR snapshot in m-c.

I'll sit tight then.  :)
Whiteboard: [needs checkin to NSPR]
Sorry, I always forget about these bugs:

Checking in pr/include/pratom.h;
/cvsroot/mozilla/nsprpub/pr/include/pratom.h,v  <--  pratom.h
new revision: 3.20; previous revision: 3.19
done
Status: NEW → RESOLVED
Closed: 13 years ago
Component: General → NSPR
Keywords: checkin-needed
Product: Core → NSPR
QA Contact: general → nspr
Resolution: --- → FIXED
Whiteboard: [needs checkin to NSPR]
Target Milestone: --- → 4.8.9
Version: unspecified → 4.8.9
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: