Apply cpu priority inheritance
[platform/core/appfw/pkgmgr-info.git] / src / server / runner.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cpu-boosting.h>
18 #include <fcntl.h>
19 #include <glib-unix.h>
20 #include <stdlib.h>
21 #include <vconf.h>
22
23 #include <algorithm>
24 #include <string>
25
26 #include "cache_flag.hh"
27 #include "create_cache_request.hh"
28 #include "cynara_checker.hh"
29 #include "pkg_request.hh"
30 #include "runner.hh"
31 #include "utils/logging.hh"
32
33 #include "pkgmgrinfo_debug.h"
34 #include "pkgmgrinfo_private.h"
35
36 #ifdef LOG_TAG
37 #undef LOG_TAG
38 #endif
39 #define LOG_TAG "PKGMGR_INFO"
40
41 namespace pkgmgr_server {
42 namespace {
43
44 static const std::string SOCK_PATH = "/run/pkgmgr-info-server";
45 constexpr const char PRIVILEGE_PACKAGE_MANAGER_ADMIN[] =
46     "http://tizen.org/privilege/packagemanager.admin";
47 constexpr const char DEST_PROCESS_NAME[] = "pkgmgr-info";
48
49 }  // namespace
50
51 Runner::Runner(unsigned int thread_num) {
52   /* thread_num_ <= hardware_concurrency */
53   thread_num_ = std::min(thread_num, std::thread::hardware_concurrency());
54   CynaraChecker::GetInst().Init();
55   server_ = std::make_unique<pkgmgr_common::socket::ServerSocket>(SOCK_PATH);
56   thread_pool_ = std::make_unique<WorkerThread>(thread_num_);
57   SetCPUInheritance();
58   auto condition = static_cast<GIOCondition>(G_IO_IN);
59   sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this);
60   pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this);
61   thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
62
63   if (CacheFlag::SetPreparing()) {
64     QueueRequest(
65         std::make_shared<CreateCacheRequest>(
66             tzplatform_getuid(TZ_SYS_DEFAULT_USER)));
67   }
68   LOGI("Start Runner");
69 }
70
71 Runner::~Runner() {
72   g_source_remove(sid_);
73   CynaraChecker::GetInst().Fini();
74   pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
75 }
76
77 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
78   auto runner = static_cast<Runner*>(user_data);
79   int client_fd = runner->server_->Accept();
80   if (client_fd < 0) {
81     LOG(ERROR) << "Failed to Accept. errno:" << errno;
82     return G_SOURCE_CONTINUE;
83   }
84
85   auto req = std::make_shared<PkgRequest>(client_fd);
86
87   if (CacheFlag::SetPreparing()) {
88     runner->QueueRequest(
89         std::make_shared<CreateCacheRequest>(req->GetSenderUID()));
90   }
91   runner->QueueRequest(std::move(req));
92
93   return G_SOURCE_CONTINUE;
94 }
95
96 void Runner::OnChanged(const std::string& locale) {
97   thread_pool_->SetLocale(locale);
98 }
99
100 bool Runner::QueueRequest(std::shared_ptr<PkgRequest> req) {
101   thread_pool_->PushQueue(std::move(req));
102   return true;
103 }
104
105 void Runner::SetCPUInheritance() {
106   int ret;
107   resource_pid_t resource_st = { 0, };
108   resource_st.pid = getpid();
109
110   ret = resource_register_cpu_inheritance_destination(
111       DEST_PROCESS_NAME, resource_st);
112   if (ret != 0)
113     LOG(ERROR) << "Fail to register cpu inheritance destination ret : " << ret;
114 }
115
116 }  // namespace pkgmgr_server