ReactOS  0.4.11-dev-765-g5e024bf
ctm.c File Reference
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <process.h>
#include <stdio.h>
#include <ndk/ntndk.h>
#include <epsapi/epsapi.h>
#include "ctm.h"
#include "resource.h"
Include dependency graph for ctm.c:

Go to the source code of this file.

Macros

#define WIN32_LEAN_AND_MEAN
 
#define WIN32_NO_STATUS
 
#define NTOS_MODE_USER
 
#define TIMES
 
#define NEW_CONSOLE
 

Functions

voidPsaiMalloc (SIZE_T size)
 
voidPsaiRealloc (void *ptr, SIZE_T size)
 
void PsaiFree (void *ptr)
 
unsigned int GetKeyPressed ()
 
void GetInputOutputHandles ()
 
void RestoreConsole ()
 
void DisplayScreen ()
 
int ProcessKeys (int numEvents)
 
void PerfInit ()
 
void PerfDataRefresh ()
 
unsigned int GetKeyPressed (int events)
 
int _tmain (int argc, char **argv)
 

Variables

HANDLE hStdin
 
HANDLE hStdout
 
HINSTANCE hInst
 
DWORD inConMode
 
DWORD outConMode
 
DWORD columnRightPositions [6]
 
TCHAR lpSeparator [80]
 
TCHAR lpHeader [80]
 
TCHAR lpMemUnit [3]
 
TCHAR lpIdleProcess [80]
 
TCHAR lpTitle [80]
 
TCHAR lpMenu [80]
 
TCHAR lpEmpty [80]
 
TCHAR KEY_QUIT
 
TCHAR KEY_KILL
 
TCHAR KEY_YES
 
TCHAR KEY_NO
 
int ProcPerScreen = 17
 
int ScreenLines =25
 
ULONG ProcessCountOld = 0
 
ULONG ProcessCount = 0
 
double dbIdleTime
 
double dbKernelTime
 
double dbSystemTime
 
LARGE_INTEGER liOldIdleTime = {{0,0}}
 
LARGE_INTEGER liOldKernelTime = {{0,0}}
 
LARGE_INTEGER liOldSystemTime = {{0,0}}
 
PPERFDATA pPerfDataOld = NULL
 
PPERFDATA pPerfData = NULL
 
int selection =0
 
int scrolled =0
 
int first = 0
 
SYSTEM_BASIC_INFORMATION SystemBasicInfo
 
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo
 

Macro Definition Documentation

#define NEW_CONSOLE

Definition at line 96 of file ctm.c.

#define NTOS_MODE_USER

Definition at line 45 of file ctm.c.

#define TIMES

Definition at line 53 of file ctm.c.

#define WIN32_LEAN_AND_MEAN

Definition at line 34 of file ctm.c.

#define WIN32_NO_STATUS

Definition at line 35 of file ctm.c.

Function Documentation

int _tmain ( int  argc,
char **  argv 
)

Definition at line 606 of file ctm.c.

