本文整理汇总了C++中perf_event_open函数的典型用法代码示例。如果您正苦于以下问题:C++ perf_event_open函数的具体用法?C++ perf_event_open怎么用?C++ perf_event_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了perf_event_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: memset
void Performance::_add(perf_type_id type, long long config) {
struct perf_event_attr pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.size = sizeof(struct perf_event_attr);
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
pe.type = type;
pe.config = config;
order[events].type = type;
order[events].config = config;
events++;
if (fd == -1) {
/* if changed: change calculations in get */
pe.read_format = PERF_FORMAT_GROUP;
pe.disabled = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0);
if (fd < 0) {
fprintf(stderr, "Error opening leader for %llx / %llx\n", (long long) type, config);
exit(EXIT_FAILURE);
}
} else {
int ret = perf_event_open(&pe, 0, -1, fd, 0);
if (ret < 0) {
fprintf(stderr, "Error connecting to leader for %llx / %llx\n", (long long) type, config);
exit(EXIT_FAILURE);
}
}
}
示例2: malloc
int *fd_group_init(int num_events, char **events) {
struct perf_event_attr *attr;
int fd, ret, leader_fd, i;
int *fds = malloc(num_events * sizeof(int));
if (!fds) err(1, "could not allocate memory");
attr = calloc(num_events, sizeof(*attr));
// do refs, then misses
for (i = 0; i < num_events; i++) {
/*
* 1st argument: event string
* 2nd argument: default privilege level (used if not specified in the event string)
* 3rd argument: the perf_event_attr to initialize
*/
ret = pfm_get_perf_event_encoding(events[i], PFM_PLM3, &attr[i], NULL, NULL);
if (ret != PFM_SUCCESS) errx(1, "evt %d: cannot find encoding: %s", i, pfm_strerror(ret));
printf("Using encoding %lx for event %s\n", attr[i].config, events[i]);
attr[i].inherit = 0; // inheritance currently doesn't work with FORMAT_GROUP
/*
* request timing information because event may be multiplexed
* and thus it may not count all the time. The scaling information
* will be used to scale the raw count as if the event had run all
* along
*/
attr[i].read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING
| PERF_FORMAT_GROUP;
/* do not start immediately after perf_event_open() */
attr[i].disabled = 1;
/*
* create the event and attach to self
* Note that it attaches only to the main thread, there is no inheritance
* to threads that may be created subsequently.
*
* if mulithreaded, then getpid() must be replaced by gettid()
*/
if (i == 0) {
fd = perf_event_open(&attr[i], 0, -1, -1, 0);
fds[i] = fd;
leader_fd = fd;
} else {
fd = perf_event_open(&attr[i], 0, -1, leader_fd, 0);
fds[i] = fd;
}
if (fd < 0) {
warn("warning: evt %d: cannot create event", i);
free(attr);
free(fds);
return NULL;
}
}
free(attr);
return fds;
}
示例3: setup
static void setup(void)
{
int i;
struct perf_event_attr tsk_event, hw_event;
/*
* According to perf_event_open's manpage, the official way of
* knowing if perf_event_open() support is enabled is checking for
* the existence of the file /proc/sys/kernel/perf_event_paranoid.
*/
if (access("/proc/sys/kernel/perf_event_paranoid", F_OK) == -1)
tst_brkm(TCONF, NULL, "Kernel doesn't have perf_event support");
tst_sig(NOFORK, DEF_HANDLER, cleanup);
TEST_PAUSE;
nhw = count_hardware_counters();
n = nhw + 4;
memset(&hw_event, 0, sizeof(struct perf_event_attr));
memset(&tsk_event, 0, sizeof(struct perf_event_attr));
tsk_event.type = PERF_TYPE_SOFTWARE;
tsk_event.size = sizeof(struct perf_event_attr);
tsk_event.disabled = 1;
tsk_event.config = PERF_COUNT_SW_TASK_CLOCK;
hw_event.type = PERF_TYPE_HARDWARE;
hw_event.size = sizeof(struct perf_event_attr);
hw_event.disabled = 1;
hw_event.config = PERF_COUNT_HW_INSTRUCTIONS;
tsk0 = perf_event_open(&tsk_event, 0, -1, -1, 0);
if (tsk0 == -1) {
tst_brkm(TBROK | TERRNO, cleanup, "perf_event_open failed");
} else {
tsk_event.disabled = 0;
for (i = 0; i < n; ++i) {
hwfd[i] = perf_event_open(&hw_event, 0, -1, -1, 0);
tskfd[i] = perf_event_open(&tsk_event, 0, -1,
hwfd[i], 0);
if (tskfd[i] == -1 || hwfd[i] == -1) {
tst_brkm(TBROK | TERRNO, cleanup,
"perf_event_open failed");
}
}
}
}
示例4: perf_event__open
struct mmap_pages *
perf_event__open(struct event_open *e_open)
{
int fd = -1;
e_open->attr.disabled = 0;
e_open->attr.size = sizeof(e_open->attr);
e_open->attr.sample_period = 1;
e_open->attr.inherit = 1;
e_open->attr.sample_type = PERF_SAMPLE_TIME |
PERF_SAMPLE_RAW |
PERF_SAMPLE_CPU |
PERF_SAMPLE_TID ;
e_open->attr.__reserved_1 = 0;
e_open->attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING ;
fd = perf_event_open(&e_open->attr,e_open->pid,e_open->cpu \
,e_open->group_id,e_open->flags);
if(fd < 0) {
fprintf(stderr,"Failed to open event FD\n");
return NULL;
}
struct mmap_pages *mmap_pages = NULL;
mmap_pages = mmap_pages__new(fd,8);
if(mmap_pages != NULL) {
mmap_pages->attr = e_open->attr;
fcntl(fd, F_SETFL, O_NONBLOCK);
}
return mmap_pages;
}
示例5: main
int main(int ac, char **av)
{
int test = 0;
int ret = 0;
while (*++av) {
if (!strcmp(*av, "--test")) {
test = 1;
continue;
}
struct perf_event_attr attr;
if (resolve_event(*av, &attr) < 0) {
fprintf(stderr, "Cannot resolve %s\n", *av);
ret = 1;
continue;
}
char *ev = format_raw_event(&attr, *av);
printf("%s\n", ev);
free(ev);
if (test) {
if (perf_event_open(&attr, 0, -1, -1, 0) < 0)
perror("perf_event_open");
}
}
return ret;
}
示例6: perf_start
void perf_start(const char* kname) {
if (!enabled) return;
char* prof_envvar = getenv("MXPA_PROFILE");
int tids[32];
int ntid;
get_tids(tids, &ntid);
g_nthreads = ntid;
int n;
for (n = 0; n < ntid; n++) {
int ret;
ret = perf_setup_list_events(prof_envvar, &(g_fds[n]), &num_fds);
perf_event_desc_t *fds = g_fds[n];
int cpu = -1;
int group_fd = -1;
int pid = tids[n];
fds[0].fd = -1;
int i;
for(i=0; i < num_fds; i++) {
fds[i].hw.read_format = PERF_FORMAT_SCALE;
fds[i].hw.disabled = 1; /* do not start now */
fds[i].hw.inherit = 1; /* XXX child process will inherit, when forked only? */
/* each event is in an independent group (multiplexing likely) */
fds[i].fd = perf_event_open(&fds[i].hw, pid, cpu, group_fd, 0);
if (fds[i].fd == -1) {
fprintf(stderr, "cannot open event %d\n", i);
exit(2);
}
}
}
prctl(PR_TASK_PERF_EVENTS_ENABLE);
}
示例7: perfevent_initialize
void perfevent_initialize(char*events) {
int i, ret;
perfevent_events = strdup(events);
/*
* Initialize pfm library (required before we can use it)
*/
ret = pfm_initialize();
if (ret != PFM_SUCCESS)
errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
ret = perf_setup_list_events(events, &perfevent_fds, &perfevent_num_fds);
if (ret || !perfevent_num_fds)
errx(1, "cannot setup events");
perfevent_fds[0].fd = -1;
for(i=0; i < perfevent_num_fds; i++) {
/* request timing information necessary for scaling */
perfevent_fds[i].hw.read_format = PERF_FORMAT_SCALE;
perfevent_fds[i].hw.disabled = (i == 0); /* do not start now */
perfevent_fds[i].hw.inherit = 1; /* pass on to child threads */
/* each event is in an independent group (multiplexing likely) */
perfevent_fds[i].fd = perf_event_open(&perfevent_fds[i].hw, 0, -1, perfevent_fds[0].fd, 0);
if (perfevent_fds[i].fd == -1)
err(1, "cannot open event %d", i);
}
}
示例8: parent
void parent(int sock) {
ssize_t size;
int fd,i;
struct perf_event_attr pe;
memset(&pe,0,sizeof(struct perf_event_attr));
pe.type=PERF_TYPE_HARDWARE;
pe.size=sizeof(struct perf_event_attr);
pe.config=PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled=1;
pe.exclude_kernel=1;
pe.exclude_hv=1;
arch_adjust_domain(&pe,quiet);
fd=perf_event_open(&pe,0,-1,-1,0);
if (fd<0) {
fprintf(stderr,"Error opening leader %llx %s\n",pe.config,strerror(errno));
test_fail(test_string);
}
size = sock_fd_write(sock, "1", 1, fd);
if (!quiet) printf ("wrote fd %d (size %d)\n", fd, (int)size);
sleep(1);
for(i=0;i<20;i++) instructions_million();
}
示例9: setup_event
int setup_event(struct event *e, int cpu, struct event *leader,
bool measure_all, int measure_pid)
{
e->attr.inherit = 1;
if (!measure_all) {
e->attr.disabled = 1;
e->attr.enable_on_exec = 1;
}
e->attr.read_format |= PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING;
e->efd[cpu].fd = perf_event_open(&e->attr,
measure_all ? -1 : measure_pid,
cpu,
leader ? leader->efd[cpu].fd : -1,
0);
if (e->efd[cpu].fd < 0) {
/* Handle offline CPU */
if (errno == EINVAL && !cpu_online(cpu))
return 0;
fprintf(stderr, "Cannot open perf event for %s/%d: %s\n",
e->event, cpu, strerror(errno));
return -1;
}
return 0;
}
示例10: main
int main(int argc, char **argv) {
int fd;
struct perf_event_attr pe;
int i;
int result;
long long counts[1],prev=0;
long long total=0,average,max=0,min=0x7ffffffffffffffULL;
memset(&pe,0,sizeof(struct perf_event_attr));
pe.type=PERF_TYPE_HARDWARE;
pe.size=sizeof(struct perf_event_attr);
pe.config=PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled=1;
pe.exclude_kernel=1;
pe.exclude_hv=1;
fd=perf_event_open(&pe,0,-1,-1,0);
if (fd<0) {
fprintf(stderr,"Error opening leader %llx %s\n",pe.config,strerror(errno));
exit(1);
}
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE,0);
for(i=0;i<NUM_RUNS;i++) {
result=instructions_million();
result=read(fd,&counts,sizeof(long long));
results[i]=counts[0]-prev;
prev=counts[0];
}
ioctl(fd, PERF_EVENT_IOC_DISABLE,0);
close(fd);
for(i=0;i<NUM_RUNS;i++) {
total+=results[i];
if (results[i]>max) max=results[i];
if (results[i]<min) min=results[i];
}
average=total/NUM_RUNS;
printf("Average=%lld max=%lld min=%lld\n",average,max,min);
(void) result;
return 0;
}
示例11: measure
int
measure(pid_t pid)
{
perf_event_desc_t *fds = NULL;
int i, ret, num_fds = 0;
char fn[32];
if (pfm_initialize() != PFM_SUCCESS)
errx(1, "libpfm initialization failed\n");
ret = perf_setup_list_events(options.events, &fds, &num_fds);
if (ret || (num_fds == 0))
exit(1);
fds[0].fd = -1;
for(i=0; i < num_fds; i++) {
fds[i].hw.disabled = 0; /* start immediately */
/* request timing information necessary for scaling counts */
fds[i].hw.read_format = PERF_FORMAT_SCALE;
fds[i].hw.pinned = !i && options.pinned;
fds[i].fd = perf_event_open(&fds[i].hw, pid, -1, (options.group? fds[0].fd : -1), 0);
if (fds[i].fd == -1)
errx(1, "cannot attach event %s", fds[i].name);
}
/*
* no notification is generated by perf_counters
* when the monitored thread exits. Thus we need
* to poll /proc/ to detect it has disappeared,
* otherwise we have to wait until the end of the
* timeout
*/
sprintf(fn, "/proc/%d/status", pid);
while(access(fn, F_OK) == 0 && options.delay) {
sleep(1);
options.delay--;
if (options.print)
print_counts(fds, num_fds, 1);
}
if (options.delay)
warn("thread %d terminated before timeout", pid);
if (!options.print)
print_counts(fds, num_fds, 0);
for(i=0; i < num_fds; i++)
close(fds[i].fd);
free(fds);
/* free libpfm resources cleanly */
pfm_terminate();
return 0;
}
示例12: main
int main()
{
#ifdef __NR_perf_event_open
struct perf_event_attr pe;
int fd;
long long count;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_SOFTWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_SW_CPU_CLOCK;
pe.disabled = 0;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0);
//staptest// perf_event_open (XXXX, 0, -1, -1, 0x0) = NNNN
sleep(1);
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(count));
// Limit testing.
fd = perf_event_open((struct perf_event_attr*)-1, 0, -1, -1, 0);
#ifdef __s390__
//staptest// perf_event_open (0x[7]?[f]+, 0, -1, -1, 0x0) = -NNNN
#else
//staptest// perf_event_open (0x[f]+, 0, -1, -1, 0x0) = -NNNN
#endif
fd = perf_event_open(&pe, -1, -1, -1, 0);
//staptest// perf_event_open (XXXX, -1, -1, -1, 0x0) = -NNNN
fd = perf_event_open(&pe, 0, -1, -1, -1);
//staptest// perf_event_open (XXXX, 0, -1, -1, PERF_FLAG_[^ ]+|XXXX) = -NNNN
close(fd);
#endif
return 0;
}
示例13: setupHandler
int setupHandler(int sig) {
// Perf event settings
struct perf_event_attr pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HW_CACHE;
pe.size = sizeof(struct perf_event_attr);
pe.config = (PERF_COUNT_HW_CACHE_L1D << 0) | (PERF_COUNT_HW_CACHE_OP_READ << 8) | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
//pe.config = (PERF_COUNT_HW_CACHE_L1D << 0) | (PERF_COUNT_HW_CACHE_OP_WRITE << 8) | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16);
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
// pe.exclude_callchain_kernel = 1;
// pe.exclude_idle = 1;
pe.sample_period = 1000;
// pe.sample_id_all = 1;
pe.sample_type=sample_type;
// pe.precise_ip = 0;
// pe.read_format=read_format;
// Record those information when trap happens
//pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR;
// Create the perf_event for this thread on all CPUs with no event group
int perf_fd = perf_event_open(&pe, 0, -1, -1, 0);
if(perf_fd == -1) {
fprintf(stderr, "Failed to open perf event file: %s\n", strerror(errno));
abort();
}
// Setting up 9 pages to pass information about a trap
our_mmap=mmap(NULL, 9*4096,
PROT_READ|PROT_WRITE, MAP_SHARED, perf_fd, 0);
// Set the perf_event file to async mode
if(fcntl(perf_fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
fprintf(stderr, "Failed to set perf event file to ASYNC mode: %s\n", strerror(errno));
abort();
}
// Tell the file to send a SIGUSR1 when an event occurs
if(fcntl(perf_fd, F_SETSIG, sig) == -1) {
fprintf(stderr, "Failed to set perf event file's async signal: %s\n", strerror(errno));
abort();
}
// Deliver the signal to this thread
if(fcntl(perf_fd, F_SETOWN, getpid()) == -1) {
fprintf(stderr, "Failed to set the owner of the perf event file: %s\n", strerror(errno));
abort();
}
return perf_fd;
}
示例14: perf_init
int perf_init(unsigned int num_ctrs, ...) {
va_list args;
int ctr, ret = 0;
number = num_ctrs;
attr = (struct perf_event_attr*)calloc(num_ctrs, sizeof(struct perf_event_attr));
fds = (int*) malloc(sizeof(int) * num_ctrs);
if (attr == NULL || fds == NULL) {
perror("Could not allocate space for counter data: ");
ret = -1;
goto error;
}
//Configure each counter, staring with 1
va_start(args, num_ctrs);
for (ctr=0; ctr<num_ctrs; ctr++) {
unsigned event, conf;
conf = va_arg(args, unsigned int) & 0xFFFFFF;
event = conf & 0xFFFF;
attr[ctr].type = PERF_TYPE_RAW;
attr[ctr].config = event; /* generic PMU event*/
attr[ctr].disabled = 0;
attr[ctr].pinned = 1;
attr[ctr].inherit = 1;
fds[ctr] = perf_event_open(&attr[ctr], getpid(), -1, -1, 0);
if ((conf & PERFMON_EVENTSEL_OS) == 0)
attr[ctr].exclude_kernel = 1;
if ((conf & PERFMON_EVENTSEL_USR) == 0)
attr[ctr].exclude_user= 1;
if (fds[ctr] < 0) {
perror("Opening performance counter");
goto error;
}
}
va_end(args);
return ret;
error:
number = 0;
if (attr) {
free(attr);
attr = NULL;
}
if (fds) {
free(fds);
fds = NULL;
}
return ret;
}
示例15: perf_open
static int perf_open(void)
{
struct perf_event_attr attr;
memset(&attr, 0, sizeof (attr));
attr.type = i915_type_id();
if (attr.type == 0)
return -ENOENT;
attr.config = I915_PERF_ENERGY;
attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED;
return perf_event_open(&attr, -1, 0, -1, 0);
}