process_prctl.cpp 4.6 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
/*!
丁劲犇's avatar
丁劲犇 已提交
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 32 33 34
  @author goldenhawking@163.com
  @date 2017-02-11
  */
#include "process_prctl.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#ifdef WIN32
#include <windows.h>
#include <psapi.h>
#endif
#ifdef linux
#include <sys/resource.h>
#include <unistd.h>
#endif

namespace TASKBUS {

#ifdef WIN32
	const int pnice_min = 0;
	const int pnice_max = 5;
	const int pnice_norm = 2;
#endif

#ifdef linux
	const int pnice_min = PRIO_MIN;
	const int pnice_max = PRIO_MAX;
	const int pnice_norm = 0;

#endif

	void set_proc_nice(QProcess * p, int nice)
	{
丁劲犇's avatar
丁劲犇 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#ifdef WIN32
		qint64 id = p->processId();
		HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,
									FALSE,id);
		if (hProcess)
		{
			const DWORD dw_pn []= {
				IDLE_PRIORITY_CLASS,
				BELOW_NORMAL_PRIORITY_CLASS,
				NORMAL_PRIORITY_CLASS,
				ABOVE_NORMAL_PRIORITY_CLASS,
				HIGH_PRIORITY_CLASS,
				REALTIME_PRIORITY_CLASS
			};
			if (nice<0)		nice = 0;
			if (nice>5)		nice = 5;
			SetPriorityClass(hProcess,dw_pn[nice]);
			CloseHandle(hProcess);
		}

#endif
丁劲犇's avatar
丁劲犇 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68

#ifdef linux
		Q_PID id = p->pid();
		if (nice<-PRIO_MIN)	nice = PRIO_MIN;
		if (nice>PRIO_MAX)	nice = PRIO_MAX;
		setpriority(PRIO_PROCESS,id,nice);
#endif


	}

	void set_proc_nice (int nice)
	{
丁劲犇's avatar
丁劲犇 已提交
69
#ifdef WIN32
丁劲犇's avatar
丁劲犇 已提交
70 71 72 73 74 75 76 77 78 79 80
		const DWORD dw_pn []= {
			IDLE_PRIORITY_CLASS,
			BELOW_NORMAL_PRIORITY_CLASS,
			NORMAL_PRIORITY_CLASS,
			ABOVE_NORMAL_PRIORITY_CLASS,
			HIGH_PRIORITY_CLASS,
			REALTIME_PRIORITY_CLASS
		};
		if (nice<0)		nice = 0;
		if (nice>5)		nice = 5;
		SetPriorityClass(GetCurrentProcess(),dw_pn[nice]);
丁劲犇's avatar
丁劲犇 已提交
81
#endif
丁劲犇's avatar
丁劲犇 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

#ifdef linux
		Q_PID id = getpid();
		if (nice<-PRIO_MIN)	nice = PRIO_MIN;
		if (nice>PRIO_MAX)	nice = PRIO_MAX;
		setpriority(PRIO_PROCESS,id,nice);
#endif
	}

	bool get_memory (qint64 p ,tagMemoryInfo * info)
	{
#ifdef linux
		info->pid = p;
		info->phandle = p;
		QString strFm;
		strFm.sprintf("/proc/%lld/status",p);
		QFile fin(strFm);
		if (fin.open(QIODevice::ReadOnly)==false)
			return false;
		QTextStream s(&fin);
		int hit = 0;
		QString l = s.readLine();
		while (l.size() && hit<2)
		{
D
dev 已提交
106
#if QT_VERSION >= 0x051500
丁劲犇's avatar
丁劲犇 已提交
107
			QStringList lst = l.split(":",Qt::SkipEmptyParts);
D
dev 已提交
108 109 110
#else
			QStringList lst = l.split(":",QString::SkipEmptyParts);
#endif
丁劲犇's avatar
丁劲犇 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
			if (lst.size()>=2)
			{
				QString keystr = lst.first().trimmed();
				lst.pop_front();
				QString v = lst.first().trimmed();
				if (keystr=="Name")
				{
					info->m_name = v;
					++hit;
				}
				if (keystr=="VmRSS")
				{
					v = v.toUpper();
					++hit;
					if (v.indexOf("K"))
					{
						QString digiga = v.left(v.indexOf("K"));
						info->m_memsize = digiga.toDouble()*1024;
					}
					else if (v.indexOf("M"))
					{
						QString digiga = v.left(v.indexOf("M"));
						info->m_memsize = digiga.toDouble()*1024*1024;
					}
					else if (v.indexOf("G"))
					{
						QString digiga = v.left(v.indexOf("G"));
						info->m_memsize = digiga.toDouble()*1024*1024*1024;
					}
					else if (v.indexOf("B"))
					{
						QString digiga = v.left(v.indexOf("B"));
						info->m_memsize = digiga.toDouble();
					}
					else if (v.toDouble()>0)
						info->m_memsize = v.toDouble();
					else
						--hit;
				}

			}
			l = s.readLine();
		}
		fin.close();
		if (hit>=2)
			return true;
#endif

#ifdef WIN32
		char ch_module[MAX_PATH] = {0,0,0,0};
		if (p==-1)
		{
			GetModuleFileNameA(0,ch_module,MAX_PATH);
			PROCESS_MEMORY_COUNTERS pmc;
			GetProcessMemoryInfo(GetCurrentProcess(),&pmc,sizeof(pmc));
			info->m_memsize  = pmc.WorkingSetSize;
			info->pid = GetProcessId(GetCurrentProcess());
			info->phandle = (qint64)GetCurrentProcess();
		}
		else if (p)
		{
丁劲犇's avatar
丁劲犇 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

			HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|
										PROCESS_VM_READ,
										FALSE,p);
			info->pid = 0;
			if (hProcess)
			{
				HMODULE hMod;
				DWORD cbNeeded;

				if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
										 &cbNeeded) )
				{
					GetModuleFileNameExA(hProcess,hMod,ch_module,MAX_PATH);
					PROCESS_MEMORY_COUNTERS pmc;
					GetProcessMemoryInfo(hProcess,&pmc,sizeof(pmc));
					info->m_memsize  = pmc.WorkingSetSize;
				}
				info->pid = p;

			}
			//printf( "%s (Process ID: %u)\n", szProcessName, processID );

			CloseHandle( hProcess );
丁劲犇's avatar
丁劲犇 已提交
196 197 198
			if (info->pid==0)
				return false;
			info->phandle = (qint64)p;
丁劲犇's avatar
丁劲犇 已提交
199

丁劲犇's avatar
丁劲犇 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		}
		char * pt =ch_module + strlen(ch_module)-1;
		while (pt > ch_module)
		{
			if (*pt == '\\' || *pt=='/'  || *pt==':')
				break;
			--pt;
		}
		++pt;
		info->m_name = QString::fromLocal8Bit(pt);

		return true;
#endif
		return false;
	}

	qint64 get_procid(QProcess * p)
	{
#ifdef linux
		if (p)
			return p->pid();
		else
			return getpid();
#endif
#ifdef WIN32
		if (p)
丁劲犇's avatar
丁劲犇 已提交
226
			return (qint64)p->processId();
丁劲犇's avatar
丁劲犇 已提交
227 228 229 230 231 232
		else
			return (qint64)GetCurrentProcess();
#endif
		return 0;
	}
}