192 template <typename QueryP> |
192 template <typename QueryP> |
193 static int open_query(QueryP query) { |
193 static int open_query(QueryP query) { |
194 return open_query(&query->query); |
194 return open_query(&query->query); |
195 } |
195 } |
196 |
196 |
197 static int allocate_counters(MultiCounterQueryP query, size_t nofCounters) { |
197 static void allocate_counters(MultiCounterQueryP query, size_t nofCounters) { |
198 assert(query != NULL, "invariant"); |
198 assert(query != NULL, "invariant"); |
199 assert(!query->initialized, "invariant"); |
199 assert(!query->initialized, "invariant"); |
200 assert(0 == query->noOfCounters, "invariant"); |
200 assert(0 == query->noOfCounters, "invariant"); |
201 assert(query->counters == NULL, "invariant"); |
201 assert(query->counters == NULL, "invariant"); |
202 query->counters = NEW_C_HEAP_ARRAY(HCOUNTER, nofCounters, mtInternal); |
202 query->counters = NEW_C_HEAP_ARRAY(HCOUNTER, nofCounters, mtInternal); |
203 if (query->counters == NULL) { |
|
204 return OS_ERR; |
|
205 } |
|
206 memset(query->counters, 0, nofCounters * sizeof(HCOUNTER)); |
203 memset(query->counters, 0, nofCounters * sizeof(HCOUNTER)); |
207 query->noOfCounters = (int)nofCounters; |
204 query->noOfCounters = (int)nofCounters; |
208 return OS_OK; |
205 } |
209 } |
206 |
210 |
207 static void allocate_counters(MultiCounterQuerySetP query_set, size_t nofCounters) { |
211 static int allocate_counters(MultiCounterQuerySetP query_set, size_t nofCounters) { |
|
212 assert(query_set != NULL, "invariant"); |
208 assert(query_set != NULL, "invariant"); |
213 assert(!query_set->initialized, "invariant"); |
209 assert(!query_set->initialized, "invariant"); |
214 for (int i = 0; i < query_set->size; ++i) { |
210 for (int i = 0; i < query_set->size; ++i) { |
215 if (allocate_counters(&query_set->queries[i], nofCounters) != OS_OK) { |
211 allocate_counters(&query_set->queries[i], nofCounters); |
216 return OS_ERR; |
212 } |
217 } |
213 } |
218 } |
214 |
219 return OS_OK; |
215 static void allocate_counters(ProcessQueryP process_query, size_t nofCounters) { |
220 } |
|
221 |
|
222 static int allocate_counters(ProcessQueryP process_query, size_t nofCounters) { |
|
223 assert(process_query != NULL, "invariant"); |
216 assert(process_query != NULL, "invariant"); |
224 return allocate_counters(&process_query->set, nofCounters); |
217 allocate_counters(&process_query->set, nofCounters); |
225 } |
218 } |
226 |
219 |
227 static void deallocate_counters(MultiCounterQueryP query) { |
220 static void deallocate_counters(MultiCounterQueryP query) { |
228 FREE_C_HEAP_ARRAY(char, query->counters); |
221 FREE_C_HEAP_ARRAY(char, query->counters); |
229 query->counters = NULL; |
222 query->counters = NULL; |
598 } |
591 } |
599 |
592 |
600 static const char* copy_string_to_c_heap(const char* string) { |
593 static const char* copy_string_to_c_heap(const char* string) { |
601 assert(string != NULL, "invariant"); |
594 assert(string != NULL, "invariant"); |
602 const size_t len = strlen(string); |
595 const size_t len = strlen(string); |
603 char* const cheap_allocated_string = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); |
596 char* const cheap_allocated_string = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len + 1, mtInternal); |
604 if (NULL == cheap_allocated_string) { |
597 if (NULL == cheap_allocated_string) { |
605 return NULL; |
598 return NULL; |
606 } |
599 } |
607 strncpy(cheap_allocated_string, string, len + 1); |
600 strncpy(cheap_allocated_string, string, len + 1); |
608 return cheap_allocated_string; |
601 return cheap_allocated_string; |
847 assert(cpu_query != NULL, "invariant"); |
840 assert(cpu_query != NULL, "invariant"); |
848 assert(!cpu_query->initialized, "invariant"); |
841 assert(!cpu_query->initialized, "invariant"); |
849 const int logical_cpu_count = number_of_logical_cpus(); |
842 const int logical_cpu_count = number_of_logical_cpus(); |
850 assert(logical_cpu_count >= os::processor_count(), "invariant"); |
843 assert(logical_cpu_count >= os::processor_count(), "invariant"); |
851 // we also add another counter for instance "_Total" |
844 // we also add another counter for instance "_Total" |
852 if (allocate_counters(cpu_query, logical_cpu_count + 1) != OS_OK) { |
845 allocate_counters(cpu_query, logical_cpu_count + 1); |
853 return OS_ERR; |
|
854 } |
|
855 assert(cpu_query->noOfCounters == logical_cpu_count + 1, "invariant"); |
846 assert(cpu_query->noOfCounters == logical_cpu_count + 1, "invariant"); |
856 return initialize_cpu_query_counters(cpu_query, pdh_counter_idx); |
847 return initialize_cpu_query_counters(cpu_query, pdh_counter_idx); |
857 } |
848 } |
858 |
849 |
859 static int initialize_process_counter(ProcessQueryP process_query, int slot_index, DWORD pdh_counter_index) { |
850 static int initialize_process_counter(ProcessQueryP process_query, int slot_index, DWORD pdh_counter_index) { |
1015 _context_switches = create_counter_query(PDH_SYSTEM_IDX, PDH_CONTEXT_SWITCH_RATE_IDX); |
1006 _context_switches = create_counter_query(PDH_SYSTEM_IDX, PDH_CONTEXT_SWITCH_RATE_IDX); |
1016 _process_cpu_load = create_process_query(); |
1007 _process_cpu_load = create_process_query(); |
1017 if (_process_cpu_load == NULL) { |
1008 if (_process_cpu_load == NULL) { |
1018 return true; |
1009 return true; |
1019 } |
1010 } |
1020 if (allocate_counters(_process_cpu_load, 2) != OS_OK) { |
1011 allocate_counters(_process_cpu_load, 2); |
1021 return true; |
|
1022 } |
|
1023 if (initialize_process_counter(_process_cpu_load, 0, PDH_PROCESSOR_TIME_IDX) != OS_OK) { |
1012 if (initialize_process_counter(_process_cpu_load, 0, PDH_PROCESSOR_TIME_IDX) != OS_OK) { |
1024 return true; |
1013 return true; |
1025 } |
1014 } |
1026 if (initialize_process_counter(_process_cpu_load, 1, PDH_PRIV_PROCESSOR_TIME_IDX) != OS_OK) { |
1015 if (initialize_process_counter(_process_cpu_load, 1, PDH_PRIV_PROCESSOR_TIME_IDX) != OS_OK) { |
1027 return true; |
1016 return true; |
1331 _cpu_info = NULL; |
1320 _cpu_info = NULL; |
1332 } |
1321 } |
1333 |
1322 |
1334 bool CPUInformationInterface::initialize() { |
1323 bool CPUInformationInterface::initialize() { |
1335 _cpu_info = new CPUInformation(); |
1324 _cpu_info = new CPUInformation(); |
1336 if (NULL == _cpu_info) { |
|
1337 return false; |
|
1338 } |
|
1339 _cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads()); |
1325 _cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads()); |
1340 _cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores()); |
1326 _cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores()); |
1341 _cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets()); |
1327 _cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets()); |
1342 _cpu_info->set_cpu_name(VM_Version_Ext::cpu_name()); |
1328 _cpu_info->set_cpu_name(VM_Version_Ext::cpu_name()); |
1343 _cpu_info->set_cpu_description(VM_Version_Ext::cpu_description()); |
1329 _cpu_info->set_cpu_description(VM_Version_Ext::cpu_description()); |