티스토리 뷰

la fermata, 개발/C, C++

[Code] CPU load

창공미나래 2019. 2. 19. 22:54

이거 만들다가 high, low 개념 잘 몰라서 소스 참고해서 만들었다.

다 만들고 나니 이해가 되더라.


원래 High 쪽 처리를 해줘야되는데, 귀찮아서 빼줬다.

7초 이내의 값의 차이를 체크한다면 High를 빼줘도 정상적으로 동작하지만,

last, current가 7초 이상 차이가 나면 문제가 발생한다. 그때는 high 값을 가지고 판단해야한다.

[참고한 사이트 : http://hushou.tistory.com/357]


5초, 10초, 15초의 평균 값은 간단하게 계산해보았다.

copy & paste하면 도움이 안된다. 주석까지 copy & paste하지 말길 ㅋ


이번 과제의 목표는 아래와 같다.

display the CPU load of your computer every 1 second.

print out the followings on the screen every 1 second.

– Current time

– Current CPU load

– Average CPU load during last 5, 10, 15 seconds


아래와 같이 출력하게 만들면 된다.


#include <windows.h>
#define _WIN32_WINNT 0x050
#include <iostream>
#include <WinBase.h>
#include <time.h>
#include <stdlib.h>
#define MAX 65535

using namespace std;

void print_less_ten(int n){ // change print style for right format with '0'
	if(n<10){
		cout << "0" ;
	}
}

void print_time(){ //print current time

		time_t t = time(0);   // set time type
	    struct tm * now = localtime( & t ); // get time now
	
		cout << (now->tm_year + 1900) << '_'; // print year
		
		print_less_ten(now->tm_mon + 1);
	
		cout << (now->tm_mon + 1) << "_" ;  // print month
		
		print_less_ten(now->tm_mday);
		
		cout << now->tm_mday << " : ";   // print day
		
		print_less_ten(now->tm_hour);

		cout << now->tm_hour << "_" ;    // print hour
		
		print_less_ten(now->tm_min);

		cout << now->tm_min << "_";     // print minute

		print_less_ten((now->tm_sec)+1);

		cout << (now->tm_sec + 1);  // print second
}

void print_avg_usage(int index, double my_array[MAX]){ // print average CPU load during 5,10,15 seconds
	double sum, avg; // define variables for sum and average
	sum = avg = 0;
	
	if(index>=4){ 
		//if index is more than 4(array index 0,1,2,3,4 = 5 things)
		// print 5 sec average
			sum = 0; // initialization sum
			for(int i=index-4; i<=index ; i++)
			{
				sum+=my_array[i];
			}
			avg = sum / 5;
			cout << "[5 sec avg : " << avg << "]";

			if(index>=9){
				//if index is more than 9(array index 0~9 = 10 things)
				// print 10 sec average
				sum = 0;  // initialization sum
				for(int i=index-9; i<=index ; i++)
				{
					sum+=my_array[i];
				}
				avg = sum / 10;
				cout << "[10 sec avg : " << avg << "]";

				if(index>=14){
					//if index is more than 14(array index 0~14 = 15 things)
					// print 15 sec average
					sum = 0; // initialization sum
					for(int i=index-14; i<=index ; i++)
					{
						sum+=my_array[i];
					}
					avg = sum / 15;
					cout << "[15 sec avg : " << avg << "]\n";
				} else {
					cout << "\n"; // if index is less than 14(index 0~14, 15 things), it will change line.
				}
			} else {
				cout << "\n"; // if index is less than 9(index 0~9, 10 things), it will change line.
			}
		} else { // if index is less than 4(index 0~4, 5 things), it will change line.
			cout << "\n";
		}
}


int main(){

	//define variables 
	FILETIME idleTime;
	FILETIME kernelTime;
	FILETIME userTime;
	BOOL res;

	//define variables for last system times
	DWORD idle_High;
	DWORD idle_Low;
	DWORD kernel_High;
	DWORD kernel_Low;
	DWORD user_High;
	DWORD user_Low;

	DWORD high_1;
	DWORD Low_1;
	
	//define variables for current system times
	DWORD idle_High_2;
	DWORD idle_Low_2;
	DWORD kernel_High_2;
	DWORD kernel_Low_2;
	DWORD user_High_2;
	DWORD user_Low_2;

	DWORD high_2;
	DWORD Low_2;

	//define variables to get idle, kernel, user mode's time;
	double idle; 
	double kernel;
	double user;

	cout.setf(ios::fixed); //cout config
	cout.precision(2); // cout config
	
	system("mode con: lines=30 cols=130"); // change console size

	double my_array[MAX]; // array to calculate average between current cpu usage and last cpu usage
	int index=0; // index for array

	while(true){

		res = GetSystemTimes(&idleTime, &kernelTime, &userTime ); // get system times (idle, kernel, user)
		if(res == NULL) return 0; // if it can't get system times, it will return zero.

		// set high, low in FILETIME structure (first time)
		idle_High = idleTime.dwHighDateTime; 
		idle_Low = idleTime.dwLowDateTime;
		kernel_High = kernelTime.dwHighDateTime;
		kernel_Low = kernelTime.dwLowDateTime;
		user_High = userTime.dwHighDateTime;
		user_Low = userTime.dwLowDateTime;

		high_1 = idle_High + kernel_High + user_High;
		Low_1 = idle_Low + kernel_Low + user_Low;
		
		Sleep(1000); // sleep to get system time for 1 sec
		
		res = GetSystemTimes(&idleTime, &kernelTime, &userTime ); // get system times (idle, kernel, user)
		if(res == NULL) return 0; // if it can't get system times, it will return zero.

		// set high, low in FILETIME structure (second time)
		idle_High_2 = idleTime.dwHighDateTime;
		idle_Low_2 = idleTime.dwLowDateTime;
		kernel_High_2 = kernelTime.dwHighDateTime;
		kernel_Low_2 = kernelTime.dwLowDateTime;
		user_High_2 = userTime.dwHighDateTime;
		user_Low_2 = userTime.dwLowDateTime;

		high_2 = idle_High_2 + kernel_High_2 + user_High_2;
		Low_2 = idle_Low_2 + kernel_Low_2 + user_Low_2;
		
		// calculate time (first time minus second time)
		idle = idle_Low_2 - idle_Low; // get time in idle mode 
		kernel = kernel_Low_2 - kernel_Low;  // get time in kernel mode
		user = user_Low_2 - user_Low;  // get time in user mode
		
		my_array[index] = ((user + kernel - idle) * 100 / (user + kernel)); // calculate cpu usage for 1 sec
		printf("%2d ",index); // print index
		
		print_time() ; // print current time
		cout << " [CPU Load : "<<  my_array[index] << "]"; // print cpu load
		
		print_avg_usage(index,my_array); // print cpu usage average for 5, 10, 15 sec 
		
		index++;

		if(index > 65535){ //if array index is more than 65535, it will exit itself.
			return 0;
		}

	}

	return 0;
}


' la fermata, 개발 > C, C++' 카테고리의 다른 글

[Code] multiplication table / 구구단  (0) 2019.02.19
[Code] Program Counter  (0) 2019.02.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함