4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongGi Jang <dg0402.jang@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <sys/types.h>
30 #include "sysman-priv.h"
32 #define PERMANENT_DIR "/tmp/permanent"
33 #define VIP_DIR "/tmp/vip"
34 #define PMON_VIP "/sys/class/pmon/mp_vip"
35 #define PMON_PNP "/sys/class/pmon/mp_pnp"
43 #define OOMADJ_SET "oomadj_set"
45 int util_oomadj_set(int pid, int oomadj_val)
50 snprintf(buf1, sizeof(buf1), "%d", pid);
51 snprintf(buf2, sizeof(buf2), "%d", oomadj_val);
52 return sysman_call_predef_action(OOMADJ_SET, 2, buf1, buf2);
55 API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
73 return util_oomadj_set(pid, oomadj_val);
76 API int sysconf_set_mempolicy(enum mem_policy mempol)
78 return sysconf_set_mempolicy_bypid(getpid(), mempol);
81 static int already_permanent(int pid)
85 snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
87 if (access(buf, R_OK) == 0) {
88 DBG("already_permanent process : %d", pid);
94 static int copy_cmdline(int pid)
97 char filepath[PATH_MAX];
101 if (access(PERMANENT_DIR, R_OK) < 0) {
102 DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR);
103 mkdir(PERMANENT_DIR, 0777);
106 snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
108 fd = open(filepath, O_RDONLY);
110 DBG("Failed to open");
114 cnt = read(fd, buf, PATH_MAX);
118 /* Read /proc/<pid>/cmdline error */
119 DBG("Failed to read");
123 snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
125 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
127 DBG("Failed to open");
131 if (write(fd, buf, cnt) == -1) {
132 DBG("Failed to write");
141 API int sysconf_set_vip(int pid)
149 if (access(VIP_DIR, R_OK) < 0) {
150 DBG("no predefined matrix dir = %s, so created", VIP_DIR);
151 mkdir(VIP_DIR, 0777);
154 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
155 fd = open(buf, O_CREAT | O_RDWR, 0644);
158 fd = open(PMON_VIP, O_WRONLY);
161 DBG("File open error");
165 write(fd, &pid, sizeof(int));
171 API int sysconf_is_vip(int pid)
178 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
180 if (access(buf, R_OK) == 0)
186 API int sysconf_set_permanent_bypid(int pid)
189 if (already_permanent(pid))
192 if (copy_cmdline(pid) < 0)
195 fd = open(PMON_PNP, O_WRONLY);
198 DBG("File open error");
202 write(fd, &pid, sizeof(int));
206 util_oomadj_set(pid, -17);
211 API int sysconf_set_permanent()
213 pid_t pid = getpid();
214 return sysconf_set_permanent_bypid(pid);