OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/debug/activity_tracker.h" |
| 6 |
| 7 #include "base/debug/stack_trace.h" |
| 8 #include "base/feature_list.h" |
| 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/pending_task.h" |
| 17 #include "base/process/process.h" |
| 18 #include "base/process/process_handle.h" |
| 19 #include "base/stl_util.h" |
| 20 #include "base/strings/string_util.h" |
| 21 #include "base/threading/platform_thread.h" |
| 22 |
| 23 namespace base { |
| 24 namespace debug { |
| 25 |
| 26 namespace { |
| 27 |
| 28 // A number that identifies the memory as having been initialized. It's |
| 29 // arbitrary but happens to be the first 8 bytes of SHA1(ThreadActivityTracker). |
| 30 // A version number is added on so that major structure changes won't try to |
| 31 // read an older version (since the cookie won't match). |
| 32 const uint64_t kHeaderCookie = 0xC0029B240D4A3092ULL + 1; // v1 |
| 33 |
| 34 // The minimum depth a stack should support. |
| 35 const int kMinStackDepth = 2; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 |
| 40 #if !defined(OS_NACL) // NACL doesn't support any kind of file access in build. |
| 41 void SetupGlobalActivityTrackerFieldTrial(const FilePath& file) { |
| 42 const Feature kActivityTrackerFeature{ |
| 43 "ActivityTracking", FEATURE_DISABLED_BY_DEFAULT |
| 44 }; |
| 45 |
| 46 if (!base::FeatureList::IsEnabled(kActivityTrackerFeature)) |
| 47 return; |
| 48 |
| 49 // TODO(bcwhite): Adjust these numbers once there is real data to show |
| 50 // just how much of an arena is necessary. |
| 51 const size_t kMemorySize = 1 << 20; // 1 MiB |
| 52 const int kStackDepth = 4; |
| 53 const uint64_t kAllocatorId = 0; |
| 54 const char kAllocatorName[] = "ActivityTracker"; |
| 55 |
| 56 GlobalActivityTracker::CreateWithFile( |
| 57 file.AddExtension(PersistentMemoryAllocator::kFileExtension), |
| 58 kMemorySize, kAllocatorId, kAllocatorName, kStackDepth); |
| 59 } |
| 60 #endif // !defined(OS_NACL) |
| 61 |
| 62 |
| 63 // This information is kept for every thread that is tracked. It is filled |
| 64 // the very first time the thread is seen. All fields must be of exact sizes |
| 65 // so there is no issue moving between 32 and 64-bit builds. |
| 66 struct ThreadActivityTracker::Header { |
| 67 // This unique number indicates a valid initialization of the memory. |
| 68 uint64_t cookie; |
| 69 |
| 70 // The process-id and thread-id to which this data belongs. These identifiers |
| 71 // are not guaranteed to mean anything but are unique, in combination, among |
| 72 // all active trackers. It would be nice to always have the process_id be a |
| 73 // 64-bit value but the necessity of having it atomic (for the memory barriers |
| 74 // it provides) limits it to the natural word size of the machine. |
| 75 #ifdef ARCH_CPU_64_BITS |
| 76 std::atomic<int64_t> process_id; |
| 77 #else |
| 78 std::atomic<int32_t> process_id; |
| 79 int32_t process_id_padding; |
| 80 #endif |
| 81 |
| 82 union { |
| 83 int64_t as_id; |
| 84 #if defined(OS_WIN) |
| 85 // On Windows, the handle itself is often a pseudo-handle with a common |
| 86 // value meaning "this thread" and so the thread-id is used. The former |
| 87 // can be converted to a thread-id with a system call. |
| 88 PlatformThreadId as_tid; |
| 89 #elif defined(OS_POSIX) |
| 90 // On Posix, the handle is always a unique identifier so no conversion |
| 91 // needs to be done. However, it's value is officially opaque so there |
| 92 // is no one correct way to convert it to a numerical identifier. |
| 93 PlatformThreadHandle::Handle as_handle; |
| 94 #endif |
| 95 } thread_ref; |
| 96 |
| 97 // The start-time and start-ticks when the data was created. Each activity |
| 98 // record has a |time_internal| value that can be converted to a "wall time" |
| 99 // with these two values. |
| 100 int64_t start_time; |
| 101 int64_t start_ticks; |
| 102 |
| 103 // The number of Activity slots in the data. |
| 104 uint32_t stack_slots; |
| 105 |
| 106 // The current depth of the stack. This may be greater than the number of |
| 107 // slots. If the depth exceeds the number of slots, the newest entries |
| 108 // won't be recorded. |
| 109 std::atomic<uint32_t> current_depth; |
| 110 |
| 111 // A memory location used to indicate if changes have been made to the stack |
| 112 // that would invalidate an in-progress read of its contents. The active |
| 113 // tracker will zero the value whenever something gets popped from the |
| 114 // stack. A monitoring tracker can write a non-zero value here, copy the |
| 115 // stack contents, and read the value to know, if it is still non-zero, that |
| 116 // the contents didn't change while being copied. This can handle concurrent |
| 117 // snapshot operations only if each snapshot writes a different bit (which |
| 118 // is not the current implementation so no parallel snapshots allowed). |
| 119 std::atomic<uint32_t> stack_unchanged; |
| 120 |
| 121 // The name of the thread (up to a maximum length). Dynamic-length names |
| 122 // are not practical since the memory has to come from the same persistent |
| 123 // allocator that holds this structure and to which this object has no |
| 124 // reference. |
| 125 char thread_name[32]; |
| 126 }; |
| 127 |
| 128 // It doesn't matter what is contained in this (though it will be all zeros) |
| 129 // as only the address of it is important. |
| 130 const ThreadActivityTracker::ActivityData |
| 131 ThreadActivityTracker::kNullActivityData = {}; |
| 132 |
| 133 ThreadActivityTracker::ActivityData |
| 134 ThreadActivityTracker::ActivityData::ForThread( |
| 135 const PlatformThreadHandle& handle) { |
| 136 // Header already has a conversion union; reuse that. |
| 137 ThreadActivityTracker::Header header; |
| 138 header.thread_ref.as_id = 0; // Zero the union in case other is smaller. |
| 139 #if defined(OS_WIN) |
| 140 header.thread_ref.as_tid = ::GetThreadId(handle.platform_handle()); |
| 141 #elif defined(OS_POSIX) |
| 142 header.thread_ref.as_handle = handle.platform_handle(); |
| 143 #endif |
| 144 return ForThread(header.thread_ref.as_id); |
| 145 } |
| 146 |
| 147 ThreadActivityTracker::ActivitySnapshot::ActivitySnapshot() {} |
| 148 ThreadActivityTracker::ActivitySnapshot::~ActivitySnapshot() {} |
| 149 |
| 150 |
| 151 ThreadActivityTracker::ThreadActivityTracker(void* base, size_t size) |
| 152 : header_(static_cast<Header*>(base)), |
| 153 stack_(reinterpret_cast<Activity*>(reinterpret_cast<char*>(base) + |
| 154 sizeof(Header))), |
| 155 stack_slots_( |
| 156 static_cast<uint32_t>((size - sizeof(Header)) / sizeof(Activity))) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 |
| 159 // Verify the parameters but fail gracefully if they're not valid so that |
| 160 // production code based on external inputs will not crash. IsValid() will |
| 161 // return false in this case. |
| 162 if (!base || |
| 163 // Ensure there is enough space for the header and at least a few records. |
| 164 size < sizeof(Header) + kMinStackDepth * sizeof(Activity) || |
| 165 // Ensure that the |stack_slots_| calculation didn't overflow. |
| 166 (size - sizeof(Header)) / sizeof(Activity) > |
| 167 std::numeric_limits<uint32_t>::max()) { |
| 168 NOTREACHED(); |
| 169 return; |
| 170 } |
| 171 |
| 172 // Ensure that the thread reference doesn't exceed the size of the ID number. |
| 173 // This won't compile at the global scope because Header is a private struct. |
| 174 static_assert( |
| 175 sizeof(header_->thread_ref) == sizeof(header_->thread_ref.as_id), |
| 176 "PlatformThreadHandle::Handle is too big to hold in 64-bit ID"); |
| 177 |
| 178 // Ensure that the alignment of Activity.data is properly aligned to a |
| 179 // 64-bit boundary so there are no interoperability-issues across cpu |
| 180 // architectures. |
| 181 static_assert(offsetof(Activity, data) % sizeof(uint64_t) == 0, |
| 182 "ActivityData.data is not 64-bit aligned"); |
| 183 |
| 184 // Provided memory should either be completely initialized or all zeros. |
| 185 if (header_->cookie == 0) { |
| 186 // This is a new file. Double-check other fields and then initialize. |
| 187 DCHECK_EQ(0, header_->process_id.load(std::memory_order_relaxed)); |
| 188 DCHECK_EQ(0, header_->thread_ref.as_id); |
| 189 DCHECK_EQ(0, header_->start_time); |
| 190 DCHECK_EQ(0, header_->start_ticks); |
| 191 DCHECK_EQ(0U, header_->stack_slots); |
| 192 DCHECK_EQ(0U, header_->current_depth.load(std::memory_order_relaxed)); |
| 193 DCHECK_EQ(0U, header_->stack_unchanged.load(std::memory_order_relaxed)); |
| 194 DCHECK_EQ(0, stack_[0].time_internal); |
| 195 DCHECK_EQ(0U, stack_[0].origin_address); |
| 196 DCHECK_EQ(0U, stack_[0].call_stack[0]); |
| 197 DCHECK_EQ(0U, stack_[0].data.task.sequence_id); |
| 198 |
| 199 #if defined(OS_WIN) |
| 200 header_->thread_ref.as_tid = PlatformThread::CurrentId(); |
| 201 #elif defined(OS_POSIX) |
| 202 header_->thread_ref.as_handle = |
| 203 PlatformThread::CurrentHandle().platform_handle(); |
| 204 #endif |
| 205 header_->start_time = base::Time::Now().ToInternalValue(); |
| 206 header_->start_ticks = base::TimeTicks::Now().ToInternalValue(); |
| 207 header_->stack_slots = stack_slots_; |
| 208 strlcpy(header_->thread_name, PlatformThread::GetName(), |
| 209 sizeof(header_->thread_name)); |
| 210 header_->cookie = kHeaderCookie; |
| 211 |
| 212 // This is done last so as to guarantee that everything above is "released" |
| 213 // by the time this value gets written. |
| 214 header_->process_id.store(GetCurrentProcId(), std::memory_order_release); |
| 215 |
| 216 valid_ = true; |
| 217 DCHECK(IsValid()); |
| 218 } else { |
| 219 // This is a file with existing data. Perform basic consistency checks. |
| 220 valid_ = true; |
| 221 valid_ = IsValid(); |
| 222 } |
| 223 } |
| 224 |
| 225 ThreadActivityTracker::~ThreadActivityTracker() {} |
| 226 |
| 227 void ThreadActivityTracker::PushActivity(const void* origin, |
| 228 ActivityType type, |
| 229 const ActivityData& data) { |
| 230 // A thread-checker creates a lock to check the thread-id which means |
| 231 // re-entry into this code if lock acquisitions are being tracked. |
| 232 DCHECK(type == ACT_LOCK_ACQUIRE || thread_checker_.CalledOnValidThread()); |
| 233 |
| 234 // Get the current depth of the stack. No access to other memory guarded |
| 235 // by this variable is done here so a "relaxed" load is acceptable. |
| 236 uint32_t depth = header_->current_depth.load(std::memory_order_relaxed); |
| 237 |
| 238 // Handle the case where the stack depth has exceeded the storage capacity. |
| 239 // Extra entries will be lost leaving only the base of the stack. |
| 240 if (depth >= stack_slots_) { |
| 241 // Since no other threads modify the data, no compare/exchange is needed. |
| 242 // Since no other memory is being modified, a "relaxed" store is acceptable. |
| 243 header_->current_depth.store(depth + 1, std::memory_order_relaxed); |
| 244 return; |
| 245 } |
| 246 |
| 247 // Get a pointer to the next activity and load it. No atomicity is required |
| 248 // here because the memory is known only to this thread. It will be made |
| 249 // known to other threads once the depth is incremented. |
| 250 Activity* activity = &stack_[depth]; |
| 251 activity->time_internal = base::TimeTicks::Now().ToInternalValue(); |
| 252 activity->origin_address = reinterpret_cast<uintptr_t>(origin); |
| 253 activity->activity_type = type; |
| 254 activity->data = data; |
| 255 |
| 256 #if defined(SYZYASAN) |
| 257 // Create a stacktrace from the current location and get the addresses. |
| 258 StackTrace stack_trace; |
| 259 size_t stack_depth; |
| 260 const void* const* stack_addrs = stack_trace.Addresses(&stack_depth); |
| 261 // Copy the stack addresses, ignoring the first one (here). |
| 262 size_t i; |
| 263 for (i = 1; i < stack_depth && i < kActivityCallStackSize; ++i) { |
| 264 activity->call_stack[i - 1] = reinterpret_cast<uintptr_t>(stack_addrs[i]); |
| 265 } |
| 266 activity->call_stack[i - 1] = 0; |
| 267 #else |
| 268 // Since the memory was initially zero and nothing ever overwrites it in |
| 269 // this "else" case, there is no need to write even the null terminator. |
| 270 //activity->call_stack[0] = 0; |
| 271 #endif |
| 272 |
| 273 // Save the incremented depth. Because this guards |activity| memory filled |
| 274 // above that may be read by another thread once the recorded depth changes, |
| 275 // a "release" store is required. |
| 276 header_->current_depth.store(depth + 1, std::memory_order_release); |
| 277 } |
| 278 |
| 279 void ThreadActivityTracker::ChangeActivity(ActivityType type, |
| 280 const ActivityData& data) { |
| 281 DCHECK(thread_checker_.CalledOnValidThread()); |
| 282 DCHECK(type != ACT_NULL || &data != &kNullActivityData); |
| 283 |
| 284 // Get the current depth of the stack and acquire the data held there. |
| 285 uint32_t depth = header_->current_depth.load(std::memory_order_acquire); |
| 286 DCHECK_LT(0U, depth); |
| 287 |
| 288 // Update the information if it is being recorded (i.e. within slot limit). |
| 289 if (depth <= stack_slots_) { |
| 290 Activity* activity = &stack_[depth - 1]; |
| 291 |
| 292 if (type != ACT_NULL) { |
| 293 DCHECK_EQ(activity->activity_type & ACT_CATEGORY_MASK, |
| 294 type & ACT_CATEGORY_MASK); |
| 295 activity->activity_type = type; |
| 296 } |
| 297 |
| 298 if (&data != &kNullActivityData) |
| 299 activity->data = data; |
| 300 } |
| 301 } |
| 302 |
| 303 void ThreadActivityTracker::PopActivity() { |
| 304 // Do an atomic decrement of the depth. No changes to stack entries guarded |
| 305 // by this variable are done here so a "relaxed" operation is acceptable. |
| 306 // |depth| will receive the value BEFORE it was modified. |
| 307 uint32_t depth = |
| 308 header_->current_depth.fetch_sub(1, std::memory_order_relaxed); |
| 309 |
| 310 // Validate that everything is running correctly. |
| 311 DCHECK_LT(0U, depth); |
| 312 |
| 313 // A thread-checker creates a lock to check the thread-id which means |
| 314 // re-entry into this code if lock acquisitions are being tracked. |
| 315 DCHECK(stack_[depth - 1].activity_type == ACT_LOCK_ACQUIRE || |
| 316 thread_checker_.CalledOnValidThread()); |
| 317 |
| 318 // The stack has shrunk meaning that some other thread trying to copy the |
| 319 // contents for reporting purposes could get bad data. That thread would |
| 320 // have written a non-zero value into |stack_unchanged|; clearing it here |
| 321 // will let that thread detect that something did change. This needs to |
| 322 // happen after the atomic |depth| operation above so a "release" store |
| 323 // is required. |
| 324 header_->stack_unchanged.store(0, std::memory_order_release); |
| 325 } |
| 326 |
| 327 bool ThreadActivityTracker::IsValid() const { |
| 328 if (header_->cookie != kHeaderCookie || |
| 329 header_->process_id.load(std::memory_order_relaxed) == 0 || |
| 330 header_->thread_ref.as_id == 0 || |
| 331 header_->start_time == 0 || |
| 332 header_->start_ticks == 0 || |
| 333 header_->stack_slots != stack_slots_ || |
| 334 header_->thread_name[sizeof(header_->thread_name) - 1] != '\0') { |
| 335 return false; |
| 336 } |
| 337 |
| 338 return valid_; |
| 339 } |
| 340 |
| 341 bool ThreadActivityTracker::Snapshot(ActivitySnapshot* output_snapshot) const { |
| 342 DCHECK(output_snapshot); |
| 343 |
| 344 // There is no "called on valid thread" check for this method as it can be |
| 345 // called from other threads or even other processes. It is also the reason |
| 346 // why atomic operations must be used in certain places above. |
| 347 |
| 348 // It's possible for the data to change while reading it in such a way that it |
| 349 // invalidates the read. Make several attempts but don't try forever. |
| 350 const int kMaxAttempts = 10; |
| 351 uint32_t depth; |
| 352 |
| 353 // Stop here if the data isn't valid. |
| 354 if (!IsValid()) |
| 355 return false; |
| 356 |
| 357 // Allocate the maximum size for the stack so it doesn't have to be done |
| 358 // during the time-sensitive snapshot operation. It is shrunk once the |
| 359 // actual size is known. |
| 360 output_snapshot->activity_stack.reserve(stack_slots_); |
| 361 |
| 362 for (int attempt = 0; attempt < kMaxAttempts; ++attempt) { |
| 363 // Remember the process and thread IDs to ensure they aren't replaced |
| 364 // during the snapshot operation. Use "acquire" to ensure that all the |
| 365 // non-atomic fields of the structure are valid (at least at the current |
| 366 // moment in time). |
| 367 const int64_t starting_process_id = |
| 368 header_->process_id.load(std::memory_order_acquire); |
| 369 const int64_t starting_thread_id = header_->thread_ref.as_id; |
| 370 |
| 371 // Write a non-zero value to |stack_unchanged| so it's possible to detect |
| 372 // at the end that nothing has changed since copying the data began. A |
| 373 // "cst" operation is required to ensure it occurs before everything else. |
| 374 // Using "cst" memory ordering is relatively expensive but this is only |
| 375 // done during analysis so doesn't directly affect the worker threads. |
| 376 header_->stack_unchanged.store(1, std::memory_order_seq_cst); |
| 377 |
| 378 // Fetching the current depth also "acquires" the contents of the stack. |
| 379 depth = header_->current_depth.load(std::memory_order_acquire); |
| 380 uint32_t count = std::min(depth, stack_slots_); |
| 381 output_snapshot->activity_stack.resize(count); |
| 382 if (count > 0) { |
| 383 // Copy the existing contents. Memcpy is used for speed. |
| 384 memcpy(&output_snapshot->activity_stack[0], stack_, |
| 385 count * sizeof(Activity)); |
| 386 } |
| 387 |
| 388 // Retry if something changed during the copy. A "cst" operation ensures |
| 389 // it must happen after all the above operations. |
| 390 if (!header_->stack_unchanged.load(std::memory_order_seq_cst)) |
| 391 continue; |
| 392 |
| 393 // Stack copied. Record it's full depth. |
| 394 output_snapshot->activity_stack_depth = depth; |
| 395 |
| 396 // TODO(bcwhite): Snapshot other things here. |
| 397 |
| 398 // Get the general thread information. Loading of "process_id" is guaranteed |
| 399 // to be last so that it's possible to detect below if any content has |
| 400 // changed while reading it. |
| 401 output_snapshot->thread_id = header_->thread_ref.as_id; |
| 402 output_snapshot->thread_name = |
| 403 std::string(header_->thread_name, sizeof(header_->thread_name) - 1); |
| 404 output_snapshot->process_id = |
| 405 header_->process_id.load(std::memory_order_seq_cst); |
| 406 |
| 407 // All characters of the thread-name buffer were copied so as to not break |
| 408 // if the trailing NUL were missing. Now limit the length if the actual |
| 409 // name is shorter. |
| 410 output_snapshot->thread_name.resize( |
| 411 strlen(output_snapshot->thread_name.c_str())); |
| 412 |
| 413 // If the process or thread ID has changed then the tracker has exited and |
| 414 // the memory reused by a new one. Try again. |
| 415 if (output_snapshot->process_id != starting_process_id || |
| 416 output_snapshot->thread_id != starting_thread_id) { |
| 417 continue; |
| 418 } |
| 419 |
| 420 // Only successful if the data is still valid once everything is done since |
| 421 // it's possible for the thread to end somewhere in the middle and all its |
| 422 // values become garbage. |
| 423 if (!IsValid()) |
| 424 return false; |
| 425 |
| 426 // Change all the timestamps in the activities from "ticks" to "wall" time. |
| 427 const Time start_time = Time::FromInternalValue(header_->start_time); |
| 428 const int64_t start_ticks = header_->start_ticks; |
| 429 for (Activity& activity : output_snapshot->activity_stack) { |
| 430 activity.time_internal = |
| 431 (start_time + |
| 432 TimeDelta::FromInternalValue(activity.time_internal - start_ticks)) |
| 433 .ToInternalValue(); |
| 434 } |
| 435 |
| 436 // Success! |
| 437 return true; |
| 438 } |
| 439 |
| 440 // Too many attempts. |
| 441 return false; |
| 442 } |
| 443 |
| 444 // static |
| 445 size_t ThreadActivityTracker::SizeForStackDepth(int stack_depth) { |
| 446 return static_cast<size_t>(stack_depth) * sizeof(Activity) + sizeof(Header); |
| 447 } |
| 448 |
| 449 |
| 450 GlobalActivityTracker* GlobalActivityTracker::g_tracker_ = nullptr; |
| 451 |
| 452 GlobalActivityTracker::ManagedActivityTracker::ManagedActivityTracker( |
| 453 PersistentMemoryAllocator::Reference mem_reference, |
| 454 void* base, |
| 455 size_t size) |
| 456 : ThreadActivityTracker(base, size), |
| 457 mem_reference_(mem_reference), |
| 458 mem_base_(base) {} |
| 459 |
| 460 GlobalActivityTracker::ManagedActivityTracker::~ManagedActivityTracker() { |
| 461 // The global |g_tracker_| must point to the owner of this class since all |
| 462 // objects of this type must be destructed before |g_tracker_| can be changed |
| 463 // (something that only occurs in tests). |
| 464 DCHECK(g_tracker_); |
| 465 g_tracker_->ReturnTrackerMemory(this); |
| 466 } |
| 467 |
| 468 void GlobalActivityTracker::CreateWithAllocator( |
| 469 std::unique_ptr<PersistentMemoryAllocator> allocator, |
| 470 int stack_depth) { |
| 471 // There's no need to do anything with the result. It is self-managing. |
| 472 GlobalActivityTracker* global_tracker = |
| 473 new GlobalActivityTracker(std::move(allocator), stack_depth); |
| 474 // Create a tracker for this thread since it is known. |
| 475 global_tracker->CreateTrackerForCurrentThread(); |
| 476 } |
| 477 |
| 478 #if !defined(OS_NACL) |
| 479 // static |
| 480 void GlobalActivityTracker::CreateWithFile(const FilePath& file_path, |
| 481 size_t size, |
| 482 uint64_t id, |
| 483 StringPiece name, |
| 484 int stack_depth) { |
| 485 DCHECK(!file_path.empty()); |
| 486 DCHECK_GE(static_cast<uint64_t>(std::numeric_limits<int64_t>::max()), size); |
| 487 |
| 488 // Create and map the file into memory and make it globally available. |
| 489 std::unique_ptr<MemoryMappedFile> mapped_file(new MemoryMappedFile()); |
| 490 bool success = |
| 491 mapped_file->Initialize(File(file_path, |
| 492 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | |
| 493 File::FLAG_WRITE | File::FLAG_SHARE_DELETE), |
| 494 {0, static_cast<int64_t>(size)}, |
| 495 MemoryMappedFile::READ_WRITE_EXTEND); |
| 496 DCHECK(success); |
| 497 CreateWithAllocator(WrapUnique(new FilePersistentMemoryAllocator( |
| 498 std::move(mapped_file), size, id, name, false)), |
| 499 stack_depth); |
| 500 } |
| 501 #endif // !defined(OS_NACL) |
| 502 |
| 503 // static |
| 504 void GlobalActivityTracker::CreateWithLocalMemory(size_t size, |
| 505 uint64_t id, |
| 506 StringPiece name, |
| 507 int stack_depth) { |
| 508 CreateWithAllocator( |
| 509 WrapUnique(new LocalPersistentMemoryAllocator(size, id, name)), |
| 510 stack_depth); |
| 511 } |
| 512 |
| 513 ThreadActivityTracker* GlobalActivityTracker::CreateTrackerForCurrentThread() { |
| 514 DCHECK(!this_thread_tracker_.Get()); |
| 515 |
| 516 PersistentMemoryAllocator::Reference mem_reference = 0; |
| 517 void* mem_base = nullptr; |
| 518 |
| 519 // Get the current count of available memories, acquiring the array values. |
| 520 int count = available_memories_count_.load(std::memory_order_acquire); |
| 521 while (count > 0) { |
| 522 // There is a memory block that was previously released (and zeroed) so |
| 523 // just re-use that rather than allocating a new one. Use "relaxed" because |
| 524 // the value is guarded by the |count| "acquire". A zero reference replaces |
| 525 // the existing value so that it can't be used by another thread that |
| 526 // manages to interrupt this one before the count can be decremented. |
| 527 // A zero reference is also required for the "push" operation to work |
| 528 // once the count finally does get decremented. |
| 529 mem_reference = |
| 530 available_memories_[count - 1].exchange(0, std::memory_order_relaxed); |
| 531 |
| 532 // If the reference is zero, it's already been taken but count hasn't yet |
| 533 // been decremented. Give that other thread a chance to finish then reload |
| 534 // the "count" value and try again. |
| 535 if (!mem_reference) { |
| 536 PlatformThread::YieldCurrentThread(); |
| 537 count = available_memories_count_.load(std::memory_order_acquire); |
| 538 continue; |
| 539 } |
| 540 |
| 541 // Decrement the count indicating that the value has been taken. If this |
| 542 // fails then another thread has pushed something new and incremented the |
| 543 // count. |
| 544 // NOTE: |oldcount| will be loaded with the existing value. |
| 545 int oldcount = count; |
| 546 if (!available_memories_count_.compare_exchange_strong( |
| 547 oldcount, count - 1, std::memory_order_acquire, |
| 548 std::memory_order_acquire)) { |
| 549 DCHECK_LT(count, oldcount); |
| 550 |
| 551 // Restore the reference that was zeroed above and try again. |
| 552 available_memories_[count - 1].store(mem_reference, |
| 553 std::memory_order_relaxed); |
| 554 count = oldcount; |
| 555 continue; |
| 556 } |
| 557 |
| 558 // Turn the reference back into one of the activity-tracker type. |
| 559 mem_base = allocator_->GetAsObject<char>(mem_reference, |
| 560 kTypeIdActivityTrackerFree); |
| 561 DCHECK(mem_base); |
| 562 DCHECK_LE(stack_memory_size_, allocator_->GetAllocSize(mem_reference)); |
| 563 bool changed = allocator_->ChangeType(mem_reference, kTypeIdActivityTracker, |
| 564 kTypeIdActivityTrackerFree); |
| 565 DCHECK(changed); |
| 566 |
| 567 // Success. |
| 568 break; |
| 569 } |
| 570 |
| 571 // Handle the case where no previously-used memories are available. |
| 572 if (count == 0) { |
| 573 // Allocate a block of memory from the persistent segment. |
| 574 mem_reference = |
| 575 allocator_->Allocate(stack_memory_size_, kTypeIdActivityTracker); |
| 576 if (mem_reference) { |
| 577 // Success. Convert the reference to an actual memory address. |
| 578 mem_base = |
| 579 allocator_->GetAsObject<char>(mem_reference, kTypeIdActivityTracker); |
| 580 // Make the allocation iterable so it can be found by other processes. |
| 581 allocator_->MakeIterable(mem_reference); |
| 582 } else { |
| 583 // Failure. This shouldn't happen. |
| 584 NOTREACHED(); |
| 585 // But if it does, probably because the allocator wasn't given enough |
| 586 // memory to satisfy all possible requests, handle it gracefully by |
| 587 // allocating the required memory from the heap. |
| 588 mem_base = new char[stack_memory_size_]; |
| 589 memset(mem_base, 0, stack_memory_size_); |
| 590 // Report the thread-count at which the allocator was full so that the |
| 591 // failure can be seen and underlying memory resized appropriately. |
| 592 UMA_HISTOGRAM_COUNTS_1000( |
| 593 "UMA.ActivityTracker.ThreadTrackers.MemLimitTrackerCount", |
| 594 thread_tracker_count_.load(std::memory_order_relaxed)); |
| 595 } |
| 596 } |
| 597 |
| 598 // Create a tracker with the acquired memory and set it as the tracker |
| 599 // for this particular thread in thread-local-storage. |
| 600 DCHECK(mem_base); |
| 601 ManagedActivityTracker* tracker = |
| 602 new ManagedActivityTracker(mem_reference, mem_base, stack_memory_size_); |
| 603 DCHECK(tracker->IsValid()); |
| 604 this_thread_tracker_.Set(tracker); |
| 605 int old_count = thread_tracker_count_.fetch_add(1, std::memory_order_relaxed); |
| 606 |
| 607 UMA_HISTOGRAM_ENUMERATION("UMA.ActivityTracker.ThreadTrackers.Count", |
| 608 old_count + 1, kMaxThreadCount); |
| 609 return tracker; |
| 610 } |
| 611 |
| 612 void GlobalActivityTracker::ReleaseTrackerForCurrentThreadForTesting() { |
| 613 ThreadActivityTracker* tracker = |
| 614 reinterpret_cast<ThreadActivityTracker*>(this_thread_tracker_.Get()); |
| 615 if (tracker) { |
| 616 this_thread_tracker_.Free(); |
| 617 delete tracker; |
| 618 } |
| 619 } |
| 620 |
| 621 GlobalActivityTracker::GlobalActivityTracker( |
| 622 std::unique_ptr<PersistentMemoryAllocator> allocator, |
| 623 int stack_depth) |
| 624 : allocator_(std::move(allocator)), |
| 625 stack_memory_size_(ThreadActivityTracker::SizeForStackDepth(stack_depth)), |
| 626 this_thread_tracker_(&OnTLSDestroy), |
| 627 thread_tracker_count_(0), |
| 628 available_memories_count_(0) { |
| 629 // Clear the available-memories array. |
| 630 memset(available_memories_, 0, sizeof(available_memories_)); |
| 631 |
| 632 // Ensure the passed memory is valid and empty (iterator finds nothing). |
| 633 uint32_t type; |
| 634 DCHECK(!PersistentMemoryAllocator::Iterator(allocator_.get()).GetNext(&type)); |
| 635 |
| 636 // Ensure that there is no other global object and then make this one such. |
| 637 DCHECK(!g_tracker_); |
| 638 g_tracker_ = this; |
| 639 } |
| 640 |
| 641 GlobalActivityTracker::~GlobalActivityTracker() { |
| 642 DCHECK_EQ(g_tracker_, this); |
| 643 DCHECK_EQ(0, thread_tracker_count_.load(std::memory_order_relaxed)); |
| 644 g_tracker_ = nullptr; |
| 645 } |
| 646 |
| 647 void GlobalActivityTracker::ReturnTrackerMemory( |
| 648 ManagedActivityTracker* tracker) { |
| 649 PersistentMemoryAllocator::Reference mem_reference = tracker->mem_reference_; |
| 650 void* mem_base = tracker->mem_base_; |
| 651 |
| 652 // Zero the memory so that it is ready for use if needed again later. It's |
| 653 // better to clear the memory now, when a thread is exiting, than to do it |
| 654 // when it is first needed by a thread doing actual work. |
| 655 memset(mem_base, 0, stack_memory_size_); |
| 656 |
| 657 // Remove the destructed tracker from the set of known ones. |
| 658 DCHECK_LE(1, thread_tracker_count_.load(std::memory_order_relaxed)); |
| 659 thread_tracker_count_.fetch_sub(1, std::memory_order_relaxed); |
| 660 |
| 661 // Deal with the memory that was used by the tracker. |
| 662 if (mem_reference) { |
| 663 // The memory was within the persistent memory allocator. Change its type |
| 664 // so that iteration won't find it. |
| 665 allocator_->ChangeType(mem_reference, kTypeIdActivityTrackerFree, |
| 666 kTypeIdActivityTracker); |
| 667 // There is no way to free memory from a persistent allocator so instead |
| 668 // push it on the internal list of available memory blocks. |
| 669 while (true) { |
| 670 // Get the existing count of available memories and ensure we won't |
| 671 // burst the array. Acquire the values in the array. |
| 672 int count = available_memories_count_.load(std::memory_order_acquire); |
| 673 if (count >= kMaxThreadCount) { |
| 674 NOTREACHED(); |
| 675 // Storage is full. Just forget about this memory. It won't be re-used |
| 676 // but there's no real loss. |
| 677 break; |
| 678 } |
| 679 |
| 680 // Write the reference of the memory being returned to this slot in the |
| 681 // array. Empty slots have a value of zero so do an atomic compare-and- |
| 682 // exchange to ensure that a race condition doesn't exist with another |
| 683 // thread doing the same. |
| 684 PersistentMemoryAllocator::Reference mem_expected = 0; |
| 685 if (!available_memories_[count].compare_exchange_strong( |
| 686 mem_expected, mem_reference, std::memory_order_release, |
| 687 std::memory_order_relaxed)) { |
| 688 PlatformThread::YieldCurrentThread(); |
| 689 continue; // Try again. |
| 690 } |
| 691 |
| 692 // Increment the count, releasing the value written to the array. This |
| 693 // could fail if a simultaneous "pop" operation decremented the counter. |
| 694 // If that happens, clear the array slot and start over. Do a "strong" |
| 695 // exchange to avoid spurious retries that can occur with a "weak" one. |
| 696 int expected = count; // Updated by compare/exchange. |
| 697 if (!available_memories_count_.compare_exchange_strong( |
| 698 expected, count + 1, std::memory_order_release, |
| 699 std::memory_order_relaxed)) { |
| 700 available_memories_[count].store(0, std::memory_order_relaxed); |
| 701 continue; |
| 702 } |
| 703 |
| 704 // Count was successfully incremented to reflect the newly added value. |
| 705 break; |
| 706 } |
| 707 } else { |
| 708 // The memory was allocated from the process heap. This shouldn't happen |
| 709 // because the persistent memory segment should be big enough for all |
| 710 // thread stacks but it's better to support falling back to allocation |
| 711 // from the heap rather than crash. Everything will work as normal but |
| 712 // the data won't be persisted. |
| 713 delete[] reinterpret_cast<char*>(mem_base); |
| 714 } |
| 715 } |
| 716 |
| 717 // static |
| 718 void GlobalActivityTracker::OnTLSDestroy(void* value) { |
| 719 delete reinterpret_cast<ManagedActivityTracker*>(value); |
| 720 } |
| 721 |
| 722 |
| 723 ScopedActivity::ScopedActivity(const tracked_objects::Location& location, |
| 724 uint8_t action, |
| 725 uint32_t id, |
| 726 int32_t info) |
| 727 : GlobalActivityTracker::ScopedThreadActivity( |
| 728 location.program_counter(), |
| 729 static_cast<ThreadActivityTracker::ActivityType>( |
| 730 ThreadActivityTracker::ACT_GENERIC | action), |
| 731 ThreadActivityTracker::ActivityData::ForGeneric(id, info), |
| 732 /*lock_allowed=*/true), |
| 733 id_(id) { |
| 734 // The action must not affect the category bits of the activity type. |
| 735 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK); |
| 736 } |
| 737 |
| 738 void ScopedActivity::ChangeAction(uint8_t action) { |
| 739 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK); |
| 740 ChangeTypeAndData(static_cast<ThreadActivityTracker::ActivityType>( |
| 741 ThreadActivityTracker::ACT_GENERIC | action), |
| 742 ThreadActivityTracker::kNullActivityData); |
| 743 } |
| 744 |
| 745 void ScopedActivity::ChangeInfo(int32_t info) { |
| 746 ChangeTypeAndData(ThreadActivityTracker::ACT_NULL, |
| 747 ThreadActivityTracker::ActivityData::ForGeneric(id_, info)); |
| 748 } |
| 749 |
| 750 void ScopedActivity::ChangeActionAndInfo(uint8_t action, int32_t info) { |
| 751 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK); |
| 752 ChangeTypeAndData(static_cast<ThreadActivityTracker::ActivityType>( |
| 753 ThreadActivityTracker::ACT_GENERIC | action), |
| 754 ThreadActivityTracker::ActivityData::ForGeneric(id_, info)); |
| 755 } |
| 756 |
| 757 ScopedTaskRunActivity::ScopedTaskRunActivity(const base::PendingTask& task) |
| 758 : GlobalActivityTracker::ScopedThreadActivity( |
| 759 task.posted_from.program_counter(), |
| 760 ThreadActivityTracker::ACT_TASK_RUN, |
| 761 ThreadActivityTracker::ActivityData::ForTask(task.sequence_num), |
| 762 /*lock_allowed=*/true) {} |
| 763 |
| 764 ScopedLockAcquireActivity::ScopedLockAcquireActivity( |
| 765 const base::internal::LockImpl* lock) |
| 766 : GlobalActivityTracker::ScopedThreadActivity( |
| 767 nullptr, |
| 768 ThreadActivityTracker::ACT_LOCK_ACQUIRE, |
| 769 ThreadActivityTracker::ActivityData::ForLock(lock), |
| 770 /*lock_allowed=*/false) {} |
| 771 |
| 772 ScopedEventWaitActivity::ScopedEventWaitActivity( |
| 773 const base::WaitableEvent* event) |
| 774 : GlobalActivityTracker::ScopedThreadActivity( |
| 775 nullptr, |
| 776 ThreadActivityTracker::ACT_EVENT_WAIT, |
| 777 ThreadActivityTracker::ActivityData::ForEvent(event), |
| 778 /*lock_allowed=*/true) {} |
| 779 |
| 780 ScopedThreadJoinActivity::ScopedThreadJoinActivity( |
| 781 const base::PlatformThreadHandle* thread) |
| 782 : GlobalActivityTracker::ScopedThreadActivity( |
| 783 nullptr, |
| 784 ThreadActivityTracker::ACT_THREAD_JOIN, |
| 785 ThreadActivityTracker::ActivityData::ForThread(*thread), |
| 786 /*lock_allowed=*/true) {} |
| 787 |
| 788 #if !defined(OS_NACL) && !defined(OS_IOS) |
| 789 ScopedProcessWaitActivity::ScopedProcessWaitActivity( |
| 790 const base::Process* process) |
| 791 : GlobalActivityTracker::ScopedThreadActivity( |
| 792 nullptr, |
| 793 ThreadActivityTracker::ACT_PROCESS_WAIT, |
| 794 ThreadActivityTracker::ActivityData::ForProcess(process->Pid()), |
| 795 /*lock_allowed=*/true) {} |
| 796 #endif |
| 797 |
| 798 } // namespace debug |
| 799 } // namespace base |
OLD | NEW |