Skip to content

Commit

Permalink
i#6938 sched migrate: Add lock contention stats
Browse files Browse the repository at this point in the history
Adds lock contention counters for mutex_dbg_owned::lock() in
non-NDEBUG builds.  While it's not there for release builds, the
results in non-release should still be indicative of general lock
behavior.

Prints the stats for sched_lock_ with the other scheduler stats.

Sample results on schedule_stats on a threadsig trace show a lot of
contention even with only 3 cores:

```
$ clients/bin64/drmemtrace_launcher -indir ../build_x64_dbg_tests/drmemtrace.threadsig.5* -core_sharded -cores 3 -tool schedule_stats -verbose 1
[scheduler] Schedule lock acquired     :   2196602
[scheduler] Schedule lock contended    :    257580
```

Issue: #6938
  • Loading branch information
derekbruening committed Sep 6, 2024
1 parent 26e6a1d commit ba317fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 30 additions & 1 deletion clients/drcachesim/common/mutex_dbg_owned.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ class mutex_dbg_owned {
void
lock()
{
#ifdef NDEBUG
lock_.lock();
#ifndef NDEBUG
#else
bool contended = true;
if (lock_.try_lock())
contended = false;
else
lock_.lock();
owner_ = std::this_thread::get_id();
++count_acquired_;
if (contended)
++count_contended_;
#endif
}
bool
Expand All @@ -76,6 +85,8 @@ class mutex_dbg_owned {
#endif
lock_.unlock();
}

#ifndef NDEBUG
// This query should only be called when the lock is required to be held
// as it is racy when the lock is not held.
bool
Expand All @@ -84,9 +95,27 @@ class mutex_dbg_owned {
return owner_ == std::this_thread::get_id();
}

// These statistics only count lock(): they do *not* count try_lock()
// (we could count try_lock with std::atomic on count_contended).
int64_t
get_count_acquired()
{
return count_acquired_;
}
int64_t
get_count_contended()
{
return count_contended_;
}
#endif

private:
std::mutex lock_;
#ifndef NDEBUG
std::thread::id owner_;
int64_t count_acquired_ = 0;
int64_t count_contended_ = 0;
#endif
};

} // namespace drmemtrace
Expand Down
4 changes: 4 additions & 0 deletions clients/drcachesim/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,10 @@ scheduler_tmpl_t<RecordType, ReaderType>::~scheduler_tmpl_t()
VPRINT(this, 1, " %-25s: %9" PRId64 "\n", "Migrations",
outputs_[i].stats[memtrace_stream_t::SCHED_STAT_MIGRATIONS]);
}
VPRINT(this, 1, "%-27s: %9" PRId64 "\n", "Schedule lock acquired",
sched_lock_.get_count_acquired());
VPRINT(this, 1, "%-27s: %9" PRId64 "\n", "Schedule lock contended",
sched_lock_.get_count_contended());
}

template <typename RecordType, typename ReaderType>
Expand Down

0 comments on commit ba317fd

Please sign in to comment.