2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
5 * Copyright (C) Marcin Krzysztof Porwit 2005,
6 * Copyright (C) Gerald (Jerry) Carter 2005.
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.
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, see <http://www.gnu.org/licenses/>.
25 #define DBGC_CLASS DBGC_REGISTRY
27 #define PERFCOUNT_MAX_LEN 256
29 #define PERFCOUNTDIR "perfmon"
30 #define NAMES_DB "names.tdb"
31 #define DATA_DB "data.tdb"
33 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind);
35 /*********************************************************************
36 *********************************************************************/
38 static char* counters_directory( const char *dbname )
46 fstr_sprintf( path, "%s/%s", PERFCOUNTDIR, dbname );
48 pstrcpy( fname, lock_path( path ) );
50 return talloc_strdup(talloc_tos(), fname);
53 /*********************************************************************
54 *********************************************************************/
56 void perfcount_init_keys( void )
58 char *p = lock_path(PERFCOUNTDIR);
60 /* no registry keys; just create the perfmon directory */
62 if ( !directory_exist( p, NULL ) )
68 /*********************************************************************
69 *********************************************************************/
71 uint32 reg_perfcount_get_base_index(void)
73 const char *fname = counters_directory( NAMES_DB );
78 char buf[PERFCOUNT_MAX_LEN];
80 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
83 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
86 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
87 where the total number of counters is stored. We're assuming no holes in the
89 The format for the counter_names.tdb file is:
96 even_num perf_counter<even_num>
97 even_num+1 perf_counter<even_num>_help
99 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
100 kbuf = string_tdb_data(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));
111 memset(buf, 0, PERFCOUNT_MAX_LEN);
112 memcpy(buf, dbuf.dptr, dbuf.dsize);
113 retval = (uint32)atoi(buf);
114 SAFE_FREE(dbuf.dptr);
120 /*********************************************************************
121 *********************************************************************/
123 uint32 reg_perfcount_get_last_counter(uint32 base_index)
130 retval = base_index * 2;
135 /*********************************************************************
136 *********************************************************************/
138 uint32 reg_perfcount_get_last_help(uint32 last_counter)
142 if(last_counter == 0)
145 retval = last_counter + 1;
151 /*********************************************************************
152 *********************************************************************/
154 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
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 = string_tdb_data(temp);
168 dbuf = tdb_fetch(tdb, kbuf);
169 if(dbuf.dptr == NULL)
171 /* If a key isn't there, just bypass it -- this really shouldn't
172 happen unless someone's mucking around with the tdb */
173 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
174 temp, tdb_name(tdb)));
177 /* First encode the name_index */
178 working_size = (kbuf.dsize + 1)*sizeof(uint16);
179 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
184 init_unistr2(&name_index, (const char *)kbuf.dptr, UNI_STR_TERMINATE);
185 memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
186 buffer_size += working_size;
187 /* Now encode the actual name */
188 working_size = (dbuf.dsize + 1)*sizeof(uint16);
189 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
194 memset(temp, 0, sizeof(temp));
195 memcpy(temp, dbuf.dptr, dbuf.dsize);
196 SAFE_FREE(dbuf.dptr);
197 init_unistr2(&name, temp, UNI_STR_TERMINATE);
198 memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
199 buffer_size += working_size;
206 /*********************************************************************
207 *********************************************************************/
209 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
212 uint32 buffer_size = 0;
214 const char *fname = counters_directory( NAMES_DB );
220 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
224 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
228 for(i = 1; i <= base_index; i++)
230 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
234 /* Now terminate the MULTI_SZ with a double unicode NULL */
236 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
240 buf1[buffer_size++] = '\0';
241 buf1[buffer_size++] = '\0';
249 /*********************************************************************
250 *********************************************************************/
252 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
255 uint32 buffer_size = 0;
257 const char *fname = counters_directory( NAMES_DB );
263 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
267 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
271 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
273 for(i = 1; i <= base_index; i++)
275 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
279 /* Now terminate the MULTI_SZ with a double unicode NULL */
281 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
285 buf1[buffer_size++] = '\0';
286 buf1[buffer_size++] = '\0';
294 /*********************************************************************
295 *********************************************************************/
297 static void _reg_perfcount_make_key(TDB_DATA *key,
301 const char *key_part2)
303 memset(buf, 0, buflen);
304 if(key_part2 != NULL)
305 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
307 snprintf(buf, buflen, "%d", key_part1);
309 *key = string_tdb_data(buf);
314 /*********************************************************************
315 *********************************************************************/
317 static bool _reg_perfcount_isparent(TDB_DATA data)
321 if(data.dptr[0] == 'p')
329 /*********************************************************************
330 *********************************************************************/
332 static bool _reg_perfcount_ischild(TDB_DATA data)
336 if(data.dptr[0] == 'c')
344 /*********************************************************************
345 *********************************************************************/
347 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
350 char buf[PERFCOUNT_MAX_LEN];
352 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
353 data = tdb_fetch(names, key);
355 if(data.dptr == NULL)
356 return (uint32)PERF_NO_INSTANCES;
358 memset(buf, 0, PERFCOUNT_MAX_LEN);
359 memcpy(buf, data.dptr, data.dsize);
360 SAFE_FREE(data.dptr);
361 return (uint32)atoi(buf);
364 /*********************************************************************
365 *********************************************************************/
367 static bool _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
375 PERF_OBJECT_TYPE *obj;
377 block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
380 block->NumObjectTypes+1);
381 if(block->objects == NULL)
383 obj = &(block->objects[block->NumObjectTypes]);
384 memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(PERF_OBJECT_TYPE));
385 block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
386 block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
387 block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
388 block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
389 block->objects[block->NumObjectTypes].NumCounters = 0;
390 block->objects[block->NumObjectTypes].DefaultCounter = 0;
391 block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
392 block->objects[block->NumObjectTypes].counters = NULL;
393 block->objects[block->NumObjectTypes].instances = NULL;
394 block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
395 block->objects[block->NumObjectTypes].counter_data.data = NULL;
396 block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
397 block->NumObjectTypes+=1;
399 for(i = 0; i < (int)obj->NumInstances; i++) {
400 success = _reg_perfcount_add_instance(obj, ps, i, names);
406 /*********************************************************************
407 *********************************************************************/
409 bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
411 TDB_CONTEXT *counters;
412 const char *fname = counters_directory( DATA_DB );
414 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
418 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
422 *data = tdb_fetch(counters, key);
429 /*********************************************************************
430 *********************************************************************/
432 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
436 retval = CounterType;
438 /* First mask out reserved lower 8 bits */
439 retval = retval & 0xFFFFFF00;
440 retval = retval << 22;
441 retval = retval >> 22;
446 /*********************************************************************
447 *********************************************************************/
449 static uint32 _reg_perfcount_compute_scale(SMB_BIG_INT data)
465 return (uint32)scale;
468 /*********************************************************************
469 *********************************************************************/
471 static bool _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
474 PERF_OBJECT_TYPE *obj,
478 char buf[PERFCOUNT_MAX_LEN];
479 size_t dsize, padding;
480 long int data32, dbuf[2];
484 obj->counters[obj->NumCounters].DefaultScale = 0;
485 dbuf[0] = dbuf[1] = 0;
488 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
489 data = tdb_fetch(names, key);
490 if(data.dptr == NULL)
492 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
495 memset(buf, 0, PERFCOUNT_MAX_LEN);
496 memcpy(buf, data.dptr, data.dsize);
497 obj->counters[obj->NumCounters].CounterType = atoi(buf);
498 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
499 obj->counters[obj->NumCounters].CounterType, CounterIndex));
500 SAFE_FREE(data.dptr);
502 /* Fetch the actual data */
503 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
504 _reg_perfcount_get_counter_data(key, &data);
505 if(data.dptr == NULL)
507 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
511 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
513 if(counter_size == PERF_SIZE_DWORD)
515 dsize = sizeof(data32);
516 memset(buf, 0, PERFCOUNT_MAX_LEN);
517 memcpy(buf, data.dptr, data.dsize);
518 data32 = strtol(buf, NULL, 0);
519 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
520 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((SMB_BIG_INT)data32);
522 obj->counters[obj->NumCounters].DefaultScale = 0;
524 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
526 else if(counter_size == PERF_SIZE_LARGE)
528 dsize = sizeof(data64);
529 memset(buf, 0, PERFCOUNT_MAX_LEN);
530 memcpy(buf, data.dptr, data.dsize);
532 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
533 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
535 obj->counters[obj->NumCounters].DefaultScale = 0;
536 memcpy((void *)dbuf, (const void *)&data64, dsize);
537 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
539 else /* PERF_SIZE_VARIABLE_LEN */
542 memset(buf, 0, PERFCOUNT_MAX_LEN);
543 memcpy(buf, data.dptr, data.dsize);
545 SAFE_FREE(data.dptr);
547 obj->counter_data.ByteLength += dsize + padding;
548 obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
549 obj->counter_data.data,
551 obj->counter_data.ByteLength - sizeof(uint32));
552 if(obj->counter_data.data == NULL)
554 if(dbuf[0] != 0 || dbuf[1] != 0)
556 memcpy((void *)(obj->counter_data.data +
557 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
558 (const void *)dbuf, dsize);
562 /* Handling PERF_SIZE_VARIABLE_LEN */
563 memcpy((void *)(obj->counter_data.data +
564 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
565 (const void *)buf, dsize);
567 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
568 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
570 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
572 obj->counters[obj->NumCounters].CounterSize = dsize;
577 /*********************************************************************
578 *********************************************************************/
580 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
584 PERF_OBJECT_TYPE *obj = NULL;
586 for(i = 0; i < block->NumObjectTypes; i++)
588 if(block->objects[i].ObjectNameTitleIndex == objind)
590 obj = &(block->objects[i]);
597 /*********************************************************************
598 *********************************************************************/
600 static bool _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
606 char *begin, *end, *start, *stop;
608 PERF_OBJECT_TYPE *obj;
610 char buf[PERFCOUNT_MAX_LEN];
613 memset(buf, 0, PERFCOUNT_MAX_LEN);
614 memcpy(buf, data.dptr, data.dsize);
615 begin = index(buf, '[');
616 end = index(buf, ']');
617 if(begin == NULL || end == NULL)
622 stop = index(start, ',');
626 parent = atoi(start);
628 obj = _reg_perfcount_find_obj(block, parent);
630 /* At this point we require that the parent object exist.
631 This can probably be handled better at some later time */
632 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
636 obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
638 PERF_COUNTER_DEFINITION,
640 if(obj->counters == NULL)
642 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(PERF_COUNTER_DEFINITION));
643 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
644 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
645 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
646 obj->counters[obj->NumCounters].ByteLength = sizeof(PERF_COUNTER_DEFINITION);
647 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
648 obj->NumCounters += 1;
652 /* Handle case of Objects/Counters without any counter data, which would suggest
653 that the required instances are not there yet, so change NumInstances from
654 PERF_NO_INSTANCES to 0 */
659 /*********************************************************************
660 *********************************************************************/
662 bool _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
665 PERF_OBJECT_TYPE *obj,
669 char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
673 /* First grab the instance data from the data file */
674 memset(temp, 0, PERFCOUNT_MAX_LEN);
675 snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
676 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
677 _reg_perfcount_get_counter_data(key, &data);
678 if(data.dptr == NULL)
680 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
684 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
685 inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
686 inst->counter_data.data,
689 if(inst->counter_data.data == NULL)
691 memset(inst->counter_data.data, 0, data.dsize);
692 memcpy(inst->counter_data.data, data.dptr, data.dsize);
693 SAFE_FREE(data.dptr);
695 /* Fetch instance name */
696 memset(temp, 0, PERFCOUNT_MAX_LEN);
697 snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
698 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
699 data = tdb_fetch(names, key);
700 if(data.dptr == NULL)
702 /* Not actually an error, but possibly unintended? -- just logging FYI */
703 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
705 inst->NameLength = 0;
709 memset(buf, 0, PERFCOUNT_MAX_LEN);
710 memcpy(buf, data.dptr, data.dsize);
711 rpcstr_push((void *)name, buf, sizeof(name), STR_TERMINATE);
712 inst->NameLength = (strlen_w(name) * 2) + 2;
713 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
717 if (inst->data == NULL) {
718 SAFE_FREE(data.dptr);
721 memcpy(inst->data, name, inst->NameLength);
722 SAFE_FREE(data.dptr);
725 inst->ParentObjectTitleIndex = 0;
726 inst->ParentObjectTitlePointer = 0;
727 inst->UniqueID = PERF_NO_UNIQUE_ID;
728 inst->NameOffset = 6 * sizeof(uint32);
730 inst->ByteLength = inst->NameOffset + inst->NameLength;
731 /* Need to be aligned on a 64-bit boundary here for counter_data */
732 if((pad = (inst->ByteLength % 8)))
735 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
738 inst->NameLength + pad);
739 memset(inst->data + inst->NameLength, 0, pad);
740 inst->ByteLength += pad;
746 /*********************************************************************
747 *********************************************************************/
749 bool _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
754 PERF_INSTANCE_DEFINITION *inst;
756 if(obj->instances == NULL) {
757 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
759 PERF_INSTANCE_DEFINITION,
762 if(obj->instances == NULL)
765 memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
766 inst = &(obj->instances[instInd]);
767 return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
770 /*********************************************************************
771 *********************************************************************/
773 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
779 int i, j, retval = 0;
780 char keybuf[PERFCOUNT_MAX_LEN];
783 for(i = 1; i <= base_index; i++)
786 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
787 data = tdb_fetch(names, key);
788 if(data.dptr != NULL)
790 if(_reg_perfcount_isparent(data))
791 success = _reg_perfcount_add_object(block, ps, j, data, names);
792 else if(_reg_perfcount_ischild(data))
793 success = _reg_perfcount_add_counter(block, ps, j, data, names);
796 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
801 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
804 SAFE_FREE(data.dptr);
807 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
812 /*********************************************************************
813 *********************************************************************/
815 static bool _reg_perfcount_get_64(SMB_BIG_UINT *retval,
818 const char *key_part2)
821 char buf[PERFCOUNT_MAX_LEN];
823 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
825 data = tdb_fetch(tdb, key);
826 if(data.dptr == NULL)
828 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
832 memset(buf, 0, PERFCOUNT_MAX_LEN);
833 memcpy(buf, data.dptr, data.dsize);
834 SAFE_FREE(data.dptr);
841 /*********************************************************************
842 *********************************************************************/
844 static bool _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
847 SMB_BIG_UINT PerfFreq, PerfTime, PerfTime100nSec;
848 TDB_CONTEXT *counters;
850 const char *fname = counters_directory( DATA_DB );
852 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
856 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
860 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
866 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
868 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
874 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
876 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
882 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
888 /*********************************************************************
889 *********************************************************************/
891 static bool _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block,
892 prs_struct *ps, TDB_CONTEXT *names)
897 memset(temp, 0, sizeof(temp));
898 rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE);
899 memcpy(block->Signature, temp, strlen_w(temp) *2);
901 if(ps->bigendian_data == RPC_BIG_ENDIAN)
902 block->LittleEndian = 0;
904 block->LittleEndian = 1;
907 block->TotalByteLength = 0;
908 block->NumObjectTypes = 0;
909 block->DefaultObject = -1;
910 block->objects = NULL;
912 make_systemtime(&(block->SystemTime), gmtime(&tm));
913 _reg_perfcount_init_data_block_perf(block, names);
914 memset(temp, 0, sizeof(temp));
915 rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
916 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
917 block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
918 if (block->data == NULL) {
921 memcpy(block->data, temp, block->SystemNameLength);
922 block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
923 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
924 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
925 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
926 block->HeaderLength += 8 - (block->HeaderLength % 8);
931 /*********************************************************************
932 *********************************************************************/
934 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
936 int obj, cnt, inst, pad, i;
937 PERF_OBJECT_TYPE *object;
938 PERF_INSTANCE_DEFINITION *instance;
939 PERF_COUNTER_DEFINITION *counter;
940 PERF_COUNTER_BLOCK *counter_data;
941 char *temp = NULL, *src_addr, *dst_addr;
943 block->TotalByteLength = 0;
944 object = block->objects;
945 for(obj = 0; obj < block->NumObjectTypes; obj++)
947 object[obj].TotalByteLength = 0;
948 object[obj].DefinitionLength = 0;
949 instance = object[obj].instances;
950 counter = object[obj].counters;
951 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
953 object[obj].TotalByteLength += counter[cnt].ByteLength;
954 object[obj].DefinitionLength += counter[cnt].ByteLength;
956 if(object[obj].NumInstances != PERF_NO_INSTANCES)
958 for(inst = 0; inst < object[obj].NumInstances; inst++)
960 instance = &(object[obj].instances[inst]);
961 object[obj].TotalByteLength += instance->ByteLength;
962 counter_data = &(instance->counter_data);
963 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
964 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
965 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
968 counter_data->ByteLength- sizeof(counter_data->ByteLength));
972 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
973 src_addr = (char *)counter_data->data;
974 for(i = 0; i < object[obj].NumCounters; i++)
976 counter = &(object[obj].counters[i]);
977 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
978 memcpy(dst_addr, src_addr, counter->CounterSize);
979 src_addr += counter->CounterSize;
981 /* Make sure to be 64-bit aligned */
982 if((pad = (counter_data->ByteLength % 8)))
986 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
989 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
990 if (counter_data->data == NULL) {
993 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
994 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
995 counter_data->ByteLength += pad;
996 object[obj].TotalByteLength += counter_data->ByteLength;
1001 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1002 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1003 if((pad = (object[obj].counter_data.ByteLength % 8)))
1006 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
1007 object[obj].counter_data.data,
1009 object[obj].counter_data.ByteLength + pad);
1010 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1011 object[obj].counter_data.ByteLength += pad;
1013 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1015 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
1016 object[obj].TotalByteLength += object[obj].HeaderLength;
1017 object[obj].DefinitionLength += object[obj].HeaderLength;
1019 block->TotalByteLength += object[obj].TotalByteLength;
1022 return block->TotalByteLength;
1025 /*********************************************************************
1026 *********************************************************************/
1028 uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
1030 PERF_DATA_BLOCK *block,
1031 const char *object_ids)
1033 uint32 buffer_size = 0;
1034 const char *fname = counters_directory( NAMES_DB );
1038 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1042 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1046 if (!_reg_perfcount_init_data_block(block, ps, names)) {
1047 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1052 reg_perfcount_get_last_counter(base_index);
1054 if(object_ids == NULL)
1056 /* we're getting a request for "Global" here */
1057 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1061 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1062 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1064 buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
1072 return buffer_size + block->HeaderLength;
1075 /*********************************************************************
1076 *********************************************************************/
1078 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1081 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1086 for(i = 0; i < 4; i++)
1088 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1091 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1093 if(!prs_uint32("Version", ps, depth, &block.Version))
1095 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1097 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1099 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1101 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1103 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1105 if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1107 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1109 if(!prs_align_uint64(ps))
1111 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1113 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1115 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1117 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1119 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1121 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1122 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1123 block.HeaderLength - block.SystemNameOffset))
1129 /*********************************************************************
1130 *********************************************************************/
1132 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1133 PERF_OBJECT_TYPE object,
1137 PERF_COUNTER_DEFINITION counter;
1139 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1142 for(cnt = 0; cnt < object.NumCounters; cnt++)
1144 counter = object.counters[cnt];
1148 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1150 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1152 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1154 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1156 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1158 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1160 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1162 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1164 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1166 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1173 /*********************************************************************
1174 *********************************************************************/
1176 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1177 PERF_COUNTER_BLOCK counter_data,
1180 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1183 if(!prs_align_uint64(ps))
1186 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1188 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1190 if(!prs_align_uint64(ps))
1196 /*********************************************************************
1197 *********************************************************************/
1199 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1200 PERF_OBJECT_TYPE object,
1203 PERF_INSTANCE_DEFINITION instance;
1206 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1209 for(inst = 0; inst < object.NumInstances; inst++)
1211 instance = object.instances[inst];
1215 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1217 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1219 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1221 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1223 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1225 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1227 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1228 instance.ByteLength - instance.NameOffset))
1230 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1237 /*********************************************************************
1238 *********************************************************************/
1240 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1244 PERF_OBJECT_TYPE object;
1246 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1249 for(obj = 0; obj < block.NumObjectTypes; obj++)
1251 object = block.objects[obj];
1256 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1258 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1260 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1262 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1264 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1266 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1268 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1270 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1272 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1274 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1276 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1278 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1280 if(!prs_align_uint64(ps))
1282 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1284 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1287 /* Now do the counters */
1288 /* If no instances, encode counter_data */
1289 /* If instances, encode instace plus counter data for each instance */
1290 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1292 if(object.NumInstances == PERF_NO_INSTANCES)
1294 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1299 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1307 /*********************************************************************
1308 *********************************************************************/
1310 static bool _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
1313 if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1315 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1321 /*********************************************************************
1322 *********************************************************************/
1324 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
1327 * For a detailed description of the layout of this structure,
1328 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1330 * By 2006-11-23 this link did not work anymore, I found something
1332 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1334 PERF_DATA_BLOCK block;
1335 uint32 buffer_size, base_index;
1338 base_index = reg_perfcount_get_base_index();
1341 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1343 if(buffer_size < max_buf_size)
1345 *outbuf_len = buffer_size;
1346 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1353 *outbuf_len = max_buf_size;
1354 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1355 return WERR_INSUFFICIENT_BUFFER;