File: pdhquery.c

package info (click to toggle)
gridengine 6.2u5-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 57,132 kB
  • ctags: 56,142
  • sloc: ansic: 438,030; java: 66,252; sh: 36,376; jsp: 7,757; xml: 5,850; makefile: 5,514; csh: 4,571; cpp: 2,848; perl: 2,401; tcl: 692; lisp: 669; yacc: 668; ruby: 642; lex: 344
file content (269 lines) | stat: -rw-r--r-- 7,568 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>

#include <stdio.h>
#include <math.h>

#include "pdhservice.h"
#include "pdhquery.h"
#include "simplelog.h"

DWORD
pdhquery_initialize(t_pdhquery *query)
{
   int ret = 0;
   LPCTSTR data_source = NULL;
   DWORD user_data = 1;
   PDH_STATUS pdh_ret = 0;

   DENTER("pdhquery_initialize");
   pdh_ret = PdhOpenQuery(data_source, user_data, &(query->query));
   if (pdh_ret != ERROR_SUCCESS) {
      DPRINTF(("PdhOpenQuery failed with %x\n", pdh_ret));
      ret = 1;
   }
   DEXIT;
   return ret;
}

DWORD 
pdhquery_free(t_pdhquery *query)
{
   DWORD ret = 0;

   DENTER("pdhquery_free");
   PdhCloseQuery(query->query);
   DEXIT;
   return ret;
}

DWORD 
pdhquery_add_counterset(t_pdhquery *query, t_pdhcounterset *counterset) 
{
   DWORD ret = 0;
   LPSTR exp_list = NULL;
   PDH_STATUS pdh_ret;

   DENTER("pdhquery_add_counterset");
   /* 
    * Problem: PdhExpandCounterPath() sometimes fills more then 'length' Bytes,
    * so it writes to unallocated memory. It seems that the functions expands
    * the last path even if there is not enough space left to expand this path.
    * A possible solution (I'm not quite sure) could be:
    * Make buffer PDH_MAX_COUNTER_PATH Bytes larger than 'length", so the 
    * function does not write to unallocated memory.
    */
   /*
    * Expand all wildcard characters
    */
   {
      DWORD length = 0;

      do {
         if (length == 0) {
            /* Allocate buffer for min. 3 strings */
            length = PDH_MAX_COUNTER_PATH*3;
            exp_list = (LPSTR) malloc(length);
         } else {
            length *= 2;
            exp_list = (LPSTR) realloc(exp_list, length);
         }
         /* Pretend buffer could hold only 2 strings to work around 
          * bug in PdhExpandCounterPath()
          */
         length -= PDH_MAX_COUNTER_PATH;
         pdh_ret = PdhExpandCounterPath(counterset->wild_counter_path, 
                                        exp_list, &length);
      } while(pdh_ret == PDH_MORE_DATA);
   }

   /*
    * Add the expanded counter names to the query
    */
   if (pdh_ret == PDH_CSTATUS_VALID_DATA) {
      LPSTR name = NULL;
      DWORD j;

      /* how many elements are in the expanded list */
      for (name = exp_list, j = 0; 
           *name != '\0'; 
           name += lstrlen(name) + 1, j++) {
         ;
      }
      counterset->number_of_counters = j;

      counterset->counter_handles = (HCOUNTER*) malloc(sizeof(HCOUNTER) * j);
      counterset->pdh_name = (TXCHAR*) malloc(sizeof(TXCHAR) * j);
      if (counterset->counter_handles != NULL) {
         for (name = exp_list, j = 0; 
              *name != '\0'; 
              name += lstrlen(name) + 1, j++) {
            lstrcpy((LPSTR)counterset->pdh_name[j], name);
            pdh_ret = PdhAddCounter(query->query, name, j, 
                                    &(counterset->counter_handles[j]));
            if (pdh_ret != ERROR_SUCCESS) {
               DPRINTF(("PdhAddCounter failed with %x\n", pdh_ret));
               ret = 3;
               break;
            }
         }            
      } else {
         DPRINTF(("malloc failed\n"));
         ret = 2;
      }
   } else {
      DPRINTF(("PdhExpandCounterPath failed with %x\n", pdh_ret));
      ret = 1;
   }
   free(exp_list);
   DEXIT;
   return ret;
}

DWORD 
pdhquery_remove_counterset(t_pdhquery *query, t_pdhcounterset *counterset) 
{
   DWORD ret = 0;
   DWORD j;

   DENTER("pdhquery_remove_counterset");
   for (j = 0; j < counterset->number_of_counters; j++) {
      PdhRemoveCounter(counterset->counter_handles[j]);
   }
   free(counterset->counter_handles);
   counterset->counter_handles = NULL;
   free(counterset->pdh_name);
   counterset->pdh_name = NULL;
   DEXIT;
   return ret;
}

DWORD
pdhquery_update(t_pdhquery *query) 
{
   DWORD ret = 0;
   int pdh_ret;

   DENTER("pdhquery_update");
   /*
    * "Prime" counters that need two values to display a formatted value.
    */
   pdh_ret = PdhCollectQueryData(query->query);
   if (pdh_ret != ERROR_SUCCESS) {
      DPRINTF(("PdhCollectQueryData failed with %x\n", pdh_ret));
      ret = 1;
   }

   /*
    * Wait some time. Code which was executed bofore this line might
    * influence certain load values (e.g. cpu usage) therefore we have
    * two wait some time before we collect the data.
    */
   Sleep(100);

   /*
    * Collect current data
    */
   pdh_ret = PdhCollectQueryData(query->query);
   if (pdh_ret != ERROR_SUCCESS) {
      DPRINTF(("PdhCollectQueryData failed with %x\n", pdh_ret));
      ret = 1;
   }
   DEXIT;
   return ret;
} 

/*
 * Translate the object counter and instance names into the language
 * of the installed OS. Store the result in an element of values[] 
 * because we need it later on.
 */
DWORD 
pdhcounterset_initialize(t_pdhcounterset *counterset, 
                         LPSTR object, LPSTR instance, LPSTR counter)
{
   DWORD ret = 0;
   LPSTR trans_object   = NULL;
   LPSTR trans_counter  = NULL;
   LPSTR trans_instance = NULL;

   DENTER("pdhcounterset_initialize");

   /* store untranslated values */
   strcpy(counterset->object, object);
   strcpy(counterset->instance, instance);
   strcpy(counterset->counter, counter);

   /* translate them */
   if(pdhservice_translate(object, counter, instance, TRUE,
      &trans_object, &trans_counter, &trans_instance)==FALSE) {
      ret = 1;
      DEXIT;
      return ret;
   }

   /* store translation */
   strcpy(counterset->trans_object, trans_object);
   strcpy(counterset->trans_counter, trans_counter);
   if(trans_instance != NULL) {   
      strcpy(counterset->trans_instance, trans_instance);
   } else {
      strcpy(counterset->trans_instance, "");
   }

   /* create full wildcard counter path */
   if (trans_instance != NULL) {
      sprintf(counterset->wild_counter_path, "\\%s(%s)\\%s", 
              trans_object, trans_instance, trans_counter);
   } else {
      sprintf(counterset->wild_counter_path, "\\%s\\%s", 
              trans_object, trans_counter);
   }
   DEXIT;
   return ret;
}
   
DWORD 
pdhcounterset_free(t_pdhcounterset *counterset)
{
   DWORD ret = 0;

   DENTER("pdhcounterset_free");
   DEXIT;
   return ret;
}