607 {
608  int i;
609  TCHAR lpStr[80];
610 
611  for (i = 0; i < 80; i++)
612  lpEmpty[i] = lpHeader[i] = _T(' ');
613  lpEmpty[79] = _T('\0');
614 
615  /* Initialize global variables */
616  hInst = 0 /* FIXME: which value? [used with LoadString(hInst, ..., ..., ...)] */;
617 
618  if (LoadString(hInst, IDS_COLUMN_NUMBER, lpStr, 80))
619  {
620  columnRightPositions[0] = _tcslen(lpStr) + 3;
621  _tcsncpy(&lpHeader[2], lpStr, _tcslen(lpStr));
622  }
623  if (LoadString(hInst, IDS_COLUMN_IMAGENAME, lpStr, 80))
624  {
626  _tcsncpy(&lpHeader[columnRightPositions[0] + 2], lpStr, _tcslen(lpStr));
627  }
628  if (LoadString(hInst, IDS_COLUMN_PID, lpStr, 80))
629  {
631  _tcsncpy(&lpHeader[columnRightPositions[1] + 2], lpStr, _tcslen(lpStr));
632  }
633  if (LoadString(hInst, IDS_COLUMN_CPU, lpStr, 80))
634  {
636  _tcsncpy(&lpHeader[columnRightPositions[2] + 2], lpStr, _tcslen(lpStr));
637  }
638  if (LoadString(hInst, IDS_COLUMN_MEM, lpStr, 80))
639  {
641  _tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
642  }
643  if (LoadString(hInst, IDS_COLUMN_PF, lpStr, 80))
644  {
646  _tcsncpy(&lpHeader[columnRightPositions[4] + 2], lpStr, _tcslen(lpStr));
647  }
648 
649  for (i = 0; i < columnRightPositions[5]; i++)
650  lpSeparator[i] = _T('-');
651  lpHeader[0] = _T('|');
652  lpSeparator[0] = _T('+');
653  for (i = 0; i < 6; i++)
654  {
655  lpHeader[columnRightPositions[i]] = _T('|');
656  lpSeparator[columnRightPositions[i]] = _T('+');
657  }
658  lpSeparator[columnRightPositions[5] + 1] = _T('\0');
659  lpHeader[columnRightPositions[5] + 1] = _T('\0');
660 
661 
663  lpTitle[0] = _T('\0');
665  lpMemUnit[0] = _T('\0');
666  if (!LoadString(hInst, IDS_MENU, lpMenu, 80))
667  lpMenu[0] = _T('\0');
669  lpIdleProcess[0] = _T('\0');
670 
671  if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
672  KEY_QUIT = lpStr[0];
673  if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))
674  KEY_KILL = lpStr[0];
675  if (LoadString(hInst, IDS_YES, lpStr, 2))
676  KEY_YES = lpStr[0];
677  if (LoadString(hInst, IDS_NO, lpStr, 2))
678  KEY_NO = lpStr[0];
679 
681 
683  {
684  if (LoadString(hInst, IDS_CTM_GENERAL_ERR1, lpStr, 80))
685  _tprintf(lpStr);
686  return -1;
687  }
688 
689  if (GetConsoleMode(hStdin, &inConMode) == 0)
690  {
691  if (LoadString(hInst, IDS_CTM_GENERAL_ERR2, lpStr, 80))
692  _tprintf(lpStr);
693  return -1;
694  }
695 
696  if (GetConsoleMode(hStdout, &outConMode) == 0)
697  {
698  if (LoadString(hInst, IDS_CTM_GENERAL_ERR3, lpStr, 80))
699  _tprintf(lpStr);
700  return -1;
701  }
702 
703  SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
704  SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
705 
706  PerfInit();
707 
708  while (1)
709  {
710  DWORD numEvents;
711 
712  PerfDataRefresh();
713  DisplayScreen();
714 
715  /* WaitForSingleObject for console handles is not implemented in ROS */
718 
719  if (numEvents > 0)
720  {
721  if (ProcessKeys(numEvents) == TRUE)
722  break;
723  }
724  }
725 
726  RestoreConsole();
727  return 0;
728 }
DWORD columnRightPositions[6]
Definition: ctm.c:62
DWORD inConMode
Definition: ctm.c:59
#define _tprintf
Definition: tchar.h:506
TCHAR lpHeader[80]
Definition: ctm.c:64
#define TRUE
Definition: types.h:120
#define IDS_COLUMN_NUMBER
Definition: resource.h:23
#define IDS_MENU
Definition: resource.h:35
int ProcessKeys(int numEvents)
Definition: ctm.c:277
TCHAR lpMenu[80]
Definition: ctm.c:69
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
TCHAR lpSeparator[80]
Definition: ctm.c:63
#define IDS_COLUMN_MEM
Definition: resource.h:27
_TCHAR * _tcsncpy(_TCHAR *dst, const _TCHAR *src, size_t n)
Definition: tcsncpy.h:9
TCHAR KEY_KILL
Definition: ctm.c:72
TCHAR KEY_YES
Definition: ctm.c:73
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD outConMode
Definition: ctm.c:60
void PerfInit()
Definition: ctm.c:396
DWORD DWORD
Definition: winlogon.h:84
void RestoreConsole()
Definition: ctm.c:129
#define IDS_COLUMN_PF
Definition: resource.h:29
#define IDS_MENU_QUIT
Definition: resource.h:36
#define IDS_YES
Definition: resource.h:16
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define IDS_COLUMN_MEM_UNIT
Definition: resource.h:28
GLenum GLclampf GLint i
Definition: glfuncs.h:14
TCHAR lpEmpty[80]
Definition: ctm.c:70
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define IDS_NO
Definition: resource.h:17
TCHAR KEY_NO
Definition: ctm.c:73
HANDLE hStdin
Definition: ctm.c:55
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define IDS_CTM_GENERAL_ERR2
Definition: resource.h:44
TCHAR lpIdleProcess[80]
Definition: ctm.c:66
void DisplayScreen()
Definition: ctm.c:139
#define IDS_CTM_GENERAL_ERR3
Definition: resource.h:45
#define IDS_IDLE_PROCESS
Definition: resource.h:3
BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput, LPDWORD lpNumberOfEvents)
Definition: console.c:1637
void GetInputOutputHandles()
Definition: ctm.c:106
#define IDS_MENU_KILL_PROCESS
Definition: resource.h:37
#define IDS_APP_TITLE
Definition: resource.h:10
HANDLE hStdout
Definition: ctm.c:56
TCHAR lpMemUnit[3]
Definition: ctm.c:65
TCHAR KEY_QUIT
Definition: ctm.c:72
void PerfDataRefresh()
Definition: ctm.c:401
TCHAR lpTitle[80]
Definition: ctm.c:67
HINSTANCE hInst
Definition: ctm.c:57
#define IDS_COLUMN_PID
Definition: resource.h:25
#define IDS_COLUMN_IMAGENAME
Definition: resource.h:24
#define LoadString
Definition: winuser.h:5685
#define IDS_COLUMN_CPU
Definition: resource.h:26
#define IDS_CTM_GENERAL_ERR1
Definition: resource.h:43
void DisplayScreen ( )

