anjaliraman 0 Newbie Poster

Hi All,
The CPU Utilization in my program is too high when I am trying to read a file. Can you please suggest a better method. Please find code snippet that I am using.

void SyslogReader::isTrapRaisedFromTestClient(list<SNMPTrapInfo>* pListSNMPTrapInfo, const int nSemID)
{
	int nWaitCount = 0;
	const int SYSLOG_MAX_MSG_LEN = 2048;
	char cBuffer[SYSLOG_MAX_MSG_LEN];
	ios::pos_type nPosMsg_1 = 0;
	ios::pos_type nPosMsg_2 = 0;
	struct stat fileAttrib;
			
	PRINTLN(DEBUGGING, "--Start verifying traps for the active client " << g_bClientActive);
	try
	{
		nPosMsg_1 = nPosMsg_2 = m_streamSyslog.tellg(); 
		while( g_bClientActive && !g_bExit )
		{
			if( stat(m_strSyslogPath.c_str(),&fileAttrib) == 0 )
			{
				if( m_nInodeSyslog != fileAttrib.st_ino )
				{
					m_nInodeSyslog = fileAttrib.st_ino;
					openNewSyslog(nPosMsg_1, nPosMsg_2, pListSNMPTrapInfo, nSemID);
				}
			}
			memset(cBuffer,0,SYSLOG_MAX_MSG_LEN);
			m_streamSyslog.seekg(nPosMsg_2, ios::beg);
			if( !readMsgSyslog(cBuffer, SYSLOG_MAX_MSG_LEN) )
			{
				if( nWaitCount > 10 )
				{
					m_streamSyslog.seekg(nPosMsg_1, ios::beg);
					readMsgSyslog(cBuffer, SYSLOG_MAX_MSG_LEN);
					validateTrap(cBuffer, pListSNMPTrapInfo, nSemID);
					nPosMsg_1 = nPosMsg_2 = m_streamSyslog.tellg();
					nWaitCount = 0;
				}
				else if(nPosMsg_1 != nPosMsg_2)
				{
					nWaitCount++;
				}
				continue;
			}
			else
			{
				if( nPosMsg_2 == nPosMsg_1 )
				{
					nPosMsg_2 = m_streamSyslog.tellg();
					continue;
				}
				else if( nPosMsg_2 < m_streamSyslog.tellg() )
				{
					ios::pos_type nPosMsgTmp = m_streamSyslog.tellg();
					m_streamSyslog.seekg(nPosMsg_1, ios::beg);
					readMsgSyslog(cBuffer, SYSLOG_MAX_MSG_LEN);
					validateTrap(cBuffer, pListSNMPTrapInfo, nSemID);
					nPosMsg_1 = nPosMsg_2;
					nPosMsg_2 = nPosMsgTmp;
				}
			}
		}
	}
	catch(...)
	{
		g_bClientActive = false;
		g_bExit = true;
		PRINTLN(MAJOR, "Exception occured during validation of traps");
	}
	PRINTLN(DEBUGGING, "--Completed verifying traps for the active client " << g_bClientActive);
}

inline bool SyslogReader::readMsgSyslog(char* pBuffer,
										const int& nMaxMsgLen)
{
	if( !m_streamSyslog.getline(pBuffer, nMaxMsgLen) )
	{
		if( m_streamSyslog.eof() )
		{
			m_streamSyslog.clear();
			usleep(100000);
		}
		return false;
	}
	return true;
}

void SyslogReader::validateTrap(const string& strMsg, 
								list<SNMPTrapInfo>* pListSNMPTrapInfo, 
								const int nSemID)
{
	string strLocalHost("");
	string::size_type nTempIndex = 0;
	string::size_type nPosition;
	string strSpecificPrb("");
	string strOID("");
	if( string::npos != (nTempIndex = nPosition = strMsg.find("snmpmdserver:",0)) )
	{
		PRINTLN(MAJOR, "!!!" << strMsg);
		if( string::npos != (nPosition = strMsg.find("SP", nPosition)) )
		{
			strSpecificPrb = strMsg.substr(nPosition+3, 5);
			if( string::npos != (nPosition = strMsg.find("IINFO", nPosition)) )
			{
				nTempIndex = strMsg.find("\\",nPosition+8);
				strLocalHost = strMsg.substr(nPosition+8,nTempIndex-(nPosition+8));
			}
			semLock(nSemID);
			list<SNMPTrapInfo>::iterator it = pListSNMPTrapInfo->begin();
			while( it != pListSNMPTrapInfo->end() )
			{
				if( ((*it).strSpecificPrb.compare(strSpecificPrb) == 0)   &&
					((*it).strAddInfo.compare(strLocalHost) == 0) )
				{
					PRINTLN(MAJOR,"*** Removed from q :" << (*it).nCounter 
														 << " :Q Size:" << pListSNMPTrapInfo->size());
					pListSNMPTrapInfo->erase(it);
					m_nAlarmCount++;
					(*m_pAlarmCntCol)++;
					(*m_pCummCntCol)++;
					break;
				}
				it++;
			}
			semUnlock(nSemID);
		}
		else if( string::npos != (nPosition = strMsg.find("trap-oid:", nTempIndex)) )
		{
			nTempIndex = strMsg.find(";", nPosition);
			strOID = strMsg.substr(nPosition+9,nTempIndex-(nPosition+9));
			semLock(nSemID);
			list<SNMPTrapInfo>::iterator it = pListSNMPTrapInfo->begin();
			while( it != pListSNMPTrapInfo->end() )
			{
				if( ((*it).strOID.compare(strOID)) == 0 ) 
				{
					PRINTLN(MAJOR,"*** Removed from q :" << (*it).nCounter 
														 << " :Q Size:" << pListSNMPTrapInfo->size());
					pListSNMPTrapInfo->erase(it);
					m_nInfoCount++;
					(*m_pInfoCntCol)++;
					(*m_pCummCntCol)++;
					break;
				}
				it++;
			}
			semUnlock(nSemID);
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.