forked from ms-iot/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication.cpp
More file actions
125 lines (106 loc) · 3.47 KB
/
ConsoleApplication.cpp
File metadata and controls
125 lines (106 loc) · 3.47 KB
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft. All rights reserved.
// ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "pch.h"
#include <windows.h>
#include <chrono>
#include <thread>
using namespace std;
// Use to convert bytes to KB
#define DIV 1024
// Specify the width of the field in which to print the numbers.
#define MESSAGE_WIDTH 30
#define NUMERIC_WIDTH 10
void printMessage(LPCSTR msg, bool addColon)
{
cout.width(MESSAGE_WIDTH);
cout << msg;
if (addColon)
{
cout << " : ";
}
}
void printMessageLine(LPCSTR msg)
{
printMessage(msg, false);
cout << endl;
}
void printMessageLine(LPCSTR msg, DWORD value)
{
printMessage(msg, true);
cout.width(NUMERIC_WIDTH);
cout << right << value << endl;
}
void printMessageLine(LPCSTR msg, DWORDLONG value)
{
printMessage(msg, true);
cout.width(NUMERIC_WIDTH);
cout << right << value << endl;
}
void checkInput(HANDLE exitEvent)
{
for (;;)
{
char character;
cin.get(character);
if (character == 'q')
{
::SetEvent(exitEvent);
break;
}
}
}
int main(int argc, char **argv)
{
printMessageLine("Starting to monitor memory consumption! Press enter to start monitoring");
printMessageLine("You can press q and enter at anytime to exit");
cin.get();
HANDLE exitEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
if (NULL == exitEvent)
{
printMessageLine("Failed to create exitEvent.");
return -1;
}
std::thread inputThread(checkInput, exitEvent);
for (;;)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
BOOL success = ::GlobalMemoryStatusEx(&statex);
if (!success)
{
DWORD error = GetLastError();
printMessageLine("*************************************************");
printMessageLine("Error getting memory information", error);
printMessageLine("*************************************************");
}
else
{
DWORD load = statex.dwMemoryLoad;
DWORDLONG physKb = statex.ullTotalPhys / DIV;
DWORDLONG freePhysKb = statex.ullAvailPhys / DIV;
DWORDLONG pageKb = statex.ullTotalPageFile / DIV;
DWORDLONG freePageKb = statex.ullAvailPageFile / DIV;
DWORDLONG virtualKb = statex.ullTotalVirtual / DIV;
DWORDLONG freeVirtualKb = statex.ullAvailVirtual / DIV;
DWORDLONG freeExtKb = statex.ullAvailExtendedVirtual / DIV;
printMessageLine("*************************************************");
printMessageLine("Percent of memory in use", load);
printMessageLine("KB of physical memory", physKb);
printMessageLine("KB of free physical memory", freePhysKb);
printMessageLine("KB of paging file", pageKb);
printMessageLine("KB of free paging file", freePageKb);
printMessageLine("KB of virtual memory", virtualKb);
printMessageLine("KB of free virtual memory", freeVirtualKb);
printMessageLine("KB of free extended memory", freeExtKb);
printMessageLine("*************************************************");
}
if (WAIT_OBJECT_0 == ::WaitForSingleObject(exitEvent, 100))
{
break;
}
}
inputThread.join();
::CloseHandle(exitEvent);
printMessageLine("No longer monitoring memory consumption!");
}