xref: /aosp_15_r20/system/extras/memory_replay/Threads.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 <memory_trace/MemoryTrace.h>
30 
31 #include "Alloc.h"
32 #include "Pointers.h"
33 #include "Thread.h"
34 #include "Threads.h"
35 
ThreadRunner(void * data)36 void* ThreadRunner(void* data) {
37   Thread* thread = reinterpret_cast<Thread*>(data);
38   while (true) {
39     thread->WaitForPending();
40     const memory_trace::Entry& entry = thread->GetEntry();
41     thread->AddTimeNsecs(AllocExecute(entry, thread->pointers()));
42     bool thread_done = entry.type == memory_trace::THREAD_DONE;
43     thread->ClearPending();
44     if (thread_done) {
45       break;
46     }
47   }
48   return nullptr;
49 }
50 
Threads(Pointers * pointers,size_t max_threads)51 Threads::Threads(Pointers* pointers, size_t max_threads)
52     : pointers_(pointers), max_threads_(max_threads) {
53   size_t pagesize = getpagesize();
54   data_size_ = (max_threads_ * sizeof(Thread) + pagesize - 1) & ~(pagesize - 1);
55   max_threads_ = data_size_ / sizeof(Thread);
56 
57   void* memory = mmap(nullptr, data_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
58   if (memory == MAP_FAILED) {
59     err(1, "Failed to map in memory for Threads: map size %zu, max threads %zu", data_size_,
60         max_threads_);
61   }
62 
63   threads_ = new (memory) Thread[max_threads_];
64 }
65 
~Threads()66 Threads::~Threads() {
67   if (threads_) {
68     munmap(threads_, data_size_);
69     threads_ = nullptr;
70     data_size_ = 0;
71   }
72 }
73 
CreateThread(pid_t tid)74 Thread* Threads::CreateThread(pid_t tid) {
75   if (num_threads_ == max_threads_) {
76     errx(1, "Too many threads created, current max %zu", num_threads_);
77   }
78   Thread* thread = FindEmptyEntry(tid);
79   if (thread == nullptr) {
80     errx(1, "No empty entries found, current max %zu, num threads %zu", max_threads_, num_threads_);
81   }
82   thread->tid_ = tid;
83   thread->pointers_ = pointers_;
84   thread->total_time_nsecs_ = 0;
85   if ((errno = pthread_create(&thread->thread_id_, nullptr, ThreadRunner, thread)) != 0) {
86     err(1, "Failed to create thread %d", tid);
87   }
88 
89   num_threads_++;
90   return thread;
91 }
92 
FindThread(pid_t tid)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 
WaitForAllToQuiesce()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 
GetHashEntry(pid_t tid)120 size_t Threads::GetHashEntry(pid_t tid) {
121   return tid % max_threads_;
122 }
123 
FindEmptyEntry(pid_t tid)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 
Finish(Thread * thread)137 void Threads::Finish(Thread* thread) {
138   int ret = pthread_join(thread->thread_id_, nullptr);
139   if (ret != 0) {
140     err(1, "pthread_join failed");
141   }
142   total_time_nsecs_ += thread->total_time_nsecs_;
143   thread->tid_ = 0;
144   num_threads_--;
145 }
146 
FinishAll()147 void Threads::FinishAll() {
148   memory_trace::Entry thread_done = {.type = memory_trace::THREAD_DONE};
149   for (size_t i = 0; i < max_threads_; i++) {
150     if (threads_[i].tid_ != 0) {
151       threads_[i].SetEntry(&thread_done);
152       threads_[i].SetPending();
153       Finish(threads_ + i);
154     }
155   }
156 }
157