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/>.
23 #include "../librpc/gen_ndr/perfcount.h"
25 #include "reg_perfcount.h"
28 #define DBGC_CLASS DBGC_REGISTRY
30 #define PERFCOUNT_MAX_LEN 256
32 #define PERFCOUNTDIR "perfmon"
33 #define NAMES_DB "names.tdb"
34 #define DATA_DB "data.tdb"
36 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind);
38 /*********************************************************************
39 *********************************************************************/
41 static char *counters_directory(const char *dbname)
45 TALLOC_CTX *ctx = talloc_tos();
47 path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
52 ret = talloc_strdup(ctx, state_path(path));
57 /*********************************************************************
58 *********************************************************************/
60 void perfcount_init_keys( void )
62 char *p = state_path(PERFCOUNTDIR);
64 /* no registry keys; just create the perfmon directory */
66 if ( !directory_exist( p ) )
72 /*********************************************************************
73 *********************************************************************/
75 uint32 reg_perfcount_get_base_index(void)
77 const char *fname = counters_directory( NAMES_DB );
82 char buf[PERFCOUNT_MAX_LEN];
84 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
87 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
90 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
91 where the total number of counters is stored. We're assuming no holes in the
93 The format for the counter_names.tdb file is:
100 even_num perf_counter<even_num>
101 even_num+1 perf_counter<even_num>_help
103 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
104 kbuf = string_tdb_data(key);
105 dbuf = tdb_fetch(names, kbuf);
106 if(dbuf.dptr == NULL)
108 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
115 memset(buf, 0, PERFCOUNT_MAX_LEN);
116 memcpy(buf, dbuf.dptr, dbuf.dsize);
117 retval = (uint32)atoi(buf);
118 SAFE_FREE(dbuf.dptr);
124 /*********************************************************************
125 *********************************************************************/
127 uint32 reg_perfcount_get_last_counter(uint32 base_index)
134 retval = base_index * 2;
139 /*********************************************************************
140 *********************************************************************/
142 uint32 reg_perfcount_get_last_help(uint32 last_counter)
146 if(last_counter == 0)
149 retval = last_counter + 1;
155 /*********************************************************************
156 *********************************************************************/
158 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
165 char *buf1 = *retbuf;
166 uint32 working_size = 0;
167 DATA_BLOB name_index, name;
169 memset(temp, 0, sizeof(temp));
170 snprintf(temp, sizeof(temp), "%d", keyval);
171 kbuf = string_tdb_data(temp);
172 dbuf = tdb_fetch(tdb, kbuf);
173 if(dbuf.dptr == NULL)
175 /* If a key isn't there, just bypass it -- this really shouldn't
176 happen unless someone's mucking around with the tdb */
177 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
178 temp, tdb_name(tdb)));
181 /* First encode the name_index */
182 working_size = (kbuf.dsize + 1)*sizeof(uint16);
183 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
188 push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
189 memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
190 buffer_size += working_size;
191 /* Now encode the actual name */
192 working_size = (dbuf.dsize + 1)*sizeof(uint16);
193 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
198 memset(temp, 0, sizeof(temp));
199 memcpy(temp, dbuf.dptr, dbuf.dsize);
200 SAFE_FREE(dbuf.dptr);
201 push_reg_sz(talloc_tos(), &name, temp);
202 memcpy(buf1+buffer_size, (char *)name.data, working_size);
203 buffer_size += working_size;
210 /*********************************************************************
211 *********************************************************************/
213 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
216 uint32 buffer_size = 0;
218 const char *fname = counters_directory( NAMES_DB );
224 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
228 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
232 for(i = 1; i <= base_index; i++)
234 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
238 /* Now terminate the MULTI_SZ with a double unicode NULL */
240 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
244 buf1[buffer_size++] = '\0';
245 buf1[buffer_size++] = '\0';
253 /*********************************************************************
254 *********************************************************************/
256 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
259 uint32 buffer_size = 0;
261 const char *fname = counters_directory( NAMES_DB );
267 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
271 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
275 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
277 for(i = 1; i <= base_index; i++)
279 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
283 /* Now terminate the MULTI_SZ with a double unicode NULL */
285 buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
289 buf1[buffer_size++] = '\0';
290 buf1[buffer_size++] = '\0';
298 /*********************************************************************
299 *********************************************************************/
301 static void _reg_perfcount_make_key(TDB_DATA *key,
305 const char *key_part2)
307 memset(buf, 0, buflen);
308 if(key_part2 != NULL)
309 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
311 snprintf(buf, buflen, "%d", key_part1);
313 *key = string_tdb_data(buf);
318 /*********************************************************************
319 *********************************************************************/
321 static bool _reg_perfcount_isparent(TDB_DATA data)
325 if(data.dptr[0] == 'p')
333 /*********************************************************************
334 *********************************************************************/
336 static bool _reg_perfcount_ischild(TDB_DATA data)
340 if(data.dptr[0] == 'c')
348 /*********************************************************************
349 *********************************************************************/
351 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
354 char buf[PERFCOUNT_MAX_LEN];
356 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
357 data = tdb_fetch(names, key);
359 if(data.dptr == NULL)
360 return (uint32)PERF_NO_INSTANCES;
362 memset(buf, 0, PERFCOUNT_MAX_LEN);
363 memcpy(buf, data.dptr, data.dsize);
364 SAFE_FREE(data.dptr);
365 return (uint32)atoi(buf);
368 /*********************************************************************
369 *********************************************************************/
371 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
376 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK *block,
384 struct PERF_OBJECT_TYPE *obj;
386 block->objects = (struct PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(mem_ctx,
388 struct PERF_OBJECT_TYPE,
389 block->NumObjectTypes+1);
390 if(block->objects == NULL)
392 obj = &(block->objects[block->NumObjectTypes]);
393 memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(struct PERF_OBJECT_TYPE));
394 block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
395 block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
396 block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
397 block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
398 block->objects[block->NumObjectTypes].NumCounters = 0;
399 block->objects[block->NumObjectTypes].DefaultCounter = 0;
400 block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
401 block->objects[block->NumObjectTypes].counters = NULL;
402 block->objects[block->NumObjectTypes].instances = NULL;
403 block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
404 block->objects[block->NumObjectTypes].counter_data.data = NULL;
405 block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
406 block->NumObjectTypes+=1;
408 for(i = 0; i < (int)obj->NumInstances; i++) {
409 success = _reg_perfcount_add_instance(obj, mem_ctx, i, names);
415 /*********************************************************************
416 *********************************************************************/
418 static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
420 TDB_CONTEXT *counters;
421 const char *fname = counters_directory( DATA_DB );
423 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
427 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
431 *data = tdb_fetch(counters, key);
438 /*********************************************************************
439 *********************************************************************/
441 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
445 retval = CounterType;
447 /* First mask out reserved lower 8 bits */
448 retval = retval & 0xFFFFFF00;
449 retval = retval << 22;
450 retval = retval >> 22;
455 /*********************************************************************
456 *********************************************************************/
458 static uint32 _reg_perfcount_compute_scale(int64_t data)
474 return (uint32)scale;
477 /*********************************************************************
478 *********************************************************************/
480 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK *block,
483 struct PERF_OBJECT_TYPE *obj,
487 char buf[PERFCOUNT_MAX_LEN];
488 size_t dsize, padding;
489 long int data32, dbuf[2];
493 obj->counters[obj->NumCounters].DefaultScale = 0;
494 dbuf[0] = dbuf[1] = 0;
497 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
498 data = tdb_fetch(names, key);
499 if(data.dptr == NULL)
501 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
504 memset(buf, 0, PERFCOUNT_MAX_LEN);
505 memcpy(buf, data.dptr, data.dsize);
506 obj->counters[obj->NumCounters].CounterType = atoi(buf);
507 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
508 obj->counters[obj->NumCounters].CounterType, CounterIndex));
509 SAFE_FREE(data.dptr);
511 /* Fetch the actual data */
512 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
513 _reg_perfcount_get_counter_data(key, &data);
514 if(data.dptr == NULL)
516 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
520 counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
522 if(counter_size == PERF_SIZE_DWORD)
524 dsize = sizeof(data32);
525 memset(buf, 0, PERFCOUNT_MAX_LEN);
526 memcpy(buf, data.dptr, data.dsize);
527 data32 = strtol(buf, NULL, 0);
528 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
529 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
531 obj->counters[obj->NumCounters].DefaultScale = 0;
533 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
535 else if(counter_size == PERF_SIZE_LARGE)
537 dsize = sizeof(data64);
538 memset(buf, 0, PERFCOUNT_MAX_LEN);
539 memcpy(buf, data.dptr, data.dsize);
541 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
542 obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
544 obj->counters[obj->NumCounters].DefaultScale = 0;
545 memcpy((void *)dbuf, (const void *)&data64, dsize);
546 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
548 else /* PERF_SIZE_VARIABLE_LEN */
551 memset(buf, 0, PERFCOUNT_MAX_LEN);
552 memcpy(buf, data.dptr, data.dsize);
554 SAFE_FREE(data.dptr);
556 obj->counter_data.ByteLength += dsize + padding;
557 obj->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
558 obj->counter_data.data,
560 obj->counter_data.ByteLength - sizeof(uint32));
561 if(obj->counter_data.data == NULL)
563 if(dbuf[0] != 0 || dbuf[1] != 0)
565 memcpy((void *)(obj->counter_data.data +
566 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
567 (const void *)dbuf, dsize);
571 /* Handling PERF_SIZE_VARIABLE_LEN */
572 memcpy((void *)(obj->counter_data.data +
573 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
574 (const void *)buf, dsize);
576 obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
577 if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
579 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
581 obj->counters[obj->NumCounters].CounterSize = dsize;
586 /*********************************************************************
587 *********************************************************************/
589 struct PERF_OBJECT_TYPE *_reg_perfcount_find_obj(struct PERF_DATA_BLOCK *block, int objind)
593 struct PERF_OBJECT_TYPE *obj = NULL;
595 for(i = 0; i < block->NumObjectTypes; i++)
597 if(block->objects[i].ObjectNameTitleIndex == objind)
599 obj = &(block->objects[i]);
606 /*********************************************************************
607 *********************************************************************/
609 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK *block,
615 char *begin, *end, *start, *stop;
617 struct PERF_OBJECT_TYPE *obj;
619 char buf[PERFCOUNT_MAX_LEN];
622 memset(buf, 0, PERFCOUNT_MAX_LEN);
623 memcpy(buf, data.dptr, data.dsize);
624 begin = strchr(buf, '[');
625 end = strchr(buf, ']');
626 if(begin == NULL || end == NULL)
631 stop = strchr(start, ',');
635 parent = atoi(start);
637 obj = _reg_perfcount_find_obj(block, parent);
639 /* At this point we require that the parent object exist.
640 This can probably be handled better at some later time */
641 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
645 obj->counters = (struct PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(mem_ctx,
647 struct PERF_COUNTER_DEFINITION,
649 if(obj->counters == NULL)
651 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(struct PERF_COUNTER_DEFINITION));
652 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
653 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
654 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
655 obj->counters[obj->NumCounters].ByteLength = sizeof(struct PERF_COUNTER_DEFINITION);
656 success = _reg_perfcount_get_counter_info(block, mem_ctx, num, obj, names);
657 obj->NumCounters += 1;
661 /* Handle case of Objects/Counters without any counter data, which would suggest
662 that the required instances are not there yet, so change NumInstances from
663 PERF_NO_INSTANCES to 0 */
668 /*********************************************************************
669 *********************************************************************/
671 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION *inst,
674 struct PERF_OBJECT_TYPE *obj,
678 char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
679 smb_ucs2_t *name = NULL;
682 /* First grab the instance data from the data file */
683 memset(temp, 0, PERFCOUNT_MAX_LEN);
684 snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
685 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
686 if (!_reg_perfcount_get_counter_data(key, &data)) {
687 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
690 if(data.dptr == NULL)
692 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
696 inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
697 inst->counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
698 inst->counter_data.data,
701 if(inst->counter_data.data == NULL)
703 memset(inst->counter_data.data, 0, data.dsize);
704 memcpy(inst->counter_data.data, data.dptr, data.dsize);
705 SAFE_FREE(data.dptr);
707 /* Fetch instance name */
708 memset(temp, 0, PERFCOUNT_MAX_LEN);
709 snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
710 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
711 data = tdb_fetch(names, key);
712 if(data.dptr == NULL)
714 /* Not actually an error, but possibly unintended? -- just logging FYI */
715 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
717 inst->NameLength = 0;
721 memset(buf, 0, PERFCOUNT_MAX_LEN);
722 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
723 buf[PERFCOUNT_MAX_LEN-1] = '\0';
724 inst->NameLength = rpcstr_push_talloc(mem_ctx, &name, buf);
725 if (inst->NameLength == (uint32_t)-1 || !name) {
726 SAFE_FREE(data.dptr);
729 inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
733 if (inst->data == NULL) {
734 SAFE_FREE(data.dptr);
737 memcpy(inst->data, name, inst->NameLength);
738 SAFE_FREE(data.dptr);
741 inst->ParentObjectTitleIndex = 0;
742 inst->ParentObjectTitlePointer = 0;
743 inst->UniqueID = PERF_NO_UNIQUE_ID;
744 inst->NameOffset = 6 * sizeof(uint32);
746 inst->ByteLength = inst->NameOffset + inst->NameLength;
747 /* Need to be aligned on a 64-bit boundary here for counter_data */
748 if((pad = (inst->ByteLength % 8)))
751 inst->data = TALLOC_REALLOC_ARRAY(mem_ctx,
754 inst->NameLength + pad);
755 memset(inst->data + inst->NameLength, 0, pad);
756 inst->ByteLength += pad;
762 /*********************************************************************
763 *********************************************************************/
765 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE *obj,
770 struct PERF_INSTANCE_DEFINITION *inst;
772 if(obj->instances == NULL) {
773 obj->instances = TALLOC_REALLOC_ARRAY(mem_ctx,
775 struct PERF_INSTANCE_DEFINITION,
778 if(obj->instances == NULL)
781 memset(&(obj->instances[instInd]), 0, sizeof(struct PERF_INSTANCE_DEFINITION));
782 inst = &(obj->instances[instInd]);
783 return _reg_perfcount_get_instance_info(inst, mem_ctx, instInd, obj, names);
786 /*********************************************************************
787 *********************************************************************/
789 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK *block,
795 int i, j, retval = 0;
796 char keybuf[PERFCOUNT_MAX_LEN];
799 for(i = 1; i <= base_index; i++)
802 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
803 data = tdb_fetch(names, key);
804 if(data.dptr != NULL)
806 if(_reg_perfcount_isparent(data))
807 success = _reg_perfcount_add_object(block, mem_ctx, j, data, names);
808 else if(_reg_perfcount_ischild(data))
809 success = _reg_perfcount_add_counter(block, mem_ctx, j, data, names);
812 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
817 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
820 SAFE_FREE(data.dptr);
823 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
828 /*********************************************************************
829 *********************************************************************/
831 static bool _reg_perfcount_get_64(uint64_t *retval,
834 const char *key_part2)
837 char buf[PERFCOUNT_MAX_LEN];
839 _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
841 data = tdb_fetch(tdb, key);
842 if(data.dptr == NULL)
844 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
848 memset(buf, 0, PERFCOUNT_MAX_LEN);
849 memcpy(buf, data.dptr, data.dsize);
850 SAFE_FREE(data.dptr);
857 /*********************************************************************
858 *********************************************************************/
860 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK *block,
863 uint64_t PerfFreq, PerfTime, PerfTime100nSec;
864 TDB_CONTEXT *counters;
866 const char *fname = counters_directory( DATA_DB );
868 counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
872 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
876 status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
882 memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
884 status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
890 memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
892 status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
898 memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
904 /*******************************************************************
905 ********************************************************************/
907 static bool make_systemtime(struct SYSTEMTIME *systime, struct tm *unixtime)
909 systime->year=unixtime->tm_year+1900;
910 systime->month=unixtime->tm_mon+1;
911 systime->dayofweek=unixtime->tm_wday;
912 systime->day=unixtime->tm_mday;
913 systime->hour=unixtime->tm_hour;
914 systime->minute=unixtime->tm_min;
915 systime->second=unixtime->tm_sec;
916 systime->milliseconds=0;
921 /*********************************************************************
922 *********************************************************************/
924 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK *block,
925 TALLOC_CTX *mem_ctx, TDB_CONTEXT *names,
928 smb_ucs2_t *temp = NULL;
931 if (rpcstr_push_talloc(mem_ctx, &temp, "PERF")==(size_t)-1) {
937 memcpy(block->Signature, temp, strlen_w(temp) *2);
940 block->LittleEndian = 0;
942 block->LittleEndian = 1;
945 block->TotalByteLength = 0;
946 block->NumObjectTypes = 0;
947 block->DefaultObject = -1;
948 block->objects = NULL;
950 make_systemtime(&(block->SystemTime), gmtime(&tm));
951 _reg_perfcount_init_data_block_perf(block, names);
952 memset(temp, 0, sizeof(temp));
953 rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
954 block->SystemNameLength = (strlen_w(temp) * 2) + 2;
955 block->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
956 if (block->data == NULL) {
959 memcpy(block->data, temp, block->SystemNameLength);
960 block->SystemNameOffset = sizeof(struct PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data);
961 block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
962 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
963 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
964 block->HeaderLength += 8 - (block->HeaderLength % 8);
969 /*********************************************************************
970 *********************************************************************/
972 static uint32 _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK *block, TALLOC_CTX *mem_ctx)
974 int obj, cnt, inst, pad, i;
975 struct PERF_OBJECT_TYPE *object;
976 struct PERF_INSTANCE_DEFINITION *instance;
977 struct PERF_COUNTER_DEFINITION *counter;
978 struct PERF_COUNTER_BLOCK *counter_data;
979 char *temp = NULL, *src_addr, *dst_addr;
981 block->TotalByteLength = 0;
982 object = block->objects;
983 for(obj = 0; obj < block->NumObjectTypes; obj++)
985 object[obj].TotalByteLength = 0;
986 object[obj].DefinitionLength = 0;
987 instance = object[obj].instances;
988 counter = object[obj].counters;
989 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
991 object[obj].TotalByteLength += counter[cnt].ByteLength;
992 object[obj].DefinitionLength += counter[cnt].ByteLength;
994 if(object[obj].NumInstances != PERF_NO_INSTANCES)
996 for(inst = 0; inst < object[obj].NumInstances; inst++)
998 instance = &(object[obj].instances[inst]);
999 object[obj].TotalByteLength += instance->ByteLength;
1000 counter_data = &(instance->counter_data);
1001 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
1002 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
1003 temp = TALLOC_REALLOC_ARRAY(mem_ctx,
1006 counter_data->ByteLength- sizeof(counter_data->ByteLength));
1010 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1011 src_addr = (char *)counter_data->data;
1012 for(i = 0; i < object[obj].NumCounters; i++)
1014 counter = &(object[obj].counters[i]);
1015 dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
1016 memcpy(dst_addr, src_addr, counter->CounterSize);
1017 src_addr += counter->CounterSize;
1019 /* Make sure to be 64-bit aligned */
1020 if((pad = (counter_data->ByteLength % 8)))
1024 counter_data->data = TALLOC_REALLOC_ARRAY(mem_ctx,
1027 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1028 if (counter_data->data == NULL) {
1031 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1032 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1033 counter_data->ByteLength += pad;
1034 object[obj].TotalByteLength += counter_data->ByteLength;
1039 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1040 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1041 if((pad = (object[obj].counter_data.ByteLength % 8)))
1044 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(mem_ctx,
1045 object[obj].counter_data.data,
1047 object[obj].counter_data.ByteLength + pad);
1048 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1049 object[obj].counter_data.ByteLength += pad;
1051 object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1053 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(struct PERF_COUNTER_BLOCK));
1054 object[obj].TotalByteLength += object[obj].HeaderLength;
1055 object[obj].DefinitionLength += object[obj].HeaderLength;
1057 block->TotalByteLength += object[obj].TotalByteLength;
1060 return block->TotalByteLength;
1063 /*********************************************************************
1064 *********************************************************************/
1066 static uint32 reg_perfcount_get_perf_data_block(uint32 base_index,
1067 TALLOC_CTX *mem_ctx,
1068 struct PERF_DATA_BLOCK *block,
1069 const char *object_ids,
1070 bool bigendian_data)
1072 uint32 buffer_size = 0;
1073 const char *fname = counters_directory( NAMES_DB );
1077 names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1081 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1085 if (!_reg_perfcount_init_data_block(block, mem_ctx, names, bigendian_data)) {
1086 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1091 reg_perfcount_get_last_counter(base_index);
1093 if(object_ids == NULL)
1095 /* we're getting a request for "Global" here */
1096 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1100 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1101 retval = _reg_perfcount_assemble_global(block, mem_ctx, base_index, names);
1103 buffer_size = _reg_perfcount_perf_data_block_fixup(block, mem_ctx);
1111 return buffer_size + block->HeaderLength;
1114 /*******************************************************************
1115 ********************************************************************/
1117 static bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, struct SYSTEMTIME *systime)
1119 if(!prs_uint16("year", ps, depth, &systime->year))
1121 if(!prs_uint16("month", ps, depth, &systime->month))
1123 if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
1125 if(!prs_uint16("day", ps, depth, &systime->day))
1127 if(!prs_uint16("hour", ps, depth, &systime->hour))
1129 if(!prs_uint16("minute", ps, depth, &systime->minute))
1131 if(!prs_uint16("second", ps, depth, &systime->second))
1133 if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
1139 /*********************************************************************
1140 *********************************************************************/
1142 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1145 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1150 for(i = 0; i < 4; i++)
1152 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1155 if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1157 if(!prs_uint32("Version", ps, depth, &block.Version))
1159 if(!prs_uint32("Revision", ps, depth, &block.Revision))
1161 if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1163 if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1165 if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1167 if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1169 if(!smb_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1171 if(!prs_uint32("Padding", ps, depth, &block.Padding))
1173 if(!prs_align_uint64(ps))
1175 if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1177 if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1179 if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1181 if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1183 if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1185 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1186 if(!prs_uint8s(False, "SystemName", ps, depth, block.data,
1187 block.HeaderLength - block.SystemNameOffset))
1193 /*********************************************************************
1194 *********************************************************************/
1196 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1197 struct PERF_OBJECT_TYPE object,
1201 struct PERF_COUNTER_DEFINITION counter;
1203 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1206 for(cnt = 0; cnt < object.NumCounters; cnt++)
1208 counter = object.counters[cnt];
1212 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1214 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1216 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1218 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1220 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1222 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1224 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1226 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1228 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1230 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1237 /*********************************************************************
1238 *********************************************************************/
1240 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps,
1241 struct PERF_COUNTER_BLOCK counter_data,
1244 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1247 if(!prs_align_uint64(ps))
1250 if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1252 if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1254 if(!prs_align_uint64(ps))
1260 /*********************************************************************
1261 *********************************************************************/
1263 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1264 struct PERF_OBJECT_TYPE object,
1267 struct PERF_INSTANCE_DEFINITION instance;
1270 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1273 for(inst = 0; inst < object.NumInstances; inst++)
1275 instance = object.instances[inst];
1279 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1281 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1283 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1285 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1287 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1289 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1291 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1292 instance.ByteLength - instance.NameOffset))
1294 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1301 /*********************************************************************
1302 *********************************************************************/
1304 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, struct PERF_DATA_BLOCK block, int depth)
1308 struct PERF_OBJECT_TYPE object;
1310 prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1313 for(obj = 0; obj < block.NumObjectTypes; obj++)
1315 object = block.objects[obj];
1320 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1322 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1324 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1326 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1328 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1330 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1332 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1334 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1336 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1338 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1340 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1342 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1344 if(!prs_align_uint64(ps))
1346 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1348 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1351 /* Now do the counters */
1352 /* If no instances, encode counter_data */
1353 /* If instances, encode instace plus counter data for each instance */
1354 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1356 if(object.NumInstances == PERF_NO_INSTANCES)
1358 if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1363 if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1371 /*********************************************************************
1372 *********************************************************************/
1374 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
1377 * For a detailed description of the layout of this structure,
1378 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1380 * By 2006-11-23 this link did not work anymore, I found something
1382 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1384 struct PERF_DATA_BLOCK block;
1385 uint32 buffer_size, base_index;
1388 base_index = reg_perfcount_get_base_index();
1391 buffer_size = reg_perfcount_get_perf_data_block(base_index, ps->mem_ctx, &block, object_ids, ps->bigendian_data);
1393 if(buffer_size < max_buf_size)
1395 *outbuf_len = buffer_size;
1397 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1400 if (!_reg_perfcount_marshall_perf_objects(ps, block, 0))
1407 *outbuf_len = max_buf_size;
1408 if (!_reg_perfcount_marshall_perf_data_block(ps, block, 0))
1411 return WERR_INSUFFICIENT_BUFFER;