ReactOS 0.4.15-dev-7834-g00c4b3d
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 lpSeparatorUp [80]
 
TCHAR lpSeparatorDown [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

◆ NEW_CONSOLE

#define NEW_CONSOLE

Definition at line 98 of file ctm.c.

◆ NTOS_MODE_USER

#define NTOS_MODE_USER

Definition at line 45 of file ctm.c.

◆ TIMES

#define TIMES

Definition at line 53 of file ctm.c.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 34 of file ctm.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 35 of file ctm.c.

Function Documentation

◆ _tmain()

int _tmain ( int  argc,
char **  argv 
)

Definition at line 617 of file ctm.c.

618{
619 int i;
620 TCHAR lpStr[80];
621
622 for (i = 0; i < 80; i++)
623 lpEmpty[i] = lpHeader[i] = _T(' ');
624 lpEmpty[79] = _T('\0');
625
626 /* Initialize global variables */
627 hInst = 0 /* FIXME: which value? [used with LoadString(hInst, ..., ..., ...)] */;
628
629 if (LoadString(hInst, IDS_COLUMN_NUMBER, lpStr, 80))
630 {
631 columnRightPositions[0] = _tcslen(lpStr) + 3;
632 _tcsncpy(&lpHeader[2], lpStr, _tcslen(lpStr));
633 }
634 if (LoadString(hInst, IDS_COLUMN_IMAGENAME, lpStr, 80))
635 {
637 _tcsncpy(&lpHeader[columnRightPositions[0] + 2], lpStr, _tcslen(lpStr));
638 }
639 if (LoadString(hInst, IDS_COLUMN_PID, lpStr, 80))
640 {
642 _tcsncpy(&lpHeader[columnRightPositions[1] + 2], lpStr, _tcslen(lpStr));
643 }
644 if (LoadString(hInst, IDS_COLUMN_CPU, lpStr, 80))
645 {
647 _tcsncpy(&lpHeader[columnRightPositions[2] + 2], lpStr, _tcslen(lpStr));
648 }
649 if (LoadString(hInst, IDS_COLUMN_MEM, lpStr, 80))
650 {
652 _tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
653 }
654 if (LoadString(hInst, IDS_COLUMN_PF, lpStr, 80))
655 {
657 _tcsncpy(&lpHeader[columnRightPositions[4] + 2], lpStr, _tcslen(lpStr));
658 }
659
660 for (i = 0; i < columnRightPositions[5]; i++)
661 {
662 lpSeparator[i] = _T('-');
663 lpSeparatorUp[i] = _T('^');
664 lpSeparatorDown[i] = _T('v');
665 }
666 lpHeader[0] = _T('|');
667 lpSeparator[0] = _T('+');
668 lpSeparatorUp[0] = _T('^');
669 lpSeparatorDown[0] = _T('v');
670 for (i = 0; i < 6; i++)
671 {
676 }
677 lpHeader[columnRightPositions[5] + 1] = _T('\0');
678 lpSeparator[columnRightPositions[5] + 1] = _T('\0');
679 lpSeparatorUp[columnRightPositions[5] + 1] = _T('\0');
681
682
684 lpTitle[0] = _T('\0');
686 lpMemUnit[0] = _T('\0');
687 if (!LoadString(hInst, IDS_MENU, lpMenu, 80))
688 lpMenu[0] = _T('\0');
690 lpIdleProcess[0] = _T('\0');
691
692 if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
693 KEY_QUIT = lpStr[0];
694 if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))
695 KEY_KILL = lpStr[0];
696 if (LoadString(hInst, IDS_YES, lpStr, 2))
697 KEY_YES = lpStr[0];
698 if (LoadString(hInst, IDS_NO, lpStr, 2))
699 KEY_NO = lpStr[0];
700
702
704 {
705 if (LoadString(hInst, IDS_CTM_GENERAL_ERR1, lpStr, 80))
706 _tprintf(lpStr);
707 return -1;
708 }
709
710 if (GetConsoleMode(hStdin, &inConMode) == 0)
711 {
712 if (LoadString(hInst, IDS_CTM_GENERAL_ERR2, lpStr, 80))
713 _tprintf(lpStr);
714 return -1;
715 }
716
718 {
719 if (LoadString(hInst, IDS_CTM_GENERAL_ERR3, lpStr, 80))
720 _tprintf(lpStr);
721 return -1;
722 }
723
724 SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
725 SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
726
727 PerfInit();
728
729 while (1)
730 {
731 DWORD numEvents;
732
735
736 /* WaitForSingleObject for console handles is not implemented in ROS */
739
740 if (numEvents > 0)
741 {
742 if (ProcessKeys(numEvents) == TRUE)
743 break;
744 first = 0;
745 }
746 }
747
749 return 0;
750}
#define IDS_YES
Definition: resource.h:16
#define IDS_NO
Definition: resource.h:17
#define IDS_APP_TITLE
Definition: resource.h:10
#define IDS_IDLE_PROCESS
Definition: resource.h:10
HANDLE hStdout
Definition: ctm.c:56
TCHAR lpIdleProcess[80]
Definition: ctm.c:68
TCHAR lpHeader[80]
Definition: ctm.c:66
TCHAR KEY_QUIT
Definition: ctm.c:74
DWORD columnRightPositions[6]
Definition: ctm.c:62
HANDLE hStdin
Definition: ctm.c:55
TCHAR lpTitle[80]
Definition: ctm.c:69
DWORD outConMode
Definition: ctm.c:60
HINSTANCE hInst
Definition: ctm.c:57
TCHAR lpSeparator[80]
Definition: ctm.c:63
void PerfInit()
Definition: ctm.c:405
TCHAR lpMemUnit[3]
Definition: ctm.c:67
TCHAR lpEmpty[80]
Definition: ctm.c:72
TCHAR lpSeparatorDown[80]
Definition: ctm.c:65
TCHAR KEY_KILL
Definition: ctm.c:74
TCHAR lpSeparatorUp[80]
Definition: ctm.c:64
int ProcessKeys(int numEvents)
Definition: ctm.c:286
void GetInputOutputHandles()
Definition: ctm.c:108
void DisplayScreen()
Definition: ctm.c:141
DWORD inConMode
Definition: ctm.c:59
void PerfDataRefresh()
Definition: ctm.c:410
void RestoreConsole()
Definition: ctm.c:131
TCHAR lpMenu[80]
Definition: ctm.c:71
TCHAR KEY_NO
Definition: ctm.c:75
TCHAR KEY_YES
Definition: ctm.c:75
#define TRUE
Definition: types.h:120
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1606
BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput, LPDWORD lpNumberOfEvents)
Definition: console.c:1635
unsigned long DWORD
Definition: ntddk_ex.h:95
const GLint * first
Definition: glext.h:5794
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _tcsncpy
Definition: tchar.h:1410
#define _tprintf
Definition: tchar.h:506
#define IDS_CTM_GENERAL_ERR2
Definition: resource.h:44
#define IDS_CTM_GENERAL_ERR3
Definition: resource.h:45
#define IDS_MENU_KILL_PROCESS
Definition: resource.h:37
#define IDS_CTM_GENERAL_ERR1
Definition: resource.h:43
#define IDS_COLUMN_PF
Definition: resource.h:29
#define IDS_COLUMN_CPU
Definition: resource.h:26
#define IDS_COLUMN_IMAGENAME
Definition: resource.h:24
#define IDS_MENU
Definition: resource.h:35
#define IDS_COLUMN_MEM_UNIT
Definition: resource.h:28
#define IDS_COLUMN_MEM
Definition: resource.h:27
#define IDS_COLUMN_PID
Definition: resource.h:25
#define IDS_MENU_QUIT
Definition: resource.h:36
#define IDS_COLUMN_NUMBER
Definition: resource.h:23
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define _T(x)
Definition: vfdio.h:22
#define LoadString
Definition: winuser.h:5819
char TCHAR
Definition: xmlstorage.h:189
#define _tcslen
Definition: xmlstorage.h:198

