src/windows/native/sun/management/OperatingSystemImpl.c
static char*
initProcessPDHHeader(void) {
    static char hotspotheader[2*COUNTER_BUF_SIZE];

    char           counter[2*COUNTER_BUF_SIZE];
    char           processes[COUNTER_BUF_SIZE];   //'Process' == #230
    char           pid[COUNTER_BUF_SIZE];           //'ID Process' == 784
    char           module_name[MAX_PATH];
    PDH_STATUS  pdhStat;
    DWORD       c_size = 0, i_size = 0;
    HQUERY      tmpQuery = NULL;
    int           i, myPid = _getpid();
    BOOL           error = false;
    char          *instances, *tmp, *instance_name, *dot_pos;

    tmpQuery = NULL;
    myPid    = _getpid();
    error    = false;

    if (find_name(PDH_PROCESS_IDX, processes, sizeof(processes) - 1) < 0) {
        return NULL;
    }

    if (find_name(PDH_ID_PROCESS_IDX, pid, sizeof(pid) - 1) < 0) {
        return NULL;
    }
    //time is same.

    c_size = 0;
    i_size = 0;

    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   // reserved
                    NULL,                   // local machine
                    processes,              // object to enumerate
                    NULL,                   // pass in NULL buffers
                    &c_size,              // and 0 length to get
                    NULL,              // required size
                    &i_size,              // of the buffers in chars
                    PERF_DETAIL_WIZARD,     // counter detail level
                    0);

    //ok, now we have enough to enumerate all processes
    if (pdh_fail(pdhStat)) {
        /* printf("Could not enumerate processes (1) error=%d", pdhStat); */
        return NULL;
    }

    // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will)
    if ((instances = calloc(i_size, 1)) == NULL) {
        /* printf("Could not allocate memory %d bytes", i_size); */
        error = true;
        goto end;
    }

    c_size = 0;

    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   // reserved
                    NULL,                   // local machine
                    processes,              // object to enumerate
                    NULL,              // pass in NULL buffers
                    &c_size,              // and 0 length to get
                    instances,          // required size
                    &i_size,              // of the buffers in chars
                    PERF_DETAIL_WIZARD,     // counter detail level
                    0);

    // ok, now we have enough to enumerate all processes
    if (pdh_fail(pdhStat)) {
        /* printf("Could not enumerate processes (2) error=%d", pdhStat); */
        error = true;
        goto end;
    }

    if (PdhOpenQuery_i(NULL, 0, &tmpQuery) != ERROR_SUCCESS) {
        /* printf("Could not create temporary query"); */
        error = true;
        goto end;
    }

    // Find our module name and use it to extract the instance name used by PDH
    if (GetModuleFileName(NULL, module_name, MAX_PATH) >= MAX_PATH-1) {
        /* printf("Module name truncated"); */
        error = true;
        goto end;
    }
    instance_name = strrchr(module_name, '\\'); //drop path
    instance_name++;                            //skip slash
    dot_pos = strchr(instance_name, '.');       //drop .exe
    dot_pos[0] = '\0';

    //now, fetch the counters.
    for (tmp = instances; *tmp != 0 && !error; tmp = &tmp[lstrlen(tmp)+1]) {
        HCOUNTER  hc = NULL;
        BOOL done = false;

        // Skip until we find our own process name
        if (strcmp(tmp, instance_name) != 0) {
            continue;
        }

        // iterate over all instance indexes and try to find our own pid
        for (i = 0; !done && !error; i++){
            PDH_STATUS res;
            _snprintf(counter, sizeof(counter)-1, "\\%s(%s#%d)\\%s", processes, tmp, i, pid);

            if (PdhAddCounter_i(tmpQuery, counter, 0, &hc) != ERROR_SUCCESS) {
                /* printf("Failed to create process id query"); */
                error = true;
                goto end;
            }

            res = PdhCollectQueryData_i(tmpQuery);

            if (res == PDH_INVALID_HANDLE) {
                /* printf("Failed to query process id"); */
                res = -1;
                done = true;
            } else if (res == PDH_NO_DATA) {
                done = true;
            } else {
                PDH_FMT_COUNTERVALUE cv;

                PdhGetFormattedCounterValue_i(hc, PDH_FMT_LONG, NULL, &cv);
               /*
                 * This check seems to be needed for Win2k SMP boxes, since
                 * they for some reason don't return PDH_NO_DATA for non existing
                 * counters.
                 */
                if (cv.CStatus != PDH_CSTATUS_VALID_DATA) {
                    done = true;
                } else if (cv.longValue == myPid) {
                    _snprintf(hotspotheader, sizeof(hotspotheader)-1, "\\%s(%s#%d)\0", processes, tmp, i);
                    PdhRemoveCounter_i(hc);
                    goto end;
                }
            }
            PdhRemoveCounter_i(hc);
        }
    }
 end:
    if (instances != NULL) {
        free(instances);
    }
    if (tmpQuery != NULL) {
        PdhCloseQuery_i(tmpQuery);
    }
    if (error) {
        return NULL;
    }
    return hotspotheader;
}
/**
 * Help function that initializes the PDH process header for the JRockit process.
 * (You should probably use getProcessPDHHeader() instead!)
 *
 * initPDH() must have been run prior to calling this function.
 *
 * @param ebuf the error buffer.
 * @param elen the length of the error buffer.
 *
 * @return the PDH instance description corresponding to the JVM process.
 */
