?? reg_perfcount.c
字號:
return True;}/******************************************************************************************************************************************/static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block, prs_struct *ps, TDB_CONTEXT *names){ wpstring temp; time_t tm; memset(temp, 0, sizeof(temp)); rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE); memcpy(block->Signature, temp, strlen_w(temp) *2); if(ps->bigendian_data == RPC_BIG_ENDIAN) block->LittleEndian = 0; else block->LittleEndian = 1; block->Version = 1; block->Revision = 1; block->TotalByteLength = 0; block->NumObjectTypes = 0; block->DefaultObject = -1; block->objects = NULL; tm = time(NULL); make_systemtime(&(block->SystemTime), gmtime(&tm)); _reg_perfcount_init_data_block_perf(block, names); memset(temp, 0, sizeof(temp)); rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE); block->SystemNameLength = (strlen_w(temp) * 2) + 2; block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8))); memcpy(block->data, temp, block->SystemNameLength); block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data); block->HeaderLength = block->SystemNameOffset + block->SystemNameLength; /* Make sure to adjust for 64-bit alignment for when we finish writing the system name, so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */ block->HeaderLength += 8 - (block->HeaderLength % 8); return;}/******************************************************************************************************************************************/static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps){ int obj, cnt, inst, pad, i; PERF_OBJECT_TYPE *object; PERF_INSTANCE_DEFINITION *instance; PERF_COUNTER_DEFINITION *counter; PERF_COUNTER_BLOCK *counter_data; char *temp = NULL, *src_addr, *dst_addr; block->TotalByteLength = 0; object = block->objects; for(obj = 0; obj < block->NumObjectTypes; obj++) { object[obj].TotalByteLength = 0; object[obj].DefinitionLength = 0; instance = object[obj].instances; counter = object[obj].counters; for(cnt = 0; cnt < object[obj].NumCounters; cnt++) { object[obj].TotalByteLength += counter[cnt].ByteLength; object[obj].DefinitionLength += counter[cnt].ByteLength; } if(object[obj].NumInstances != PERF_NO_INSTANCES) { for(inst = 0; inst < object[obj].NumInstances; inst++) { instance = &(object[obj].instances[inst]); object[obj].TotalByteLength += instance->ByteLength; counter_data = &(instance->counter_data); counter = &(object[obj].counters[object[obj].NumCounters - 1]); counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength); temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx, temp, char, counter_data->ByteLength- sizeof(counter_data->ByteLength)); memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength)); src_addr = (char *)counter_data->data; for(i = 0; i < object[obj].NumCounters; i++) { counter = &(object[obj].counters[i]); dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength); memcpy(dst_addr, src_addr, counter->CounterSize); src_addr += counter->CounterSize; } /* Make sure to be 64-bit aligned */ if((pad = (counter_data->ByteLength % 8))) { pad = 8 - pad; } counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, counter_data->data, uint8, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad); memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad); memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength)); counter_data->ByteLength += pad; object[obj].TotalByteLength += counter_data->ByteLength; } } else { /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary, so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */ if((pad = (object[obj].counter_data.ByteLength % 8))) { pad = 8 - pad; object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, object[obj].counter_data.data, uint8, object[obj].counter_data.ByteLength + pad); memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad); object[obj].counter_data.ByteLength += pad; } object[obj].TotalByteLength += object[obj].counter_data.ByteLength; } object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK)); object[obj].TotalByteLength += object[obj].HeaderLength; object[obj].DefinitionLength += object[obj].HeaderLength; block->TotalByteLength += object[obj].TotalByteLength; } return block->TotalByteLength;}/******************************************************************************************************************************************/uint32 reg_perfcount_get_perf_data_block(uint32 base_index, prs_struct *ps, PERF_DATA_BLOCK *block, char *object_ids){ uint32 buffer_size = 0, last_counter; const char *fname = counters_directory( NAMES_DB ); TDB_CONTEXT *names; int retval; names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444); if(names == NULL) { DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname)); return 0; } _reg_perfcount_init_data_block(block, ps, names); last_counter = reg_perfcount_get_last_counter(base_index); if(object_ids == NULL) { /* we're getting a request for "Global" here */ retval = _reg_perfcount_assemble_global(block, ps, base_index, names); } else { /* we're getting a request for a specific set of PERF_OBJECT_TYPES */ retval = _reg_perfcount_assemble_global(block, ps, base_index, names); } buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps); tdb_close(names); return buffer_size + block->HeaderLength;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth){ int i; prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block"); depth++; if(!prs_align(ps)) return False; for(i = 0; i < 4; i++) { if(!prs_uint16("Signature", ps, depth, &block.Signature[i])) return False; } if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian)) return False; if(!prs_uint32("Version", ps, depth, &block.Version)) return False; if(!prs_uint32("Revision", ps, depth, &block.Revision)) return False; if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength)) return False; if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength)) return False; if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes)) return False; if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject)) return False; if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime)) return False; if(!prs_uint32("Padding", ps, depth, &block.Padding)) return False; if(!prs_align_uint64(ps)) return False; if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime)) return False; if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq)) return False; if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec)) return False; if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength)) return False; if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset)) return False; /* hack to make sure we're 64-bit aligned at the end of this whole mess */ if(!prs_uint8s(False, "SystemName", ps, depth, block.data, block.HeaderLength - block.SystemNameOffset)) return False; return True;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps, PERF_OBJECT_TYPE object, int depth){ int cnt; PERF_COUNTER_DEFINITION counter; prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters"); depth++; for(cnt = 0; cnt < object.NumCounters; cnt++) { counter = object.counters[cnt]; if(!prs_align(ps)) return False; if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength)) return False; if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex)) return False; if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer)) return False; if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex)) return False; if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer)) return False; if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale)) return False; if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel)) return False; if(!prs_uint32("CounterType", ps, depth, &counter.CounterType)) return False; if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize)) return False; if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset)) return False; } return True;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, PERF_COUNTER_BLOCK counter_data, int depth){ prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data"); depth++; if(!prs_align_uint64(ps)) return False; if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength)) return False; if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32))) return False; if(!prs_align_uint64(ps)) return False; return True;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps, PERF_OBJECT_TYPE object, int depth){ PERF_INSTANCE_DEFINITION instance; int inst; prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances"); depth++; for(inst = 0; inst < object.NumInstances; inst++) { instance = object.instances[inst]; if(!prs_align(ps)) return False; if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength)) return False; if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex)) return False; if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer)) return False; if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID)) return False; if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset)) return False; if(!prs_uint32("NameLength", ps, depth, &instance.NameLength)) return False; if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data, instance.ByteLength - instance.NameOffset)) return False; if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False) return False; } return True;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth){ int obj; PERF_OBJECT_TYPE object; prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects"); depth++; for(obj = 0; obj < block.NumObjectTypes; obj++) { object = block.objects[obj]; if(!prs_align(ps)) return False; if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength)) return False; if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength)) return False; if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength)) return False; if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex)) return False; if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer)) return False; if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex)) return False; if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer)) return False; if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel)) return False; if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters)) return False; if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter)) return False; if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances)) return False; if(!prs_uint32("CodePage", ps, depth, &object.CodePage)) return False; if(!prs_align_uint64(ps)) return False; if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime)) return False; if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq)) return False; /* Now do the counters */ /* If no instances, encode counter_data */ /* If instances, encode instace plus counter data for each instance */ if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False) return False; if(object.NumInstances == PERF_NO_INSTANCES) { if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False) return False; } else { if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False) return False; } } return True;}/******************************************************************************************************************************************/static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block){ int depth = 0; if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True) { if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True) return True; } return False;}/******************************************************************************************************************************************/WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, char *object_ids){ /* * For a detailed description of the layout of this structure, * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp */ PERF_DATA_BLOCK block; uint32 buffer_size, base_index; buffer_size = 0; base_index = reg_perfcount_get_base_index(); ZERO_STRUCT(block); buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids); if(buffer_size < max_buf_size) { *outbuf_len = buffer_size; if(_reg_perfcount_marshall_hkpd(ps, block) == True) return WERR_OK; else return WERR_NOMEM; } else { *outbuf_len = max_buf_size; _reg_perfcount_marshall_perf_data_block(ps, block, 0); return WERR_INSUFFICIENT_BUFFER; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -