Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_debug_refcnt.c
4561 views
/**************************************************************************1*2* Copyright 2010 Luca Barbieri3*4* Permission is hereby granted, free of charge, to any person obtaining5* a copy of this software and associated documentation files (the6* "Software"), to deal in the Software without restriction, including7* without limitation the rights to use, copy, modify, merge, publish,8* distribute, sublicense, and/or sell copies of the Software, and to9* permit persons to whom the Software is furnished to do so, subject to10* the following conditions:11*12* The above copyright notice and this permission notice (including the13* next paragraph) shall be included in all copies or substantial14* portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.19* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*24**************************************************************************/2526#if defined(DEBUG)2728/**29* If the GALLIUM_REFCNT_LOG env var is defined as a filename, gallium30* reference counting will be logged to the file.31*32* See http://www-archive.mozilla.org/performance/refcnt-balancer.html33* for what to do with the output on Linux, use tools/addr2line.sh to34* postprocess it before anything else.35*/3637#include <stdio.h>3839#include "util/u_debug.h"40#include "util/u_debug_refcnt.h"41#include "util/u_debug_stack.h"42#include "util/u_debug_symbol.h"43#include "util/u_string.h"44#include "util/u_hash_table.h"45#include "os/os_thread.h"46#include "pipe/p_config.h"4748int debug_refcnt_state;4950static FILE *stream;5152/* TODO: maybe move this serial machinery to a stand-alone module and53* expose it?54*/55#ifdef PIPE_OS_WINDOWS56static mtx_t serials_mutex;57#else58static mtx_t serials_mutex = _MTX_INITIALIZER_NP;59#endif6061static struct hash_table *serials_hash;62static unsigned serials_last;636465/**66* Return a small integer serial number for the given pointer.67*/68static boolean69debug_serial(void *p, unsigned *pserial)70{71unsigned serial;72boolean found = TRUE;73#ifdef PIPE_OS_WINDOWS74static boolean first = TRUE;7576if (first) {77(void) mtx_init(&serials_mutex, mtx_plain);78first = FALSE;79}80#endif8182mtx_lock(&serials_mutex);83if (!serials_hash)84serials_hash = util_hash_table_create_ptr_keys();8586serial = (unsigned) (uintptr_t) util_hash_table_get(serials_hash, p);87if (!serial) {88/* time to stop logging... (you'll have a 100 GB logfile at least at89* this point) TODO: avoid this90*/91serial = ++serials_last;92if (!serial) {93debug_error("More than 2^32 objects detected, aborting.\n");94os_abort();95}9697_mesa_hash_table_insert(serials_hash, p, (void *) (uintptr_t) serial);98found = FALSE;99}100mtx_unlock(&serials_mutex);101102*pserial = serial;103104return found;105}106107108/**109* Free the serial number for the given pointer.110*/111static void112debug_serial_delete(void *p)113{114mtx_lock(&serials_mutex);115_mesa_hash_table_remove_key(serials_hash, p);116mtx_unlock(&serials_mutex);117}118119120#if defined(PIPE_OS_WINDOWS)121#define STACK_LEN 60122#else123#define STACK_LEN 64124#endif125126/**127* Log a reference count change to the log file (if enabled).128* This is called via the pipe_reference() and debug_reference() functions,129* basically whenever a reference count is initialized or changed.130*131* \param p the refcount being changed (the value is not changed here)132* \param get_desc a function which will be called to print an object's133* name/pointer into a string buffer during logging134* \param change the reference count change which must be +/-1 or 0 when135* creating the object and initializing the refcount.136*/137void138debug_reference_slowpath(const struct pipe_reference *p,139debug_reference_descriptor get_desc, int change)140{141assert(change >= -1);142assert(change <= 1);143144if (debug_refcnt_state < 0)145return;146147if (!debug_refcnt_state) {148const char *filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);149if (filename && filename[0])150stream = fopen(filename, "wt");151152if (stream)153debug_refcnt_state = 1;154else155debug_refcnt_state = -1;156}157158if (debug_refcnt_state > 0) {159struct debug_stack_frame frames[STACK_LEN];160char buf[1024];161unsigned i;162unsigned refcnt = p->count;163unsigned serial;164boolean existing = debug_serial((void *) p, &serial);165166debug_backtrace_capture(frames, 1, STACK_LEN);167168get_desc(buf, p);169170if (!existing) {171fprintf(stream, "<%s> %p %u Create\n", buf, (void *) p, serial);172debug_backtrace_print(stream, frames, STACK_LEN);173174/* this is here to provide a gradual change even if we don't see175* the initialization176*/177for (i = 1; i <= refcnt - change; ++i) {178fprintf(stream, "<%s> %p %u AddRef %u\n", buf, (void *) p,179serial, i);180debug_backtrace_print(stream, frames, STACK_LEN);181}182}183184if (change) {185fprintf(stream, "<%s> %p %u %s %u\n", buf, (void *) p, serial,186change > 0 ? "AddRef" : "Release", refcnt);187debug_backtrace_print(stream, frames, STACK_LEN);188}189190if (!refcnt) {191debug_serial_delete((void *) p);192fprintf(stream, "<%s> %p %u Destroy\n", buf, (void *) p, serial);193debug_backtrace_print(stream, frames, STACK_LEN);194}195196fflush(stream);197}198}199200#endif /* DEBUG */201202203