src/windows/native/sun/management/OperatingSystemImpl.c
static int
initProcessorCounters(void) {
    char          processor[COUNTER_BUF_SIZE]; //'Processor' == #238
    char          time[COUNTER_BUF_SIZE];      //'Time' == 6
    DWORD      c_size, i_size;
    HQUERY     tmpQuery;
    DWORD      i, p_count;
    BOOL          error;
    char         *instances, *tmp;
    PDH_STATUS pdhStat;

    c_size   = i_size = 0;
    tmpQuery = NULL;
    error    = false;

    // This __try / __except stuff is there since Windows 2000 beta (or so) sometimes triggered
    // an access violation when the user had insufficient privileges to use the performance
    // counters. This was previously guarded by a very ugly piece of code which disabled the
    // global trap handling in JRockit. Don't know if this really is needed anymore, but otoh,
    // if we keep it we don't crash on Win2k beta. /Ihse, 2005-05-30
    __try {
        if (find_name(PDH_PROCESSOR_IDX, processor, sizeof(processor)-1) < 0) {
            return -1;
        }
    } __except (EXCEPTION_EXECUTE_HANDLER) { // We'll catch all exceptions here.
        /* printf("User does not have sufficient privileges to use performance counters"); */
        return -1;
    }

    if (find_name(PDH_PROCESSOR_TIME_IDX, time, sizeof(time)-1) < 0) {
        return -1;
    }
    //ok, now we have enough to enumerate all processors.
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   // reserved
                    NULL,                   // local machine
                    processor,          // 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);
    if (pdh_fail(pdhStat)) {
        /* printf("could not enumerate processors (1) error=%d", pdhStat); */
        return -1;
    }

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

    c_size  = 0;
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   // reserved
                    NULL,                   // local machine
                    processor,              // 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);

    if (pdh_fail(pdhStat)) {
        /* printf("could not enumerate processors (2) error=%d", pdhStat); */
        error = true;
        goto end;
    }
    //count perf count instances.
    for (p_count = 0, tmp = instances; *tmp != 0; tmp = &tmp[lstrlen(tmp)+1], p_count++);

    //is this correct for HT?
    assert(p_count == num_cpus+1);

    //ok, have number of perf counters.
    multiCounterCPULoad.counters = calloc(p_count, sizeof(HCOUNTER));
    if (multiCounterCPULoad.counters == NULL) {
        /* printf("could not allocate memory (2) count=%d", p_count); */
        error = true;
        goto end;
    }

    multiCounterCPULoad.noOfCounters = p_count;

    if (PdhOpenQuery_i(NULL, 0, &multiCounterCPULoad.query.query) != ERROR_SUCCESS) {
        /* printf("could not create query"); */
        error = true;
        goto end;
    }
    //now, fetch the counters.
    for (i = 0, tmp = instances; *tmp != '\0'; tmp = &tmp[lstrlen(tmp)+1], i++) {
    char counter[2*COUNTER_BUF_SIZE];

    _snprintf(counter, sizeof(counter)-1, "\\%s(%s)\\%s", processor, tmp, time);

    if (PdhAddCounter_i(multiCounterCPULoad.query.query, counter, 0, &multiCounterCPULoad.counters[i]) != ERROR_SUCCESS) {
            /* printf("error adding processor counter %s", counter); */
            error = true;
            goto end;
        }
    }

    free(instances);
    instances = NULL;

    // Query once to initialize the counters needing at least two queries
    // (like the % CPU usage) to calculate correctly.
    if (PdhCollectQueryData_i(multiCounterCPULoad.query.query) != ERROR_SUCCESS)
        error = true;

 end:
    if (instances != NULL) {
        free(instances);
    }
    if (tmpQuery != NULL) {
        PdhCloseQuery_i(tmpQuery);
    }
    if (error) {
        int i;

        if (multiCounterCPULoad.counters != NULL) {
            for (i = 0; i < multiCounterCPULoad.noOfCounters; i++) {
                if (multiCounterCPULoad.counters[i] != NULL) {
                    PdhRemoveCounter_i(multiCounterCPULoad.counters[i]);
                }
            }
            free(multiCounterCPULoad.counters[i]);
        }
        if (multiCounterCPULoad.query.query != NULL) {
            PdhCloseQuery_i(multiCounterCPULoad.query.query);
        }
        memset(&multiCounterCPULoad, 0, sizeof(MultipleCounterQueryS));
        return -1;
    }
    return CONFIG_SUCCESSFUL;
}
/**
 * Sets up the supplied MultipleCounterQuery to check on the processors.
 * (Comment: Refactor and prettify as with the the SingleCounter queries
 * if more MultipleCounterQueries are discovered.)
 *
 * initPDH() must have been run prior to calling this function.
 *
 * @param multiQuery  a pointer to a MultipleCounterQueryS, will be filled in with
 *                    the necessary info to check the PDH processor counters.
 * @return            CONFIG_SUCCESSFUL if successful, negative on failure.
 */
