Repository: mesos Updated Branches: refs/heads/master 277565709 -> 7aafb8e44
Split os::memory() out into platform specific files. Review: https://reviews.apache.org/r/42757/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7aafb8e4 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7aafb8e4 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7aafb8e4 Branch: refs/heads/master Commit: 7aafb8e44d347a03cbef83d3f7ee4705b9d23c09 Parents: 2775657 Author: David Forsythe <[email protected]> Authored: Mon Feb 8 13:59:37 2016 -0800 Committer: Ian Downes <[email protected]> Committed: Mon Feb 8 13:59:37 2016 -0800 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/os/freebsd.hpp | 59 +++++++++ .../3rdparty/stout/include/stout/os/linux.hpp | 26 ++++ .../3rdparty/stout/include/stout/os/osx.hpp | 49 +++++++ .../3rdparty/stout/include/stout/os/sunos.hpp | 7 + .../3rdparty/stout/include/stout/posix/os.hpp | 130 ------------------- 5 files changed, 141 insertions(+), 130 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/7aafb8e4/3rdparty/libprocess/3rdparty/stout/include/stout/os/freebsd.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/freebsd.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/freebsd.hpp index 10346de..6b81bf1 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/freebsd.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/freebsd.hpp @@ -70,6 +70,65 @@ inline Try<std::set<pid_t>> pids() return result; } + + +// Returns the total size of main and free memory. +inline Try<Memory> memory() +{ + Memory memory; + + const Try<int64_t> physicalMemory = os::sysctl(CTL_HW, HW_PHYSMEM).integer(); + if (physicalMemory.isError()) { + return Error(physicalMemory.error()); + } + memory.total = Bytes(physicalMemory.get()); + + const int pageSize = getpagesize(); + + unsigned int freeCount; + size_t length = sizeof(freeCount); + + if (sysctlbyname( + "vm.stats.v_free_count", + &freeCount, + &length, + NULL, + 0) != 0) { + return ErrnoError(); + } + memory.free = Bytes(freeCount * pageSize); + + int totalBlocks = 0; + int usedBlocks = 0; + + int mib[3]; + size_t mibSize = 2; + if (::sysctlnametomib("vm.swap_info", mib, &mibSize) != 0) { + return ErrnoError(); + } + + // FreeBSD supports multiple swap devices. Here we sum across all of them. + struct xswdev xswd; + size_t xswdSize = sizeof(xswd); + int* mibDevice = &(mib[mibSize + 1]); + for (*mibDevice = 0; ; (*mibDevice)++) { + if (::sysctl(mib, 3, &xswd, &xswdSize, NULL, 0) != 0) { + if (errno == ENOENT) { + break; + } + return ErrnoError(); + } + + totalBlocks += xswd.xsw_nblks; + usedBlocks += xswd.xsw_used; + } + + memory.totalSwap = Bytes(totalBlocks * pageSize); + memory.freeSwap = Bytes((totalBlocks - usedBlocks) * pageSize); + + return memory; +} + } // namespace os { #endif http://git-wip-us.apache.org/repos/asf/mesos/blob/7aafb8e4/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp index f8a4d5a..182ac9c 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp @@ -139,6 +139,32 @@ inline Try<std::set<pid_t>> pids() return proc::pids(); } + +// Returns the total size of main and free memory. +inline Try<Memory> memory() +{ + Memory memory; + + struct sysinfo info; + if (sysinfo(&info) != 0) { + return ErrnoError(); + } + +# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 23) + memory.total = Bytes(info.totalram * info.mem_unit); + memory.free = Bytes(info.freeram * info.mem_unit); + memory.totalSwap = Bytes(info.totalswap * info.mem_unit); + memory.freeSwap = Bytes(info.freeswap * info.mem_unit); +# else + memory.total = Bytes(info.totalram); + memory.free = Bytes(info.freeram); + memory.totalSwap = Bytes(info.totalswap); + memory.freeSwap = Bytes(info.freeswap); +# endif + + return memory; +} + } // namespace os { #endif // __STOUT_OS_POSIX_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/7aafb8e4/3rdparty/libprocess/3rdparty/stout/include/stout/os/osx.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/osx.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/osx.hpp index cc9bfe8..692fa48 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/osx.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/osx.hpp @@ -187,6 +187,55 @@ inline Try<std::set<pid_t> > pids() return result; } + +// Returns the total size of main and free memory. +inline Try<Memory> memory() +{ + Memory memory; + + const Try<int64_t> totalMemory = os::sysctl(CTL_HW, HW_MEMSIZE).integer(); + + if (totalMemory.isError()) { + return Error(totalMemory.error()); + } + memory.total = Bytes(totalMemory.get()); + + // Size of free memory is available in terms of number of + // free pages on Mac OS X. + const long pageSize = sysconf(_SC_PAGESIZE); + if (pageSize < 0) { + return ErrnoError(); + } + + unsigned int freeCount; + size_t length = sizeof(freeCount); + + if (sysctlbyname( + "vm.page_free_count", + &freeCount, + &length, + NULL, + 0) != 0) { + return ErrnoError(); + } + memory.free = Bytes(freeCount * pageSize); + + struct xsw_usage usage; + length = sizeof(struct xsw_usage); + if (sysctlbyname( + "vm.swapusage", + &usage, + &length, + NULL, + 0) != 0) { + return ErrnoError(); + } + memory.totalSwap = Bytes(usage.xsu_total * pageSize); + memory.freeSwap = Bytes(usage.xsu_avail * pageSize); + + return memory; +} + } // namespace os { #endif // __STOUT_OS_OSX_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/7aafb8e4/3rdparty/libprocess/3rdparty/stout/include/stout/os/sunos.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/sunos.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/sunos.hpp index 0e897ea..ec8e1f7 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/sunos.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/sunos.hpp @@ -109,6 +109,13 @@ inline Try<std::set<pid_t> > pids() return Error("Failed to determine pids from /proc"); } + +// Returns the total size of main and free memory. +inline Try<Memory> memory() +{ + return Error("Cannot determine the size of total and free memory"); +} + } // namespace os { #endif // __STOUT_OS_SUNOS_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/7aafb8e4/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp index b471878..84a2a02 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp @@ -81,9 +81,6 @@ #ifdef __sun #include <stout/os/sunos.hpp> #endif // __sun -#if defined(__APPLE__) || defined(__FreeBSD__) -#include <stout/os/sysctl.hpp> -#endif // __APPLE__ || __FreeBSD__ #include <stout/os/posix/chown.hpp> #include <stout/os/raw/environment.hpp> @@ -429,133 +426,6 @@ inline Try<Load> loadavg() } -// Returns the total size of main and free memory. -inline Try<Memory> memory() -{ - Memory memory; - -// TODO(dforsyth): Refactor these implementations into seperate, platform -// specific files. -#ifdef __linux__ - struct sysinfo info; - if (sysinfo(&info) != 0) { - return ErrnoError(); - } - -# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 23) - memory.total = Bytes(info.totalram * info.mem_unit); - memory.free = Bytes(info.freeram * info.mem_unit); - memory.totalSwap = Bytes(info.totalswap * info.mem_unit); - memory.freeSwap = Bytes(info.freeswap * info.mem_unit); -# else - memory.total = Bytes(info.totalram); - memory.free = Bytes(info.freeram); - memory.totalSwap = Bytes(info.totalswap); - memory.freeSwap = Bytes(info.freeswap); -# endif - - return memory; - -#elif defined __APPLE__ - const Try<int64_t> totalMemory = os::sysctl(CTL_HW, HW_MEMSIZE).integer(); - - if (totalMemory.isError()) { - return Error(totalMemory.error()); - } - memory.total = Bytes(totalMemory.get()); - - // Size of free memory is available in terms of number of - // free pages on Mac OS X. - const long pageSize = sysconf(_SC_PAGESIZE); - if (pageSize < 0) { - return ErrnoError(); - } - - unsigned int freeCount; - size_t length = sizeof(freeCount); - - if (sysctlbyname( - "vm.page_free_count", - &freeCount, - &length, - NULL, - 0) != 0) { - return ErrnoError(); - } - memory.free = Bytes(freeCount * pageSize); - - struct xsw_usage usage; - length = sizeof(struct xsw_usage); - if (sysctlbyname( - "vm.swapusage", - &usage, - &length, - NULL, - 0) != 0) { - return ErrnoError(); - } - memory.totalSwap = Bytes(usage.xsu_total * pageSize); - memory.freeSwap = Bytes(usage.xsu_avail * pageSize); - - return memory; - -#elif defined __FreeBSD__ - const Try<int64_t> physicalMemory = os::sysctl(CTL_HW, HW_PHYSMEM).integer(); - if (physicalMemory.isError()) { - return Error(physicalMemory.error()); - } - memory.total = Bytes(physicalMemory.get()); - - const int pageSize = getpagesize(); - - unsigned int freeCount; - size_t length = sizeof(freeCount); - - if (sysctlbyname( - "vm.stats.v_free_count", - &freeCount, - &length, - NULL, - 0) != 0) { - return ErrnoError(); - } - memory.free = Bytes(freeCount * pageSize); - - int totalBlocks = 0; - int usedBlocks = 0; - - int mib[3]; - size_t mibSize = 2; - if (::sysctlnametomib("vm.swap_info", mib, &mibSize) != 0) { - return ErrnoError(); - } - - // FreeBSD supports multiple swap devices. Here we sum across all of them. - struct xswdev xswd; - size_t xswdSize = sizeof(xswd); - int* mibDevice = &(mib[mibSize + 1]); - for (*mibDevice = 0; ; (*mibDevice)++) { - if (::sysctl(mib, 3, &xswd, &xswdSize, NULL, 0) != 0) { - if (errno == ENOENT) { - break; - } - return ErrnoError(); - } - - totalBlocks += xswd.xsw_nblks; - usedBlocks += xswd.xsw_used; - } - - memory.totalSwap = Bytes(totalBlocks * pageSize); - memory.freeSwap = Bytes((totalBlocks - usedBlocks) * pageSize); - - return memory; -#else - return Error("Cannot determine the size of total and free memory"); -#endif -} - - // Return the system information. inline Try<UTSInfo> uname() {
