int test_Cpu(loadInfo *pval) {


    ULONGLONG Sys_KernelTime_Diff;
    ULONGLONG Sys_UserTime_Diff;
    ULONGLONG Sys_IdleTime_Diff;
    ULONGLONG Proc_KernelTime_Diff;
    ULONGLONG Proc_UserTime_Diff;
    ULONGLONG nTotalSys;
    ULONGLONG nTotalProc;
    ULONGLONG nTotalMachine;

    
    GetSystemTimes(&Sys_IdleTime, &Sys_KernelTime, &Sys_UserTime);
    GetProcessTimes(GetCurrentProcess(),&Proc_Creationtime,&Proc_IdleTime, &Proc_KernelTime, &Proc_UserTime);
 
    Sys_KernelTime_Diff = SubtractTimes(Sys_KernelTime, Prev_Sys_KernelTime);

    Sys_UserTime_Diff = SubtractTimes(Sys_UserTime, Prev_Sys_UserTime);

    Sys_IdleTime_Diff = SubtractTimes(Sys_IdleTime, Prev_Sys_IdleTime);

    Proc_KernelTime_Diff = SubtractTimes(Proc_KernelTime, Prev_Proc_KernelTime);

    Proc_UserTime_Diff = SubtractTimes(Proc_UserTime, Prev_Proc_UserTime);

   nTotalSys = Sys_KernelTime_Diff + Sys_UserTime_Diff;

    nTotalProc = Proc_UserTime_Diff + Proc_KernelTime_Diff;

    nTotalMachine = (Sys_KernelTime_Diff + Sys_UserTime_Diff)-Sys_IdleTime_Diff;
    
    if (nTotalSys > 0) {
        pval->iocLoad=((100.0 * nTotalProc) / nTotalSys);
        pval->cpuLoad = ((100.0 * nTotalMachine) /nTotalSys );
    }

    Prev_Ticks=Ticks;
    Prev_Proc_IdleTime = Proc_IdleTime;
    Prev_Proc_KernelTime = Proc_KernelTime;
    Prev_Proc_UserTime = Proc_UserTime;

    Prev_Sys_IdleTime = Sys_IdleTime;
    Prev_Sys_KernelTime = Sys_KernelTime;
    Prev_Sys_UserTime = Sys_UserTime;

    return 0;

}
Exemple #2
0
/*
 * PgOctopusWorkerMain is the main entry-point for the background worker
 * that performs health checks.
 */
static void
PgOctopusWorkerMain(Datum arg)
{
	MemoryContext healthCheckContext = NULL;

	/* Establish signal handlers before unblocking signals. */
	pqsignal(SIGHUP, pg_octopus_sighup);
	pqsignal(SIGINT, SIG_IGN);
	pqsignal(SIGTERM, pg_octopus_sigterm);

	/* We're now ready to receive signals */
	BackgroundWorkerUnblockSignals();

	/* Connect to our database */
	BackgroundWorkerInitializeConnection("postgres", NULL);

	healthCheckContext = AllocSetContextCreate(CurrentMemoryContext,
											   "Health check context",
											   ALLOCSET_DEFAULT_MINSIZE,
											   ALLOCSET_DEFAULT_INITSIZE,
											   ALLOCSET_DEFAULT_MAXSIZE);

	MemoryContextSwitchTo(healthCheckContext);

	elog(LOG, "pg_octopus monitor started");

	/*
	 * Main loop: do this until the SIGTERM handler tells us to terminate
	 */
	while (!got_sigterm)
	{
		struct timeval currentTime = {0, 0};
		struct timeval roundEndTime = {0, 0};
		struct timeval timeout = {0, 0};
		List *nodeHealthList = NIL;
		List *healthCheckList = NIL;

		gettimeofday(&currentTime, NULL);

		roundEndTime = AddTimeMillis(currentTime, HealthCheckPeriod);

		nodeHealthList = LoadNodeHealthList();

		healthCheckList = CreateHealthChecks(nodeHealthList);

		DoHealthChecks(healthCheckList);

		MemoryContextReset(healthCheckContext);

		gettimeofday(&currentTime, NULL);

		timeout = SubtractTimes(roundEndTime, currentTime);

		if (timeout.tv_sec >= 0 && timeout.tv_usec >= 0)
		{
			LatchWait(timeout);
		}

		if (got_sighup)
		{
			got_sighup = false;
			ProcessConfigFile(PGC_SIGHUP);
		}
	}

	elog(LOG, "pg_octopus monitor exiting");

	proc_exit(0);
}
Exemple #3
0
/**********************************************
* CpuUsage::GetUsage
* returns the percent of the CPU that this process
* has used since the last time the method was called.
* If there is not enough information, -1 is returned.
* If the method is recalled to quickly, the previous value
* is returned.
***********************************************/
double CpuUsage::GetUsage()
{
	//create a local copy to protect against race conditions in setting the 
	//member variable
	double nCpuCopy = m_nCpuUsage;
	if (::InterlockedIncrement(&m_lRunCount) == 1)
	{
		/*
		If this is called too often, the measurement itself will greatly affect the
		results.
		*/

		if (!EnoughTimePassed())
		{
			::InterlockedDecrement(&m_lRunCount);
			return nCpuCopy;
		}

		FILETIME ftSysIdle, ftSysKernel, ftSysUser;
		FILETIME ftProcCreation, ftProcExit, ftProcKernel, ftProcUser;

		if (!GetSystemTimes(&ftSysIdle, &ftSysKernel, &ftSysUser) ||
			!GetProcessTimes(GetCurrentProcess(), &ftProcCreation, &ftProcExit, &ftProcKernel, &ftProcUser))
		{
			::InterlockedDecrement(&m_lRunCount);
			return nCpuCopy;
		}

		if (!IsFirstRun())
		{
			/*
			CPU usage is calculated by getting the total amount of time the system has operated
			since the last measurement (made up of kernel + user) and the total
			amount of time the process has run (kernel + user).
			*/
			ULONGLONG ftSysKernelDiff = SubtractTimes(ftSysKernel, m_ftPrevSysKernel);
			ULONGLONG ftSysUserDiff = SubtractTimes(ftSysUser, m_ftPrevSysUser);

			ULONGLONG ftProcKernelDiff = SubtractTimes(ftProcKernel, m_ftPrevProcKernel);
			ULONGLONG ftProcUserDiff = SubtractTimes(ftProcUser, m_ftPrevProcUser);

			ULONGLONG nTotalSys =  ftSysKernelDiff + ftSysUserDiff;
			ULONGLONG nTotalProc = ftProcKernelDiff + ftProcUserDiff;

			if (nTotalSys > 0)
			{
				//m_nCpuUsage = (short)((100.0 * nTotalProc) / nTotalSys);
                m_nCpuUsage = (100.0 * nTotalProc) / nTotalSys;
			}
		}
		
		m_ftPrevSysKernel = ftSysKernel;
		m_ftPrevSysUser = ftSysUser;
		m_ftPrevProcKernel = ftProcKernel;
		m_ftPrevProcUser = ftProcUser;
		
		//m_dwLastRun = GetTickCount64();
        DWORD dwLastRun = GetTickCount();
        m_dwLastRun = (ULONGLONG)dwLastRun;

		nCpuCopy = m_nCpuUsage;
	}
	
	::InterlockedDecrement(&m_lRunCount);

	return nCpuCopy;
}