Skip to main content
summaryrefslogtreecommitdiffstats
blob: 32ead06ca81b6e9839318d4ad501969194b2a391 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 This DLL provides some native utility methods for the Performance Plugin.

 2003/07/08 - I tried providing my own replacement to GetPerformanceInfo by calling pdhopenquery()
 but wsad always crashed when I shut it down, so I backed off (at least for now) and simply return
 a function not available return code. At some later point in time you can go back in VSS and restore
 that code and try again.
 */

#include <windows.h>
#include <wincon.h>
#include <psapi.h>

#include "ivjperf.h"

enum loadStatusType {unknown, loaded, notFound} loadStatus = unknown, loadHandleCount = unknown;

// loadStatusType loadStatus;

// depending on the OS (NT4/W2k or XP) we use different functions
typedef BOOL WINAPI _GPI(PPERFORMACE_INFORMATION pPerformanceInformation, DWORD cb);

_GPI* gpGetPerformanceInfo = NULL;	// this is our function pointer

typedef BOOL WINAPI _GPHC(HANDLE hProcess, PDWORD count);
_GPHC* gpGetProcessHandleCount = NULL;


/*
	A helper function that makes it easier to throw exceptions.
*/
void throwException(JNIEnv * jniEnv, char* details)
{
	jclass exceptionClass;

	exceptionClass = (*jniEnv)->FindClass(jniEnv, "java/lang/IllegalArgumentException"); 
	if (exceptionClass == 0)
	{
		printf("Could not find the exception class I have to give up");
		return;
	}
	(*jniEnv)->ThrowNew(jniEnv, exceptionClass, details);
	return;

}

/*
	A helper function that throws an exception that tells us a function is not supported
*/
void throwUnsupported(JNIEnv * jniEnv, char* details)
{
	jclass exceptionClass;

	exceptionClass = (*jniEnv)->FindClass(jniEnv, "java/lang/UnsupportedOperationException"); 
	if (exceptionClass == 0)
	{
		printf("Could not find the UnsupportedOperationException class I have to give up");
		return;
	}
	(*jniEnv)->ThrowNew(jniEnv, exceptionClass, details);
	return;

}

/*
	A helper method that makes it easier to handle errors. If a windows
	error is encountered, then a RuntimeException is thrown.
*/
void handleSystemError(JNIEnv * jniEnv)
{
	jclass exceptionClass;
	LPVOID lpMsgBuf;

	if (!FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL ))
	{
		// I give up
		return;
	}

	exceptionClass = (*jniEnv)->FindClass(jniEnv, "java/lang/RuntimeException"); 
	(*jniEnv)->ThrowNew(jniEnv, exceptionClass, lpMsgBuf);
	LocalFree( lpMsgBuf );
}

/*
	Determine if you can (or have) loaded the GetPerformanceInfo function. Set the global variable
	loadStatus with your determination.
*/
void checkGetPerformanceInfo(JNIEnv * jniEnv)
{
	if (loadStatus == unknown)
	{
		HMODULE psapiHandle = LoadLibraryA("PSAPI.dll");
		if (psapiHandle == NULL)
		{
			loadStatus = notFound;
			throwUnsupported(jniEnv, "Could not load psapi.dll");
			return;
		}
		gpGetPerformanceInfo = (_GPI*)GetProcAddress(psapiHandle, "GetPerformanceInfo");

		if(gpGetPerformanceInfo == NULL)loadStatus = notFound;
		else loadStatus = loaded;
	}
}

/*
	If you can answer the total amount of committed memory (for the entire machine). If you can't
	figure this out then return -1.
*/
jlong getTotalCommitted(JNIEnv * jniEnv)
{
	BOOL rc;
	jlong result = -1;
	PERFORMACE_INFORMATION	pi;

	checkGetPerformanceInfo(jniEnv);
	if (loadStatus == loaded)
	{
		rc = (gpGetPerformanceInfo)(&pi, sizeof(pi));
		if (!rc)handleSystemError(jniEnv);
		else result = pi.CommitTotal * pi.PageSize;
	}
	return result;
}

/*
	Answer the number of open handles in the process. If you can't get this information return -1.
*/
jlong getHandleCount(JNIEnv * jniEnv, HANDLE me)
{
	jlong result = -1;
	DWORD handleCount;

	if (loadHandleCount == unknown)
	{
		HMODULE kernel32 = LoadLibraryA("kernel32.dll");
		if (kernel32 == NULL)
		{
			loadHandleCount = notFound;
			throwUnsupported(jniEnv, "Could not load kernel32.dll");
		}
		else
		{
			gpGetProcessHandleCount = (_GPHC*)GetProcAddress(kernel32, "GetProcessHandleCount");

			if(gpGetProcessHandleCount == NULL)loadHandleCount = notFound;
			else loadHandleCount = loaded;
		}
	}

	if (loadHandleCount == loaded)
	{
		(gpGetProcessHandleCount)(me, &handleCount);
		result = handleCount;
	}
	return result;
}

