HPCToolkit
event_custom.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // --------------------------------------------------------------------------
6 // Part of HPCToolkit (hpctoolkit.org)
7 //
8 // Information about sources of support for research and development of
9 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
10 // --------------------------------------------------------------------------
11 //
12 // Copyright ((c)) 2002-2019, Rice University
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions are
17 // met:
18 //
19 // * Redistributions of source code must retain the above copyright
20 // notice, this list of conditions and the following disclaimer.
21 //
22 // * Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 //
26 // * Neither the name of Rice University (RICE) nor the names of its
27 // contributors may be used to endorse or promote products derived from
28 // this software without specific prior written permission.
29 //
30 // This software is provided by RICE and contributors "as is" and any
31 // express or implied warranties, including, but not limited to, the
32 // implied warranties of merchantability and fitness for a particular
33 // purpose are disclaimed. In no event shall RICE or contributors be
34 // liable for any direct, indirect, incidental, special, exemplary, or
35 // consequential damages (including, but not limited to, procurement of
36 // substitute goods or services; loss of use, data, or profits; or
37 // business interruption) however caused and on any theory of liability,
38 // whether in contract, strict liability, or tort (including negligence
39 // or otherwise) arising in any way out of the use of this software, even
40 // if advised of the possibility of such damage.
41 //
42 // ******************************************************* EndRiceCopyright *
43 
44 
45 //***************************************************************************
46 // user include files
47 //***************************************************************************
48 
49 #include "include/queue.h" // Singly-linkled list macros
50 
51 #include "sample-sources/display.h"
52 
53 #include "event_custom.h"
54 
55 //*************************** type data structure **************************
56 
57 // list of callbacks
58 typedef struct events_list_s {
60  SLIST_ENTRY(events_list_s) entries;
62 
63 
64 //*************************** Local variables **************************
65 
66 static SLIST_HEAD(event_list_head, events_list_s) list_events_head =
67  SLIST_HEAD_INITIALIZER(event_list_head);
68 
69 
70 //*************************** Interfaces **************************
71 
73 event_custom_find(const char *name)
74 {
75  events_list_t *item = NULL;
76 
77  // check if we already have the event
78  SLIST_FOREACH(item, &list_events_head, entries) {
79  if (item != NULL && strcasecmp(item->event->name, name)==0)
80  return item->event;
81  }
82  return NULL;
83 }
84 
85 void
87 {
88  if (SLIST_EMPTY(&list_events_head)) {
89  return;
90  }
91  events_list_t *item = NULL;
92 
93  display_header(stdout, "Customized perf-event based events");
94  fprintf(std, "Name\t\tDescription\n");
95  display_line_single(stdout);
96 
97  // check if we already have the event
98  SLIST_FOREACH(item, &list_events_head, entries) {
99  if (item != NULL) {
100  display_event_info(stdout, item->event->name, item->event->desc);
101  }
102  }
103  fprintf(std, "\n");
104 }
105 
106 // --------------------------------------------------------------
107 // Adding a new event on the head without checking if it already exists or not
108 // --------------------------------------------------------------
109 int
111 {
112  events_list_t item;
113  item.event = event_custom_find(event->name);
114  if (item.event != NULL)
115  return 0;
116 
117  events_list_t *list_item = (events_list_t *) hpcrun_malloc(sizeof(events_list_t));
118  if (list_item == NULL)
119  return -1;
120 
121  list_item->event = event;
122 
123  SLIST_INSERT_HEAD(&list_events_head, list_item, entries);
124  return 1;
125 }
126 
127 // --------------------------------------------------------------
128 // Create a custom event
129 // --------------------------------------------------------------
135 int
137 {
138  event_custom_t *event = event_custom_find(name);
139 
140  if (event == NULL) {
141  return 0;
142  }
143 
144  struct event_threshold_s default_threshold;
145  perf_util_get_default_threshold( &default_threshold );
146 
147  return event->register_fn(self, event, &default_threshold);
148 }
149 
150 int
152 {
153  if (args == NULL || args->current == NULL)
154  return 0;
155 
156  events_list_t *item = NULL;
157 
158  SLIST_FOREACH(item, &list_events_head, entries) {
159  if (item != NULL) {
160  // if the custom event is inclusive, we call the handler
161  // even if this is not its event
162  // if the custom event is exclusive, we call the handler
163  // iff the event is from the custom event
164 
165  if (item->event->handle_type == INCLUSIVE) {
166  item->event->handler_fn(args);
167 
168  } else if (item->event == args->current->metric_custom) {
169  // exclusive event: make sure the event is the same is the current event
170  item->event->handler_fn(args);
171  }
172  }
173  }
174  return 0;
175 }
176 
177 
struct event_info_s * current
Definition: event_custom.h:64
#define SLIST_HEAD_INITIALIZER(head)
Definition: queue.h:190
event_custom_t * event
Definition: event_custom.c:59
event_handler_t * handler_fn
Definition: event_custom.h:89
struct event_custom_s * metric_custom
Definition: perf-util.h:139
int event_custom_handler(event_handler_arg_t *args)
Definition: event_custom.c:151
void display_event_info(FILE *output, const char *event, const char *desc)
Definition: display.c:142
struct events_list_s events_list_t
void perf_util_get_default_threshold(struct event_threshold_s *threshold)
Definition: perf-util.c:347
int event_custom_create_event(sample_source_t *self, char *name)
Definition: event_custom.c:136
#define SLIST_EMPTY(head)
Definition: queue.h:229
void display_line_single(FILE *output)
Definition: display.c:115
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
const char * desc
Definition: event_custom.h:86
event_custom_t * event_custom_find(const char *name)
void event_custom_display(FILE *std)
Definition: event_custom.c:86
#define NULL
Definition: ElfHelper.cpp:85
#define SLIST_FOREACH(var, head, field)
Definition: queue.h:233
event_handle_type_t handle_type
Definition: event_custom.h:91
static SLIST_HEAD(event_list_head, events_list_s)
Definition: event_custom.c:66
void display_header(FILE *output, const char *title)
Definition: display.c:127
const char * name
Definition: event_custom.h:85
#define SLIST_ENTRY(type)
Definition: queue.h:193
int event_custom_register(event_custom_t *event)
Definition: event_custom.c:110
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:267