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