erlang/otp — pull request #528 by arekinath (2014-11-03 01:22:04+00:00)
Title: os_mon: cpu_sup should use native sysctl/libkvm calls on BSD

Currently the cpu_sup module in os_mon forks out with os:cmd on most BSD platforms, to run "uptime" or "ps" to get things like the load average and number of running processes. There is also a C port driver to get this information directly from the system on Solaris and Linux.

This patch extends the C port driver to support FreeBSD, OpenBSD, Darwin (Mac OSX) and DragonFlyBSD, and replaces the os:cmd calls. This is much faster and lighter on the system, and relies on less fragile interfaces (eg, using getloadavg(3) which has been unchanged since 4.3BSD, as opposed to relying on the exact text output format of the "uptime" command).

The motivation for this is mostly Riak, which calls the load average functions about once a second (to decide when load is low enough to run cleanup tasks etc). Riak processes get quite large in RSS terms, and the overhead of forking them and then cleaning up the child all the time can be quite expensive on these platforms. This is particularly the case on OpenBSD, where the big kernel lock still exists -- the cleanup of these processes can lock up the entire system for milliseconds at a time.

I've compiled and tested this patch on all 4 platforms, at their latest release versions.