◆ DisplayScreen()

void DisplayScreen ( )

Definition at line 141 of file ctm.c.

142{
143 COORD pos;
144 COORD size;
145 TCHAR lpStr[80];
147 int lines;
148 int idx;
153 if (first == 0)
154 {
155 // Header
156 pos.X = 1; pos.Y = 1;
158
159 pos.X = 1; pos.Y = 2;
161
162 pos.X = 1; pos.Y = 3;
164
165 pos.X = 1; pos.Y = 4;
166 if (scrolled)
168 else
170
171 // Footer
172 pos.X = 1; pos.Y = ScreenLines-2;
175 else
177
178 // Menu
179 pos.X = 1; pos.Y = ScreenLines-1;
182
183 first = 1;
184 }
185
186 // Processess
188 if (lines > ProcPerScreen)
190 for (idx=0; idx<ProcPerScreen; idx++)
191 {
192 int i;
193 SIZE_T len;
194 TCHAR lpNumber[5];
195 TCHAR lpPid[8];
196 TCHAR lpCpu[6];
197 TCHAR lpMemUsg[12];
198 TCHAR lpPageFaults[15];
199 WORD wColor;
200
201 for (i = 0; i < 80; i++)
202 lpStr[i] = _T(' ');
203
204 // data
205 if (idx < lines && scrolled + idx < ProcessCount)
206 {
207
208 // number
209 _stprintf(lpNumber, _T("%3d"), idx+scrolled);
210 _tcsncpy(&lpStr[2], lpNumber, 3);
211
212 // image name
213#ifdef _UNICODE
215#else
217 imgName, MAX_PATH, NULL, NULL);
218 len = strlen(imgName);
219#endif
220 if (len > columnRightPositions[1])
221 {
223 }
224#ifdef _UNICODE
226#else
227 strncpy(&lpStr[columnRightPositions[0]+3], imgName, len);
228#endif
229
230 // PID
231 _stprintf(lpPid, _T("%6ld"), pPerfData[scrolled+idx].ProcessId);
232 _tcsncpy(&lpStr[columnRightPositions[2] - 6], lpPid, 6);
233
234#ifdef TIMES
235 // CPU
236 _stprintf(lpCpu, _T("%3d%%"), pPerfData[scrolled+idx].CPUUsage);
237 _tcsncpy(&lpStr[columnRightPositions[3] - 4], lpCpu, 4);
238#endif
239
240 // Mem usage
241 _stprintf(lpMemUsg, _T("%6ld %s"), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024, lpMemUnit);
242 _tcsncpy(&lpStr[columnRightPositions[4] - 9], lpMemUsg, 9);
243
244 // Page Fault
245 _stprintf(lpPageFaults, _T("%12ld"), pPerfData[scrolled+idx].PageFaultCount);
246 _tcsncpy(&lpStr[columnRightPositions[5] - 12], lpPageFaults, 12);
247 }
248
249 // columns
250 lpStr[0] = _T(' ');
251 lpStr[1] = _T('|');
252 for (i = 0; i < 6; i++)
253 lpStr[columnRightPositions[i] + 1] = _T('|');
254 pos.X = 0; pos.Y = 5+idx;
256
257 // Attributes now...
258 pos.X = columnRightPositions[0] + 1; pos.Y = 5+idx;
259 if (selection == idx)
260 {
265 }
266 else
267 {
272 }
273
275 hStdout, // screen buffer handle
276 wColor, // color to fill with
277 columnRightPositions[1] - 4, // number of cells to fill
278 pos, // first cell to write to
279 &numChars); // actual number written
280 }
281
282 return;
283}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
BOOL WINAPI FillConsoleOutputAttribute(IN HANDLE hConsoleOutput, IN WORD wAttribute, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfAttrsWritten)
Definition: console.c:525
BOOL WINAPI GetConsoleScreenBufferInfo(IN HANDLE hConsoleOutput, OUT PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
Definition: console.c:595
WORD wColor
#define BACKGROUND_GREEN
Definition: blue.h:66
#define BACKGROUND_BLUE
Definition: blue.h:65
#define FOREGROUND_GREEN
Definition: blue.h:62
#define FOREGROUND_BLUE
Definition: blue.h:61
#define FOREGROUND_RED
Definition: blue.h:63
int ScreenLines
Definition: ctm.c:78
PPERFDATA pPerfData
Definition: ctm.c:90
int scrolled
Definition: ctm.c:93
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo
Definition: ctm.c:97
int ProcPerScreen
Definition: ctm.c:77
ULONG ProcessCount
Definition: ctm.c:80
int selection
Definition: ctm.c:92
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
#define CP_ACP
Definition: compat.h:109
#define MAX_PATH
Definition: compat.h:34
#define WideCharToMultiByte
Definition: compat.h:111
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2711
unsigned short WORD
Definition: ntddk_ex.h:93
USHORT numChars
GLsizeiptr size
Definition: glext.h:5919
GLenum GLsizei len
Definition: glext.h:6722
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define _stprintf
Definition: utility.h:124
static const char * ImageName
Definition: image.c:34
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
Definition: bl.h:1338
eMaj lines
Definition: tritemp.h:206
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define WriteConsoleOutputCharacter
Definition: wincon.h:789

Referenced by _tmain().

◆ GetInputOutputHandles()

void GetInputOutputHandles ( )

Definition at line 108 of file ctm.c.

109{
110#ifdef NEW_CONSOLE
114
115 if (SetConsoleActiveScreenBuffer(console) == FALSE)
116 {
119 }
120 else
121 {
123 hStdout = console;
124 }
125#else
128#endif
129}
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define FALSE
Definition: types.h:117
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode, CONST SECURITY_ATTRIBUTES *lpSecurityAttributes, DWORD dwFlags, LPVOID lpScreenBufferData)
Definition: console.c:2313
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
Definition: console.c:1799
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define CONSOLE_TEXTMODE_BUFFER
Definition: wincon.h:62

