@@ -319,6 +319,10 @@ void LockManager::ProcessQueue(Environment* env) {
319319 * remove later
320320 */
321321 if (if_available_request) {
322+ {
323+ Mutex::ScopedLock scoped_lock (mutex_);
324+ counters.total_aborts ++;
325+ }
322326 Local<Value> null_arg = Null (isolate);
323327 Local<Value> callback_result;
324328 {
@@ -456,7 +460,17 @@ void LockManager::ProcessQueue(Environment* env) {
456460 grantable_request->released_promise ());
457461 {
458462 Mutex::ScopedLock scoped_lock (mutex_);
459- held_locks_[grantable_request->name ()].push_back (granted_lock);
463+ auto & resource_locks = held_locks_[grantable_request->name ()];
464+ resource_locks.push_back (granted_lock);
465+ if (grantable_request->steal ()) {
466+ counters.total_steals ++;
467+ }
468+
469+ if (grantable_request->mode () == Lock::Mode::Exclusive) {
470+ counters.total_exclusive_acquired ++;
471+ } else {
472+ counters.total_shared_acquired ++;
473+ }
460474 }
461475
462476 // Create and store the new granted lock
@@ -715,6 +729,10 @@ void LockManager::ReleaseLockAndProcessQueue(Environment* env,
715729 // stolen.
716730 if (!lock->is_stolen ()) {
717731 if (was_rejected) {
732+ {
733+ Mutex::ScopedLock scoped_lock (mutex_);
734+ counters.total_aborts ++;
735+ }
718736 // Propagate rejection from the user callback
719737 if (lock->released_promise ()
720738 ->Reject (context, callback_result)
@@ -800,6 +818,37 @@ void LockManager::CleanupEnvironment(Environment* env_to_cleanup) {
800818 registered_envs_.erase (env_to_cleanup);
801819}
802820
821+ LockManager::LocksCountersSnapshot LockManager::GetCountersSnapshot () const {
822+ LocksCountersSnapshot snapshot;
823+ Mutex::ScopedLock scoped_lock (mutex_);
824+
825+ snapshot.total_steals = counters.total_steals ;
826+ snapshot.total_aborts = counters.total_aborts ;
827+ snapshot.total_exclusive_acquired = counters.total_exclusive_acquired ;
828+ snapshot.total_shared_acquired = counters.total_shared_acquired ;
829+
830+ for (const auto & pending_request : pending_queue_) {
831+ if (pending_request->mode () == Lock::Mode::Exclusive) {
832+ snapshot.pending_exclusive ++;
833+ } else {
834+ snapshot.pending_shared ++;
835+ }
836+ }
837+
838+ for (const auto & resource : held_locks_) {
839+ const auto & locks = resource.second ;
840+ for (const auto & lock : locks) {
841+ if (lock->mode () == Lock::Mode::Exclusive) {
842+ snapshot.holders_exclusive ++;
843+ } else {
844+ snapshot.holders_shared ++;
845+ }
846+ }
847+ }
848+
849+ return snapshot;
850+ }
851+
803852// Cleanup hook wrapper
804853void LockManager::OnEnvironmentCleanup (void * arg) {
805854 Environment* env = static_cast <Environment*>(arg);
0 commit comments