Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <pthread.h> |
| 6 | #include <sys/mman.h> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 7 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 8 | #include <algorithm> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 9 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 10 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 11 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 12 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 13 | #include "class_linker.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 14 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 15 | #include "jni_internal.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 16 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "runtime.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 18 | #include "runtime_support.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 19 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 23 | /* desktop Linux needs a little help with gettid() */ |
| 24 | #if !defined(HAVE_ANDROID_OS) |
| 25 | #define __KERNEL__ |
| 26 | # include <linux/unistd.h> |
| 27 | #ifdef _syscall0 |
| 28 | _syscall0(pid_t, gettid) |
| 29 | #else |
| 30 | pid_t gettid() { return syscall(__NR_gettid);} |
| 31 | #endif |
| 32 | #undef __KERNEL__ |
| 33 | #endif |
| 34 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 35 | pthread_key_t Thread::pthread_key_self_; |
| 36 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 37 | // TODO: placeholder. This is what generated code will call to throw |
| 38 | static void ThrowException(Thread* thread, Throwable* exception) { |
| 39 | /* |
| 40 | * exception may be NULL, in which case this routine should |
| 41 | * throw NPE. NOTE: this is a convenience for generated code, |
| 42 | * which previuosly did the null check inline and constructed |
| 43 | * and threw a NPE if NULL. This routine responsible for setting |
| 44 | * exception_ in thread. |
| 45 | */ |
| 46 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw"; |
| 47 | } |
| 48 | |
| 49 | // TODO: placeholder. Helper function to type |
| 50 | static Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 51 | /* |
| 52 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 53 | * Returns initialized type. Does not return normally if an exception |
| 54 | * is thrown, but instead initiates the catch. Should be similar to |
| 55 | * ClassLinker::InitializeStaticStorageFromCode. |
| 56 | */ |
| 57 | UNIMPLEMENTED(FATAL); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 61 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 62 | #if defined(__arm__) |
| 63 | pShlLong = art_shl_long; |
| 64 | pShrLong = art_shr_long; |
| 65 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 66 | pIdiv = __aeabi_idiv; |
| 67 | pIdivmod = __aeabi_idivmod; |
| 68 | pI2f = __aeabi_i2f; |
| 69 | pF2iz = __aeabi_f2iz; |
| 70 | pD2f = __aeabi_d2f; |
| 71 | pF2d = __aeabi_f2d; |
| 72 | pD2iz = __aeabi_d2iz; |
| 73 | pL2f = __aeabi_l2f; |
| 74 | pL2d = __aeabi_l2d; |
| 75 | pFadd = __aeabi_fadd; |
| 76 | pFsub = __aeabi_fsub; |
| 77 | pFdiv = __aeabi_fdiv; |
| 78 | pFmul = __aeabi_fmul; |
| 79 | pFmodf = fmodf; |
| 80 | pDadd = __aeabi_dadd; |
| 81 | pDsub = __aeabi_dsub; |
| 82 | pDdiv = __aeabi_ddiv; |
| 83 | pDmul = __aeabi_dmul; |
| 84 | pFmod = fmod; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 85 | pF2l = F2L; |
| 86 | pD2l = D2L; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 87 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 88 | pLmul = __aeabi_lmul; |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 89 | #endif |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 90 | pAllocFromCode = Array::AllocFromCode; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 91 | pAllocObjectFromCode = Class::AllocObjectFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 92 | pMemcpy = memcpy; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 93 | pHandleFillArrayDataFromCode = HandleFillArrayDataFromCode; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 94 | pGet32Static = Field::Get32StaticFromCode; |
| 95 | pSet32Static = Field::Set32StaticFromCode; |
| 96 | pGet64Static = Field::Get64StaticFromCode; |
| 97 | pSet64Static = Field::Set64StaticFromCode; |
| 98 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 99 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 100 | pCanPutArrayElementFromCode = Class::CanPutArrayElementFromCode; |
| 101 | pThrowException = ThrowException; |
| 102 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 103 | #if 0 |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 104 | bool (Thread::*pUnlockObject)(Thread*, Object*); |
| 105 | int (Thread::*pInstanceofNonTrivialFromCode)(const Class*, const Class*); |
| 106 | Method* (Thread::*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, DvmDex*); |
| 107 | bool (Thread::*pUnlockObjectFromCode)(Thread*, Object*); |
| 108 | void (Thread::*pLockObjectFromCode)(Thread*, Object*); |
| 109 | Object* (Thread::*pAllocObjectFromCode)(Class*, int); |
| 110 | void (Thread::*pThrowException)(Thread*, Object*); |
| 111 | bool (Thread::*pHandleFillArrayDataFromCode)(Array*, const uint16_t*); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 112 | #endif |
| 113 | } |
| 114 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 115 | Mutex* Mutex::Create(const char* name) { |
| 116 | Mutex* mu = new Mutex(name); |
| 117 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 118 | CHECK_EQ(0, result); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 119 | return mu; |
| 120 | } |
| 121 | |
| 122 | void Mutex::Lock() { |
| 123 | int result = pthread_mutex_lock(&lock_impl_); |
| 124 | CHECK_EQ(result, 0); |
| 125 | SetOwner(Thread::Current()); |
| 126 | } |
| 127 | |
| 128 | bool Mutex::TryLock() { |
| 129 | int result = pthread_mutex_lock(&lock_impl_); |
| 130 | if (result == EBUSY) { |
| 131 | return false; |
| 132 | } else { |
| 133 | CHECK_EQ(result, 0); |
| 134 | SetOwner(Thread::Current()); |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void Mutex::Unlock() { |
| 140 | CHECK(GetOwner() == Thread::Current()); |
| 141 | int result = pthread_mutex_unlock(&lock_impl_); |
| 142 | CHECK_EQ(result, 0); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 143 | SetOwner(NULL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 146 | void Frame::Next() { |
| 147 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 148 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 149 | sp_ = reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 152 | uintptr_t Frame::GetPC() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 153 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 154 | GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 155 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 158 | Method* Frame::NextMethod() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 159 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 160 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 161 | return *reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 164 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 165 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 166 | return NULL; |
| 167 | } |
| 168 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 169 | Thread* Thread::Create(const Runtime* runtime) { |
| 170 | size_t stack_size = runtime->GetStackSize(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 171 | |
| 172 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 173 | new_thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 174 | |
| 175 | pthread_attr_t attr; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 176 | errno = pthread_attr_init(&attr); |
| 177 | if (errno != 0) { |
| 178 | PLOG(FATAL) << "pthread_attr_init failed"; |
| 179 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 181 | errno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 182 | if (errno != 0) { |
| 183 | PLOG(FATAL) << "pthread_attr_setdetachstate(PTHREAD_CREATE_DETACHED) failed"; |
| 184 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 185 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 186 | errno = pthread_attr_setstacksize(&attr, stack_size); |
| 187 | if (errno != 0) { |
| 188 | PLOG(FATAL) << "pthread_attr_setstacksize(" << stack_size << ") failed"; |
| 189 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 191 | errno = pthread_create(&new_thread->handle_, &attr, ThreadStart, new_thread); |
| 192 | if (errno != 0) { |
| 193 | PLOG(FATAL) << "pthread_create failed"; |
| 194 | } |
| 195 | |
| 196 | errno = pthread_attr_destroy(&attr); |
| 197 | if (errno != 0) { |
| 198 | PLOG(FATAL) << "pthread_attr_destroy failed"; |
| 199 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 200 | |
| 201 | return new_thread; |
| 202 | } |
| 203 | |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 204 | Thread* Thread::Attach(const Runtime* runtime) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 205 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 206 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 207 | |
| 208 | thread->handle_ = pthread_self(); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 209 | thread->tid_ = gettid(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 210 | |
| 211 | thread->state_ = kRunnable; |
| 212 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 213 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 214 | if (errno != 0) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 215 | PLOG(FATAL) << "pthread_setspecific failed"; |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 218 | thread->jni_env_ = new JNIEnvExt(thread, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 219 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 220 | return thread; |
| 221 | } |
| 222 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 223 | void Thread::Dump(std::ostream& os) const { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 224 | /* |
| 225 | * Get the java.lang.Thread object. This function gets called from |
| 226 | * some weird debug contexts, so it's possible that there's a GC in |
| 227 | * progress on some other thread. To decrease the chances of the |
| 228 | * thread object being moved out from under us, we add the reference |
| 229 | * to the tracked allocation list, which pins it in place. |
| 230 | * |
| 231 | * If threadObj is NULL, the thread is still in the process of being |
| 232 | * attached to the VM, and there's really nothing interesting to |
| 233 | * say about it yet. |
| 234 | */ |
| 235 | os << "TODO: pin Thread before dumping\n"; |
| 236 | #if 0 |
| 237 | if (java_thread_ == NULL) { |
| 238 | LOGI("Can't dump thread %d: threadObj not set", threadId); |
| 239 | return; |
| 240 | } |
| 241 | dvmAddTrackedAlloc(java_thread_, NULL); |
| 242 | #endif |
| 243 | |
| 244 | DumpState(os); |
| 245 | DumpStack(os); |
| 246 | |
| 247 | #if 0 |
| 248 | dvmReleaseTrackedAlloc(java_thread_, NULL); |
| 249 | #endif |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 252 | std::string GetSchedulerGroup(pid_t tid) { |
| 253 | // /proc/<pid>/group looks like this: |
| 254 | // 2:devices:/ |
| 255 | // 1:cpuacct,cpu:/ |
| 256 | // We want the third field from the line whose second field contains the "cpu" token. |
| 257 | std::string cgroup_file; |
| 258 | if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { |
| 259 | return ""; |
| 260 | } |
| 261 | std::vector<std::string> cgroup_lines; |
| 262 | Split(cgroup_file, '\n', cgroup_lines); |
| 263 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 264 | std::vector<std::string> cgroup_fields; |
| 265 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 266 | std::vector<std::string> cgroups; |
| 267 | Split(cgroup_fields[1], ',', cgroups); |
| 268 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 269 | if (cgroups[i] == "cpu") { |
| 270 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | return ""; |
| 275 | } |
| 276 | |
| 277 | void Thread::DumpState(std::ostream& os) const { |
| 278 | std::string thread_name("unknown"); |
| 279 | int priority = -1; |
| 280 | bool is_daemon = false; |
| 281 | #if 0 // TODO |
| 282 | nameStr = (StringObject*) dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_name); |
| 283 | threadName = dvmCreateCstrFromString(nameStr); |
| 284 | priority = dvmGetFieldInt(threadObj, gDvm.offJavaLangThread_priority); |
| 285 | is_daemon = dvmGetFieldBoolean(threadObj, gDvm.offJavaLangThread_daemon); |
| 286 | #else |
| 287 | thread_name = "TODO"; |
| 288 | priority = -1; |
| 289 | is_daemon = false; |
| 290 | #endif |
| 291 | |
| 292 | int policy; |
| 293 | sched_param sp; |
| 294 | errno = pthread_getschedparam(handle_, &policy, &sp); |
| 295 | if (errno != 0) { |
| 296 | PLOG(FATAL) << "pthread_getschedparam failed"; |
| 297 | } |
| 298 | |
| 299 | std::string scheduler_group(GetSchedulerGroup(GetTid())); |
| 300 | if (scheduler_group.empty()) { |
| 301 | scheduler_group = "default"; |
| 302 | } |
| 303 | |
| 304 | std::string group_name("(null; initializing?)"); |
| 305 | #if 0 |
| 306 | groupObj = (Object*) dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_group); |
| 307 | if (groupObj != NULL) { |
| 308 | nameStr = (StringObject*) dvmGetFieldObject(groupObj, gDvm.offJavaLangThreadGroup_name); |
| 309 | groupName = dvmCreateCstrFromString(nameStr); |
| 310 | } |
| 311 | #else |
| 312 | group_name = "TODO"; |
| 313 | #endif |
| 314 | |
| 315 | os << '"' << thread_name << '"'; |
| 316 | if (is_daemon) { |
| 317 | os << " daemon"; |
| 318 | } |
| 319 | os << " prio=" << priority |
| 320 | << " tid=" << GetId() |
| 321 | << " " << state_ << "\n"; |
| 322 | |
| 323 | int suspend_count = 0; // TODO |
| 324 | int debug_suspend_count = 0; // TODO |
| 325 | void* java_thread_ = NULL; // TODO |
| 326 | os << " | group=\"" << group_name << "\"" |
| 327 | << " sCount=" << suspend_count |
| 328 | << " dsCount=" << debug_suspend_count |
| 329 | << " obj=" << reinterpret_cast<void*>(java_thread_) |
| 330 | << " self=" << reinterpret_cast<const void*>(this) << "\n"; |
| 331 | os << " | sysTid=" << GetTid() |
| 332 | << " nice=" << getpriority(PRIO_PROCESS, GetTid()) |
| 333 | << " sched=" << policy << "/" << sp.sched_priority |
| 334 | << " cgrp=" << scheduler_group |
| 335 | << " handle=" << GetImpl() << "\n"; |
| 336 | |
| 337 | // Grab the scheduler stats for this thread. |
| 338 | std::string scheduler_stats; |
| 339 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { |
| 340 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 341 | } else { |
| 342 | scheduler_stats = "0 0 0"; |
| 343 | } |
| 344 | |
| 345 | int utime = 0; |
| 346 | int stime = 0; |
| 347 | int task_cpu = 0; |
| 348 | std::string stats; |
| 349 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 350 | // Skip the command, which may contain spaces. |
| 351 | stats = stats.substr(stats.find(')') + 2); |
| 352 | // Extract the three fields we care about. |
| 353 | std::vector<std::string> fields; |
| 354 | Split(stats, ' ', fields); |
| 355 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 356 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 357 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 358 | } |
| 359 | |
| 360 | os << " | schedstat=( " << scheduler_stats << " )" |
| 361 | << " utm=" << utime |
| 362 | << " stm=" << stime |
| 363 | << " core=" << task_cpu |
| 364 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 365 | } |
| 366 | |
| 367 | void Thread::DumpStack(std::ostream& os) const { |
| 368 | os << "UNIMPLEMENTED: Thread::DumpStack\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 371 | static void ThreadExitCheck(void* arg) { |
| 372 | LG << "Thread exit check"; |
| 373 | } |
| 374 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 375 | bool Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 376 | // Allocate a TLS slot. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 377 | errno = pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck); |
| 378 | if (errno != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 379 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
| 383 | // Double-check the TLS slot allocation. |
| 384 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 385 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // TODO: initialize other locks and condition variables |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 394 | void Thread::Shutdown() { |
| 395 | errno = pthread_key_delete(Thread::pthread_key_self_); |
| 396 | if (errno != 0) { |
| 397 | PLOG(WARNING) << "pthread_key_delete failed"; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | Thread::~Thread() { |
| 402 | delete jni_env_; |
| 403 | } |
| 404 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 405 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 406 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 407 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 408 | count += cur->NumberOfReferences(); |
| 409 | } |
| 410 | return count; |
| 411 | } |
| 412 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 413 | bool Thread::SirtContains(jobject obj) { |
| 414 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 415 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 416 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 417 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 418 | // in a this pointer or a class |
| 419 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 420 | if ((&cur->References()[0] <= sirt_entry) && |
| 421 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 428 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 429 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 430 | if (obj == NULL) { |
| 431 | return NULL; |
| 432 | } |
| 433 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 434 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 435 | Object* result; |
| 436 | switch (kind) { |
| 437 | case kLocal: |
| 438 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 439 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 440 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | case kGlobal: |
| 444 | { |
| 445 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 446 | IndirectReferenceTable& globals = vm->globals; |
| 447 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 448 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | case kWeakGlobal: |
| 452 | { |
| 453 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 454 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 455 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 456 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 457 | if (result == kClearedJniWeakGlobal) { |
| 458 | // This is a special case where it's okay to return NULL. |
| 459 | return NULL; |
| 460 | } |
| 461 | break; |
| 462 | } |
| 463 | case kSirtOrInvalid: |
| 464 | default: |
| 465 | // TODO: make stack indirect reference table lookup more efficient |
| 466 | // Check if this is a local reference in the SIRT |
| 467 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 468 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 469 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 470 | // Assume an invalid local reference is actually a direct pointer. |
| 471 | result = reinterpret_cast<Object*>(obj); |
| 472 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 473 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 478 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 479 | JniAbort(NULL); |
| 480 | } else { |
| 481 | if (result != kInvalidIndirectRefObject) { |
| 482 | Heap::VerifyObject(result); |
| 483 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 484 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 485 | return result; |
| 486 | } |
| 487 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 488 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 489 | public: |
| 490 | CountStackDepthVisitor() : depth(0) {} |
| 491 | virtual bool VisitFrame(const Frame&) { |
| 492 | ++depth; |
| 493 | return true; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 494 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 495 | |
| 496 | int GetDepth() const { |
| 497 | return depth; |
| 498 | } |
| 499 | |
| 500 | private: |
| 501 | uint32_t depth; |
| 502 | }; |
| 503 | |
| 504 | class BuildStackTraceVisitor : public Thread::StackVisitor { |
| 505 | public: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 506 | explicit BuildStackTraceVisitor(int depth) : count(0) { |
| 507 | method_trace = Runtime::Current()->GetClassLinker()->AllocObjectArray<Method>(depth); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 508 | pc_trace = IntArray::Alloc(depth); |
| 509 | } |
| 510 | |
| 511 | virtual ~BuildStackTraceVisitor() {} |
| 512 | |
| 513 | virtual bool VisitFrame(const Frame& frame) { |
| 514 | method_trace->Set(count, frame.GetMethod()); |
| 515 | pc_trace->Set(count, frame.GetPC()); |
| 516 | ++count; |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | const Method* GetMethod(uint32_t i) { |
| 521 | DCHECK(i < count); |
| 522 | return method_trace->Get(i); |
| 523 | } |
| 524 | |
| 525 | uintptr_t GetPC(uint32_t i) { |
| 526 | DCHECK(i < count); |
| 527 | return pc_trace->Get(i); |
| 528 | } |
| 529 | |
| 530 | private: |
| 531 | uint32_t count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 532 | ObjectArray<Method>* method_trace; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 533 | IntArray* pc_trace; |
| 534 | }; |
| 535 | |
| 536 | void Thread::WalkStack(StackVisitor* visitor) { |
| 537 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 538 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 539 | // CHECK(native_to_managed_record_ != NULL); |
| 540 | NativeToManagedRecord* record = native_to_managed_record_; |
| 541 | |
| 542 | while (frame.GetSP()) { |
| 543 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
| 544 | visitor->VisitFrame(frame); |
| 545 | } |
| 546 | if (record == NULL) { |
| 547 | break; |
| 548 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 549 | frame.SetSP(reinterpret_cast<art::Method**>(record->last_top_of_managed_stack)); // last_tos should return Frame instead of sp? |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 550 | record = record->link; |
| 551 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 554 | ObjectArray<StackTraceElement>* Thread::AllocStackTrace() { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 555 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 556 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 557 | CountStackDepthVisitor count_visitor; |
| 558 | WalkStack(&count_visitor); |
| 559 | int32_t depth = count_visitor.GetDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 560 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 561 | BuildStackTraceVisitor build_trace_visitor(depth); |
| 562 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 563 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 564 | ObjectArray<StackTraceElement>* java_traces = class_linker->AllocStackTraceElementArray(depth); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 565 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 566 | for (int32_t i = 0; i < depth; ++i) { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 567 | // Prepare parameter for StackTraceElement(String cls, String method, String file, int line) |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 568 | const Method* method = build_trace_visitor.GetMethod(i); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 569 | const Class* klass = method->GetDeclaringClass(); |
| 570 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 571 | String* readable_descriptor = String::AllocFromModifiedUtf8( |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 572 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 573 | |
| 574 | StackTraceElement* obj = |
| 575 | StackTraceElement::Alloc(readable_descriptor, |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 576 | method->GetName(), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 577 | String::AllocFromModifiedUtf8(klass->GetSourceFile()), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 578 | dex_file.GetLineNumFromPC(method, |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 579 | method->ToDexPC(build_trace_visitor.GetPC(i)))); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 580 | java_traces->Set(i, obj); |
| 581 | } |
| 582 | return java_traces; |
| 583 | } |
| 584 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 585 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 586 | std::string msg; |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 587 | va_list args; |
| 588 | va_start(args, fmt); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 589 | StringAppendV(&msg, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 590 | va_end(args); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 591 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 592 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 593 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 594 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 595 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 596 | descriptor.erase(descriptor.length() - 1); |
| 597 | |
| 598 | JNIEnv* env = GetJniEnv(); |
| 599 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 600 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 601 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 602 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 605 | void Thread::ThrowOutOfMemoryError() { |
| 606 | UNIMPLEMENTED(FATAL); |
| 607 | } |
| 608 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 609 | Frame Thread::FindExceptionHandler(void* throw_pc, void** handler_pc) { |
| 610 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 611 | DCHECK(class_linker != NULL); |
| 612 | |
| 613 | Frame cur_frame = GetTopOfStack(); |
| 614 | for (int unwind_depth = 0; ; unwind_depth++) { |
| 615 | const Method* cur_method = cur_frame.GetMethod(); |
| 616 | DexCache* dex_cache = cur_method->GetDeclaringClass()->GetDexCache(); |
| 617 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 618 | |
| 619 | void* handler_addr = FindExceptionHandlerInMethod(cur_method, |
| 620 | throw_pc, |
| 621 | dex_file, |
| 622 | class_linker); |
| 623 | if (handler_addr) { |
| 624 | *handler_pc = handler_addr; |
| 625 | return cur_frame; |
| 626 | } else { |
| 627 | // Check if we are at the last frame |
| 628 | if (cur_frame.HasNext()) { |
| 629 | cur_frame.Next(); |
| 630 | } else { |
| 631 | // Either at the top of stack or next frame is native. |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | *handler_pc = NULL; |
| 637 | return Frame(); |
| 638 | } |
| 639 | |
| 640 | void* Thread::FindExceptionHandlerInMethod(const Method* method, |
| 641 | void* throw_pc, |
| 642 | const DexFile& dex_file, |
| 643 | ClassLinker* class_linker) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 644 | Throwable* exception_obj = exception_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 645 | exception_ = NULL; |
| 646 | |
| 647 | intptr_t dex_pc = -1; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 648 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 649 | DexFile::CatchHandlerIterator iter; |
| 650 | for (iter = dex_file.dexFindCatchHandler(*code_item, |
| 651 | method->ToDexPC(reinterpret_cast<intptr_t>(throw_pc))); |
| 652 | !iter.HasNext(); |
| 653 | iter.Next()) { |
| 654 | Class* klass = class_linker->FindSystemClass(dex_file.dexStringByTypeIdx(iter.Get().type_idx_)); |
| 655 | DCHECK(klass != NULL); |
| 656 | if (exception_obj->InstanceOf(klass)) { |
| 657 | dex_pc = iter.Get().address_; |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | exception_ = exception_obj; |
| 663 | if (iter.HasNext()) { |
| 664 | return NULL; |
| 665 | } else { |
| 666 | return reinterpret_cast<void*>( method->ToNativePC(dex_pc) ); |
| 667 | } |
| 668 | } |
| 669 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 670 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 671 | //(*visitor)(&thread->threadObj, threadId, ROOT_THREAD_OBJECT, arg); |
| 672 | //(*visitor)(&thread->exception, threadId, ROOT_NATIVE_STACK, arg); |
| 673 | jni_env_->locals.VisitRoots(visitor, arg); |
| 674 | jni_env_->monitors.VisitRoots(visitor, arg); |
| 675 | // visitThreadStack(visitor, thread, arg); |
| 676 | UNIMPLEMENTED(WARNING) << "some per-Thread roots not visited"; |
| 677 | } |
| 678 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 679 | static const char* kStateNames[] = { |
| 680 | "New", |
| 681 | "Runnable", |
| 682 | "Blocked", |
| 683 | "Waiting", |
| 684 | "TimedWaiting", |
| 685 | "Native", |
| 686 | "Terminated", |
| 687 | }; |
| 688 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 689 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 690 | os << kStateNames[state-Thread::kNew]; |
| 691 | } else { |
| 692 | os << "State[" << static_cast<int>(state) << "]"; |
| 693 | } |
| 694 | return os; |
| 695 | } |
| 696 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 697 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 698 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 699 | << ",pthread_t=" << thread.GetImpl() |
| 700 | << ",tid=" << thread.GetTid() |
| 701 | << ",id=" << thread.GetId() |
| 702 | << ",state=" << thread.GetState() << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 703 | return os; |
| 704 | } |
| 705 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 706 | ThreadList* ThreadList::Create() { |
| 707 | return new ThreadList; |
| 708 | } |
| 709 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 710 | ThreadList::ThreadList() { |
| 711 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 712 | } |
| 713 | |
| 714 | ThreadList::~ThreadList() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 715 | if (Contains(Thread::Current())) { |
| 716 | Runtime::Current()->DetachCurrentThread(); |
| 717 | } |
| 718 | |
| 719 | // All threads should have exited and unregistered when we |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 720 | // reach this point. This means that all daemon threads had been |
| 721 | // shutdown cleanly. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 722 | // TODO: dump ThreadList if non-empty. |
| 723 | CHECK_EQ(list_.size(), 0U); |
| 724 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 725 | delete lock_; |
| 726 | lock_ = NULL; |
| 727 | } |
| 728 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 729 | bool ThreadList::Contains(Thread* thread) { |
| 730 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 731 | } |
| 732 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 733 | void ThreadList::Dump(std::ostream& os) { |
| 734 | MutexLock mu(lock_); |
| 735 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
| 736 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 737 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 738 | (*it)->Dump(os); |
| 739 | } |
| 740 | } |
| 741 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 742 | void ThreadList::Register(Thread* thread) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 743 | //LOG(INFO) << "ThreadList::Register() " << *thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 744 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 745 | CHECK(!Contains(thread)); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 746 | list_.push_front(thread); |
| 747 | } |
| 748 | |
| 749 | void ThreadList::Unregister(Thread* thread) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame^] | 750 | //LOG(INFO) << "ThreadList::Unregister() " << *thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 751 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 752 | CHECK(Contains(thread)); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 753 | list_.remove(thread); |
| 754 | } |
| 755 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 756 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 757 | MutexLock mu(lock_); |
| 758 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 759 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 760 | (*it)->VisitRoots(visitor, arg); |
| 761 | } |
| 762 | } |
| 763 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 764 | } // namespace |