Referenced by _tmain().

◆ GetKeyPressed() [1/2]

unsigned int GetKeyPressed ( )

Referenced by ProcessKeys().

◆ GetKeyPressed() [2/2]

unsigned int GetKeyPressed ( int  events)

Definition at line 596 of file ctm.c.

597{
598 DWORD bytesRead;
600 int i;
601
602
603 for (i=0; i<events; i++)
604 {
605 if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
606 return 0;
607 }
608
609 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
610 return record.Event.KeyEvent.wVirtualKeyCode;//.uChar.AsciiChar;
611 }
612
613 return 0;
614}
HANDLE events[2]
Definition: event.c:4
#define ReadConsoleInput
Definition: wincon.h:778
#define KEY_EVENT
Definition: wincon.h:128

◆ PerfDataRefresh()

void PerfDataRefresh ( void  )

Definition at line 410 of file ctm.c.

411{
412 LONG status;
413 ULONG ulSize;
415 ULONG Idx, Idx2;
417 PPERFDATA pPDOld;
418#ifdef EXTRA_INFO
420 HANDLE hProcessToken;
421 TCHAR szTemp[MAX_PATH];
423#endif
424#ifdef TIMES
425 LARGE_INTEGER liCurrentKernelTime;
426 LARGE_INTEGER liCurrentIdleTime;
427 LARGE_INTEGER liCurrentTime;
428#endif
431
432#ifdef TIMES
433 // Get new system time
434 status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
435 if (status != NO_ERROR)
436 return;
437#endif
438 // Get processor information
441
442
443 // Get process information
445
446#ifdef TIMES
447 liCurrentKernelTime.QuadPart = 0;
448 liCurrentIdleTime.QuadPart = 0;
449 for (Idx=0; Idx<SystemBasicInfo.NumberOfProcessors; Idx++) {
450 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].KernelTime.QuadPart;
451 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].DpcTime.QuadPart;
452 liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].InterruptTime.QuadPart;
453 liCurrentIdleTime.QuadPart += SysProcessorTimeInfo[Idx].IdleTime.QuadPart;
454 }
455
456 // If it's a first call - skip idle time calcs
457 if (liOldIdleTime.QuadPart != 0) {
458 // CurrentValue = NewValue - OldValue
459 liCurrentTime.QuadPart = liCurrentIdleTime.QuadPart - liOldIdleTime.QuadPart;
460 dbIdleTime = Li2Double(liCurrentTime);
461 liCurrentTime.QuadPart = liCurrentKernelTime.QuadPart - liOldKernelTime.QuadPart;
462 dbKernelTime = Li2Double(liCurrentTime);
463 liCurrentTime.QuadPart = SysTimeInfo.CurrentTime.QuadPart - liOldSystemTime.QuadPart;
464 dbSystemTime = Li2Double(liCurrentTime);
465
466 // CurrentCpuIdle = IdleTime / SystemTime
469
470 // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
471 dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
472 dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
473 }
474
475 // Store new CPU's idle and system time
476 liOldIdleTime = liCurrentIdleTime;
477 liOldSystemTime = SysTimeInfo.CurrentTime;
478 liOldKernelTime = liCurrentKernelTime;
479#endif
480
481 // Determine the process count
482 // We loop through the data we got from PsaCaptureProcessesAndThreads
483 // and count how many structures there are (until PsaWalkNextProcess
484 // returns NULL)
486 ProcessCount = 0;
488 while (pSPI) {
489 ProcessCount++;
490 pSPI = PsaWalkNextProcess(pSPI);
491 }
492
493 // Now alloc a new PERFDATA array and fill in the data
494 if (pPerfDataOld) {
496 }
500 for (Idx=0; Idx<ProcessCount; Idx++) {
501 // Get the old perf data for this process (if any)
502 // so that we can establish delta values
503 pPDOld = NULL;
504 for (Idx2=0; Idx2<ProcessCountOld; Idx2++) {
506 /* check also for the creation time, a new process may have an id of an old one */
508 pPDOld = &pPerfDataOld[Idx2];
509 break;
510 }
511 }
512
513 // Clear out process perf data structure
514 memset(&pPerfData[Idx], 0, sizeof(PERFDATA));
515
516 if (pSPI->ImageName.Buffer) {
517 wcsncpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer, pSPI->ImageName.Length / sizeof(WCHAR));
518 pPerfData[Idx].ImageName[pSPI->ImageName.Length / sizeof(WCHAR)] = 0;
519 }
520 else
521 {
522#ifdef _UNICODE
524#else
526#endif
527 }
528
530 pPerfData[Idx].CreateTime = pSPI->CreateTime;
531
532 if (pPDOld) {
533#ifdef TIMES
534 double CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
535 double OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
536 double CpuTime = (CurTime - OldTime) / dbSystemTime;
537 CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; // + 0.5;
538
539 pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
540#else
541 pPerfData[Idx].CPUUsage = 0;
542#endif
543 }
544
548 if (pPDOld)
550 else
553 if (pPDOld)
555 else
561 pPerfData[Idx].HandleCount = pSPI->HandleCount;
563 //pPerfData[Idx].SessionId = pSPI->SessionId;
564
565#ifdef EXTRA_INFO
567 if (hProcess) {
569 ImpersonateLoggedOnUser(hProcessToken);
570 memset(szTemp, 0, sizeof(TCHAR[MAX_PATH]));
572 GetUserName(szTemp, &dwSize);
573#ifndef UNICODE
574 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
575#endif
576 RevertToSelf();
577 CloseHandle(hProcessToken);
578 }
580 }
581#endif
582#ifdef TIMES
585#endif
586 pSPI = PsaWalkNextProcess(pSPI);
587 }
589
590 free(SysProcessorTimeInfo);
592 first = 0;
593}
#define HandleToUlong(h)
Definition: basetsd.h:79
SYSTEM_BASIC_INFORMATION SystemBasicInfo
Definition: ctm.c:95
LARGE_INTEGER liOldKernelTime
Definition: ctm.c:86
double dbIdleTime
Definition: ctm.c:82
PPERFDATA pPerfDataOld
Definition: ctm.c:89
ULONG ProcessCountOld
Definition: ctm.c:79
double dbKernelTime
Definition: ctm.c:83
double dbSystemTime
Definition: ctm.c:84
LARGE_INTEGER liOldIdleTime
Definition: ctm.c:85
LARGE_INTEGER liOldSystemTime
Definition: ctm.c:87
#define SystemTimeInformation
Definition: ctm.h:57
#define NO_ERROR
Definition: dderror.h:5
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define CloseHandle
Definition: compat.h:739
#define MultiByteToWideChar
Definition: compat.h:110
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
@ SystemProcessorPerformanceInformation
Definition: ntddk_ex.h:19
_Check_return_ long __cdecl labs(_In_ long x)
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:166
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
VOID NTAPI PsaFreeCapture(IN PVOID Capture)
Definition: processes.c:188
PSYSTEM_PROCESSES FASTCALL PsaWalkNextProcess(IN PSYSTEM_PROCESSES CurrentProcess)
NTSTATUS NTAPI PsaCaptureProcessesAndThreads(OUT PSYSTEM_PROCESSES *ProcessesAndThreads)
PSYSTEM_PROCESSES FASTCALL PsaWalkFirstProcess(IN PSYSTEM_PROCESSES ProcessesAndThreads)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:80
struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION * PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
long LONG
Definition: pedump.c:60
#define Li2Double(x)
Definition: perfdata.h:11
struct _PERFDATA * PPERFDATA
PVOID pBuffer
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define memset(x, y, z)
Definition: compat.h:39
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
ULONG PageFaultCountDelta
Definition: perfdata.h:25
LARGE_INTEGER UserTime
Definition: perfdata.h:36
LARGE_INTEGER CPUTime
Definition: perfdata.h:20
ULONG WorkingSetSizeDelta
Definition: perfdata.h:23
ULONG CPUUsage
Definition: perfdata.h:19
LARGE_INTEGER KernelTime
Definition: perfdata.h:37
ULONG NonPagedPoolUsagePages
Definition: perfdata.h:28
WCHAR ImageName[MAX_PATH]
Definition: perfdata.h:15
ULONG VirtualMemorySizeBytes
Definition: perfdata.h:26
ULONG PagedPoolUsagePages
Definition: perfdata.h:27
LARGE_INTEGER CreateTime
Definition: ctm.h:54
ULONG ThreadCount
Definition: perfdata.h:31
ULONG PageFaultCount
Definition: perfdata.h:24
ULONG HandleCount
Definition: perfdata.h:30
HANDLE ProcessId
Definition: perfdata.h:16
ULONG WorkingSetSizeBytes
Definition: perfdata.h:21
ULONG PeakWorkingSetSizeBytes
Definition: perfdata.h:22
ULONG BasePriority
Definition: perfdata.h:29
LARGE_INTEGER UserTime
Definition: extypes.h:900
UNICODE_STRING ImageName
Definition: extypes.h:902
LARGE_INTEGER CreateTime
Definition: extypes.h:899
LARGE_INTEGER KernelTime
Definition: extypes.h:901
LARGE_INTEGER CurrentTime
Definition: extypes.h:858
Definition: ps.c:97
unsigned char * LPBYTE
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
#define GetUserName
Definition: winbase.h:3786
BOOL WINAPI RevertToSelf(void)
Definition: security.c:1608
#define MB_PRECOMPOSED
Definition: winnls.h:281
#define TOKEN_DUPLICATE
Definition: setypes.h:926
#define TOKEN_QUERY
Definition: setypes.h:928
#define TOKEN_IMPERSONATE
Definition: setypes.h:927
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by _tmain().

◆ PerfInit()

void PerfInit ( )

Definition at line 405 of file ctm.c.

Referenced by _tmain().

◆ ProcessKeys()

int ProcessKeys ( int  numEvents)

Definition at line 286 of file ctm.c.

287{
289 TCHAR key;
290 if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
292
293 key = GetKeyPressed(numEvents);
294 if (key == KEY_QUIT)
295 return TRUE;
296 else if (key == KEY_KILL)
297 {
298 // user wants to kill some process, get his acknowledgement
299 DWORD pId;
300 COORD pos;
301 TCHAR lpStr[100];
302
303 pos.X = 1; pos.Y =ScreenLines-1;
304 if (LoadString(hInst, IDS_KILL_PROCESS, lpStr, 100))
306
307 do {
310 } while (key != KEY_YES && key != KEY_NO);
311
312 if (key == KEY_YES)
313 {
317
318 if (hProcess)
319 {
320 if (!TerminateProcess(hProcess, 0))
321 {
322 if (LoadString(hInst, IDS_KILL_PROCESS_ERR1, lpStr, 80))
323 {
326 }
327 Sleep(1000);
328 }
329
331 }
332 else
333 {
334 if (LoadString(hInst, IDS_KILL_PROCESS_ERR2, lpStr, 80))
335 {
337 _stprintf(lpStr, lpStr, pId);
339 }
340 Sleep(1000);
341 }
342 }
343
344 first = 0;
345 }
346 else if (key == VK_UP)
347 {
348 if (selection > 0)
349 selection--;
350 else if ((selection == 0) && (scrolled > 0))
351 scrolled--;
352 }
353 else if (key == VK_DOWN)
354 {
356 selection++;
357 else if ((selection == ProcPerScreen-1) && (selection+scrolled < ProcessCount-1))
358 scrolled++;
359 }
360 else if (key == VK_PRIOR)
361 {
364 else
365 {
366 scrolled=0; //First
367 selection=0;
368 }
369 //selection=0;
370 }
371 else if (key == VK_NEXT)
372 {
375 {
378 }
379
380 //selection=ProcPerScreen-1;
381 if (ProcessCount<=ProcPerScreen) //If there are less process than fits on the screen
382 {
383 scrolled=0;
385 }
386 }
387 else if (key == VK_HOME)
388 {
389 selection=0;
390 scrolled=0;
391 }
392 else if (key == VK_END)
393 {
396 if (ProcessCount<=ProcPerScreen) //If there are less process than fits on the screen
397 {
398 scrolled=0;
400 }
401 }
402 return FALSE;
403}
unsigned int GetKeyPressed()
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER pId
#define PROCESS_TERMINATE
Definition: pstypes.h:157
#define IDS_KILL_PROCESS_ERR1
Definition: resource.h:40
#define IDS_KILL_PROCESS_ERR2
Definition: resource.h:41
#define IDS_KILL_PROCESS
Definition: resource.h:39
Definition: copy.c:22
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define VK_UP
Definition: winuser.h:2225
#define VK_NEXT
Definition: winuser.h:2221
#define VK_END
Definition: winuser.h:2222
#define VK_HOME
Definition: winuser.h:2223
#define VK_DOWN
Definition: winuser.h:2227
#define VK_PRIOR
Definition: winuser.h:2220

