r17333: Some C++ warnings
[Samba.git] / source / registry / reg_perfcount.c
blobfebae62ad01efb18474d7cb2758a332a3dbee648
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
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 2 of the License, or
11 * (at your option) any later version.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
28 #define PERFCOUNT_MAX_LEN 256
30 #define PERFCOUNTDIR "perfmon"
31 #define NAMES_DB "names.tdb"
32 #define DATA_DB "data.tdb"
34 /*********************************************************************
35 *********************************************************************/
37 static char* counters_directory( const char *dbname )
39 static pstring fname;
40 fstring path;
42 if ( !dbname )
43 return NULL;
45 fstr_sprintf( path, "%s/%s", PERFCOUNTDIR, dbname );
47 pstrcpy( fname, lock_path( path ) );
49 return fname;
52 /*********************************************************************
53 *********************************************************************/
55 void perfcount_init_keys( void )
57 char *p = lock_path(PERFCOUNTDIR);
59 /* no registry keys; just create the perfmon directory */
61 if ( !directory_exist( p, NULL ) )
62 mkdir( p, 0755 );
64 return;
67 /*********************************************************************
68 *********************************************************************/
70 uint32 reg_perfcount_get_base_index(void)
72 const char *fname = counters_directory( NAMES_DB );
73 TDB_CONTEXT *names;
74 TDB_DATA kbuf, dbuf;
75 char key[] = "1";
76 uint32 retval = 0;
77 char buf[PERFCOUNT_MAX_LEN];
79 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
81 if ( !names ) {
82 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
83 return 0;
85 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
86 where the total number of counters is stored. We're assuming no holes in the
87 enumeration.
88 The format for the counter_names.tdb file is:
89 key value
90 1 num_counters
91 2 perf_counter1
92 3 perf_counter1_help
93 4 perf_counter2
94 5 perf_counter2_help
95 even_num perf_counter<even_num>
96 even_num+1 perf_counter<even_num>_help
97 and so on.
98 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
99 kbuf.dptr = key;
100 kbuf.dsize = strlen(key);
101 dbuf = tdb_fetch(names, kbuf);
102 if(dbuf.dptr == NULL)
104 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
105 tdb_close(names);
106 return 0;
108 else
110 tdb_close(names);
111 memset(buf, 0, PERFCOUNT_MAX_LEN);
112 memcpy(buf, dbuf.dptr, dbuf.dsize);
113 retval = (uint32)atoi(buf);
114 SAFE_FREE(dbuf.dptr);
115 return retval;
117 return 0;
120 /*********************************************************************
121 *********************************************************************/
123 uint32 reg_perfcount_get_last_counter(uint32 base_index)
125 uint32 retval;
127 if(base_index == 0)
128 retval = 0;
129 else
130 retval = base_index * 2;
132 return retval;
135 /*********************************************************************
136 *********************************************************************/
138 uint32 reg_perfcount_get_last_help(uint32 last_counter)
140 uint32 retval;
142 if(last_counter == 0)
143 retval = 0;
144 else
145 retval = last_counter + 1;
147 return retval;
151 /*********************************************************************
152 *********************************************************************/
154 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
155 int keyval,
156 char **retbuf,
157 uint32 buffer_size)
159 TDB_DATA kbuf, dbuf;
160 char temp[256];
161 char *buf1 = *retbuf;
162 uint32 working_size = 0;
163 UNISTR2 name_index, name;
165 memset(temp, 0, sizeof(temp));
166 snprintf(temp, sizeof(temp), "%d", keyval);
167 kbuf.dptr = temp;
168 kbuf.dsize = strlen(temp);
169 dbuf = tdb_fetch(tdb, kbuf);
170 if(dbuf.dptr == NULL)
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;
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;
185 init_unistr2(&name_index, kbuf.dptr, UNI_STR_TERMINATE);
186 memcpy(buf1+buffer_size, (char *)name_index.buffer, 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;
195 memset(temp, 0, sizeof(temp));
196 memcpy(temp, dbuf.dptr, dbuf.dsize);
197 SAFE_FREE(dbuf.dptr);
198 init_unistr2(&name, temp, UNI_STR_TERMINATE);
199 memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
200 buffer_size += working_size;
202 *retbuf = buf1;
204 return buffer_size;
207 /*********************************************************************
208 *********************************************************************/
210 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
212 char *buf1 = NULL;
213 uint32 buffer_size = 0;
214 TDB_CONTEXT *names;
215 const char *fname = counters_directory( NAMES_DB );
216 int i;
218 if(base_index == 0)
219 return 0;
221 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
223 if(names == NULL)
225 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
226 return 0;
229 for(i = 1; i <= base_index; i++)
231 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
233 tdb_close(names);
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';
245 *retbuf = buf1;
247 return buffer_size;
250 /*********************************************************************
251 *********************************************************************/
253 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
255 char *buf1 = NULL;
256 uint32 buffer_size = 0;
257 TDB_CONTEXT *names;
258 const char *fname = counters_directory( NAMES_DB );
259 int i;
261 if(base_index == 0)
262 return 0;
264 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
266 if(names == NULL)
268 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
269 return 0;
272 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
274 for(i = 1; i <= base_index; i++)
276 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
278 tdb_close(names);
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';
290 *retbuf=buf1;
292 return buffer_size;
295 /*********************************************************************
296 *********************************************************************/
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)
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);
310 key->dptr = buf;
311 key->dsize = strlen(buf);
313 return;
316 /*********************************************************************
317 *********************************************************************/
319 static BOOL _reg_perfcount_isparent(TDB_DATA data)
321 if(data.dsize > 0)
323 if(data.dptr[0] == 'p')
324 return True;
325 else
326 return False;
328 return False;
331 /*********************************************************************
332 *********************************************************************/
334 static BOOL _reg_perfcount_ischild(TDB_DATA data)
336 if(data.dsize > 0)
338 if(data.dptr[0] == 'c')
339 return True;
340 else
341 return False;
343 return False;
346 /*********************************************************************
347 *********************************************************************/
349 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
351 TDB_DATA key, data;
352 char buf[PERFCOUNT_MAX_LEN];
354 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
355 data = tdb_fetch(names, key);
357 if(data.dptr == NULL)
358 return (uint32)PERF_NO_INSTANCES;
360 memset(buf, 0, PERFCOUNT_MAX_LEN);
361 memcpy(buf, data.dptr, data.dsize);
362 return (uint32)atoi(buf);
365 /*********************************************************************
366 *********************************************************************/
368 static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
369 prs_struct *ps,
370 int num,
371 TDB_DATA data,
372 TDB_CONTEXT *names)
374 int i;
375 BOOL success = True;
376 PERF_OBJECT_TYPE *obj;
378 block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
379 block->objects,
380 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(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;
400 for(i = 0; i < (int)obj->NumInstances; i++) {
401 success = _reg_perfcount_add_instance(obj, ps, i, names);
404 return success;
407 /*********************************************************************
408 *********************************************************************/
410 BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
412 TDB_CONTEXT *counters;
413 const char *fname = counters_directory( DATA_DB );
415 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
417 if(counters == NULL)
419 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
420 return False;
423 *data = tdb_fetch(counters, key);
425 tdb_close(counters);
427 return True;
430 /*********************************************************************
431 *********************************************************************/
433 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
435 uint32 retval;
437 retval = CounterType;
439 /* First mask out reserved lower 8 bits */
440 retval = retval & 0xFFFFFF00;
441 retval = retval << 22;
442 retval = retval >> 22;
444 return retval;
447 /*********************************************************************
448 *********************************************************************/
450 static uint32 _reg_perfcount_compute_scale(SMB_BIG_INT data)
452 int scale = 0;
453 if(data == 0)
454 return scale;
455 while(data > 100)
457 data /= 10;
458 scale--;
460 while(data < 10)
462 data *= 10;
463 scale++;
466 return (uint32)scale;
469 /*********************************************************************
470 *********************************************************************/
472 static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
473 prs_struct *ps,
474 int CounterIndex,
475 PERF_OBJECT_TYPE *obj,
476 TDB_CONTEXT *names)
478 TDB_DATA key, data;
479 char buf[PERFCOUNT_MAX_LEN];
480 size_t dsize, padding;
481 long int data32, dbuf[2];
482 SMB_BIG_INT data64;
483 uint32 counter_size;
485 obj->counters[obj->NumCounters].DefaultScale = 0;
486 dbuf[0] = dbuf[1] = 0;
487 padding = 0;
489 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
490 data = tdb_fetch(names, key);
491 if(data.dptr == NULL)
493 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
494 return False;
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);
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)
508 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
509 return False;
512 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
514 if(counter_size == PERF_SIZE_DWORD)
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((SMB_BIG_INT)data32);
522 else
523 obj->counters[obj->NumCounters].DefaultScale = 0;
524 dbuf[0] = data32;
525 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
527 else if(counter_size == PERF_SIZE_LARGE)
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;
540 else /* PERF_SIZE_VARIABLE_LEN */
542 dsize = data.dsize;
543 memset(buf, 0, PERFCOUNT_MAX_LEN);
544 memcpy(buf, data.dptr, data.dsize);
546 SAFE_FREE(data.dptr);
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)
557 memcpy((void *)(obj->counter_data.data +
558 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
559 (const void *)dbuf, dsize);
561 else
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);
568 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
569 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
571 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
573 obj->counters[obj->NumCounters].CounterSize = dsize;
575 return True;
578 /*********************************************************************
579 *********************************************************************/
581 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
583 int i;
585 PERF_OBJECT_TYPE *obj = NULL;
587 for(i = 0; i < block->NumObjectTypes; i++)
589 if(block->objects[i].ObjectNameTitleIndex == objind)
591 obj = &(block->objects[i]);
595 return obj;
598 /*********************************************************************
599 *********************************************************************/
601 static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
602 prs_struct *ps,
603 int num,
604 TDB_DATA data,
605 TDB_CONTEXT *names)
607 char *begin, *end, *start, *stop;
608 int parent;
609 PERF_OBJECT_TYPE *obj;
610 BOOL success = True;
611 char buf[PERFCOUNT_MAX_LEN];
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;
622 while(start < end) {
623 stop = index(start, ',');
624 if(stop == NULL)
625 stop = end;
626 *stop = '\0';
627 parent = atoi(start);
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;
637 obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
638 obj->counters,
639 PERF_COUNTER_DEFINITION,
640 obj->NumCounters+1);
641 if(obj->counters == NULL)
642 return False;
643 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(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(PERF_COUNTER_DEFINITION);
648 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
649 obj->NumCounters += 1;
650 start = stop + 1;
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 */
657 return success;
660 /*********************************************************************
661 *********************************************************************/
663 BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
664 prs_struct *ps,
665 int instId,
666 PERF_OBJECT_TYPE *obj,
667 TDB_CONTEXT *names)
669 TDB_DATA key, data;
670 char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
671 wpstring name;
672 int pad;
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 _reg_perfcount_get_counter_data(key, &data);
679 if(data.dptr == NULL)
681 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
682 buf));
683 return False;
685 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
686 inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
687 inst->counter_data.data,
688 uint8,
689 data.dsize);
690 if(inst->counter_data.data == NULL)
691 return False;
692 memset(inst->counter_data.data, 0, data.dsize);
693 memcpy(inst->counter_data.data, data.dptr, data.dsize);
694 SAFE_FREE(data.dptr);
696 /* Fetch instance name */
697 memset(temp, 0, PERFCOUNT_MAX_LEN);
698 snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
699 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
700 data = tdb_fetch(names, key);
701 if(data.dptr == NULL)
703 /* Not actually an error, but possibly unintended? -- just logging FYI */
704 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
705 buf));
706 inst->NameLength = 0;
708 else
710 memset(buf, 0, PERFCOUNT_MAX_LEN);
711 memcpy(buf, data.dptr, data.dsize);
712 rpcstr_push((void *)name, buf, sizeof(name), STR_TERMINATE);
713 inst->NameLength = (strlen_w(name) * 2) + 2;
714 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
715 inst->data,
716 uint8,
717 inst->NameLength);
718 if (inst->data == NULL) {
719 SAFE_FREE(data.dptr);
720 return False;
722 memcpy(inst->data, name, inst->NameLength);
723 SAFE_FREE(data.dptr);
726 inst->ParentObjectTitleIndex = 0;
727 inst->ParentObjectTitlePointer = 0;
728 inst->UniqueID = PERF_NO_UNIQUE_ID;
729 inst->NameOffset = 6 * sizeof(uint32);
731 inst->ByteLength = inst->NameOffset + inst->NameLength;
732 /* Need to be aligned on a 64-bit boundary here for counter_data */
733 if((pad = (inst->ByteLength % 8)))
735 pad = 8 - pad;
736 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
737 inst->data,
738 uint8,
739 inst->NameLength + pad);
740 memset(inst->data + inst->NameLength, 0, pad);
741 inst->ByteLength += pad;
744 return True;
747 /*********************************************************************
748 *********************************************************************/
750 BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
751 prs_struct *ps,
752 int instInd,
753 TDB_CONTEXT *names)
755 PERF_INSTANCE_DEFINITION *inst;
757 if(obj->instances == NULL) {
758 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
759 obj->instances,
760 PERF_INSTANCE_DEFINITION,
761 obj->NumInstances);
763 if(obj->instances == NULL)
764 return False;
766 memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
767 inst = &(obj->instances[instInd]);
768 return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
771 /*********************************************************************
772 *********************************************************************/
774 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
775 prs_struct *ps,
776 int base_index,
777 TDB_CONTEXT *names)
779 BOOL success;
780 int i, j, retval = 0;
781 char keybuf[PERFCOUNT_MAX_LEN];
782 TDB_DATA key, data;
784 for(i = 1; i <= base_index; i++)
786 j = i*2;
787 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
788 data = tdb_fetch(names, key);
789 if(data.dptr != NULL)
791 if(_reg_perfcount_isparent(data))
792 success = _reg_perfcount_add_object(block, ps, j, data, names);
793 else if(_reg_perfcount_ischild(data))
794 success = _reg_perfcount_add_counter(block, ps, j, data, names);
795 else
797 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
798 success = False;
800 if(success == False)
802 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
803 retval = -1;
805 SAFE_FREE(data.dptr);
807 else
808 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
810 return retval;
813 /*********************************************************************
814 *********************************************************************/
816 static BOOL _reg_perfcount_get_64(SMB_BIG_UINT *retval,
817 TDB_CONTEXT *tdb,
818 int key_part1,
819 const char *key_part2)
821 TDB_DATA key, data;
822 char buf[PERFCOUNT_MAX_LEN];
824 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
826 data = tdb_fetch(tdb, key);
827 if(data.dptr == NULL)
829 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
830 return False;
833 memset(buf, 0, PERFCOUNT_MAX_LEN);
834 memcpy(buf, data.dptr, data.dsize);
835 SAFE_FREE(data.dptr);
837 *retval = atof(buf);
839 return True;
842 /*********************************************************************
843 *********************************************************************/
845 static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
846 TDB_CONTEXT *names)
848 SMB_BIG_UINT PerfFreq, PerfTime, PerfTime100nSec;
849 TDB_CONTEXT *counters;
850 BOOL status = False;
851 const char *fname = counters_directory( DATA_DB );
853 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
855 if(counters == NULL)
857 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
858 return False;
861 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
862 if(status == False)
864 tdb_close(counters);
865 return status;
867 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
869 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
870 if(status == False)
872 tdb_close(counters);
873 return status;
875 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
877 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
878 if(status == False)
880 tdb_close(counters);
881 return status;
883 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
885 tdb_close(counters);
886 return True;
889 /*********************************************************************
890 *********************************************************************/
892 static BOOL _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block,
893 prs_struct *ps, TDB_CONTEXT *names)
895 wpstring temp;
896 time_t tm;
898 memset(temp, 0, sizeof(temp));
899 rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE);
900 memcpy(block->Signature, temp, strlen_w(temp) *2);
902 if(ps->bigendian_data == RPC_BIG_ENDIAN)
903 block->LittleEndian = 0;
904 else
905 block->LittleEndian = 1;
906 block->Version = 1;
907 block->Revision = 1;
908 block->TotalByteLength = 0;
909 block->NumObjectTypes = 0;
910 block->DefaultObject = -1;
911 block->objects = NULL;
912 tm = time(NULL);
913 make_systemtime(&(block->SystemTime), gmtime(&tm));
914 _reg_perfcount_init_data_block_perf(block, names);
915 memset(temp, 0, sizeof(temp));
916 rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
917 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
918 block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
919 if (block->data == NULL) {
920 return False;
922 memcpy(block->data, temp, block->SystemNameLength);
923 block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
924 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
925 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
926 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
927 block->HeaderLength += 8 - (block->HeaderLength % 8);
929 return True;
932 /*********************************************************************
933 *********************************************************************/
935 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
937 int obj, cnt, inst, pad, i;
938 PERF_OBJECT_TYPE *object;
939 PERF_INSTANCE_DEFINITION *instance;
940 PERF_COUNTER_DEFINITION *counter;
941 PERF_COUNTER_BLOCK *counter_data;
942 char *temp = NULL, *src_addr, *dst_addr;
944 block->TotalByteLength = 0;
945 object = block->objects;
946 for(obj = 0; obj < block->NumObjectTypes; obj++)
948 object[obj].TotalByteLength = 0;
949 object[obj].DefinitionLength = 0;
950 instance = object[obj].instances;
951 counter = object[obj].counters;
952 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
954 object[obj].TotalByteLength += counter[cnt].ByteLength;
955 object[obj].DefinitionLength += counter[cnt].ByteLength;
957 if(object[obj].NumInstances != PERF_NO_INSTANCES)
959 for(inst = 0; inst < object[obj].NumInstances; inst++)
961 instance = &(object[obj].instances[inst]);
962 object[obj].TotalByteLength += instance->ByteLength;
963 counter_data = &(instance->counter_data);
964 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
965 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
966 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
967 temp,
968 char,
969 counter_data->ByteLength- sizeof(counter_data->ByteLength));
970 if (temp == NULL) {
971 return 0;
973 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
974 src_addr = (char *)counter_data->data;
975 for(i = 0; i < object[obj].NumCounters; i++)
977 counter = &(object[obj].counters[i]);
978 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
979 memcpy(dst_addr, src_addr, counter->CounterSize);
980 src_addr += counter->CounterSize;
982 /* Make sure to be 64-bit aligned */
983 if((pad = (counter_data->ByteLength % 8)))
985 pad = 8 - pad;
987 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
988 counter_data->data,
989 uint8,
990 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
991 if (counter_data->data == NULL) {
992 return 0;
994 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
995 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
996 counter_data->ByteLength += pad;
997 object[obj].TotalByteLength += counter_data->ByteLength;
1000 else
1002 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1003 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1004 if((pad = (object[obj].counter_data.ByteLength % 8)))
1006 pad = 8 - pad;
1007 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
1008 object[obj].counter_data.data,
1009 uint8,
1010 object[obj].counter_data.ByteLength + pad);
1011 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1012 object[obj].counter_data.ByteLength += pad;
1014 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1016 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
1017 object[obj].TotalByteLength += object[obj].HeaderLength;
1018 object[obj].DefinitionLength += object[obj].HeaderLength;
1020 block->TotalByteLength += object[obj].TotalByteLength;
1023 return block->TotalByteLength;
1026 /*********************************************************************
1027 *********************************************************************/
1029 uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
1030 prs_struct *ps,
1031 PERF_DATA_BLOCK *block,
1032 char *object_ids)
1034 uint32 buffer_size = 0;
1035 const char *fname = counters_directory( NAMES_DB );
1036 TDB_CONTEXT *names;
1037 int retval = 0;
1039 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1041 if(names == NULL)
1043 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1044 return 0;
1047 if (!_reg_perfcount_init_data_block(block, ps, names)) {
1048 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1049 tdb_close(names);
1050 return 0;
1053 reg_perfcount_get_last_counter(base_index);
1055 if(object_ids == NULL)
1057 /* we're getting a request for "Global" here */
1058 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1060 else
1062 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1063 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1065 buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
1067 tdb_close(names);
1069 if (retval == -1) {
1070 return 0;
1073 return buffer_size + block->HeaderLength;
1076 /*********************************************************************
1077 *********************************************************************/
1079 static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1081 int i;
1082 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1083 depth++;
1085 if(!prs_align(ps))
1086 return False;
1087 for(i = 0; i < 4; i++)
1089 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1090 return False;
1092 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1093 return False;
1094 if(!prs_uint32("Version", ps, depth, &block.Version))
1095 return False;
1096 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1097 return False;
1098 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1099 return False;
1100 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1101 return False;
1102 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1103 return False;
1104 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1105 return False;
1106 if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1107 return False;
1108 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1109 return False;
1110 if(!prs_align_uint64(ps))
1111 return False;
1112 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1113 return False;
1114 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1115 return False;
1116 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1117 return False;
1118 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1119 return False;
1120 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1121 return False;
1122 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1123 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1124 block.HeaderLength - block.SystemNameOffset))
1125 return False;
1127 return True;
1130 /*********************************************************************
1131 *********************************************************************/
1133 static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1134 PERF_OBJECT_TYPE object,
1135 int depth)
1137 int cnt;
1138 PERF_COUNTER_DEFINITION counter;
1140 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1141 depth++;
1143 for(cnt = 0; cnt < object.NumCounters; cnt++)
1145 counter = object.counters[cnt];
1147 if(!prs_align(ps))
1148 return False;
1149 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1150 return False;
1151 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1152 return False;
1153 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1154 return False;
1155 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1156 return False;
1157 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1158 return False;
1159 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1160 return False;
1161 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1162 return False;
1163 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1164 return False;
1165 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1166 return False;
1167 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1168 return False;
1171 return True;
1174 /*********************************************************************
1175 *********************************************************************/
1177 static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1178 PERF_COUNTER_BLOCK counter_data,
1179 int depth)
1181 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1182 depth++;
1184 if(!prs_align_uint64(ps))
1185 return False;
1187 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1188 return False;
1189 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1190 return False;
1191 if(!prs_align_uint64(ps))
1192 return False;
1194 return True;
1197 /*********************************************************************
1198 *********************************************************************/
1200 static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1201 PERF_OBJECT_TYPE object,
1202 int depth)
1204 PERF_INSTANCE_DEFINITION instance;
1205 int inst;
1207 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1208 depth++;
1210 for(inst = 0; inst < object.NumInstances; inst++)
1212 instance = object.instances[inst];
1214 if(!prs_align(ps))
1215 return False;
1216 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1217 return False;
1218 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1219 return False;
1220 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1221 return False;
1222 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1223 return False;
1224 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1225 return False;
1226 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1227 return False;
1228 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1229 instance.ByteLength - instance.NameOffset))
1230 return False;
1231 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1232 return False;
1235 return True;
1238 /*********************************************************************
1239 *********************************************************************/
1241 static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1243 int obj;
1245 PERF_OBJECT_TYPE object;
1247 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1248 depth++;
1250 for(obj = 0; obj < block.NumObjectTypes; obj++)
1252 object = block.objects[obj];
1254 if(!prs_align(ps))
1255 return False;
1257 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1258 return False;
1259 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1260 return False;
1261 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1262 return False;
1263 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1264 return False;
1265 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1266 return False;
1267 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1268 return False;
1269 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1270 return False;
1271 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1272 return False;
1273 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1274 return False;
1275 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1276 return False;
1277 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1278 return False;
1279 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1280 return False;
1281 if(!prs_align_uint64(ps))
1282 return False;
1283 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1284 return False;
1285 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1286 return False;
1288 /* Now do the counters */
1289 /* If no instances, encode counter_data */
1290 /* If instances, encode instace plus counter data for each instance */
1291 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1292 return False;
1293 if(object.NumInstances == PERF_NO_INSTANCES)
1295 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1296 return False;
1298 else
1300 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1301 return False;
1305 return True;
1308 /*********************************************************************
1309 *********************************************************************/
1311 static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
1313 int depth = 0;
1314 if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1316 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1317 return True;
1319 return False;
1322 /*********************************************************************
1323 *********************************************************************/
1325 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, char *object_ids)
1328 * For a detailed description of the layout of this structure,
1329 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1331 PERF_DATA_BLOCK block;
1332 uint32 buffer_size, base_index;
1334 buffer_size = 0;
1335 base_index = reg_perfcount_get_base_index();
1336 ZERO_STRUCT(block);
1338 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1340 if(buffer_size < max_buf_size)
1342 *outbuf_len = buffer_size;
1343 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1344 return WERR_OK;
1345 else
1346 return WERR_NOMEM;
1348 else
1350 *outbuf_len = max_buf_size;
1351 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1352 return WERR_INSUFFICIENT_BUFFER;