-
Notifications
You must be signed in to change notification settings - Fork 15
Add a method to trace call paths (for create/ref/deref) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2f654eb
7952939
2aeffbd
26698ea
d09742e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * Stores a call trace ("backtrace") for later inspection. | ||
| * | ||
| * Copyright (C) 2014 Peter Wu <peter@lekensteyn.nl> | ||
| * | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
| * USA | ||
| */ | ||
|
|
||
| /* TODO | ||
| * - faster lookups. The path lookup can surely be optimized (cached?) | ||
| * - This code allocates memory which may or may not be a problem depending on | ||
| * context. | ||
| */ | ||
|
|
||
| #include <glib.h> | ||
| #include "bt-tree.h" | ||
|
|
||
| enum { | ||
| COUNT_REF = 0, | ||
| COUNT_UNREF, | ||
|
|
||
| COUNT_LAST | ||
| }; | ||
|
|
||
| typedef struct BtTrie { | ||
| GHashTable *children; | ||
| char *label; | ||
| unsigned count[COUNT_LAST]; | ||
| } BtTrie; | ||
|
|
||
| BtTrie * | ||
| bt_create (char *label) | ||
| { | ||
| BtTrie *bt_trie = g_malloc0 (sizeof(BtTrie)); | ||
| bt_trie->label = label; | ||
| bt_trie->children = g_hash_table_new_full (g_str_hash, g_str_equal, | ||
| NULL, (GDestroyNotify) bt_free); | ||
| return bt_trie; | ||
| } | ||
|
|
||
| void | ||
| bt_free (BtTrie *bt_trie) | ||
| { | ||
| g_free (bt_trie->label); | ||
| g_hash_table_unref (bt_trie->children); | ||
| g_free (bt_trie); | ||
| } | ||
|
|
||
| /* returns the child of bt_trie with the item at position i inserted. The memory | ||
| * is freed if such a child already exists. */ | ||
| static inline BtTrie * | ||
| find_child (BtTrie *bt_trie, const GPtrArray *items, guint i) | ||
| { | ||
| BtTrie *child = NULL; | ||
| char *label = g_ptr_array_index (items, i); | ||
| g_hash_table_lookup_extended (bt_trie->children, label, | ||
| NULL, (gpointer *)&child); | ||
| if (child == NULL) { | ||
| child = bt_create (label); | ||
| g_hash_table_insert (bt_trie->children, child->label, child); | ||
| } else { | ||
| /* unused label */ | ||
| g_free (label); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're releasing the memory but the label is still in the GPtrArray. Create your GPtrArray with a destroy func and then use g_ptr_array_remove_index() that will cause it to release the memory while removing it. |
||
| } | ||
| return child; | ||
| } | ||
|
|
||
| /** | ||
| * Inserts a trace described by items into a trie. Memory can be allocated if a | ||
| * node is missing. | ||
| * @bt_trie: root of the tree. | ||
| * @items: the items to insert (in reverse order: the first element is the leaf, | ||
| * the last element is the root). Must not be empty. The control of the contents | ||
| * is transferred from the caller. | ||
| */ | ||
| void | ||
| bt_insert (BtTrie *bt_trie, const GPtrArray *items, gboolean is_ref) | ||
| { | ||
| guint i = items->len; | ||
| ++bt_trie->count[is_ref ? COUNT_REF : COUNT_UNREF]; /* mark root */ | ||
| while (i-- > 0) { | ||
| bt_trie = find_child (bt_trie, items, i); | ||
| ++bt_trie->count[is_ref ? COUNT_REF : COUNT_UNREF]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You use this ternary twice, assign it somewhere and use the variable name. |
||
| } | ||
| } | ||
|
|
||
| static void | ||
| _bt_print_tree (gpointer key, gpointer value, gpointer user_data) | ||
| { | ||
| const char *label = key; | ||
| BtTrie *tree = value; | ||
| guint indent = GPOINTER_TO_INT (user_data), i; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One declaration per line please. |
||
| gint diff = tree->count[COUNT_REF] - tree->count[COUNT_UNREF]; | ||
| const char | ||
| *color_default = "\e[1;34m", /* blue */ | ||
| *color_unref = "\e[0;31m", /* red */ | ||
| *color_ref = "\e[0;33m", /* yellow */ | ||
| *color_diff; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make these #defines. |
||
|
|
||
| if (diff == 0) /* not important */ | ||
| color_default = color_unref = color_ref = color_diff = | ||
| "\e[1;30m"; /* gray */ | ||
| else if (diff < 0) /* more unrefs than refs */ | ||
| color_diff = "\e[1;31m"; /* red */ | ||
| else /* diff > 0, more refs than unrefs */ | ||
| color_diff = "\e[1;33m"; /* yellow */ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copies of what's above. |
||
|
|
||
| for (i = 0; i < indent; i++) | ||
| g_print("| "); | ||
| g_print ("%s# %s ", color_default, label); /* name */ | ||
| g_print ("(" | ||
| "%s+%u%s" /* refs */ | ||
| "/" | ||
| "%s-%u%s" /* unrefs */ | ||
| " = %s%d%s", /* diff */ | ||
| color_ref, tree->count[COUNT_REF], color_default, | ||
| color_unref, tree->count[COUNT_UNREF], color_default, | ||
| color_diff, diff, color_default); | ||
| g_print (")\e[m\n"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this in a #define also. |
||
| g_hash_table_foreach (tree->children, _bt_print_tree, | ||
| GINT_TO_POINTER (indent + 1)); | ||
| } | ||
|
|
||
| void | ||
| bt_print_tree (BtTrie *root, guint indent) | ||
| { | ||
| _bt_print_tree (root->label, root, GINT_TO_POINTER (indent)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| typedef struct BtTrie BtTrie; | ||
|
|
||
| BtTrie *bt_create (char *label); | ||
| void bt_free (BtTrie *bt_trie); | ||
| void bt_insert (BtTrie *root, const GPtrArray *items, gboolean is_ref); | ||
| void bt_print_tree (BtTrie *root, guint indent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BtTree ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A trie is a data structure also known as prefix tree. "BtTrie" = "prefix tree for backtraces storage".