/*
    PIP - Platform Independent Primitives
    Process resource monitor
    Copyright (C) 2014  Ivan Pelipenko peri4ko@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pisystemmonitor.h"


PISystemMonitor::PISystemMonitor(): PIThread() {
	pID_ = cycle = 0;
	cpu_count = 1;
#ifndef WINDOWS
#  ifdef QNX
	page_size = 4096;
#  else
	page_size = getpagesize();
#  endif
	cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
	if (cpu_count < 1) cpu_count = 1;
#endif
}


bool PISystemMonitor::startOnProcess(int pID) {
	stop();
	pID_ = pID;
#ifndef WINDOWS
	file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIIODevice::ReadOnly);
	filem.open("/proc/" + PIString::fromNumber(pID_) + "/statm", PIIODevice::ReadOnly);
	if (!file.isOpened()) {
		piCoutObj << "Can`t find process with ID = " << pID_ << "!";
		return false;
	}
	cycle = -1;
#endif
	return start(25);
}


void PISystemMonitor::run() {
#ifndef WINDOWS
	file.seekToBegin();
	PIString str(file.readAll(true));
	int si = str.find('(') + 1, fi = 0, cc = 1;
	for (int i = si; i < str.size_s(); ++i) {
		if (str[i] == '(') cc++;
		if (str[i] == ')') cc--;
		if (cc <= 0) {
			fi = i;
			break;
		}
	}
	stat.exec_name = str.mid(si, fi - si);
	str.cutMid(si - 1, fi - si + 3);
	PIStringList sl = str.split(" ");
	if (sl.size_s() < 18) return;
	stat.ID = sl[0].toInt();
	stat.state = sl[1];
	stat.parent_ID = sl[2].toInt();
	stat.group_ID = sl[3].toInt();
	stat.session_ID = sl[4].toInt();
	if (cycle < 0) {
		cpu_u_prev = cpu_u_cur = sl[12].toLLong();
		cpu_s_prev = cpu_s_cur = sl[13].toLLong();
	}
	cycle++;
	if (cycle >= 40) {
		cpu_u_prev = cpu_u_cur;
		cpu_s_prev = cpu_s_cur;
		cpu_u_cur = sl[12].toLLong();
		cpu_s_cur = sl[13].toLLong();
		stat.cpu_load_system = cpu_s_cur - cpu_s_prev;
		stat.cpu_load_user = cpu_u_cur - cpu_u_prev;
		if (stat.cpu_load_system > 100) stat.cpu_load_system = 100;
		if (stat.cpu_load_user > 100) stat.cpu_load_user = 100;
		stat.cpu_load_system /= cpu_count;
		stat.cpu_load_user /= cpu_count;
		cycle = 0;
	}
	stat.priority = sl[16].toInt();
	stat.threads = sl[18].toInt();

	filem.seekToBegin();
	str = filem.readAll(true);
	sl = str.split(" ");
	if (sl.size_s() < 5) return;
	stat.virtual_memsize = sl[0].toLong() * page_size;
	stat.resident_memsize = sl[1].toLong() * page_size;
	stat.share_memsize = sl[2].toLong() * page_size;
	stat.data_memsize = sl[5].toLong() * page_size;
	stat.physical_memsize = stat.resident_memsize - stat.share_memsize;

	stat.physical_memsize_readable = PIString::readableSize(stat.physical_memsize);
	stat.resident_memsize_readable = PIString::readableSize(stat.resident_memsize);
	stat.share_memsize_readable = PIString::readableSize(stat.share_memsize);
	stat.virtual_memsize_readable = PIString::readableSize(stat.virtual_memsize);
	stat.data_memsize_readable = PIString::readableSize(stat.data_memsize);
#endif
}
