File: pdhservice.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (409 lines) | stat: -rw-r--r-- 14,476 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*___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 <stdio.h>

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

#define TOTALBYTES    1024 * 1000
#define BYTEINCREMENT 1024

struct _pdhservice_name {
   LPSTR name_strings;
   LPSTR *names_array;
};

typedef struct _pdhservice_name pdhservice_container;

static pdhservice_container container_system_lang;
static pdhservice_container container_english;

static PPERF_OBJECT_TYPE 
get_first_object(PPERF_DATA_BLOCK data)
{
    return((PPERF_OBJECT_TYPE)((PBYTE)data + data->HeaderLength));
}

static PPERF_OBJECT_TYPE 
get_next_object(PPERF_OBJECT_TYPE object)
{
    return((PPERF_OBJECT_TYPE)((PBYTE)object + object->TotalByteLength));
}

static PPERF_INSTANCE_DEFINITION 
get_first_instance( PPERF_OBJECT_TYPE object )
{
    return((PPERF_INSTANCE_DEFINITION)((PBYTE)object + object->DefinitionLength));
}

static PPERF_INSTANCE_DEFINITION 
get_next_instance(PPERF_INSTANCE_DEFINITION instance)
{
    PPERF_COUNTER_BLOCK counter_block;

    counter_block = (PPERF_COUNTER_BLOCK)((PBYTE)instance + instance->ByteLength);
    return( (PPERF_INSTANCE_DEFINITION)((PBYTE)counter_block + counter_block->ByteLength));
}

static PPERF_COUNTER_DEFINITION 
get_first_counter( PPERF_OBJECT_TYPE object )
{
    return((PPERF_COUNTER_DEFINITION) ((PBYTE)object + object->HeaderLength));
}

static PPERF_COUNTER_DEFINITION 
get_next_counter(PPERF_COUNTER_DEFINITION counter)
{
    return((PPERF_COUNTER_DEFINITION)((PBYTE)counter + counter->ByteLength));
}

static WORD 
pdhservice_get_system_primary_lang_id(void)
{
   LANGID lang_id = GetSystemDefaultUILanguage();
   WORD prim_lang_id = PRIMARYLANGID(lang_id);

   return prim_lang_id;
}

