2
* Copyright (C) 2014 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
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/>.
18
#include "processcontroller.h"
23
#include <core/posix/process.h>
24
#include <core/posix/this_process.h>
25
#include <core/posix/linux/proc/process/oom_adj.h>
26
#include <core/posix/linux/proc/process/oom_score_adj.h>
32
namespace plpp = core::posix::linux::proc::process;
34
ProcessController::OomController::OomController()
38
void ProcessController::OomController::ensureProcessLikelyToBeKilled(pid_t pid)
40
// We avoid boundary values for oom_score_adj. For that, we
41
// set it to 80% of the total available range.
42
static const float defaultPercentage = 0.8;
44
core::posix::Process process(pid);
47
plpp::OomScoreAdj shellScore;
48
core::posix::this_process::instance() >> shellScore;
50
plpp::OomScoreAdj processScore
52
static_cast<int>((plpp::OomScoreAdj::max_value() - shellScore.value) * defaultPercentage) + shellScore.value
55
process << processScore;
57
// Accessing OomScoreAdj resulted in an exception being thrown.
58
// Trying with the deprecated OomAdj now as a last resort.
61
process << plpp::OomAdj{plpp::OomAdj::max_value()};
64
LOG("ensureProcessIsLikelyToBeKilled failed");
69
void ProcessController::OomController::ensureProcessUnlikelyToBeKilled(pid_t pid)
71
// By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
72
// As we want to avoid that any app's oom_score_adj is <= Unity8's oom_score_adj,
73
// we choose a default increase of +1.
74
static const int default_increase = 1;
76
core::posix::Process process(pid);
79
plpp::OomScoreAdj shellScore;
80
core::posix::this_process::instance() >> shellScore;
82
plpp::OomScoreAdj processScore
84
shellScore.value + default_increase
87
process << processScore;
89
// Accessing OomScoreAdj resulted in an exception being thrown.
90
// Trying with the deprecated OomAdj now as a last resort.
91
// By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
92
// As we want to avoid that any app's oom_score_adj or oom_adj is <= Unity8's oom_score_adj,
93
// we choose a default value of -9 for oom_score_adj and 0 for oom_adj.
94
static const int defaultValue = 0;
98
process << plpp::OomAdj{defaultValue};
101
LOG("ensureProcessIsUnlikelyToBeKilled failed");
106
ProcessController::ProcessController()
107
: m_oomController(new ProcessController::OomController())
111
ProcessController::~ProcessController()
115
const QSharedPointer<ProcessController::OomController>& ProcessController::oomController() const
117
return m_oomController;
120
bool ProcessController::sigStopProcessGroupForPid(pid_t pid) const
122
return -1 != kill(-pid, SIGSTOP);
125
bool ProcessController::sigContinueProcessGroupForPid(pid_t pid) const
127
return -1 != kill(-pid, SIGCONT);