The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_Process.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "Process" |
| 19 | |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 20 | // To make sure cpu_set_t is included from sched.h |
| 21 | #define _GNU_SOURCE 1 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 25 | #include <cutils/sched_policy.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/String8.h> |
| 27 | #include <utils/Vector.h> |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 28 | #include <processgroup/processgroup.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 30 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | #include "android_util_Binder.h" |
| 33 | #include "JNIHelp.h" |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <dirent.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <grp.h> |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 38 | #include <inttypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <pwd.h> |
| 40 | #include <signal.h> |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 41 | #include <sys/errno.h> |
| 42 | #include <sys/resource.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/types.h> |
Glenn Kasten | 6af763b | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 45 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 47 | #define GUARD_THREAD_PRIORITY 0 |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | using namespace android; |
| 50 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 51 | static const bool kDebugPolicy = false; |
| 52 | static const bool kDebugProc = false; |
| 53 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 54 | #if GUARD_THREAD_PRIORITY |
| 55 | Mutex gKeyCreateMutex; |
| 56 | static pthread_key_t gBgKey = -1; |
| 57 | #endif |
| 58 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 59 | // For both of these, err should be in the errno range (positive), not a status_t (negative) |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 60 | static void signalExceptionForError(JNIEnv* env, int err, int tid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | switch (err) { |
| 62 | case EINVAL: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 63 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 64 | "Invalid argument: %d", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | break; |
| 66 | case ESRCH: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 67 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 68 | "Given thread %d does not exist", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | break; |
| 70 | case EPERM: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 71 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 72 | "No permission to modify given thread %d", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | break; |
| 74 | default: |
| 75 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 80 | static void signalExceptionForPriorityError(JNIEnv* env, int err, int tid) { |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 81 | switch (err) { |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 82 | case EACCES: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 83 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 84 | "No permission to set the priority of %d", tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 85 | break; |
| 86 | default: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 87 | signalExceptionForError(env, err, tid); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | static void signalExceptionForGroupError(JNIEnv* env, int err, int tid) { |
| 94 | switch (err) { |
| 95 | case EACCES: |
| 96 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 97 | "No permission to set the group of %d", tid); |
| 98 | break; |
| 99 | default: |
| 100 | signalExceptionForError(env, err, tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name) |
| 106 | { |
| 107 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 108 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | return -1; |
| 110 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 113 | String8 name8; |
| 114 | if (str16) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 115 | name8 = String8(reinterpret_cast<const char16_t*>(str16), |
| 116 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | env->ReleaseStringCritical(name, str16); |
| 118 | } |
| 119 | |
| 120 | const size_t N = name8.size(); |
| 121 | if (N > 0) { |
| 122 | const char* str = name8.string(); |
| 123 | for (size_t i=0; i<N; i++) { |
| 124 | if (str[i] < '0' || str[i] > '9') { |
| 125 | struct passwd* pwd = getpwnam(str); |
| 126 | if (pwd == NULL) { |
| 127 | return -1; |
| 128 | } |
| 129 | return pwd->pw_uid; |
| 130 | } |
| 131 | } |
| 132 | return atoi(str); |
| 133 | } |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) |
| 138 | { |
| 139 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 140 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 145 | String8 name8; |
| 146 | if (str16) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 147 | name8 = String8(reinterpret_cast<const char16_t*>(str16), |
| 148 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | env->ReleaseStringCritical(name, str16); |
| 150 | } |
| 151 | |
| 152 | const size_t N = name8.size(); |
| 153 | if (N > 0) { |
| 154 | const char* str = name8.string(); |
| 155 | for (size_t i=0; i<N; i++) { |
| 156 | if (str[i] < '0' || str[i] > '9') { |
| 157 | struct group* grp = getgrnam(str); |
| 158 | if (grp == NULL) { |
| 159 | return -1; |
| 160 | } |
| 161 | return grp->gr_gid; |
| 162 | } |
| 163 | } |
| 164 | return atoi(str); |
| 165 | } |
| 166 | return -1; |
| 167 | } |
| 168 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 169 | void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp) |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 170 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 171 | ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp); |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 172 | SchedPolicy sp = (SchedPolicy) grp; |
| 173 | int res = set_sched_policy(tid, sp); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 174 | if (res != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 175 | signalExceptionForGroupError(env, -res, tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 176 | } |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Joel Fernandes | 474d311 | 2017-04-04 16:32:15 -0700 | [diff] [blame] | 179 | void android_os_Process_setThreadGroupAndCpuset(JNIEnv* env, jobject clazz, int tid, jint grp) |
| 180 | { |
| 181 | ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp); |
| 182 | SchedPolicy sp = (SchedPolicy) grp; |
| 183 | int res = set_sched_policy(tid, sp); |
| 184 | |
| 185 | if (res != NO_ERROR) { |
| 186 | signalExceptionForGroupError(env, -res, tid); |
| 187 | } |
| 188 | |
| 189 | res = set_cpuset_policy(tid, sp); |
| 190 | if (res != NO_ERROR) { |
| 191 | signalExceptionForGroupError(env, -res, tid); |
| 192 | } |
| 193 | } |
| 194 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 195 | void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 196 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 197 | ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 198 | DIR *d; |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 199 | char proc_path[255]; |
| 200 | struct dirent *de; |
| 201 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 202 | if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 203 | signalExceptionForGroupError(env, EINVAL, pid); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 207 | bool isDefault = false; |
| 208 | if (grp < 0) { |
| 209 | grp = SP_FOREGROUND; |
| 210 | isDefault = true; |
| 211 | } |
| 212 | SchedPolicy sp = (SchedPolicy) grp; |
| 213 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 214 | if (kDebugPolicy) { |
| 215 | char cmdline[32]; |
| 216 | int fd; |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 217 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 218 | strcpy(cmdline, "unknown"); |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 219 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 220 | sprintf(proc_path, "/proc/%d/cmdline", pid); |
| 221 | fd = open(proc_path, O_RDONLY); |
| 222 | if (fd >= 0) { |
| 223 | int rc = read(fd, cmdline, sizeof(cmdline)-1); |
| 224 | cmdline[rc] = 0; |
| 225 | close(fd); |
| 226 | } |
| 227 | |
| 228 | if (sp == SP_BACKGROUND) { |
| 229 | ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); |
| 230 | } else { |
| 231 | ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); |
| 232 | } |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 233 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 234 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 235 | sprintf(proc_path, "/proc/%d/task", pid); |
| 236 | if (!(d = opendir(proc_path))) { |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 237 | // If the process exited on us, don't generate an exception |
| 238 | if (errno != ENOENT) |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 239 | signalExceptionForGroupError(env, errno, pid); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | while ((de = readdir(d))) { |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 244 | int t_pid; |
| 245 | int t_pri; |
| 246 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 247 | if (de->d_name[0] == '.') |
| 248 | continue; |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 249 | t_pid = atoi(de->d_name); |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 250 | |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 251 | if (!t_pid) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 252 | ALOGE("Error getting pid for '%s'\n", de->d_name); |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 256 | t_pri = getpriority(PRIO_PROCESS, t_pid); |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 257 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 258 | if (t_pri <= ANDROID_PRIORITY_AUDIO) { |
Tim Murray | 20375fe | 2016-12-20 11:47:24 -0800 | [diff] [blame] | 259 | int scheduler = sched_getscheduler(t_pid) & ~SCHED_RESET_ON_FORK; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 260 | if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) { |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 261 | // This task wants to stay in its current audio group so it can keep its budget |
| 262 | // don't update its cpuset or cgroup |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (isDefault) { |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 268 | if (t_pri >= ANDROID_PRIORITY_BACKGROUND) { |
| 269 | // This task wants to stay at background |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 270 | // update its cpuset so it doesn't only run on bg core(s) |
Isaac Chen | d34fac5 | 2017-02-16 11:51:08 +0800 | [diff] [blame] | 271 | if (cpusets_enabled()) { |
| 272 | int err = set_cpuset_policy(t_pid, sp); |
| 273 | if (err != NO_ERROR) { |
| 274 | signalExceptionForGroupError(env, -err, t_pid); |
| 275 | break; |
| 276 | } |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 277 | } |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 278 | continue; |
| 279 | } |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 280 | } |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 281 | int err; |
Isaac Chen | d34fac5 | 2017-02-16 11:51:08 +0800 | [diff] [blame] | 282 | |
| 283 | if (cpusets_enabled()) { |
| 284 | // set both cpuset and cgroup for general threads |
| 285 | err = set_cpuset_policy(t_pid, sp); |
| 286 | if (err != NO_ERROR) { |
| 287 | signalExceptionForGroupError(env, -err, t_pid); |
| 288 | break; |
| 289 | } |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 290 | } |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 291 | |
| 292 | err = set_sched_policy(t_pid, sp); |
| 293 | if (err != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 294 | signalExceptionForGroupError(env, -err, t_pid); |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 298 | } |
| 299 | closedir(d); |
| 300 | } |
| 301 | |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 302 | jint android_os_Process_getProcessGroup(JNIEnv* env, jobject clazz, jint pid) |
| 303 | { |
| 304 | SchedPolicy sp; |
| 305 | if (get_sched_policy(pid, &sp) != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 306 | signalExceptionForGroupError(env, errno, pid); |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 307 | } |
| 308 | return (int) sp; |
| 309 | } |
| 310 | |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 311 | /** Sample CPUset list format: |
| 312 | * 0-3,4,6-8 |
| 313 | */ |
| 314 | static void parse_cpuset_cpus(char *cpus, cpu_set_t *cpu_set) { |
| 315 | unsigned int start, end, matched, i; |
| 316 | char *cpu_range = strtok(cpus, ","); |
| 317 | while (cpu_range != NULL) { |
| 318 | start = end = 0; |
| 319 | matched = sscanf(cpu_range, "%u-%u", &start, &end); |
| 320 | cpu_range = strtok(NULL, ","); |
| 321 | if (start >= CPU_SETSIZE) { |
| 322 | ALOGE("parse_cpuset_cpus: ignoring CPU number larger than %d.", CPU_SETSIZE); |
| 323 | continue; |
| 324 | } else if (end >= CPU_SETSIZE) { |
| 325 | ALOGE("parse_cpuset_cpus: ignoring CPU numbers larger than %d.", CPU_SETSIZE); |
| 326 | end = CPU_SETSIZE - 1; |
| 327 | } |
| 328 | if (matched == 1) { |
| 329 | CPU_SET(start, cpu_set); |
| 330 | } else if (matched == 2) { |
| 331 | for (i = start; i <= end; i++) { |
| 332 | CPU_SET(i, cpu_set); |
| 333 | } |
| 334 | } else { |
| 335 | ALOGE("Failed to match cpus"); |
| 336 | } |
| 337 | } |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Stores the CPUs assigned to the cpuset corresponding to the |
| 343 | * SchedPolicy in the passed in cpu_set. |
| 344 | */ |
| 345 | static void get_cpuset_cores_for_policy(SchedPolicy policy, cpu_set_t *cpu_set) |
| 346 | { |
| 347 | FILE *file; |
| 348 | const char *filename; |
| 349 | |
| 350 | CPU_ZERO(cpu_set); |
| 351 | |
| 352 | switch (policy) { |
| 353 | case SP_BACKGROUND: |
| 354 | filename = "/dev/cpuset/background/cpus"; |
| 355 | break; |
| 356 | case SP_FOREGROUND: |
| 357 | case SP_AUDIO_APP: |
| 358 | case SP_AUDIO_SYS: |
Glenn Kasten | 73a7800 | 2017-04-27 15:29:23 -0700 | [diff] [blame] | 359 | case SP_RT_APP: |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 360 | filename = "/dev/cpuset/foreground/cpus"; |
| 361 | break; |
| 362 | case SP_TOP_APP: |
| 363 | filename = "/dev/cpuset/top-app/cpus"; |
| 364 | break; |
| 365 | default: |
| 366 | filename = NULL; |
| 367 | } |
| 368 | |
| 369 | if (!filename) return; |
| 370 | |
| 371 | file = fopen(filename, "re"); |
| 372 | if (file != NULL) { |
| 373 | // Parse cpus string |
| 374 | char *line = NULL; |
| 375 | size_t len = 0; |
| 376 | ssize_t num_read = getline(&line, &len, file); |
| 377 | fclose (file); |
| 378 | if (num_read > 0) { |
| 379 | parse_cpuset_cpus(line, cpu_set); |
| 380 | } else { |
| 381 | ALOGE("Failed to read %s", filename); |
| 382 | } |
| 383 | free(line); |
| 384 | } |
| 385 | return; |
| 386 | } |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 387 | |
| 388 | |
| 389 | /** |
| 390 | * Determine CPU cores exclusively assigned to the |
| 391 | * cpuset corresponding to the SchedPolicy and store |
| 392 | * them in the passed in cpu_set_t |
| 393 | */ |
| 394 | void get_exclusive_cpuset_cores(SchedPolicy policy, cpu_set_t *cpu_set) { |
Isaac Chen | d34fac5 | 2017-02-16 11:51:08 +0800 | [diff] [blame] | 395 | if (cpusets_enabled()) { |
| 396 | int i; |
| 397 | cpu_set_t tmp_set; |
| 398 | get_cpuset_cores_for_policy(policy, cpu_set); |
| 399 | for (i = 0; i < SP_CNT; i++) { |
| 400 | if ((SchedPolicy) i == policy) continue; |
| 401 | get_cpuset_cores_for_policy((SchedPolicy)i, &tmp_set); |
| 402 | // First get cores exclusive to one set or the other |
| 403 | CPU_XOR(&tmp_set, cpu_set, &tmp_set); |
| 404 | // Then get the ones only in cpu_set |
| 405 | CPU_AND(cpu_set, cpu_set, &tmp_set); |
| 406 | } |
| 407 | } else { |
| 408 | CPU_ZERO(cpu_set); |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 409 | } |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
| 413 | jintArray android_os_Process_getExclusiveCores(JNIEnv* env, jobject clazz) { |
| 414 | SchedPolicy sp; |
| 415 | cpu_set_t cpu_set; |
| 416 | jintArray cpus; |
| 417 | int pid = getpid(); |
| 418 | if (get_sched_policy(pid, &sp) != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 419 | signalExceptionForGroupError(env, errno, pid); |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 420 | return NULL; |
| 421 | } |
| 422 | get_exclusive_cpuset_cores(sp, &cpu_set); |
| 423 | int num_cpus = CPU_COUNT(&cpu_set); |
| 424 | cpus = env->NewIntArray(num_cpus); |
| 425 | if (cpus == NULL) { |
| 426 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | jint* cpu_elements = env->GetIntArrayElements(cpus, 0); |
| 431 | int count = 0; |
| 432 | for (int i = 0; i < CPU_SETSIZE && count < num_cpus; i++) { |
| 433 | if (CPU_ISSET(i, &cpu_set)) { |
| 434 | cpu_elements[count++] = i; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | env->ReleaseIntArrayElements(cpus, cpu_elements, 0); |
| 439 | return cpus; |
| 440 | } |
| 441 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 442 | static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) { |
| 443 | // Establishes the calling thread as illegal to put into the background. |
| 444 | // Typically used only for the system process's main looper. |
| 445 | #if GUARD_THREAD_PRIORITY |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 446 | ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid()); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 447 | { |
| 448 | Mutex::Autolock _l(gKeyCreateMutex); |
| 449 | if (gBgKey == -1) { |
| 450 | pthread_key_create(&gBgKey, NULL); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // inverted: not-okay, we set a sentinel value |
| 455 | pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad)); |
| 456 | #endif |
| 457 | } |
| 458 | |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 459 | jint android_os_Process_getThreadScheduler(JNIEnv* env, jclass clazz, |
| 460 | jint tid) |
| 461 | { |
| 462 | int policy = 0; |
| 463 | // linux has sched_getscheduler(), others don't. |
| 464 | #if defined(__linux__) |
| 465 | errno = 0; |
| 466 | policy = sched_getscheduler(tid); |
| 467 | if (errno != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 468 | signalExceptionForPriorityError(env, errno, tid); |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 469 | } |
| 470 | #else |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 471 | signalExceptionForPriorityError(env, ENOSYS, tid); |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 472 | #endif |
| 473 | return policy; |
| 474 | } |
| 475 | |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 476 | void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz, |
| 477 | jint tid, jint policy, jint pri) |
| 478 | { |
Yabin Cui | 65b4a68 | 2014-11-10 12:15:46 -0800 | [diff] [blame] | 479 | // linux has sched_setscheduler(), others don't. |
| 480 | #if defined(__linux__) |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 481 | struct sched_param param; |
| 482 | param.sched_priority = pri; |
| 483 | int rc = sched_setscheduler(tid, policy, ¶m); |
| 484 | if (rc) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 485 | signalExceptionForPriorityError(env, errno, tid); |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 486 | } |
Glenn Kasten | cc76719 | 2012-01-17 08:38:51 -0800 | [diff] [blame] | 487 | #else |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 488 | signalExceptionForPriorityError(env, ENOSYS, tid); |
Glenn Kasten | cc76719 | 2012-01-17 08:38:51 -0800 | [diff] [blame] | 489 | #endif |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 490 | } |
| 491 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, |
| 493 | jint pid, jint pri) |
| 494 | { |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 495 | #if GUARD_THREAD_PRIORITY |
| 496 | // if we're putting the current thread into the background, check the TLS |
| 497 | // to make sure this thread isn't guarded. If it is, raise an exception. |
| 498 | if (pri >= ANDROID_PRIORITY_BACKGROUND) { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 499 | if (pid == gettid()) { |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 500 | void* bgOk = pthread_getspecific(gBgKey); |
| 501 | if (bgOk == ((void*)0xbaad)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 502 | ALOGE("Thread marked fg-only put self in background!"); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 503 | jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background"); |
| 504 | return; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | #endif |
| 509 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 510 | int rc = androidSetThreadPriority(pid, pri); |
| 511 | if (rc != 0) { |
| 512 | if (rc == INVALID_OPERATION) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 513 | signalExceptionForPriorityError(env, errno, pid); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 514 | } else { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 515 | signalExceptionForGroupError(env, errno, pid); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 516 | } |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 517 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 518 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 519 | //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | // pid, pri, getpriority(PRIO_PROCESS, pid)); |
| 521 | } |
| 522 | |
| 523 | void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz, |
| 524 | jint pri) |
| 525 | { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 526 | android_os_Process_setThreadPriority(env, clazz, gettid(), pri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz, |
| 530 | jint pid) |
| 531 | { |
| 532 | errno = 0; |
| 533 | jint pri = getpriority(PRIO_PROCESS, pid); |
| 534 | if (errno != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 535 | signalExceptionForPriorityError(env, errno, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 537 | //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | return pri; |
| 539 | } |
| 540 | |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 541 | jboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz, |
| 542 | jint pid, jboolean is_increased) |
| 543 | { |
| 544 | char text[64]; |
| 545 | |
| 546 | if (is_increased) { |
| 547 | strcpy(text, "/sys/fs/cgroup/memory/sw/tasks"); |
| 548 | } else { |
| 549 | strcpy(text, "/sys/fs/cgroup/memory/tasks"); |
| 550 | } |
| 551 | |
| 552 | struct stat st; |
| 553 | if (stat(text, &st) || !S_ISREG(st.st_mode)) { |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | int fd = open(text, O_WRONLY); |
| 558 | if (fd >= 0) { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 559 | sprintf(text, "%" PRId32, pid); |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 560 | write(fd, text, strlen(text)); |
| 561 | close(fd); |
| 562 | } |
| 563 | |
| 564 | return true; |
| 565 | } |
| 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name) |
| 568 | { |
| 569 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 570 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | return; |
| 572 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 573 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | const jchar* str = env->GetStringCritical(name, 0); |
| 575 | String8 name8; |
| 576 | if (str) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 577 | name8 = String8(reinterpret_cast<const char16_t*>(str), |
| 578 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | env->ReleaseStringCritical(name, str); |
| 580 | } |
| 581 | |
Dmitriy Filchenko | 342c7dc | 2016-07-12 15:40:54 -0700 | [diff] [blame] | 582 | if (!name8.isEmpty()) { |
Dmitriy Filchenko | f5b6e55 | 2016-07-18 16:00:35 -0700 | [diff] [blame] | 583 | AndroidRuntime::getRuntime()->setArgv0(name8.string(), true /* setProcName */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid) |
| 588 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | return setuid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid) |
| 593 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | return setgid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
| 596 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | static int pid_compare(const void* v1, const void* v2) |
| 598 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 599 | //ALOGI("Compare %" PRId32 " vs %" PRId32 "\n", *((const jint*)v1), *((const jint*)v2)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | return *((const jint*)v1) - *((const jint*)v2); |
| 601 | } |
| 602 | |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 603 | static jlong getFreeMemoryImpl(const char* const sums[], const size_t sumsLen[], size_t num) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | { |
| 605 | int fd = open("/proc/meminfo", O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 608 | ALOGW("Unable to open /proc/meminfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | return -1; |
| 610 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | char buffer[256]; |
| 613 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 614 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | if (len < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 617 | ALOGW("Unable to read /proc/meminfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | return -1; |
| 619 | } |
| 620 | buffer[len] = 0; |
| 621 | |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 622 | size_t numFound = 0; |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 623 | jlong mem = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | char* p = buffer; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 626 | while (*p && numFound < num) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | int i = 0; |
| 628 | while (sums[i]) { |
| 629 | if (strncmp(p, sums[i], sumsLen[i]) == 0) { |
| 630 | p += sumsLen[i]; |
| 631 | while (*p == ' ') p++; |
| 632 | char* num = p; |
| 633 | while (*p >= '0' && *p <= '9') p++; |
| 634 | if (*p != 0) { |
| 635 | *p = 0; |
| 636 | p++; |
| 637 | if (*p == 0) p--; |
| 638 | } |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 639 | mem += atoll(num) * 1024; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | numFound++; |
| 641 | break; |
| 642 | } |
| 643 | i++; |
| 644 | } |
| 645 | p++; |
| 646 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | return numFound > 0 ? mem : -1; |
| 649 | } |
| 650 | |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 651 | static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) |
| 652 | { |
| 653 | static const char* const sums[] = { "MemFree:", "Cached:", NULL }; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 654 | static const size_t sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 }; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 655 | return getFreeMemoryImpl(sums, sumsLen, 2); |
| 656 | } |
| 657 | |
| 658 | static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz) |
| 659 | { |
| 660 | static const char* const sums[] = { "MemTotal:", NULL }; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 661 | static const size_t sumsLen[] = { strlen("MemTotal:"), 0 }; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 662 | return getFreeMemoryImpl(sums, sumsLen, 1); |
| 663 | } |
| 664 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, |
| 666 | jobjectArray reqFields, jlongArray outFields) |
| 667 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 668 | //ALOGI("getMemInfo: %p %p", reqFields, outFields); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | if (fileStr == NULL || reqFields == NULL || outFields == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 671 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | return; |
| 673 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | const char* file8 = env->GetStringUTFChars(fileStr, NULL); |
| 676 | if (file8 == NULL) { |
| 677 | return; |
| 678 | } |
| 679 | String8 file(file8); |
| 680 | env->ReleaseStringUTFChars(fileStr, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | jsize count = env->GetArrayLength(reqFields); |
| 683 | if (count > env->GetArrayLength(outFields)) { |
| 684 | jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ"); |
| 685 | return; |
| 686 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | Vector<String8> fields; |
| 689 | int i; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | for (i=0; i<count; i++) { |
| 692 | jobject obj = env->GetObjectArrayElement(reqFields, i); |
| 693 | if (obj != NULL) { |
| 694 | const char* str8 = env->GetStringUTFChars((jstring)obj, NULL); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 695 | //ALOGI("String at %d: %p = %s", i, obj, str8); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | if (str8 == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 697 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | fields.add(String8(str8)); |
| 701 | env->ReleaseStringUTFChars((jstring)obj, str8); |
| 702 | } else { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 703 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | jlong* sizesArray = env->GetLongArrayElements(outFields, 0); |
| 709 | if (sizesArray == NULL) { |
| 710 | return; |
| 711 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 712 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 713 | //ALOGI("Clearing %" PRId32 " sizes", count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | for (i=0; i<count; i++) { |
| 715 | sizesArray[i] = 0; |
| 716 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | int fd = open(file.string(), O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | if (fd >= 0) { |
| 721 | const size_t BUFFER_SIZE = 2048; |
| 722 | char* buffer = (char*)malloc(BUFFER_SIZE); |
| 723 | int len = read(fd, buffer, BUFFER_SIZE-1); |
| 724 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | if (len < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 727 | ALOGW("Unable to read %s", file.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | len = 0; |
| 729 | } |
| 730 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | int foundCount = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | char* p = buffer; |
| 735 | while (*p && foundCount < count) { |
| 736 | bool skipToEol = true; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 737 | //ALOGI("Parsing at: %s", p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | for (i=0; i<count; i++) { |
| 739 | const String8& field = fields[i]; |
| 740 | if (strncmp(p, field.string(), field.length()) == 0) { |
| 741 | p += field.length(); |
Amith Yamasani | add868c | 2009-06-25 11:57:40 -0700 | [diff] [blame] | 742 | while (*p == ' ' || *p == '\t') p++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | char* num = p; |
| 744 | while (*p >= '0' && *p <= '9') p++; |
| 745 | skipToEol = *p != '\n'; |
| 746 | if (*p != 0) { |
| 747 | *p = 0; |
| 748 | p++; |
| 749 | } |
| 750 | char* end; |
| 751 | sizesArray[i] = strtoll(num, &end, 10); |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 752 | //ALOGI("Field %s = %" PRId64, field.string(), sizesArray[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | foundCount++; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | if (skipToEol) { |
| 758 | while (*p && *p != '\n') { |
| 759 | p++; |
| 760 | } |
| 761 | if (*p == '\n') { |
| 762 | p++; |
| 763 | } |
| 764 | } |
| 765 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 766 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | free(buffer); |
| 768 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 769 | ALOGW("Unable to open %s", file.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 771 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 772 | //ALOGI("Done!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | env->ReleaseLongArrayElements(outFields, sizesArray, 0); |
| 774 | } |
| 775 | |
| 776 | jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz, |
| 777 | jstring file, jintArray lastArray) |
| 778 | { |
| 779 | if (file == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 780 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | return NULL; |
| 782 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 783 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 785 | if (file8 == NULL) { |
| 786 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 787 | return NULL; |
| 788 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 789 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | DIR* dirp = opendir(file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | if(dirp == NULL) { |
| 795 | return NULL; |
| 796 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | jsize curCount = 0; |
| 799 | jint* curData = NULL; |
| 800 | if (lastArray != NULL) { |
| 801 | curCount = env->GetArrayLength(lastArray); |
| 802 | curData = env->GetIntArrayElements(lastArray, 0); |
| 803 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | jint curPos = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | struct dirent* entry; |
| 808 | while ((entry=readdir(dirp)) != NULL) { |
| 809 | const char* p = entry->d_name; |
| 810 | while (*p) { |
| 811 | if (*p < '0' || *p > '9') break; |
| 812 | p++; |
| 813 | } |
| 814 | if (*p != 0) continue; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 815 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | char* end; |
| 817 | int pid = strtol(entry->d_name, &end, 10); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 818 | //ALOGI("File %s pid=%d\n", entry->d_name, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | if (curPos >= curCount) { |
| 820 | jsize newCount = (curCount == 0) ? 10 : (curCount*2); |
| 821 | jintArray newArray = env->NewIntArray(newCount); |
| 822 | if (newArray == NULL) { |
| 823 | closedir(dirp); |
| 824 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 825 | return NULL; |
| 826 | } |
| 827 | jint* newData = env->GetIntArrayElements(newArray, 0); |
| 828 | if (curData != NULL) { |
| 829 | memcpy(newData, curData, sizeof(jint)*curCount); |
| 830 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 831 | } |
| 832 | lastArray = newArray; |
| 833 | curCount = newCount; |
| 834 | curData = newData; |
| 835 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | curData[curPos] = pid; |
| 838 | curPos++; |
| 839 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | closedir(dirp); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 842 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | if (curData != NULL && curPos > 0) { |
| 844 | qsort(curData, curPos, sizeof(jint), pid_compare); |
| 845 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | while (curPos < curCount) { |
| 848 | curData[curPos] = -1; |
| 849 | curPos++; |
| 850 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 851 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | if (curData != NULL) { |
| 853 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 854 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | return lastArray; |
| 857 | } |
| 858 | |
| 859 | enum { |
| 860 | PROC_TERM_MASK = 0xff, |
| 861 | PROC_ZERO_TERM = 0, |
| 862 | PROC_SPACE_TERM = ' ', |
| 863 | PROC_COMBINE = 0x100, |
| 864 | PROC_PARENS = 0x200, |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 865 | PROC_QUOTES = 0x400, |
Dianne Hackborn | 4de5a3a | 2016-06-20 15:20:15 -0700 | [diff] [blame] | 866 | PROC_CHAR = 0x800, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | PROC_OUT_STRING = 0x1000, |
| 868 | PROC_OUT_LONG = 0x2000, |
| 869 | PROC_OUT_FLOAT = 0x4000, |
| 870 | }; |
| 871 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 872 | jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 873 | char* buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 874 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 876 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | const jsize NF = env->GetArrayLength(format); |
| 878 | const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0; |
| 879 | const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0; |
| 880 | const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | jint* formatData = env->GetIntArrayElements(format, 0); |
| 883 | jlong* longsData = outLongs ? |
| 884 | env->GetLongArrayElements(outLongs, 0) : NULL; |
| 885 | jfloat* floatsData = outFloats ? |
| 886 | env->GetFloatArrayElements(outFloats, 0) : NULL; |
| 887 | if (formatData == NULL || (NL > 0 && longsData == NULL) |
| 888 | || (NR > 0 && floatsData == NULL)) { |
| 889 | if (formatData != NULL) { |
| 890 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 891 | } |
| 892 | if (longsData != NULL) { |
| 893 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 894 | } |
| 895 | if (floatsData != NULL) { |
| 896 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 897 | } |
| 898 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 899 | return JNI_FALSE; |
| 900 | } |
| 901 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 902 | jsize i = startIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | jsize di = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | jboolean res = JNI_TRUE; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | for (jsize fi=0; fi<NF; fi++) { |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 908 | jint mode = formatData[fi]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | if ((mode&PROC_PARENS) != 0) { |
| 910 | i++; |
Bernhard Rosenkränzer | 3b1e22e | 2014-11-17 22:30:56 +0100 | [diff] [blame] | 911 | } else if ((mode&PROC_QUOTES) != 0) { |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 912 | if (buffer[i] == '"') { |
| 913 | i++; |
| 914 | } else { |
| 915 | mode &= ~PROC_QUOTES; |
| 916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
| 918 | const char term = (char)(mode&PROC_TERM_MASK); |
| 919 | const jsize start = i; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 920 | if (i >= endIndex) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 921 | if (kDebugProc) { |
| 922 | ALOGW("Ran off end of data @%d", i); |
| 923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | res = JNI_FALSE; |
| 925 | break; |
| 926 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | jsize end = -1; |
| 929 | if ((mode&PROC_PARENS) != 0) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 930 | while (i < endIndex && buffer[i] != ')') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | i++; |
| 932 | } |
| 933 | end = i; |
| 934 | i++; |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 935 | } else if ((mode&PROC_QUOTES) != 0) { |
| 936 | while (buffer[i] != '"' && i < endIndex) { |
| 937 | i++; |
| 938 | } |
| 939 | end = i; |
| 940 | i++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 942 | while (i < endIndex && buffer[i] != term) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | i++; |
| 944 | } |
| 945 | if (end < 0) { |
| 946 | end = i; |
| 947 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 948 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 949 | if (i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | i++; |
| 951 | if ((mode&PROC_COMBINE) != 0) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 952 | while (i < endIndex && buffer[i] == term) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | i++; |
| 954 | } |
| 955 | } |
| 956 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 957 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 958 | //ALOGI("Field %" PRId32 ": %" PRId32 "-%" PRId32 " dest=%" PRId32 " mode=0x%" PRIx32 "\n", i, start, end, di, mode); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) { |
| 961 | char c = buffer[end]; |
| 962 | buffer[end] = 0; |
| 963 | if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) { |
| 964 | char* end; |
| 965 | floatsData[di] = strtof(buffer+start, &end); |
| 966 | } |
| 967 | if ((mode&PROC_OUT_LONG) != 0 && di < NL) { |
Dianne Hackborn | 4de5a3a | 2016-06-20 15:20:15 -0700 | [diff] [blame] | 968 | if ((mode&PROC_CHAR) != 0) { |
| 969 | // Caller wants single first character returned as one long. |
| 970 | longsData[di] = buffer[start]; |
| 971 | } else { |
| 972 | char* end; |
| 973 | longsData[di] = strtoll(buffer+start, &end, 10); |
| 974 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | } |
| 976 | if ((mode&PROC_OUT_STRING) != 0 && di < NS) { |
| 977 | jstring str = env->NewStringUTF(buffer+start); |
| 978 | env->SetObjectArrayElement(outStrings, di, str); |
| 979 | } |
| 980 | buffer[end] = c; |
| 981 | di++; |
| 982 | } |
| 983 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 984 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 986 | if (longsData != NULL) { |
| 987 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 988 | } |
| 989 | if (floatsData != NULL) { |
| 990 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 991 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 992 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | return res; |
| 994 | } |
| 995 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 996 | jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 997 | jbyteArray buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 998 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
| 999 | { |
| 1000 | jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL); |
| 1001 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1002 | jboolean result = android_os_Process_parseProcLineArray(env, clazz, |
| 1003 | (char*) bufferArray, startIndex, endIndex, format, outStrings, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1004 | outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1005 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1006 | env->ReleaseByteArrayElements(buffer, bufferArray, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1007 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1008 | return result; |
| 1009 | } |
| 1010 | |
| 1011 | jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz, |
| 1012 | jstring file, jintArray format, jobjectArray outStrings, |
| 1013 | jlongArray outLongs, jfloatArray outFloats) |
| 1014 | { |
| 1015 | if (file == NULL || format == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1016 | jniThrowNullPointerException(env, NULL); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1017 | return JNI_FALSE; |
| 1018 | } |
| 1019 | |
| 1020 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 1021 | if (file8 == NULL) { |
| 1022 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 1023 | return JNI_FALSE; |
| 1024 | } |
| 1025 | int fd = open(file8, O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1026 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1027 | if (fd < 0) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1028 | if (kDebugProc) { |
| 1029 | ALOGW("Unable to open process file: %s\n", file8); |
| 1030 | } |
Dianne Hackborn | 306af67 | 2014-06-24 15:41:03 -0700 | [diff] [blame] | 1031 | env->ReleaseStringUTFChars(file, file8); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1032 | return JNI_FALSE; |
| 1033 | } |
Dianne Hackborn | 306af67 | 2014-06-24 15:41:03 -0700 | [diff] [blame] | 1034 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1035 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1036 | char buffer[256]; |
| 1037 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 1038 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1039 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1040 | if (len < 0) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1041 | if (kDebugProc) { |
| 1042 | ALOGW("Unable to open process file: %s fd=%d\n", file8, fd); |
| 1043 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1044 | return JNI_FALSE; |
| 1045 | } |
| 1046 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1047 | |
| 1048 | return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1049 | format, outStrings, outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1050 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz, |
| 1054 | jobject binderObject) |
| 1055 | { |
| 1056 | if (binderObject == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1057 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | sp<IBinder> binder = ibinderForJavaObject(env, binderObject); |
| 1062 | } |
| 1063 | |
| 1064 | void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 1065 | { |
| 1066 | if (pid > 0) { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1067 | ALOGI("Sending signal. PID: %" PRId32 " SIG: %" PRId32, pid, sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | kill(pid, sig); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1072 | void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 1073 | { |
| 1074 | if (pid > 0) { |
| 1075 | kill(pid, sig); |
| 1076 | } |
| 1077 | } |
| 1078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz) |
| 1080 | { |
| 1081 | struct timespec ts; |
| 1082 | |
| 1083 | int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1084 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | if (res != 0) { |
| 1086 | return (jlong) 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 1090 | return (jlong) nanoseconds_to_milliseconds(when); |
| 1091 | } |
| 1092 | |
| 1093 | static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid) |
| 1094 | { |
| 1095 | char filename[64]; |
| 1096 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1097 | snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/smaps", pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | |
| 1099 | FILE * file = fopen(filename, "r"); |
| 1100 | if (!file) { |
| 1101 | return (jlong) -1; |
| 1102 | } |
| 1103 | |
| 1104 | // Tally up all of the Pss from the various maps |
| 1105 | char line[256]; |
| 1106 | jlong pss = 0; |
| 1107 | while (fgets(line, sizeof(line), file)) { |
| 1108 | jlong v; |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1109 | if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | pss += v; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | fclose(file); |
| 1115 | |
| 1116 | // Return the Pss value in bytes, not kilobytes |
| 1117 | return pss * 1024; |
| 1118 | } |
| 1119 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1120 | jintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz, |
| 1121 | jobjectArray commandNames) |
| 1122 | { |
| 1123 | if (commandNames == NULL) { |
| 1124 | jniThrowNullPointerException(env, NULL); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | Vector<String8> commands; |
| 1129 | |
| 1130 | jsize count = env->GetArrayLength(commandNames); |
| 1131 | |
| 1132 | for (int i=0; i<count; i++) { |
| 1133 | jobject obj = env->GetObjectArrayElement(commandNames, i); |
| 1134 | if (obj != NULL) { |
| 1135 | const char* str8 = env->GetStringUTFChars((jstring)obj, NULL); |
| 1136 | if (str8 == NULL) { |
| 1137 | jniThrowNullPointerException(env, "Element in commandNames"); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | commands.add(String8(str8)); |
| 1141 | env->ReleaseStringUTFChars((jstring)obj, str8); |
| 1142 | } else { |
| 1143 | jniThrowNullPointerException(env, "Element in commandNames"); |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | Vector<jint> pids; |
| 1149 | |
| 1150 | DIR *proc = opendir("/proc"); |
| 1151 | if (proc == NULL) { |
| 1152 | fprintf(stderr, "/proc: %s\n", strerror(errno)); |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | |
| 1156 | struct dirent *d; |
| 1157 | while ((d = readdir(proc))) { |
| 1158 | int pid = atoi(d->d_name); |
| 1159 | if (pid <= 0) continue; |
| 1160 | |
| 1161 | char path[PATH_MAX]; |
| 1162 | char data[PATH_MAX]; |
| 1163 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 1164 | |
| 1165 | int fd = open(path, O_RDONLY); |
| 1166 | if (fd < 0) { |
| 1167 | continue; |
| 1168 | } |
| 1169 | const int len = read(fd, data, sizeof(data)-1); |
| 1170 | close(fd); |
| 1171 | |
| 1172 | if (len < 0) { |
| 1173 | continue; |
| 1174 | } |
| 1175 | data[len] = 0; |
| 1176 | |
| 1177 | for (int i=0; i<len; i++) { |
| 1178 | if (data[i] == ' ') { |
| 1179 | data[i] = 0; |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | for (size_t i=0; i<commands.size(); i++) { |
| 1185 | if (commands[i] == data) { |
| 1186 | pids.add(pid); |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | closedir(proc); |
| 1193 | |
| 1194 | jintArray pidArray = env->NewIntArray(pids.size()); |
| 1195 | if (pidArray == NULL) { |
| 1196 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
| 1200 | if (pids.size() > 0) { |
| 1201 | env->SetIntArrayRegion(pidArray, 0, pids.size(), pids.array()); |
| 1202 | } |
| 1203 | |
| 1204 | return pidArray; |
| 1205 | } |
| 1206 | |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 1207 | jint android_os_Process_killProcessGroup(JNIEnv* env, jobject clazz, jint uid, jint pid) |
| 1208 | { |
| 1209 | return killProcessGroup(uid, pid, SIGKILL); |
| 1210 | } |
| 1211 | |
| 1212 | void android_os_Process_removeAllProcessGroups(JNIEnv* env, jobject clazz) |
| 1213 | { |
| 1214 | return removeAllProcessGroups(); |
| 1215 | } |
| 1216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | static const JNINativeMethod methods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName}, |
| 1219 | {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName}, |
| 1220 | {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority}, |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 1221 | {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler}, |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1222 | {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority}, |
| 1224 | {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority}, |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 1225 | {"getThreadScheduler", "(I)I", (void*)android_os_Process_getThreadScheduler}, |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 1226 | {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup}, |
Joel Fernandes | 474d311 | 2017-04-04 16:32:15 -0700 | [diff] [blame] | 1227 | {"setThreadGroupAndCpuset", "(II)V", (void*)android_os_Process_setThreadGroupAndCpuset}, |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 1228 | {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup}, |
| 1229 | {"getProcessGroup", "(I)I", (void*)android_os_Process_getProcessGroup}, |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 1230 | {"getExclusiveCores", "()[I", (void*)android_os_Process_getExclusiveCores}, |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 1231 | {"setSwappiness", "(IZ)Z", (void*)android_os_Process_setSwappiness}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0}, |
| 1233 | {"setUid", "(I)I", (void*)android_os_Process_setUid}, |
| 1234 | {"setGid", "(I)I", (void*)android_os_Process_setGid}, |
| 1235 | {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal}, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1236 | {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet}, |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 1237 | {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory}, |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 1238 | {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines}, |
| 1240 | {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids}, |
| 1241 | {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile}, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1242 | {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime}, |
| 1244 | {"getPss", "(I)J", (void*)android_os_Process_getPss}, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1245 | {"getPidsForCommands", "([Ljava/lang/String;)[I", (void*)android_os_Process_getPidsForCommands}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject}, |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 1247 | {"killProcessGroup", "(II)I", (void*)android_os_Process_killProcessGroup}, |
| 1248 | {"removeAllProcessGroups", "()V", (void*)android_os_Process_removeAllProcessGroups}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | }; |
| 1250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | int register_android_os_Process(JNIEnv* env) |
| 1252 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1253 | return RegisterMethodsOrDie(env, "android/os/Process", methods, NELEM(methods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | } |