J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | #include "jni.h" |
| 27 | #include "jni_util.h" |
| 28 | #include "jlong.h" |
| 29 | #include "jvm.h" |
| 30 | #include "management.h" |
| 31 | #include "com_sun_management_OperatingSystem.h" |
| 32 | |
| 33 | #include <errno.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | typedef unsigned __int32 juint; |
| 37 | typedef unsigned __int64 julong; |
| 38 | |
| 39 | static void set_low(jlong* value, jint low) { |
| 40 | *value &= (jlong)0xffffffff << 32; |
| 41 | *value |= (jlong)(julong)(juint)low; |
| 42 | } |
| 43 | |
| 44 | static void set_high(jlong* value, jint high) { |
| 45 | *value &= (jlong)(julong)(juint)0xffffffff; |
| 46 | *value |= (jlong)high << 32; |
| 47 | } |
| 48 | |
| 49 | static jlong jlong_from(jint h, jint l) { |
| 50 | jlong result = 0; // initialization to avoid warning |
| 51 | set_high(&result, h); |
| 52 | set_low(&result, l); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | // From psapi.h |
| 57 | typedef struct _PROCESS_MEMORY_COUNTERS { |
| 58 | DWORD cb; |
| 59 | DWORD PageFaultCount; |
| 60 | SIZE_T PeakWorkingSetSize; |
| 61 | SIZE_T WorkingSetSize; |
| 62 | SIZE_T QuotaPeakPagedPoolUsage; |
| 63 | SIZE_T QuotaPagedPoolUsage; |
| 64 | SIZE_T QuotaPeakNonPagedPoolUsage; |
| 65 | SIZE_T QuotaNonPagedPoolUsage; |
| 66 | SIZE_T PagefileUsage; |
| 67 | SIZE_T PeakPagefileUsage; |
| 68 | } PROCESS_MEMORY_COUNTERS; |
| 69 | |
| 70 | static HINSTANCE hInstPsapi = NULL; |
| 71 | typedef BOOL (WINAPI *LPFNGETPROCESSMEMORYINFO)(HANDLE, PROCESS_MEMORY_COUNTERS*, DWORD); |
| 72 | |
| 73 | static jboolean is_nt = JNI_FALSE; |
| 74 | static HANDLE main_process; |
| 75 | |
| 76 | JNIEXPORT void JNICALL |
| 77 | Java_com_sun_management_OperatingSystem_initialize |
| 78 | (JNIEnv *env, jclass cls) |
| 79 | { |
| 80 | OSVERSIONINFO oi; |
| 81 | oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 82 | GetVersionEx(&oi); |
| 83 | switch(oi.dwPlatformId) { |
| 84 | case VER_PLATFORM_WIN32_WINDOWS: is_nt = JNI_FALSE; break; |
| 85 | case VER_PLATFORM_WIN32_NT: is_nt = JNI_TRUE; break; |
| 86 | default: |
| 87 | throw_internal_error(env, "Unsupported Platform"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | main_process = GetCurrentProcess(); |
| 92 | } |
| 93 | |
| 94 | JNIEXPORT jlong JNICALL |
| 95 | Java_com_sun_management_OperatingSystem_getCommittedVirtualMemorySize0 |
| 96 | (JNIEnv *env, jobject mbean) |
| 97 | { |
| 98 | |
| 99 | /* |
| 100 | * In bytes. NT/2000/XP only - using GetProcessMemoryInfo from psapi.dll |
| 101 | */ |
| 102 | static LPFNGETPROCESSMEMORYINFO lpfnGetProcessMemoryInfo = NULL; |
| 103 | static volatile jboolean psapi_inited = JNI_FALSE; |
| 104 | PROCESS_MEMORY_COUNTERS pmc; |
| 105 | |
| 106 | if (!is_nt) return -1; |
| 107 | |
| 108 | if (!psapi_inited) { |
| 109 | psapi_inited = JNI_TRUE; |
| 110 | if ((hInstPsapi = LoadLibrary("PSAPI.DLL")) == NULL) return -1; |
| 111 | if ((lpfnGetProcessMemoryInfo = (LPFNGETPROCESSMEMORYINFO) |
| 112 | GetProcAddress( hInstPsapi, "GetProcessMemoryInfo")) == NULL) { |
| 113 | FreeLibrary(hInstPsapi); |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (lpfnGetProcessMemoryInfo == NULL) return -1; |
| 119 | |
| 120 | lpfnGetProcessMemoryInfo(main_process, &pmc, |
| 121 | sizeof(PROCESS_MEMORY_COUNTERS)); |
| 122 | return (jlong) pmc.PagefileUsage; |
| 123 | } |
| 124 | |
| 125 | JNIEXPORT jlong JNICALL |
| 126 | Java_com_sun_management_OperatingSystem_getTotalSwapSpaceSize |
| 127 | (JNIEnv *env, jobject mbean) |
| 128 | { |
| 129 | MEMORYSTATUS ms; |
| 130 | GlobalMemoryStatus(&ms); |
| 131 | return (jlong)ms.dwTotalPageFile; |
| 132 | } |
| 133 | |
| 134 | JNIEXPORT jlong JNICALL |
| 135 | Java_com_sun_management_OperatingSystem_getFreeSwapSpaceSize |
| 136 | (JNIEnv *env, jobject mbean) |
| 137 | { |
| 138 | MEMORYSTATUS ms; |
| 139 | GlobalMemoryStatus(&ms); |
| 140 | return (jlong)ms.dwAvailPageFile; |
| 141 | } |
| 142 | |
| 143 | JNIEXPORT jlong JNICALL |
| 144 | Java_com_sun_management_OperatingSystem_getProcessCpuTime |
| 145 | (JNIEnv *env, jobject mbean) |
| 146 | { |
| 147 | |
| 148 | FILETIME process_creation_time, process_exit_time, |
| 149 | process_user_time, process_kernel_time; |
| 150 | |
| 151 | // Windows NT only |
| 152 | if (is_nt) { |
| 153 | // Using static variables declared above |
| 154 | // Units are 100-ns intervals. Convert to ns. |
| 155 | GetProcessTimes(main_process, &process_creation_time, |
| 156 | &process_exit_time, |
| 157 | &process_kernel_time, &process_user_time); |
| 158 | return (jlong_from(process_user_time.dwHighDateTime, |
| 159 | process_user_time.dwLowDateTime) + |
| 160 | jlong_from(process_kernel_time.dwHighDateTime, |
| 161 | process_kernel_time.dwLowDateTime)) * 100; |
| 162 | } else { |
| 163 | return -1; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | JNIEXPORT jlong JNICALL |
| 168 | Java_com_sun_management_OperatingSystem_getFreePhysicalMemorySize |
| 169 | (JNIEnv *env, jobject mbean) |
| 170 | { |
| 171 | MEMORYSTATUS ms; |
| 172 | GlobalMemoryStatus(&ms); |
| 173 | return (jlong) ms.dwAvailPhys; |
| 174 | } |
| 175 | |
| 176 | JNIEXPORT jlong JNICALL |
| 177 | Java_com_sun_management_OperatingSystem_getTotalPhysicalMemorySize |
| 178 | (JNIEnv *env, jobject mbean) |
| 179 | { |
| 180 | MEMORYSTATUS ms; |
| 181 | // also returns dwAvailPhys (free physical memory bytes), |
| 182 | // dwTotalVirtual, dwAvailVirtual, |
| 183 | // dwMemoryLoad (% of memory in use) |
| 184 | GlobalMemoryStatus(&ms); |
| 185 | return ms.dwTotalPhys; |
| 186 | } |