Fix SCOPE_LOCK to be usable twice in the same scope.

Asias discovered that if SCOPE_LOCK is used twice in the same block,
it gives a compilation error because the same lock name is used for
both.

The existing code attempted to solve this problem using __COUNTER__,
but did it wrongly... The problem is that the "##" c-preprocessor
operator does concatenation before macro expansion, so the literal string
"__COUNTER__" was used, not a numeric counter as we had hoped.
To do this correctly, we need to pass the task to another macro, or
two actually!

A more detailed explanation about the solution:

* #define CONCATENATE2(x, y) x##y
This macro concatenates the two literal arguments passed to it, and only
then does macro expansion. Naively calling CONCATENATE2(_SCOPE_LOCK,
__COUNTER__) would result in just the string _SCOPE_LOCK__COUNTER.

* #define CONCATENATE(x, y) CONCATENATE2(x, y)
CONCATENATE(_SCOPE_LOCK, __COUNTER__) expands to CONCATENATE2(_SCOPE_LOCK,
<actual counter>) and that is now exapnded again to get the actual
behaviour we need.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
rwlock: Prevent construction of rwlock_for_* objects

rwlock_for_read, rwlock_for_write are just meant to be interfaces, and
shouldn't be instanciated. We have a private constructor to ensure that
they are never constructed. Unfortunately, because of a gcc (<4.9) bug
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56429) an explicit
"=default" constructor is always public, so this protection doesn't
work at all, and rwlock_for_* objects can be created (and obviously
crash when used).

This patch changes the "default" constructor to an empty constructor,
which does the same thing but avoids this gcc bug.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
zfs: automatically import extra pools

This patch will provide OSv with an important functionality when
multiple ZFS pools are being used in the system. It follows a ZFS on
Linux scheme where all available pools in the system are imported and
their dataset mounted during system initialization process.

It was tested both locally and on EC2.

Details on EC2 test: Started a EC2 instance, created a new ZFS pool
comprising two additional non-ephemeral disks, stopped the instance,
re-started the instance, meaning a new OSv instance, and checked that
the new ZFS pool would be automatically available on subsequent OSv
instances.

Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build: parallel build support for modules

Pass the -j and --jobserver-fds (an internal make option) to submakes so
that modules can be built in parallel.

Currently, the modules themselves are built serially, but each individual
module is built in parallel.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Fix spinlock busy-loop to use non-atomic test

Optimize spin_lock() to busy-loop without using an atomic operation in the
contended case. This reduces the need for inter-CPU communication because cache
line does not need to be locked for every iteration of the loop. This improves
spinlock performance for the contended case.

Before:

    Running mutex tests

    Sizes of mutual exclusion primitives:
    Size of lockfree::mutex: 40
    Size of spinlock: 1
    Size of rwlock: 96

    ==== BENCHMARK 1 ====
    Uncontended single-thread lock/unlock cycle:
    Measuring uncontended lockfree::mutex lock/unlock: 29 ns
    Measuring uncontended handoff_stressing_mutex lock/unlock: 51 ns
    Measuring uncontended spinlock lock/unlock: 13 ns
    Measuring uncontended rwlock_read_lock lock/unlock: 63 ns

    ==== BENCHMARK 2 ====
    Contended tests using increment_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads
    AAAAAAAAAABBBBBBBBBB
    26 ns
    Contended mutex test, lockfree::mutex, 4 pinned threads
    AAAAAAAAAADCBDCBDCBDCBDCBDCBDCBDCBDCBDCB
    709 ns
    Contended mutex test, lockfree::mutex, 20 non-pinned threads
    ABCDEFGHIJKLMNOPQRSTABDFGJHRKLNOIPMSTCQEADGFJBRKHNOLCQSIMEPTADGBRJKHFNOLCISQMEPTADGRBKHFJNOLISMECPQTADGRBKHFJNOLEMIQSPTCADRGBHJKNOLEIMTSQCPFADBGRJKHNOLEISMPCQTFADBGRJHKNOLIESPCQMTFAGJBRDHKONLIESPTMQCF
    672 ns
    Contended mutex test, spinlock, 2 pinned threads
    ABAAAAAAAAABBBBBBBBB
    52 ns
    Contended mutex test, spinlock, 4 pinned threads
    AAABDCABDBDBDBDBDBDBDBADCBDACACACACACCCC
    252 ns
    Contended mutex test, spinlock, 20 non-pinned threads
    AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTTAABBCCDDEEFFGHIJTGKHLMNOPQRSIJTABCDFEGKLJKRKRHCRKHCFQMRSTKEFCNITNHRAKPCEOBFDTHANRKCEFDTHANJREDFTGAHNDIELAONJDQPOIJQBLPOJIQBMPOJSIQPONBMIQPOMSQLMGSPMSGBLGMLGLSS
    253 ns
    Contended mutex test, rwlock_read_lock, 2 pinned threads
    ABABABABABABABABABAB
    1531 ns
    Contended mutex test, rwlock_read_lock, 4 pinned threads
    ABCDACDBACDBACDBACDBACDBACDBACDBACDBACDB
    2167 ns
    Contended mutex test, rwlock_read_lock, 20 non-pinned threads
    ABCDEFGHIJKLMNOPQRSTBAGCKLONEIDTMJQFSHRPBACGODKLIENMJTQFHSRPBACOGLEDIKTJNMQFSHRPBACITGLEDKJOMNQFSHRPBACILTEOGDJKMNQFHSRPBACIELODGNKJMQFHSRPTBACIELGODKMNJQFHSRPTBAIECLGDKOMJNQFHSPRTBAICLDEKGMONJQFHSPRT
    1432 ns

    ==== MISC TESTS ====

    Trylock tests using spinning_increment_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads
    AAABABABABABABAABBBB
    101 ns

    Mutual exclusion test using checker_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads

    2087 ns
    Contended mutex test, lockfree::mutex, 4 pinned threads

    2956 ns
    Contended mutex test, lockfree::mutex, 20 non-pinned threads

    2259 ns
    mutex tests succeeded