Referenced by _tmain().

◆ PsaiFree()

void PsaiFree ( void ptr)

Definition at line 103 of file ctm.c.

103{ free(ptr); }
static PVOID ptr
Definition: dispmode.c:27

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

◆ PsaiMalloc()

void * PsaiMalloc ( SIZE_T  size)

Definition at line 101 of file ctm.c.

101{ return malloc(size); }

Referenced by PsaCaptureProcessesAndThreads(), and PsaCaptureSystemModules().

◆ PsaiRealloc()

void * PsaiRealloc ( void ptr,
SIZE_T  size 
)

Definition at line 102 of file ctm.c.

102{ return realloc(ptr, size); }
#define realloc
Definition: debug_ros.c:6

◆ RestoreConsole()

void RestoreConsole ( )

Definition at line 131 of file ctm.c.

Referenced by _tmain().

Variable Documentation

◆ columnRightPositions

DWORD columnRightPositions[6]

Definition at line 62 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ dbIdleTime

double dbIdleTime

Definition at line 82 of file ctm.c.

Referenced by PerfDataRefresh().

◆ dbKernelTime

double dbKernelTime

Definition at line 83 of file ctm.c.

Referenced by PerfDataRefresh().

◆ dbSystemTime

double dbSystemTime

Definition at line 84 of file ctm.c.

