From: Hwankyu Jhun Date: Wed, 24 May 2023 08:51:34 +0000 (+0000) Subject: Modify setting scheduling priority X-Git-Tag: accepted/tizen/unified/20230601.162952~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F10%2F293310%2F5;p=platform%2Fcore%2Fappfw%2Flaunchpad.git Modify setting scheduling priority To set the scheduling priority properly, the launchpad-process-pool sends the priority to the child process. And, after calling fork(), the child process creates a new session using setsid(). The argument of the setpriority() is changed to PRIO_PGRP from PRIO_PROCESS. The hydra loader changes the priority to 0 to restore to its original state. Change-Id: Ie5d5d6d55493934c7706b32789d35697d05285d6 Signed-off-by: Hwankyu Jhun --- diff --git a/src/launchpad-process-pool/app_executor.cc b/src/launchpad-process-pool/app_executor.cc index 86accb1..ff38056 100644 --- a/src/launchpad-process-pool/app_executor.cc +++ b/src/launchpad-process-pool/app_executor.cc @@ -61,8 +61,6 @@ AppExecutor::AppExecutor() : Executor(this) { launcher_infos_ = inflator.Inflate("/usr/share/aul"); prepare_funcs_.push_back( - std::bind(&AppExecutor::StepCreateNewSession, this)); - prepare_funcs_.push_back( std::bind(&AppExecutor::StepPluginPrepareApp, this)); prepare_funcs_.push_back( std::bind(&AppExecutor::StepEnableExternalPackage, this)); @@ -174,11 +172,6 @@ int AppExecutor::Prepare() { return 0; } -int AppExecutor::StepCreateNewSession() { - setsid(); - return 0; -} - int AppExecutor::StepPluginPrepareApp() { return Plugin::PrepareApp(app_info_->GetAppId(), app_info_->GetBundle()); } diff --git a/src/launchpad-process-pool/app_executor.hh b/src/launchpad-process-pool/app_executor.hh index 5d1327e..94b23fa 100644 --- a/src/launchpad-process-pool/app_executor.hh +++ b/src/launchpad-process-pool/app_executor.hh @@ -46,7 +46,6 @@ class AppExecutor : public Executor::Delegator, void OnRequestReceived(tizen_base::Parcel* parcel) override; int Prepare(); - int StepCreateNewSession(); int StepPluginPrepareApp(); int StepEnableExternalPackage(); int StepEnableTrustAnchor(); diff --git a/src/launchpad-process-pool/executor.cc b/src/launchpad-process-pool/executor.cc index 74541d6..4aea7ca 100644 --- a/src/launchpad-process-pool/executor.cc +++ b/src/launchpad-process-pool/executor.cc @@ -16,9 +16,10 @@ #include "launchpad-process-pool/executor.hh" +#include #include -#include +#include #include "launchpad-process-pool/log_private.hh" @@ -34,8 +35,9 @@ pid_t Executor::Execute(int priority) { } if (pid == 0) { + setsid(); if (priority != 0) - Util::SetPriority(priority); + SchedPriority::Set(priority); _W("security_manager_prepare_app_candidate ++"); int ret = security_manager_prepare_app_candidate(); diff --git a/src/launchpad-process-pool/hydra_loader_context.cc b/src/launchpad-process-pool/hydra_loader_context.cc index b42db39..545e107 100644 --- a/src/launchpad-process-pool/hydra_loader_context.cc +++ b/src/launchpad-process-pool/hydra_loader_context.cc @@ -110,7 +110,7 @@ void HydraLoaderContext::SetHydraPid(pid_t hydra_pid) { void HydraLoaderContext::PrepareCandidateProcess() { _W("Send launch request to hydra loader. fd(%d)", client_socket_->GetFd()); - HydraRequest request(LAUNCH_CANDIDATE); + HydraRequest request(LAUNCH_CANDIDATE, GetSchedPriority()); tizen_base::Parcel parcel; parcel.WriteParcelable(request); diff --git a/src/launchpad-process-pool/loader_context.cc b/src/launchpad-process-pool/loader_context.cc index 8ced9b8..7458bbb 100644 --- a/src/launchpad-process-pool/loader_context.cc +++ b/src/launchpad-process-pool/loader_context.cc @@ -283,6 +283,10 @@ void LoaderContext::SetPrepared(bool prepared) { prepared_ = prepared; } +int LoaderContext::GetSchedPriority() const { + return loader_info_->GetSchedPriority(); +} + pid_t LoaderContext::GetPid() const { return pid_; } diff --git a/src/launchpad-process-pool/loader_context.hh b/src/launchpad-process-pool/loader_context.hh index 8559ebe..1dfbc26 100644 --- a/src/launchpad-process-pool/loader_context.hh +++ b/src/launchpad-process-pool/loader_context.hh @@ -114,6 +114,7 @@ class LoaderContext : public std::enable_shared_from_this, protected: void SetPrepared(bool prepared); void OnIOEventReceived(int fd, int condition) override; + int GetSchedPriority() const; private: void UpdateScore(); diff --git a/src/launchpad-process-pool/loader_executor.cc b/src/launchpad-process-pool/loader_executor.cc index cdf9f4a..43f1d4c 100644 --- a/src/launchpad-process-pool/loader_executor.cc +++ b/src/launchpad-process-pool/loader_executor.cc @@ -23,6 +23,7 @@ #include #include +#include #include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/log_private.hh" @@ -33,20 +34,17 @@ namespace launchpad { namespace { -tizen_base::Parcel CreateParcelFromArgv(const std::vector& argv) { - tizen_base::Bundle b; - b.Add("LOADER_ARGS", argv); - auto b_raw = b.ToRaw(); +tizen_base::Parcel CreateParcelFromBundle(tizen_base::Bundle* b) { + auto b_raw = b->ToRaw(); std::string raw(reinterpret_cast(b_raw.first.get())); tizen_base::Parcel parcel; parcel.WriteString(raw); return parcel; } -std::vector CreateArgvFromParcel(tizen_base::Parcel* parcel) { +tizen_base::Bundle CreateBundleFromParcel(tizen_base::Parcel* parcel) { std::string raw = parcel->ReadString(); - tizen_base::Bundle b(raw); - return b.GetStringArray("LOADER_ARGS"); + return tizen_base::Bundle(raw); } } // namespace @@ -67,7 +65,10 @@ pid_t LoaderExecutor::Execute(const LoaderContext* loader_context, int priority) { loader_argv_ = CreateLoaderArgv(loader_context); if (process_pool_->IsPrepared()) { - auto parcel = CreateParcelFromArgv(loader_argv_); + tizen_base::Bundle b; + b.Add("LOADER_ARGS", loader_argv_); + b.Add("LOADER_PRIORITY", std::to_string(priority)); + auto parcel = CreateParcelFromBundle(&b); return process_pool_->Execute(parcel); } @@ -107,7 +108,12 @@ void LoaderExecutor::OnExecution() { void LoaderExecutor::OnRequestReceived(tizen_base::Parcel* parcel) { _W("Request received"); - loader_argv_ = CreateArgvFromParcel(parcel); + tizen_base::Bundle b = CreateBundleFromParcel(parcel); + int priority = std::stoi(b.GetString("LOADER_PRIORITY")); + if (priority != 0) + SchedPriority::Set(priority); + + loader_argv_ = b.GetStringArray("LOADER_ARGS"); OnExecution(); } diff --git a/src/lib/common/src/launchpad_common.c b/src/lib/common/src/launchpad_common.c index b12f10d..9f75801 100644 --- a/src/lib/common/src/launchpad_common.c +++ b/src/lib/common/src/launchpad_common.c @@ -1051,7 +1051,7 @@ int _set_priority(int prio) { int ret; - ret = setpriority(PRIO_PROCESS, 0, prio); + ret = setpriority(PRIO_PGRP, 0, prio); if (ret != 0) { SECURE_LOGE("Failed to set process(%d) priority(%d) - err(%d)", getpid(), prio, errno); diff --git a/src/lib/launchpad-common/hydra_request.cc b/src/lib/launchpad-common/hydra_request.cc index 3d4fc64..7001680 100644 --- a/src/lib/launchpad-common/hydra_request.cc +++ b/src/lib/launchpad-common/hydra_request.cc @@ -20,15 +20,20 @@ namespace launchpad { -HydraRequest::HydraRequest(int cmd) : cmd_(cmd) {} +HydraRequest::HydraRequest(int cmd, int priority) + : cmd_(cmd), priority_(priority) {} -HydraRequest::HydraRequest(int cmd, int argc, char** argv) - : cmd_(cmd), argc_(argc), argv_(argv) {} +HydraRequest::HydraRequest(int cmd, int priority, int argc, char** argv) + : cmd_(cmd), priority_(priority), argc_(argc), argv_(argv) {} int HydraRequest::GetCommand() const { return cmd_; } +int HydraRequest::GetPriority() const { + return priority_; +} + int HydraRequest::GetArgc() const { return argc_; } @@ -39,6 +44,7 @@ char** HydraRequest::GetArgv() const { void HydraRequest::WriteToParcel(tizen_base::Parcel* parcel) const { parcel->WriteInt32(cmd_); + parcel->WriteInt32(priority_); parcel->WriteInt32(argc_); for (int i = 0; i < argc_; ++i) parcel->WriteCString(argv_[i]); @@ -52,6 +58,7 @@ void HydraRequest::ReadFromParcel(tizen_base::Parcel* parcel) { argv_ = nullptr; parcel->ReadInt32(&cmd_); + parcel->ReadInt32(&priority_); parcel->ReadInt32(&argc_); if (argc_ == 0) return; diff --git a/src/lib/launchpad-common/hydra_request.hh b/src/lib/launchpad-common/hydra_request.hh index 0d06c93..fcffdd6 100644 --- a/src/lib/launchpad-common/hydra_request.hh +++ b/src/lib/launchpad-common/hydra_request.hh @@ -29,12 +29,13 @@ namespace launchpad { class EXPORT_API HydraRequest : public tizen_base::Parcelable { public: - explicit HydraRequest(int cmd); - HydraRequest(int cmd, int argc, char** argv); + explicit HydraRequest(int cmd, int priority); + HydraRequest(int cmd, int priority, int argc, char** argv); HydraRequest() = default; virtual ~HydraRequest() = default; int GetCommand() const; + int GetPriority() const; int GetArgc() const; char** GetArgv() const; @@ -43,6 +44,7 @@ class EXPORT_API HydraRequest : public tizen_base::Parcelable { private: int cmd_ = 0; + int priority_ = 0; int argc_ = 0; char** argv_ = nullptr; }; diff --git a/src/lib/launchpad-common/sched_priority.cc b/src/lib/launchpad-common/sched_priority.cc new file mode 100644 index 0000000..68d5e48 --- /dev/null +++ b/src/lib/launchpad-common/sched_priority.cc @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "launchpad-common/sched_priority.hh" + +#include +#include + +#include "launchpad-common/log_private.hh" + +namespace launchpad { + +void SchedPriority::Set(int priority) { + int ret = setpriority(PRIO_PGRP, 0, priority); + if (ret != 0) { + _E("Failed to set process priority. priority(%d), errno(%d)", + priority, errno); + } else { + _D("Setting priority(%d) is sucessful", priority); + } +} + +int SchedPriority::Get() { + return getpriority(PRIO_PGRP, 0); +} + + +} // namespace launchpad diff --git a/src/lib/launchpad-common/sched_priority.hh b/src/lib/launchpad-common/sched_priority.hh new file mode 100644 index 0000000..6e8d197 --- /dev/null +++ b/src/lib/launchpad-common/sched_priority.hh @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIB_LAUNCHPAD_COMMON_SCHED_PRIORITY_HH_ +#define LIB_LAUNCHPAD_COMMON_SCHED_PRIORITY_HH_ + +#undef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) + +namespace launchpad { + +class EXPORT_API SchedPriority { + public: + static void Set(int priority); + static int Get(); +}; + +} // namespace launchpad + +#endif // LIB_LAUNCHPAD_COMMON_SCHED_PRIORITY_HH_ diff --git a/src/lib/launchpad-glib/util.cc b/src/lib/launchpad-glib/util.cc index 3839f17..2d4e5cf 100644 --- a/src/lib/launchpad-glib/util.cc +++ b/src/lib/launchpad-glib/util.cc @@ -130,16 +130,6 @@ void Util::SetEnvironments(const AppInfo* app_info) { setenv("GCOV_PREFIX", "/tmp", 1); } -void Util::SetPriority(int priority) { - int ret = setpriority(PRIO_PROCESS, 0, priority); - if (ret != 0) { - _E("Failed to set process priority. priority(%d), errno(%d)", - priority, errno); - } else { - _D("Setting priority(%d) is sucessful", priority); - } -} - void Util::DeleteSocketPath(pid_t pid, uid_t uid) { std::string path ="/run/aul/apps/" + std::to_string(uid) + "/" + std::to_string(pid); diff --git a/src/lib/launchpad-glib/util.hh b/src/lib/launchpad-glib/util.hh index 80f9001..0589202 100644 --- a/src/lib/launchpad-glib/util.hh +++ b/src/lib/launchpad-glib/util.hh @@ -27,7 +27,6 @@ namespace launchpad { class EXPORT_API Util { public: static void SetEnvironments(const AppInfo* app_info); - static void SetPriority(int priority); static void DeleteSocketPath(pid_t pid, uid_t uid); }; diff --git a/src/lib/launchpad-hydra/executor.cc b/src/lib/launchpad-hydra/executor.cc index 06fe350..92191a6 100644 --- a/src/lib/launchpad-hydra/executor.cc +++ b/src/lib/launchpad-hydra/executor.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include "launchpad-hydra/log_private.hh" @@ -54,16 +55,6 @@ int SecurityManagerPrepareAppCandidate() { return 0; } -void SetPriority(int priority) { - int ret = setpriority(PRIO_PROCESS, 0, priority); - if (ret != 0) { - _E("Failed to set process priority. priority(%d), errno(%d)", - priority, errno); - } else { - _D("Setting priority(%d) is successful", priority); - } -} - } // namespace Executor::Executor(Executor::Delegator* delegator) : delegator_(delegator) {} @@ -76,8 +67,9 @@ pid_t Executor::Execute(int argc, char** argv, int priority) { } if (pid == 0) { + setsid(); if (priority != 0) - SetPriority(priority); + SchedPriority::Set(priority); int ret = SecurityManagerPrepareAppCandidate(); if (ret != 0) { diff --git a/src/lib/launchpad-hydra/launchpad_hydra.cc b/src/lib/launchpad-hydra/launchpad_hydra.cc index 1d301e2..c08106b 100644 --- a/src/lib/launchpad-hydra/launchpad_hydra.cc +++ b/src/lib/launchpad-hydra/launchpad_hydra.cc @@ -24,8 +24,9 @@ #include #include -#include #include +#include +#include #include "common/inc/launchpad_common.h" #include "common/inc/launchpad_types.h" @@ -131,6 +132,7 @@ void LaunchpadHydra::Run(hydra_lifecycle_callback_s* callback, candidate_pid_ = executor_->Execute(argc_, argv_); _D("Candidate process(%d)", candidate_pid_); socket_->Send(&candidate_pid_, sizeof(candidate_pid_)); + SchedPriority::Set(0); EventLoop::GetInst().Run(); OnTerminate(); @@ -195,10 +197,11 @@ void LaunchpadHydra::HandleLaunchEvent(const HydraRequest& request) { pid_t pid = -1; if (request.GetCommand() == LAUNCH_CANDIDATE) { _W("LAUNCH_CANDIDATE"); - pid = executor_->Execute(argc_, argv_); + pid = executor_->Execute(argc_, argv_, request.GetPriority()); } else if (request.GetCommand() == LAUNCH_CANDIDATE_WITH_ARGS) { _W("LAUNCH_CANDIDATE_WITH_ARGS"); - pid = executor_->Execute(request.GetArgc(), request.GetArgv()); + pid = executor_->Execute(request.GetArgc(), request.GetArgv(), + request.GetPriority()); } else { _W("Unknown command(%d)", request.GetCommand()); } @@ -269,7 +272,6 @@ void LaunchpadHydra::OnSigchldReceived(pid_t pid, int status) { void LaunchpadHydra::OnLoaderExecution(int argc, char** argv) { _D("Run loader. pid(%d)", getpid()); - setsid(); SignalManager::GetInst().Dispose(); EventLoop::GetInst().Exit(); diff --git a/src/lib/launchpad-hydra/loader_executor.cc b/src/lib/launchpad-hydra/loader_executor.cc index 9f5b8b7..d4929b4 100644 --- a/src/lib/launchpad-hydra/loader_executor.cc +++ b/src/lib/launchpad-hydra/loader_executor.cc @@ -18,12 +18,15 @@ #include #include +#include +#include #include #include #include #include +#include #include "launchpad-hydra/log_private.hh" #include "launchpad-hydra/signal_manager.hh" @@ -33,7 +36,8 @@ namespace { class LoaderArgs : public tizen_base::Parcelable { public: - explicit LoaderArgs(int argc, char** argv) { + explicit LoaderArgs(int priority, int argc, char** argv) + : priority_(priority) { for (int i = 0; i < argc; ++i) args_.push_back(argv[i]); } @@ -42,12 +46,14 @@ class LoaderArgs : public tizen_base::Parcelable { ~LoaderArgs() = default; void WriteToParcel(tizen_base::Parcel* parcel) const override { + parcel->WriteInt32(priority_); parcel->WriteInt32(args_.size()); for (size_t i = 0; i < args_.size(); ++i) parcel->WriteString(args_[i]); } void ReadFromParcel(tizen_base::Parcel* parcel) override { + parcel->ReadInt32(&priority_); int data_size = 0; parcel->ReadInt32(&data_size); args_.resize(data_size); @@ -55,11 +61,16 @@ class LoaderArgs : public tizen_base::Parcelable { args_[i] = parcel->ReadString(); } + int GetPriority() const { + return priority_; + } + const std::vector& GetArgs() const { return args_; } private: + int priority_ = 0; std::vector args_; }; @@ -71,7 +82,7 @@ LoaderExecutor::LoaderExecutor(IEvent* listener) pid_t LoaderExecutor::Execute(int argc, char** argv, int priority) { if (process_pool_->IsPrepared()) { - LoaderArgs loader_args(argc, argv); + LoaderArgs loader_args(priority, argc, argv); tizen_base::Parcel parcel; parcel.WriteParcelable(loader_args); return process_pool_->Execute(parcel); @@ -95,6 +106,10 @@ void LoaderExecutor::OnRequestReceived(tizen_base::Parcel* parcel) { LoaderArgs loader_args; parcel->ReadParcelable(&loader_args); + int priority = loader_args.GetPriority(); + if (priority != 0) + SchedPriority::Set(priority); + auto& args = loader_args.GetArgs(); std::vector loader_argv(args.size() + 1); int argc = args.size();