HPCToolkit
perf-util.h
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 
38 #ifndef __PERF_UTIL_H__
39 #define __PERF_UTIL_H__
40 
41 #include <sys/syscall.h>
42 
43 #include <unistd.h>
44 
45 #include <linux/types.h>
46 #include <linux/perf_event.h>
47 #include <linux/version.h>
48 
50 #include <sample_event.h>
51 
52 #include "perf_constants.h"
53 
54 /******************************************************************************
55  * macros
56  *****************************************************************************/
57 
58 #define KERNEL_SAMPLING_ENABLED (LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0))
59 
60 
61 
62 // the number of maximum frames (call chains)
63 // For kernel only call chain, I think 32 is a good number.
64 // If we include user call chains, it should be bigger than that.
65 #define MAX_CALLCHAIN_FRAMES 32
66 
67 // --------------------------------------------------------------------
68 // threshold data structure (period or frequency
69 // --------------------------------------------------------------------
70 
72 
76 };
77 
78 
79 /******************************************************************************
80  * Data types
81  *****************************************************************************/
82 
83 // data from perf's mmap. See perf_event_open man page
84 typedef struct perf_mmap_data_s {
85  struct perf_event_header header;
86  u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
87  u64 ip; /* if PERF_SAMPLE_IP */
88  u32 pid, tid; /* if PERF_SAMPLE_TID */
89  u64 time; /* if PERF_SAMPLE_TIME */
90  u64 addr; /* if PERF_SAMPLE_ADDR */
91  u64 id; /* if PERF_SAMPLE_ID */
92  u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
93  u32 cpu, res; /* if PERF_SAMPLE_CPU */
94  u64 period; /* if PERF_SAMPLE_PERIOD */
95  /* if PERF_SAMPLE_READ */
96  u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
97  u64 ips[MAX_CALLCHAIN_FRAMES]; /* if PERF_SAMPLE_CALLCHAIN */
98  u32 size; /* if PERF_SAMPLE_RAW */
99  char *data; /* if PERF_SAMPLE_RAW */
100  /* if PERF_SAMPLE_BRANCH_STACK */
101 
102  /* if PERF_SAMPLE_BRANCH_STACK */
103  u64 abi; /* if PERF_SAMPLE_REGS_USER */
105  /* if PERF_SAMPLE_REGS_USER */
106  u64 stack_size; /* if PERF_SAMPLE_STACK_USER */
107  char *stack_data; /* if PERF_SAMPLE_STACK_USER */
108  u64 stack_dyn_size; /* if PERF_SAMPLE_STACK_USER &&
109  size != 0 */
110  u64 weight; /* if PERF_SAMPLE_WEIGHT */
111  u64 data_src; /* if PERF_SAMPLE_DATA_SRC */
112  u64 transaction;/* if PERF_SAMPLE_TRANSACTION */
113  u64 intr_abi; /* if PERF_SAMPLE_REGS_INTR */
115  /* if PERF_SAMPLE_REGS_INTR */
116 
117  // header information in the buffer
118  u32 header_misc; /* information about the sample */
119  u32 header_type; /* either sample record or other */
120 
121  // only for PERF_RECORD_SWITCH
123 
125 
126 // forward declaratio for custom event
127 struct event_custom_s;
128 
129 // --------------------------------------------------------------
130 // main data structure to store the information of an event.
131 // this structure is designed to be created once during the initialization.
132 // this code doesn't work if the number of events change dynamically.
133 //
134 // this data is designed to be stored in evlist.h's _ev_t.event_info
135 // --------------------------------------------------------------
136 typedef struct event_info_s {
137 
138  struct perf_event_attr attr; // the event attribute
139  struct event_custom_s *metric_custom; // pointer to the predefined metric
140 
141 } event_info_t;
142 
143 
144 typedef struct perf_event_mmap_page pe_mmap_t;
145 
146 
147 // --------------------------------------------------------------
148 // data perf event per thread per event
149 // this data is designed to be used within a thread
150 // --------------------------------------------------------------
151 typedef struct event_thread_s {
152 
153  pe_mmap_t *mmap; // mmap buffer
154  int fd; // file descriptor of the event
155 
157 
158 
159 
160 
161 /******************************************************************************
162  * Interfaces
163  *****************************************************************************/
164 
165 void
167 
168 int
170  const char *event_name,
171  struct perf_event_attr *attr,
172  bool usePeriod,
173  u64 threshold,
174  u64 sampletype
175 );
176 
177 bool
179 
180 int
182 
183 #if KERNEL_SAMPLING_ENABLED
184 cct_node_t *
185 perf_util_add_kernel_callchain( cct_node_t *leaf, void *data_aux);
186 #endif
187 
188 void
190 
191 int
193 
194 #endif
cct_node_t * perf_util_add_kernel_callchain(cct_node_t *leaf, void *data_aux)
Definition: perf-util.c:358
#define MAX_CALLCHAIN_FRAMES
Definition: perf-util.h:65
threshold_e
Definition: perf-util.h:71
pe_mmap_t * mmap
Definition: perf-util.h:153
void perf_util_init()
Definition: perf-util.c:303
struct event_info_s event_info_t
int perf_util_check_precise_ip_suffix(char *event)
int perf_util_attr_init(const char *event_name, struct perf_event_attr *attr, bool usePeriod, u64 threshold, u64 sampletype)
Definition: perf-util.c:403
struct event_custom_s * metric_custom
Definition: perf-util.h:139
static __thread u32 pid
struct perf_event_mmap_page pe_mmap_t
Definition: perf-util.h:144
char * stack_data
Definition: perf-util.h:107
u64 context_switch_time
Definition: perf-util.h:122
__u32 u32
enum threshold_e threshold_type
Definition: perf-util.h:75
int perf_util_get_paranoid_level()
bool perf_util_is_ksym_available()
Definition: perf-util.c:339
__u64 u64
struct event_thread_s event_thread_t
static __thread u32 cpu
Definition: cct.c:96
static int const threshold
struct perf_mmap_data_s perf_mmap_data_t
void perf_util_get_default_threshold(struct event_threshold_s *threshold)
Definition: perf-util.c:347