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