Ejemplo n.º 1
0
void CpuMuninNodePlugin::CalculateCpuLoad()
{
  if (NtQuerySystemInformation != NULL && GetSystemTimes != NULL) {
    LONG status;
    SYSTEM_TIME_INFORMATION SysTimeInfo;
    SYSTEM_BASIC_INFORMATION SysBaseInfo;

    // get number of processors in the system
    status = NtQuerySystemInformation(SystemBasicInformation, &SysBaseInfo, sizeof(SysBaseInfo), NULL);
    if (status != NO_ERROR) {
      printf("Querying SystemBasicInformation failed: 0x%x\n", status);
      return;
    }

    // get new system time
    status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), NULL);
    if (status!=NO_ERROR) {
      printf("Querying SystemTimeInformation failed: 0x%x\n", status);
      return;
    }

    // get new CPU times
    // http://www.codeproject.com/Articles/9113/Get-CPU-Usage-with-GetSystemTimes
    FILETIME ftIdleTime;
    FILETIME ftKernelTime;
    FILETIME ftUserTime;
    BOOL result = GetSystemTimes((LPFILETIME)&ftIdleTime, (LPFILETIME)&ftKernelTime, (LPFILETIME)&ftUserTime);
    if (result == FALSE) {
      printf("GetSystemTimes failed\n");
      return;
    }
    unsigned long long systemTime = FileTimeToInt64(ftKernelTime) + FileTimeToInt64(ftUserTime);
    unsigned long long idleTime = FileTimeToInt64(ftIdleTime);

    // if it's a first call - skip it
    if (liOldIdleTime != 0)
    {
      // CurrentValue = NewValue - OldValue
      __int64 diffIdleTime = idleTime - liOldIdleTime;
      __int64 diffSystemTime = systemTime - liOldSystemTime;

      dbCpuTimePercent = (1.0f - ((diffSystemTime > 0) ? ((float)diffIdleTime) / diffSystemTime : 0)) * 100;
    }

    // store new times
    liOldIdleTime = idleTime;
    liOldSystemTime = systemTime;
  }
  else {
    printf("NtQuerySystemInformation or GetSystemTimes functions not available\n");
  }
}
Ejemplo n.º 2
0
    Timestamp GetTimeOfDay()
    {
        FILETIME now;
        GetSystemTimeAsFileTime (&now); // 获得系统UTC格式时间

        int64 nowIn100ns = FileTimeToInt64(now);// 单位100ns
        return nowIn100ns / 10000; //转化为ms单位
    }
Ejemplo n.º 3
0
    Timestamp GetLocalTime()
    {
        FILETIME local;
        GetSystemTimeAsFileTime (&local); // 获得系统UTC格式时间
        FileTimeToLocalFileTime(&local, &local); // 转换为本地时间

        int64 localIn100ns = FileTimeToInt64(local);// 单位100ns
        return localIn100ns / 10000;
    }
Ejemplo n.º 4
0
__int64 SysytemTimeToInt64(const SYSTEMTIME& time_value)
{
	FILETIME ft;

	BOOL errorFlag = SystemTimeToFileTime(&time_value, &ft);
	__int64 res = FileTimeToInt64(ft);

	if (errorFlag == 0)
		res = -1;

	return res;
}
Ejemplo n.º 5
0
int ProcessWatch::GetProcessCpuUsage(int pid)
{  
	static int processor_count_ = -1;
	static __int64 last_time_ = 0;
	static __int64 last_system_time_ = 0;

	FILETIME now;
	FILETIME creation_time;
	FILETIME exit_time;
	FILETIME kernel_time;
	FILETIME user_time;
	__int64 system_time;
	__int64 time;
	// 	__int64 system_time_delta;
	// 	__int64 time_delta;

	double cpu = -1;

	if(processor_count_ == -1)
	{
		processor_count_ = GetProcessorNumber();
	}

	GetSystemTimeAsFileTime(&now);

	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, pid);
	if (!hProcess)
	{
		return -1;
	}
	if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
	{
		return -1;
	}
	system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_;  
	time = FileTimeToInt64(now);		

	last_system_time_ = system_time;
	last_time_ = time;
	CloseHandle( hProcess );

	Sleep(30);

	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, pid);
	if (!hProcess)
	{
		return -1;
	}
	if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
	{
		return -1;
	}
	GetSystemTimeAsFileTime(&now);
	system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_; 
	time = FileTimeToInt64(now);		

	CloseHandle( hProcess );

	cpu = ((double)(system_time - last_system_time_) / (double)(time - last_time_)) * 100;
	return (int)cpu;
}