2003-04-04 19:26:04 +00:00
|
|
|
/* Console Task Manager
|
|
|
|
|
|
|
|
ctm.c - main program file
|
|
|
|
|
|
|
|
Written by: Aleksey Bragin (aleksey@studiocerebral.com)
|
|
|
|
|
|
|
|
Most of the code dealing with getting system parameters is taken from
|
2003-04-09 16:31:28 +00:00
|
|
|
ReactOS Task Manager written by Brian Palmer (brianp@reactos.org)
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
History:
|
2003-04-09 16:31:28 +00:00
|
|
|
09 April 2003 - v0.1, fixed bugs, added features, ported to mingw
|
|
|
|
20 March 2003 - v0.03, works good under ReactOS, and allows process
|
|
|
|
killing
|
|
|
|
18 March 2003 - Initial version 0.01, doesn't work under RectOS
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
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 2 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, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
|
|
|
|
|
|
|
|
|
//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows //headers
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-04-13 03:57:41 +00:00
|
|
|
#include <ddk/ntddk.h>
|
|
|
|
#include <epsapi.h>
|
|
|
|
|
2003-04-04 19:26:04 +00:00
|
|
|
#include "ctm.h"
|
|
|
|
|
|
|
|
#define MAX_PROC 17
|
|
|
|
//#define TIMES
|
|
|
|
|
|
|
|
HANDLE hStdin;
|
|
|
|
HANDLE hStdout;
|
|
|
|
|
|
|
|
DWORD inConMode;
|
|
|
|
DWORD outConMode;
|
|
|
|
|
|
|
|
//PROCNTQSI NtQuerySystemInformation= NULL;
|
|
|
|
|
|
|
|
const int ProcPerScreen = 17; // 17 processess are displayed on one page
|
|
|
|
ULONG ProcessCountOld = 0;
|
|
|
|
ULONG ProcessCount = 0;
|
|
|
|
|
|
|
|
double dbIdleTime;
|
|
|
|
double dbKernelTime;
|
|
|
|
double dbSystemTime;
|
|
|
|
LARGE_INTEGER liOldIdleTime = {{0,0}};
|
|
|
|
double OldKernelTime = 0;
|
|
|
|
LARGE_INTEGER liOldSystemTime = {{0,0}};
|
|
|
|
|
|
|
|
PPERFDATA pPerfDataOld = NULL; // Older perf data (saved to establish delta values)
|
|
|
|
PPERFDATA pPerfData = NULL; // Most recent copy of perf data
|
|
|
|
|
|
|
|
int selection=0;
|
|
|
|
int scrolled=0; // offset from which process start showing
|
|
|
|
|
|
|
|
#define NEW_CONSOLE
|
|
|
|
|
2003-04-13 03:57:41 +00:00
|
|
|
void *PsaiMalloc(SIZE_T size) { return malloc(size); }
|
|
|
|
void *PsaiRealloc(void *ptr, SIZE_T size) { return realloc(ptr, size); }
|
|
|
|
void PsaiFree(void *ptr) { free(ptr); }
|
|
|
|
|
2003-04-04 19:26:04 +00:00
|
|
|
// Prototypes
|
|
|
|
unsigned int GetKeyPressed();
|
|
|
|
|
|
|
|
void GetInputOutputHandles()
|
|
|
|
{
|
|
|
|
#ifdef NEW_CONSOLE
|
|
|
|
HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
0, CONSOLE_TEXTMODE_BUFFER, 0);
|
|
|
|
|
|
|
|
if (SetConsoleActiveScreenBuffer(console) == FALSE)
|
|
|
|
{
|
|
|
|
hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hStdin = GetStdHandle(STD_INPUT_HANDLE);//console;
|
|
|
|
hStdout = console;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void RestoreConsole()
|
|
|
|
{
|
|
|
|
SetConsoleMode(hStdin, inConMode);
|
|
|
|
SetConsoleMode(hStdout, outConMode);
|
|
|
|
|
|
|
|
#ifdef NEW_CONSOLE
|
|
|
|
SetConsoleActiveScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayScreen()
|
|
|
|
{
|
|
|
|
COORD pos;
|
|
|
|
char lpStr[80];
|
|
|
|
int idx;
|
|
|
|
DWORD numChars;
|
|
|
|
int lines;
|
|
|
|
|
|
|
|
// Header
|
|
|
|
pos.X = 2; pos.Y = 2;
|
2003-04-09 16:31:28 +00:00
|
|
|
strcpy(lpStr, "Console TaskManager v0.1 by Aleksey Bragin <aleksey@studiocerebral.com>");
|
2003-04-04 19:26:04 +00:00
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
pos.X = 2; pos.Y = 3;
|
|
|
|
strcpy(lpStr, "+-------------------------------+-------+-----+-----------+-------------+");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
pos.X = 2; pos.Y = 4;
|
|
|
|
strcpy(lpStr, "| Image name | PID | CPU | Mem Usage | Page Faults |");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
pos.X = 2; pos.Y = 5;
|
|
|
|
strcpy(lpStr, "+-------------------------------+-------+-----+-----------+-------------+");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
// Footer
|
|
|
|
pos.X = 2; pos.Y = 23;
|
|
|
|
strcpy(lpStr, "+-------------------------------+-------+-----+-----------+-------------+");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
// Menu
|
|
|
|
pos.X = 2; pos.Y = 24;
|
|
|
|
strcpy(lpStr, "Press: q - quit, k - kill process ");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &numChars);
|
|
|
|
|
|
|
|
// Processess
|
|
|
|
lines = ProcessCount;
|
|
|
|
if (lines > MAX_PROC)
|
|
|
|
lines = MAX_PROC;
|
|
|
|
for (idx=0; idx<lines; idx++)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char imgName[MAX_PATH];
|
|
|
|
char lpPid[8];
|
|
|
|
char lpCpu[6];
|
|
|
|
char lpMemUsg[12];
|
|
|
|
char lpPageFaults[15];
|
|
|
|
WORD wColor;
|
|
|
|
|
|
|
|
// data
|
|
|
|
// image name
|
|
|
|
pos.X = 3; pos.Y = 6+idx;
|
|
|
|
memset(imgName, 0, MAX_PATH);
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, pPerfData[scrolled+idx].ImageName, -1,
|
|
|
|
imgName, MAX_PATH, NULL, NULL);
|
|
|
|
len = strlen(imgName);
|
2003-04-09 16:31:28 +00:00
|
|
|
WriteConsoleOutputCharacter(hStdout, " ", 30, pos, &numChars);
|
2003-04-04 19:26:04 +00:00
|
|
|
WriteConsoleOutputCharacter(hStdout, imgName, (len > 30) ? 30 : len, pos, &numChars);
|
|
|
|
|
|
|
|
// PID
|
|
|
|
pos.X = 35; pos.Y = 6+idx;
|
|
|
|
sprintf(lpPid, "%6ld", pPerfData[scrolled+idx].ProcessId);
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpPid, strlen(lpPid), pos, &numChars);
|
|
|
|
|
|
|
|
// CPU
|
|
|
|
pos.X = 43; pos.Y = 6+idx;
|
|
|
|
sprintf(lpCpu, "%3d%%", pPerfData[scrolled+idx].CPUUsage);
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpCpu, strlen(lpCpu), pos, &numChars);
|
|
|
|
|
|
|
|
// Mem usage
|
|
|
|
pos.X = 49; pos.Y = 6+idx;
|
|
|
|
sprintf(lpMemUsg, "%6ld", pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024);
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpMemUsg, strlen(lpMemUsg), pos, &numChars);
|
|
|
|
|
|
|
|
// Page Fault
|
|
|
|
pos.X = 61; pos.Y = 6+idx;
|
|
|
|
sprintf(lpPageFaults, "%12ld", pPerfData[scrolled+idx].PageFaultCount);
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpPageFaults, strlen(lpPageFaults), pos, &numChars);
|
|
|
|
|
|
|
|
// columns
|
|
|
|
pos.X = 2; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
pos.X = 34; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
pos.X = 42; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
pos.X = 48; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
pos.X = 60; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
pos.X = 74; pos.Y = 6+idx;
|
|
|
|
WriteConsoleOutputCharacter(hStdout, "|", 1, pos, &numChars);
|
|
|
|
|
|
|
|
|
|
|
|
// Attributes now...
|
|
|
|
pos.X = 3; pos.Y = 6+idx;
|
|
|
|
if (selection == idx)
|
|
|
|
{
|
|
|
|
wColor = BACKGROUND_GREEN |
|
|
|
|
FOREGROUND_RED |
|
|
|
|
FOREGROUND_GREEN |
|
|
|
|
FOREGROUND_BLUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wColor = BACKGROUND_BLUE |
|
|
|
|
FOREGROUND_RED |
|
|
|
|
FOREGROUND_GREEN |
|
|
|
|
FOREGROUND_BLUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FillConsoleOutputAttribute(
|
|
|
|
hStdout, // screen buffer handle
|
|
|
|
wColor, // color to fill with
|
|
|
|
31, // number of cells to fill
|
|
|
|
pos, // first cell to write to
|
|
|
|
&numChars); // actual number written
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns TRUE if exiting
|
|
|
|
int ProcessKeys(int numEvents)
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
|
|
|
|
scrolled = ProcessCount-17;
|
|
|
|
|
2003-04-04 19:26:04 +00:00
|
|
|
unsigned char key = GetKeyPressed(numEvents);
|
|
|
|
if (key == VK_Q)
|
|
|
|
return TRUE;
|
|
|
|
else if (key == VK_K)
|
|
|
|
{
|
|
|
|
// user wants to kill some process, get his acknowledgement
|
|
|
|
DWORD pId;
|
|
|
|
COORD pos;
|
|
|
|
char lpStr[100];
|
|
|
|
|
|
|
|
pos.X = 2; pos.Y = 24;
|
|
|
|
strcpy(lpStr, "Are you sure you want to kill this process? (y/n)");
|
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &pId);
|
|
|
|
|
|
|
|
do {
|
|
|
|
GetNumberOfConsoleInputEvents(hStdin, &pId);
|
|
|
|
key = GetKeyPressed(pId);
|
|
|
|
} while (key == 0);
|
|
|
|
|
|
|
|
if (key == VK_Y)
|
|
|
|
{
|
|
|
|
HANDLE hProcess;
|
2003-04-09 16:31:28 +00:00
|
|
|
pId = pPerfData[selection+scrolled].ProcessId;
|
2003-04-04 19:26:04 +00:00
|
|
|
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pId);
|
|
|
|
|
|
|
|
if (hProcess)
|
|
|
|
{
|
|
|
|
if (!TerminateProcess(hProcess, 0))
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
strcpy(lpStr, "Unable to terminate this process... ");
|
2003-04-04 19:26:04 +00:00
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &pId);
|
|
|
|
Sleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
sprintf(lpStr, "Unable to terminate process %3d (unable to OpenProcess) ", pId);
|
2003-04-04 19:26:04 +00:00
|
|
|
WriteConsoleOutputCharacter(hStdout, lpStr, strlen(lpStr), pos, &pId);
|
|
|
|
Sleep(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (key == VK_UP)
|
|
|
|
{
|
|
|
|
if (selection > 0)
|
|
|
|
selection--;
|
|
|
|
else if ((selection == 0) && (scrolled > 0))
|
|
|
|
scrolled--;
|
|
|
|
}
|
|
|
|
else if (key == VK_DOWN)
|
|
|
|
{
|
|
|
|
if ((selection < MAX_PROC-1) && (selection < ProcessCount-1))
|
|
|
|
selection++;
|
|
|
|
else if ((selection == MAX_PROC-1) && (selection+scrolled < ProcessCount-1))
|
|
|
|
scrolled++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfInit()
|
|
|
|
{
|
|
|
|
// NtQuerySystemInformation = //(PROCNTQSI)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), //"NtQuerySystemInformation");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfDataRefresh()
|
|
|
|
{
|
|
|
|
LONG status;
|
|
|
|
ULONG ulSize;
|
|
|
|
LPBYTE pBuffer;
|
|
|
|
ULONG BufferSize;
|
|
|
|
ULONG Idx, Idx2;
|
|
|
|
HANDLE hProcess;
|
|
|
|
HANDLE hProcessToken;
|
2003-04-13 03:57:41 +00:00
|
|
|
PSYSTEM_PROCESSES pSPI;
|
2003-04-04 19:26:04 +00:00
|
|
|
PPERFDATA pPDOld;
|
|
|
|
TCHAR szTemp[MAX_PATH];
|
|
|
|
DWORD dwSize;
|
|
|
|
double CurrentKernelTime;
|
|
|
|
PSYSTEM_PROCESSORTIME_INFO SysProcessorTimeInfo;
|
2003-04-13 03:57:41 +00:00
|
|
|
SYSTEM_PERFORMANCE_INFO SysPerfInfo;
|
|
|
|
SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo;
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
#ifdef TIMES
|
|
|
|
// Get new system time
|
|
|
|
status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
|
|
|
|
if (status != NO_ERROR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get new CPU's idle time
|
|
|
|
status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
|
|
|
|
if (status != NO_ERROR)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
// Get processor information
|
|
|
|
SysProcessorTimeInfo = (PSYSTEM_PROCESSORTIME_INFO)malloc(sizeof(SYSTEM_PROCESSORTIME_INFO) * 1/*SystemBasicInfo.bKeNumberProcessors*/);
|
2003-04-13 03:57:41 +00:00
|
|
|
status = NtQuerySystemInformation(SystemProcessorTimes, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSORTIME_INFO) * 1/*SystemBasicInfo.bKeNumberProcessors*/, &ulSize);
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Get process information
|
2003-04-13 03:57:41 +00:00
|
|
|
PsaCaptureProcessesAndThreads((PSYSTEM_PROCESSES *)&pBuffer);
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
#ifdef TIMES
|
|
|
|
for (CurrentKernelTime=0, Idx=0; Idx<1/*SystemBasicInfo.bKeNumberProcessors*/; Idx++) {
|
|
|
|
CurrentKernelTime += Li2Double(SysProcessorTimeInfo[Idx].KernelTime);
|
|
|
|
CurrentKernelTime += Li2Double(SysProcessorTimeInfo[Idx].DpcTime);
|
|
|
|
CurrentKernelTime += Li2Double(SysProcessorTimeInfo[Idx].InterruptTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a first call - skip idle time calcs
|
|
|
|
if (liOldIdleTime.QuadPart != 0) {
|
|
|
|
// CurrentValue = NewValue - OldValue
|
|
|
|
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
|
|
|
|
dbKernelTime = CurrentKernelTime - OldKernelTime;
|
2003-04-13 03:57:41 +00:00
|
|
|
dbSystemTime = Li2Double(SysTimeInfo.CurrentTime) - Li2Double(liOldSystemTime);
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
// CurrentCpuIdle = IdleTime / SystemTime
|
|
|
|
dbIdleTime = dbIdleTime / dbSystemTime;
|
|
|
|
dbKernelTime = dbKernelTime / dbSystemTime;
|
|
|
|
|
|
|
|
// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
|
|
|
|
dbIdleTime = 100.0 - dbIdleTime * 100.0; /* / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5; */
|
|
|
|
dbKernelTime = 100.0 - dbKernelTime * 100.0; /* / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5; */
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store new CPU's idle and system time
|
|
|
|
liOldIdleTime = SysPerfInfo.liIdleTime;
|
2003-04-13 03:57:41 +00:00
|
|
|
liOldSystemTime = SysTimeInfo.CurrentTime;
|
2003-04-04 19:26:04 +00:00
|
|
|
OldKernelTime = CurrentKernelTime;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Determine the process count
|
2003-04-13 03:57:41 +00:00
|
|
|
// We loop through the data we got from PsaCaptureProcessesAndThreads
|
|
|
|
// and count how many structures there are (until PsaWalkNextProcess
|
|
|
|
// returns NULL)
|
2003-04-04 19:26:04 +00:00
|
|
|
ProcessCountOld = ProcessCount;
|
|
|
|
ProcessCount = 0;
|
2003-04-13 03:57:41 +00:00
|
|
|
pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESSES)pBuffer);
|
2003-04-04 19:26:04 +00:00
|
|
|
while (pSPI) {
|
|
|
|
ProcessCount++;
|
2003-04-13 03:57:41 +00:00
|
|
|
pSPI = PsaWalkNextProcess(pSPI);
|
2003-04-04 19:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now alloc a new PERFDATA array and fill in the data
|
|
|
|
if (pPerfDataOld) {
|
|
|
|
//delete[] pPerfDataOld;
|
|
|
|
free(pPerfDataOld);
|
|
|
|
}
|
|
|
|
pPerfDataOld = pPerfData;
|
|
|
|
//pPerfData = new PERFDATA[ProcessCount];
|
|
|
|
pPerfData = (PPERFDATA)malloc(sizeof(PERFDATA) * ProcessCount);
|
2003-04-13 03:57:41 +00:00
|
|
|
pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESSES)pBuffer);
|
2003-04-04 19:26:04 +00:00
|
|
|
for (Idx=0; Idx<ProcessCount; Idx++) {
|
|
|
|
// Get the old perf data for this process (if any)
|
|
|
|
// so that we can establish delta values
|
|
|
|
pPDOld = NULL;
|
|
|
|
for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
|
|
|
|
if (pPerfDataOld[Idx2].ProcessId == pSPI->ProcessId) {
|
|
|
|
pPDOld = &pPerfDataOld[Idx2];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out process perf data structure
|
|
|
|
memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
|
|
|
|
|
2003-04-13 03:57:41 +00:00
|
|
|
if (pSPI->ProcessName.Buffer)
|
|
|
|
wcsncpy(pPerfData[Idx].ImageName, pSPI->ProcessName.Buffer, pSPI->ProcessName.MaximumLength);
|
2003-04-04 19:26:04 +00:00
|
|
|
else
|
|
|
|
wcscpy(pPerfData[Idx].ImageName, L"System Idle Process");
|
|
|
|
|
|
|
|
pPerfData[Idx].ProcessId = pSPI->ProcessId;
|
|
|
|
|
|
|
|
if (pPDOld) {
|
|
|
|
#ifdef TIMES
|
|
|
|
double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
|
|
|
|
double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
|
|
|
|
double CpuTime = (CurTime - OldTime) / dbSystemTime;
|
|
|
|
CpuTime = CpuTime * 100.0; /* / (double)SystemBasicInfo.bKeNumberProcessors;// + 0.5;*/
|
|
|
|
|
|
|
|
pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
|
|
|
|
#else
|
|
|
|
pPerfData[Idx].CPUUsage = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
|
2003-04-13 03:57:41 +00:00
|
|
|
pPerfData[Idx].WorkingSetSizeBytes = pSPI->VmCounters.WorkingSetSize;
|
|
|
|
pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->VmCounters.PeakWorkingSetSize;
|
2003-04-04 19:26:04 +00:00
|
|
|
if (pPDOld)
|
2003-04-13 03:57:41 +00:00
|
|
|
pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->VmCounters.WorkingSetSize - (LONG)pPDOld->WorkingSetSizeBytes);
|
2003-04-04 19:26:04 +00:00
|
|
|
else
|
|
|
|
pPerfData[Idx].WorkingSetSizeDelta = 0;
|
2003-04-13 03:57:41 +00:00
|
|
|
pPerfData[Idx].PageFaultCount = pSPI->VmCounters.PageFaultCount;
|
2003-04-04 19:26:04 +00:00
|
|
|
if (pPDOld)
|
2003-04-13 03:57:41 +00:00
|
|
|
pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->VmCounters.PageFaultCount - (LONG)pPDOld->PageFaultCount);
|
2003-04-04 19:26:04 +00:00
|
|
|
else
|
|
|
|
pPerfData[Idx].PageFaultCountDelta = 0;
|
2003-04-13 03:57:41 +00:00
|
|
|
pPerfData[Idx].VirtualMemorySizeBytes = pSPI->VmCounters.VirtualSize;
|
|
|
|
pPerfData[Idx].PagedPoolUsagePages = pSPI->VmCounters.QuotaPagedPoolUsage;
|
|
|
|
pPerfData[Idx].NonPagedPoolUsagePages = pSPI->VmCounters.QuotaNonPagedPoolUsage;
|
2003-04-04 19:26:04 +00:00
|
|
|
pPerfData[Idx].BasePriority = pSPI->BasePriority;
|
|
|
|
pPerfData[Idx].HandleCount = pSPI->HandleCount;
|
|
|
|
pPerfData[Idx].ThreadCount = pSPI->ThreadCount;
|
2003-04-13 03:57:41 +00:00
|
|
|
//pPerfData[Idx].SessionId = pSPI->SessionId;
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
#ifdef EXTRA_INFO
|
|
|
|
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pSPI->ProcessId);
|
|
|
|
if (hProcess) {
|
|
|
|
if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
|
|
|
|
ImpersonateLoggedOnUser(hProcessToken);
|
|
|
|
memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
|
|
|
|
dwSize = MAX_PATH;
|
|
|
|
GetUserName(szTemp, &dwSize);
|
|
|
|
#ifndef UNICODE
|
|
|
|
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
|
|
|
|
/*
|
|
|
|
int MultiByteToWideChar(
|
|
|
|
UINT CodePage, // code page
|
|
|
|
DWORD dwFlags, // character-type options
|
|
|
|
LPCSTR lpMultiByteStr, // string to map
|
|
|
|
int cbMultiByte, // number of bytes in string
|
|
|
|
LPWSTR lpWideCharStr, // wide-character buffer
|
|
|
|
int cchWideChar // size of buffer
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
RevertToSelf();
|
|
|
|
CloseHandle(hProcessToken);
|
|
|
|
}
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef TIMES
|
|
|
|
pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
|
|
|
|
pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
|
|
|
|
#endif
|
2003-04-13 03:57:41 +00:00
|
|
|
pSPI = PsaWalkNextProcess(pSPI);
|
2003-04-04 19:26:04 +00:00
|
|
|
}
|
|
|
|
//delete[] pBuffer;
|
2003-04-13 03:57:41 +00:00
|
|
|
PsaFreeCapture(pBuffer);
|
2003-04-04 19:26:04 +00:00
|
|
|
|
|
|
|
free(SysProcessorTimeInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code partly taken from slw32tty.c from mc/slang
|
|
|
|
unsigned int GetKeyPressed(int events)
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
long key;
|
|
|
|
DWORD bytesRead;
|
2003-04-04 19:26:04 +00:00
|
|
|
INPUT_RECORD record;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0; i<events; i++)
|
|
|
|
{
|
|
|
|
if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
|
|
|
|
return record.Event.KeyEvent.wVirtualKeyCode;//.uChar.AsciiChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int *argc, char **argv)
|
|
|
|
{
|
|
|
|
GetInputOutputHandles();
|
|
|
|
|
|
|
|
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
printf("ctm: can't use console.");
|
2003-04-04 19:26:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetConsoleMode(hStdin, &inConMode) == 0)
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
printf("ctm: can't GetConsoleMode() for input console.");
|
2003-04-04 19:26:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetConsoleMode(hStdout, &outConMode) == 0)
|
|
|
|
{
|
2003-04-09 16:31:28 +00:00
|
|
|
printf("ctm: can't GetConsoleMode() for output console.");
|
2003-04-04 19:26:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
|
|
|
|
SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
|
|
|
|
|
|
|
|
PerfInit();
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
DWORD numEvents;
|
|
|
|
|
|
|
|
PerfDataRefresh();
|
|
|
|
DisplayScreen();
|
|
|
|
|
|
|
|
//WriteConsole(hStdin, " ", 1, &numEvents, NULL); // TODO: Make another way (this is ugly, I know)
|
|
|
|
GetNumberOfConsoleInputEvents(hStdin, &numEvents);
|
|
|
|
|
|
|
|
if (numEvents > 0)
|
|
|
|
{
|
|
|
|
if (ProcessKeys(numEvents) == TRUE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2003-04-09 16:31:28 +00:00
|
|
|
Sleep(40); // TODO: Should be done more efficient (might be another thread handling input/etc)*/
|
2003-04-04 19:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RestoreConsole();
|
|
|
|
return 0;
|
|
|
|
}
|