linux_process_test.cpp
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#include <core/posix/fork.h>
#include <core/posix/this_process.h>
#include <core/posix/linux/proc/process/stat.h>
#include <core/posix/linux/proc/process/oom_adj.h>
#include <core/posix/linux/proc/process/oom_score.h>
#include <core/posix/linux/proc/process/oom_score_adj.h>
#include <gtest/gtest.h>
#include <map>
TEST(LinuxProcess, accessing_proc_stats_works)
{
auto child = core::posix::fork(
[](){ while(true); return core::posix::exit::Status::success;},
core::posix::StandardStream::empty);
core::posix::linux::proc::process::Stat stat;
EXPECT_NO_THROW(child >> stat);
ASSERT_EQ(core::posix::linux::proc::process::State::running, stat.state);
}
TEST(LinuxProcess, accessing_proc_oom_score_works)
{
core::posix::linux::proc::process::OomScore oom_score;
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score);
}
TEST(LinuxProcess, accessing_proc_oom_score_adj_works)
{
core::posix::linux::proc::process::OomScoreAdj oom_score_adj;
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
}
namespace
{
// A custom predicate to evaluate values in /proc/pid/oom_score.
// The file contains the oom_score as calculated by the kernel and we are racing
// against the badness heuristics. However, we only accept values within an error
// margin of 10 as the test process does not extensively allocate memory, uses hw devices etc..
::testing::AssertionResult is_approximately_equal(int a, int b)
{
static const int error_margin = 10;
if (::abs(a-b) <= 10)
return ::testing::AssertionSuccess() << ::abs(a-b) << " <= " << error_margin;
return ::testing::AssertionFailure() << ::abs(a-b) << " > " << error_margin;
}
}
TEST(LinuxProcess, adjusting_proc_oom_score_adj_works)
{
core::posix::linux::proc::process::OomScoreAdj oom_score_adj
{
core::posix::linux::proc::process::OomScoreAdj::max_value()
};
EXPECT_NO_THROW(core::posix::this_process::instance() << oom_score_adj);
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
EXPECT_EQ(core::posix::linux::proc::process::OomScoreAdj::max_value(),
oom_score_adj.value);
core::posix::linux::proc::process::OomScore oom_score;
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score);
EXPECT_TRUE(is_approximately_equal(oom_score.value, core::posix::linux::proc::process::OomScoreAdj::max_value()));
}
// For this test we assume that we are not privileged and that the test binary
// does not have CAP_SYS_RESOURCE capabilities.
TEST(LinuxProcess, adjusting_proc_oom_score_adj_to_privileged_values_only_works_if_root)
{
core::posix::linux::proc::process::OomScoreAdj oom_score_adj
{
core::posix::linux::proc::process::OomScoreAdj::min_value()
};
EXPECT_NO_THROW(core::posix::this_process::instance() << oom_score_adj);
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
// If we are running on virtualized builders or buildds we are running under a fakeroot environment.
// However, that environment does not give us the required privileges and capabilities to adjust OOM values
// as we like. At any rate, this check seems to be flaky and we just comment it out.
// EXPECT_NE(core::posix::linux::proc::process::OomScoreAdj::min_value(),
// oom_score_adj.value);
}
TEST(LinuxProcess, trying_to_write_an_invalid_oom_score_adj_throws)
{
core::posix::linux::proc::process::OomScoreAdj invalid_adj
{
core::posix::linux::proc::process::OomScoreAdj::min_value() -1000
};
EXPECT_ANY_THROW(core::posix::this_process::instance() << invalid_adj);
}
TEST(LinuxProcess, adjusting_proc_oom_adj_works)
{
core::posix::linux::proc::process::OomAdj oom_adj
{
core::posix::linux::proc::process::OomAdj::max_value()
};
EXPECT_NO_THROW(core::posix::this_process::instance() << oom_adj);
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_adj);
EXPECT_EQ(core::posix::linux::proc::process::OomAdj::max_value(),
oom_adj.value);
core::posix::linux::proc::process::OomScore oom_score;
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score);
// This looks weird as we are comparing to OomScoreAdj as opposed to OomAdj.
// However, /proc/pid/oom_adj is deprecated as of linux 2.6.36 and the value
// reported in oom_score is in the scale of /proc/pid/oom_score_adj, i.e., [-1000, 1000].
EXPECT_TRUE(is_approximately_equal(oom_score.value, core::posix::linux::proc::process::OomScoreAdj::max_value()));
}
// For this test we assume that we are not privileged and that the test binary
// does not have CAP_SYS_RESOURCE capabilities.
TEST(LinuxProcess, adjusting_proc_oom_adj_to_privileged_values_does_not_work)
{
core::posix::linux::proc::process::OomAdj oom_adj
{
core::posix::linux::proc::process::OomAdj::min_value()
};
EXPECT_NO_THROW(core::posix::this_process::instance() << oom_adj);
EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_adj);
// If we are running on virtualized builders or buildds we are running under a fakeroot environment.
// However, that environment does not give us the required privileges and capabilities to adjust OOM values
// as we like. At any rate, this check seems to be flaky and we just comment it out.
// EXPECT_NE(core::posix::linux::proc::process::OomAdj::min_value(),
// oom_adj.value);
}
TEST(LinuxProcess, trying_to_write_an_invalid_oom_adj_throws)
{
core::posix::linux::proc::process::OomAdj invalid_adj
{
core::posix::linux::proc::process::OomAdj::min_value() - 1000
};
EXPECT_ANY_THROW(core::posix::this_process::instance() << invalid_adj);
}
EXPECT_ANY_THROW
EXPECT_ANY_THROW(auto death_observer=core::posix::ChildProcess::DeathObserver::create_once_with_signal_trap(trap))
fork.h
TEST
TEST(LinuxProcess, accessing_proc_stats_works)
Definition: linux_process_test.cpp:31
core::posix::exit::Status::success
@ success
core::posix::linux::proc::process::State::running
@ running
core::posix::this_process::instance
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
Definition: this_process.cpp:150
core::posix::fork
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process.
Definition: fork.cpp:57
core::posix::StandardStream::empty
@ empty
oom_adj.h
oom_score.h
oom_score_adj.h
stat.h
core::posix::linux::proc::process::OomAdj
Definition: oom_adj.h:51
core::posix::linux::proc::process::OomAdj::max_value
static int max_value()
Returns the maximum valid value.
Definition: oom_adj.cpp:50
core::posix::linux::proc::process::OomAdj::min_value
static int min_value()
Returns the minimum valid value.
Definition: oom_adj.cpp:45
core::posix::linux::proc::process::OomScoreAdj
Definition: oom_score_adj.h:82
core::posix::linux::proc::process::OomScoreAdj::min_value
static int min_value()
Returns the minimum valid value.
Definition: oom_score_adj.cpp:40
core::posix::linux::proc::process::OomScoreAdj::max_value
static int max_value()
Returns the maximum valid value.
Definition: oom_score_adj.cpp:45
core::posix::linux::proc::process::OomScore
Definition: oom_score.h:51
core::posix::linux::proc::process::OomScore::value
int value
Current OomScore as calculated by the kernel.
Definition: oom_score.h:52
core::posix::linux::proc::process::Stat
The Stat struct encapsulates status information about a process.
Definition: stat.h:42
this_process.h