Windows: Added `os::get_job_info` to stout. This returns a `JOBOBJECT_BASIC_ACCOUNTING_INFORMATION`, which can be inspected for basic CPU / process accounting information for a group of processes within a job object.
Review: https://reviews.apache.org/r/63272 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7d1c5804 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7d1c5804 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7d1c5804 Branch: refs/heads/master Commit: 7d1c58041d0f619306af13cb3ee4645b14355d15 Parents: 2aad4d8 Author: Andrew Schwartzmeyer <[email protected]> Authored: Fri Oct 13 11:39:20 2017 -0700 Committer: Andrew Schwartzmeyer <[email protected]> Committed: Thu Nov 30 15:54:53 2017 -0800 ---------------------------------------------------------------------- 3rdparty/stout/include/stout/windows/os.hpp | 41 ++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/7d1c5804/3rdparty/stout/include/stout/windows/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/windows/os.hpp b/3rdparty/stout/include/stout/windows/os.hpp index a22e435..57481be 100644 --- a/3rdparty/stout/include/stout/windows/os.hpp +++ b/3rdparty/stout/include/stout/windows/os.hpp @@ -211,10 +211,10 @@ inline void setenv( { // Do not set the variable if already set and `overwrite` was not specified. // - // Per MSDN[1], `GetEnvironmentVariable` returns 0 on error and sets the + // Per MSDN, `GetEnvironmentVariable` returns 0 on error and sets the // error code to `ERROR_ENVVAR_NOT_FOUND` if the variable was not found. // - // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx + // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx // NOLINT(whitespace/line_length) if (!overwrite && ::GetEnvironmentVariableW(wide_stringify(key).data(), nullptr, 0) != 0 && ::GetLastError() == ERROR_ENVVAR_NOT_FOUND) { @@ -723,6 +723,43 @@ inline Try<SharedHandle> create_job(const std::wstring& name) } +// `get_job_info` gets the job object information for the process group +// represented by `pid`, assuming it is assigned to a job object. This function +// will fail otherwise. +// +// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684925(v=vs.85).aspx // NOLINT(whitespace/line_length) +inline Try<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION> get_job_info(pid_t pid) +{ + Try<std::wstring> name = os::name_job(pid); + if (name.isError()) { + return Error(name.error()); + } + + Try<SharedHandle> job_handle = os::open_job( + JOB_OBJECT_QUERY, + false, + name.get()); + if (job_handle.isError()) { + return Error(job_handle.error()); + } + + JOBOBJECT_BASIC_ACCOUNTING_INFORMATION info = {}; + + BOOL result = ::QueryInformationJobObject( + job_handle.get().get_handle(), + JobObjectBasicAccountingInformation, + &info, + sizeof(info), + nullptr); + if (result == FALSE) { + return WindowsError( + "os::get_job_info: call to `QueryInformationJobObject` failed"); + } + + return info; +} + + // `set_job_cpu_limit` sets a CPU limit for the process represented by // `pid`, assuming it is assigned to a job object. This function will fail // otherwise. This limit is a hard cap enforced by the OS.