Definition at line 139 of file ctm.c.

Referenced by _tmain().

140 {
141  COORD pos;
142  COORD size;
143  TCHAR lpStr[80];
144  DWORD numChars;
145  int lines;
146  int idx;
149  ScreenLines=size.Y;
151  if (first == 0)
152  {
153  // Header
154  pos.X = 1; pos.Y = 1;
156 
157  pos.X = 1; pos.Y = 2;
159 
160  pos.X = 1; pos.Y = 3;
162 
163  pos.X = 1; pos.Y = 4;
165 
166  // Footer
167  pos.X = 1; pos.Y = ScreenLines-2;
169 
170  // Menu
171  pos.X = 1; pos.Y = ScreenLines-1;
174 
175  first = 1;
176  }
177 
178  // Processess
179  lines = ProcessCount;
180  if (lines > ProcPerScreen)
181  lines = ProcPerScreen;
182  for (idx=0; idx<ProcPerScreen; idx++)
183  {
184  int len, i;
185  TCHAR lpNumber[5];
186  TCHAR lpPid[8];
187  TCHAR lpCpu[6];
188  TCHAR lpMemUsg[12];
189  TCHAR lpPageFaults[15];
190  WORD wColor;
191 
192  for (i = 0; i < 80; i++)
193  lpStr[i] = _T(' ');
194 
195  // data
196  if (idx < lines && scrolled + idx < ProcessCount)
197  {
198 
199  // number
200  _stprintf(lpNumber, _T("%3d"), idx+scrolled);
201  _tcsncpy(&lpStr[2], lpNumber, 3);
202 
203  // image name
204 #ifdef _UNICODE
205  len = wcslen(pPerfData[scrolled+idx].ImageName);
206 #else
208  imgName, MAX_PATH, NULL, NULL);
209  len = strlen(imgName);
210 #endif
211  if (len > columnRightPositions[1])
212  {
213  len = columnRightPositions[1];
214  }
215 #ifdef _UNICODE
216  wcsncpy(&lpStr[columnRightPositions[0]+3], pPerfData[scrolled+idx].ImageName, len);
217 #else
218  strncpy(&lpStr[columnRightPositions[0]+3], imgName, len);
219 #endif
220 
221  // PID
222  _stprintf(lpPid, _T("%6ld"), pPerfData[scrolled+idx].ProcessId);
223  _tcsncpy(&lpStr[columnRightPositions[2] - 6], lpPid, 6);
224 
225 #ifdef TIMES
226  // CPU
227  _stprintf(lpCpu, _T("%3d%%"), pPerfData[scrolled+idx].CPUUsage);
228  _tcsncpy(&lpStr[columnRightPositions[3] - 4], lpCpu, 4);
229 #endif
230 
231  // Mem usage
232  _stprintf(lpMemUsg, _T("%6ld %s"), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024, lpMemUnit);
233  _tcsncpy(&lpStr[columnRightPositions[4] - 9], lpMemUsg, 9);
234 
235  // Page Fault
236  _stprintf(lpPageFaults, _T("%12ld"), pPerfData[scrolled+idx].PageFaultCount);
237  _tcsncpy(&lpStr[columnRightPositions[5] - 12], lpPageFaults, 12);
238  }
239 
240  // columns
241  lpStr[0] = _T(' ');
242  lpStr[1] = _T('|');
243  for (i = 0; i < 6; i++)
244  lpStr[columnRightPositions[i] + 1] = _T('|');
245  pos.X = 0; pos.Y = 5+idx;
246  WriteConsoleOutputCharacter(hStdout, lpStr, 80, pos, &numChars);
247 
248  // Attributes now...
249  pos.X = columnRightPositions[0] + 1; pos.Y = 5+idx;
250  if (selection == idx)
251  {
252  wColor = BACKGROUND_GREEN |
256  }
257  else
258  {
259  wColor = BACKGROUND_BLUE |
263  }
264 
266  hStdout, // screen buffer handle
267  wColor, // color to fill with
268  columnRightPositions[1] - 4, // number of cells to fill
269  pos, // first cell to write to
270  &numChars); // actual number written
271  }
272 
273  return;
274 }
DWORD columnRightPositions[6]
Definition: ctm.c:62
TCHAR lpHeader[80]
Definition: ctm.c:64
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2697
unsigned short WORD
Definition: ntddk_ex.h:93
#define BACKGROUND_GREEN
Definition: wincon.h:44
BOOL WINAPI FillConsoleOutputAttribute(IN HANDLE hConsoleOutput, IN WORD wAttribute, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfAttrsWritten)
Definition: console.c:474
int ScreenLines
Definition: ctm.c:76
#define WideCharToMultiByte
Definition: compat.h:101
int scrolled
Definition: ctm.c:91
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
TCHAR lpMenu[80]
Definition: ctm.c:69
#define CP_ACP
Definition: compat.h:99
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
const GLint * first
Definition: glext.h:5794
TCHAR lpSeparator[80]
Definition: ctm.c:63
_TCHAR * _tcsncpy(_TCHAR *dst, const _TCHAR *src, size_t n)
Definition: tcsncpy.h:9
int selection
Definition: ctm.c:90
DWORD DWORD
Definition: winlogon.h:84
GLenum GLclampf GLint i
Definition: glfuncs.h:14
TCHAR lpEmpty[80]
Definition: ctm.c:70
unsigned int idx
Definition: utils.c:41
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
ULONG ProcessCount
Definition: ctm.c:78
BOOL WINAPI GetConsoleScreenBufferInfo(IN HANDLE hConsoleOutput, OUT PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
Definition: console.c:544
ULONG X
Definition: bl.h:1340
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
GLsizeiptr size
Definition: glext.h:5919
#define MAX_PATH
Definition: compat.h:26
PPERFDATA pPerfData
Definition: ctm.c:88
WORD wColor
static const char * ImageName
Definition: image.c:34
GLenum GLsizei len
Definition: glext.h:6722
Definition: bl.h:1338
#define BACKGROUND_BLUE
Definition: wincon.h:43
int ProcPerScreen
Definition: ctm.c:75
#define _stprintf
Definition: utility.h:124
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
#define FOREGROUND_GREEN
Definition: wincon.h:40
#define FOREGROUND_RED
Definition: wincon.h:41
HANDLE hStdout
Definition: ctm.c:56
TCHAR lpMemUnit[3]
Definition: ctm.c:65
TCHAR lpTitle[80]
Definition: ctm.c:67
#define FOREGROUND_BLUE
Definition: wincon.h:39
#define WriteConsoleOutputCharacter
Definition: wincon.h:777
ULONG Y
Definition: bl.h:1341
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo
Definition: ctm.c:95
void GetInputOutputHandles ( )

Definition at line 106 of file ctm.c.

Referenced by _tmain().

107 {
108 #ifdef NEW_CONSOLE
112 
113  if (SetConsoleActiveScreenBuffer(console) == FALSE)
114  {
117  }
118  else
119  {
120  hStdin = GetStdHandle(STD_INPUT_HANDLE);//console;
121  hStdout = console;
122  }
123 #else
126 #endif
127 }
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
Definition: console.c:1801
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:125
#define FALSE
Definition: types.h:117
#define GENERIC_WRITE
Definition: nt_native.h:90
#define STD_INPUT_HANDLE
Definition: winbase.h:264
HANDLE hStdin
Definition: ctm.c:55
#define GENERIC_READ
Definition: compat.h:124
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
DWORD *typedef HANDLE
Definition: winlogon.h:61
#define CONSOLE_TEXTMODE_BUFFER
Definition: wincon.h:59
HANDLE hStdout
Definition: ctm.c:56
HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode, CONST SECURITY_ATTRIBUTES *lpSecurityAttributes, DWORD dwFlags, LPVOID lpScreenBufferData)
Definition: console.c:2315
unsigned int GetKeyPressed ( )

