APIRET CDECL KalSleep(va_list a) { ULONG ms; ms = va_arg(a, ULONG); return kalSleep(ms); }
/***************************************************************************** 函 数 名 : thread_func 功能描述 : 线程函数 *****************************************************************************/ void* thread_func(void *arg) { int iMyID = get_thread_id(); int iDestIP = inet_addr(g_Config.m_szDestIp); struct TUsrResult stUsrResult; CUserFunc* pUserFunc = NULL; srand(iMyID); int iStartWaitus = rand()%(g_Config.m_iSampleSecs*1000000); usleep(iStartWaitus); try { pUserFunc = new CUserFunc(iMyID,iDestIP,g_Config.m_iDestPort,CFGFILE, &m_mutex_thread_user); }catch(...) { printf("ERR:thread %d can not run!\n",iMyID); return NULL; } timeval tBegin,tEnd; while(!isTimeEnd) { //执行 memset(&stUsrResult,0,sizeof(stUsrResult)); gettimeofday(&tBegin,NULL); int iRet = pUserFunc->DoOnce(&stUsrResult); gettimeofday(&tEnd,NULL); unsigned long long ullRspTimeUs = (tEnd.tv_sec - tBegin.tv_sec)*1000000 + (tEnd.tv_usec - tBegin.tv_usec); //if(stUsrResult.iOkResponseNum == 0) // ullRspTimeUs = 0; if(iRet != 0) { sleep(1); } //上报到总结果集合,完成是打印 Results_History[iMyID].iOkResponseNum += stUsrResult.iOkResponseNum; Results_History[iMyID].iAllReqNum+= stUsrResult.iAllReqNum; Results_History[iMyID].iBadResponseNum += stUsrResult.iBadResponseNum; Results_History[iMyID].iNoResponseNum+= stUsrResult.iNoResponseNum; Results_History[iMyID].ullRspTimeUs+= ullRspTimeUs; if(ullRspTimeUs > Results_History[iMyID].ullMaxRspTimeUs) Results_History[iMyID].ullMaxRspTimeUs = ullRspTimeUs ; //上报单位时间结果,周期性打印 pthread_mutex_lock(&Results_NowLock[iMyID]); Results_Now[iMyID].iOkResponseNum += stUsrResult.iOkResponseNum; Results_Now[iMyID].iAllReqNum+= stUsrResult.iAllReqNum; Results_Now[iMyID].iBadResponseNum += stUsrResult.iBadResponseNum; Results_Now[iMyID].iNoResponseNum+= stUsrResult.iNoResponseNum; Results_Now[iMyID].ullRspTimeUs+= ullRspTimeUs; if(ullRspTimeUs > Results_Now[iMyID].ullMaxRspTimeUs) Results_Now[iMyID].ullMaxRspTimeUs = ullRspTimeUs ; if(ullRspTimeUs >= (unsigned int)g_Config.m_iTimeLevel3) { Results_Now[iMyID].m_iTimeL3Num++; } if((ullRspTimeUs < (unsigned int)g_Config.m_iTimeLevel3) && (ullRspTimeUs >= (unsigned int)g_Config.m_iTimeLevel2)) { Results_Now[iMyID].m_iTimeL2Num++; } if((ullRspTimeUs < (unsigned int)g_Config.m_iTimeLevel2) && (ullRspTimeUs >= (unsigned int)g_Config.m_iTimeLevel1)) { Results_Now[iMyID].m_iTimeL1Num++; } pthread_mutex_unlock(&Results_NowLock[iMyID]); if (isTimeEnd) return NULL; kalSleep(g_Config.m_iThreadSleepMs); } delete pUserFunc; return NULL; }