static BOOL
pdhservice_get_names_from_registry(pdhservice_container *container, 
                                   WORD primary_lang_id)
{
   BOOL ret = FALSE;
   LONG local_ret;
   HKEY perflib_key;      // handle to registry key
   HKEY perflib_key_lang; // handle to registry key
   DWORD buffer;          // bytes to allocate for buffers
   DWORD buffer_size;     // size of dwBuffer

   // Get the number of Counter items.
   local_ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
                    0, KEY_READ, &perflib_key);
   if (local_ret == ERROR_SUCCESS) {
      buffer_size = sizeof(buffer);
      local_ret = RegQueryValueEx(perflib_key, "Last Counter", NULL, NULL,
                                  (LPBYTE) &buffer, &buffer_size);
      if (local_ret == ERROR_SUCCESS) {
         RegCloseKey(perflib_key);

         /*
          * get memory for the array of names
          *
          * NOTE: correct size would be "(buffer+1) * sizeof(LPSTR)" but ....
          * Due to a bug the "Last Counter" registry entry is some elements to
          * small. I make it twice as big to be sure.
          */
         container->names_array = (LPSTR*)malloc(2*(buffer+1) * sizeof(LPSTR));
         memset(container->names_array, 0, 2*(buffer+1) * sizeof(LPSTR));
         if (container->names_array != NULL) {
            char key[256];

            /*
             * open key containing conter and object names
             */
            sprintf(key, "SOFTWARE\\Microsoft\\Windows NT\\"
                    "CurrentVersion\\Perflib\\%03X", primary_lang_id);

            local_ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0,
                                     KEY_READ, &perflib_key_lang);
            if (local_ret != ERROR_SUCCESS) {
               /*
                * Try English values if primary language values are not available.
                */
               primary_lang_id = 9;
               sprintf(key, "SOFTWARE\\Microsoft\\Windows NT\\"
                     "CurrentVersion\\Perflib\\%03X", primary_lang_id);
               local_ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0,
                                        KEY_READ, &perflib_key_lang);
            }
            if (local_ret == ERROR_SUCCESS) {
               /*
                * get the size of the largest key
                */
               local_ret = RegQueryInfoKey(perflib_key_lang, NULL, NULL, NULL,
                                           NULL, NULL, NULL, NULL, NULL,
                                           &buffer, NULL, NULL);
               if (local_ret == ERROR_SUCCESS) {
                  /*
                   * malloc memory for counters and objects
                   */
                  buffer++;
                  container->name_strings = 
                                         (LPSTR) malloc(buffer * sizeof(CHAR));
                  if (container->name_strings != NULL) {
                     /*
                      * Read the name strings from registry
                      */
                     local_ret = RegQueryValueEx(perflib_key_lang, "Counter", 
                                               NULL, NULL,
                                               (LPBYTE)container->name_strings, 
                                               &buffer);
                     if (local_ret == ERROR_SUCCESS) {
                        LPSTR next_string;

                        /*
                         * Now feed all strings into the array
                         */
                        for(next_string = container->name_strings; 
                            *next_string != '\0';
                            next_string += lstrlen(next_string) + 1) {
                           DWORD value = atol(next_string);

                           next_string += lstrlen(next_string) + 1;
                           container->names_array[value] = (LPSTR)next_string;
                        }                              
                        ret = TRUE;
                     } else {
                        // error handling
                        fprintf(stderr, "RegQueryValueEx() failed\n");
                        fflush(stderr);
                     }
                  } else {
                     free(container->names_array);
                     container->names_array = NULL;

                     // error handling
                     fprintf(stderr, "malloc() failed\n");
                     fflush(stderr);
                  }
                  RegCloseKey(perflib_key_lang);
               } else {
                  // error handling
                  fprintf(stderr, "RegQueryInfoKey() failed\n");
                  fflush(stderr);
               }
            } else {
               // error handling
               fprintf(stderr, "RegOpenKeyEx() failed (error=%ld)\n", local_ret);
               fflush(stderr);
            }
         } else {
            // error handling
            fprintf(stderr, "malloc() failed\n");
            fflush(stderr);
         }
      } else {
         // error handling
         fprintf(stderr, "RegQueryValueEx() failed\n");
         fflush(stderr);
      }
   } else { 
      // error handling
      fprintf(stderr, "RegOpenKeyEx() failed (error=%ld)\n", local_ret);
      fflush(stderr);
   }
   return ret;
}

static BOOL
pdhservice_free_container(pdhservice_container *container)
{
   BOOL ret = TRUE;
 
   free(container->name_strings);
   free(container->names_array);
   return ret;
}

