s3: add perfcount idl and generated files.
[ira/wip.git] / source3 / registry / reg_perfcount.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit    2005,
6  *  Copyright (C) Gerald (Jerry) Carter      2005.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 #define PERFCOUNT_MAX_LEN 256
28
29 #define PERFCOUNTDIR    "perfmon"
30 #define NAMES_DB        "names.tdb"
31 #define DATA_DB         "data.tdb"
32
33 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind);
34
35 /*********************************************************************
36 *********************************************************************/
37
38 static char *counters_directory(const char *dbname)
39 {
40         char *path = NULL;
41         char *ret = NULL;
42         TALLOC_CTX *ctx = talloc_tos();
43
44         path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
45         if (!path) {
46                 return NULL;
47         }
48
49         ret = talloc_strdup(ctx, state_path(path));
50         TALLOC_FREE(path);
51         return ret;
52 }
53
54 /*********************************************************************
55 *********************************************************************/
56
57 void perfcount_init_keys( void )
58 {
59         char *p = state_path(PERFCOUNTDIR);
60
61         /* no registry keys; just create the perfmon directory */
62
63         if ( !directory_exist( p ) )
64                 mkdir( p, 0755 );
65
66         return;
67 }
68
69 /*********************************************************************
70 *********************************************************************/
71
72 uint32 reg_perfcount_get_base_index(void)
73 {
74         const char *fname = counters_directory( NAMES_DB );
75         TDB_CONTEXT *names;
76         TDB_DATA kbuf, dbuf;
77         char key[] = "1";
78         uint32 retval = 0;
79         char buf[PERFCOUNT_MAX_LEN];
80
81         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
82
83         if ( !names ) {
84                 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
85                 return 0;
86         }    
87         /* needs to read the value of key "1" from the counter_names.tdb file, as that is
88            where the total number of counters is stored. We're assuming no holes in the
89            enumeration.
90            The format for the counter_names.tdb file is:
91            key        value
92            1          num_counters
93            2          perf_counter1
94            3          perf_counter1_help
95            4          perf_counter2
96            5          perf_counter2_help
97            even_num   perf_counter<even_num>
98            even_num+1 perf_counter<even_num>_help
99            and so on.
100            So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
101         kbuf = string_tdb_data(key);
102         dbuf = tdb_fetch(names, kbuf);
103         if(dbuf.dptr == NULL)
104         {
105                 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
106                 tdb_close(names);
107                 return 0;
108         }
109         else
110         {
111                 tdb_close(names);
112                 memset(buf, 0, PERFCOUNT_MAX_LEN);
113                 memcpy(buf, dbuf.dptr, dbuf.dsize);
114                 retval = (uint32)atoi(buf);
115                 SAFE_FREE(dbuf.dptr);
116                 return retval;
117         }
118         return 0;
119 }
120
121 /*********************************************************************
122 *********************************************************************/
123
124 uint32 reg_perfcount_get_last_counter(uint32 base_index)
125 {
126         uint32 retval;
127
128         if(base_index == 0)
129                 retval = 0;
130         else
131                 retval = base_index * 2;
132
133         return retval;
134 }
135
136 /*********************************************************************
137 *********************************************************************/
138
139 uint32 reg_perfcount_get_last_help(uint32 last_counter)
140 {
141         uint32 retval;
142
143         if(last_counter == 0)
144                 retval = 0;
145         else
146                 retval = last_counter + 1;
147
148         return retval;
149 }
150
151
152 /*********************************************************************
153 *********************************************************************/
154
155 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
156                                                int keyval,
157                                                char **retbuf,
158                                                uint32 buffer_size)
159 {
160         TDB_DATA kbuf, dbuf;
161         char temp[256];
162         char *buf1 = *retbuf;
163         uint32 working_size = 0;
164         DATA_BLOB name_index, name;
165
166         memset(temp, 0, sizeof(temp));
167         snprintf(temp, sizeof(temp), "%d", keyval);
168         kbuf = string_tdb_data(temp);
169         dbuf = tdb_fetch(tdb, kbuf);
170         if(dbuf.dptr == NULL)
171         {
172                 /* If a key isn't there, just bypass it -- this really shouldn't 
173                    happen unless someone's mucking around with the tdb */
174                 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
175                           temp, tdb_name(tdb)));
176                 return buffer_size;
177         }
178         /* First encode the name_index */
179         working_size = (kbuf.dsize + 1)*sizeof(uint16);
180         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
181         if(!buf1) {
182                 buffer_size = 0;
183                 return buffer_size;
184         }
185         push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
186         memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
187         buffer_size += working_size;
188         /* Now encode the actual name */
189         working_size = (dbuf.dsize + 1)*sizeof(uint16);
190         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
191         if(!buf1) {
192                 buffer_size = 0;
193                 return buffer_size;
194         }
195         memset(temp, 0, sizeof(temp));
196         memcpy(temp, dbuf.dptr, dbuf.dsize);
197         SAFE_FREE(dbuf.dptr);
198         push_reg_sz(talloc_tos(), &name, temp);
199         memcpy(buf1+buffer_size, (char *)name.data, working_size);
200         buffer_size += working_size;
201
202         *retbuf = buf1;
203
204         return buffer_size;
205 }
206
207 /*********************************************************************
208 *********************************************************************/
209
210 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
211 {
212         char *buf1 = NULL;
213         uint32 buffer_size = 0;
214         TDB_CONTEXT *names;
215         const char *fname = counters_directory( NAMES_DB );
216         int i;
217
218         if(base_index == 0)
219                 return 0;
220
221         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
222
223         if(names == NULL)
224         {
225                 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
226                 return 0;
227         }    
228
229         for(i = 1; i <= base_index; i++)
230         {
231                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
232         }
233         tdb_close(names);
234
235         /* Now terminate the MULTI_SZ with a double unicode NULL */
236         buf1 = *retbuf;
237         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
238         if(!buf1) {
239                 buffer_size = 0;
240         } else {
241                 buf1[buffer_size++] = '\0';
242                 buf1[buffer_size++] = '\0';
243         }
244
245         *retbuf = buf1;
246
247         return buffer_size;
248 }
249
250 /*********************************************************************
251 *********************************************************************/
252
253 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
254 {
255         char *buf1 = NULL;
256         uint32 buffer_size = 0;
257         TDB_CONTEXT *names;
258         const char *fname = counters_directory( NAMES_DB );
259         int i;
260
261         if(base_index == 0)
262                 return 0;
263
264         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
265
266         if(names == NULL)
267         {
268                 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
269                 return 0;
270         }    
271
272         buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
273
274         for(i = 1; i <= base_index; i++)
275         {
276                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
277         }
278         tdb_close(names);
279
280         /* Now terminate the MULTI_SZ with a double unicode NULL */
281         buf1 = *retbuf;
282         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
283         if(!buf1) {
284                 buffer_size = 0;
285         } else {
286                 buf1[buffer_size++] = '\0';
287                 buf1[buffer_size++] = '\0';
288         }
289
290         *retbuf=buf1;
291
292         return buffer_size;
293 }
294
295 /*********************************************************************
296 *********************************************************************/
297
298 static void _reg_perfcount_make_key(TDB_DATA *key,
299                                     char *buf,
300                                     int buflen,
301                                     int key_part1,
302                                     const char *key_part2)
303 {
304         memset(buf, 0, buflen);
305         if(key_part2 != NULL)
306                 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
307         else 
308                 snprintf(buf, buflen, "%d", key_part1);
309
310         *key = string_tdb_data(buf);
311
312         return;
313 }
314
315 /*********************************************************************
316 *********************************************************************/
317
318 static bool _reg_perfcount_isparent(TDB_DATA data)
319 {
320         if(data.dsize > 0)
321         {
322                 if(data.dptr[0] == 'p')
323                         return True;
324                 else
325                         return False;
326         }
327         return False;
328 }
329
330 /*********************************************************************
331 *********************************************************************/
332
333 static bool _reg_perfcount_ischild(TDB_DATA data)
334 {
335         if(data.dsize > 0)
336         {
337                 if(data.dptr[0] == 'c')
338                         return True;
339                 else
340                         return False;
341         }
342         return False;
343 }
344
345 /*********************************************************************
346 *********************************************************************/
347
348 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
349 {
350         TDB_DATA key, data;
351         char buf[PERFCOUNT_MAX_LEN];
352
353         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
354         data = tdb_fetch(names, key);
355
356         if(data.dptr == NULL)
357                 return (uint32)PERF_NO_INSTANCES;
358
359         memset(buf, 0, PERFCOUNT_MAX_LEN);
360         memcpy(buf, data.dptr, data.dsize);
361         SAFE_FREE(data.dptr);
362         return (uint32)atoi(buf);
363 }
364
365 /*********************************************************************
366 *********************************************************************/
367
368 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
369                                       prs_struct *ps,
370                                       int num,
371                                       TDB_DATA data,
372                                       TDB_CONTEXT *names)
373 {
374         int i;
375         bool success = True;
376         struct PERF_OBJECT_TYPE *obj;
377
378         block->objects = (struct PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
379                                                                   block->objects,
380                                                                   struct PERF_OBJECT_TYPE,
381                                                                   block->NumObjectTypes+1);
382         if(block->objects == NULL)
383                 return False;
384         obj = &(block->objects[block->NumObjectTypes]);
385         memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(struct PERF_OBJECT_TYPE));
386         block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
387         block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
388         block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
389         block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
390         block->objects[block->NumObjectTypes].NumCounters = 0;
391         block->objects[block->NumObjectTypes].DefaultCounter = 0;
392         block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
393         block->objects[block->NumObjectTypes].counters = NULL;
394         block->objects[block->NumObjectTypes].instances = NULL;
395         block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
396         block->objects[block->NumObjectTypes].counter_data.data = NULL;
397         block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
398         block->NumObjectTypes+=1;
399
400         for(i = 0; i < (int)obj->NumInstances; i++) {
401                 success = _reg_perfcount_add_instance(obj, ps, i, names);
402         }
403
404         return success;
405 }
406
407 /*********************************************************************
408 *********************************************************************/
409
410 bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
411 {
412         TDB_CONTEXT *counters;
413         const char *fname = counters_directory( DATA_DB );
414
415         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
416
417         if(counters == NULL)
418         {
419                 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
420                 return False;
421         }    
422
423         *data = tdb_fetch(counters, key);
424
425         tdb_close(counters);
426
427         return True;
428 }
429
430 /*********************************************************************
431 *********************************************************************/
432
433 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
434 {
435         uint32 retval;
436
437         retval = CounterType;
438
439         /* First mask out reserved lower 8 bits */
440         retval = retval & 0xFFFFFF00;
441         retval = retval << 22;
442         retval = retval >> 22;
443
444         return retval;
445 }
446
447 /*********************************************************************
448 *********************************************************************/
449
450 static uint32 _reg_perfcount_compute_scale(int64_t data)
451 {
452         int scale = 0;
453         if(data == 0)
454                 return scale;
455         while(data > 100)
456         {
457                 data /= 10;
458                 scale--;
459         }
460         while(data < 10)
461         {
462                 data *= 10;
463                 scale++;
464         }
465
466         return (uint32)scale;
467 }
468
469 /*********************************************************************
470 *********************************************************************/
471
472 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
473                                             prs_struct *ps,
474                                             int CounterIndex,
475                                             struct PERF_OBJECT_TYPE *obj,
476                                             TDB_CONTEXT *names)
477 {
478         TDB_DATA key, data;
479         char buf[PERFCOUNT_MAX_LEN];
480         size_t dsize, padding;
481         long int data32, dbuf[2];
482         int64_t data64;
483         uint32 counter_size;
484
485         obj->counters[obj->NumCounters].DefaultScale = 0;
486         dbuf[0] = dbuf[1] = 0;
487         padding = 0;
488
489         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
490         data = tdb_fetch(names, key);
491         if(data.dptr == NULL)
492         {
493                 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
494                 return False;
495         }
496         memset(buf, 0, PERFCOUNT_MAX_LEN);
497         memcpy(buf, data.dptr, data.dsize);
498         obj->counters[obj->NumCounters].CounterType = atoi(buf);
499         DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
500                    obj->counters[obj->NumCounters].CounterType, CounterIndex));
501         SAFE_FREE(data.dptr);
502
503         /* Fetch the actual data */
504         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
505         _reg_perfcount_get_counter_data(key, &data);
506         if(data.dptr == NULL)
507         {
508                 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
509                 return False;
510         }
511
512         counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
513
514         if(counter_size == PERF_SIZE_DWORD)
515         {
516                 dsize = sizeof(data32);
517                 memset(buf, 0, PERFCOUNT_MAX_LEN);
518                 memcpy(buf, data.dptr, data.dsize);
519                 data32 = strtol(buf, NULL, 0);
520                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
521                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
522                 else
523                         obj->counters[obj->NumCounters].DefaultScale = 0;
524                 dbuf[0] = data32;
525                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
526         }
527         else if(counter_size == PERF_SIZE_LARGE)
528         {
529                 dsize = sizeof(data64);
530                 memset(buf, 0, PERFCOUNT_MAX_LEN);
531                 memcpy(buf, data.dptr, data.dsize);
532                 data64 = atof(buf);
533                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
534                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
535                 else
536                         obj->counters[obj->NumCounters].DefaultScale = 0;
537                 memcpy((void *)dbuf, (const void *)&data64, dsize);
538                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
539         }
540         else /* PERF_SIZE_VARIABLE_LEN */
541         {
542                 dsize = data.dsize;
543                 memset(buf, 0, PERFCOUNT_MAX_LEN);
544                 memcpy(buf, data.dptr, data.dsize);
545         }
546         SAFE_FREE(data.dptr);
547
548         obj->counter_data.ByteLength += dsize + padding;
549         obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
550                                                       obj->counter_data.data,
551                                                       uint8,
552                                                       obj->counter_data.ByteLength - sizeof(uint32));
553         if(obj->counter_data.data == NULL)
554                 return False;
555         if(dbuf[0] != 0 || dbuf[1] != 0)
556         {
557                 memcpy((void *)(obj->counter_data.data + 
558                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
559                        (const void *)dbuf, dsize);
560         }
561         else
562         {
563                 /* Handling PERF_SIZE_VARIABLE_LEN */
564                 memcpy((void *)(obj->counter_data.data +
565                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
566                        (const void *)buf, dsize);
567         }
568         obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
569         if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
570         {
571                 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
572         }
573         obj->counters[obj->NumCounters].CounterSize = dsize;
574
575         return True;
576 }
577
578 /*********************************************************************
579 *********************************************************************/
580
581 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
582 {
583         int i;
584
585         struct PERF_OBJECT_TYPE *obj = NULL;
586
587         for(i = 0; i < block->NumObjectTypes; i++)
588         {
589                 if(block->objects[i].ObjectNameTitleIndex == objind)
590                 {
591                         obj = &(block->objects[i]);
592                 }
593         }
594
595         return obj;
596 }
597
598 /*********************************************************************
599 *********************************************************************/
600
601 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
602                                        prs_struct *ps,
603                                        int num,
604                                        TDB_DATA data,
605                                        TDB_CONTEXT *names)
606 {
607         char *begin, *end, *start, *stop;
608         int parent;
609         struct PERF_OBJECT_TYPE *obj;
610         bool success = True;
611         char buf[PERFCOUNT_MAX_LEN];
612
613         obj = NULL;
614         memset(buf, 0, PERFCOUNT_MAX_LEN);
615         memcpy(buf, data.dptr, data.dsize);
616         begin = index(buf, '[');
617         end = index(buf, ']');
618         if(begin == NULL || end == NULL)
619                 return False;
620         start = begin+1;
621
622         while(start < end) {
623                 stop = index(start, ',');
624                 if(stop == NULL)
625                         stop = end;
626                 *stop = '\0';
627                 parent = atoi(start);
628
629                 obj = _reg_perfcount_find_obj(block, parent);
630                 if(obj == NULL) {
631                         /* At this point we require that the parent object exist.
632                            This can probably be handled better at some later time */
633                         DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
634                                   parent, num));
635                         return False;
636                 }
637                 obj->counters = (struct PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
638                                                                                 obj->counters,
639                                                                                 struct PERF_COUNTER_DEFINITION,
640                                                                                 obj->NumCounters+1);
641                 if(obj->counters == NULL)
642                         return False;
643                 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(struct PERF_COUNTER_DEFINITION));
644                 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
645                 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
646                 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
647                 obj->counters[obj->NumCounters].ByteLength = sizeof(struct PERF_COUNTER_DEFINITION);
648                 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
649                 obj->NumCounters += 1;
650                 start = stop + 1;
651         }
652
653         /* Handle case of Objects/Counters without any counter data, which would suggest
654            that the required instances are not there yet, so change NumInstances from
655            PERF_NO_INSTANCES to 0 */
656
657         return success;
658 }
659
660 /*********************************************************************
661 *********************************************************************/
662
663 bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
664                                       prs_struct *ps,
665                                       int instId,
666                                       struct PERF_OBJECT_TYPE *obj,
667                                       TDB_CONTEXT *names)
668 {
669         TDB_DATA key, data;
670         char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
671         smb_ucs2_t *name = NULL;
672         int pad;
673
674         /* First grab the instance data from the data file */
675         memset(temp, 0, PERFCOUNT_MAX_LEN);
676         snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
677         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
678         if (!_reg_perfcount_get_counter_data(key, &data)) {
679                 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
680                 return false;
681         }
682         if(data.dptr == NULL)
683         {
684                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
685                           buf));
686                 return False;
687         }
688         inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
689         inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
690                                                        inst->counter_data.data,
691                                                        uint8,
692                                                        data.dsize);
693         if(inst->counter_data.data == NULL)
694                 return False;
695         memset(inst->counter_data.data, 0, data.dsize);
696         memcpy(inst->counter_data.data, data.dptr, data.dsize);
697         SAFE_FREE(data.dptr);
698
699         /* Fetch instance name */
700         memset(temp, 0, PERFCOUNT_MAX_LEN);
701         snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
702         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
703         data = tdb_fetch(names, key);
704         if(data.dptr == NULL)
705         {
706                 /* Not actually an error, but possibly unintended? -- just logging FYI */
707                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
708                           buf));
709                 inst->NameLength = 0;
710         }
711         else
712         {
713                 memset(buf, 0, PERFCOUNT_MAX_LEN);
714                 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
715                 buf[PERFCOUNT_MAX_LEN-1] = '\0';
716                 inst->NameLength = rpcstr_push_talloc(ps->mem_ctx, &name, buf);
717                 if (inst->NameLength == (uint32_t)-1 || !name) {
718                         SAFE_FREE(data.dptr);
719                         return False;
720                 }
721                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
722                                                   inst->data,
723                                                   uint8,
724                                                   inst->NameLength);
725                 if (inst->data == NULL) {
726                         SAFE_FREE(data.dptr);
727                         return False;
728                 }
729                 memcpy(inst->data, name, inst->NameLength);
730                 SAFE_FREE(data.dptr);
731         }
732
733         inst->ParentObjectTitleIndex = 0;
734         inst->ParentObjectTitlePointer = 0;
735         inst->UniqueID = PERF_NO_UNIQUE_ID;
736         inst->NameOffset = 6 * sizeof(uint32);
737
738         inst->ByteLength = inst->NameOffset + inst->NameLength;
739         /* Need to be aligned on a 64-bit boundary here for counter_data */
740         if((pad = (inst->ByteLength % 8)))
741         {
742                 pad = 8 - pad;
743                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
744                                                   inst->data,
745                                                   uint8,
746                                                   inst->NameLength + pad);
747                 memset(inst->data + inst->NameLength, 0, pad);
748                 inst->ByteLength += pad;
749         }
750
751         return True;
752 }
753
754 /*********************************************************************
755 *********************************************************************/
756
757 bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
758                                  prs_struct *ps,
759                                  int instInd,
760                                  TDB_CONTEXT *names)
761 {
762         struct PERF_INSTANCE_DEFINITION *inst;
763
764         if(obj->instances == NULL) {
765                 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
766                                                       obj->instances,
767                                                       struct PERF_INSTANCE_DEFINITION,
768                                                       obj->NumInstances);
769         }
770         if(obj->instances == NULL)
771                 return False;
772
773         memset(&(obj->instances[instInd]), 0, sizeof(struct PERF_INSTANCE_DEFINITION));
774         inst = &(obj->instances[instInd]);
775         return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
776 }
777
778 /*********************************************************************
779 *********************************************************************/
780
781 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
782                                           prs_struct *ps,
783                                           int base_index,
784                                           TDB_CONTEXT *names)
785 {
786         bool success;
787         int i, j, retval = 0;
788         char keybuf[PERFCOUNT_MAX_LEN];
789         TDB_DATA key, data;
790
791         for(i = 1; i <= base_index; i++)
792         {
793                 j = i*2;
794                 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
795                 data = tdb_fetch(names, key);
796                 if(data.dptr != NULL)
797                 {
798                         if(_reg_perfcount_isparent(data))
799                                 success = _reg_perfcount_add_object(block, ps, j, data, names);
800                         else if(_reg_perfcount_ischild(data))
801                                 success = _reg_perfcount_add_counter(block, ps, j, data, names);
802                         else
803                         {
804                                 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
805                                 success = False;
806                         }
807                         if(success == False)
808                         {
809                                 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
810                                 retval = -1;
811                         }
812                         SAFE_FREE(data.dptr);
813                 }
814                 else
815                         DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
816         }       
817         return retval;
818 }
819
820 /*********************************************************************
821 *********************************************************************/
822
823 static bool _reg_perfcount_get_64(uint64_t *retval,
824                                   TDB_CONTEXT *tdb,
825                                   int key_part1,
826                                   const char *key_part2)
827 {
828         TDB_DATA key, data;
829         char buf[PERFCOUNT_MAX_LEN];
830
831         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
832
833         data = tdb_fetch(tdb, key);
834         if(data.dptr == NULL)
835         {
836                 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
837                 return False;
838         }
839
840         memset(buf, 0, PERFCOUNT_MAX_LEN);
841         memcpy(buf, data.dptr, data.dsize);
842         SAFE_FREE(data.dptr);
843
844         *retval = atof(buf);
845
846         return True;
847 }
848
849 /*********************************************************************
850 *********************************************************************/
851
852 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
853                                                 TDB_CONTEXT *names)
854 {
855         uint64_t PerfFreq, PerfTime, PerfTime100nSec;
856         TDB_CONTEXT *counters;
857         bool status = False;
858         const char *fname = counters_directory( DATA_DB );
859
860         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
861
862         if(counters == NULL)
863         {
864                 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
865                 return False;
866         }    
867
868         status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
869         if(status == False)
870         {
871                 tdb_close(counters);
872                 return status;
873         }
874         memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
875
876         status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
877         if(status == False)
878         {
879                 tdb_close(counters);
880                 return status;
881         }
882         memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
883
884         status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
885         if(status == False)
886         {
887                 tdb_close(counters);
888                 return status;
889         }
890         memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
891
892         tdb_close(counters);
893         return True;
894 }
895
896 /*******************************************************************
897 ********************************************************************/
898
899 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
900 {
901         systime->year=unixtime->tm_year+1900;
902         systime->month=unixtime->tm_mon+1;
903         systime->dayofweek=unixtime->tm_wday;
904         systime->day=unixtime->tm_mday;
905         systime->hour=unixtime->tm_hour;
906         systime->minute=unixtime->tm_min;
907         systime->second=unixtime->tm_sec;
908         systime->milliseconds=0;
909
910         return True;
911 }
912
913 /*********************************************************************
914 *********************************************************************/
915
916 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
917                                            prs_struct *ps, TDB_CONTEXT *names)
918 {
919         smb_ucs2_t *temp = NULL;
920         time_t tm;
921
922         if (rpcstr_push_talloc(ps->mem_ctx, &temp, "PERF")==(size_t)-1) {
923                 return false;
924         }
925         if (!temp) {
926                 return false;
927         }
928         memcpy(block->Signature, temp, strlen_w(temp) *2);
929
930         if(ps->bigendian_data == RPC_BIG_ENDIAN)
931                 block->LittleEndian = 0;
932         else
933                 block->LittleEndian = 1;
934         block->Version = 1;
935         block->Revision = 1;
936         block->TotalByteLength = 0;
937         block->NumObjectTypes = 0;
938         block->DefaultObject = -1;
939         block->objects = NULL;
940         tm = time(NULL);
941         make_systemtime(&(block->SystemTime), gmtime(&tm));
942         _reg_perfcount_init_data_block_perf(block, names);
943         memset(temp, 0, sizeof(temp));
944         rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
945         block->SystemNameLength = (strlen_w(temp) * 2) + 2;
946         block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
947         if (block->data == NULL) {
948                 return False;
949         }
950         memcpy(block->data, temp, block->SystemNameLength);
951         block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
952         block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
953         /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
954            so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
955         block->HeaderLength += 8 - (block->HeaderLength % 8);
956
957         return True;
958 }
959
960 /*********************************************************************
961 *********************************************************************/
962
963 static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, prs_struct *ps)
964 {
965         int obj, cnt, inst, pad, i;
966         struct PERF_OBJECT_TYPE *object;
967         struct PERF_INSTANCE_DEFINITION *instance;
968         struct PERF_COUNTER_DEFINITION *counter;
969         struct PERF_COUNTER_BLOCK *counter_data;
970         char *temp = NULL, *src_addr, *dst_addr;
971
972         block->TotalByteLength = 0;
973         object = block->objects;
974         for(obj = 0; obj < block->NumObjectTypes; obj++)
975         {
976                 object[obj].TotalByteLength = 0;
977                 object[obj].DefinitionLength = 0;
978                 instance = object[obj].instances;
979                 counter = object[obj].counters;
980                 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
981                 {
982                         object[obj].TotalByteLength += counter[cnt].ByteLength;
983                         object[obj].DefinitionLength += counter[cnt].ByteLength;
984                 }
985                 if(object[obj].NumInstances != PERF_NO_INSTANCES)
986                 {
987                         for(inst = 0; inst < object[obj].NumInstances; inst++)
988                         {
989                                 instance = &(object[obj].instances[inst]);
990                                 object[obj].TotalByteLength += instance->ByteLength;
991                                 counter_data = &(instance->counter_data);
992                                 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
993                                 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
994                                 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
995                                                             temp, 
996                                                             char, 
997                                                             counter_data->ByteLength- sizeof(counter_data->ByteLength));
998                                 if (temp == NULL) {
999                                         return 0;
1000                                 }
1001                                 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1002                                 src_addr = (char *)counter_data->data;
1003                                 for(i = 0; i < object[obj].NumCounters; i++)
1004                                 {
1005                                         counter = &(object[obj].counters[i]);
1006                                         dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
1007                                         memcpy(dst_addr, src_addr, counter->CounterSize);
1008                                         src_addr += counter->CounterSize;
1009                                 }
1010                                 /* Make sure to be 64-bit aligned */
1011                                 if((pad = (counter_data->ByteLength % 8)))
1012                                 {
1013                                         pad = 8 - pad;
1014                                 }
1015                                 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
1016                                                                          counter_data->data,
1017                                                                          uint8,
1018                                                                          counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1019                                 if (counter_data->data == NULL) {
1020                                         return 0;
1021                                 }
1022                                 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1023                                 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1024                                 counter_data->ByteLength += pad;
1025                                 object[obj].TotalByteLength += counter_data->ByteLength;
1026                         }
1027                 }
1028                 else
1029                 {
1030                         /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1031                            so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1032                         if((pad = (object[obj].counter_data.ByteLength % 8)))
1033                         {
1034                                 pad = 8 - pad;
1035                                 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
1036                                                                                      object[obj].counter_data.data,
1037                                                                                      uint8, 
1038                                                                                      object[obj].counter_data.ByteLength + pad);
1039                                 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1040                                 object[obj].counter_data.ByteLength += pad;
1041                         }
1042                         object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1043                 }
1044                 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(struct PERF_COUNTER_BLOCK));
1045                 object[obj].TotalByteLength += object[obj].HeaderLength;
1046                 object[obj].DefinitionLength += object[obj].HeaderLength;
1047
1048                 block->TotalByteLength += object[obj].TotalByteLength;
1049         }
1050
1051         return block->TotalByteLength;
1052 }
1053
1054 /*********************************************************************
1055 *********************************************************************/
1056
1057 uint32 reg_perfcount_get_perf_data_block(uint32 base_index, 
1058                                          prs_struct *ps, 
1059                                          struct PERF_DATA_BLOCK *block,
1060                                          const char *object_ids)
1061 {
1062         uint32 buffer_size = 0;
1063         const char *fname = counters_directory( NAMES_DB );
1064         TDB_CONTEXT *names;
1065         int retval = 0;
1066
1067         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1068
1069         if(names == NULL)
1070         {
1071                 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1072                 return 0;
1073         }
1074
1075         if (!_reg_perfcount_init_data_block(block, ps, names)) {
1076                 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1077                 tdb_close(names);
1078                 return 0;
1079         }
1080
1081         reg_perfcount_get_last_counter(base_index);
1082
1083         if(object_ids == NULL)
1084         {
1085                 /* we're getting a request for "Global" here */
1086                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1087         }
1088         else
1089         {
1090                 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1091                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1092         }
1093         buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
1094
1095         tdb_close(names);
1096
1097         if (retval == -1) {
1098                 return 0;
1099         }
1100
1101         return buffer_size + block->HeaderLength;
1102 }
1103
1104 /*******************************************************************
1105 ********************************************************************/
1106
1107 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1108 {
1109         if(!prs_uint16("year", ps, depth, &systime->year))
1110                 return False;
1111         if(!prs_uint16("month", ps, depth, &systime->month))
1112                 return False;
1113         if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1114                 return False;
1115         if(!prs_uint16("day", ps, depth, &systime->day))
1116                 return False;
1117         if(!prs_uint16("hour", ps, depth, &systime->hour))
1118                 return False;
1119         if(!prs_uint16("minute", ps, depth, &systime->minute))
1120                 return False;
1121         if(!prs_uint16("second", ps, depth, &systime->second))
1122                 return False;
1123         if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1124                 return False;
1125
1126         return True;
1127 }
1128
1129 /*********************************************************************
1130 *********************************************************************/
1131
1132 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1133 {
1134         int i;
1135         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1136         depth++;
1137
1138         if(!prs_align(ps))
1139                 return False;
1140         for(i = 0; i < 4; i++)
1141         {
1142                 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1143                         return False;
1144         }
1145         if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1146                 return False;
1147         if(!prs_uint32("Version", ps, depth, &block.Version))
1148                 return False;
1149         if(!prs_uint32("Revision", ps, depth, &block.Revision))
1150                 return False;
1151         if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1152                 return False;
1153         if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1154                 return False;
1155         if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1156                 return False;
1157         if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1158                 return False;
1159         if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1160                 return False;
1161         if(!prs_uint32("Padding", ps, depth, &block.Padding))
1162                 return False;
1163         if(!prs_align_uint64(ps))
1164                 return False;
1165         if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1166                 return False;
1167         if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1168                 return False;
1169         if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1170                 return False;
1171         if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1172                 return False;
1173         if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1174                 return False;
1175         /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1176         if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
1177                        block.HeaderLength - block.SystemNameOffset)) 
1178                 return False;
1179
1180         return True;
1181 }
1182
1183 /*********************************************************************
1184 *********************************************************************/
1185
1186 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1187                                                   struct PERF_OBJECT_TYPE object,
1188                                                   int depth)
1189 {
1190         int cnt;
1191         struct PERF_COUNTER_DEFINITION counter;
1192
1193         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1194         depth++;
1195
1196         for(cnt = 0; cnt < object.NumCounters; cnt++)
1197         {
1198                 counter = object.counters[cnt];
1199
1200                 if(!prs_align(ps))
1201                         return False;
1202                 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1203                         return False;
1204                 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1205                         return False;
1206                 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1207                         return False;
1208                 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1209                         return False;
1210                 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1211                         return False;
1212                 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1213                         return False;
1214                 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1215                         return False;
1216                 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1217                         return False;
1218                 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1219                         return False;
1220                 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1221                         return False;
1222         }
1223
1224         return True;
1225 }
1226
1227 /*********************************************************************
1228 *********************************************************************/
1229
1230 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
1231                                                       struct PERF_COUNTER_BLOCK counter_data,
1232                                                       int depth)
1233 {
1234         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1235         depth++;
1236
1237         if(!prs_align_uint64(ps))
1238                 return False;
1239
1240         if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1241                 return False;
1242         if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1243                 return False;
1244         if(!prs_align_uint64(ps))
1245                 return False;
1246
1247         return True;
1248 }
1249
1250 /*********************************************************************
1251 *********************************************************************/
1252
1253 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1254                                                    struct PERF_OBJECT_TYPE object,
1255                                                    int depth)
1256 {
1257         struct PERF_INSTANCE_DEFINITION instance;
1258         int inst;
1259
1260         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1261         depth++;
1262
1263         for(inst = 0; inst < object.NumInstances; inst++)
1264         {
1265                 instance = object.instances[inst];
1266
1267                 if(!prs_align(ps))
1268                         return False;
1269                 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1270                         return False;
1271                 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1272                         return False;
1273                 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1274                         return False;
1275                 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1276                         return False;
1277                 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1278                         return False;
1279                 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1280                         return False;
1281                 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1282                                instance.ByteLength - instance.NameOffset))
1283                         return False;
1284                 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1285                         return False;
1286         }
1287
1288         return True;
1289 }
1290
1291 /*********************************************************************
1292 *********************************************************************/
1293
1294 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1295 {
1296         int obj;
1297
1298         struct PERF_OBJECT_TYPE object;
1299
1300         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1301         depth++;
1302
1303         for(obj = 0; obj < block.NumObjectTypes; obj++)
1304         {
1305                 object = block.objects[obj];
1306
1307                 if(!prs_align(ps))
1308                         return False;
1309
1310                 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1311                         return False;
1312                 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1313                         return False;
1314                 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1315                         return False;
1316                 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1317                         return False;
1318                 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1319                         return False;
1320                 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1321                         return False;
1322                 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1323                         return False;
1324                 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1325                         return False;
1326                 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1327                         return False;
1328                 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1329                         return False;
1330                 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1331                         return False;
1332                 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1333                         return False;
1334                 if(!prs_align_uint64(ps))
1335                         return False;
1336                 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1337                         return False;
1338                 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1339                         return False;
1340
1341                 /* Now do the counters */
1342                 /* If no instances, encode counter_data */
1343                 /* If instances, encode instace plus counter data for each instance */
1344                 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1345                         return False;
1346                 if(object.NumInstances == PERF_NO_INSTANCES)
1347                 {
1348                         if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1349                                 return False;
1350                 }
1351                 else
1352                 {
1353                         if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1354                                 return False;
1355                 }
1356         }
1357
1358         return True;
1359 }
1360
1361 /*********************************************************************
1362 *********************************************************************/
1363
1364 static bool _reg_perfcount_marshall_hkpd(prs_struct *ps, struct PERF_DATA_BLOCK block)
1365 {
1366         int depth = 0;
1367         if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1368         {
1369                 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1370                         return True;
1371         }
1372         return False;
1373 }
1374
1375 /*********************************************************************
1376 *********************************************************************/
1377
1378 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
1379 {
1380         /*
1381          * For a detailed description of the layout of this structure,
1382          * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1383          *
1384          * By 2006-11-23 this link did not work anymore, I found something
1385          * promising under
1386          * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1387          */
1388         struct PERF_DATA_BLOCK block;
1389         uint32 buffer_size, base_index; 
1390
1391         buffer_size = 0;
1392         base_index = reg_perfcount_get_base_index();
1393         ZERO_STRUCT(block);
1394
1395         buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1396
1397         if(buffer_size < max_buf_size)
1398         {
1399                 *outbuf_len = buffer_size;
1400                 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1401                         return WERR_OK;
1402                 else
1403                         return WERR_NOMEM;
1404         }
1405         else
1406         {
1407                 *outbuf_len = max_buf_size;
1408                 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1409                 return WERR_INSUFFICIENT_BUFFER;
1410         }
1411 }