Referenced by ProcessKeys().

unsigned int GetKeyPressed ( int  events)

Definition at line 585 of file ctm.c.

586 {
587  DWORD bytesRead;
589  int i;
590 
591 
592  for (i=0; i<events; i++)
593  {
594  if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
595  return 0;
596  }
597 
598  if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
599  return record.Event.KeyEvent.wVirtualKeyCode;//.uChar.AsciiChar;
600  }
601 
602  return 0;
603 }
DWORD DWORD
Definition: winlogon.h:84
WORD wVirtualKeyCode
Definition: wincon.h:227
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define ReadConsoleInput
Definition: wincon.h:766
#define KEY_EVENT
Definition: wincon.h:122
HANDLE hStdin
Definition: ctm.c:55
KEY_EVENT_RECORD KeyEvent
Definition: wincon.h:263
WORD EventType
Definition: wincon.h:261
HANDLE events[2]
Definition: event.c:4
union _INPUT_RECORD::@3146 Event
void PerfDataRefresh ( void  )

Definition at line 401 of file ctm.c.

Referenced by _tmain().

402 {
403  LONG status;
404  ULONG ulSize;
405  LPBYTE pBuffer;
406  ULONG Idx, Idx2;
408  PPERFDATA pPDOld;
409 #ifdef EXTRA_INFO
411  HANDLE hProcessToken;
412  TCHAR szTemp[MAX_PATH];
413  DWORD dwSize;
414 #endif
415 #ifdef TIMES
416  LARGE_INTEGER liCurrentKernelTime;
417  LARGE_INTEGER liCurrentIdleTime;
418  LARGE_INTEGER liCurrentTime;
419 #endif
420  PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo;
421  SYSTEM_TIMEOFDAY_INFORMATION SysTimeInfo;
422 
423 #ifdef TIMES
424  // Get new system time
425  status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
426  if (status != NO_ERROR)
427  return;
428 #endif
429  // Get processor information
432 
433 
434  // Get process information
436 
437 #ifdef TIMES
438  liCurrentKernelTime.QuadPart = 0;
439  liCurrentIdleTime.QuadPart = 0;
440  for (Idx=0; Idx<SystemBasicInfo.NumberOfProcessors; Idx++) {
441  liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].KernelTime.QuadPart;
442  liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].DpcTime.QuadPart;
443  liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].InterruptTime.QuadPart;
444  liCurrentIdleTime.QuadPart += SysProcessorTimeInfo[Idx].IdleTime.QuadPart;
445  }
446 
447  // If it's a first call - skip idle time calcs
448  if (liOldIdleTime.QuadPart != 0) {
449  // CurrentValue = NewValue - OldValue
450  liCurrentTime.QuadPart = liCurrentIdleTime.QuadPart - liOldIdleTime.QuadPart;
451  dbIdleTime = Li2Double(liCurrentTime);
452  liCurrentTime.QuadPart = liCurrentKernelTime.QuadPart - liOldKernelTime.QuadPart;
453  dbKernelTime = Li2Double(liCurrentTime);
454  liCurrentTime.QuadPart = SysTimeInfo.CurrentTime.QuadPart - liOldSystemTime.QuadPart;
455  dbSystemTime = Li2Double(liCurrentTime);
456 
457  // CurrentCpuIdle = IdleTime / SystemTime
460 
461  // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
462  dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
463  dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
464  }
465 
466  // Store new CPU's idle and system time
467  liOldIdleTime = liCurrentIdleTime;
468  liOldSystemTime = SysTimeInfo.CurrentTime;
469  liOldKernelTime = liCurrentKernelTime;
470 #endif
471 
472  // Determine the process count
473  // We loop through the data we got from PsaCaptureProcessesAndThreads
474  // and count how many structures there are (until PsaWalkNextProcess
475  // returns NULL)
477  ProcessCount = 0;
479  while (pSPI) {
480  ProcessCount++;
481  pSPI = PsaWalkNextProcess(pSPI);
482  }
483 
484  // Now alloc a new PERFDATA array and fill in the data
485  if (pPerfDataOld) {
487  }
491  for (Idx=0; Idx<ProcessCount; Idx++) {
492  // Get the old perf data for this process (if any)
493  // so that we can establish delta values
494  pPDOld = NULL;
495  for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
496  if (pPerfDataOld[Idx2].ProcessId == (ULONG)(pSPI->UniqueProcessId) &&
497  /* check also for the creation time, a new process may have an id of an old one */
499  pPDOld = &pPerfDataOld[Idx2];
500  break;
501  }
502  }
503 
504  // Clear out process perf data structure
505  memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
506 
507  if (pSPI->ImageName.Buffer) {
508  wcsncpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer, pSPI->ImageName.Length / sizeof(WCHAR));
509  pPerfData[Idx].ImageName[pSPI->ImageName.Length / sizeof(WCHAR)] = 0;
510  }
511  else
512  {
513 #ifdef _UNICODE
515 #else
517 #endif
518  }
519 
520  pPerfData[Idx].ProcessId = (ULONG)(pSPI->UniqueProcessId);
521  pPerfData[Idx].CreateTime = pSPI->CreateTime;
522 
523  if (pPDOld) {
524 #ifdef TIMES
525  double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
526  double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
527  double CpuTime = (CurTime - OldTime) / dbSystemTime;
528  CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; // + 0.5;
529 
530  pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
531 #else
532  pPerfData[Idx].CPUUsage = 0;
533 #endif
534  }
535 
539  if (pPDOld)
541  else
544  if (pPDOld)
546  else
551  pPerfData[Idx].BasePriority = pSPI->BasePriority;
552  pPerfData[Idx].HandleCount = pSPI->HandleCount;
554  //pPerfData[Idx].SessionId = pSPI->SessionId;
555 
556 #ifdef EXTRA_INFO
558  if (hProcess) {
559  if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
560  ImpersonateLoggedOnUser(hProcessToken);
561  memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
562  dwSize = MAX_PATH;
563  GetUserName(szTemp, &dwSize);
564 #ifndef UNICODE
565  MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
566 #endif
567  RevertToSelf();
568  CloseHandle(hProcessToken);
569  }
570  CloseHandle(hProcess);
571  }
572 #endif
573 #ifdef TIMES
576 #endif
577  pSPI = PsaWalkNextProcess(pSPI);
578  }
579  PsaFreeCapture(pBuffer);
580 
581  free(SysProcessorTimeInfo);
582 }
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2697
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
struct _PERFDATA * PPERFDATA
#define CloseHandle
Definition: compat.h:398
double dbSystemTime
Definition: ctm.c:82
double dbIdleTime
Definition: ctm.c:80
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:158
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL WINAPI RevertToSelf(VOID)
Definition: security.c:1483
#define GetUserName
Definition: winbase.h:3665
#define CP_ACP
Definition: compat.h:99
unsigned char * LPBYTE
Definition: typedefs.h:52
#define free
Definition: debug_ros.c:5
#define MB_PRECOMPOSED
Definition: winnls.h:269
LARGE_INTEGER liOldSystemTime
Definition: ctm.c:85
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define TOKEN_IMPERSONATE
Definition: setypes.h:873
PSYSTEM_PROCESSES FASTCALL PsaWalkNextProcess(IN PSYSTEM_PROCESSES CurrentProcess)
ULONG CPUUsage
Definition: perfdata.h:37
#define WCHAR
Definition: msvc.h:43
#define NO_ERROR
Definition: dderror.h:5
SYSTEM_BASIC_INFORMATION SystemBasicInfo
Definition: ctm.c:93
LARGE_INTEGER CreateTime
Definition: extypes.h:887
DWORD DWORD
Definition: winlogon.h:84
PPERFDATA pPerfDataOld
Definition: ctm.c:87
LARGE_INTEGER CurrentTime
Definition: extypes.h:846
#define FALSE
Definition: types.h:117
long LONG
Definition: pedump.c:60
LARGE_INTEGER CreateTime
Definition: ctm.h:54
ULONG NonPagedPoolUsagePages
Definition: perfdata.h:46
WCHAR ImageName[MAX_PATH]
Definition: perfdata.h:33
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:80
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
ULONG WorkingSetSizeDelta
Definition: perfdata.h:41
ULONG ProcessCount
Definition: ctm.c:78
LARGE_INTEGER liOldKernelTime
Definition: ctm.c:84
PSYSTEM_PROCESSES FASTCALL PsaWalkFirstProcess(IN PSYSTEM_PROCESSES ProcessesAndThreads)
ULONG HandleCount
Definition: perfdata.h:48
char TCHAR
Definition: xmlstorage.h:189
#define SystemTimeInformation
Definition: ctm.h:57
#define TOKEN_QUERY
Definition: setypes.h:874
ULONG PageFaultCountDelta
Definition: perfdata.h:43
VOID NTAPI PsaFreeCapture(IN PVOID Capture)
Definition: processes.c:188
#define MAX_PATH
Definition: compat.h:26
TCHAR lpIdleProcess[80]
Definition: ctm.c:66
ULONG BasePriority
Definition: perfdata.h:47
struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION * PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
NTSTATUS NTAPI PsaCaptureProcessesAndThreads(OUT PSYSTEM_PROCESSES *ProcessesAndThreads)
PPERFDATA pPerfData
Definition: ctm.c:88
UNICODE_STRING ImageName
Definition: extypes.h:890
#define TOKEN_DUPLICATE
Definition: setypes.h:872
ULONG WorkingSetSizeBytes
Definition: perfdata.h:39
static const char * ImageName
Definition: image.c:34
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
LARGE_INTEGER CPUTime
Definition: perfdata.h:38
ULONG ThreadCount
Definition: perfdata.h:49
ULONG ProcessCountOld
Definition: ctm.c:77
HANDLE ProcessId
Definition: perfdata.h:34
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1257
DWORD *typedef HANDLE
Definition: winlogon.h:61
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
LARGE_INTEGER KernelTime
Definition: extypes.h:889
ULONG PeakWorkingSetSizeBytes
Definition: perfdata.h:40
ULONG PageFaultCount
Definition: perfdata.h:42
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:292
double dbKernelTime
Definition: ctm.c:81
#define MultiByteToWideChar
Definition: compat.h:100
#define Li2Double(x)
Definition: perfdata.h:29
unsigned int ULONG
Definition: retypes.h:1
LARGE_INTEGER UserTime
Definition: extypes.h:888
#define malloc
Definition: debug_ros.c:4
_Check_return_ long __cdecl labs(_In_ long x)
LARGE_INTEGER KernelTime
Definition: perfdata.h:55
ULONG VirtualMemorySizeBytes
Definition: perfdata.h:44
LARGE_INTEGER liOldIdleTime
Definition: ctm.c:83
#define memset(x, y, z)
Definition: compat.h:39
static SERVICE_STATUS status
Definition: service.c:31
LARGE_INTEGER UserTime
Definition: perfdata.h:54
LONGLONG QuadPart
Definition: typedefs.h:112
ULONG PagedPoolUsagePages
Definition: perfdata.h:45
DWORD dwSize
Definition: wglext.h:734
void PerfInit ( )