BOOL
pdhservice_translate(LPSTR object, LPSTR counter, LPSTR instance, 
                     BOOL is_english, LPSTR *new_object, LPSTR *new_counter, 
                     LPSTR *new_instance)
{
#if 0
#define DEBUG_SHOW_NAME
#endif
   BOOL instance_found = FALSE;
   BOOL counter_found = FALSE;
   BOOL object_found = FALSE;
   PPERF_DATA_BLOCK perf_data = NULL;
   DWORD buffer_size = TOTALBYTES;
   pdhservice_container *container1;
   pdhservice_container *container2;

   DENTER("pdhservice_translate");
   if (is_english) {
      container1 = &container_english;
      container2 = &container_system_lang;
   } else {
      container1 = &container_system_lang;
      container2 = &container_english;
   }

   // Allocate the buffer for the performance data.
   perf_data = (PPERF_DATA_BLOCK) malloc(buffer_size);
   if (perf_data != NULL) {
      PPERF_OBJECT_TYPE perf_object;
      DWORD i;

      while (RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Global",
                             NULL, NULL, (LPBYTE) perf_data, 
                             &buffer_size) == ERROR_MORE_DATA) {
         /*
          * Increase buffer until it is big enough
          */
         buffer_size += BYTEINCREMENT;
         perf_data = (PPERF_DATA_BLOCK) realloc(perf_data, buffer_size);
      }

      /*
       * Process all objects
       */
      perf_object = get_first_object(perf_data);
      for(i = 0; !object_found && i < perf_data->NumObjectTypes; i++) {
         PPERF_COUNTER_DEFINITION perf_counter;

#ifdef DEBUG_SHOW_NAME
         printf("\nO %ld: %s\n", perf_object->ObjectNameTitleIndex,
                container1->names_array[perf_object->ObjectNameTitleIndex]);
         fflush(stdout);
#endif
         if (lstrcmp(container1->names_array[perf_object->ObjectNameTitleIndex], object) == 0) {
            *new_object = container2->names_array[perf_object->ObjectNameTitleIndex];
            object_found = TRUE;
         }

         perf_counter = get_first_counter(perf_object);
         if (perf_object->NumInstances > 0) {
            int k;

            /*
             * Process all instances
             */
            PPERF_INSTANCE_DEFINITION perf_instance = get_first_instance(perf_object);
            for (k = 0; !instance_found && k < perf_object->NumInstances; k++) {
               PPERF_COUNTER_DEFINITION perf_curinst;
               DWORD j;
               WCHAR wide_string[1024];


#ifdef DEBUG_SHOW_NAME
               printf( "\n\tI %S: \n", 
                      (char *)((PBYTE)perf_instance + perf_instance->NameOffset));
               fflush(stdout);
#endif
               /* The instance seems to be stored as multibyte string !? */
               mbstowcs(wide_string, instance, sizeof(wide_string)/sizeof(WCHAR));
               if (object_found && wcscmp((wchar_t *)((PBYTE)perf_instance + perf_instance->NameOffset), wide_string) == 0) {
                  *new_instance = instance;
                  instance_found = TRUE;
               } else if (object_found && lstrcmp("*", instance) == 0) {
                  *new_instance = "*";
                  instance_found = TRUE;
               }
               perf_curinst = perf_counter;
               // Retrieve all counters.

               for (j = 0; !counter_found && j < perf_object->NumCounters; j++) {
#ifdef DEBUG_SHOW_NAME
                    printf("\t\tC %ld: %s\n", 
                        perf_curinst->CounterNameTitleIndex,
                        container1->names_array[perf_curinst->CounterNameTitleIndex]);
                    fflush(stdout);
#endif

                  if (instance_found && lstrcmp(container1->names_array[perf_curinst->CounterNameTitleIndex], counter) == 0) {
                     *new_counter = container2->names_array[perf_curinst->CounterNameTitleIndex];
                     counter_found = TRUE;
                  }

                  perf_curinst = get_next_counter(perf_curinst);             
               }

               perf_instance = get_next_instance(perf_instance);
            }
         } else {
            WORD j;
            PPERF_COUNTER_BLOCK counter_block = (PPERF_COUNTER_BLOCK) ((PBYTE)perf_object +
                                                perf_object->DefinitionLength );

            for (j = 0; !counter_found && j < perf_object->NumCounters; j++) {
               // Display the counter by index and name.

#ifdef DEBUG_SHOW_NAME
               printf("\tC %ld: %s\n", perf_counter->CounterNameTitleIndex,
                      container1->names_array[perf_counter->CounterNameTitleIndex] != NULL ? 
                      container1->names_array[perf_counter->CounterNameTitleIndex] : "NULL");
               fflush(stdout);
#endif

               if (object_found && lstrcmp(container1->names_array[perf_counter->CounterNameTitleIndex], counter) == 0) {
                  *new_counter = container2->names_array[perf_counter->CounterNameTitleIndex];
                  counter_found = TRUE;
               }
                
               // Get the next counter.
               perf_counter = get_next_counter(perf_counter);
            }
         }
         
         // Get the next object type.

         perf_object = get_next_object(perf_object);
      }
      free(perf_data);
      perf_data = NULL;
   }
   DEXIT;
   return counter_found && object_found;
}

BOOL
pdhservice_initialize(void)
{
   BOOL ret = TRUE;
   WORD prim_lang_id = pdhservice_get_system_primary_lang_id();

   DENTER("pdhservice_initialize");
   pdhservice_get_names_from_registry(&container_system_lang, prim_lang_id);
   pdhservice_get_names_from_registry(&container_english, LANG_ENGLISH);
   DEXIT;
   return ret;
}

BOOL 
pdhservice_free(void)
{
   BOOL ret = TRUE;

   DENTER("pdhservice_free");
   pdhservice_free_container(&container_system_lang);
   pdhservice_free_container(&container_english);
   DEXIT;
   return ret;
}