OSDN Git Service

am 0cb579b3: am 6ddfa666: am 92e11ea1: Merge "Add NOTICE files"
[android-x86/system-extras.git] / memory_replay / Threads.cpp
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 <string.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24
25 #include <new>
26
27 #include "Action.h"
28 #include "Thread.h"
29 #include "Threads.h"
30
31 void* ThreadRunner(void* data) {
32   Thread* thread = reinterpret_cast<Thread*>(data);
33   while (true) {
34     thread->WaitForPending();
35     Action* action = thread->GetAction();
36     action->Execute(thread->pointers());
37     bool end_thread = action->EndThread();
38     thread->ClearPending();
39     if (end_thread) {
40       break;
41     }
42   }
43   return nullptr;
44 }
45
46 Threads::Threads(Pointers* pointers, size_t max_threads)
47     : pointers_(pointers), max_threads_(max_threads) {
48   size_t pagesize = getpagesize();
49   data_size_ = (max_threads_ * sizeof(Thread) + pagesize - 1) & ~(pagesize - 1);
50   max_threads_ = data_size_ / sizeof(Thread);
51
52   void* memory = mmap(nullptr, data_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
53   if (memory == MAP_FAILED) {
54     err(1, "Failed to map in memory for Threads: map size %zu, max threads %zu\n",
55         data_size_, max_threads_);
56   }
57
58   if (Thread::ACTION_SIZE < Action::MaxActionSize()) {
59     err(1, "Thread action size is too small: ACTION_SIZE %zu, max size %zu\n",
60         Thread::ACTION_SIZE, Action::MaxActionSize());
61   }
62
63   threads_ = new (memory) Thread[max_threads_];
64 }
65
66 Threads::~Threads() {
67   if (threads_) {
68     munmap(threads_, data_size_);
69     threads_ = nullptr;
70     data_size_ = 0;
71   }
72 }
73
74 Thread* Threads::CreateThread(pid_t tid) {
75   if (num_threads_ == max_threads_) {
76     err(1, "Too many threads created, current max %zu.\n", num_threads_);
77   }
78   Thread* thread = FindEmptyEntry(tid);
79   if (thread == nullptr) {
80     err(1, "No empty entries found, current max %zu, num threads %zu\n",
81           max_threads_, num_threads_);
82   }
83   thread->tid_ = tid;
84   thread->pointers_ = pointers_;
85   if (pthread_create(&thread->thread_id_, nullptr, ThreadRunner, thread) == -1) {
86     err(1, "Failed to create thread %d: %s\n", tid, strerror(errno));
87   }
88
89   num_threads_++;
90   return thread;
91 }
92
93 Thread* Threads::FindThread(pid_t tid) {
94   size_t index = GetHashEntry(tid);
95   for (size_t entries = num_threads_; entries != 0; ) {
96     pid_t cur_tid = threads_[index].tid_;
97     if (cur_tid == tid) {
98       return threads_ + index;
99     }
100     if (cur_tid != 0) {
101       entries--;
102     }
103     if (++index == max_threads_) {
104       index = 0;
105     }
106   }
107   return nullptr;
108 }
109
110 void Threads::WaitForAllToQuiesce() {
111   for (size_t i = 0, threads = 0; threads < num_threads_; i++) {
112     pid_t cur_tid = threads_[i].tid_;
113     if (cur_tid != 0) {
114       threads++;
115       threads_[i].WaitForReady();
116     }
117   }
118 }
119
120 size_t Threads::GetHashEntry(pid_t tid) {
121   return tid % max_threads_;
122 }
123
124 Thread* Threads::FindEmptyEntry(pid_t tid) {
125   size_t index = GetHashEntry(tid);
126   for (size_t entries = 0; entries < max_threads_; entries++) {
127     if (threads_[index].tid_ == 0) {
128       return threads_ + index;
129     }
130     if (++index == max_threads_) {
131       index = 0;
132     }
133   }
134   return nullptr;
135 }
136
137 void Threads::Finish(Thread* thread) {
138   pthread_join(thread->thread_id_, nullptr);
139   thread->tid_ = 0;
140   num_threads_--;
141 }
142
143 void Threads::FinishAll() {
144   for (size_t i = 0; i < max_threads_; i++) {
145     if (threads_[i].tid_ != 0) {
146       threads_[i].CreateAction(0, "thread_done", nullptr);
147       threads_[i].SetPending();
148       Finish(threads_ + i);
149     }
150   }
151 }