Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[b187c7]: perf_examples / notify_self.c Maximize Restore History

Download this file

notify_self.c    261 lines (222 with data), 7.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* notify_self.c - example of how you can use overflow notifications
*
* Copyright (c) 2009 Google, Inc
* Contributed by Stephane Eranian <eranian@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <err.h>
#include <locale.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "perf_util.h"
#define SMPL_PERIOD 2400000000ULL
static volatile unsigned long notification_received;
static perf_event_desc_t *fds = NULL;
static int num_fds = 0;
static int buffer_pages = 1; /* size of buffer payload (must be power of 2)*/
static void
sigio_handler(int n, siginfo_t *info, void *uc)
{
struct perf_event_header ehdr;
int ret, id;
/*
* positive si_code indicate kernel generated signal
* which is normal for SIGIO
*/
if (info->si_code < 0)
errx(1, "signal not generated by kernel");
/*
* SIGPOLL = SIGIO
* expect POLL_HUP instead of POLL_IN because we are
* in one-shot mode (IOC_REFRESH)
*/
if (info->si_code != POLL_HUP)
errx(1, "signal not generated by SIGIO");
id = perf_fd2event(fds, num_fds, info->si_fd);
if (id == -1)
errx(1, "no event associated with fd=%d", info->si_fd);
ret = perf_read_buffer(fds+id, &ehdr, sizeof(ehdr));
if (ret)
errx(1, "cannot read event header");
if (ehdr.type != PERF_RECORD_SAMPLE) {
warnx("unexpected sample type=%d, skipping\n", ehdr.type);
perf_skip_buffer(fds+id, ehdr.size);
goto skip;
}
printf("Notification:%lu ", notification_received);
ret = perf_display_sample(fds, num_fds, 0, &ehdr, stdout);
/*
* increment our notification counter
*/
notification_received++;
skip:
/*
* rearm the counter for one more shot
*/
ret = ioctl(info->si_fd, PERF_EVENT_IOC_REFRESH, 1);
if (ret == -1)
err(1, "cannot refresh");
}
/*
* infinite loop waiting for notification to get out
*/
void
busyloop(void)
{
/*
* busy loop to burn CPU cycles
*/
for(;notification_received < 20;) ;
}
int
main(int argc, char **argv)
{
struct sigaction act;
uint64_t *val;
size_t sz, pgsz;
int ret, i;
setlocale(LC_ALL, "");
ret = pfm_initialize();
if (ret != PFM_SUCCESS)
errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
pgsz = sysconf(_SC_PAGESIZE);
/*
* Install the signal handler (SIGIO)
* need SA_SIGINFO because we need the fd
* in the signal handler
*/
memset(&act, 0, sizeof(act));
act.sa_sigaction = sigio_handler;
act.sa_flags = SA_SIGINFO;
sigaction (SIGIO, &act, 0);
/*
* allocates fd for us
*/
ret = perf_setup_list_events("cycles,"
"instructions",
&fds, &num_fds);
if (ret || (num_fds == 0))
exit(1);
fds[0].fd = -1;
for(i=0; i < num_fds; i++) {
/* want a notification for every each added to the buffer */
fds[i].hw.disabled = !i;
if (!i) {
fds[i].hw.wakeup_events = 1;
fds[i].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_READ|PERF_SAMPLE_PERIOD;
fds[i].hw.sample_period = SMPL_PERIOD;
/* read() returns event identification for signal handler */
fds[i].hw.read_format = PERF_FORMAT_GROUP|PERF_FORMAT_ID|PERF_FORMAT_SCALE;
}
fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, fds[0].fd, 0);
if (fds[i].fd == -1)
err(1, "cannot attach event %s", fds[i].name);
}
sz = (3+2*num_fds)*sizeof(uint64_t);
val = malloc(sz);
if (!val)
err(1, "cannot allocated memory");
/*
* On overflow, the non lead events are stored in the sample.
* However we need some key to figure the order in which they
* were laid out in the buffer. The file descriptor does not
* work for this. Instead, we extract a unique ID for each event.
* That id will be part of the sample for each event value.
* Therefore we will be able to match value to events
*
* PERF_FORMAT_ID: returns unique 64-bit identifier in addition
* to event value.
*/
if (fds[0].fd == -1)
errx(1, "cannot create event 0");
ret = read(fds[0].fd, val, sz);
if (ret == -1)
err(1, "cannot read id %zu", sizeof(val));
/*
* we are using PERF_FORMAT_GROUP, therefore the structure
* of val is as follows:
*
* { u64 nr;
* { u64 time_enabled; } && PERF_FORMAT_ENABLED
* { u64 time_running; } && PERF_FORMAT_RUNNING
* { u64 value;
* { u64 id; } && PERF_FORMAT_ID
* } cntr[nr];
* We are skipping the first 3 values (nr, time_enabled, time_running)
* and then for each event we get a pair of values.
*/
for(i=0; i < num_fds; i++) {
fds[i].id = val[2*i+1+3];
printf("%"PRIu64" %s\n", fds[i].id, fds[i].name);
}
fds[0].buf = mmap(NULL, (buffer_pages+1)*pgsz, PROT_READ|PROT_WRITE, MAP_SHARED, fds[0].fd, 0);
if (fds[0].buf == MAP_FAILED)
err(1, "cannot mmap buffer");
fds[0].pgmsk = (buffer_pages * pgsz) - 1;
/*
* setup asynchronous notification on the file descriptor
*/
ret = fcntl(fds[0].fd, F_SETFL, fcntl(fds[0].fd, F_GETFL, 0) | O_ASYNC);
if (ret == -1)
err(1, "cannot set ASYNC");
/*
* necessary if we want to get the file descriptor for
* which the SIGIO is sent in siginfo->si_fd.
* SA_SIGINFO in itself is not enough
*/
ret = fcntl(fds[0].fd, F_SETSIG, SIGIO);
if (ret == -1)
err(1, "cannot setsig");
/*
* get ownership of the descriptor
*/
ret = fcntl(fds[0].fd, F_SETOWN, getpid());
if (ret == -1)
err(1, "cannot setown");
/*
* enable the group for one period
*/
ret = ioctl(fds[0].fd, PERF_EVENT_IOC_REFRESH , 1);
if (ret == -1)
err(1, "cannot refresh");
busyloop();
ret = ioctl(fds[0].fd, PERF_EVENT_IOC_DISABLE, 1);
if (ret == -1)
err(1, "cannot disable");
/*
* destroy our session
*/
for(i=0; i < num_fds; i++)
close(fds[i].fd);
perf_free_fds(fds, num_fds);
free(val);
/* free libpfm resources cleanly */
pfm_terminate();
return 0;
}