Referenced by PerfDataRefresh().

◆ first

int first = 0

Definition at line 94 of file ctm.c.

◆ hInst

HINSTANCE hInst

Definition at line 57 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

◆ hStdin

HANDLE hStdin

◆ hStdout

◆ inConMode

DWORD inConMode

Definition at line 59 of file ctm.c.

Referenced by _tmain(), and RestoreConsole().

◆ KEY_KILL

TCHAR KEY_KILL

Definition at line 74 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

◆ KEY_NO

TCHAR KEY_NO

Definition at line 75 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

◆ KEY_QUIT

TCHAR KEY_QUIT

Definition at line 74 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

◆ KEY_YES

TCHAR KEY_YES

Definition at line 75 of file ctm.c.

Referenced by _tmain(), and ProcessKeys().

◆ liOldIdleTime

LARGE_INTEGER liOldIdleTime = {{0,0}}

Definition at line 85 of file ctm.c.

Referenced by PerfDataRefresh().

◆ liOldKernelTime

LARGE_INTEGER liOldKernelTime = {{0,0}}

Definition at line 86 of file ctm.c.

Referenced by PerfDataRefresh().

◆ liOldSystemTime

LARGE_INTEGER liOldSystemTime = {{0,0}}

Definition at line 87 of file ctm.c.

