op_perf_events_checker.c

Go to the documentation of this file.
00001 /*
00002  * @file op-perf-events-checker.c
00003  *
00004  * Utility program for determining the existence and functionality
00005  * of the Linux kernel's Performance Events Subsystem.
00006  *
00007  * @remark Copyright 2011 OProfile authors
00008  * @remark Read the file COPYING
00009  *
00010  * Created on: Dec 7, 2011
00011  * @author Maynard Johnson
00012  * (C) Copyright IBM Corp. 2011
00013  *
00014  */
00015 
00016 #include <sys/types.h>
00017 #include <unistd.h>
00018 #include <asm/unistd.h>
00019 #include <stdio.h>
00020 #include <errno.h>
00021 #include <string.h>
00022 #include "config.h"
00023 #if HAVE_PERF_EVENTS
00024 #include <linux/perf_event.h>
00025     struct perf_event_attr attr;
00026     pid_t pid ;
00027 #endif
00028 
00029 static void usage(void)
00030 {
00031     fprintf(stderr, "usage: op-check-perfevents [OPTION]\n");
00032     fprintf(stderr, "\t-h, --help\t\tPrint this help message\n");
00033     fprintf(stderr, "\t-v, --verbose\t\tPrint errno value of perf_event_open syscall\n");
00034 }
00035 
00036 int main(int argc, char **argv)
00037 {
00038     int _verbose = 0;
00039     if (argc > 1) {
00040         if ((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help"))) {
00041             usage();
00042             return 0;
00043         } else if ((!strcmp(argv[1], "-v")) || (!strcmp(argv[1], "--verbose"))) {
00044             _verbose = 1;
00045         } else {
00046             usage();
00047             return -1;
00048         }
00049     }
00050 
00051 #if HAVE_PERF_EVENTS
00052     /* If perf_events syscall is not implemented, the syscall below will fail
00053      * with ENOSYS (38).  If implemented, but the processor type on which this
00054      * program is running is not supported by perf_events, the syscall returns
00055      * ENOENT (2).
00056      */
00057         memset(&attr, 0, sizeof(attr));
00058         attr.size = sizeof(attr);
00059         attr.sample_type = PERF_SAMPLE_IP;
00060 
00061     pid = getpid();
00062     syscall(__NR_perf_event_open, &attr, pid, 0, -1, 0);
00063     if (_verbose)
00064         fprintf(stderr, "perf_event_open syscall returned %s\n", strerror(errno));
00065     return errno;
00066 #else
00067     return -1;
00068 #endif
00069 }
00070 

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1