loader: remove redundant postfix incrementation

It only tricks a (hasted) reader that this actually means that we want
to skip first element but in fact, because it is postfix, it has no
effect whatsoever.
loader: handle opt_leak before commands are run
loader: print boot chart once, before commands are run
core: introduce applicaton abstraction

This introduces an abstration for OSv processes, called
"application". It represents an execution of a single command. The
goal of having this is to have a uniform interface for managaing
processes on OSv. This class is responsible for tracking all resources
associated with a running application.

For example, this will be used to track termination callbacks which an
application can register and which will be executed when application
termination is requested.
core: track current application context

We need to know to which application current thread belongs so that we
can expose per-application APIs like termination callback registreation.

In the future this could be also used to decide which threads should
be terminated when application falls out of its main().
core: add support for requesting application's termination

Application can register as many callbacks as it wants. The callbacks
will be executed exactly once when after application termination is
requested. If callback is registerred after termination was already
requested it is executed immediately. This way we avoid races between
callback registration and termination.
core: introduce application::get_return_code()
core: sprinkle tracepoints around application management code
core: add application::get_command()
httpserver: add termination handler
core: request termination of detached commands before proceeding to shutdown

Letting commands run while we shut down may result in page faults due
to DSOs getting unmapped while in use. This is often the case with
httpserver. This change will make OSv trigger termination of detached
commands before it shuts down. The callback is supposed to cause the
application to return from its main() in a timely manner.
core: integrate osv::run with osv::application

osv::application models a process more accurately than osv::run and
hence it should be used instead in most of the places which currently
use osv::run. This change moves osv::run() logic into osv::application
and makes the run() variants create an application.

Because users of run() are expected to get informed whether
application was run or not we need to change the API so that
application() will thrown a launch_error, instead of aborting, which
can be then caught by interested parties. This has a nice feature of
propagating more details about why we can't run the application.
tests: add test for application termination requesting
memory: don't allocate huge page range if it is not big enough

That's embarassing. It is obviously possible that even the largest of the
huge page ranges is not big enough, so blindly taking the biggest huge
area and trying to use it in allocation without any size check is a very
wrong thing to do. Found while looking at the code.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
loader.py: get free memory from memory::free_memory

As Zifei suggested there is no need to iterate over all unallocated
page ranges to get the amount of free memory.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
memory: call on_alloc()/on_free() hooks in bitmap allocation

page_range_allocator::_bitmap, because of its special role, uses custom
allocator. There is no reason, though, not to call on_alloc() and on_free()
hooks.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
memory: don't allocate huge page range if it is not big enough

That's embarassing. It is obviously possible that even the largest of the
huge page ranges is not big enough, so blindly taking the biggest huge
area and trying to use it in allocation without any size check is a very
wrong thing to do. Found while looking at the code.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
memory: update memory allocation related comments

Recent changes in memory allocation algorithms made some of the comments
outdated.

Signed-off-by: Paweł Dziepak <pdziepak@quarnos.org>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
Merge branch 'stable/v0.11'
Fix empty-path case of futimesat()

The Linux futimesat(2) specifies that:

  Glibc notes: If pathname is  NULL, then the glibc futimesat() wrapper
  function updates the times for the file referred to by dirfd.

In other words, when pathname==NULL, futimesat() behaves like futimes(),
at this works whether or not the given fd is a directory.

We did not support this corner case, and it turns out that OpenJDK
uses exactly this case: futimesat(fd, NULL, times), so not supporting
this resulted in weird "Not a directory" message when trying to change
the times of a non-directory file.

Since futimes(fd, times) is equivalent to the newly fixed
futimesat(fd, NULL, times), we also implement the former function (missing
until now) by calling the latter.

Fixes #425 (Lyor, please confirm).

This patch also adds tests for this case.

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