Referenced by PerfDataRefresh().

◆ lpEmpty

TCHAR lpEmpty[80]

Definition at line 72 of file ctm.c.

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

◆ lpHeader

TCHAR lpHeader

Definition at line 66 of file ctm.c.

Referenced by _tmain(), CommitWaveBufferApc(), DisplayScreen(), and WdmAudCommitWaveBufferByMMixer().

◆ lpIdleProcess

TCHAR lpIdleProcess[80]

Definition at line 68 of file ctm.c.

Referenced by _tmain(), and PerfDataRefresh().

◆ lpMemUnit

TCHAR lpMemUnit[3]

Definition at line 67 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ lpMenu

TCHAR lpMenu[80]

Definition at line 71 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ lpSeparator

TCHAR lpSeparator[80]

Definition at line 63 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ lpSeparatorDown

TCHAR lpSeparatorDown[80]

Definition at line 65 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ lpSeparatorUp

TCHAR lpSeparatorUp[80]

Definition at line 64 of file ctm.c.

Referenced by _tmain(), and DisplayScreen().

◆ lpTitle

◆ outConMode

DWORD outConMode

Definition at line 60 of file ctm.c.

Referenced by _tmain(), and RestoreConsole().

◆ pPerfData

PPERFDATA pPerfData = NULL

Definition at line 90 of file ctm.c.

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

◆ pPerfDataOld

PPERFDATA pPerfDataOld = NULL

Definition at line 89 of file ctm.c.

Referenced by PerfDataRefresh().

◆ ProcessCount

ULONG ProcessCount = 0

Definition at line 80 of file ctm.c.

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

◆ ProcessCountOld

ULONG ProcessCountOld = 0

Definition at line 79 of file ctm.c.

Referenced by PerfDataRefresh().

◆ ProcPerScreen

int ProcPerScreen = 17

Definition at line 77 of file ctm.c.

Referenced by DisplayScreen(), and ProcessKeys().

◆ screenBufferInfo

CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo

Definition at line 97 of file ctm.c.

Referenced by DisplayScreen().

◆ ScreenLines

int ScreenLines =25

Definition at line 78 of file ctm.c.

Referenced by DisplayScreen(), and ProcessKeys().

◆ scrolled

int scrolled =0

Definition at line 93 of file ctm.c.

Referenced by DisplayScreen(), and ProcessKeys().

◆ selection

◆ SystemBasicInfo

SYSTEM_BASIC_INFORMATION SystemBasicInfo

Definition at line 95 of file ctm.c.

Referenced by PerfDataRefresh(), and PerfInit().