119 bool initialized;
120 } MultiCounterQuerySetS, *MultiCounterQuerySetP;
121
122 typedef struct {
123 MultiCounterQuerySetS set;
124 int process_index;
125 } ProcessQueryS, *ProcessQueryP;
126
127 static void pdh_cleanup(HQUERY* const query, HCOUNTER* const counter) {
128 if (counter != NULL && *counter != NULL) {
129 PdhDll::PdhRemoveCounter(*counter);
130 *counter = NULL;
131 }
132 if (query != NULL && *query != NULL) {
133 PdhDll::PdhCloseQuery(*query);
134 *query = NULL;
135 }
136 }
137
138 static CounterQueryP create_counter_query() {
139 CounterQueryP const query = NEW_C_HEAP_ARRAY(CounterQueryS, 1, mtInternal);
140 memset(query, 0, sizeof(CounterQueryS));
141 return query;
142 }
143
144 static void destroy_counter_query(CounterQueryP query) {
145 assert(query != NULL, "invariant");
146 pdh_cleanup(&query->query.query, &query->counter);
147 FREE_C_HEAP_ARRAY(CounterQueryS, query);
148 }
149
150 static MultiCounterQueryP create_multi_counter_query() {
151 MultiCounterQueryP const query = NEW_C_HEAP_ARRAY(MultiCounterQueryS, 1, mtInternal);
152 memset(query, 0, sizeof(MultiCounterQueryS));
153 return query;
154 }
155
156 static void destroy_counter_query(MultiCounterQueryP counter_query) {
157 if (counter_query != NULL) {
158 for (int i = 0; i < counter_query->noOfCounters; ++i) {
159 pdh_cleanup(NULL, &counter_query->counters[i]);
160 }
161 FREE_C_HEAP_ARRAY(char, counter_query->counters);
162 pdh_cleanup(&counter_query->query.query, NULL);
163 FREE_C_HEAP_ARRAY(MultiCounterQueryS, counter_query);
164 }
165 }
166
167 static void destroy_multi_counter_query(MultiCounterQuerySetP counter_query_set) {
168 for (int i = 0; i < counter_query_set->size; i++) {
169 for (int j = 0; j < counter_query_set->queries[i].noOfCounters; ++j) {
170 pdh_cleanup(NULL, &counter_query_set->queries[i].counters[j]);
171 }
182
183 static void destroy_counter_query(ProcessQueryP process_query) {
184 destroy_multi_counter_query(&process_query->set);
185 FREE_C_HEAP_ARRAY(ProcessQueryS, process_query);
186 }
187
188 static int open_query(HQUERY* query) {
189 return PdhDll::PdhOpenQuery(NULL, 0, query);
190 }
191
192 template <typename QueryP>
193 static int open_query(QueryP query) {
194 return open_query(&query->query);
195 }
196
197 static int allocate_counters(MultiCounterQueryP query, size_t nofCounters) {
198 assert(query != NULL, "invariant");
199 assert(!query->initialized, "invariant");
200 assert(0 == query->noOfCounters, "invariant");
201 assert(query->counters == NULL, "invariant");
202 query->counters = (HCOUNTER*)NEW_C_HEAP_ARRAY(char, nofCounters * sizeof(HCOUNTER), mtInternal);
203 if (query->counters == NULL) {
204 return OS_ERR;
205 }
206 memset(query->counters, 0, nofCounters * sizeof(HCOUNTER));
207 query->noOfCounters = (int)nofCounters;
208 return OS_OK;
209 }
210
211 static int allocate_counters(MultiCounterQuerySetP query_set, size_t nofCounters) {
212 assert(query_set != NULL, "invariant");
213 assert(!query_set->initialized, "invariant");
214 for (int i = 0; i < query_set->size; ++i) {
215 if (allocate_counters(&query_set->queries[i], nofCounters) != OS_OK) {
216 return OS_ERR;
217 }
218 }
219 return OS_OK;
220 }
221
222 static int allocate_counters(ProcessQueryP process_query, size_t nofCounters) {
371 return OS_ERR;
372 } else {
373 PDH_FMT_COUNTERVALUE counter_value;
374 formatted_counter_value(handle_counter, PDH_FMT_LONG, &counter_value);
375 pdh_cleanup(NULL, &handle_counter);
376 if ((LONG)os::current_process_id() == counter_value.longValue) {
377 pdh_cleanup(&tmpQuery, NULL);
378 return index;
379 }
380 }
381 }
382 pdh_cleanup(&tmpQuery, NULL);
383 return OS_ERR;
384 }
385
386 static ProcessQueryP create_process_query() {
387 const int current_process_idx = current_query_index_for_process();
388 if (OS_ERR == current_process_idx) {
389 return NULL;
390 }
391 ProcessQueryP const process_query = NEW_C_HEAP_ARRAY(ProcessQueryS, 1, mtInternal);
392 memset(process_query, 0, sizeof(ProcessQueryS));
393 process_query->set.queries = NEW_C_HEAP_ARRAY(MultiCounterQueryS, current_process_idx + 1, mtInternal);
394 memset(process_query->set.queries, 0, sizeof(MultiCounterQueryS) * (current_process_idx + 1));
395 process_query->process_index = current_process_idx;
396 process_query->set.size = current_process_idx + 1;
397 assert(process_query->set.size > process_query->process_index, "invariant");
398 return process_query;
399 }
400
401 static MultiCounterQueryP current_process_counter_query(ProcessQueryP process_query) {
402 assert(process_query != NULL, "invariant");
403 assert(process_query->process_index < process_query->set.size, "invariant");
404 return &process_query->set.queries[process_query->process_index];
405 }
406
407 static void clear_multi_counter(MultiCounterQueryP query) {
408 for (int i = 0; i < query->noOfCounters; ++i) {
409 pdh_cleanup(NULL, &query->counters[i]);
410 }
411 pdh_cleanup(&query->query.query, NULL);
|
119 bool initialized;
120 } MultiCounterQuerySetS, *MultiCounterQuerySetP;
121
122 typedef struct {
123 MultiCounterQuerySetS set;
124 int process_index;
125 } ProcessQueryS, *ProcessQueryP;
126
127 static void pdh_cleanup(HQUERY* const query, HCOUNTER* const counter) {
128 if (counter != NULL && *counter != NULL) {
129 PdhDll::PdhRemoveCounter(*counter);
130 *counter = NULL;
131 }
132 if (query != NULL && *query != NULL) {
133 PdhDll::PdhCloseQuery(*query);
134 *query = NULL;
135 }
136 }
137
138 static CounterQueryP create_counter_query() {
139 CounterQueryP const query = NEW_C_HEAP_OBJ(CounterQueryS, mtInternal);
140 memset(query, 0, sizeof(CounterQueryS));
141 return query;
142 }
143
144 static void destroy_counter_query(CounterQueryP query) {
145 assert(query != NULL, "invariant");
146 pdh_cleanup(&query->query.query, &query->counter);
147 FREE_C_HEAP_ARRAY(CounterQueryS, query);
148 }
149
150 static MultiCounterQueryP create_multi_counter_query() {
151 MultiCounterQueryP const query = NEW_C_HEAP_OBJ(MultiCounterQueryS, mtInternal);
152 memset(query, 0, sizeof(MultiCounterQueryS));
153 return query;
154 }
155
156 static void destroy_counter_query(MultiCounterQueryP counter_query) {
157 if (counter_query != NULL) {
158 for (int i = 0; i < counter_query->noOfCounters; ++i) {
159 pdh_cleanup(NULL, &counter_query->counters[i]);
160 }
161 FREE_C_HEAP_ARRAY(char, counter_query->counters);
162 pdh_cleanup(&counter_query->query.query, NULL);
163 FREE_C_HEAP_ARRAY(MultiCounterQueryS, counter_query);
164 }
165 }
166
167 static void destroy_multi_counter_query(MultiCounterQuerySetP counter_query_set) {
168 for (int i = 0; i < counter_query_set->size; i++) {
169 for (int j = 0; j < counter_query_set->queries[i].noOfCounters; ++j) {
170 pdh_cleanup(NULL, &counter_query_set->queries[i].counters[j]);
171 }
182
183 static void destroy_counter_query(ProcessQueryP process_query) {
184 destroy_multi_counter_query(&process_query->set);
185 FREE_C_HEAP_ARRAY(ProcessQueryS, process_query);
186 }
187
188 static int open_query(HQUERY* query) {
189 return PdhDll::PdhOpenQuery(NULL, 0, query);
190 }
191
192 template <typename QueryP>
193 static int open_query(QueryP query) {
194 return open_query(&query->query);
195 }
196
197 static int allocate_counters(MultiCounterQueryP query, size_t nofCounters) {
198 assert(query != NULL, "invariant");
199 assert(!query->initialized, "invariant");
200 assert(0 == query->noOfCounters, "invariant");
201 assert(query->counters == NULL, "invariant");
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));
207 query->noOfCounters = (int)nofCounters;
208 return OS_OK;
209 }
210
211 static int allocate_counters(MultiCounterQuerySetP query_set, size_t nofCounters) {
212 assert(query_set != NULL, "invariant");
213 assert(!query_set->initialized, "invariant");
214 for (int i = 0; i < query_set->size; ++i) {
215 if (allocate_counters(&query_set->queries[i], nofCounters) != OS_OK) {
216 return OS_ERR;
217 }
218 }
219 return OS_OK;
220 }
221
222 static int allocate_counters(ProcessQueryP process_query, size_t nofCounters) {
371 return OS_ERR;
372 } else {
373 PDH_FMT_COUNTERVALUE counter_value;
374 formatted_counter_value(handle_counter, PDH_FMT_LONG, &counter_value);
375 pdh_cleanup(NULL, &handle_counter);
376 if ((LONG)os::current_process_id() == counter_value.longValue) {
377 pdh_cleanup(&tmpQuery, NULL);
378 return index;
379 }
380 }
381 }
382 pdh_cleanup(&tmpQuery, NULL);
383 return OS_ERR;
384 }
385
386 static ProcessQueryP create_process_query() {
387 const int current_process_idx = current_query_index_for_process();
388 if (OS_ERR == current_process_idx) {
389 return NULL;
390 }
391 ProcessQueryP const process_query = NEW_C_HEAP_OBJ(ProcessQueryS, mtInternal);
392 memset(process_query, 0, sizeof(ProcessQueryS));
393 process_query->set.queries = NEW_C_HEAP_ARRAY(MultiCounterQueryS, current_process_idx + 1, mtInternal);
394 memset(process_query->set.queries, 0, sizeof(MultiCounterQueryS) * (current_process_idx + 1));
395 process_query->process_index = current_process_idx;
396 process_query->set.size = current_process_idx + 1;
397 assert(process_query->set.size > process_query->process_index, "invariant");
398 return process_query;
399 }
400
401 static MultiCounterQueryP current_process_counter_query(ProcessQueryP process_query) {
402 assert(process_query != NULL, "invariant");
403 assert(process_query->process_index < process_query->set.size, "invariant");
404 return &process_query->set.queries[process_query->process_index];
405 }
406
407 static void clear_multi_counter(MultiCounterQueryP query) {
408 for (int i = 0; i < query->noOfCounters; ++i) {
409 pdh_cleanup(NULL, &query->counters[i]);
410 }
411 pdh_cleanup(&query->query.query, NULL);
|