Definition at line 396 of file ctm.c.

Referenced by _tmain().

397 {
399 }
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
SYSTEM_BASIC_INFORMATION SystemBasicInfo
Definition: ctm.c:93
#define SystemBasicInformation
Definition: xboxvmp.h:35
int ProcessKeys ( int  numEvents)

Definition at line 277 of file ctm.c.

Referenced by _tmain().

278 {
279  DWORD numChars;
280  TCHAR key;
281  if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
282  scrolled = ProcessCount-17;
283 
284  key = GetKeyPressed(numEvents);
285  if (key == KEY_QUIT)
286  return TRUE;
287  else if (key == KEY_KILL)
288  {
289  // user wants to kill some process, get his acknowledgement
290  DWORD pId;
291  COORD pos;
292  TCHAR lpStr[100];
293 
294  pos.X = 1; pos.Y =ScreenLines-1;
295  if (LoadString(hInst, IDS_KILL_PROCESS, lpStr, 100))
296  WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
297 
298  do {
300  key = GetKeyPressed(pId);
301  } while (key != KEY_YES && key != KEY_NO);
302 
303  if (key == KEY_YES)
304  {
307  hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pId);
308 
309  if (hProcess)
310  {
311  if (!TerminateProcess(hProcess, 0))
312  {
313  if (LoadString(hInst, IDS_KILL_PROCESS_ERR1, lpStr, 80))
314  {
316  WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
317  }
318  Sleep(1000);
319  }
320 
321  CloseHandle(hProcess);
322  }
323  else
324  {
325  if (LoadString(hInst, IDS_KILL_PROCESS_ERR2, lpStr, 80))
326  {
328  _stprintf(lpStr, lpStr, pId);
329  WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
330  }
331  Sleep(1000);
332  }
333  }
334 
335  first = 0;
336  }
337  else if (key == VK_UP)
338  {
339  if (selection > 0)
340  selection--;
341  else if ((selection == 0) && (scrolled > 0))
342  scrolled--;
343  }
344  else if (key == VK_DOWN)
345  {
346  if ((selection < ProcPerScreen-1) && (selection < ProcessCount-1))
347  selection++;
348  else if ((selection == ProcPerScreen-1) && (selection+scrolled < ProcessCount-1))
349  scrolled++;
350  }
351  else if (key == VK_PRIOR)
352  {
353  if (scrolled>ProcPerScreen-1)
355  else
356  {
357  scrolled=0; //First
358  selection=0;
359  }
360  //selection=0;
361  }
362  else if (key == VK_NEXT)
363  {
366  {
368  selection=ProcPerScreen-1;
369  }
370 
371  //selection=ProcPerScreen-1;
372  if (ProcessCount<=ProcPerScreen) //If there are less process than fits on the screen
373  {
374  scrolled=0;
376  }
377  }
378  else if (key == VK_HOME)
379  {
380  selection=0;
381  scrolled=0;
382  }
383  else if (key == VK_END)
384  {
387  if (ProcessCount<=ProcPerScreen) //If there are less process than fits on the screen
388  {
389  scrolled=0;
391  }
392  }
393  return FALSE;
394 }
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define IDS_KILL_PROCESS_ERR1
Definition: resource.h:40
int ScreenLines
Definition: ctm.c:76
int scrolled
Definition: ctm.c:91
const GLint * first
Definition: glext.h:5794
TCHAR KEY_KILL
Definition: ctm.c:72
TCHAR KEY_YES
Definition: ctm.c:73
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define VK_DOWN
Definition: winuser.h:2181
#define VK_PRIOR
Definition: winuser.h:2174
int selection
Definition: ctm.c:90
DWORD DWORD
Definition: winlogon.h:84
#define VK_NEXT
Definition: winuser.h:2175
#define VK_HOME
Definition: winuser.h:2177
TCHAR lpEmpty[80]
Definition: ctm.c:70
#define VK_UP
Definition: winuser.h:2179
#define FALSE
Definition: types.h:117
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define IDS_KILL_PROCESS_ERR2
Definition: resource.h:41
ULONG ProcessCount
Definition: ctm.c:78
TCHAR KEY_NO
Definition: ctm.c:73
unsigned int GetKeyPressed()
HANDLE hStdin
Definition: ctm.c:55
ULONG X
Definition: bl.h:1340
char TCHAR
Definition: xmlstorage.h:189
#define IDS_KILL_PROCESS
Definition: resource.h:39
PPERFDATA pPerfData
Definition: ctm.c:88
BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput, LPDWORD lpNumberOfEvents)
Definition: console.c:1637
HKEY key
Definition: reg.c:42
Definition: bl.h:1338
int ProcPerScreen
Definition: ctm.c:75
#define _stprintf
Definition: utility.h:124
HANDLE ProcessId
Definition: perfdata.h:34
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1257
DWORD *typedef HANDLE
Definition: winlogon.h:61
#define PROCESS_TERMINATE
Definition: pstypes.h:149
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1562
HANDLE hStdout
Definition: ctm.c:56
TCHAR KEY_QUIT
Definition: ctm.c:72
HINSTANCE hInst
Definition: ctm.c:57
#define WriteConsoleOutputCharacter
Definition: wincon.h:777
ULONG Y
Definition: bl.h:1341
#define VK_END
Definition: winuser.h:2176
#define LoadString
Definition: winuser.h:5685
void PsaiFree ( void ptr)

