[PATCH] Basic KSM Support

Be sure to add '#define MADV_MERGEABLE 12' to
bionic/libc/include/sys/mman.h so we can have a central place for this

parse the process memory map and inform madvise common HEAP/Stack
regions on fork from zygote

These then can be used by the kernel KSM module to re-join (copy on write)
pages between the various dalvik processes.

As is on a fully loaded system you can expect ~12MB to be indicated to be
scanned by KSM.. of this 10-15% will be saved; as such a recommend a very
slow scan until as any logic to stop scanning while sleeping will require
another process and remove all memory savings provided by this logic.

To activate on a KSM kernel (2min for full scan of ~16m and 1%CPU):

echo 4000 > /sys/kernel/mm/ksm/sleep_millisecs
echo 128 > /sys/kernel/mm/ksm/pages_to_scan
echo 1 > /sys/kernel/mm/ksm/run

Newer kernels only require the last line.

In future additional memory regions and kernel logic may improve this.

Change-Id: I271cbee74a1f02a89c34c6ee9006a4ea81360154
Zygote: Restrict slave mountspace so Dalvik apps can mount system-wide volumes

Android 4.2 implements multi-user storage using per-process mount
namespaces.  Originally, everything under "/" (the entire filesystem
hierarchy) is marked as a recursive-slave mountspace for all zygote
instances.  This is done so that user-storage sandbox mounts under
/storage/emulated are hidden from other apps and users.

Unfortunately this means that any Dalvik app (actually, any program whose
clone/fork ancestry includes a Dalvik zygote, which is everything except
services spawned directly from init) cannot mount system-wide volumes.
Thus, apps like CifsManager are effectively broken in Android 4.2, since
its cifs mounts are only visible to the CifsManager app itself.  All other
apps see empty mountpoints instead of the mounted volume.  Furthermore,
Linux provides no provision for a process to "escape" a recursive-slave
mountspace in versions prior to Linux 3.8 (setns syscall).

Here, we restrict the slave mountspace to /storage (and, due to a possible
kernel bug, /mnt/shell/emulated) so that Dalvik apps can mount system-wide
volumes elsewhere (with appropriate permission, as in earlier versions of
Android), while retaining full multi-user storage compatibility.

This change requires that a tmpfs volume is mounted as /storage in init.rc.
If this volume is unavailable, then Zygote falls back to the previous
behavior of marking the entire filesystem hierarchy as slave.  It also
implicitly requires that EMULATED_STORAGE_TARGET is path-prefixed by (part
of the subhierarchy of) ANDROID_STORAGE, which is the typical case.
Add -Wno-unused-but-set-variable to the Dalvik project.

This is to meet long term goal that we don't suppress any warnings in
the global makefile.

BUG: 10495043
Change-Id: I1eece42a9f2c43b614cfbeb1397c8071fb3db04a
(cherry picked from commit 0c4625a4e1464544625723b013b3febb1cc9359e)
vm: fix extraneous parentheses warnings

Summary:
This warning is treated as an error with clang and -Werror.
Fixes the following:
  equality comparison with extraneous parentheses

Test Plan: verify

Reviewers: randall.rushing, synergy

Reviewed By: synergy

Differential Revision: http://review.codefirex.com/D598
Small tweak
Add interface for updating process state.

Used only in ART. Stub function added to Dalvik.

Bug: 8981901
Change-Id: Icc4d3c73c7959bae97d43752f3b4c5e146b7cb1f