4162N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4162N/A/* Disable warnings due to broken header files from Microsoft... */ 3901N/A // Using static variables declared above 3901N/A // Units are 100-ns intervals. Convert to ns. 0N/A // also returns dwAvailPhys (free physical memory bytes), 0N/A // dwTotalVirtual, dwAvailVirtual, 0N/A // dwMemoryLoad (% of memory in use) 4162N/A// Seems WinXP PDH returns PDH_MORE_DATA whenever we send in a NULL buffer. 4162N/A// Let's just ignore it, since we make sure we have enough buffer anyway. 4162N/A// INFO: Using PDH APIs Correctly in a Localized Language (Q287159) 4162N/A// The index value for the base system counters and objects like processor, 4162N/A// process, thread, memory, and so forth are always the same irrespective 4162N/A// of the localized version of the operating system or service pack installed. 4162N/A// Min time between query updates. 4162N/A * Struct for the processor load counters. 4162N/A * Struct for the jvm process load counter. 4162N/A * Currently available counters. 4162N/A * Initialize the perf module at startup. 4162N/A * Dynamically sets up function pointers to the PDH library. 4162N/A * @return CONFIG_SUCCESSFUL on success, negative on failure. 4162N/A // The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods 4162N/A * Returns the counter value as a double for the specified query. 4162N/A * Will collect the query data and update the counter values as necessary. 4162N/A * @param query the query to update (if needed). 4162N/A * @param c the counter to read. 4162N/A * @param value where to store the formatted value. 4162N/A * @param format the format to use (i.e. PDH_FMT_DOUBLE, PDH_FMT_LONG etc) 4162N/A * @return CONFIG_SUCCESSFUL if no error 4162N/A * -1 if PdhCollectQueryData fails 4162N/A * -2 if PdhGetFormattedCounterValue fails 4162N/A // Need to limit how often we update the query 4162N/A // to mimise the heisenberg effect. 4162N/A // (PDH behaves erratically if the counters are 4162N/A // queried too often, especially counters that 4162N/A // store and use values from two consecutive updates, 4162N/A * Places the resolved counter name of the counter at the specified index in the 4162N/A * supplied buffer. There must be enough space in the buffer to hold the counter name. 4162N/A * @param index the counter index as specified in the registry. 4162N/A * @param buf the buffer in which to place the counter name. 4162N/A * @param size the size of the counter name buffer. 4162N/A * @param ebuf the error message buffer. 4162N/A * @param elen the length of the error buffer. 4162N/A * @return CONFIG_SUCCESSFUL if successful, negative on failure. 4162N/A /* printf("Could not open counter %d: error=0x%08x", index, res); */ 4162N/A /* if (res == PDH_CSTATUS_NO_MACHINE) { */ 4162N/A /* printf("User probably does not have sufficient privileges to use"); */ 4162N/A /* printf("performance counters. If you are running on Windows 2003"); */ 4162N/A /* printf("or Windows Vista, make sure the user is in the"); */ 4162N/A /* printf("Performance Logs user group."); */ 4162N/A /* printf("Failed to get counter name for %d: empty string", index); */ 4162N/A // windows vista does not null-terminate the string (allthough the docs says it will) 4162N/A * Sets up the supplied SingleCounterQuery to listen for the specified counter. 4162N/A * initPDH() must have been run prior to calling this function! 4162N/A * @param counterQuery the counter query to set up. 4162N/A * @param counterString the string specifying the path to the counter. 4162N/A * @param ebuf the error buffer. 4162N/A * @param elen the length of the error buffer. 4162N/A * @returns CONFIG_SUCCESSFUL if successful, negative on failure. 4162N/A /* printf("Could not open query for %s", counterString); */ 4162N/A /* printf("Could not add counter %s for query", counterString); */ 4162N/A * Sets up the supplied SingleCounterQuery to listen for the time spent 4162N/A * @param counterQuery the counter query to set up as a process counter. 4162N/A * @param ebuf the error buffer. 4162N/A * @param elen the length of the error buffer. 4162N/A * @returns CONFIG_SUCCESSFUL if successful, negative on failure. 4162N/A * Sets up the supplied MultipleCounterQuery to check on the processors. 4162N/A * (Comment: Refactor and prettify as with the the SingleCounter queries 4162N/A * if more MultipleCounterQueries are discovered.) 4162N/A * initPDH() must have been run prior to calling this function. 4162N/A * @param multiQuery a pointer to a MultipleCounterQueryS, will be filled in with 4162N/A * the necessary info to check the PDH processor counters. 4162N/A * @return CONFIG_SUCCESSFUL if successful, negative on failure. 4162N/A // This __try / __except stuff is there since Windows 2000 beta (or so) sometimes triggered 4162N/A // an access violation when the user had insufficient privileges to use the performance 4162N/A // counters. This was previously guarded by a very ugly piece of code which disabled the 4162N/A // global trap handling in JRockit. Don't know if this really is needed anymore, but otoh, 4162N/A // if we keep it we don't crash on Win2k beta. /Ihse, 2005-05-30 4162N/A /* printf("User does not have sufficient privileges to use performance counters"); */ 4162N/A //ok, now we have enough to enumerate all processors. 4162N/A /* printf("could not enumerate processors (1) error=%d", pdhStat); */ 4162N/A // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will) 4162N/A /* printf("could not allocate memory (1) %d bytes", i_size); */ 4162N/A /* printf("could not enumerate processors (2) error=%d", pdhStat); */ 4162N/A //count perf count instances. 4162N/A //ok, have number of perf counters. 4162N/A /* printf("could not allocate memory (2) count=%d", p_count); */ 4162N/A /* printf("could not create query"); */ 4162N/A /* printf("error adding processor counter %s", counter); */ 4162N/A // Query once to initialize the counters needing at least two queries 4162N/A // (like the % CPU usage) to calculate correctly. 4162N/A * Help function that initializes the PDH process header for the JRockit process. 4162N/A * (You should probably use getProcessPDHHeader() instead!) 4162N/A * initPDH() must have been run prior to calling this function. 4162N/A * @param ebuf the error buffer. 4162N/A * @param elen the length of the error buffer. 4162N/A * @return the PDH instance description corresponding to the JVM process. 4162N/A //ok, now we have enough to enumerate all processes 4162N/A /* printf("Could not enumerate processes (1) error=%d", pdhStat); */ 4162N/A // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will) 4162N/A /* printf("Could not allocate memory %d bytes", i_size); */ 4162N/A // ok, now we have enough to enumerate all processes 4162N/A /* printf("Could not enumerate processes (2) error=%d", pdhStat); */ 4162N/A /* printf("Could not create temporary query"); */ 4162N/A // Find our module name and use it to extract the instance name used by PDH 4162N/A /* printf("Module name truncated"); */ 4162N/A // Skip until we find our own process name 4162N/A // iterate over all instance indexes and try to find our own pid 4162N/A /* printf("Failed to create process id query"); */ 4162N/A /* printf("Failed to query process id"); */ 4162N/A * This check seems to be needed for Win2k SMP boxes, since 4162N/A * they for some reason don't return PDH_NO_DATA for non existing 4162N/A * Returns the PDH string prefix identifying the HotSpot process. Use this prefix when getting 4162N/A * counters from the PDH process object representing HotSpot. 4162N/A * Note: this call may take some time to complete. 4162N/A * @param ebuf error buffer. 4162N/A * @param elen error buffer length. 4162N/A * @return the header to be used when retrieving PDH counters from the HotSpot process. 4162N/A * Will return NULL if the call failed. 4162N/A * Helper to initialize the PDH library. Loads the library and sets up the functions. 4162N/A * Note that once loaded, we will never unload the PDH library. 4162N/A * @return CONFIG_SUCCESSFUL if successful, negative on failure. 4162N/A // this is double checked locking again, but we try to bypass the worst by 4162N/A // implicit membar at end of lock. 4162N/A // CMH. But windows will not care about our affinity when giving 4162N/A // us measurements. Need the real, raw num cpus. 4162N/A // Initialize the denominator for the jvm load calculations 4162N/A * Do this dynamically, so we don't fail to start on systems without pdh. 4162N/A /* printf("Could not load pdh.dll (%d)", GetLastError()); */ 4162N/A /* printf("Failed to init pdh functions: %s.\n", buf); */ 4162N/A /* printf("Failed to init system load counters.\n"); */ 4162N/A /* printf("Failed to init process load counter.\n"); */ 4162N/A /* printf("Failed to init process system load counter.\n"); */