hprof_site.c
来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 903 行 · 第 1/3 页
C
903 行
/* * @(#)hprof_site.c 1.41 06/02/18 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* Allocation site table. *//* * Every object allocation will have a place where it was allocated, * this is the purpose of the SiteIndex. * * The allocation site or SiteIndex is unique via a (class,trace) pair. * * The allocation statistics are accumulated in the SiteInfo for each * site. * * This file also contains the heap iterate logic, which is closely * associated with the site table, the object table, and the * reference table. Each object has an element in the object table * and as the heap is traversed, and information contained in each * object is saved as a linked list of references. * */#include "hprof.h"typedef struct SiteKey { ClassIndex cnum; /* Unique class number */ TraceIndex trace_index; /* Trace number */} SiteKey;typedef struct SiteInfo { int changed; /* Objects at this site changed? */ unsigned n_alloced_instances; /* Total allocated instances */ unsigned n_alloced_bytes; /* Total bytes allocated from here */ unsigned n_live_instances; /* Live instances for this site. */ unsigned n_live_bytes; /* Live byte count for this site. */} SiteInfo;typedef struct IterateInfo { SiteIndex * site_nums; int count; int changed_only;} IterateInfo;/* Private internal functions. */static SiteKey*get_pkey(SiteIndex index){ void *key_ptr; int key_len; table_get_key(gdata->site_table, index, &key_ptr, &key_len); HPROF_ASSERT(key_len==sizeof(SiteKey)); HPROF_ASSERT(key_ptr!=NULL); return (SiteKey*)key_ptr;}ClassIndexsite_get_class_index(SiteIndex index) { SiteKey *pkey; pkey = get_pkey(index); return pkey->cnum;}TraceIndexsite_get_trace_index(SiteIndex index) { SiteKey *pkey; pkey = get_pkey(index); return pkey->trace_index;}static SiteInfo *get_info(SiteIndex index){ SiteInfo *info; info = (SiteInfo*)table_get_info(gdata->site_table, index); return info;}static voidlist_item(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg){ SiteKey *pkey; jlong n_alloced_instances; jlong n_alloced_bytes; jlong n_live_instances; jlong n_live_bytes; HPROF_ASSERT(key_ptr!=NULL); HPROF_ASSERT(key_len==sizeof(SiteKey)); pkey = (SiteKey*)key_ptr; if ( info_ptr != NULL ) { SiteInfo *info; info = (SiteInfo *)info_ptr; n_alloced_instances = info->n_alloced_instances; n_alloced_bytes = info->n_alloced_bytes; n_live_instances = info->n_live_instances; n_live_bytes = info->n_live_bytes; } else { n_alloced_instances = 0; n_alloced_bytes = 0; n_live_instances = 0; n_live_bytes = 0; } debug_message( "Site 0x%08x: class=0x%08x, trace=0x%08x, " "Ninst=(%d,%d), Nbytes=(%d,%d), " "Nlive=(%d,%d), NliveBytes=(%d,%d)\n", i, pkey->cnum, pkey->trace_index, jlong_high(n_alloced_instances), jlong_low(n_alloced_instances), jlong_high(n_alloced_bytes), jlong_low(n_alloced_bytes), jlong_high(n_live_instances), jlong_low(n_live_instances), jlong_high(n_live_bytes), jlong_low(n_live_bytes));}static voidcollect_iterator(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg){ IterateInfo *iterate; HPROF_ASSERT(key_ptr!=NULL); HPROF_ASSERT(key_len==sizeof(SiteKey)); HPROF_ASSERT(arg!=NULL); iterate = (IterateInfo *)arg; if ( iterate->changed_only ) { SiteInfo *info; info = (SiteInfo *)info_ptr; if ( info==NULL || !info->changed ) { return; } } iterate->site_nums[iterate->count++] = i;}static voidmark_unchanged_iterator(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg){ SiteInfo *info; HPROF_ASSERT(key_ptr!=NULL); HPROF_ASSERT(key_len==sizeof(SiteKey)); info = (SiteInfo *)info_ptr; if ( info != NULL ) { info->changed = 0; }}static int qsort_compare_allocated_bytes(const void *p_site1, const void *p_site2){ SiteIndex site1; SiteIndex site2; SiteInfo *info1; SiteInfo *info2; HPROF_ASSERT(p_site1!=NULL); HPROF_ASSERT(p_site2!=NULL); site1 = *(SiteIndex *)p_site1; site2 = *(SiteIndex *)p_site2; info1 = get_info(site1); info2 = get_info(site2); return info2->n_alloced_bytes - info1->n_alloced_bytes;}static int qsort_compare_live_bytes(const void *p_site1, const void *p_site2){ SiteIndex site1; SiteIndex site2; SiteInfo *info1; SiteInfo *info2; HPROF_ASSERT(p_site1!=NULL); HPROF_ASSERT(p_site2!=NULL); site1 = *(SiteIndex *)p_site1; site2 = *(SiteIndex *)p_site2; info1 = get_info(site1); info2 = get_info(site2); return info2->n_live_bytes - info1->n_live_bytes;}static ClassIndexfind_cnum(jlong class_tag){ ClassIndex cnum; ObjectIndex class_object_index; SiteIndex class_site_index; SiteKey *pkey; HPROF_ASSERT(class_tag!=(jlong)0); class_object_index = tag_extract(class_tag); class_site_index = object_get_site(class_object_index); pkey = get_pkey(class_site_index); cnum = pkey->cnum; return cnum;}/* Create tag and object entry for an untagged object (should be rare) */static jlongmake_new_tag(jlong class_tag, jlong size, TraceIndex trace_index, SerialNumber thread_serial_num, ObjectIndex *pindex, SiteIndex *psite){ ObjectIndex object_index; SiteIndex object_site_index; HPROF_ASSERT(class_tag!=(jlong)0); object_site_index = site_find_or_create(find_cnum(class_tag), trace_index); object_index = object_new(object_site_index, (jint)size, OBJECT_SYSTEM, thread_serial_num); if ( pindex != NULL ) { *pindex = object_index; } if ( psite != NULL ) { *psite = object_site_index; } return tag_create(object_index);}/* Setup tag on root object, if tagged return object index and site index */static voidsetup_tag_on_root(jlong *tag_ptr, jlong class_tag, jlong size, SerialNumber thread_serial_num, ObjectIndex *pindex, SiteIndex *psite){ HPROF_ASSERT(class_tag!=(jlong)0); if ( (*tag_ptr) != (jlong)0 ) { if ( pindex != NULL ) { *pindex = tag_extract(*tag_ptr); } if ( psite != NULL ) { *psite = object_get_site(tag_extract(*tag_ptr)); } } else { /* Create and set the tag. */ *tag_ptr = make_new_tag(class_tag, size, gdata->system_trace_index, thread_serial_num, pindex, psite); }}/* External interfaces */SiteIndexsite_find_or_create(ClassIndex cnum, TraceIndex trace_index){ SiteIndex index; static SiteKey empty_key; SiteKey key; key = empty_key; HPROF_ASSERT(cnum!=0); HPROF_ASSERT(trace_index!=0); key.cnum = cnum; key.trace_index = trace_index; index = table_find_or_create_entry(gdata->site_table, &key, (int)sizeof(key), NULL, NULL);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?