~fboucault/unity-mir/dpr_rebase_qt_5.1

« back to all changes in this revision

Viewing changes to src/modules/Unity/Application/processcontroller.cpp

Refactor Oom(Score)Adj to rely on process-cpp helper library. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
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
 */
 
17
 
 
18
#include "processcontroller.h"
 
19
 
 
20
#include "logging.h"
 
21
 
 
22
// Process C++
 
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>
 
27
 
 
28
// std
 
29
#include <signal.h>
 
30
#include <unistd.h>
 
31
 
 
32
namespace plpp = core::posix::linux::proc::process;
 
33
 
 
34
ProcessController::OomController::OomController()
 
35
{
 
36
}
 
37
 
 
38
void ProcessController::OomController::ensureProcessLikelyToBeKilled(pid_t pid)
 
39
{
 
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;
 
43
 
 
44
    core::posix::Process process(pid);
 
45
 
 
46
    try {
 
47
        plpp::OomScoreAdj shellScore;
 
48
        core::posix::this_process::instance() >> shellScore;
 
49
 
 
50
        plpp::OomScoreAdj processScore
 
51
        {
 
52
            static_cast<int>((plpp::OomScoreAdj::max_value() - shellScore.value) * defaultPercentage) + shellScore.value
 
53
        };
 
54
 
 
55
        process << processScore;
 
56
    } catch(...) {
 
57
        // Accessing OomScoreAdj resulted in an exception being thrown.
 
58
        // Trying with the deprecated OomAdj now as a last resort.
 
59
        try
 
60
        {
 
61
            process << plpp::OomAdj{plpp::OomAdj::max_value()};
 
62
        } catch(...)
 
63
        {
 
64
            LOG("ensureProcessIsLikelyToBeKilled failed");
 
65
        }
 
66
    }
 
67
}
 
68
 
 
69
void ProcessController::OomController::ensureProcessUnlikelyToBeKilled(pid_t pid)
 
70
{
 
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;
 
75
 
 
76
    core::posix::Process process(pid);
 
77
 
 
78
    try {
 
79
        plpp::OomScoreAdj shellScore;
 
80
        core::posix::this_process::instance() >> shellScore;
 
81
 
 
82
        plpp::OomScoreAdj processScore
 
83
        {
 
84
            shellScore.value + default_increase
 
85
        };
 
86
 
 
87
        process << processScore;
 
88
    } catch(...) {
 
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;
 
95
 
 
96
        try
 
97
        {
 
98
            process << plpp::OomAdj{defaultValue};
 
99
        } catch(...)
 
100
        {
 
101
            LOG("ensureProcessIsUnlikelyToBeKilled failed");
 
102
        }
 
103
    }
 
104
}
 
105
 
 
106
ProcessController::ProcessController()
 
107
    : m_oomController(new ProcessController::OomController())
 
108
{
 
109
}
 
110
 
 
111
ProcessController::~ProcessController()
 
112
{
 
113
}
 
114
 
 
115
const QSharedPointer<ProcessController::OomController>& ProcessController::oomController() const
 
116
{
 
117
    return m_oomController;
 
118
}
 
119
 
 
120
bool ProcessController::sigStopProcessGroupForPid(pid_t pid) const
 
121
{
 
122
    return -1 != kill(-pid, SIGSTOP);
 
123
}
 
124
 
 
125
bool ProcessController::sigContinueProcessGroupForPid(pid_t pid) const
 
126
{
 
127
    return -1 != kill(-pid, SIGCONT);
 
128
}