Set nice value -10 when making cache
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / create_cache_request_handler.cc
1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "create_cache_request_handler.hh"
6
7 #include <sys/resource.h>
8
9 #include <string>
10
11 #include "cache_db_handler.hh"
12 #include "utils/logging.hh"
13
14 namespace psd = pkgmgr_server::database;
15
16 namespace pkgmgr_server {
17 namespace request_handler {
18
19 CreateCacheRequestHandler::Scheduler::Scheduler(pid_t tid)
20     : tid_(tid), priority_(0) {
21   if (Get(&priority_) != 0)
22     priority_ = 0;
23 }
24
25 void CreateCacheRequestHandler::Scheduler::ChangePriority() {
26   if (Set(-10) != 0)
27     return;
28
29   int priority = 0;
30   if (Get(&priority) == 0)
31     LOG(WARNING) << "Current priority: " << priority;
32 }
33
34 void CreateCacheRequestHandler::Scheduler::ResetPriority() {
35   if (Set(priority_) != 0)
36     return;
37
38   int priority = 0;
39   if (Get(&priority) == 0)
40     LOG(WARNING) << "Current priority: " << priority;
41 }
42
43 int CreateCacheRequestHandler::Scheduler::Get(int* priority) {
44   errno = 0;
45   *priority = getpriority(PRIO_PROCESS, 0);
46   if (errno != 0) {
47     LOG(ERROR) << "getpriority() is failed. errno: " << errno;
48     return -1;
49   }
50
51   return 0;
52 }
53
54 int CreateCacheRequestHandler::Scheduler::Set(int priority) {
55   if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
56     LOG(ERROR) << "setpriority() is failed. , errno: " << errno;
57     return -1;
58   }
59
60   LOG(WARNING) << "priority: " << priority;
61   return 0;
62 }
63
64 CreateCacheRequestHandler::CreateCacheRequestHandler(): scheduler_(gettid()) {
65 }
66
67 bool CreateCacheRequestHandler::HandleRequest(const unsigned char* data, int size,
68                                             const std::string& locale) {
69   psd::CacheDBHandler db(GetUID(), GetPID());
70   db.SetLocale(locale);
71
72   int ret = db.Execute();
73
74   return ret == PMINFO_R_OK;
75 }
76
77 std::vector<uint8_t> CreateCacheRequestHandler::ExtractResult() {
78   return {};
79 }
80
81 void CreateCacheRequestHandler::PreExec() {
82   scheduler_.ChangePriority();
83 }
84
85 void CreateCacheRequestHandler::PostExec() {
86   scheduler_.ResetPriority();
87 }
88
89 }  // namespace request_handler
90 }  // namespace pkgmgr_server