JNIEXPORT jlong JNICALL Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize (JNIEnv *env, jobject mbean) { #ifdef __solaris__ psinfo_t psinfo; ssize_t result; size_t remaining; char* addr; int fd; fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); if (fd < 0) { throw_internal_error(env, "Unable to open /proc/self/psinfo"); return -1; } addr = (char *)&psinfo; for (remaining = sizeof(psinfo_t); remaining > 0;) { result = JVM_Read(fd, addr, remaining); if (result < 0) { JVM_Close(fd); throw_internal_error(env, "Unable to read /proc/self/psinfo"); return -1; } remaining -= result; addr += result; } JVM_Close(fd); return (jlong) psinfo.pr_size * 1024; #else /* __linux__ */ FILE *fp; unsigned long vsize = 0; if ((fp = fopen("/proc/self/stat", "r")) == NULL) { throw_internal_error(env, "Unable to open /proc/self/stat"); return -1; } // Ignore everything except the vsize entry if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %lu %*[^\n]\n", &vsize) == EOF) { throw_internal_error(env, "Unable to get virtual memory usage"); fclose(fp); return -1; } fclose(fp); return (jlong)vsize; #endif }
/* * The io_util_md.h files do not provide IO_CLOSE, hence we use platform * specifics. */ static void ZFILE_Close(ZFILE zfd) { #ifdef WIN32 CloseHandle((HANDLE) zfd); #else JVM_Close(zfd); #endif }
JNIEXPORT jlong JNICALL Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize (JNIEnv *env, jobject mbean) { #ifdef __solaris__ psinfo_t psinfo; ssize_t result; size_t remaining; char* addr; int fd; fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); if (fd < 0) { throw_internal_error(env, "Unable to open /proc/self/psinfo"); return -1; } addr = (char *)&psinfo; for (remaining = sizeof(psinfo_t); remaining > 0;) { result = JVM_Read(fd, addr, remaining); if (result < 0) { JVM_Close(fd); throw_internal_error(env, "Unable to read /proc/self/psinfo"); return -1; } remaining -= result; addr += result; } JVM_Close(fd); return (jlong) psinfo.pr_size * 1024; #elif defined(__linux__) FILE *fp; unsigned long vsize = 0; if ((fp = fopen("/proc/self/stat", "r")) == NULL) { throw_internal_error(env, "Unable to open /proc/self/stat"); return -1; } // Ignore everything except the vsize entry if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %lu %*[^\n]\n", &vsize) == EOF) { throw_internal_error(env, "Unable to get virtual memory usage"); fclose(fp); return -1; } fclose(fp); return (jlong)vsize; #elif defined(__APPLE__) struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); if (res != KERN_SUCCESS) { throw_internal_error(env, "task_info failed"); } return t_info.virtual_size; #else /* _ALLBSD_SOURCE */ /* * XXXBSD: there's no way available to do it in FreeBSD, AFAIK. */ // throw_internal_error(env, "Unimplemented in FreeBSD"); return (64 * MB); #endif }