Definition at line 101 of file ctm.c.

Referenced by PsaCaptureProcessesAndThreads(), PsaCaptureSystemModules(), and PsaFreeCapture().

101 { free(ptr); }
#define free
Definition: debug_ros.c:5
void* PsaiMalloc ( SIZE_T  size)

Definition at line 99 of file ctm.c.

Referenced by PsaCaptureProcessesAndThreads(), and PsaCaptureSystemModules().

99 { return malloc(size); }
GLsizeiptr size
Definition: glext.h:5919
#define malloc
Definition: debug_ros.c:4
void* PsaiRealloc ( void ptr,
SIZE_T  size 
)

Definition at line 100 of file ctm.c.

100 { return realloc(ptr, size); }
#define realloc
Definition: debug_ros.c:6
GLsizeiptr size
Definition: glext.h:5919
void RestoreConsole ( )

Definition at line 129 of file ctm.c.

Referenced by _tmain().

130 {
133 
134 #ifdef NEW_CONSOLE
136 #endif
137 }
DWORD inConMode
Definition: ctm.c:59
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
Definition: console.c:1801
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
DWORD outConMode
Definition: ctm.c:60
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
HANDLE hStdin
Definition: ctm.c:55
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
HANDLE hStdout
Definition: ctm.c:56

Variable Documentation

