process-cpp
3.0.0
A simple convenience library for handling processes in C++11.
linux_process_test.cpp
Go to the documentation of this file.
1
/*
2
* Copyright © 2013 Canonical Ltd.
3
*
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
12
*
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
17
*/
18
19
#include <
core/posix/fork.h
>
20
#include <
core/posix/this_process.h
>
21
22
#include <
core/posix/linux/proc/process/stat.h
>
23
#include <
core/posix/linux/proc/process/oom_adj.h
>
24
#include <
core/posix/linux/proc/process/oom_score.h
>
25
#include <
core/posix/linux/proc/process/oom_score_adj.h
>
26
27
#include <gtest/gtest.h>
28
29
#include <map>
30
31
TEST
(LinuxProcess, accessing_proc_stats_works)
32
{
33
auto
child =
core::posix::fork
(
34
[](){
while
(
true
);
return
core::posix::exit::Status::success
;},
35
core::posix::StandardStream::empty
);
36
37
core::posix::linux::proc::process::Stat
stat;
38
EXPECT_NO_THROW(child >> stat);
39
ASSERT_EQ(
core::posix::linux::proc::process::State::running
, stat.state);
40
}
41
42
TEST
(LinuxProcess, accessing_proc_oom_score_works)
43
{
44
core::posix::linux::proc::process::OomScore
oom_score;
45
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_score);
46
}
47
48
TEST
(LinuxProcess, accessing_proc_oom_score_adj_works)
49
{
50
core::posix::linux::proc::process::OomScoreAdj
oom_score_adj;
51
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_score_adj);
52
}
53
54
TEST
(LinuxProcess, adjusting_proc_oom_score_adj_works)
55
{
56
core::posix::linux::proc::process::OomScoreAdj
oom_score_adj
57
{
58
core::posix::linux::proc::process::OomScoreAdj::max_value
()
59
};
60
EXPECT_NO_THROW(
core::posix::this_process::instance
() << oom_score_adj);
61
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_score_adj);
62
EXPECT_EQ(
core::posix::linux::proc::process::OomScoreAdj::max_value
(),
63
oom_score_adj.value);
64
}
65
66
// For this test we assume that we are not privileged and that the test binary
67
// does not have CAP_SYS_RESOURCE capabilities.
68
TEST
(LinuxProcess, adjusting_proc_oom_score_adj_to_privileged_values_only_works_if_root)
69
{
70
core::posix::linux::proc::process::OomScoreAdj
oom_score_adj
71
{
72
core::posix::linux::proc::process::OomScoreAdj::min_value
()
73
};
74
EXPECT_NO_THROW(
core::posix::this_process::instance
() << oom_score_adj);
75
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_score_adj);
76
77
// If we are running on virtualized builders or buildds we are running under a fakeroot environment.
78
// However, that environment does not give us the required privileges and capabilities to adjust OOM values
79
// as we like. At any rate, this check seems to be flaky and we just comment it out.
80
// EXPECT_NE(core::posix::linux::proc::process::OomScoreAdj::min_value(),
81
// oom_score_adj.value);
82
}
83
84
TEST
(LinuxProcess, trying_to_write_an_invalid_oom_score_adj_throws)
85
{
86
core::posix::linux::proc::process::OomScoreAdj
invalid_adj
87
{
88
core::posix::linux::proc::process::OomScoreAdj::min_value
() -1000
89
};
90
91
EXPECT_ANY_THROW
(
core::posix::this_process::instance
() << invalid_adj);
92
}
93
94
TEST
(LinuxProcess, adjusting_proc_oom_adj_works)
95
{
96
core::posix::linux::proc::process::OomAdj
oom_adj
97
{
98
core::posix::linux::proc::process::OomAdj::max_value
()
99
};
100
EXPECT_NO_THROW(
core::posix::this_process::instance
() << oom_adj);
101
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_adj);
102
EXPECT_EQ(
core::posix::linux::proc::process::OomAdj::max_value
(),
103
oom_adj.value);
104
}
105
106
// For this test we assume that we are not privileged and that the test binary
107
// does not have CAP_SYS_RESOURCE capabilities.
108
TEST
(LinuxProcess, adjusting_proc_oom_adj_to_privileged_values_does_not_work)
109
{
110
core::posix::linux::proc::process::OomAdj
oom_adj
111
{
112
core::posix::linux::proc::process::OomAdj::min_value
()
113
};
114
EXPECT_NO_THROW(
core::posix::this_process::instance
() << oom_adj);
115
EXPECT_NO_THROW(
core::posix::this_process::instance
() >> oom_adj);
116
117
// If we are running on virtualized builders or buildds we are running under a fakeroot environment.
118
// However, that environment does not give us the required privileges and capabilities to adjust OOM values
119
// as we like. At any rate, this check seems to be flaky and we just comment it out.
120
// EXPECT_NE(core::posix::linux::proc::process::OomAdj::min_value(),
121
// oom_adj.value);
122
}
123
124
TEST
(LinuxProcess, trying_to_write_an_invalid_oom_adj_throws)
125
{
126
core::posix::linux::proc::process::OomAdj
invalid_adj
127
{
128
core::posix::linux::proc::process::OomAdj::min_value
() - 1000
129
};
130
131
EXPECT_ANY_THROW
(
core::posix::this_process::instance
() << invalid_adj);
132
}
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::Stat
The Stat struct encapsulates status information about a process.
Definition
stat.h:42
this_process.h
tests
linux_process_test.cpp
Generated on Thu Aug 15 2024 22:08:10 for process-cpp by
1.9.8