Sets up the supplied MultipleCounterQuery to check on the processors.
(Comment: Refactor and prettify as with the the SingleCounter queries
if more MultipleCounterQueries are discovered.)

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

@param multiQuery  a pointer to a MultipleCounterQueryS, will be filled in with
the necessary info to check the PDH processor counters.
@return            CONFIG_SUCCESSFUL if successful, negative on failure.
Sets up the supplied MultipleCounterQuery to check on the processors . ( Comment : Refactor and prettify as with the the SingleCounter queries if more MultipleCounterQueries are discovered . ) initPDH () must have been run prior to calling this function . @param multiQuery a pointer to a MultipleCounterQueryS will be filled in with the necessary info to check the PDH processor counters . @return CONFIG_SUCCESSFUL if successful negative on failure .
static int
initProcessorCounters(void) {
    char          processor[COUNTER_BUF_SIZE]; 
    char          time[COUNTER_BUF_SIZE];      
    DWORD      c_size, i_size;
    HQUERY     tmpQuery;
    DWORD      i, p_count;
    BOOL          error;
    char         *instances, *tmp;
    PDH_STATUS pdhStat;
    c_size   = i_size = 0;
    tmpQuery = NULL;
    error    = false;
    __try {
        if (find_name(PDH_PROCESSOR_IDX, processor, sizeof(processor)-1) < 0) {
            return -1;
        }
    } __except (EXCEPTION_EXECUTE_HANDLER) { 
        return -1;
    }
    if (find_name(PDH_PROCESSOR_TIME_IDX, time, sizeof(time)-1) < 0) {
        return -1;
    }
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   
                    NULL,                   
                    processor,          
                    NULL,              
                    &c_size,              
                    NULL,              
                    &i_size,              
                    PERF_DETAIL_WIZARD,     
                    0);
    if (pdh_fail(pdhStat)) {
        return -1;
    }
    instances = calloc(i_size, 1);
    if (instances == NULL) {
        error = true;
        goto end;
    }
    c_size  = 0;
    pdhStat = PdhEnumObjectItems_i (
                    NULL,                   
                    NULL,                   
                    processor,              
                    NULL,              
                    &c_size,              
                    instances,          
                    &i_size,              
                    PERF_DETAIL_WIZARD,     
                    0);
    if (pdh_fail(pdhStat)) {
        error = true;
        goto end;
    }
    for (p_count = 0, tmp = instances; *tmp != 0; tmp = &tmp[lstrlen(tmp)+1], p_count++);
    assert(p_count == num_cpus+1);
    multiCounterCPULoad.counters = calloc(p_count, sizeof(HCOUNTER));
    if (multiCounterCPULoad.counters == NULL) {
        error = true;
        goto end;
    }
    multiCounterCPULoad.noOfCounters = p_count;
    if (PdhOpenQuery_i(NULL, 0, &multiCounterCPULoad.query.query) != ERROR_SUCCESS) {
        error = true;
        goto end;
    }
    for (i = 0, tmp = instances; *tmp != '\0'; tmp = &tmp[lstrlen(tmp)+1], i++) {
    char counter[2*COUNTER_BUF_SIZE];
    _snprintf(counter, sizeof(counter)-1, "\\%s(%s)\\%s", processor, tmp, time);
    if (PdhAddCounter_i(multiCounterCPULoad.query.query, counter, 0, &multiCounterCPULoad.counters[i]) != ERROR_SUCCESS) {
            error = true;
            goto end;
        }
    }
    free(instances);
    instances = NULL;
    if (PdhCollectQueryData_i(multiCounterCPULoad.query.query) != ERROR_SUCCESS)
        error = true;
 end:
    if (instances != NULL) {
        free(instances);
    }
    if (tmpQuery != NULL) {
        PdhCloseQuery_i(tmpQuery);
    }
    if (error) {
        int i;
        if (multiCounterCPULoad.counters != NULL) {
            for (i = 0; i < multiCounterCPULoad.noOfCounters; i++) {
                if (multiCounterCPULoad.counters[i] != NULL) {
                    PdhRemoveCounter_i(multiCounterCPULoad.counters[i]);
                }
            }
            free(multiCounterCPULoad.counters[i]);
        }
        if (multiCounterCPULoad.query.query != NULL) {
            PdhCloseQuery_i(multiCounterCPULoad.query.query);
        }
        memset(&multiCounterCPULoad, 0, sizeof(MultipleCounterQueryS));
        return -1;
    }
    return CONFIG_SUCCESSFUL;
}
static int initProcessorCounters ( void ) { char processor [ COUNTER_BUF_SIZE ] ; char time [ COUNTER_BUF_SIZE ] ; DWORD c_size , i_size ; HQUERY tmpQuery ; DWORD i , p_count ; BOOL error ; char * instances , * tmp ; PDH_STATUS pdhStat ; c_size = i_size = 0 ; tmpQuery = NULL ; error = false ; __try { if ( find_name ( PDH_PROCESSOR_IDX , processor , sizeof ( processor ) - 1 ) < 0 ) { return -1 ; } } __except ( EXCEPTION_EXECUTE_HANDLER ) { return -1 ; } if ( find_name ( PDH_PROCESSOR_TIME_IDX , time , sizeof ( time ) - 1 ) < 0 ) { return -1 ; } pdhStat = PdhEnumObjectItems_i ( NULL , NULL , processor , NULL , & c_size , NULL , & i_size , PERF_DETAIL_WIZARD , 0 ) ; if ( pdh_fail ( pdhStat ) ) { return -1 ; } instances = calloc ( i_size , 1 ) ; if ( instances == NULL ) { error = true ; goto end ; } c_size = 0 ; pdhStat = PdhEnumObjectItems_i ( NULL , NULL , processor , NULL , & c_size , instances , & i_size , PERF_DETAIL_WIZARD , 0 ) ; if ( pdh_fail ( pdhStat ) ) { error = true ; goto end ; } for ( p_count = 0 , tmp = instances ; * tmp != 0 ; tmp = & tmp [ lstrlen ( tmp ) + 1 ] , p_count ++ ) ; assert ( p_count == num_cpus + 1 ) ; multiCounterCPULoad . counters = calloc ( p_count , sizeof ( HCOUNTER ) ) ; if ( multiCounterCPULoad . counters == NULL ) { error = true ; goto end ; } multiCounterCPULoad . noOfCounters = p_count ; if ( PdhOpenQuery_i ( NULL , 0 , & multiCounterCPULoad . query . query ) != ERROR_SUCCESS ) { error = true ; goto end ; } for ( i = 0 , tmp = instances ; * tmp != ' \0 ' ; tmp = & tmp [ lstrlen ( tmp ) + 1 ] , i ++ ) { char counter [ 2 * COUNTER_BUF_SIZE ] ; _snprintf ( counter , sizeof ( counter ) - 1 , " \\ \\ " , processor , tmp , time ) ; if ( PdhAddCounter_i ( multiCounterCPULoad . query . query , counter , 0 , & multiCounterCPULoad . counters [ i ] ) != ERROR_SUCCESS ) { error = true ; goto end ; } } free ( instances ) ; instances = NULL ; if ( PdhCollectQueryData_i ( multiCounterCPULoad . query . query ) != ERROR_SUCCESS ) error = true ; end : if ( instances != NULL ) { free ( instances ) ; } if ( tmpQuery != NULL ) { PdhCloseQuery_i ( tmpQuery ) ; } if ( error ) { int i ; if ( multiCounterCPULoad . counters != NULL ) { for ( i = 0 ; i < multiCounterCPULoad . noOfCounters ; i ++ ) { if ( multiCounterCPULoad . counters [ i ] != NULL ) { PdhRemoveCounter_i ( multiCounterCPULoad . counters [ i ] ) ; } } free ( multiCounterCPULoad . counters [ i ] ) ; } if ( multiCounterCPULoad . query . query != NULL ) { PdhCloseQuery_i ( multiCounterCPULoad . query . query ) ; } memset ( & multiCounterCPULoad , 0 , sizeof ( MultipleCounterQueryS ) ) ; return -1 ; } return CONFIG_SUCCESSFUL ; }
Sets up the supplied MultipleCounterQuery to check on the processors.
Sets up the supplied MultipleCounterQuery to check on the processors .
//'Processor' == #238 //'Time' == 6 // This __try / __except stuff is there since Windows 2000 beta (or so) sometimes triggered // an access violation when the user had insufficient privileges to use the performance // counters. This was previously guarded by a very ugly piece of code which disabled the // global trap handling in JRockit. Don't know if this really is needed anymore, but otoh, // if we keep it we don't crash on Win2k beta. /Ihse, 2005-05-30 // We'll catch all exceptions here. /* printf("User does not have sufficient privileges to use performance counters"); */ //ok, now we have enough to enumerate all processors. // 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 /* printf("could not enumerate processors (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 (1) %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 /* printf("could not enumerate processors (2) error=%d", pdhStat); */ //count perf count instances. //is this correct for HT? //ok, have number of perf counters. /* printf("could not allocate memory (2) count=%d", p_count); */ /* printf("could not create query"); */ //now, fetch the counters. /* printf("error adding processor counter %s", counter); */ // Query once to initialize the counters needing at least two queries // (like the % CPU usage) to calculate correctly.