DWORD columnRightPositions[6]

Definition at line 62 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

double dbIdleTime

Definition at line 80 of file ctm.c.

Referenced by PerfDataRefresh().

double dbKernelTime

Definition at line 81 of file ctm.c.

Referenced by PerfDataRefresh().

double dbSystemTime

Definition at line 82 of file ctm.c.

Referenced by PerfDataRefresh().

int first = 0

Definition at line 92 of file ctm.c.

HINSTANCE hInst

Definition at line 57 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

HANDLE hStdin
DWORD inConMode

Definition at line 59 of file ctm.c.

Referenced by _tmain(), and RestoreConsole().

TCHAR KEY_KILL

Definition at line 72 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

TCHAR KEY_NO

Definition at line 73 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

TCHAR KEY_QUIT

Definition at line 72 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

TCHAR KEY_YES

Definition at line 73 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

LARGE_INTEGER liOldIdleTime = {{0,0}}

Definition at line 83 of file ctm.c.

LARGE_INTEGER liOldKernelTime = {{0,0}}

Definition at line 84 of file ctm.c.

LARGE_INTEGER liOldSystemTime = {{0,0}}

Definition at line 85 of file ctm.c.

TCHAR lpEmpty[80]

Definition at line 70 of file ctm.c.

Referenced by _tmain(), DisplayScreen(), and ProcessKeys().

