bool CaProfileWriter::writeProfileTotal(PidProcessMap * procMap)
{
	AggregatedSample aggSamp;

	PidProcessMap::iterator pit  = procMap->begin();
	PidProcessMap::iterator pend = procMap->end();
	for(; pit != pend; pit++) {
		aggSamp.addSamples((const AggregatedSample *)&(pit->second));
	}
	
	fwprintf(m_pFile, L"TOTAL=");

	CA_SampleMap::const_iterator ait  = aggSamp.getBeginSample();
	CA_SampleMap::const_iterator aend = aggSamp.getEndSample();
	while (ait != aend) {
		SampleKey key = ait->first;
	
		map<EVMASK_T, int>::iterator eit;
		if ((eit = m_evToIndexMap.find(key.event)) == m_evToIndexMap.end()) {
			m_err = E_INVALIDPROCESSMAP;
			return false;
		}

		fwprintf(m_pFile, L"[%u %d] %u", key.cpu, eit->second, 
											ait->second);
		if (++ait != aend)
			fwprintf(m_pFile, L",");
		else
			fwprintf(m_pFile, L"\n");
	}
	
	return true;
}
bool SystemDataTab::displayProcessToPidToModule()
{

	m_pList->setColumnText (SYS_MOD_NAME, "Process -> Pid -> Module");

	m_nameProgItemMap.clear();
	
	PidProcessMap::const_iterator pit  = m_pProcMap->begin();
	PidProcessMap::const_iterator pend = m_pProcMap->end();
	for(; pit != pend; pit++) {
		SystemItem * pProgItem = NULL;
		AggregatedSample * pProgAgg  = NULL;

		NameProgramItemMap::iterator nit = m_nameProgItemMap.find(pit->second.getPath());
		if (nit == m_nameProgItemMap.end()) {
			ProgramItem progItem;
			
			progItem.pProg = new SystemItem(m_pViewShown, SYS_OFFSET_INDEX, m_pList);
			RETURN_FALSE_IF_NULL (progItem.pProg, this);
		
			m_nameProgItemMap.insert(
				NameProgramItemMap::value_type(pit->second.getPath(), progItem));	
		
			// Refind
			nit = m_nameProgItemMap.find(pit->second.getPath());
			if (nit == m_nameProgItemMap.end())
				return false;

			pProgItem = nit->second.pProg;
			pProgAgg  = &(nit->second.progAgg);
			
			// Set Process Name
			pProgItem->setText (SYS_MOD_NAME, wstrToQStr(pit->second.getPath()));

			// Set SixtyFourBit
			if (!pit->second.m_is32Bit)
				pProgItem->setChecked (SYS_64_BIT);
			else
				pProgItem->setChecked (-1);

			pProgAgg->clear();

		} else {
			pProgItem = nit->second.pProg;
			pProgAgg  = &(nit->second.progAgg);
		}

		SystemItem * pPidItem = new SystemItem(m_pViewShown, SYS_OFFSET_INDEX, pProgItem);
		RETURN_FALSE_IF_NULL (pPidItem, this);

		// Set Process Name
		pPidItem->setText (SYS_MOD_NAME, QString("PID : ") + QString::number(pit->first));

		// Set SixtyFourBit
		if (!pit->second.m_is32Bit)
			pPidItem->setChecked (SYS_64_BIT);
		else
			pPidItem->setChecked (-1);

		pPidItem->m_taskId = pit->first;

		AggregatedSample pidAgg;

		// For each module
		NameModuleMap::const_iterator mit  = m_pModMap->begin();
		NameModuleMap::const_iterator mend = m_pModMap->end();
		for(; mit != mend; mit++) {
			// Find PID 
			PidAggregatedSampleMap::const_iterator ait = mit->second.findSampleForPid(pit->first);
			if(ait == mit->second.getEndSample())
				continue;

			SystemItem * pModItem = new SystemItem(m_pViewShown, SYS_OFFSET_INDEX, pPidItem);
			RETURN_FALSE_IF_NULL (pModItem, this);

			// Set Module Name
			pModItem->setText (SYS_MOD_NAME, wstrToQStr(mit->first));
			
			// Set Module for the process item
			if (mit->first == pit->second.getPath())
				pProgItem->m_pMod = &(mit->second);
			
			pModItem->m_pMod = &(mit->second);
			pModItem->m_taskId = ait->first;

			// Set Module Data
			pModItem->setTotal(&m_totalAgg);
			pModItem->drawData(&(ait->second), true);
			pModItem->updateShown ();

			pModItem->setBackgroundColor(Qt::lightGray);

			pidAgg.addSamples(&(ait->second));
		} // for each module
	
		// Set Pid Data	
		pPidItem->setTotal(&m_totalAgg);
		pPidItem->drawData(&(pidAgg), true);
		pPidItem->updateShown ();

		// Set Program Data
		pProgAgg->addSamples(&(pidAgg));
		pProgItem->setTotal(&m_totalAgg);
		pProgItem->drawData(pProgAgg, true);
		pProgItem->updateShown ();
		
	} // for each pid

	return TRUE;
}
bool SystemDataTab::displayModuleToProcess()
{

	m_pList->setColumnText (SYS_MOD_NAME, "Module -> Process");

	// For each module
	NameModuleMap::const_iterator mit  = m_pModMap->begin();
	NameModuleMap::const_iterator mend = m_pModMap->end();
	for(; mit != mend; mit++) {
		SystemItem * pModItem = new SystemItem(m_pViewShown, SYS_OFFSET_INDEX, m_pList);
		RETURN_FALSE_IF_NULL (pModItem, this);

		// Set Module Name
		pModItem->setText (SYS_MOD_NAME, wstrToQStr(mit->first));

		// Set SixtyFourBit
		if (!mit->second.m_is32Bit)
			pModItem->setChecked (SYS_64_BIT);
		else
			pModItem->setChecked (-1);

		// Set Module
		pModItem->m_pMod = &(mit->second);
		pModItem->m_taskId = SHOW_ALL_TASKS;

		AggregatedSample modAgg;

		// For each Pid
		PidAggregatedSampleMap::const_iterator ait  = mit->second.getBeginSample();
		PidAggregatedSampleMap::const_iterator aend = mit->second.getEndSample();
		for(; ait != aend; ait++)  {
			SystemItem * pProcItem = new SystemItem(m_pViewShown, SYS_OFFSET_INDEX, pModItem);
			RETURN_FALSE_IF_NULL (pProcItem, this);

			// Set Process Name
			pProcItem->setText (SYS_MOD_NAME, getProcNameForPid(ait) 
				+ " (" 
				+ QString::number(ait->first)
				+ ")");

			// Set Module
			pProcItem->m_pMod = &(mit->second);
			pProcItem->m_taskId = ait->first;

			// Set Process Data
			pProcItem->setTotal(&m_totalAgg);
			pProcItem->drawData(&(ait->second), true);
			pProcItem->updateShown ();

			pProcItem->setBackgroundColor(Qt::lightGray);

			modAgg.addSamples(&(ait->second));
		}
	
		// Set Module Data	
		pModItem->setTotal(&m_totalAgg);
		pModItem->drawData(&(modAgg), true);
		pModItem->updateShown ();
	}

	return TRUE;
}