Improve benchmarking around stringPrintf functions

Summary:
Before optimizing this code, it is better to measure different
aspects of it to ensure improvements are worth it and don'tintroduce
other regressions.

This adds a new benchmark as well as parameterizes an old one.

Test Plan: run it

Reviewed By: lovro@fb.com

Subscribers: lins, anca, folly-diffs@

FB internal diff: D1733760

Tasks: 5735468

Signature: t1:1733760:1418313479:9f572d5a4cf014bd266d91d0f7a75407d1514f65
AsyncUDPSocket

Summary:
Move AsyncUDPSocket to folly.

There is also one under realtime/voip/async that looks functionaly equivalent?  I think this one is only used in gangplank currently.

Test Plan: contbuild

Reviewed By: hans@fb.com

Subscribers: trunkagent, doug, alandau, bmatheny, njormrod, mshneer, folly-diffs@

FB internal diff: D1710675

Tasks: 5788116

Signature: t1:1710675:1417477000:9aebb466757554a5fa49d7c36cb504b4d8711b68
Add convenience method for folly::join

Summary: While folly has join methods that return a string (and thus don't need a string referenced passed), it does not support this idiom with iterators. This diff adds support for calls like folly::join("/", itr.begin(), itr.end() - 1).

Test Plan: Unit test

Reviewed By: njormrod@fb.com

Subscribers: philipp, trunkagent, njormrod, folly-diffs@, yfeldblum

FB internal diff: D1702892

Tasks: 5691439

Signature: t1:1702892:1418344206:9c1736f5d8e41be1df71a415e3803fe846b387b7
Improve performance of stringPrintf and related functions

Summary:
It turned out at least one optimization we were doing for
stringPrintf (using the tail of the input buffer) was causing a
performance degradation in some cases -- if the string was already
pre-sized, our resize() call would end up memset'ing the tail.  In some
instances, this could cause significant performance penalties, such as
when multiple stringAppendf calls were made.

So, this diff removes the "optimization" around using the tail of the input
buffer and instead uses a standalone stack buffer.  If vsnprintf deems
that buffer too small, a heap buffer is instead used.

As there is no std::string method that resizes the string to fill the
underlying buffer without setting the values to a default, and as it is
not legal to write past the end of the data buffer (even if capacity()
says there is enough), let's just abandon that optimization.  It turns
out this doesn't have a major performance loss for most cases since,
with this diff, most small strings will fit on-stack and then hopefully
in the string's tail anyway.

Test Plan: runtests

Reviewed By: simpkins@fb.com

Subscribers: trunkagent, net-systems@, lins, anca, folly-diffs@

FB internal diff: D1733774

Tasks: 5735468

Signature: t1:1733774:1418323943:ec47007c9756aca6cf0466bce92722ac4c7e89b2
move wangle/Executor.h to folly/ root

Summary:
this removes the dep EventBase has on wangle as we prepare to split off wangle
also changes namespace from folly::wangle to folly

Test Plan: just a couple of codemods so waiting for contbuild

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, fbcode-common-diffs@, chaoyc, search-fbcode-diffs@, zeus-diffs@, nli, cold-storage-diffs@, unicorn-diffs@, targeting-diff-backend@, hannesr, vighnesh, fugalh, alandau, bmatheny, adityab, zhuohuang, luk, darshan, gunjan, hdoshi, dzhulgakov, alihussains, panin, ves, mshneer, folly-diffs@, lins, nimishshah

FB internal diff: D1737376

Tasks: 5802833

Signature: t1:1737376:1418423430:82d219c34fcc50218c380a17435e5880e53db6bd
global io executor

Summary:
This is something we've talked about for a while. It's also an alternative to the mechanism in D1714645.
If we like it, I'll do something similar for a global cpu executor. That functionality should probably just be
baked into Executor itself instead of a separate subclass, which is why the IOExecutor stuff is in Executor.h/.cpp,
because it'll be pretty similar. The main exception is that for getCPUExecutor() you could return a default global
InlineExecutor instead of exploding as in getIOExecutor()

Test Plan: wangle unit, will start plumbing this into the services in #5003045 if we like it

Reviewed By: davejwatson@fb.com

Subscribers: hannesr, trunkagent, fugalh, alandau, mshneer, folly-diffs@, bmatheny

FB internal diff: D1727894

Tasks: 5002442

Signature: t1:1727894:1418344077:9e54088a6acb3f78e53011a32fd1dfe8b3214c1d
Remove locking when getting ptr to Singleton

Summary: This removes one layer on locking on the fast path, when ptr to singleton object is read from SingletonEntry.

Test Plan:
unit test

Before:

============================================================================
folly/experimental/test/SingletonTest.cpp       relative  time/iter  iters/s
============================================================================
NormalSingleton                                            335.26ps    2.98G
MeyersSingleton                                   99.50%   336.96ps    2.97G
FollySingleton                                     0.28%   120.64ns    8.29M
============================================================================

After:

============================================================================
folly/experimental/test/SingletonTest.cpp       relative  time/iter  iters/s
============================================================================
NormalSingleton                                            336.76ps    2.97G
MeyersSingleton                                   99.91%   337.07ps    2.97G
FollySingleton                                     0.36%    92.69ns   10.79M
============================================================================

Reviewed By: alikhtarov@fb.com

Subscribers: trunkagent, folly-diffs@

FB internal diff: D1727604

Signature: t1:1727604:1418701171:1728b516191a8ec4439e981d78634370b4bcf7a1
codemod: folly/wangle/ -> folly/wangle/futures

Summary: Last thing before moving experimental/wangle here. Once everything is in the same directory I'm probably going to consolidate experimental/wangle/concurrent with the executors in this directory into wangle/executors/. And probably rename some of these targets. For now, a dumb move is enough.

Test Plan: waiting for contbuild

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, fbcode-common-diffs@, chaoyc, search-fbcode-diffs@, lars, ruibalp, hero-diffs@, zeus-diffs@, vikas, danielg, mcduff, cold-storage-diffs@, unicorn-diffs@, ldbrandy, targeting-diff-backend@, netego-diffs@, fugalh, adamsyta, atlas2-eng@, alandau, bmatheny, adityab, everstore-dev@, zhuohuang, sweeney, mwa, jgehring, smarlow, akr, bnitka, jcoens, luk, zhguo, jying, apodsiadlo, alikhtarov, fuegen, dzhulgakov, mshneer, folly-diffs@, wch, tingy, hannesr

FB internal diff: D1740327

Tasks: 5802833

Signature: t1:1740327:1418752541:82d7486293b0a12938730ae66d480c120aded4dc
fix Futex when steady_clock and system_clock precisions differ

Summary:
To handle the strange relationship between steady_clock and
system_clock (on some platforms these represent only one type, on some
platforms they are separate) Futex::futexWaitUntil converts the deadline
to a duration and back.  On Xcode 6 system_clock is measured in
microseconds and steady_clock in nanoseconds, resulting in a compilation
failure.  This diff fixes it.

Test Plan:
1. compile snippet manually using Xcode
2. new unit test that causes the same implicit conversion failure in a slightly different way

Reviewed By: mssarang@fb.com

Subscribers: trunkagent, folly-diffs@

FB internal diff: D1740903

Tasks: 5831196

Signature: t1:1740903:1418754770:32c999abf1dc87415ffebf45083a903abbded9f2
Add specific multi-bind to AsyncServerSocket

Summary:
Add a bind api similar to binding by port across all interfaces.
However, this bind will only attempt to bind sockets to the supplied
IP addresses. I'd like to add this to aid in moving TURN to IPv6.

TURN will use two specified addresses, one for IPv4, and one for IPv6, to
handle incoming connections from clients. In order to avoid duplicating
workers, we'd like to take advantage of the multi-socket implementation
of AsyncServerSocket. However, we don't want to bind to all interfaces,
especially for port 443.

Test Plan:
Plan to test by adapting TurnTcpListener to use this new API. See that
1. using one address still works as expected,
2. using zero addesses will cause exception
3. using multiple addresses works as expected

Will write unit tests if needed

Reviewed By: davejwatson@fb.com

Subscribers: hannesr, trunkagent, net-systems@, folly-diffs@, naizhi

FB internal diff: D1729442

Tasks: 3633642

Signature: t1:1729442:1418752467:22a60da4ec9009ea0b7fe28a8a436a179e0449aa
get_fast/get_weak_fast API for folly::Singleton

Summary: This adds API which makes folly::Singleton as performant as Meyers/static-object singletons.

Test Plan:
unit test + benchmark

============================================================================
folly/experimental/test/SingletonTest.cpp       relative  time/iter  iters/s
============================================================================
NormalSingleton                                            333.32ps    3.00G
MeyersSingleton                                  100.00%   333.33ps    3.00G
FollySingletonSlow                                 0.35%    94.36ns   10.60M
FollySingletonFast                                99.43%   335.24ps    2.98G
FollySingletonFastWeak                             0.62%    53.74ns   18.61M
============================================================================

Reviewed By: alikhtarov@fb.com

Subscribers: trunkagent, folly-diffs@

FB internal diff: D1741961

Signature: t1:1741961:1418765462:d9806f1bf5275bfbe2c4c53a41b735bda93753fe
rename io::PortableSpinLock to SpinLock

Summary:
folly::io::PortableSpinLock seems generally useful outside of the io
code.  This moves it into the base folly namespace, and renames it from
PortableSpinLock to just SpinLock.

For most users, the main difference between MicroSpinLock and SpinLock
is that SpinLock provides a constructor that does the right thing, while
MicroSpinLock has to be explicitly initialized.

Test Plan:
Added some new unit tests, and tested both the MicroSpinLock and
pthread_spinlock_t implementations.  I didn't test the Mac OS version,
although that code remains unchanged.

Reviewed By: seanc@fb.com

Subscribers: trunkagent, doug, net-systems@, exa, folly-diffs@

FB internal diff: D1734632

Signature: t1:1734632:1418394033:99f6fbe73b485a1d631a2ef7d1b39ea6f49ecb0b
update call sites to use SpinLock

Summary:
Update call sites to use the new folly::SpinLock name, instead of
folly::io::PortableSpinLock.

Test Plan: Built and ran the folly unit tests.

Reviewed By: davejwatson@fb.com

Subscribers: doug, net-systems@, exa, folly-diffs@

FB internal diff: D1734647

Signature: t1:1734647:1418334925:1bbcffccd06907224de2a102f8d4bfbe1bd62fd1
remove PortableSpinLock.h

Summary:
Remove the PortableSpinLock header file now that all call sites have
been migrated to the new SpinLock name.

Test Plan: Built and ran unit tests for folly, thrift, and proxygen.

Reviewed By: seanc@fb.com

Subscribers: trunkagent, doug, net-systems@, exa, folly-diffs@

FB internal diff: D1734685

Signature: t1:1734685:1418335405:e834f64d7282393e942a1de3638f4cf67410eaab
SpinLock improvements

Summary:
This makes several improvements to the SpinLock code:

- Add a SpinLock implementation using pthread_spinlock_t.  On non-x86_64
platforms this is preferred over the pthread_mutex_t implementation
when available.

- For the pthread implementations, throw std::system_error on error,
rather than completely aborting the program using glog's CHECK()
macros.

- Update the pthread_mutex_t implementation to call
pthread_mutex_destroy() on destruction.

- Always unit test all implementations that can be compiled on the
current build platform, even though only a single implementation will
be selected as folly::SpinLock.  This way x86_64 builds will still
unit test the pthread-based implementations.

Test Plan: Ran the unit tests.

Reviewed By: seanc@fb.com

Subscribers: trunkagent, doug, net-systems@, exa, folly-diffs@

FB internal diff: D1735770

Signature: t1:1735770:1418445953:b238aa8fb835a8d55e6e98e20c4615ae1938b98f
fix bug with elf object left with file open upon error causing Entered fatal signal handler recursively later

Summary: fix bug with elf object left with file open upon error causing Entered fatal signal handler recursively later

Test Plan: run publisher at the limit

Reviewed By: tudorb@fb.com

Subscribers: trunkagent, chip, folly-diffs@

FB internal diff: D1743052

Tasks: 5752190

Signature: t1:1743052:1418780889:ad2ee0e6999cf417c2b9fd1ef6bc82d2086774a1
fix the open source build

Summary: Fix the path to detail/SpinLockImpl.h in Makefile.am

Test Plan: Will confirm the Jenkins builds pass for fbthrift and proxygen.

Reviewed By: davejwatson@fb.com

Subscribers: doug, net-systems@, exa, folly-diffs@

FB internal diff: D1745795

Signature: t1:1745795:1418841636:8a861275a62b018a8181541c7d70ba0890d7129f
stabilize IOFuturePool test

Summary: see attached task. concurrency is hard.

Test Plan: stress ran the hell out of it, induced failures before, can't after

Reviewed By: hans@fb.com

Subscribers: fugalh, folly-diffs@

FB internal diff: D1746879

Tasks: 5589311

Signature: t1:1746879:1418860467:0ec3f51a398eee8a836532770682a4c58758ce93
Disable implicit conversions from std::string for non-char* Range

Summary:
`Range<Iter>` has an implicit constructors from strings for any
`Iter`, however such constructors are invalid (compilation fails)
if `Iter` is not `[const] char *`.

This can be an issue for overload resolution: for example

struct IsString {
bool operator()(folly::Range<int*>) { return false; }
bool operator()(folly::StringPiece) { return true; }
};

IsString()(std::string());

fails to compile because the overload is ambiguous, even if the
conversion to `ByteRange` is invalid.

This patch disables all the invalid constructors from
`[const] char*`, `std::string`, and `fbstring`.

Test Plan:
fbconfig -r folly && fbmake runtests_opt

Reviewed By: philipp@fb.com

Subscribers: folly-diffs@

FB internal diff: D1746899

Signature: t1:1746899:1418868361:50784c4993df0bd96eeb62c09c659d5e53964d9b
remove Cpp2WorkerFactory

Summary:
In preparation for a global IOThreadPoolExecutor - we can't override the workerFactory if we are using a global one.

Instead, add a new getEventBases() to IOTHreadPoolExecutro and just run code on each eventBase.  For shutdown, explicitly add a stop(), instead of doing the work after loop() as exited

Cpp2Worker required minor changes to be able to use EventBaseManager's eventbase vs. their own

Test Plan: fbconfig -r thrift/lib/cpp2; fbmake runtests

Reviewed By: jsedgwick@fb.com

Subscribers: trunkagent, andrewcox, doug, fugalh, alandau, bmatheny, mshneer, folly-diffs@

FB internal diff: D1734795

Signature: t1:1734795:1418759367:b45f419e5cbc482a64a5873d9e00e3bcb071db5a