src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30355 jdk/src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c@e37c7eba132f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
     2
 * Copyright (c) 2003, 2015, 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"
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
    30
#include "management_ext.h"
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
    31
#include "com_sun_management_internal_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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static void set_low(jlong* value, jint low) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    *value &= (jlong)0xffffffff << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    *value |= (jlong)(julong)(juint)low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
static void set_high(jlong* value, jint high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    *value &= (jlong)(julong)(juint)0xffffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    *value |= (jlong)high       << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jlong jlong_from(jint h, jint l) {
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    67
    jlong result = 0; // initialization to avoid warning
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    68
    set_high(&result, h);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    69
    set_low(&result,  l);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    70
    return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static HANDLE main_process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    75
static void perfInit(void);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
JNIEXPORT void JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
    78
Java_com_sun_management_internal_OperatingSystemImpl_initialize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    main_process = GetCurrentProcess();
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
    82
    perfInit();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
    86
Java_com_sun_management_internal_OperatingSystemImpl_getCommittedVirtualMemorySize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    PROCESS_MEMORY_COUNTERS pmc;
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    90
    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
    91
        return (jlong)-1L;
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    92
    } else {
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
    93
        return (jlong) pmc.PagefileUsage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
    98
Java_com_sun_management_internal_OperatingSystemImpl_getTotalSwapSpaceSize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   101
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   102
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   103
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   104
    return (jlong) ms.ullTotalPageFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
   108
Java_com_sun_management_internal_OperatingSystemImpl_getFreeSwapSpaceSize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   111
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   112
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   113
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   114
    return (jlong) ms.ullAvailPageFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
   118
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuTime0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    FILETIME process_creation_time, process_exit_time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             process_user_time, process_kernel_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   125
    // Using static variables declared above
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   126
    // Units are 100-ns intervals.  Convert to ns.
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   127
    GetProcessTimes(main_process, &process_creation_time,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   128
                    &process_exit_time,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   129
                    &process_kernel_time, &process_user_time);
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   130
    return (jlong_from(process_user_time.dwHighDateTime,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   131
                        process_user_time.dwLowDateTime) +
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   132
            jlong_from(process_kernel_time.dwHighDateTime,
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   133
                        process_kernel_time.dwLowDateTime)) * 100;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
   137
Java_com_sun_management_internal_OperatingSystemImpl_getFreePhysicalMemorySize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   140
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   141
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   142
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   143
    return (jlong) ms.ullAvailPhys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNIEXPORT jlong JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
   147
Java_com_sun_management_internal_OperatingSystemImpl_getTotalPhysicalMemorySize0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
13668
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   150
    MEMORYSTATUSEX ms;
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   151
    ms.dwLength = sizeof(ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   152
    GlobalMemoryStatusEx(&ms);
c85b9828f8ff 6853676: OperatingSystemMXBean.TotalPhysicalMemorySize has incorrect value
sla
parents: 9559
diff changeset
   153
    return (jlong) ms.ullTotalPhys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   155
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   156
/* Performance Data Helper API (PDH) support */
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
typedef PDH_STATUS (WINAPI *PdhAddCounterFunc)(
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   159
                           HQUERY      hQuery,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   160
                           LPCSTR      szFullCounterPath,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   161
                           DWORD       dwUserData,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   162
                           HCOUNTER    *phCounter
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
typedef PDH_STATUS (WINAPI *PdhOpenQueryFunc)(
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   165
                           LPCWSTR     szDataSource,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   166
                           DWORD       dwUserData,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   167
                           HQUERY      *phQuery
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   168
                           );
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   169
typedef PDH_STATUS (WINAPI *PdhCollectQueryDataFunc)(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   170
                           HQUERY      hQuery
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   171
                           );
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   172
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   173
typedef PDH_STATUS (WINAPI *PdhEnumObjectItemsFunc)(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   174
                           LPCTSTR     szDataSource,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   175
                           LPCTSTR     szMachineName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   176
                           LPCTSTR     szObjectName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   177
                           LPTSTR      mszCounterList,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   178
                           LPDWORD     pcchCounterListLength,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   179
                           LPTSTR      mszInstanceList,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   180
                           LPDWORD     pcchInstanceListLength,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   181
                           DWORD       dwDetailLevel,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   182
                           DWORD       dwFlags
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   183
                           );
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   184
typedef PDH_STATUS (WINAPI *PdhRemoveCounterFunc)(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   185
                           HCOUNTER   hCounter
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   186
                           );
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   187
typedef PDH_STATUS (WINAPI *PdhLookupPerfNameByIndexFunc)(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   188
                           LPCSTR     szMachineName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   189
                           DWORD      dwNameIndex,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   190
                           LPSTR      szNameBuffer,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   191
                           LPDWORD    pcchNameBufferSize
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   192
                           );
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   193
typedef DWORD (WINAPI *PdhCloseQueryFunc)(
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
                      );
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   196
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   197
typedef DWORD (WINAPI *PdhGetFormattedCounterValueFunc)(
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   198
                      HCOUNTER                hCounter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   199
                      DWORD                   dwFormat,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   200
                      LPDWORD                 lpdwType,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   201
                      PPDH_FMT_COUNTERVALUE   pValue
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   202
                      );
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   203
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   204
static PdhAddCounterFunc PdhAddCounter_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   205
static PdhOpenQueryFunc PdhOpenQuery_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   206
static PdhCloseQueryFunc PdhCloseQuery_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   207
static PdhCollectQueryDataFunc PdhCollectQueryData_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   208
static PdhGetFormattedCounterValueFunc PdhGetFormattedCounterValue_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   209
static PdhEnumObjectItemsFunc PdhEnumObjectItems_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   210
static PdhRemoveCounterFunc PdhRemoveCounter_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   211
static PdhLookupPerfNameByIndexFunc PdhLookupPerfNameByIndex_i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   212
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   213
/*
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   214
 * Struct for PDH queries.
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 struct {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   217
    HQUERY      query;
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   218
    uint64_t    lastUpdate; // Last time query was updated (ticks)
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   219
} UpdateQueryS, *UpdateQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   220
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   221
// Min time between query updates (ticks)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   222
static const int MIN_UPDATE_INTERVAL = 500;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   223
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   224
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   225
 * Struct for a PDH query with multiple counters.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   226
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   227
typedef struct {
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   228
    UpdateQueryS  query;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   229
    HCOUNTER*     counters;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   230
    int           noOfCounters;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   231
} MultipleCounterQueryS, *MultipleCounterQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   232
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   233
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   234
 * Struct for a PDH query with a single counter.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   235
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   236
typedef struct {
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   237
    UpdateQueryS  query;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   238
    HCOUNTER      counter;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   239
} SingleCounterQueryS, *SingleCounterQueryP;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   240
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   241
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   242
typedef struct {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   243
    CRITICAL_SECTION cs;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   244
    DWORD owningThread;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   245
    DWORD recursionCount;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   246
} PdhCriticalSectionS, *PdhCriticalSectionP;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   247
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   248
static PdhCriticalSectionS initializationLock;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   249
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   250
static void InitializePdhCriticalSection(PdhCriticalSectionP criticalSection) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   251
    assert(criticalSection);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   252
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   253
    InitializeCriticalSection(&criticalSection->cs);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   254
    criticalSection->owningThread = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   255
    criticalSection->recursionCount = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   256
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   257
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   258
static void EnterPdhCriticalSection(PdhCriticalSectionP criticalSection) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   259
    assert(criticalSection);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   260
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   261
    EnterCriticalSection(&criticalSection->cs);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   262
    criticalSection->recursionCount++;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   263
    if (!criticalSection->owningThread) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   264
        criticalSection->owningThread = GetCurrentThreadId();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   265
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   266
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   267
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   268
static void LeavePdhCriticalSection(PdhCriticalSectionP criticalSection) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   269
    assert(criticalSection);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   270
    assert(GetCurrentThreadId() == criticalSection->owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   271
    assert(criticalSection->recursionCount >= 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   272
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   273
    criticalSection->recursionCount--;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   274
    if (!criticalSection->recursionCount) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   275
        criticalSection->owningThread = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   276
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   277
    LeaveCriticalSection(&criticalSection->cs);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   278
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   279
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   280
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   281
 * INFO: Using PDH APIs Correctly in a Localized Language (Q287159)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   282
 *   http://support.microsoft.com/default.aspx?scid=kb;EN-US;q287159
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   283
 * The index value for the base system counters and objects like processor,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   284
 * process, thread, memory, and so forth are always the same irrespective
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   285
 * of the localized version of the operating system or service pack installed.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   286
 * To find the correct index for an object or counter, inspect the registry key/value:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   287
 * [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009\Counter]
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   288
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   289
static const DWORD PDH_PROCESSOR_IDX = 238;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   290
static const DWORD PDH_PROCESSOR_TIME_IDX = 6;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   291
static const DWORD PDH_PROCESS_IDX = 230;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   292
static const DWORD PDH_ID_PROCESS_IDX = 784;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   293
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   294
/* useful pdh fmt's */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   295
static const char* const OBJECT_COUNTER_FMT = "\\%s\\%s";
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   296
static const size_t OBJECT_COUNTER_FMT_LEN = 2;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   297
static const char* const OBJECT_WITH_INSTANCES_COUNTER_FMT = "\\%s(%s)\\%s";
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   298
static const size_t OBJECT_WITH_INSTANCES_COUNTER_FMT_LEN = 4;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   299
static const char* const PROCESS_OBJECT_INSTANCE_COUNTER_FMT = "\\%s(%s#%s)\\%s";
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   300
static const size_t PROCESS_OBJECT_INSTANCE_COUNTER_FMT_LEN = 5;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   301
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   302
static const char* pdhProcessImageName = NULL; /* "java" */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   303
static char* pdhIDProcessCounterFmt = NULL;    /* "\Process(java#%d)\ID Process" */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   304
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   305
static int numberOfJavaProcessesAtInitialization = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   306
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   307
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   308
 * Currently used CPU queries/counters and variables
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   309
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   310
static SingleCounterQueryP processTotalCPULoad = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   311
static MultipleCounterQueryP multiCounterCPULoad = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   312
static double cpuFactor = .0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   313
static DWORD  numCpus = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   314
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   315
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   316
 * Seems WinXP PDH returns PDH_MORE_DATA whenever we send in a NULL buffer.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   317
 * Let's just ignore it, since we make sure we have enough buffer anyway.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   318
 */
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   319
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   320
pdhFail(PDH_STATUS pdhStat) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   321
    return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   322
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   323
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   324
static const char*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   325
allocateAndCopy(const char* const originalString) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   326
    size_t len;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   327
    char* allocatedString;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   328
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   329
    assert(originalString);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   330
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   331
    len = strlen(originalString);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   332
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   333
    allocatedString = malloc(len + 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   334
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   335
    if (!allocatedString) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   336
        return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   337
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   338
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   339
    strncpy(allocatedString, originalString, len);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   340
    allocatedString[len] = '\0';
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   341
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   342
    return allocatedString;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   343
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   344
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   345
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   346
 * Allocates memory into the supplied pointer and
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   347
 * fills it with the localized PDH artifact description, if indexed correctly.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   348
 * Caller owns the memory from the point of returning from this function.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   349
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   350
 * @param index    the PDH counter index as specified in the registry
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   351
 * @param ppBuffer pointer to a char*.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   352
 * @return         0 if successful, negative on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   353
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   354
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   355
lookupNameByIndex(DWORD index, char** ppBuffer) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   356
    DWORD size;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   357
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   358
    assert(ppBuffer);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   359
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   360
    /* determine size needed */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   361
    if (PdhLookupPerfNameByIndex_i(NULL, index, NULL, &size) != PDH_MORE_DATA) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   362
      /* invalid index? */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   363
      return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   364
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   365
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   366
    *ppBuffer = malloc((size_t)size);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   367
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   368
    if (!*ppBuffer) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   369
        return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   370
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   371
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   372
    if (PdhLookupPerfNameByIndex_i(NULL, index, *ppBuffer, &size) != ERROR_SUCCESS) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   373
        free(*ppBuffer);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   374
        *ppBuffer = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   375
        return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   376
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   377
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   378
    /* windows vista does not null-terminate the string
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   379
     * (although the docs says it will) */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   380
    (*ppBuffer)[size - 1] = '\0';
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   381
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   382
    return 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   383
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   384
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   385
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   386
* Construct a fully qualified PDH path
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   387
*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   388
* @param objectName   a PDH Object string representation (required)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   389
* @param counterName  a PDH Counter string representation (required)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   390
* @param imageName    a process image name string, ex. "java" (opt)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   391
* @param instance     an instance string, ex. "0", "1", ... (opt)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   392
* @return             the fully qualified PDH path.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   393
*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   394
* Caller will own the returned malloc:ed string
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   395
*/
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   396
static const char*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   397
makeFullCounterPath(const char* const objectName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   398
                    const char* const counterName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   399
                    const char* const imageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   400
                    const char* const instance) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   401
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   402
    size_t fullCounterPathLen;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   403
    char* fullCounterPath;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   404
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   405
    assert(objectName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   406
    assert(counterName);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   407
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   408
    fullCounterPathLen = strlen(objectName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   409
    fullCounterPathLen += strlen(counterName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   410
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   411
    if (imageName) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   412
        /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   413
         * For paths using the "Process" Object.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   414
         *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   415
         * Examples:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   416
         * abstract: "\Process(imageName#instance)\Counter"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   417
         * actual:   "\Process(java#2)\ID Process"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   418
         */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   419
        fullCounterPathLen += PROCESS_OBJECT_INSTANCE_COUNTER_FMT_LEN;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   420
        fullCounterPathLen += strlen(imageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   421
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   422
        /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   423
         * imageName must be passed together with an associated
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   424
         * instance "number" ("0", "1", "2", ...).
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   425
         * This is required in order to create valid "Process" Object paths.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   426
         *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   427
         * Examples: "\Process(java#0)", \Process(java#1"), ...
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   428
         */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   429
        assert(instance);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   430
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   431
        fullCounterPathLen += strlen(instance);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   432
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   433
        fullCounterPath = malloc(fullCounterPathLen + 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   434
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   435
        if (!fullCounterPath) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   436
            return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   437
        }
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   438
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   439
        _snprintf(fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   440
                  fullCounterPathLen,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   441
                  PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   442
                  objectName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   443
                  imageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   444
                  instance,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   445
                  counterName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   446
    } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   447
        if (instance) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   448
            /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   449
             * For paths where the Object has multiple instances.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   450
             *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   451
             * Examples:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   452
             * abstract: "\Object(instance)\Counter"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   453
             * actual:   "\Processor(0)\% Privileged Time"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   454
             */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   455
            fullCounterPathLen += strlen(instance);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   456
            fullCounterPathLen += OBJECT_WITH_INSTANCES_COUNTER_FMT_LEN;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   457
        } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   458
            /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   459
             * For "normal" paths.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   460
             *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   461
             * Examples:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   462
             * abstract: "\Object\Counter"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   463
             * actual:   "\Memory\Available Mbytes"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   464
             */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   465
            fullCounterPathLen += OBJECT_COUNTER_FMT_LEN;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   466
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   467
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   468
        fullCounterPath = malloc(fullCounterPathLen + 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   469
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   470
        if (!fullCounterPath) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   471
            return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   472
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   473
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   474
        if (instance) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   475
            _snprintf(fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   476
                      fullCounterPathLen,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   477
                      OBJECT_WITH_INSTANCES_COUNTER_FMT,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   478
                      objectName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   479
                      instance,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   480
                      counterName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   481
        } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   482
            _snprintf(fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   483
                      fullCounterPathLen,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   484
                      OBJECT_COUNTER_FMT,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   485
                      objectName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   486
                      counterName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   487
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   488
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   489
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   490
    fullCounterPath[fullCounterPathLen] = '\0';
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   491
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   492
    return fullCounterPath;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   493
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   495
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   496
 * Resolves an index for a PDH artifact to
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   497
 * a localized, malloc:ed string representation.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   498
 * Caller will own the returned malloc:ed string.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   499
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   500
 * @param pdhArtifactIndex  PDH index
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   501
 * @return                  malloc:ed string representation
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   502
 *                          of the requested pdh artifact (localized).
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   503
 *                          NULL on failure.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   504
 */
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   505
static const char*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   506
getPdhLocalizedArtifact(DWORD pdhArtifactIndex) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   507
    char* pdhLocalizedArtifactString;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   508
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   509
    if (lookupNameByIndex(pdhArtifactIndex,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   510
                          &pdhLocalizedArtifactString) != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   511
        return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   512
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   513
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   514
    return pdhLocalizedArtifactString;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   515
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   516
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   517
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   518
pdhCleanup(HQUERY* const query, HCOUNTER* const counter) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   519
    if (counter && *counter) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   520
        PdhRemoveCounter_i(*counter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   521
        *counter = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   522
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   523
    if (query && *query) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   524
        PdhCloseQuery_i(*query);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   525
        *query = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   526
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   527
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   528
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   529
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   530
destroySingleCounter(SingleCounterQueryP counterQuery) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   531
    if (counterQuery) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   532
        pdhCleanup(&counterQuery->query.query, &counterQuery->counter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   533
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   534
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   535
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   536
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   537
destroyMultiCounter(MultipleCounterQueryP multiCounterQuery) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   538
    int i;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   539
    if (multiCounterQuery) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   540
        if (multiCounterQuery->counters) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   541
            for (i = 0; i < multiCounterQuery->noOfCounters; i++) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   542
                pdhCleanup(NULL, &multiCounterQuery->counters[i]);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   543
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   544
            free(multiCounterQuery->counters);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   545
            multiCounterQuery->counters = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   546
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   547
        pdhCleanup(&multiCounterQuery->query.query, NULL);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   548
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   549
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   550
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   551
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   552
openQuery(HQUERY* const query) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   553
    assert(query);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   554
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   555
    if (PdhOpenQuery_i(NULL, 0, query) != ERROR_SUCCESS) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   556
        return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   557
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   558
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   559
    return 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   560
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   561
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   562
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   563
addCounter(HQUERY query,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   564
           const char* const fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   565
           HCOUNTER* const counter) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   566
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   567
    assert(fullCounterPath);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   568
    assert(counter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   569
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   570
    if (PdhAddCounter_i(query,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   571
                        fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   572
                        0,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   573
                        counter) != ERROR_SUCCESS) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   574
        return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   575
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   576
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   577
    return 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   578
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   579
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   580
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   581
 * Sets up the supplied SingleCounterQuery to listen for the specified counter.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   582
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   583
 * @param counterQuery       the counter query to set up.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   584
 * @param fullCounterPath    the string specifying the full path to the counter.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   585
 * @returns                  0 if successful, negative on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   586
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   587
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   588
initializeSingleCounterQuery(SingleCounterQueryP counterQuery,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   589
                             const char* const fullCounterPath) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   590
    assert(counterQuery);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   591
    assert(fullCounterPath);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   592
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   593
    if (openQuery(&counterQuery->query.query) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   594
        if (addCounter(counterQuery->query.query,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   595
                       fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   596
                       &counterQuery->counter) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   597
            return 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   598
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   599
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   600
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   601
    return -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   602
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   603
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   604
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   605
 * Sets up a SingleCounterQuery
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   606
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   607
 * param counter             the counter query to set up.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   608
 * param localizedObject     string representing the PDH object to query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   609
 * param localizedCounter    string representing the PDH counter to query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   610
 * param processImageName    if the counter query needs the process image name ("java")
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   611
 * param instance            if the counter has instances, this is the instance ("\Processor(0)\")
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   612
                                 where 0 is the instance
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   613
 * param firstSampleOnInit   for counters that need two queries to yield their values,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   614
                                 the first query can be issued just after initialization
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   615
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   616
 * @returns                   0 if successful, negative on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   617
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   618
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   619
initializeSingleCounter(SingleCounterQueryP const counter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   620
                        const char* const localizedObject,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   621
                        const char* const localizedCounter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   622
                        const char* const processImageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   623
                        const char* const instance,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   624
                        BOOL firstSampleOnInit) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   625
    int retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   626
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   627
    const char* fullCounterPath = makeFullCounterPath(localizedObject,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   628
                                                      localizedCounter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   629
                                                      processImageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   630
                                                      instance);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   631
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   632
    if (fullCounterPath) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   633
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   634
        assert(counter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   635
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   636
        if (initializeSingleCounterQuery(counter, fullCounterPath) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   637
            /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   638
             * According to the MSDN documentation, rate counters must be read twice:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   639
             *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   640
             * "Obtaining the value of rate counters such as Page faults/sec requires that
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   641
             *  PdhCollectQueryData be called twice, with a specific time interval between
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   642
             *  the two calls, before calling PdhGetFormattedCounterValue. Call Sleep to
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   643
             *  implement the waiting period between the two calls to PdhCollectQueryData."
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   644
             *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   645
             *  Take the first sample here already to allow for the next (first) "real" sample
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   646
             *  to succeed.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   647
             */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   648
            if (firstSampleOnInit) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   649
                PdhCollectQueryData_i(counter->query.query);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   650
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   651
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   652
            retValue = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   653
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   654
        free((char*)fullCounterPath);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   655
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   656
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   657
    return retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   658
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   659
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   660
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   661
perfInit(void) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   662
    InitializePdhCriticalSection(&initializationLock);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   663
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   664
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   665
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   666
getProcessID() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   667
    static int myPid = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   668
    if (0 == myPid) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   669
        myPid = _getpid();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   670
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   671
    return myPid;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   672
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   673
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   674
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   675
 * Working against the Process object and it's related counters is inherently problematic
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   676
 * when using the PDH API:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   677
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   678
 * For PDH, a process is not primarily identified by it's process id,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   679
 * but with a sequential number, for example \Process(java#0), \Process(java#1), ....
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   680
 * The really bad part is that this list is reset as soon as one process exits:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   681
 * If \Process(java#1) exits, \Process(java#3) now becomes \Process(java#2) etc.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   682
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   683
 * The PDH query api requires a process identifier to be submitted when registering
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   684
 * a query, but as soon as the list resets, the query is invalidated (since the name
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   685
 * changed).
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   686
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   687
 * Solution:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   688
 * The #number identifier for a Process query can only decrease after process creation.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   689
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   690
 * Therefore we create an array of counter queries for all process object instances
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   691
 * up to and including ourselves:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   692
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   693
 * Ex. we come in as third process instance (java#2), we then create and register
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   694
 * queries for the following Process object instances:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   695
 * java#0, java#1, java#2
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   696
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   697
 * currentQueryIndexForProcess() keeps track of the current "correct" query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   698
 * (in order to keep this index valid when the list resets from underneath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   699
 * ensure to call getCurrentQueryIndexForProcess() before every query involving
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   700
 * Process object instance data).
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   701
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   702
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   703
currentQueryIndexForProcess(void) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   704
    HQUERY tmpQuery = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   705
    HCOUNTER handleCounter = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   706
    int retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   707
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   708
    assert(pdhProcessImageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   709
    assert(pdhIDProcessCounterFmt);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   710
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   711
    if (openQuery(&tmpQuery) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   712
        int index;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   713
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   714
        /* iterate over all instance indexes and try to find our own pid */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   715
        for (index = 0; index < INT_MAX; ++index) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   716
            char fullIDProcessCounterPath[MAX_PATH];
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   717
            PDH_FMT_COUNTERVALUE counterValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   718
            PDH_STATUS res;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   719
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   720
            _snprintf(fullIDProcessCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   721
                      MAX_PATH,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   722
                      pdhIDProcessCounterFmt,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   723
                      index);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   724
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   725
            if (addCounter(tmpQuery, fullIDProcessCounterPath, &handleCounter) != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   726
                break;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   727
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   728
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   729
            res = PdhCollectQueryData_i(tmpQuery);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   730
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   731
            if (PDH_INVALID_HANDLE == res || PDH_NO_DATA == res) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   732
                break;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   733
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   734
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   735
            PdhGetFormattedCounterValue_i(handleCounter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   736
                                          PDH_FMT_LONG,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   737
                                          NULL,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   738
                                          &counterValue);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   739
            /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   740
             * This check seems to be needed for Win2k SMP boxes, since
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   741
             * they for some reason don't return PDH_NO_DATA for non existing
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   742
             * counters.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   743
             */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   744
            if (counterValue.CStatus != PDH_CSTATUS_VALID_DATA) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   745
                break;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   746
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   747
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   748
            if ((LONG)getProcessID() == counterValue.longValue) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   749
                retValue = index;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   750
                break;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   751
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   752
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   753
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   754
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   755
    pdhCleanup(&tmpQuery, &handleCounter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   756
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   757
    return retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   758
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   759
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   760
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   761
 * If successful, returns the #index corresponding to our PID
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   762
 * as resolved by the pdh query:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   763
 * "\Process(java#index)\ID Process" (or localized equivalent)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   764
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   765
 * This function should be called before attempting to read
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   766
 * from any Process related counter(s), and the return value
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   767
 * is the index to be used for indexing an array of Process object query's:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   768
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   769
 * Example:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   770
 * processTotalCPULoad[currentQueryIndex].query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   771
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   772
 * Returns -1 on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   773
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   774
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   775
getCurrentQueryIndexForProcess() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   776
    int currentQueryIndex = currentQueryIndexForProcess();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   777
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   778
    assert(currentQueryIndex >= 0 &&
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   779
           currentQueryIndex < numberOfJavaProcessesAtInitialization);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   780
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   781
    return currentQueryIndex;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   782
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   783
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   784
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   785
 * Returns the PDH string identifying the current process image name.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   786
 * Use this name as a qualifier when getting counters from the PDH Process Object
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   787
 * representing your process.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   788
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   789
 * Example:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   790
 * "\Process(java#0)\Virtual Bytes" - where "java" is the PDH process
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   791
 * image name.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   792
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   793
 * Please note that the process image name is not necessarily "java",
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   794
 * hence the use of GetModuleFileName() to detect the process image name.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   795
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   796
 * @return   the process image name to be used when retrieving
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   797
 *           PDH counters from the current process. The caller will
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   798
             own the returned malloc:ed string. NULL if failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   799
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   800
static const char*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   801
getPdhProcessImageName() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   802
    char moduleName[MAX_PATH];
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   803
    char* processImageName;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   804
    char* dotPos;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   805
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   806
    // Find our module name and use it to extract the image name used by PDH
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   807
    DWORD getmfnReturn = GetModuleFileName(NULL, moduleName, sizeof(moduleName));
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   808
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   809
    if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   810
        return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   811
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   812
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   813
    if (getmfnReturn >= MAX_PATH || 0 == getmfnReturn) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   814
        return NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   815
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   816
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   817
    processImageName = strrchr(moduleName, '\\'); //drop path
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   818
    processImageName++;                           //skip slash
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   819
    dotPos = strrchr(processImageName, '.');      //drop .exe
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   820
    dotPos[0] = '\0';
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   821
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   822
    return allocateAndCopy(processImageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   823
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   824
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   825
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   826
 * Sets up the supplied MultipleCounterQuery to check on the processors via PDH CPU counters.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   827
 * TODO: Refactor and prettify as with the the SingleCounter queries
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   828
 * if more MultipleCounterQueries are discovered/needed.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   829
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   830
 * @param multiCounterCPULoad  a pointer to a MultipleCounterQueryS, will be filled in with
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   831
 *                             the necessary info to check the PDH processor counters.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   832
 * @return                     0 if successful, negative on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   833
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   834
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   835
initializeMultipleCounterForCPUs(MultipleCounterQueryP multiCounterCPULoad) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   836
    DWORD cSize = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   837
    DWORD iSize = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   838
    DWORD pCount;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   839
    DWORD index;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   840
    char* processor = NULL; //'Processor' == PDH_PROCESSOR_IDX
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   841
    char* time = NULL;      //'Time' == PDH_PROCESSOR_TIME_IDX
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   842
    char* instances = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   843
    char* tmp;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   844
    int   retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   845
    PDH_STATUS pdhStat;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   846
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   847
    if (lookupNameByIndex(PDH_PROCESSOR_IDX, &processor) != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   848
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   849
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   850
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   851
    if (lookupNameByIndex(PDH_PROCESSOR_TIME_IDX, &time) != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   852
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   853
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   854
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   855
    //ok, now we have enough to enumerate all processors.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   856
    pdhStat = PdhEnumObjectItems_i(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   857
                                   NULL, // reserved
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   858
                                   NULL, // local machine
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   859
                                   processor, // object to enumerate
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   860
                                   NULL, // pass in NULL buffers
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   861
                                   &cSize, // and 0 length to get
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   862
                                   NULL, // required size
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   863
                                   &iSize, // of the buffers in chars
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   864
                                   PERF_DETAIL_WIZARD, // counter detail level
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   865
                                   0);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   866
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   867
    if (pdhFail(pdhStat)) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   868
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   869
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   870
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   871
    instances = calloc(iSize, 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   872
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   873
    if (!instances) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   874
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   875
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   876
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   877
    cSize = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   878
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   879
    pdhStat = PdhEnumObjectItems_i(
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   880
                                   NULL, // reserved
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   881
                                   NULL, // local machine
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   882
                                   processor, // object to enumerate
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   883
                                   NULL, // pass in NULL buffers
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   884
                                   &cSize,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   885
                                   instances, // now allocated to be filled in
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   886
                                   &iSize, // and size is known
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   887
                                   PERF_DETAIL_WIZARD, // counter detail level
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   888
                                   0);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   889
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   890
    if (pdhFail(pdhStat)) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   891
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   892
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   893
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   894
    // enumerate the Processor instances ("\Processor(0)", "\Processor(1)", ..., "\Processor(_Total)")
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   895
    for (pCount = 0, tmp = instances; *tmp != '\0'; tmp = &tmp[strlen(tmp)+1], pCount++);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   896
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   897
    assert(pCount == numCpus+1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   898
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   899
    //ok, we now have the number of Processor instances - allocate an HCOUNTER for each
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   900
    multiCounterCPULoad->counters = (HCOUNTER*)malloc(pCount * sizeof(HCOUNTER));
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   901
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   902
    if (!multiCounterCPULoad->counters) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   903
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   904
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   905
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   906
    multiCounterCPULoad->noOfCounters = pCount;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   907
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   908
    if (openQuery(&multiCounterCPULoad->query.query) != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   909
        goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   910
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   911
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   912
    // fetch instance and register its corresponding HCOUNTER with the query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   913
    for (index = 0, tmp = instances; *tmp != '\0'; tmp = &tmp[strlen(tmp)+1], ++index) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   914
        const char* const fullCounterPath = makeFullCounterPath(processor, time, NULL, tmp);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   915
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   916
        if (!fullCounterPath) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   917
            goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   918
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   919
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   920
        retValue = addCounter(multiCounterCPULoad->query.query,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   921
                              fullCounterPath,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   922
                              &multiCounterCPULoad->counters[index]);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   923
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   924
        free((char*)fullCounterPath);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   925
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   926
        if (retValue != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   927
            goto end;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   928
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   929
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   930
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   931
    // Query once to initialize the counters which require at least two samples
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   932
    // (like the % CPU usage) to calculate correctly.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   933
    PdhCollectQueryData_i(multiCounterCPULoad->query.query);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   934
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   935
  end:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   936
    if (processor) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   937
        free(processor);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   938
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   939
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   940
    if (time) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   941
        free(time);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   942
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   943
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   944
    if (instances) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   945
        free(instances);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   946
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   947
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   948
    return retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   949
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   950
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   951
/*
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   952
 * 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
   953
 *
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   954
 * @param h  HMODULE for the PDH library
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   955
 * @return   0 on success, negative on failure.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   956
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   957
static int
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   958
bindPdhFunctionPointers(HMODULE h) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   959
    assert(h);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   960
    assert(GetCurrentThreadId() == initializationLock.owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   961
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   962
    /* The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods */
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   963
    PdhAddCounter_i         = (PdhAddCounterFunc)GetProcAddress(h, "PdhAddCounterA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   964
    PdhOpenQuery_i         = (PdhOpenQueryFunc)GetProcAddress(h, "PdhOpenQueryA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   965
    PdhCloseQuery_i         = (PdhCloseQueryFunc)GetProcAddress(h, "PdhCloseQuery");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   966
    PdhCollectQueryData_i     = (PdhCollectQueryDataFunc)GetProcAddress(h, "PdhCollectQueryData");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   967
    PdhGetFormattedCounterValue_i = (PdhGetFormattedCounterValueFunc)GetProcAddress(h, "PdhGetFormattedCounterValue");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   968
    PdhEnumObjectItems_i         = (PdhEnumObjectItemsFunc)GetProcAddress(h, "PdhEnumObjectItemsA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   969
    PdhRemoveCounter_i         = (PdhRemoveCounterFunc)GetProcAddress(h, "PdhRemoveCounter");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   970
    PdhLookupPerfNameByIndex_i     = (PdhLookupPerfNameByIndexFunc)GetProcAddress(h, "PdhLookupPerfNameByIndexA");
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   971
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   972
    if (!PdhAddCounter_i || !PdhOpenQuery_i ||
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   973
        !PdhCloseQuery_i || !PdhCollectQueryData_i ||
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   974
        !PdhGetFormattedCounterValue_i || !PdhEnumObjectItems_i ||
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   975
        !PdhRemoveCounter_i || !PdhLookupPerfNameByIndex_i)
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   976
    {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   977
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   978
    }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   979
    return 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   980
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   981
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   982
/*
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   983
 * 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
   984
 * 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
   985
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   986
 * @param query       the query to update (if needed).
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   987
 * @param c           the counter to read.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   988
 * @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
   989
 * @param format      the format to use (i.e. PDH_FMT_DOUBLE, PDH_FMT_LONG etc)
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   990
 * @return            0 if no error
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   991
 *                    -1 if PdhCollectQueryData fails
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   992
 *                    -2 if PdhGetFormattedCounterValue fails
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   993
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   994
static int
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   995
getPerformanceData(UpdateQueryP query, HCOUNTER c, PDH_FMT_COUNTERVALUE* value, DWORD format) {
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   996
    clock_t now = clock();
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
   997
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   998
    /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
   999
     * Need to limit how often we update the query
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1000
     * to minimize the Heisenberg effect.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1001
     * (PDH behaves erratically if the counters are
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1002
     * queried too often, especially counters that
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1003
     * store and use values from two consecutive updates,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1004
     * like cpu load.)
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1005
     */
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1006
    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
  1007
        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
  1008
            return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1009
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1010
        query->lastUpdate = now;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1011
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1012
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1013
    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
  1014
        return -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1015
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1016
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1017
    return 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1018
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1019
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1020
static int
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1021
allocateAndInitializePdhConstants() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1022
    const char* pdhLocalizedProcessObject = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1023
    const char* pdhLocalizedIDProcessCounter = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1024
    size_t pdhIDProcessCounterFmtLen;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1025
    int currentQueryIndex;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1026
    int retValue = -1;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1027
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1028
    assert(GetCurrentThreadId() == initializationLock.owningThread);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1029
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1030
    assert(!pdhProcessImageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1031
    pdhProcessImageName = getPdhProcessImageName();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1032
    if (!pdhProcessImageName) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1033
        goto end;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1034
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1035
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1036
    pdhLocalizedProcessObject = getPdhLocalizedArtifact(PDH_PROCESS_IDX);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1037
    if (!pdhLocalizedProcessObject) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1038
        goto end;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1039
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1040
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1041
    pdhLocalizedIDProcessCounter = getPdhLocalizedArtifact(PDH_ID_PROCESS_IDX);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1042
    if (!pdhLocalizedIDProcessCounter) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1043
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1044
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1045
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1046
    assert(!pdhIDProcessCounterFmt);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1047
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1048
    pdhIDProcessCounterFmtLen = strlen(pdhProcessImageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1049
    pdhIDProcessCounterFmtLen += strlen(pdhLocalizedProcessObject);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1050
    pdhIDProcessCounterFmtLen += strlen(pdhLocalizedIDProcessCounter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1051
    pdhIDProcessCounterFmtLen += PROCESS_OBJECT_INSTANCE_COUNTER_FMT_LEN;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1052
    pdhIDProcessCounterFmtLen += 2; // "%d"
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1053
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1054
    assert(pdhIDProcessCounterFmtLen < MAX_PATH);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1055
    pdhIDProcessCounterFmt = malloc(pdhIDProcessCounterFmtLen + 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1056
    if (!pdhIDProcessCounterFmt) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1057
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1058
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1059
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1060
    /* "\Process(java#%d)\ID Process" */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1061
    _snprintf(pdhIDProcessCounterFmt,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1062
              pdhIDProcessCounterFmtLen,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1063
              PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1064
              pdhLocalizedProcessObject,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1065
              pdhProcessImageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1066
              "%d",
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1067
              pdhLocalizedIDProcessCounter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1068
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1069
    pdhIDProcessCounterFmt[pdhIDProcessCounterFmtLen] = '\0';
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1070
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1071
    assert(0 == numberOfJavaProcessesAtInitialization);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1072
    currentQueryIndex = currentQueryIndexForProcess();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1073
    if (-1 == currentQueryIndex) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1074
        goto end;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1075
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1076
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1077
    numberOfJavaProcessesAtInitialization = currentQueryIndex + 1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1078
    assert(numberOfJavaProcessesAtInitialization >= 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1079
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1080
    retValue = 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1081
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1082
  end:
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1083
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1084
    if (pdhLocalizedProcessObject) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1085
        free((char*)pdhLocalizedProcessObject);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1086
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1087
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1088
    if (pdhLocalizedIDProcessCounter) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1089
        free((char*)pdhLocalizedIDProcessCounter);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1090
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1091
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1092
    return retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1093
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1094
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1095
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1096
deallocatePdhConstants() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1097
    assert(GetCurrentThreadId() == initializationLock.owningThread);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1098
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1099
    if (pdhProcessImageName) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1100
        free((char*)pdhProcessImageName);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1101
        pdhProcessImageName = NULL;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1102
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1103
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1104
    if (pdhIDProcessCounterFmt) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1105
      free(pdhIDProcessCounterFmt);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1106
      pdhIDProcessCounterFmt = NULL;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1107
    }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1108
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1109
    numberOfJavaProcessesAtInitialization = 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1110
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1111
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1112
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1113
initializeCPUCounters() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1114
    SYSTEM_INFO si;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1115
    char* localizedProcessObject;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1116
    char* localizedProcessorTimeCounter;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1117
    int i;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1118
    int retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1119
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1120
    assert(GetCurrentThreadId() == initializationLock.owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1121
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1122
    assert(0 == numCpus);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1123
    GetSystemInfo(&si);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1124
    numCpus = si.dwNumberOfProcessors;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1125
    assert(numCpus >= 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1126
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1127
    /* Initialize the denominator for the jvm load calculations */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1128
    assert(.0 == cpuFactor);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1129
    cpuFactor = numCpus * 100;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1130
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1131
    if (lookupNameByIndex(PDH_PROCESS_IDX,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1132
                          &localizedProcessObject) == 0) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1133
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1134
        if (lookupNameByIndex(PDH_PROCESSOR_TIME_IDX,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1135
                              &localizedProcessorTimeCounter) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1136
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1137
            assert(processTotalCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1138
            assert(pdhProcessImageName);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1139
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1140
            for (i = 0; i < numberOfJavaProcessesAtInitialization; ++i) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1141
                char instanceIndexBuffer[32];
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1142
                retValue = initializeSingleCounter(&processTotalCPULoad[i],
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1143
                                                   localizedProcessObject,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1144
                                                   localizedProcessorTimeCounter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1145
                                                   pdhProcessImageName,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1146
                                                   itoa(i, instanceIndexBuffer, 10),
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1147
                                                   TRUE);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1148
                if (retValue != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1149
                    break;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1150
                }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1151
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1152
            free(localizedProcessorTimeCounter);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1153
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1154
        free(localizedProcessObject);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1155
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1156
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1157
    if (retValue != 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1158
        return -1;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1159
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1160
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1161
    assert(multiCounterCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1162
    return initializeMultipleCounterForCPUs(multiCounterCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1163
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1164
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1165
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1166
deallocateCPUCounters() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1167
    int i;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1168
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1169
    assert(GetCurrentThreadId() == initializationLock.owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1170
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1171
    if (processTotalCPULoad) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1172
        for (i = 0; i < numberOfJavaProcessesAtInitialization; ++i) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1173
            destroySingleCounter(&processTotalCPULoad[i]);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1174
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1175
        free(processTotalCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1176
        processTotalCPULoad = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1177
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1178
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1179
    if (multiCounterCPULoad) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1180
        destroyMultiCounter(multiCounterCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1181
        free(multiCounterCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1182
        multiCounterCPULoad = NULL;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1183
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1184
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1185
    cpuFactor = .0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1186
    numCpus = 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1187
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1188
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1189
static void
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1190
pdhInitErrorHandler(HMODULE h) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1191
    assert(GetCurrentThreadId() == initializationLock.owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1192
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1193
    deallocatePdhConstants();
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1194
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1195
    if (h) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1196
        FreeLibrary(h);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1197
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1198
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1199
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1200
/*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1201
 * Helper to initialize the PDH library, function pointers and constants.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1202
 *
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1203
 * @return  0 if successful, negative on failure.
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1204
 */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1205
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1206
pdhInit() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1207
    static BOOL initialized = FALSE;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1208
    int retValue;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1209
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1210
    if (initialized) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1211
        return 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1212
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1213
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1214
    retValue = 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1215
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1216
    EnterPdhCriticalSection(&initializationLock); {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1217
        if (!initialized) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1218
            HMODULE h = NULL;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1219
            if ((h = LoadLibrary("pdh.dll")) == NULL) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1220
                retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1221
            } else if (bindPdhFunctionPointers(h) < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1222
                retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1223
            } else if (allocateAndInitializePdhConstants() < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1224
                retValue = -1;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1225
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1226
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1227
            if (0 == retValue) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1228
                initialized = TRUE;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1229
            } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1230
                pdhInitErrorHandler(h);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1231
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1232
        }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1233
    } LeavePdhCriticalSection(&initializationLock);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1234
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1235
    return retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1236
}
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1237
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1238
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1239
allocateCPUCounters() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1240
    assert(GetCurrentThreadId() == initializationLock.owningThread);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1241
    assert(numberOfJavaProcessesAtInitialization >= 1);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1242
    assert(!processTotalCPULoad);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1243
    assert(!multiCounterCPULoad);
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1244
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1245
    /*
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1246
     * Create an array of Process object queries, for each instance
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1247
     * up to and including our own (java#0, java#1, java#2, ...).
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1248
     */
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1249
    processTotalCPULoad = calloc(numberOfJavaProcessesAtInitialization,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1250
                                 sizeof(SingleCounterQueryS));
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1251
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1252
    if (!processTotalCPULoad) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1253
        return -1;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1254
    }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1255
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1256
    multiCounterCPULoad = calloc(1, sizeof(MultipleCounterQueryS));
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1257
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1258
    if (!multiCounterCPULoad) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1259
        return -1;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1260
    }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1261
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1262
    return 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1263
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1264
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1265
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1266
initializePdhCPUCounters() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1267
    static BOOL initialized = FALSE;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1268
    int retValue;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1269
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1270
    if (initialized) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1271
        return 0;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1272
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1273
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1274
    retValue = 0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1275
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1276
    EnterPdhCriticalSection(&initializationLock); {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1277
        if (!initialized) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1278
            if (pdhInit() < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1279
                retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1280
            }  else if (allocateCPUCounters() < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1281
                retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1282
            } else if (initializeCPUCounters() < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1283
                retValue = -1;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1284
            }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1285
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1286
            if (0 == retValue) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1287
                initialized = TRUE;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1288
            } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1289
              deallocateCPUCounters();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1290
            }
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1291
        }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1292
    } LeavePdhCriticalSection(&initializationLock);
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1293
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1294
    return retValue;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1295
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1296
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1297
static int
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1298
perfCPUInit() {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1299
    return initializePdhCPUCounters();
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1300
}
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1301
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1302
static double
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1303
perfGetProcessCPULoad() {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1304
    PDH_FMT_COUNTERVALUE cv;
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1305
    int currentQueryIndex;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1306
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1307
    if (perfCPUInit() < 0) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1308
        // warn?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1309
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1310
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1311
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1312
    currentQueryIndex = getCurrentQueryIndexForProcess();
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1313
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1314
    if (getPerformanceData(&processTotalCPULoad[currentQueryIndex].query,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1315
                           processTotalCPULoad[currentQueryIndex].counter,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1316
                           &cv,
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1317
                           PDH_FMT_DOUBLE | PDH_FMT_NOCAP100) == 0) {
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1318
        double d = cv.doubleValue / cpuFactor;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1319
        d = min(1, d);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1320
        d = max(0, d);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1321
        return d;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1322
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1323
    return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1324
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1325
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1326
static double
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1327
perfGetCPULoad(int which) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1328
    PDH_FMT_COUNTERVALUE cv;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1329
    HCOUNTER c;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1330
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1331
    if (perfCPUInit() < 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1332
        // warn?
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1333
        return -1.0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1334
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1335
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1336
    if (-1 == which) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1337
        c = multiCounterCPULoad->counters[multiCounterCPULoad->noOfCounters - 1];
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1338
    } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1339
        if (which < multiCounterCPULoad->noOfCounters) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1340
            c = multiCounterCPULoad->counters[which];
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1341
        } else {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1342
            return -1.0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1343
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1344
    }
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1345
    if (getPerformanceData(&multiCounterCPULoad->query, c, &cv, PDH_FMT_DOUBLE ) == 0) {
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1346
        return cv.doubleValue / 100;
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1347
    }
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1348
    return -1.0;
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1349
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1350
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1351
JNIEXPORT jdouble JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
  1352
Java_com_sun_management_internal_OperatingSystemImpl_getSystemCpuLoad0
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1353
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1354
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1355
    return perfGetCPULoad(-1);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1356
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1357
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1358
JNIEXPORT jdouble JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27494
diff changeset
  1359
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1360
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1361
{
27494
22353a4c3b4e 8056049: getProcessCpuLoad() stops working in one process when a different process exits
mgronlun
parents: 25859
diff changeset
  1362
    return perfGetProcessCPULoad();
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents: 9027
diff changeset
  1363
}