jdk/src/windows/native/sun/management/OperatingSystemImpl.c
author mchung
Fri, 08 Nov 2013 12:13:02 -0800
changeset 21633 6cc4c9a56216
parent 13668 jdk/src/windows/native/com/sun/management/OperatingSystem_md.c@c85b9828f8ff
child 22583 e7d5af0b11e9
permissions -rw-r--r--
8025985: com.sun.management.OSMBeanFactory should not be public Reviewed-by: alanb, erikj, ihse, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "management.h"
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
    31
#include "sun_management_OperatingSystemImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    33
#include <psapi.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    37
#include <malloc.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    38
#pragma warning (push,0)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    39
#include <windows.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    40
#pragma warning (pop)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    41
#include <stdio.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    42
#include <time.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    43
#include <stdint.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    44
#include <assert.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    45
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    46
/* Disable warnings due to broken header files from Microsoft... */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    47
#pragma warning(push, 3)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    48
#include <pdh.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    49
#include <pdhmsg.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    50
#include <process.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    51
#pragma warning(pop)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
typedef unsigned __int32 juint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
typedef unsigned __int64 julong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    56
typedef enum boolean_values { false=0, true=1};
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static void set_low(jlong* value, jint low) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    *value &= (jlong)0xffffffff << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    *value |= (jlong)(julong)(juint)low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static void set_high(jlong* value, jint high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    *value &= (jlong)(julong)(juint)0xffffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    *value |= (jlong)high       << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static jlong jlong_from(jint h, jint l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  jlong result = 0; // initialization to avoid warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  set_high(&result, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  set_low(&result,  l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static HANDLE main_process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    77
int perfiInit(void);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
JNIEXPORT void JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
    80
Java_sun_management_OperatingSystemImpl_initialize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    main_process = GetCurrentProcess();
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    84
     perfiInit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
    88
Java_sun_management_OperatingSystemImpl_getCommittedVirtualMemorySize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    PROCESS_MEMORY_COUNTERS pmc;
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    92
    if (GetProcessMemoryInfo(main_process, &pmc, sizeof(PROCESS_MEMORY_COUNTERS)) == 0) {
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    93
        return (jlong)-1L;
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    94
    } else {
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    95
        return (jlong) pmc.PagefileUsage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   100
Java_sun_management_OperatingSystemImpl_getTotalSwapSpaceSize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   103
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   104
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   105
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   106
    return (jlong) ms.ullTotalPageFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   110
Java_sun_management_OperatingSystemImpl_getFreeSwapSpaceSize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   113
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   114
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   115
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   116
    return (jlong) ms.ullAvailPageFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   120
Java_sun_management_OperatingSystemImpl_getProcessCpuTime
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    FILETIME process_creation_time, process_exit_time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             process_user_time, process_kernel_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   127
    // Using static variables declared above
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   128
    // Units are 100-ns intervals.  Convert to ns.
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   129
    GetProcessTimes(main_process, &process_creation_time,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   130
                    &process_exit_time,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   131
                    &process_kernel_time, &process_user_time);
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   132
    return (jlong_from(process_user_time.dwHighDateTime,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   133
                        process_user_time.dwLowDateTime) +
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   134
            jlong_from(process_kernel_time.dwHighDateTime,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   135
                        process_kernel_time.dwLowDateTime)) * 100;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   139
Java_sun_management_OperatingSystemImpl_getFreePhysicalMemorySize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   142
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   143
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   144
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   145
    return (jlong) ms.ullAvailPhys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
JNIEXPORT jlong JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   149
Java_sun_management_OperatingSystemImpl_getTotalPhysicalMemorySize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   152
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   153
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   154
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   155
    return (jlong) ms.ullTotalPhys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   157
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   158
// Seems WinXP PDH returns PDH_MORE_DATA whenever we send in a NULL buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   159
// Let's just ignore it, since we make sure we have enough buffer anyway.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   160
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   161
pdh_fail(PDH_STATUS pdhStat) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   162
    return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   163
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   164
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   165
// INFO: Using PDH APIs Correctly in a Localized Language (Q287159)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   166
//       http://support.microsoft.com/default.aspx?scid=kb;EN-US;q287159
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   167
// The index value for the base system counters and objects like processor,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   168
// process, thread, memory, and so forth are always the same irrespective
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   169
// of the localized version of the operating system or service pack installed.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   170
#define PDH_PROCESSOR_IDX        ((DWORD) 238)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   171
#define PDH_PROCESSOR_TIME_IDX        ((DWORD)   6)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   172
#define PDH_PRIV_PROCESSOR_TIME_IDX ((DWORD) 144)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   173
#define PDH_PROCESS_IDX            ((DWORD) 230)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   174
#define PDH_ID_PROCESS_IDX        ((DWORD) 784)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   175
#define PDH_CONTEXT_SWITCH_RATE_IDX ((DWORD) 146)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   176
#define PDH_SYSTEM_IDX            ((DWORD)   2)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   177
#define PDH_VIRTUAL_BYTES_IDX        ((DWORD) 174)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   178
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   179
typedef PDH_STATUS (WINAPI *PdhAddCounterFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   180
                           HQUERY      hQuery,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   181
                           LPCSTR      szFullCounterPath,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   182
                           DWORD       dwUserData,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   183
                           HCOUNTER    *phCounter
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   184
                           );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   185
typedef PDH_STATUS (WINAPI *PdhOpenQueryFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   186
                          LPCWSTR     szDataSource,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   187
                          DWORD       dwUserData,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   188
                          HQUERY      *phQuery
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   189
                          );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   190
typedef DWORD (WINAPI *PdhCloseQueryFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   191
                      HQUERY      hQuery
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   192
                      );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   193
typedef PDH_STATUS (WINAPI *PdhCollectQueryDataFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   194
                             HQUERY      hQuery
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   195
                             );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   196
typedef DWORD (WINAPI *PdhGetFormattedCounterValueFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   197
                            HCOUNTER                hCounter,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   198
                            DWORD                   dwFormat,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   199
                            LPDWORD                 lpdwType,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   200
                            PPDH_FMT_COUNTERVALUE   pValue
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   201
                            );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   202
typedef PDH_STATUS (WINAPI *PdhEnumObjectItemsFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   203
                            LPCTSTR    szDataSource,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   204
                            LPCTSTR    szMachineName,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   205
                            LPCTSTR    szObjectName,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   206
                            LPTSTR     mszCounterList,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   207
                            LPDWORD    pcchCounterListLength,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   208
                            LPTSTR     mszInstanceList,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   209
                            LPDWORD    pcchInstanceListLength,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   210
                            DWORD      dwDetailLevel,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   211
                            DWORD      dwFlags
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   212
                            );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   213
typedef PDH_STATUS (WINAPI *PdhRemoveCounterFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   214
                          HCOUNTER  hCounter
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   215
                          );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   216
typedef PDH_STATUS (WINAPI *PdhLookupPerfNameByIndexFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   217
                              LPCSTR  szMachineName,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   218
                              DWORD   dwNameIndex,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   219
                              LPSTR   szNameBuffer,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   220
                              LPDWORD pcchNameBufferSize
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   221
                              );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   222
typedef PDH_STATUS (WINAPI *PdhMakeCounterPathFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   223
                            PDH_COUNTER_PATH_ELEMENTS *pCounterPathElements,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   224
                            LPTSTR szFullPathBuffer,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   225
                            LPDWORD pcchBufferSize,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   226
                            DWORD dwFlags
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   227
                            );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   228
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   229
static PdhAddCounterFunc PdhAddCounter_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   230
static PdhOpenQueryFunc PdhOpenQuery_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   231
static PdhCloseQueryFunc PdhCloseQuery_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   232
static PdhCollectQueryDataFunc PdhCollectQueryData_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   233
static PdhGetFormattedCounterValueFunc PdhGetFormattedCounterValue_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   234
static PdhEnumObjectItemsFunc PdhEnumObjectItems_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   235
static PdhRemoveCounterFunc PdhRemoveCounter_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   236
static PdhLookupPerfNameByIndexFunc PdhLookupPerfNameByIndex_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   237
static PdhMakeCounterPathFunc PdhMakeCounterPath_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   238
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   239
static HANDLE thisProcess;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   240
static double cpuFactor;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   241
static DWORD  num_cpus;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   242
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   243
#define FT2JLONG(X)  ((((jlong)X.dwHighDateTime) << 32) | ((jlong)X.dwLowDateTime))
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   244
#define COUNTER_BUF_SIZE 256
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   245
// Min time between query updates.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   246
#define MIN_UPDATE_INTERVAL 500
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   247
#define CONFIG_SUCCESSFUL 0
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   248
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   249
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   250
 * Struct for PDH queries.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   251
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   252
typedef struct {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   253
    HQUERY      query;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   254
    uint64_t      lastUpdate; // Last time query was updated (current millis).
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   255
} UpdateQueryS, *UpdateQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   256
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   257
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   258
 * Struct for the processor load counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   259
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   260
typedef struct {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   261
    UpdateQueryS      query;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   262
    HCOUNTER*      counters;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   263
    int          noOfCounters;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   264
} MultipleCounterQueryS, *MultipleCounterQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   265
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   266
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   267
 * Struct for the jvm process load counter.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   268
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   269
typedef struct {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   270
    UpdateQueryS      query;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   271
    HCOUNTER      counter;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   272
} SingleCounterQueryS, *SingleCounterQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   273
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   274
static char* getProcessPDHHeader(void);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   275
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   276
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   277
 * Currently available counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   278
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   279
static SingleCounterQueryS cntCtxtSwitchRate;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   280
static SingleCounterQueryS cntVirtualSize;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   281
static SingleCounterQueryS cntProcLoad;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   282
static SingleCounterQueryS cntProcSystemLoad;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   283
static MultipleCounterQueryS multiCounterCPULoad;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   284
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   285
static CRITICAL_SECTION processHeaderLock;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   286
static CRITICAL_SECTION initializationLock;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   287
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   288
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   289
 * Initialize the perf module at startup.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   290
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   291
int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   292
perfiInit(void)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   293
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   294
    InitializeCriticalSection(&processHeaderLock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   295
    InitializeCriticalSection(&initializationLock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   296
    return 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   297
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   298
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   299
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   300
 * Dynamically sets up function pointers to the PDH library.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   301
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   302
 * @return CONFIG_SUCCESSFUL on success, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   303
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   304
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   305
get_functions(HMODULE h, char *ebuf, size_t elen) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   306
    // The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   307
    PdhAddCounter_i         = (PdhAddCounterFunc)GetProcAddress(h, "PdhAddCounterA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   308
    PdhOpenQuery_i         = (PdhOpenQueryFunc)GetProcAddress(h, "PdhOpenQueryA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   309
    PdhCloseQuery_i         = (PdhCloseQueryFunc)GetProcAddress(h, "PdhCloseQuery");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   310
    PdhCollectQueryData_i     = (PdhCollectQueryDataFunc)GetProcAddress(h, "PdhCollectQueryData");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   311
    PdhGetFormattedCounterValue_i = (PdhGetFormattedCounterValueFunc)GetProcAddress(h, "PdhGetFormattedCounterValue");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   312
    PdhEnumObjectItems_i         = (PdhEnumObjectItemsFunc)GetProcAddress(h, "PdhEnumObjectItemsA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   313
    PdhRemoveCounter_i         = (PdhRemoveCounterFunc)GetProcAddress(h, "PdhRemoveCounter");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   314
    PdhLookupPerfNameByIndex_i     = (PdhLookupPerfNameByIndexFunc)GetProcAddress(h, "PdhLookupPerfNameByIndexA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   315
    PdhMakeCounterPath_i         = (PdhMakeCounterPathFunc)GetProcAddress(h, "PdhMakeCounterPathA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   316
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   317
    if (PdhAddCounter_i == NULL || PdhOpenQuery_i == NULL ||
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   318
    PdhCloseQuery_i == NULL || PdhCollectQueryData_i == NULL ||
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   319
    PdhGetFormattedCounterValue_i == NULL || PdhEnumObjectItems_i == NULL ||
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   320
    PdhRemoveCounter_i == NULL || PdhLookupPerfNameByIndex_i == NULL || PdhMakeCounterPath_i == NULL)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   321
    {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   322
        _snprintf(ebuf, elen, "Required method could not be found.");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   323
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   324
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   325
    return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   326
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   327
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   328
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   329
 * Returns the counter value as a double for the specified query.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   330
 * Will collect the query data and update the counter values as necessary.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   331
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   332
 * @param query       the query to update (if needed).
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   333
 * @param c          the counter to read.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   334
 * @param value       where to store the formatted value.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   335
 * @param format      the format to use (i.e. PDH_FMT_DOUBLE, PDH_FMT_LONG etc)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   336
 * @return            CONFIG_SUCCESSFUL if no error
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   337
 *                    -1 if PdhCollectQueryData fails
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   338
 *                    -2 if PdhGetFormattedCounterValue fails
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   339
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   340
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   341
getPerformanceData(UpdateQueryP query, HCOUNTER c, PDH_FMT_COUNTERVALUE* value, DWORD format) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   342
    clock_t now;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   343
    now = clock();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   344
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   345
    // Need to limit how often we update the query
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   346
    // to mimise the heisenberg effect.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   347
    // (PDH behaves erratically if the counters are
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   348
    // queried too often, especially counters that
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   349
    // store and use values from two consecutive updates,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   350
    // like cpu load.)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   351
    if (now - query->lastUpdate > MIN_UPDATE_INTERVAL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   352
        if (PdhCollectQueryData_i(query->query) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   353
            return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   354
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   355
        query->lastUpdate = now;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   356
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   357
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   358
    if (PdhGetFormattedCounterValue_i(c, format, NULL, value) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   359
        return -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   360
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   361
    return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   362
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   363
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   364
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   365
 * Places the resolved counter name of the counter at the specified index in the
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   366
 * supplied buffer. There must be enough space in the buffer to hold the counter name.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   367
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   368
 * @param index   the counter index as specified in the registry.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   369
 * @param buf     the buffer in which to place the counter name.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   370
 * @param size      the size of the counter name buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   371
 * @param ebuf    the error message buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   372
 * @param elen    the length of the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   373
 * @return        CONFIG_SUCCESSFUL if successful, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   374
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   375
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   376
find_name(DWORD index, char *buf, DWORD size) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   377
    PDH_STATUS res;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   378
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   379
    if ((res = PdhLookupPerfNameByIndex_i(NULL, index, buf, &size)) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   380
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   381
        /* printf("Could not open counter %d: error=0x%08x", index, res); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   382
        /* if (res == PDH_CSTATUS_NO_MACHINE) { */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   383
        /*      printf("User probably does not have sufficient privileges to use"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   384
        /*      printf("performance counters. If you are running on Windows 2003"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   385
        /*      printf("or Windows Vista, make sure the user is in the"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   386
        /*      printf("Performance Logs user group."); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   387
        /* } */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   388
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   389
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   390
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   391
    if (size == 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   392
        /* printf("Failed to get counter name for %d: empty string", index); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   393
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   394
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   395
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   396
    // windows vista does not null-terminate the string (allthough the docs says it will)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   397
    buf[size - 1] = '\0';
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   398
    return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   399
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   400
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   401
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   402
 * Sets up the supplied SingleCounterQuery to listen for the specified counter.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   403
 * initPDH() must have been run prior to calling this function!
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   404
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   405
 * @param counterQuery   the counter query to set up.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   406
 * @param counterString  the string specifying the path to the counter.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   407
 * @param ebuf           the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   408
 * @param elen           the length of the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   409
 * @returns              CONFIG_SUCCESSFUL if successful, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   410
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   411
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   412
initSingleCounterQuery(SingleCounterQueryP counterQuery, char *counterString) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   413
    if (PdhOpenQuery_i(NULL, 0, &counterQuery->query.query) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   414
        /* printf("Could not open query for %s", counterString); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   415
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   416
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   417
    if (PdhAddCounter_i(counterQuery->query.query, counterString, 0, &counterQuery->counter) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   418
        /* printf("Could not add counter %s for query", counterString); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   419
        if (counterQuery->counter != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   420
            PdhRemoveCounter_i(counterQuery->counter);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   421
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   422
        if (counterQuery->query.query != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   423
            PdhCloseQuery_i(counterQuery->query.query);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   424
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   425
        memset(counterQuery, 0, sizeof(SingleCounterQueryS));
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   426
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   427
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   428
    return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   429
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   430
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   431
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   432
 * Sets up the supplied SingleCounterQuery to listen for the time spent
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   433
 * by the HotSpot process.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   434
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   435
 * @param counterQuery   the counter query to set up as a process counter.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   436
 * @param ebuf           the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   437
 * @param elen           the length of the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   438
 * @returns              CONFIG_SUCCESSFUL if successful, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   439
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   440
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   441
initProcLoadCounter(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   442
    char time[COUNTER_BUF_SIZE];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   443
    char counter[COUNTER_BUF_SIZE*2];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   444
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   445
    if (find_name(PDH_PROCESSOR_TIME_IDX, time, sizeof(time)-1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   446
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   447
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   448
    _snprintf(counter, sizeof(counter)-1, "%s\\%s", getProcessPDHHeader(), time);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   449
    return initSingleCounterQuery(&cntProcLoad, counter);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   450
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   451
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   452
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   453
initProcSystemLoadCounter(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   454
    char time[COUNTER_BUF_SIZE];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   455
    char counter[COUNTER_BUF_SIZE*2];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   456
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   457
    if (find_name(PDH_PRIV_PROCESSOR_TIME_IDX, time, sizeof(time)-1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   458
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   459
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   460
    _snprintf(counter, sizeof(counter)-1, "%s\\%s", getProcessPDHHeader(), time);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   461
    return initSingleCounterQuery(&cntProcSystemLoad, counter);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   462
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   463
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   464
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   465
 * Sets up the supplied MultipleCounterQuery to check on the processors.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   466
 * (Comment: Refactor and prettify as with the the SingleCounter queries
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   467
 * if more MultipleCounterQueries are discovered.)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   468
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   469
 * initPDH() must have been run prior to calling this function.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   470
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   471
 * @param multiQuery  a pointer to a MultipleCounterQueryS, will be filled in with
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   472
 *                    the necessary info to check the PDH processor counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   473
 * @return            CONFIG_SUCCESSFUL if successful, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   474
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   475
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   476
initProcessorCounters(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   477
    char          processor[COUNTER_BUF_SIZE]; //'Processor' == #238
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   478
    char          time[COUNTER_BUF_SIZE];      //'Time' == 6
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   479
    DWORD      c_size, i_size;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   480
    HQUERY     tmpQuery;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   481
    DWORD      i, p_count;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   482
    BOOL          error;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   483
    char         *instances, *tmp;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   484
    PDH_STATUS pdhStat;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   485
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   486
    c_size   = i_size = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   487
    tmpQuery = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   488
    error    = false;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   489
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   490
    // This __try / __except stuff is there since Windows 2000 beta (or so) sometimes triggered
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   491
    // an access violation when the user had insufficient privileges to use the performance
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   492
    // counters. This was previously guarded by a very ugly piece of code which disabled the
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   493
    // global trap handling in JRockit. Don't know if this really is needed anymore, but otoh,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   494
    // if we keep it we don't crash on Win2k beta. /Ihse, 2005-05-30
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   495
    __try {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   496
        if (find_name(PDH_PROCESSOR_IDX, processor, sizeof(processor)-1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   497
            return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   498
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   499
    } __except (EXCEPTION_EXECUTE_HANDLER) { // We'll catch all exceptions here.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   500
        /* printf("User does not have sufficient privileges to use performance counters"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   501
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   502
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   503
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   504
    if (find_name(PDH_PROCESSOR_TIME_IDX, time, sizeof(time)-1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   505
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   506
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   507
    //ok, now we have enough to enumerate all processors.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   508
    pdhStat = PdhEnumObjectItems_i (
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   509
                    NULL,                   // reserved
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   510
                    NULL,                   // local machine
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   511
                    processor,          // object to enumerate
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   512
                    NULL,              // pass in NULL buffers
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   513
                    &c_size,              // and 0 length to get
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   514
                    NULL,              // required size
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   515
                    &i_size,              // of the buffers in chars
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   516
                    PERF_DETAIL_WIZARD,     // counter detail level
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   517
                    0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   518
    if (pdh_fail(pdhStat)) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   519
        /* printf("could not enumerate processors (1) error=%d", pdhStat); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   520
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   521
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   522
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   523
    // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   524
    instances = calloc(i_size, 1);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   525
    if (instances == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   526
        /* printf("could not allocate memory (1) %d bytes", i_size); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   527
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   528
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   529
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   530
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   531
    c_size  = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   532
    pdhStat = PdhEnumObjectItems_i (
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   533
                    NULL,                   // reserved
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   534
                    NULL,                   // local machine
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   535
                    processor,              // object to enumerate
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   536
                    NULL,              // pass in NULL buffers
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   537
                    &c_size,              // and 0 length to get
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   538
                    instances,          // required size
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   539
                    &i_size,              // of the buffers in chars
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   540
                    PERF_DETAIL_WIZARD,     // counter detail level
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   541
                    0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   542
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   543
    if (pdh_fail(pdhStat)) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   544
        /* printf("could not enumerate processors (2) error=%d", pdhStat); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   545
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   546
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   547
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   548
    //count perf count instances.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   549
    for (p_count = 0, tmp = instances; *tmp != 0; tmp = &tmp[lstrlen(tmp)+1], p_count++);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   550
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   551
    //is this correct for HT?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   552
    assert(p_count == num_cpus+1);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   553
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   554
    //ok, have number of perf counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   555
    multiCounterCPULoad.counters = calloc(p_count, sizeof(HCOUNTER));
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   556
    if (multiCounterCPULoad.counters == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   557
        /* printf("could not allocate memory (2) count=%d", p_count); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   558
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   559
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   560
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   561
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   562
    multiCounterCPULoad.noOfCounters = p_count;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   563
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   564
    if (PdhOpenQuery_i(NULL, 0, &multiCounterCPULoad.query.query) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   565
        /* printf("could not create query"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   566
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   567
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   568
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   569
    //now, fetch the counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   570
    for (i = 0, tmp = instances; *tmp != '\0'; tmp = &tmp[lstrlen(tmp)+1], i++) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   571
    char counter[2*COUNTER_BUF_SIZE];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   572
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   573
    _snprintf(counter, sizeof(counter)-1, "\\%s(%s)\\%s", processor, tmp, time);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   574
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   575
    if (PdhAddCounter_i(multiCounterCPULoad.query.query, counter, 0, &multiCounterCPULoad.counters[i]) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   576
            /* printf("error adding processor counter %s", counter); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   577
            error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   578
            goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   579
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   580
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   581
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   582
    free(instances);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   583
    instances = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   584
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   585
    // Query once to initialize the counters needing at least two queries
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   586
    // (like the % CPU usage) to calculate correctly.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   587
    if (PdhCollectQueryData_i(multiCounterCPULoad.query.query) != ERROR_SUCCESS)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   588
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   589
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   590
 end:
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   591
    if (instances != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   592
        free(instances);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   593
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   594
    if (tmpQuery != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   595
        PdhCloseQuery_i(tmpQuery);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   596
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   597
    if (error) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   598
        int i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   599
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   600
        if (multiCounterCPULoad.counters != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   601
            for (i = 0; i < multiCounterCPULoad.noOfCounters; i++) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   602
                if (multiCounterCPULoad.counters[i] != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   603
                    PdhRemoveCounter_i(multiCounterCPULoad.counters[i]);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   604
                }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   605
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   606
            free(multiCounterCPULoad.counters[i]);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   607
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   608
        if (multiCounterCPULoad.query.query != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   609
            PdhCloseQuery_i(multiCounterCPULoad.query.query);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   610
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   611
        memset(&multiCounterCPULoad, 0, sizeof(MultipleCounterQueryS));
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   612
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   613
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   614
    return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   615
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   616
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   617
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   618
 * Help function that initializes the PDH process header for the JRockit process.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   619
 * (You should probably use getProcessPDHHeader() instead!)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   620
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   621
 * initPDH() must have been run prior to calling this function.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   622
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   623
 * @param ebuf the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   624
 * @param elen the length of the error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   625
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   626
 * @return the PDH instance description corresponding to the JVM process.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   627
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   628
static char*
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   629
initProcessPDHHeader(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   630
    static char hotspotheader[2*COUNTER_BUF_SIZE];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   631
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   632
    char           counter[2*COUNTER_BUF_SIZE];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   633
    char           processes[COUNTER_BUF_SIZE];   //'Process' == #230
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   634
    char           pid[COUNTER_BUF_SIZE];           //'ID Process' == 784
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   635
    char           module_name[MAX_PATH];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   636
    PDH_STATUS  pdhStat;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   637
    DWORD       c_size = 0, i_size = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   638
    HQUERY      tmpQuery = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   639
    int           i, myPid = _getpid();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   640
    BOOL           error = false;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   641
    char          *instances, *tmp, *instance_name, *dot_pos;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   642
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   643
    tmpQuery = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   644
    myPid    = _getpid();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   645
    error    = false;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   646
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   647
    if (find_name(PDH_PROCESS_IDX, processes, sizeof(processes) - 1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   648
        return NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   649
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   650
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   651
    if (find_name(PDH_ID_PROCESS_IDX, pid, sizeof(pid) - 1) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   652
        return NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   653
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   654
    //time is same.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   655
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   656
    c_size = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   657
    i_size = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   658
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   659
    pdhStat = PdhEnumObjectItems_i (
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   660
                    NULL,                   // reserved
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   661
                    NULL,                   // local machine
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   662
                    processes,              // object to enumerate
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   663
                    NULL,                   // pass in NULL buffers
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   664
                    &c_size,              // and 0 length to get
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   665
                    NULL,              // required size
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   666
                    &i_size,              // of the buffers in chars
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   667
                    PERF_DETAIL_WIZARD,     // counter detail level
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   668
                    0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   669
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   670
    //ok, now we have enough to enumerate all processes
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   671
    if (pdh_fail(pdhStat)) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   672
        /* printf("Could not enumerate processes (1) error=%d", pdhStat); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   673
        return NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   674
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   675
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   676
    // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   677
    if ((instances = calloc(i_size, 1)) == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   678
        /* printf("Could not allocate memory %d bytes", i_size); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   679
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   680
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   681
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   682
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   683
    c_size = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   684
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   685
    pdhStat = PdhEnumObjectItems_i (
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   686
                    NULL,                   // reserved
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   687
                    NULL,                   // local machine
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   688
                    processes,              // object to enumerate
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   689
                    NULL,              // pass in NULL buffers
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   690
                    &c_size,              // and 0 length to get
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   691
                    instances,          // required size
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   692
                    &i_size,              // of the buffers in chars
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   693
                    PERF_DETAIL_WIZARD,     // counter detail level
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   694
                    0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   695
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   696
    // ok, now we have enough to enumerate all processes
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   697
    if (pdh_fail(pdhStat)) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   698
        /* printf("Could not enumerate processes (2) error=%d", pdhStat); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   699
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   700
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   701
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   702
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   703
    if (PdhOpenQuery_i(NULL, 0, &tmpQuery) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   704
        /* printf("Could not create temporary query"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   705
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   706
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   707
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   708
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   709
    // Find our module name and use it to extract the instance name used by PDH
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   710
    if (GetModuleFileName(NULL, module_name, MAX_PATH) >= MAX_PATH-1) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   711
        /* printf("Module name truncated"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   712
        error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   713
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   714
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   715
    instance_name = strrchr(module_name, '\\'); //drop path
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   716
    instance_name++;                            //skip slash
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   717
    dot_pos = strchr(instance_name, '.');       //drop .exe
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   718
    dot_pos[0] = '\0';
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   719
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   720
    //now, fetch the counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   721
    for (tmp = instances; *tmp != 0 && !error; tmp = &tmp[lstrlen(tmp)+1]) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   722
        HCOUNTER  hc = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   723
        BOOL done = false;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   724
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   725
        // Skip until we find our own process name
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   726
        if (strcmp(tmp, instance_name) != 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   727
            continue;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   728
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   729
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   730
        // iterate over all instance indexes and try to find our own pid
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   731
        for (i = 0; !done && !error; i++){
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   732
            PDH_STATUS res;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   733
            _snprintf(counter, sizeof(counter)-1, "\\%s(%s#%d)\\%s", processes, tmp, i, pid);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   734
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   735
            if (PdhAddCounter_i(tmpQuery, counter, 0, &hc) != ERROR_SUCCESS) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   736
                /* printf("Failed to create process id query"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   737
                error = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   738
                goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   739
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   740
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   741
            res = PdhCollectQueryData_i(tmpQuery);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   742
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   743
            if (res == PDH_INVALID_HANDLE) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   744
                /* printf("Failed to query process id"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   745
                res = -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   746
                done = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   747
            } else if (res == PDH_NO_DATA) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   748
                done = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   749
            } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   750
                PDH_FMT_COUNTERVALUE cv;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   751
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   752
                PdhGetFormattedCounterValue_i(hc, PDH_FMT_LONG, NULL, &cv);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   753
               /*
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   754
                 * This check seems to be needed for Win2k SMP boxes, since
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   755
                 * they for some reason don't return PDH_NO_DATA for non existing
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   756
                 * counters.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   757
                 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   758
                if (cv.CStatus != PDH_CSTATUS_VALID_DATA) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   759
                    done = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   760
                } else if (cv.longValue == myPid) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   761
                    _snprintf(hotspotheader, sizeof(hotspotheader)-1, "\\%s(%s#%d)\0", processes, tmp, i);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   762
                    PdhRemoveCounter_i(hc);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   763
                    goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   764
                }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   765
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   766
            PdhRemoveCounter_i(hc);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   767
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   768
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   769
 end:
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   770
    if (instances != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   771
        free(instances);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   772
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   773
    if (tmpQuery != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   774
        PdhCloseQuery_i(tmpQuery);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   775
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   776
    if (error) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   777
        return NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   778
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   779
    return hotspotheader;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   780
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   781
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   782
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   783
 * Returns the PDH string prefix identifying the HotSpot process. Use this prefix when getting
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   784
 * counters from the PDH process object representing HotSpot.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   785
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   786
 * Note: this call may take some time to complete.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   787
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   788
 * @param ebuf error buffer.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   789
 * @param elen error buffer length.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   790
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   791
 * @return the header to be used when retrieving PDH counters from the HotSpot process.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   792
 * Will return NULL if the call failed.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   793
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   794
static char *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   795
getProcessPDHHeader(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   796
    static char *processHeader = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   797
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   798
    EnterCriticalSection(&processHeaderLock); {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   799
        if (processHeader == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   800
            processHeader = initProcessPDHHeader();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   801
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   802
    } LeaveCriticalSection(&processHeaderLock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   803
    return processHeader;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   804
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   805
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   806
int perfInit(void);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   807
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   808
double
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   809
perfGetCPULoad(int which)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   810
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   811
    PDH_FMT_COUNTERVALUE cv;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   812
    HCOUNTER            c;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   813
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   814
    if (perfInit() < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   815
        // warn?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   816
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   817
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   818
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   819
    if (multiCounterCPULoad.query.query == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   820
        // warn?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   821
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   822
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   823
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   824
    if (which == -1) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   825
        c = multiCounterCPULoad.counters[multiCounterCPULoad.noOfCounters - 1];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   826
    } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   827
        if (which < multiCounterCPULoad.noOfCounters) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   828
            c = multiCounterCPULoad.counters[which];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   829
        } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   830
            return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   831
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   832
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   833
    if (getPerformanceData(&multiCounterCPULoad.query, c, &cv, PDH_FMT_DOUBLE ) == CONFIG_SUCCESSFUL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   834
        return cv.doubleValue / 100;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   835
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   836
    return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   837
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   838
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   839
double
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   840
perfGetProcessLoad(void)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   841
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   842
    PDH_FMT_COUNTERVALUE cv;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   843
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   844
    if (perfInit() < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   845
        // warn?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   846
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   847
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   848
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   849
    if (cntProcLoad.query.query == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   850
        // warn?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   851
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   852
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   853
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   854
    if (getPerformanceData(&cntProcLoad.query, cntProcLoad.counter, &cv, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100) == CONFIG_SUCCESSFUL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   855
        double d = cv.doubleValue / cpuFactor;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   856
        d = min(1, d);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   857
        d = max(0, d);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   858
        return d;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   859
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   860
    return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   861
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   862
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   863
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   864
 * Helper to initialize the PDH library. Loads the library and sets up the functions.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   865
 * Note that once loaded, we will never unload the PDH library.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   866
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   867
 * @return  CONFIG_SUCCESSFUL if successful, negative on failure.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   868
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   869
int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   870
perfInit(void) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   871
    static HMODULE    h;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   872
    static BOOL        running, inited;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   873
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   874
    int error;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   875
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   876
    if (running) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   877
        return CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   878
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   879
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   880
    error = CONFIG_SUCCESSFUL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   881
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   882
    // this is double checked locking again, but we try to bypass the worst by
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   883
    // implicit membar at end of lock.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   884
    EnterCriticalSection(&initializationLock); {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   885
        if (!inited) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   886
            char         buf[64] = "";
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   887
            SYSTEM_INFO si;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   888
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   889
            // CMH. But windows will not care about our affinity when giving
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   890
            // us measurements. Need the real, raw num cpus.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   891
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   892
            GetSystemInfo(&si);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   893
            num_cpus  = si.dwNumberOfProcessors;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   894
            // Initialize the denominator for the jvm load calculations
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   895
            cpuFactor = num_cpus * 100;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   896
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   897
            /**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   898
             * Do this dynamically, so we don't fail to start on systems without pdh.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   899
             */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   900
            if ((h = LoadLibrary("pdh.dll")) == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   901
                /* printf("Could not load pdh.dll (%d)", GetLastError()); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   902
                error = -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   903
            } else if (get_functions(h, buf, sizeof(buf)) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   904
                FreeLibrary(h);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   905
                h = NULL;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   906
                error = -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   907
               /* printf("Failed to init pdh functions: %s.\n", buf); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   908
            } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   909
                if (initProcessorCounters() != 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   910
                    /* printf("Failed to init system load counters.\n"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   911
                } else if (initProcLoadCounter() != 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   912
                    /* printf("Failed to init process load counter.\n"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   913
                } else if (initProcSystemLoadCounter() != 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   914
                    /* printf("Failed to init process system load counter.\n"); */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   915
                } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   916
                    inited = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   917
                }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   918
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   919
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   920
    } LeaveCriticalSection(&initializationLock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   921
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   922
    if (inited && error == CONFIG_SUCCESSFUL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   923
        running = true;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   924
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   925
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   926
    return error;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   927
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   928
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   929
JNIEXPORT jdouble JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   930
Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   931
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   932
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   933
    return perfGetCPULoad(-1);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   934
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   935
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   936
JNIEXPORT jdouble JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 13668
diff changeset
   937
Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   938
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   939
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   940
    return perfGetProcessLoad();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   941
}