Results 1 to 5 of 5

Thread: Why concurrent access to FreezeMap is so slow?

  1. #1
    kingbo is offline Registered User
    Name: Tang Fan
    Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server
    Join Date
    Jul 2005
    Posts
    16

    Why concurrent access to FreezeMap is so slow?

    When I test the concurrent access to FreezeMap,I found it is much slower than single-thread method.My code like following:

    Code:
    ObjectAdapterPtr	g_pAdapter;
    vector<string> g_GetNamesFromMap(long beginTime,long endTime)
    {
    	vector<string> back;
    	Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
    	IHisDataMap hisDataMap(pConn, "map");
    	
    	IHisDataMap::const_iterator pEnd = hisDataMap.upperBoundForTime(endTime);
    	IHisDataMap::const_iterator pos;
    	for(pos = hisDataMap.lowerBoundForTime(beginTime);pos!=pEnd;++pos)
    	{
    		back.push_back(pos->first);
    	}
    	return back;
    }
    vector<string> g_SetNamesToMap(string name,long time)
    {
    	Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
    	IHisDataMap hisDataMap(pConn, "map");
    
    	IHisDataMap::iterator pos = hisDataMap.find(name);	
    	if(pos != hisDataMap.end())
    	{
    		pos.set(time);
    	}else
    	{
    		hisDataMap.insert(make_pair(name, time));	
    	}
    	
    }
    
    
    void JobThread::run()
    {
    	m_bRunning = true;
    	IceUtil::Time sleepTime = IceUtil::Time::milliSeconds(1000);
    	while(true)
    	{
    		if(!m_bRunning)
    		{
    			break;
    		}else
    		{
    			vector<string> names = g_GetNamesFromMap(0,ULONG_MAX);
    		}
    		ThreadControl::sleep(sleepTime);
    	}
    }
    
    void main()
    {
    	//Init communicator and g_pAdapter
    	.............................
    	
    	int iThreadNum = 30;
    	for(int i=0;i<iThreadNum;++i)
    	{
    		JobThread pThread = new JobThread();
    		pThread->start();
    	}
    }
    1. When iThreadNum = 1, the g_GetNamesFromMap will use 600ms to finish per time. When iThreadNum = 30, it will takes 33s to finish. Is there anything wrong?
    2. When some threads are invoking g_GetNamesFromMap, I will call g_SetNamesToMap to update the map. In this case, do I have to serialize the two function or just call g_SetNamesToMap directly?
    Tang Fan
    ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Quote Originally Posted by kingbo
    1. When iThreadNum = 1, the g_GetNamesFromMap will use 600ms to finish per time. When iThreadNum = 30, it will takes 33s to finish. Is there anything wrong?
    Your code is not very clear to me: each thread is an infinite loop that gets the names and sleeps for one full second? Which time are you measuring?

    In g_GetNamesFromMap, you can avoid creating transactions by using a const map:
    Code:
    const IHisDataMap hisDataMap(pConn, "map");
    This could improve performance a little.


    Quote Originally Posted by kingbo
    2. When some threads are invoking g_GetNamesFromMap, I will call g_SetNamesToMap to update the map. In this case, do I have to serialize the two function or just call g_SetNamesToMap directly?
    You don't need to serialize, since you're using separate connections/maps in each function. However, you need to deal with deadlocks. See the Ice manual for details.

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  3. #3
    kingbo is offline Registered User
    Name: Tang Fan
    Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server
    Join Date
    Jul 2005
    Posts
    16
    vow! So quick response in weekend. Thank you a lot!

    Your code is not very clear to me: each thread is an infinite loop that gets the names and sleeps for one full second? Which time are you measuring?
    In fact,the code is:

    Code:
    void JobThread::run()
    {
    	m_bRunning = true;
    	IceUtil::Time sleepTime = IceUtil::Time::milliSeconds(1000);
    	while(true)
    	{
    		if(!m_bRunning)
    		{
    			break;
    		}else
    		{
    			long beginPos = GetTickCount();
    			vector<string> names = g_GetNamesFromMap(0,ULONG_MAX);
    			long endPos = GetTickCount();
    			printf("g_GetNamesFromMap takes %d\n",endPos-beginPos);
    		}
    		ThreadControl::sleep(sleepTime);
    	}
    }
    I just tested your suggestion of "const IHisDataMap hisDataMap(pConn, "map");" and found the performence is improved a little but not satisfied. It still takes 20s to 30s per time.
    Tang Fan
    ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server

  4. #4
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Another improvement would be to keep a Map opened in your main thread:

    Code:
    Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
    	IHisDataMap hisDataMap(pConn, "map");
    This way, you would avoid opening/closing the map all the time.

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  5. #5
    kingbo is offline Registered User
    Name: Tang Fan
    Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server
    Join Date
    Jul 2005
    Posts
    16
    There is huge distance between 33s and 600ms. I guess I must miss some important configuration in DB_CONFIG. My DB_CONFIG is:
    set_cachesize 0 1073741824 1
    set_lk_max_lockers 40000
    set_lk_max_locks 40000
    set_lk_max_objects 40000
    set_tx_max 20000
    set_lg_bsize 2097152
    set_lg_max 2097152
    set_flags DB_TXN_NOSYNC
    set_flags DB_TXN_WRITE_NOSYNC
    Tang Fan
    ShenZhen Yijin Platform Network Technology Co.,Ltd
    Project: Stock quotation server

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 09-08-2009, 06:22 AM
  2. Replies: 2
    Last Post: 07-24-2007, 10:51 AM
  3. FreezeMap lazy initialize
    By xdm in forum Help Center
    Replies: 3
    Last Post: 03-01-2006, 10:07 PM
  4. FreezeMap Use Clarification
    By Ken Carpenter in forum Comments
    Replies: 11
    Last Post: 03-01-2003, 05:42 AM
  5. FreezeMap Database Directory
    By Ken Carpenter in forum Comments
    Replies: 1
    Last Post: 02-28-2003, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •