From 009e2e4e0a1c1bbcfcc1acb4b18f035dee7b31fb Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Tue, 6 Apr 2010 13:49:51 -0400 Subject: [PATCH] adaptive sleep for ClientCusror::yield SERVER-975 --- db/client.cpp | 17 +++++++++++++++++ db/client.h | 2 ++ db/clientcursor.cpp | 1 + util/goodies.h | 4 ++++ 4 files changed, 24 insertions(+) diff --git a/db/client.cpp b/db/client.cpp index d8114c2c291..dbfcbd02aae 100644 --- a/db/client.cpp +++ b/db/client.cpp @@ -275,4 +275,21 @@ namespace mongo { } handshakeCmd; + + int Client::recommendedYieldMicros(){ + int num = 0; + { + scoped_lock bl(clientsMutex); + num = clients.size(); + } + + if ( --num <= 0 ) // -- is for myself + return 0; + + if ( num > 50 ) + num = 50; + + num *= 100; + return num; + } } diff --git a/db/client.h b/db/client.h index 022c530fd12..96dbdc085c6 100644 --- a/db/client.h +++ b/db/client.h @@ -45,6 +45,8 @@ namespace mongo { static mongo::mutex clientsMutex; static set clients; // always be in clientsMutex when manipulating this + static int recommendedYieldMicros(); + class GodScope { bool _prev; public: diff --git a/db/clientcursor.cpp b/db/clientcursor.cpp index 3a1c7391640..5b40cf29a14 100644 --- a/db/clientcursor.cpp +++ b/db/clientcursor.cpp @@ -233,6 +233,7 @@ namespace mongo { { dbtempreleasecond unlock; + sleepmicros( Client::recommendedYieldMicros() ); } if ( ClientCursor::find( id , false ) == 0 ){ diff --git a/util/goodies.h b/util/goodies.h index f6a1f4c55d2..5b6c834480d 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -191,6 +191,8 @@ namespace mongo { boost::thread::sleep(xt); } inline void sleepmicros(int s) { + if ( s <= 0 ) + return; boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += ( s / 1000000 ); @@ -211,6 +213,8 @@ namespace mongo { } } inline void sleepmicros(int s) { + if ( s <= 0 ) + return; struct timespec t; t.tv_sec = (int)(s / 1000000); t.tv_nsec = 1000 * ( s % 1000000 );