xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/util/u_debug_refcnt.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2010 Luca Barbieri
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  **************************************************************************/
26 
27 #if MESA_DEBUG
28 
29 /**
30  * If the GALLIUM_REFCNT_LOG env var is defined as a filename, gallium
31  * reference counting will be logged to the file.
32  *
33  * See http://www-archive.mozilla.org/performance/refcnt-balancer.html
34  * for what to do with the output on Linux, use tools/addr2line.sh to
35  * postprocess it before anything else.
36  */
37 
38 #include <stdio.h>
39 
40 #include "util/simple_mtx.h"
41 #include "util/u_debug.h"
42 #include "util/u_debug_refcnt.h"
43 #include "util/u_debug_stack.h"
44 #include "util/u_debug_symbol.h"
45 #include "util/u_string.h"
46 #include "util/u_hash_table.h"
47 #include "util/u_thread.h"
48 
49 int debug_refcnt_state;
50 
51 static FILE *stream;
52 
53 /* TODO: maybe move this serial machinery to a stand-alone module and
54  * expose it?
55  */
56 
57 static simple_mtx_t serials_mutex = SIMPLE_MTX_INITIALIZER;
58 static struct hash_table *serials_hash;
59 static unsigned serials_last;
60 
61 
62 /**
63  * Return a small integer serial number for the given pointer.
64  */
65 static bool
debug_serial(void * p,unsigned * pserial)66 debug_serial(void *p, unsigned *pserial)
67 {
68    unsigned serial;
69    bool found = true;
70 
71    simple_mtx_lock(&serials_mutex);
72    if (!serials_hash)
73       serials_hash = util_hash_table_create_ptr_keys();
74 
75    serial = (unsigned) (uintptr_t) util_hash_table_get(serials_hash, p);
76    if (!serial) {
77       /* time to stop logging... (you'll have a 100 GB logfile at least at
78        * this point)  TODO: avoid this
79        */
80       serial = ++serials_last;
81       if (!serial) {
82          debug_error("More than 2^32 objects detected, aborting.\n");
83          os_abort();
84       }
85 
86       _mesa_hash_table_insert(serials_hash, p, (void *) (uintptr_t) serial);
87       found = false;
88    }
89    simple_mtx_unlock(&serials_mutex);
90 
91    *pserial = serial;
92 
93    return found;
94 }
95 
96 
97 /**
98  * Free the serial number for the given pointer.
99  */
100 static void
debug_serial_delete(void * p)101 debug_serial_delete(void *p)
102 {
103    simple_mtx_lock(&serials_mutex);
104    _mesa_hash_table_remove_key(serials_hash, p);
105    simple_mtx_unlock(&serials_mutex);
106 }
107 
108 
109 #if DETECT_OS_WINDOWS
110 #define STACK_LEN 60
111 #else
112 #define STACK_LEN 64
113 #endif
114 
115 /**
116  * Log a reference count change to the log file (if enabled).
117  * This is called via the pipe_reference() and debug_reference() functions,
118  * basically whenever a reference count is initialized or changed.
119  *
120  * \param p  the refcount being changed (the value is not changed here)
121  * \param get_desc  a function which will be called to print an object's
122  *                  name/pointer into a string buffer during logging
123  * \param change  the reference count change which must be +/-1 or 0 when
124  *                creating the object and initializing the refcount.
125  */
126 void
debug_reference_slowpath(const struct pipe_reference * p,debug_reference_descriptor get_desc,int change)127 debug_reference_slowpath(const struct pipe_reference *p,
128                          debug_reference_descriptor get_desc, int change)
129 {
130    assert(change >= -1);
131    assert(change <= 1);
132 
133    if (debug_refcnt_state < 0)
134       return;
135 
136    if (!debug_refcnt_state) {
137       const char *filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);
138       if (filename && filename[0])
139          stream = fopen(filename, "wt");
140 
141       if (stream)
142          debug_refcnt_state = 1;
143       else
144          debug_refcnt_state = -1;
145    }
146 
147    if (debug_refcnt_state > 0) {
148       struct debug_stack_frame frames[STACK_LEN];
149       char buf[1024];
150       unsigned i;
151       unsigned refcnt = p->count;
152       unsigned serial;
153       bool existing = debug_serial((void *) p, &serial);
154 
155       debug_backtrace_capture(frames, 1, STACK_LEN);
156 
157       get_desc(buf, p);
158 
159       if (!existing) {
160          fprintf(stream, "<%s> %p %u Create\n", buf, (void *) p, serial);
161          debug_backtrace_print(stream, frames, STACK_LEN);
162 
163          /* this is here to provide a gradual change even if we don't see
164           * the initialization
165           */
166          if (refcnt <= 10) {
167             for (i = 1; i <= refcnt - change; ++i) {
168                fprintf(stream, "<%s> %p %u AddRef %u\n", buf, (void *) p,
169                   serial, i);
170                debug_backtrace_print(stream, frames, STACK_LEN);
171             }
172          }
173       }
174 
175       if (change) {
176          fprintf(stream, "<%s> %p %u %s %u\n", buf, (void *) p, serial,
177                  change > 0 ? "AddRef" : "Release", refcnt);
178          debug_backtrace_print(stream, frames, STACK_LEN);
179       }
180 
181       if (!refcnt) {
182          debug_serial_delete((void *) p);
183          fprintf(stream, "<%s> %p %u Destroy\n", buf, (void *) p, serial);
184          debug_backtrace_print(stream, frames, STACK_LEN);
185       }
186 
187       fflush(stream);
188    }
189 }
190 
191 #endif /* MESA_DEBUG */
192