Help function that initializes the PDH process header for the JRockit process.
(You should probably use getProcessPDHHeader() instead!)

initPDH() must have been run prior to calling this function.

@param ebuf the error buffer.
@param elen the length of the error buffer.

@return the PDH instance description corresponding to the JVM process.
Help function that initializes the PDH process header for the JRockit process . ( You should probably use getProcessPDHHeader () instead! ) initPDH () must have been run prior to calling this function . @param ebuf the error buffer . @param elen the length of the error buffer . @return the PDH instance description corresponding to the JVM process .
static char*
initProcessPDHHeader(void) {
    static char hotspotheader[2*COUNTER_BUF_SIZE];
    char           counter[2*COUNTER_BUF_SIZE];
    char           processes[COUNTER_BUF_SIZE];   
    char           pid[COUNTER_BUF_SIZE];           
    char           module_name[MAX_PATH];
    PDH_STATUS  pdhStat;
    DWORD       c_size = 0, i_size = 0;
    HQUERY      tmpQuery = NULL;
    int           i, myPid = _getpid();
    BOOL           error = false;
    char          *instances, *tmp, *instance_name, *dot_pos;
    tmpQuery = NULL;
    myPid    = _getpid();
    error    = false;
    if (find_name(PDH_PROCESS_IDX, processes, sizeof(processes) - 1) < 0) {
        return NULL;
    }
    if (find_name(PDH_ID_PROCESS_IDX, pid, sizeof(pid) - 1) < 0) {
        return NULL;
    }
    c_size = 0;
    i_size = 0;
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   
                    NULL,                   
                    processes,              
                    NULL,                   
                    &c_size,              
                    NULL,              
                    &i_size,              
                    PERF_DETAIL_WIZARD,     
                    0);
    if (pdh_fail(pdhStat)) {
        return NULL;
    }
    if ((instances = calloc(i_size, 1)) == NULL) {
        error = true;
        goto end;
    }
    c_size = 0;
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   
                    NULL,                   
                    processes,              
                    NULL,              
                    &c_size,              
                    instances,          
                    &i_size,              
                    PERF_DETAIL_WIZARD,     
                    0);
    if (pdh_fail(pdhStat)) {
        error = true;
        goto end;
    }
    if (PdhOpenQuery_i(NULL, 0, &tmpQuery) != ERROR_SUCCESS) {
        error = true;
        goto end;
    }
    if (GetModuleFileName(NULL, module_name, MAX_PATH) >= MAX_PATH-1) {
        error = true;
        goto end;
    }
    instance_name = strrchr(module_name, '\\'); 
    instance_name++;                            
    dot_pos = strchr(instance_name, '.');       
    dot_pos[0] = '\0';
    for (tmp = instances; *tmp != 0 && !error; tmp = &tmp[lstrlen(tmp)+1]) {
        HCOUNTER  hc = NULL;
        BOOL done = false;
        if (strcmp(tmp, instance_name) != 0) {
            continue;
        }
        for (i = 0; !done && !error; i++){
            PDH_STATUS res;
            _snprintf(counter, sizeof(counter)-1, "\\%s(%s#%d)\\%s", processes, tmp, i, pid);
            if (PdhAddCounter_i(tmpQuery, counter, 0, &hc) != ERROR_SUCCESS) {
                error = true;
                goto end;
            }
            res = PdhCollectQueryData_i(tmpQuery);
            if (res == PDH_INVALID_HANDLE) {
                res = -1;
                done = true;
            } else if (res == PDH_NO_DATA) {
                done = true;
            } else {
                PDH_FMT_COUNTERVALUE cv;
                PdhGetFormattedCounterValue_i(hc, PDH_FMT_LONG, NULL, &cv);
                if (cv.CStatus != PDH_CSTATUS_VALID_DATA) {
                    done = true;
                } else if (cv.longValue == myPid) {
                    _snprintf(hotspotheader, sizeof(hotspotheader)-1, "\\%s(%s#%d)\0", processes, tmp, i);
                    PdhRemoveCounter_i(hc);
                    goto end;
                }
            }
            PdhRemoveCounter_i(hc);
        }
    }
 end:
    if (instances != NULL) {
        free(instances);
    }
    if (tmpQuery != NULL) {
        PdhCloseQuery_i(tmpQuery);
    }
    if (error) {
        return NULL;
    }
    return hotspotheader;
}
static char * initProcessPDHHeader ( void ) { static char hotspotheader [ 2 * COUNTER_BUF_SIZE ] ; char counter [ 2 * COUNTER_BUF_SIZE ] ; char processes [ COUNTER_BUF_SIZE ] ; char pid [ COUNTER_BUF_SIZE ] ; char module_name [ MAX_PATH ] ; PDH_STATUS pdhStat ; DWORD c_size = 0 , i_size = 0 ; HQUERY tmpQuery = NULL ; int i , myPid = _getpid ( ) ; BOOL error = false ; char * instances , * tmp , * instance_name , * dot_pos ; tmpQuery = NULL ; myPid = _getpid ( ) ; error = false ; if ( find_name ( PDH_PROCESS_IDX , processes , sizeof ( processes ) - 1 ) < 0 ) { return NULL ; } if ( find_name ( PDH_ID_PROCESS_IDX , pid , sizeof ( pid ) - 1 ) < 0 ) { return NULL ; } c_size = 0 ; i_size = 0 ; pdhStat = PdhEnumObjectItems_i ( NULL , NULL , processes , NULL , & c_size , NULL , & i_size , PERF_DETAIL_WIZARD , 0 ) ; if ( pdh_fail ( pdhStat ) ) { return NULL ; } if ( ( instances = calloc ( i_size , 1 ) ) == NULL ) { error = true ; goto end ; } c_size = 0 ; pdhStat = PdhEnumObjectItems_i ( NULL , NULL , processes , NULL , & c_size , instances , & i_size , PERF_DETAIL_WIZARD , 0 ) ; if ( pdh_fail ( pdhStat ) ) { error = true ; goto end ; } if ( PdhOpenQuery_i ( NULL , 0 , & tmpQuery ) != ERROR_SUCCESS ) { error = true ; goto end ; } if ( GetModuleFileName ( NULL , module_name , MAX_PATH ) >= MAX_PATH - 1 ) { error = true ; goto end ; } instance_name = strrchr ( module_name , ' \\ ' ) ; instance_name ++ ; dot_pos = strchr ( instance_name , ' ' ) ; dot_pos [ 0 ] = ' \0 ' ; for ( tmp = instances ; * tmp != 0 && ! error ; tmp = & tmp [ lstrlen ( tmp ) + 1 ] ) { HCOUNTER hc = NULL ; BOOL done = false ; if ( strcmp ( tmp , instance_name ) != 0 ) { continue ; } for ( i = 0 ; ! done && ! error ; i ++ ) { PDH_STATUS res ; _snprintf ( counter , sizeof ( counter ) - 1 , " \\ \\ " , processes , tmp , i , pid ) ; if ( PdhAddCounter_i ( tmpQuery , counter , 0 , & hc ) != ERROR_SUCCESS ) { error = true ; goto end ; } res = PdhCollectQueryData_i ( tmpQuery ) ; if ( res == PDH_INVALID_HANDLE ) { res = -1 ; done = true ; } else if ( res == PDH_NO_DATA ) { done = true ; } else { PDH_FMT_COUNTERVALUE cv ; PdhGetFormattedCounterValue_i ( hc , PDH_FMT_LONG , NULL , & cv ) ; if ( cv . CStatus != PDH_CSTATUS_VALID_DATA ) { done = true ; } else if ( cv . longValue == myPid ) { _snprintf ( hotspotheader , sizeof ( hotspotheader ) - 1 , " \\ \0 " , processes , tmp , i ) ; PdhRemoveCounter_i ( hc ) ; goto end ; } } PdhRemoveCounter_i ( hc ) ; } } end : if ( instances != NULL ) { free ( instances ) ; } if ( tmpQuery != NULL ) { PdhCloseQuery_i ( tmpQuery ) ; } if ( error ) { return NULL ; } return hotspotheader ; }
Help function that initializes the PDH process header for the JRockit process.
Help function that initializes the PDH process header for the JRockit process .
//'Process' == #230 //'ID Process' == 784 //time is same. // reserved // local machine // object to enumerate // pass in NULL buffers // and 0 length to get // required size // of the buffers in chars // counter detail level //ok, now we have enough to enumerate all processes /* printf("Could not enumerate processes (1) error=%d", pdhStat); */ // use calloc because windows vista does not null terminate the instance names (allthough the docs says it will) /* printf("Could not allocate memory %d bytes", i_size); */ // reserved // local machine // object to enumerate // pass in NULL buffers // and 0 length to get // required size // of the buffers in chars // counter detail level // ok, now we have enough to enumerate all processes /* printf("Could not enumerate processes (2) error=%d", pdhStat); */ /* printf("Could not create temporary query"); */ // Find our module name and use it to extract the instance name used by PDH /* printf("Module name truncated"); */ //drop path //skip slash //drop .exe //now, fetch the counters. // Skip until we find our own process name // iterate over all instance indexes and try to find our own pid /* printf("Failed to create process id query"); */ /* printf("Failed to query process id"); */ /*
                 * This check seems to be needed for Win2k SMP boxes, since
                 * they for some reason don't return PDH_NO_DATA for non existing
                 * counters.
                 */