HPCToolkit
cache.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 /* cache functions: accessors, resizers, etc. */
48 
49 #ifndef CSPROF_TRAMPOLINE_BACKEND
50 /* cache_set: Updates the cached arrays that are designed to speed up
51  tree insertions. Adds tree node 'node' and instruction pointer 'ip' to
52  the cached arrays at depth 'depth'. Note that depth is 1 based and a
53  depth of 1 refers to the last element of the array. */
54 static int
55 cache_set(hpcrun_cct_t* x, unsigned int depth, void* ip,
56  hpcrun_cct_node_t* node)
57 {
58  unsigned int i;
59 
60  /* 1. Resize the arrays if necessary */
61  if (depth > x->cache_len) {
62  cache_resize(x, depth);
63  }
64 
65  /* 2. Update cached arrays */
66  i = x->cache_len - depth;
67  x->cache_bt[i] = ip;
68  x->cache_nodes[i] = node;
69 
70  return CSPROF_OK;
71 }
72 
73 /* cache_set_depth: sets the cache depth to 'depth' */
74 static int
75 cache_set_depth(hpcrun_cct_t* x, unsigned int depth)
76 {
77  if (depth > x->cache_len) {
78  cache_resize(x, depth);
79  }
80  x->cache_top = x->cache_len - depth;
81  return CSPROF_OK;
82 }
83 
84 /* cache_resize: resizes the cache, ensuring that it is at least of
85  size 'depth' */
86 static int
87 cache_resize(hpcrun_cct_t* x, unsigned int depth)
88 {
89  if (depth > x->cache_len) {
90  // only grow larger
91  void** old_bt = x->cache_bt;
92  hpcrun_cct_node_t** old_nodes = x->cache_nodes;
93  unsigned int old_len = x->cache_len, padding = 0;
94 
95  x->cache_len = x->cache_len * 2;
96  if (depth > x->cache_len) { x->cache_len = depth; }
97 
98  x->cache_bt = hpcrun_malloc(sizeof(void*) * x->cache_len);
99  x->cache_nodes = hpcrun_malloc(sizeof(hpcrun_cct_node_t*) *
100  x->cache_len);
101  DBGMSG_PUB(CSPROF_DBG_CCT_INSERTION,
102  "resizing cct cache: %d -> %d length", old_len, x->cache_len);
103 
104  padding = x->cache_len - old_len;
105  memcpy(x->cache_bt + padding, old_bt, sizeof(void*) * old_len);
106  memcpy(x->cache_nodes + padding, old_nodes, sizeof(void*) * old_len);
107  x->cache_top = x->cache_top + padding;
108 
109  /* no need to free */
110  }
111  return CSPROF_OK;
112 }
113 
114 /* cache_get_bt: Gets the backtrace cache entry at depth 'depth'.
115  Note that 'depth' is 1 based and that 1 refers to the bottom of the
116  stack (the root of the tree). */
117 static void*
118 cache_get_bt(hpcrun_cct_t* x, unsigned int depth)
119 {
120  unsigned int i = x->cache_len - depth;
121  if (depth > x->cache_len || i < x->cache_top) { return NULL; }
122  return x->cache_bt[i];
123 }
124 
125 /* cache_get_node: Gets the cache node at depth 'depth'.
126  Note that 'depth' is 1 based and that 1 refers to the bottom of the
127  stack (the root of the tree). */
128 static hpcrun_cct_node_t*
129 cache_get_node(hpcrun_cct_t* x, unsigned int depth)
130 {
131  unsigned int i = x->cache_len - depth;
132  if (depth > x->cache_len || i < x->cache_top) { return NULL; }
133  return x->cache_nodes[i];
134 }
135 
136 #endif
static int cache_set_depth(hpcrun_cct_t *x, unsigned int depth)
Definition: cache.c:75
static int cache_set(hpcrun_cct_t *x, unsigned int depth, void *ip, hpcrun_cct_node_t *node)
Definition: cache.c:55
cct_node_t * node
Definition: cct.c:128
static int cache_resize(hpcrun_cct_t *x, unsigned int depth)
Definition: cache.c:87
static hpcrun_cct_node_t * cache_get_node(hpcrun_cct_t *x, unsigned int depth)
Definition: cache.c:129
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
#define NULL
Definition: ElfHelper.cpp:85
static void * cache_get_bt(hpcrun_cct_t *x, unsigned int depth)
Definition: cache.c:118