Преглед изворни кода

Count usual clients same way as vips

That should fix #2806
alexey пре 3 година
родитељ
комит
0c19ad8d1b
4 измењених фајлова са 22 додато и 16 уклоњено
  1. 2 1
      src/client_task_info.cpp
  2. 7 5
      src/client_task_info.h
  3. 4 2
      src/net_action_accept.cpp
  4. 9 8
      src/task_info.h

+ 2 - 1
src/client_task_info.cpp

@@ -11,7 +11,8 @@
 #include "client_task_info.h"
 #include "client_session.h"
 
-int ClientTaskInfo_t::m_iVips = 0;
+std::atomic<int> ClientTaskInfo_t::m_iClients { 0 };
+std::atomic<int> ClientTaskInfo_t::m_iVips { 0 };
 
 DEFINE_RENDER ( ClientTaskInfo_t )
 {

+ 7 - 5
src/client_task_info.h

@@ -32,7 +32,7 @@ private:
 	TaskState_e m_eTaskState = TaskState_e::UNKNOWN;
 	Proto_e m_eProto = Proto_e::UNKNOWN;
 	int m_iConnID = -1;
-	CSphString m_sClientName; // set once before info is published and never changes. So, assume always mt-safeprivate:
+	CSphString m_sClientName; // set once before info is published and never changes. So, assume always mt-safe
 	bool m_bSsl = false;
 	bool m_bVip = false;
 	bool m_bReadOnly = false;
@@ -47,7 +47,8 @@ public:
 	ESphCollation m_eCollation { GlobalCollation () };
 	Profile_e			m_eProfile { Profile_e::NONE };
 	bool m_bPersistent = false;
-	static int m_iVips;
+	static std::atomic<int> m_iClients;
+	static std::atomic<int> m_iVips;
 
 private:
 	ClientSession_c* 	m_pSession = nullptr;
@@ -72,7 +73,8 @@ public:
 
 	void SetVip ( bool bVip ) { m_bVip = bVip; }
 	bool GetVip() const { return m_bVip; }
-	inline static int GetVips() { return m_iVips; }
+	inline static int GetVips() { return m_iVips.load ( std::memory_order_relaxed ); }
+	inline static int GetClients() { return m_iClients.load ( std::memory_order_relaxed ); }
 
 	void SetReadOnly ( bool bReadOnly ) { m_bReadOnly = bReadOnly; }
 	bool GetReadOnly() const { return m_bReadOnly; }
@@ -131,9 +133,9 @@ namespace session {
 	inline void SetSsl ( bool bSsl ) { ClientTaskInfo_t::Info().SetSsl (bSsl); }
 	inline bool GetSsl() { return ClientTaskInfo_t::Info().GetSsl(); }
 
-	inline void SetVip ( bool bVip ) { ClientTaskInfo_t::Info().SetVip (bVip); }
 	inline bool GetVip() { return ClientTaskInfo_t::Info().GetVip(); }
 	inline int GetVips() { return ClientTaskInfo_t::GetVips(); }
+	inline int GetClients() { return ClientTaskInfo_t::GetClients(); }
 
 	inline void SetReadOnly ( bool bReadOnly ) { ClientTaskInfo_t::Info().SetReadOnly (bReadOnly); }
 	inline bool GetReadOnly() { return ClientTaskInfo_t::Info().GetReadOnly(); }
@@ -172,7 +174,7 @@ namespace myinfo {
 	// num of client tasks, not including vips
 	inline int CountClients ()
 	{
-		return Count ( ClientTaskInfo_t::m_eTask ) - session::GetVips();
+		return session::GetClients() - session::GetVips();
 	}
 
 	// num of real tasks (that is mini-info + client-info)

+ 4 - 2
src/net_action_accept.cpp

@@ -123,14 +123,16 @@ public:
 		: ScopedInfo_T<ClientTaskInfo_t> ( pInfo )
 		, m_bVip ( pInfo->GetVip() )
 	{
+		ClientTaskInfo_t::m_iClients.fetch_add ( 1, std::memory_order_relaxed );
 		if ( m_bVip )
-			++ClientTaskInfo_t::m_iVips;
+			ClientTaskInfo_t::m_iVips.fetch_add ( 1, std::memory_order_relaxed );
 	}
 
 	~ScopedClientInfo_c()
 	{
 		if ( m_bVip )
-			--ClientTaskInfo_t::m_iVips;
+			ClientTaskInfo_t::m_iVips.fetch_sub ( 1, std::memory_order_relaxed );
+		ClientTaskInfo_t::m_iClients.fetch_sub ( 1, std::memory_order_relaxed );
 	}
 };
 

+ 9 - 8
src/task_info.h

@@ -122,28 +122,29 @@ struct NoRefCount_t
 // On dtr restores stored chain as root and retire info (uses hazard pointers)
 // if explicit root provided - uses it instead of TLS root.
 template<typename TASKINFO, typename REFCOUNT=RefCount_t>
-class ScopedInfo_T : public hazard::ScopedPtr_t<TASKINFO*>
+class ScopedInfo_T
 {
-	using BASE = hazard::ScopedPtr_t<TASKINFO*>;
+	hazard::ScopedPtr_t<TASKINFO*> m_pInfo;
 
 public:
 	explicit ScopedInfo_T ( TASKINFO* pInfo )
-		: BASE ( pInfo )
+		: m_pInfo ( pInfo )
 	{
-		pInfo->m_pPrev = Threads::MyThd ().m_pTaskInfo.load ( std::memory_order_acquire );
+		pInfo->m_pPrev = Threads::MyThd().m_pTaskInfo.exchange ( pInfo, std::memory_order_acq_rel );
 		REFCOUNT::Inc ( TASKINFO::m_eTask );
-		Threads::MyThd ().m_pTaskInfo.store ( pInfo, std::memory_order_release );
 	}
 
+	operator TASKINFO*() const { return m_pInfo.operator TASKINFO*(); };
+	TASKINFO* operator->() const { return m_pInfo.operator->(); }
+
 	ScopedInfo_T ( ScopedInfo_T && rhs ) noexcept
 	{
-		BASE::Swap ( rhs );
-		REFCOUNT::Inc ( TASKINFO::m_eTask );
+		m_pInfo.Swap ( rhs.m_pInfo );
 	}
 
 	~ScopedInfo_T ()
 	{
-		Threads::MyThd ().m_pTaskInfo.store ( ( BASE::operator TASKINFO * () )->m_pPrev, std::memory_order_release );
+		Threads::MyThd ().m_pTaskInfo.store ( m_pInfo->m_pPrev, std::memory_order_release );
 		REFCOUNT::Dec ( TASKINFO::m_eTask );
 	}
 };