From 310f6704d0dfe9ca4cfb153ce8e4212cc7596190 Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Fri, 22 Jul 2016 12:07:06 -0700 Subject: [PATCH] Allow setting oom_score_adj for services spawned from init (cherry picked from commit f7adf8e4739812a2a4f194b17f84f4b9df42d04b) Bug: 29831602 Change-Id: I4d24264bb6e879935a0b2adbb2e49ddf458980cf --- init/readme.txt | 4 ++++ init/service.cpp | 26 ++++++++++++++++++++++++-- init/service.h | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/init/readme.txt b/init/readme.txt index 56918f60d..bc3874a87 100644 --- a/init/readme.txt +++ b/init/readme.txt @@ -191,6 +191,10 @@ priority namespace Enter a new PID or mount namespace when forking the service. +oom_score_adjust + Sets the child's /proc/self/oom_score_adj to the specified value, + which must range from -1000 to 1000. + Triggers -------- diff --git a/init/service.cpp b/init/service.cpp index 32aafd61b..1caf7c690 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -163,7 +163,7 @@ Service::Service(const std::string& name, const std::string& classname, : name_(name), classname_(classname), flags_(0), pid_(0), time_started_(0), time_crashed_(0), nr_crashed_(0), uid_(0), gid_(0), namespace_flags_(0), seclabel_(""), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), - priority_(0), args_(args) { + priority_(0), oom_score_adjust_(-1000), args_(args) { onrestart_.InitSingleTrigger("onrestart"); } @@ -176,7 +176,7 @@ Service::Service(const std::string& name, const std::string& classname, time_started_(0), time_crashed_(0), nr_crashed_(0), uid_(uid), gid_(gid), supp_gids_(supp_gids), namespace_flags_(namespace_flags), seclabel_(seclabel), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), - priority_(0), args_(args) { + priority_(0), oom_score_adjust_(-1000), args_(args) { onrestart_.InitSingleTrigger("onrestart"); } @@ -419,6 +419,18 @@ bool Service::ParseNamespace(const std::vector& args, std::string* return true; } +bool Service::ParseOomScoreAdjust(const std::vector& args, std::string* err) { + oom_score_adjust_ = std::stol(args[1], 0, 10); + + if (oom_score_adjust_ < -1000 || oom_score_adjust_ > 1000) { + *err = "oom_score_adjust value must be in range -1000 - +1000"; + return false; + } + + return true; +} + + bool Service::ParseSeclabel(const std::vector& args, std::string* err) { seclabel_ = args[1]; return true; @@ -476,6 +488,8 @@ Service::OptionParserMap::Map& Service::OptionParserMap::map() const { {"keycodes", {1, kMax, &Service::ParseKeycodes}}, {"oneshot", {0, 0, &Service::ParseOneshot}}, {"onrestart", {1, kMax, &Service::ParseOnrestart}}, + {"oom_score_adjust", + {1, 1, &Service::ParseOomScoreAdjust}}, {"namespace", {1, 2, &Service::ParseNamespace}}, {"seclabel", {1, 1, &Service::ParseSeclabel}}, {"setenv", {2, 2, &Service::ParseSetenv}}, @@ -611,6 +625,14 @@ bool Service::Start() { return false; } + if (oom_score_adjust_ != -1000) { + std::string oom_str = StringPrintf("%d", oom_score_adjust_); + std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid); + if (!WriteStringToFile(oom_str, oom_file)) { + PLOG(ERROR) << "couldn't write oom_score_adj: " << strerror(errno); + } + } + time_started_ = gettime(); pid_ = pid; flags_ |= SVC_RUNNING; diff --git a/init/service.h b/init/service.h index fb03a0732..4a3412ce9 100644 --- a/init/service.h +++ b/init/service.h @@ -126,6 +126,7 @@ private: bool ParseKeycodes(const std::vector& args, std::string* err); bool ParseOneshot(const std::vector& args, std::string* err); bool ParseOnrestart(const std::vector& args, std::string* err); + bool ParseOomScoreAdjust(const std::vector& args, std::string* err); bool ParseNamespace(const std::vector& args, std::string* err); bool ParseSeclabel(const std::vector& args, std::string* err); bool ParseSetenv(const std::vector& args, std::string* err); @@ -165,6 +166,8 @@ private: int ioprio_pri_; int priority_; + int oom_score_adjust_; + std::vector args_; };