/*
	The following block is copied from the Java source code. It documents the counters array.

	 * @param counters the results are returned in this array.
	 * <ol>
	 * <li>working set in bytes for this process
	 * <li>peak working set in bytes for this process
	 * <li>elapsed time in milliseconds
	 * <li>user time in milliseconds
	 * <li>kernel time in milliseconds
	 * <li>page faults for the process
	 * <li>commit charge total in bytes (working set for the entire machine). On some 
	 * machines we have problems getting this value so we return -1 in that case.
	 * <li>number of GDI objects in the process
	 * <li>number of USER objects in the process
	 * <li>number of open handles in the process. returns -1 if this information is not available
	 * <li>Number of read operations
	 * <li>Number of write operations
	 * <li>Number of bytes read
	 * <li>Number of bytes written
	 * </ol>

*/
JNIEXPORT jboolean JNICALL Java_org_eclipse_perfmsr_core_PerformanceMonitor_nativeGetPerformanceCounters
  (JNIEnv * jniEnv, jclass jniClass, jlongArray counters)
{
	FILETIME creationTime, exitTime, kernelTime, userTime, systemTime;
	ULARGE_INTEGER uliCreation, uliSystem, uliKernel, uliUser;
	ULONGLONG diff;
	IO_COUNTERS ioCounters;

	jboolean result = TRUE;
	jsize len = (*jniEnv)->GetArrayLength(jniEnv, counters);
	jlong *body = (*jniEnv)->GetLongArrayElements(jniEnv, counters, 0);
	HANDLE me = GetCurrentProcess();
	PROCESS_MEMORY_COUNTERS memCounters;
	DWORD cb = sizeof(PROCESS_MEMORY_COUNTERS);
	BOOL rc = GetProcessMemoryInfo(me, &memCounters, cb);
	if (rc != 0)
	{
		body[0] = memCounters.WorkingSetSize;
		body[1] = memCounters.PeakWorkingSetSize;
		body[5] = memCounters.PageFaultCount;
	}
	else
	{
		handleSystemError(jniEnv);
		return FALSE;
	}

	if (!GetProcessTimes(me, &creationTime, &exitTime, &kernelTime, &userTime))
	{
		handleSystemError(jniEnv);
		return FALSE;
	}
	GetSystemTimeAsFileTime(&systemTime);

	memcpy(&uliCreation, &creationTime, sizeof(uliCreation));  
	memcpy(&uliSystem, &systemTime, sizeof(uliSystem));
	memcpy(&uliKernel, &kernelTime, sizeof(uliSystem));
	memcpy(&uliUser, &userTime, sizeof(ULARGE_INTEGER));
	diff = uliSystem.QuadPart - uliCreation.QuadPart;
	body[2] = diff / 10000;
	body[3] = uliUser.QuadPart / 10000;
	body[4] = uliKernel.QuadPart / 10000;
	body[6] = getTotalCommitted(jniEnv);

	body[7] = GetGuiResources(me, GR_GDIOBJECTS);
	body[8] = GetGuiResources(me, GR_USEROBJECTS);
	body[9] = getHandleCount(jniEnv, me);

	if (!GetProcessIoCounters(me, &ioCounters))
	{
		handleSystemError(jniEnv);
		return FALSE;
	}
	body[10] = ioCounters.ReadOperationCount;
	body[11] = ioCounters.WriteOperationCount;
	body[12] = ioCounters.ReadTransferCount;
	body[13] = ioCounters.WriteTransferCount;

	(*jniEnv)->ReleaseLongArrayElements(jniEnv, counters, body, 0);

	return result;
}

JNIEXPORT jstring JNICALL Java_org_eclipse_perfmsr_core_PerformanceMonitor_nativeGetUUID
  (JNIEnv * jniEnv, jclass jniClass)
{
	UUID				uuid;
	unsigned char*		uuidStr;
	jstring				result;

	UuidCreate(&uuid);
	UuidToString(&uuid, &uuidStr);

	result = (*jniEnv)->NewStringUTF(jniEnv, uuidStr); 
	RpcStringFree(&uuidStr);

	return result;
}


JNIEXPORT void JNICALL Java_org_eclipse_perfmsr_core_PerformanceMonitor_nativeGetPerformanceInfo
  (JNIEnv * jniEnv, jclass jniClass, jlongArray counters)
{
	jlong *body;
	BOOL rc;
	PERFORMACE_INFORMATION	pi;

	jsize len = (*jniEnv)->GetArrayLength(jniEnv, counters); 
	
	if (len != 13)
	{
		throwException(jniEnv, "Wrong number of elements in array, 13 are expected");
		return;
	}

	checkGetPerformanceInfo(jniEnv);
	if (loadStatus == notFound)
	{
		throwUnsupported(jniEnv, "The GetPerformanceInfo() function is not available");
		return;
	}
	
	body = (*jniEnv)->GetLongArrayElements(jniEnv, counters, 0);
	
	rc = (gpGetPerformanceInfo)(&pi, sizeof(pi));
	if (!rc)
	{
		handleSystemError(jniEnv);
		return;
	}

	body[0] = pi.CommitTotal;
	body[1] = pi.CommitLimit;
	body[2] = pi.CommitPeak;
	body[3] = pi.PhysicalTotal;
	body[4] = pi.PhysicalAvailable;
	body[5] = pi.SystemCache;
	body[6] = pi.KernelTotal;
	body[7] = pi.KernelPaged;
	body[8] = pi.KernelNonpaged;
	body[9] = pi.PageSize;
	body[10] = pi.HandleCount;
	body[11] = pi.ProcessCount;
	body[12] = pi.ThreadCount;

	(*jniEnv)->ReleaseLongArrayElements(jniEnv, counters, body, 0);

}

Back to the top