TCHAR lpHeader

Definition at line 64 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

TCHAR lpIdleProcess[80]

Definition at line 66 of file ctm.c.

Referenced by _tmain(), and PerfDataRefresh().

TCHAR lpMemUnit[3]

Definition at line 65 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

TCHAR lpMenu[80]

Definition at line 69 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

TCHAR lpSeparator[80]

Definition at line 63 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

TCHAR lpTitle[80]
DWORD outConMode

Definition at line 60 of file ctm.c.

Referenced by _tmain(), and RestoreConsole().

PPERFDATA pPerfData = NULL

Definition at line 88 of file ctm.c.

Referenced by PerfDataRefresh().

PPERFDATA pPerfDataOld = NULL

Definition at line 87 of file ctm.c.

ULONG ProcessCount = 0

Definition at line 78 of file ctm.c.

Referenced by DisplayScreen(), PerfDataRefresh(), and ProcessKeys().

ULONG ProcessCountOld = 0

Definition at line 77 of file ctm.c.

Referenced by PerfDataRefresh().

int ProcPerScreen = 17

Definition at line 75 of file ctm.c.

Referenced by DisplayScreen(), and ProcessKeys().

CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo

Definition at line 95 of file ctm.c.

int ScreenLines =25

Definition at line 76 of file ctm.c.

Referenced by ConWritePaging(), DisplayScreen(), and ProcessKeys().

int scrolled =0

Definition at line 91 of file ctm.c.

Referenced by DisplayScreen(), and ProcessKeys().

SYSTEM_BASIC_INFORMATION SystemBasicInfo

Definition at line 93 of file ctm.c.