epoll: Reduce lock contetion in epoll_file::wake

I saw lock contention of _activity_lock in epoll_file::wake:

  Collected 30667 samples spanning 11.97 s

  2.73 s (100.00%, #15332) All
   lockfree::mutex::lock() /data/asias/osv/core/lfmutex.cc:87
   |-- 1.82 s (66.64%, #8153) lock_guard /data/asias/osv/external/x64/gcc.bin/usr/include/c++/4.8.2/mutex:414
   |    |-- 1.82 s (66.64%, #8151) lock_guard_for_with_lock /data/asias/osv/include/osv/mutex.h:75
   |    |    |-- 1.69 s (61.90%, #4200) epoll_file::wake(epoll_key) /data/asias/osv/core/epoll.cc:249
   |    |    |    |-- 1.68 s (61.51%, #4102) epoll_wake(epoll_ptr const&) /data/asias/osv/core/epoll.cc:362
   |    |    |    |    file::wake_epoll(int) /data/asias/osv/fs/vfs/kern_descrip.cc:177
   |    |    |    |    poll_wake /data/asias/osv/core/poll.cc:168

With this patch, the contention is gone.

Signed-off-by: Asias He <asias@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
mgmt: update to latest

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
routecache: initialize route cache early

Initialize global variables of route cache
before networking subsystem.

This patch fixes chashes observed on EC2:

page fault outside application, addr: 0x0000000000000000
[registers]
RIP: 0x00000000002577c8 <route_cache::lookup(bsd_sockaddr_in*, unsigned int, rtentry*)+1000>
RFL: 0x0000000000010282  CS:  0x0000000000000008  SS:  0x0000000000000010
RAX: 0x0000000000000000  RBX: 0xffffa00005eb0120  RCX: 0xffff800005eb0210  RDX: 0x00000000000000b7
RSI: 0xffff800005eb0120  RDI: 0x0000000000e290a0  RBP: 0xffff80010258fa70  R8:  0x0000000000e290a0
R9:  0x0000000000e290a0  R10: 0x00171fdfe79c2d73  R11: 0x0000000000000000  R12: 0xffff800005eb0210
R13: 0x0000000000e290a0  R14: 0x00171fdfe79c2d73  R15: 0x0000000000000000  RSP: 0xffff80010258f9e0
Aborted

[backtrace]
0x000000000032851f <???+3310879>
0x0000000000329613 <mmu::vm_fault(unsigned long, exception_frame*)+147>
0x00000000003899f9 <page_fault+105>
0x00000000003888e6 <???+3705062>
0x00000000002649a2 <ip_output(mbuf*, mbuf*, route*, int, ip_moptions*, inpcb*)+2754>
0x000000000027761d <tcp_respond+413>
0x000000000026d2a1 <???+2544289>
0x0000000000271908 <tcp_input+2712>
0x0000000000263769 <ip_input+57>
0x0000000000250871 <netisr_dispatch_src+417>
0x000000000024884e <???+2394190>
0x0000000000250871 <netisr_dispatch_src+417>
0x0000000000288030 <???+2654256>
0x000000000039bd68 <xen::xen_irq::do_irq()+376>
0x000000000039befe <???+3784446>
0x00000000003e394a <thread_main_c+26>
0x0000000000389865 <???+3709029>

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Dmitry Fleytman <dmitry@daynix.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build.mk: replace kernel base include file

with a definition in build.mk, which allows to set different
base addresses for different architectures, avoids the call
to the preprocessor, and makes it visible in the build.mk,
which makes it more accessible for architectures where the
base address might need to be adjusted.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
libc: missing __isnanf

Implement that, together with the ones we already have.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
pthread: implement missing scope functions

Those functions set and get the scheduling scope of a thread. Luckily for us,
out of the two scheduling scopes defined, Linux only supports one
(PTHREAD_SCOPE_SYSTEM). The implementation of these functions is therefore
trivial.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
libc: implement ftime.c

This is an import from musl. It exists in a newer version, but not in the one
we're using. When we update, we can get rid of this one. But that has to be a
concious and coordinated decision.

For now let's bring it in and keep it verbatim.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
aarch64: gic: set target mask to zero for the uniproc

in commit ad8466ba18d24c72c96c4d78d173f5042974f5f7 we removed
an abort and changed it to a warning when we cannot read our
own cpu mask target from the GIC, as zero is allowed as a
special case for uniprocessor.
However, we logically still need to set the cpu target mask
to zero in our gic driver state.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Cc: Geyslan G. Bem <geyslan@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
sched: add tracepoint for wakeup IPIs

Add a tracepoint "sched_ipi" for IPIs sent to wake up a thread on a
remote CPU.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
sched: add tracepoint for yield

Add a tracepoint "sched_yield" for sched::thread::yield(), sched_yield()
or pthread_yield() calls, and a second tracepoint "sched_yield_switch"
when the yield operation actually yielded (i.e., there was another
thread in the run queue).

Some sources on the Web suggest that Java uses sched_yield() heavily
and yield's behavior makes a big difference (see issue #482).
However, with this tracepoint, I disproved this conjecture when it
comes to workloads that interest us: For example, in Cassandra,
yield() is rarely used (10 times a second most of the time, 1000 times
a second in rare cases), and when it is used, there is usually no other
thread to yield to and we never see more than a couple of sched_yield_switch
occurances per second. So the implementation of yield() is of no
consequence for Cassandra's performance.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
sched: add tracepoint for rescheduling

Add a tracepoint "sched_sched" for scheduler runs. Note that
scheduler may decide to do nothing (continuing to run the current
thread), so the sched_switch frequency is <= the sched_sched frequency.

Showing the frequency of both sched_sched and sched_switch in one
example workload (Cassandra), I noticed that in about half of the
reschedule calls we decide not to context switch. This suggests it
might be a good idea to optimize this case, to realise as quickly as
possible there is nothing to do. We already try to return quickly,
but maybe we can do this even more quickly.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tests:remove unused code in misc-timeslice.cc

The _loop function is no long used here,so remove it.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Haifeng Gao <gaohaifeng.gao@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
setup.py: fixing typo in Ubuntu packages list

Reviewed-by: Tomasz Grabiec <tgrabiec@gmail.com>
Signed-of-by: William Dauchy <william@gandi.net>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
setup.py: adding base support for debian

also update the base list package for debian jessie in README

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
epoll: Do not search again in EPOLLONESHOT case

The f_lock is not dropped. No need to searth the key in the map again.

Signed-off-by: Asias He <asias@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
epoll: Update comments in epoll::wait

To reflect the current locking scheme.

Signed-off-by: Asias He <asias@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
epoll: Fix clearing evt.events in EPOLLONESHOT case

In commit e8a60448031b07cf7ae9e71ce269ad6eea07c46e (epoll: Do not search
again in EPOLLONESHOT case), when we clear evt.events, we clear a local
copy instead of the one in the map. Fix by taking a reference of
epoll_event.

Passed scripts/run.py -e "/tests/tst-epoll.so" test.

Signed-off-by: Asias He <asias@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
scripts: clean leading whitespaces in python scripts

Signed-off-by: William Dauchy <william@gandi.net>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
scripts: remove unused import

Signed-off-by: William Dauchy <william@gandi.net>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
libc: import getsrvbyname from musl

In conjunction with a module-provided /etc/services, allows programs to find out where
to register. MySQL needs it.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>