After:

    Running mutex tests

    Sizes of mutual exclusion primitives:
    Size of lockfree::mutex: 40
    Size of spinlock: 1
    Size of rwlock: 96

    ==== BENCHMARK 1 ====
    Uncontended single-thread lock/unlock cycle:
    Measuring uncontended lockfree::mutex lock/unlock: 29 ns
    Measuring uncontended handoff_stressing_mutex lock/unlock: 51 ns
    Measuring uncontended spinlock lock/unlock: 13 ns
    Measuring uncontended rwlock_read_lock lock/unlock: 64 ns

    ==== BENCHMARK 2 ====
    Contended tests using increment_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads
    AAAABABABABBABABABBB
    484 ns
    Contended mutex test, lockfree::mutex, 4 pinned threads
    BACDBACDBACDBACDBACDBACDBACDBACDBACDBACD
    902 ns
    Contended mutex test, lockfree::mutex, 20 non-pinned threads
    ABCDEFGHIJKLMNOPQRSTABCEGHILMNROPSQJKFDTABCEGLMIHORFPJKQSTDNABCELGIMOHKPJQSDNTRFABCELIGMHOSPTNKFDQJRABCLEIGMHTKSONDPFJQRABCLEIGMHTKFDSOPJNQRABCLGEIMTHDFKPSJQONRABCGLEIMHTDFKOPQSNJRABCEGLIMHDFTKPSNJOQR
    592 ns
    Contended mutex test, spinlock, 2 pinned threads
    BAAABAAAAABAABBBBBBB
    34 ns
    Contended mutex test, spinlock, 4 pinned threads
    BACDACBADCACBACDACABCDACACBDACBDBDBDBDBD
    68 ns
    Contended mutex test, spinlock, 20 non-pinned threads
    AAABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTAAAAAAATDDFEBBGGIIHHJJTKKLLMMOOPPQQRRSCNDFEBGIHJTKLMOPQSCNFERBGDJTKLIMOPQSCNFERBDTKLIMHQOSCNFEPRDLIHJMQOGSCNFEPRDHJMQOSCNFEPTBRIGDLKQOSCNFEPTBRIGLNTBJGMCSHKJHK
    16 ns
    Contended mutex test, rwlock_read_lock, 2 pinned threads
    BABABABABABABABABABA
    1302 ns
    Contended mutex test, rwlock_read_lock, 4 pinned threads
    ACBDABDCABCDABCDACDBACDBACDBADCBADCBACDB
    1773 ns
    Contended mutex test, rwlock_read_lock, 20 non-pinned threads
    ABCDEFGHIJKLMNOPQRSTABDGLNCQIJEFRSOMKHTPABNLDGQICEORSJPHMKFTANBGQDILCOESHPRJFMKTALIQCBDNGSOEHRJPFMKTADIBQCLNGRSHEOPFJKMTAIBDCNGLQRHESOFPJMKTAICBNDLGHRQEOPFSJKMTANBLCIDGHREQPOSFJKMTALBNCDGIRHQEOSJPFMKT
    1249 ns

    ==== MISC TESTS ====

    Trylock tests using spinning_increment_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads
    BABABBABABABABBABAAA
    92 ns

    Mutual exclusion test using checker_thread:
    Contended mutex test, lockfree::mutex, 2 pinned threads

    2652 ns
    Contended mutex test, lockfree::mutex, 4 pinned threads

    5129 ns
    Contended mutex test, lockfree::mutex, 20 non-pinned threads

    2004 ns
    mutex tests succeeded

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>