1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <err.h> 18 #include <errno.h> 19 #include <pthread.h> 20 #include <stdint.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <sys/mman.h> 25 #include <unistd.h> 26 27 #include <new> 28 29 #include "Alloc.h" 30 #include "Pointers.h" 31 #include "Thread.h" 32 #include "Threads.h" 33 34 void* ThreadRunner(void* data) { 35 Thread* thread = reinterpret_cast<Thread*>(data); 36 while (true) { 37 thread->WaitForPending(); 38 const AllocEntry& entry = thread->GetAllocEntry(); 39 thread->AddTimeNsecs(AllocExecute(entry, thread->pointers())); 40 bool thread_done = entry.type == THREAD_DONE; 41 thread->ClearPending(); 42 if (thread_done) { 43 break; 44 } 45 } 46 return nullptr; 47 } 48 49 Threads::Threads(Pointers* pointers, size_t max_threads) 50 : pointers_(pointers), max_threads_(max_threads) { 51 size_t pagesize = getpagesize(); 52 data_size_ = (max_threads_ * sizeof(Thread) + pagesize - 1) & ~(pagesize - 1); 53 max_threads_ = data_size_ / sizeof(Thread); 54 55 void* memory = mmap(nullptr, data_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 56 if (memory == MAP_FAILED) { 57 err(1, "Failed to map in memory for Threads: map size %zu, max threads %zu\n", 58 data_size_, max_threads_); 59 } 60 61 threads_ = new (memory) Thread[max_threads_]; 62 } 63 64 Threads::~Threads() { 65 if (threads_) { 66 munmap(threads_, data_size_); 67 threads_ = nullptr; 68 data_size_ = 0; 69 } 70 } 71 72 Thread* Threads::CreateThread(pid_t tid) { 73 if (num_threads_ == max_threads_) { 74 err(1, "Too many threads created, current max %zu.\n", num_threads_); 75 } 76 Thread* thread = FindEmptyEntry(tid); 77 if (thread == nullptr) { 78 err(1, "No empty entries found, current max %zu, num threads %zu\n", 79 max_threads_, num_threads_); 80 } 81 thread->tid_ = tid; 82 thread->pointers_ = pointers_; 83 thread->total_time_nsecs_ = 0; 84 if ((errno = pthread_create(&thread->thread_id_, nullptr, ThreadRunner, thread)) != 0) { 85 err(1, "Failed to create thread %d: %s\n", tid, strerror(errno)); 86 } 87 88 num_threads_++; 89 return thread; 90 } 91 92 Thread* Threads::FindThread(pid_t tid) { 93 size_t index = GetHashEntry(tid); 94 for (size_t entries = num_threads_; entries != 0; ) { 95 pid_t cur_tid = threads_[index].tid_; 96 if (cur_tid == tid) { 97 return threads_ + index; 98 } 99 if (cur_tid != 0) { 100 entries--; 101 } 102 if (++index == max_threads_) { 103 index = 0; 104 } 105 } 106 return nullptr; 107 } 108 109 void Threads::WaitForAllToQuiesce() { 110 for (size_t i = 0, threads = 0; threads < num_threads_; i++) { 111 pid_t cur_tid = threads_[i].tid_; 112 if (cur_tid != 0) { 113 threads++; 114 threads_[i].WaitForReady(); 115 } 116 } 117 } 118 119 size_t Threads::GetHashEntry(pid_t tid) { 120 return tid % max_threads_; 121 } 122 123 Thread* Threads::FindEmptyEntry(pid_t tid) { 124 size_t index = GetHashEntry(tid); 125 for (size_t entries = 0; entries < max_threads_; entries++) { 126 if (threads_[index].tid_ == 0) { 127 return threads_ + index; 128 } 129 if (++index == max_threads_) { 130 index = 0; 131 } 132 } 133 return nullptr; 134 } 135 136 void Threads::Finish(Thread* thread) { 137 int ret = pthread_join(thread->thread_id_, nullptr); 138 if (ret != 0) { 139 fprintf(stderr, "pthread_join failed: %s\n", strerror(ret)); 140 exit(1); 141 } 142 total_time_nsecs_ += thread->total_time_nsecs_; 143 thread->tid_ = 0; 144 num_threads_--; 145 } 146 147 void Threads::FinishAll() { 148 AllocEntry thread_done = {.type = THREAD_DONE}; 149 for (size_t i = 0; i < max_threads_; i++) { 150 if (threads_[i].tid_ != 0) { 151 threads_[i].SetAllocEntry(&thread_done); 152 threads_[i].SetPending(); 153 Finish(threads_ + i); 154 } 155 } 156 } 157