src/windows/native/sun/management/OperatingSystemImpl.c

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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 <psapi.h>
  34 #include <errno.h>
  35 #include <stdlib.h>
  36 
  37 #include <malloc.h>
  38 #pragma warning (push,0)
  39 #include <windows.h>
  40 #pragma warning (pop)
  41 #include <stdio.h>
  42 #include <time.h>
  43 #include <stdint.h>
  44 #include <assert.h>
  45 
  46 /* Disable warnings due to broken header files from Microsoft... */
  47 #pragma warning(push, 3)
  48 #include <pdh.h>
  49 #include <pdhmsg.h>
  50 #include <process.h>
  51 #pragma warning(pop)


  60     *value |= (jlong)(julong)(juint)low;
  61 }
  62 
  63 static void set_high(jlong* value, jint high) {
  64     *value &= (jlong)(julong)(juint)0xffffffff;
  65     *value |= (jlong)high       << 32;
  66 }
  67 
  68 static jlong jlong_from(jint h, jint l) {
  69   jlong result = 0; // initialization to avoid warning
  70   set_high(&result, h);
  71   set_low(&result,  l);
  72   return result;
  73 }
  74 
  75 static HANDLE main_process;
  76 
  77 int perfiInit(void);
  78 
  79 JNIEXPORT void JNICALL
  80 Java_com_sun_management_OperatingSystem_initialize
  81   (JNIEnv *env, jclass cls)
  82 {
  83     main_process = GetCurrentProcess();
  84      perfiInit();
  85 }
  86 
  87 JNIEXPORT jlong JNICALL
  88 Java_com_sun_management_OperatingSystem_getCommittedVirtualMemorySize0
  89   (JNIEnv *env, jobject mbean)
  90 {
  91     PROCESS_MEMORY_COUNTERS pmc;
  92     if (GetProcessMemoryInfo(main_process, &pmc, sizeof(PROCESS_MEMORY_COUNTERS)) == 0) {
  93         return (jlong)-1L;
  94     } else {
  95         return (jlong) pmc.PagefileUsage;
  96     }
  97 }
  98 
  99 JNIEXPORT jlong JNICALL
 100 Java_com_sun_management_OperatingSystem_getTotalSwapSpaceSize
 101   (JNIEnv *env, jobject mbean)
 102 {
 103     MEMORYSTATUSEX ms;
 104     ms.dwLength = sizeof(ms);
 105     GlobalMemoryStatusEx(&ms);
 106     return (jlong) ms.ullTotalPageFile;
 107 }
 108 
 109 JNIEXPORT jlong JNICALL
 110 Java_com_sun_management_OperatingSystem_getFreeSwapSpaceSize
 111   (JNIEnv *env, jobject mbean)
 112 {
 113     MEMORYSTATUSEX ms;
 114     ms.dwLength = sizeof(ms);
 115     GlobalMemoryStatusEx(&ms);
 116     return (jlong) ms.ullAvailPageFile;
 117 }
 118 
 119 JNIEXPORT jlong JNICALL
 120 Java_com_sun_management_OperatingSystem_getProcessCpuTime
 121   (JNIEnv *env, jobject mbean)
 122 {
 123 
 124     FILETIME process_creation_time, process_exit_time,
 125              process_user_time, process_kernel_time;
 126 
 127     // Using static variables declared above
 128     // Units are 100-ns intervals.  Convert to ns.
 129     GetProcessTimes(main_process, &process_creation_time,
 130                     &process_exit_time,
 131                     &process_kernel_time, &process_user_time);
 132     return (jlong_from(process_user_time.dwHighDateTime,
 133                         process_user_time.dwLowDateTime) +
 134             jlong_from(process_kernel_time.dwHighDateTime,
 135                         process_kernel_time.dwLowDateTime)) * 100;
 136 }
 137 
 138 JNIEXPORT jlong JNICALL
 139 Java_com_sun_management_OperatingSystem_getFreePhysicalMemorySize
 140   (JNIEnv *env, jobject mbean)
 141 {
 142     MEMORYSTATUSEX ms;
 143     ms.dwLength = sizeof(ms);
 144     GlobalMemoryStatusEx(&ms);
 145     return (jlong) ms.ullAvailPhys;
 146 }
 147 
 148 JNIEXPORT jlong JNICALL
 149 Java_com_sun_management_OperatingSystem_getTotalPhysicalMemorySize
 150   (JNIEnv *env, jobject mbean)
 151 {
 152     MEMORYSTATUSEX ms;
 153     ms.dwLength = sizeof(ms);
 154     GlobalMemoryStatusEx(&ms);
 155     return (jlong) ms.ullTotalPhys;
 156 }
 157 
 158 // Seems WinXP PDH returns PDH_MORE_DATA whenever we send in a NULL buffer.
 159 // Let's just ignore it, since we make sure we have enough buffer anyway.
 160 static int
 161 pdh_fail(PDH_STATUS pdhStat) {
 162     return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;
 163 }
 164 
 165 // INFO: Using PDH APIs Correctly in a Localized Language (Q287159)
 166 //       http://support.microsoft.com/default.aspx?scid=kb;EN-US;q287159
 167 // The index value for the base system counters and objects like processor,
 168 // process, thread, memory, and so forth are always the same irrespective
 169 // of the localized version of the operating system or service pack installed.


 910                     /* printf("Failed to init system load counters.\n"); */
 911                 } else if (initProcLoadCounter() != 0) {
 912                     /* printf("Failed to init process load counter.\n"); */
 913                 } else if (initProcSystemLoadCounter() != 0) {
 914                     /* printf("Failed to init process system load counter.\n"); */
 915                 } else {
 916                     inited = true;
 917                 }
 918             }
 919         }
 920     } LeaveCriticalSection(&initializationLock);
 921 
 922     if (inited && error == CONFIG_SUCCESSFUL) {
 923         running = true;
 924     }
 925 
 926     return error;
 927 }
 928 
 929 JNIEXPORT jdouble JNICALL
 930 Java_com_sun_management_OperatingSystem_getSystemCpuLoad
 931 (JNIEnv *env, jobject dummy)
 932 {
 933     return perfGetCPULoad(-1);
 934 }
 935 
 936 JNIEXPORT jdouble JNICALL
 937 Java_com_sun_management_OperatingSystem_getProcessCpuLoad
 938 (JNIEnv *env, jobject dummy)
 939 {
 940     return perfGetProcessLoad();
 941 }


  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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 "sun_management_OperatingSystemImpl.h"
  32 
  33 #include <psapi.h>
  34 #include <errno.h>
  35 #include <stdlib.h>
  36 
  37 #include <malloc.h>
  38 #pragma warning (push,0)
  39 #include <windows.h>
  40 #pragma warning (pop)
  41 #include <stdio.h>
  42 #include <time.h>
  43 #include <stdint.h>
  44 #include <assert.h>
  45 
  46 /* Disable warnings due to broken header files from Microsoft... */
  47 #pragma warning(push, 3)
  48 #include <pdh.h>
  49 #include <pdhmsg.h>
  50 #include <process.h>
  51 #pragma warning(pop)


  60     *value |= (jlong)(julong)(juint)low;
  61 }
  62 
  63 static void set_high(jlong* value, jint high) {
  64     *value &= (jlong)(julong)(juint)0xffffffff;
  65     *value |= (jlong)high       << 32;
  66 }
  67 
  68 static jlong jlong_from(jint h, jint l) {
  69   jlong result = 0; // initialization to avoid warning
  70   set_high(&result, h);
  71   set_low(&result,  l);
  72   return result;
  73 }
  74 
  75 static HANDLE main_process;
  76 
  77 int perfiInit(void);
  78 
  79 JNIEXPORT void JNICALL
  80 Java_sun_management_OperatingSystemImpl_initialize
  81   (JNIEnv *env, jclass cls)
  82 {
  83     main_process = GetCurrentProcess();
  84      perfiInit();
  85 }
  86 
  87 JNIEXPORT jlong JNICALL
  88 Java_sun_management_OperatingSystemImpl_getCommittedVirtualMemorySize0
  89   (JNIEnv *env, jobject mbean)
  90 {
  91     PROCESS_MEMORY_COUNTERS pmc;
  92     if (GetProcessMemoryInfo(main_process, &pmc, sizeof(PROCESS_MEMORY_COUNTERS)) == 0) {
  93         return (jlong)-1L;
  94     } else {
  95         return (jlong) pmc.PagefileUsage;
  96     }
  97 }
  98 
  99 JNIEXPORT jlong JNICALL
 100 Java_sun_management_OperatingSystemImpl_getTotalSwapSpaceSize
 101   (JNIEnv *env, jobject mbean)
 102 {
 103     MEMORYSTATUSEX ms;
 104     ms.dwLength = sizeof(ms);
 105     GlobalMemoryStatusEx(&ms);
 106     return (jlong) ms.ullTotalPageFile;
 107 }
 108 
 109 JNIEXPORT jlong JNICALL
 110 Java_sun_management_OperatingSystemImpl_getFreeSwapSpaceSize
 111   (JNIEnv *env, jobject mbean)
 112 {
 113     MEMORYSTATUSEX ms;
 114     ms.dwLength = sizeof(ms);
 115     GlobalMemoryStatusEx(&ms);
 116     return (jlong) ms.ullAvailPageFile;
 117 }
 118 
 119 JNIEXPORT jlong JNICALL
 120 Java_sun_management_OperatingSystemImpl_getProcessCpuTime
 121   (JNIEnv *env, jobject mbean)
 122 {
 123 
 124     FILETIME process_creation_time, process_exit_time,
 125              process_user_time, process_kernel_time;
 126 
 127     // Using static variables declared above
 128     // Units are 100-ns intervals.  Convert to ns.
 129     GetProcessTimes(main_process, &process_creation_time,
 130                     &process_exit_time,
 131                     &process_kernel_time, &process_user_time);
 132     return (jlong_from(process_user_time.dwHighDateTime,
 133                         process_user_time.dwLowDateTime) +
 134             jlong_from(process_kernel_time.dwHighDateTime,
 135                         process_kernel_time.dwLowDateTime)) * 100;
 136 }
 137 
 138 JNIEXPORT jlong JNICALL
 139 Java_sun_management_OperatingSystemImpl_getFreePhysicalMemorySize
 140   (JNIEnv *env, jobject mbean)
 141 {
 142     MEMORYSTATUSEX ms;
 143     ms.dwLength = sizeof(ms);
 144     GlobalMemoryStatusEx(&ms);
 145     return (jlong) ms.ullAvailPhys;
 146 }
 147 
 148 JNIEXPORT jlong JNICALL
 149 Java_sun_management_OperatingSystemImpl_getTotalPhysicalMemorySize
 150   (JNIEnv *env, jobject mbean)
 151 {
 152     MEMORYSTATUSEX ms;
 153     ms.dwLength = sizeof(ms);
 154     GlobalMemoryStatusEx(&ms);
 155     return (jlong) ms.ullTotalPhys;
 156 }
 157 
 158 // Seems WinXP PDH returns PDH_MORE_DATA whenever we send in a NULL buffer.
 159 // Let's just ignore it, since we make sure we have enough buffer anyway.
 160 static int
 161 pdh_fail(PDH_STATUS pdhStat) {
 162     return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;
 163 }
 164 
 165 // INFO: Using PDH APIs Correctly in a Localized Language (Q287159)
 166 //       http://support.microsoft.com/default.aspx?scid=kb;EN-US;q287159
 167 // The index value for the base system counters and objects like processor,
 168 // process, thread, memory, and so forth are always the same irrespective
 169 // of the localized version of the operating system or service pack installed.


 910                     /* printf("Failed to init system load counters.\n"); */
 911                 } else if (initProcLoadCounter() != 0) {
 912                     /* printf("Failed to init process load counter.\n"); */
 913                 } else if (initProcSystemLoadCounter() != 0) {
 914                     /* printf("Failed to init process system load counter.\n"); */
 915                 } else {
 916                     inited = true;
 917                 }
 918             }
 919         }
 920     } LeaveCriticalSection(&initializationLock);
 921 
 922     if (inited && error == CONFIG_SUCCESSFUL) {
 923         running = true;
 924     }
 925 
 926     return error;
 927 }
 928 
 929 JNIEXPORT jdouble JNICALL
 930 Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
 931 (JNIEnv *env, jobject dummy)
 932 {
 933     return perfGetCPULoad(-1);
 934 }
 935 
 936 JNIEXPORT jdouble JNICALL
 937 Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
 938 (JNIEnv *env, jobject dummy)
 939 {
 940     return perfGetProcessLoad();
 941 }