perf_event_lib.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pe_event_info_t
struct  pe_control_t
struct  pe_context_t

Defines

#define PERF_EVENT_MAX_MPX_COUNTERS   192

Functions

int _pe_shutdown_thread (hwd_context_t *ctx)
int _pe_reset (hwd_context_t *ctx, hwd_control_state_t *ctl)
int _pe_write (hwd_context_t *ctx, hwd_control_state_t *ctl, long long *from)
int _pe_read (hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
int _pe_start (hwd_context_t *ctx, hwd_control_state_t *ctl)
int _pe_stop (hwd_context_t *ctx, hwd_control_state_t *ctl)
int _pe_ctl (hwd_context_t *ctx, int code, _papi_int_option_t *option)

Define Documentation

#define PERF_EVENT_MAX_MPX_COUNTERS   192

Definition at line 5 of file perf_event_lib.h.


Function Documentation

int _pe_ctl ( hwd_context_t ctx,
int  code,
_papi_int_option_t option 
)

Definition at line 1351 of file perf_event.c.

01352 {
01353    int ret;
01354    pe_context_t *pe_ctx = ( pe_context_t *) ctx;
01355    pe_control_t *pe_ctl = NULL;
01356 
01357    switch ( code ) {
01358       case PAPI_MULTIPLEX:
01359        pe_ctl = ( pe_control_t * ) ( option->multiplex.ESI->ctl_state );
01360        ret = check_permissions( pe_ctl->tid, pe_ctl->cpu, pe_ctl->domain,
01361                     pe_ctl->granularity,
01362                     1, pe_ctl->inherit );
01363            if (ret != PAPI_OK) {
01364           return ret;
01365        }
01366 
01367        /* looks like we are allowed, so set multiplexed attribute */
01368        pe_ctl->multiplexed = 1;
01369        ret = _pe_update_control_state( pe_ctl, NULL,
01370                         pe_ctl->num_events, pe_ctx );
01371        if (ret != PAPI_OK) {
01372           pe_ctl->multiplexed = 0;
01373        }
01374        return ret;
01375 
01376       case PAPI_ATTACH:
01377        pe_ctl = ( pe_control_t * ) ( option->attach.ESI->ctl_state );
01378        ret = check_permissions( option->attach.tid, pe_ctl->cpu,
01379                   pe_ctl->domain, pe_ctl->granularity,
01380                   pe_ctl->multiplexed,
01381                     pe_ctl->inherit );
01382        if (ret != PAPI_OK) {
01383           return ret;
01384        }
01385 
01386        pe_ctl->tid = option->attach.tid;
01387 
01388        /* If events have been already been added, something may */
01389        /* have been done to the kernel, so update */
01390        ret =_pe_update_control_state( pe_ctl, NULL,
01391                         pe_ctl->num_events, pe_ctx);
01392 
01393        return ret;
01394 
01395       case PAPI_DETACH:
01396        pe_ctl = ( pe_control_t *) ( option->attach.ESI->ctl_state );
01397 
01398        pe_ctl->tid = 0;
01399        return PAPI_OK;
01400 
01401       case PAPI_CPU_ATTACH:
01402        pe_ctl = ( pe_control_t *) ( option->cpu.ESI->ctl_state );
01403        ret = check_permissions( pe_ctl->tid, option->cpu.cpu_num,
01404                     pe_ctl->domain, pe_ctl->granularity,
01405                     pe_ctl->multiplexed,
01406                     pe_ctl->inherit );
01407            if (ret != PAPI_OK) {
01408            return ret;
01409        }
01410        /* looks like we are allowed so set cpu number */
01411 
01412        /* this tells the kernel not to count for a thread   */
01413        /* should we warn if we try to set both?  perf_event */
01414        /* will reject it.                                   */
01415        pe_ctl->tid = -1;
01416 
01417        pe_ctl->cpu = option->cpu.cpu_num;
01418 
01419        return PAPI_OK;
01420 
01421       case PAPI_DOMAIN:
01422        pe_ctl = ( pe_control_t *) ( option->domain.ESI->ctl_state );
01423        ret = check_permissions( pe_ctl->tid, pe_ctl->cpu,
01424                     option->domain.domain,
01425                     pe_ctl->granularity,
01426                     pe_ctl->multiplexed,
01427                     pe_ctl->inherit );
01428            if (ret != PAPI_OK) {
01429           return ret;
01430        }
01431        /* looks like we are allowed, so set event set level counting domains */
01432        pe_ctl->domain = option->domain.domain;
01433        return PAPI_OK;
01434 
01435       case PAPI_GRANUL:
01436        pe_ctl = (pe_control_t *) ( option->granularity.ESI->ctl_state );
01437 
01438        /* FIXME: we really don't support this yet */
01439 
01440            switch ( option->granularity.granularity  ) {
01441               case PAPI_GRN_PROCG:
01442               case PAPI_GRN_SYS_CPU:
01443               case PAPI_GRN_PROC:
01444            return PAPI_ECMP;
01445 
01446           /* Currently we only support thread and CPU granularity */
01447               case PAPI_GRN_SYS:
01448            pe_ctl->granularity=PAPI_GRN_SYS;
01449            pe_ctl->cpu=_papi_getcpu();
01450            break;
01451 
01452               case PAPI_GRN_THR:
01453            pe_ctl->granularity=PAPI_GRN_THR;
01454            break;
01455 
01456 
01457               default:
01458            return PAPI_EINVAL;
01459        }
01460            return PAPI_OK;
01461 
01462       case PAPI_INHERIT:
01463        pe_ctl = (pe_control_t *) ( option->inherit.ESI->ctl_state );
01464        ret = check_permissions( pe_ctl->tid, pe_ctl->cpu, pe_ctl->domain,
01465                   pe_ctl->granularity, pe_ctl->multiplexed,
01466                     option->inherit.inherit );
01467            if (ret != PAPI_OK) {
01468           return ret;
01469        }
01470        /* looks like we are allowed, so set the requested inheritance */
01471        if (option->inherit.inherit) {
01472           /* children will inherit counters */
01473           pe_ctl->inherit = 1;
01474        } else {
01475           /* children won't inherit counters */
01476           pe_ctl->inherit = 0;
01477        }
01478        return PAPI_OK;
01479 
01480       case PAPI_DATA_ADDRESS:
01481        return PAPI_ENOSUPP;
01482 #if 0
01483        pe_ctl = (pe_control_t *) (option->address_range.ESI->ctl_state);
01484        ret = set_default_domain( pe_ctl, option->address_range.domain );
01485        if ( ret != PAPI_OK ) {
01486           return ret;
01487        }
01488        set_drange( pe_ctx, pe_ctl, option );
01489        return PAPI_OK;
01490 #endif
01491       case PAPI_INSTR_ADDRESS:
01492        return PAPI_ENOSUPP;
01493 #if 0
01494        pe_ctl = (pe_control_t *) (option->address_range.ESI->ctl_state);
01495        ret = set_default_domain( pe_ctl, option->address_range.domain );
01496        if ( ret != PAPI_OK ) {
01497           return ret;
01498        }
01499        set_irange( pe_ctx, pe_ctl, option );
01500        return PAPI_OK;
01501 #endif
01502 
01503       case PAPI_DEF_ITIMER:
01504        /* What should we be checking for here?                   */
01505        /* This seems like it should be OS-specific not component */
01506        /* specific.                                              */
01507 
01508        return PAPI_OK;
01509 
01510       case PAPI_DEF_MPX_NS:
01511        /* Defining a given ns per set is not current supported */
01512        return PAPI_ENOSUPP;
01513 
01514       case PAPI_DEF_ITIMER_NS:
01515        /* We don't support this... */
01516        return PAPI_OK;
01517 
01518       default:
01519        return PAPI_ENOSUPP;
01520    }
01521 }

Here is the call graph for this function:

int _pe_read ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long **  events,
int  flags 
)

Definition at line 942 of file perf_event.c.

00944 {
00945     SUBDBG("ENTER: ctx: %p, ctl: %p, events: %p, flags: %#x\n", ctx, ctl, events, flags);
00946 
00947    ( void ) flags;           /*unused */
00948    int i, ret = -1;
00949    pe_context_t *pe_ctx = ( pe_context_t *) ctx;
00950    pe_control_t *pe_ctl = ( pe_control_t *) ctl;
00951    long long papi_pe_buffer[READ_BUFFER_SIZE];
00952    long long tot_time_running, tot_time_enabled, scale;
00953 
00954    /* On kernels before 2.6.33 the TOTAL_TIME_ENABLED and TOTAL_TIME_RUNNING */
00955    /* fields are always 0 unless the counter is disabled.  So if we are on   */
00956    /* one of these kernels, then we must disable events before reading.      */
00957 
00958    /* Elsewhere though we disable multiplexing on kernels before 2.6.34 */
00959    /* so maybe this isn't even necessary.                               */
00960 
00961    if (bug_sync_read()) {
00962       if ( pe_ctx->state & PERF_EVENTS_RUNNING ) {
00963          for ( i = 0; i < pe_ctl->num_events; i++ ) {
00964         /* disable only the group leaders */
00965         if ( pe_ctl->events[i].group_leader_fd == -1 ) {
00966            ret = ioctl( pe_ctl->events[i].event_fd, 
00967                PERF_EVENT_IOC_DISABLE, NULL );
00968            if ( ret == -1 ) {
00969               PAPIERROR("ioctl(PERF_EVENT_IOC_DISABLE) "
00970                "returned an error: ", strerror( errno ));
00971               return PAPI_ESYS;
00972            }
00973         }
00974      }
00975       }
00976    }
00977 
00978 
00979    /* Handle case where we are multiplexing */
00980    if (pe_ctl->multiplexed) {
00981 
00982       /* currently we handle multiplexing by having individual events */
00983       /* so we read from each in turn.                                */
00984 
00985       for ( i = 0; i < pe_ctl->num_events; i++ ) {
00986 
00987          ret = read( pe_ctl->events[i].event_fd, papi_pe_buffer,
00988             sizeof ( papi_pe_buffer ) );
00989          if ( ret == -1 ) {
00990         PAPIERROR("read returned an error: ", strerror( errno ));
00991         return PAPI_ESYS;
00992      }
00993 
00994      /* We should read 3 64-bit values from the counter */
00995      if (ret<(signed)(3*sizeof(long long))) {
00996         PAPIERROR("Error!  short read");
00997         return PAPI_ESYS;
00998      }
00999 
01000          SUBDBG("read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
01001             pe_ctl->events[i].event_fd,
01002         (long)pe_ctl->tid, pe_ctl->events[i].cpu, ret);
01003          SUBDBG("read: %lld %lld %lld\n",papi_pe_buffer[0],
01004             papi_pe_buffer[1],papi_pe_buffer[2]);
01005 
01006          tot_time_enabled = papi_pe_buffer[1];
01007          tot_time_running = papi_pe_buffer[2];
01008 
01009          SUBDBG("count[%d] = (papi_pe_buffer[%d] %lld * "
01010         "tot_time_enabled %lld) / tot_time_running %lld\n",
01011         i, 0,papi_pe_buffer[0],
01012         tot_time_enabled,tot_time_running);
01013 
01014          if (tot_time_running == tot_time_enabled) {
01015         /* No scaling needed */
01016         pe_ctl->counts[i] = papi_pe_buffer[0];
01017          } else if (tot_time_running && tot_time_enabled) {
01018         /* Scale factor of 100 to avoid overflows when computing */
01019         /*enabled/running */
01020 
01021         scale = (tot_time_enabled * 100LL) / tot_time_running;
01022         scale = scale * papi_pe_buffer[0];
01023         scale = scale / 100LL;
01024         pe_ctl->counts[i] = scale;
01025      } else {
01026        /* This should not happen, but Phil reports it sometime does. */
01027         SUBDBG("perf_event kernel bug(?) count, enabled, "
01028            "running: %lld, %lld, %lld\n",
01029            papi_pe_buffer[0],tot_time_enabled,
01030            tot_time_running);
01031 
01032         pe_ctl->counts[i] = papi_pe_buffer[0];
01033      }
01034       }
01035    }
01036 
01037    /* Handle cases where we cannot use FORMAT GROUP */
01038    else if (bug_format_group() || pe_ctl->inherit) {
01039 
01040       /* we must read each counter individually */
01041       for ( i = 0; i < pe_ctl->num_events; i++ ) {
01042 
01043          ret = read( pe_ctl->events[i].event_fd, papi_pe_buffer,
01044             sizeof ( papi_pe_buffer ) );
01045          if ( ret == -1 ) {
01046         PAPIERROR("read returned an error: ", strerror( errno ));
01047         return PAPI_ESYS;
01048      }
01049 
01050      /* we should read one 64-bit value from each counter */
01051      if (ret!=sizeof(long long)) {
01052         PAPIERROR("Error!  short read");
01053         PAPIERROR("read: fd: %2d, tid: %ld, cpu: %d, ret: %d",
01054            pe_ctl->events[i].event_fd,
01055            (long)pe_ctl->tid, pe_ctl->events[i].cpu, ret);
01056         return PAPI_ESYS;
01057      }
01058 
01059          SUBDBG("read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
01060             pe_ctl->events[i].event_fd, (long)pe_ctl->tid,
01061         pe_ctl->events[i].cpu, ret);
01062          SUBDBG("read: %lld\n",papi_pe_buffer[0]);
01063 
01064      pe_ctl->counts[i] = papi_pe_buffer[0];
01065       }
01066    }
01067 
01068 
01069    /* Handle cases where we are using FORMAT_GROUP   */
01070    /* We assume only one group leader, in position 0 */
01071 
01072    else {
01073       if (pe_ctl->events[0].group_leader_fd!=-1) {
01074      PAPIERROR("Was expecting group leader");
01075       }
01076 
01077       ret = read( pe_ctl->events[0].event_fd, papi_pe_buffer,
01078           sizeof ( papi_pe_buffer ) );
01079 
01080       if ( ret == -1 ) {
01081      PAPIERROR("read returned an error: ", strerror( errno ));
01082      return PAPI_ESYS;
01083       }
01084 
01085       /* we read 1 64-bit value (number of events) then     */
01086       /* num_events more 64-bit values that hold the counts */
01087       if (ret<(signed)((1+pe_ctl->num_events)*sizeof(long long))) {
01088      PAPIERROR("Error! short read");
01089      return PAPI_ESYS;
01090       }
01091 
01092       SUBDBG("read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
01093          pe_ctl->events[0].event_fd,
01094          (long)pe_ctl->tid, pe_ctl->events[0].cpu, ret);
01095       {
01096      int j;
01097      for(j=0;j<ret/8;j++) {
01098             SUBDBG("read %d: %lld\n",j,papi_pe_buffer[j]);
01099      }
01100       }
01101 
01102       /* Make sure the kernel agrees with how many events we have */
01103       if (papi_pe_buffer[0]!=pe_ctl->num_events) {
01104      PAPIERROR("Error!  Wrong number of events");
01105      return PAPI_ESYS;
01106       }
01107 
01108       /* put the count values in their proper location */
01109       for(i=0;i<pe_ctl->num_events;i++) {
01110          pe_ctl->counts[i] = papi_pe_buffer[1+i];
01111       }
01112    }
01113 
01114 
01115    /* If we disabled the counters due to the sync_read_bug(), */
01116    /* then we need to re-enable them now.                     */
01117    if (bug_sync_read()) {
01118       if ( pe_ctx->state & PERF_EVENTS_RUNNING ) {
01119          for ( i = 0; i < pe_ctl->num_events; i++ ) {
01120         if ( pe_ctl->events[i].group_leader_fd == -1 ) {
01121            /* this should refresh any overflow counters too */
01122            ret = ioctl( pe_ctl->events[i].event_fd,
01123                 PERF_EVENT_IOC_ENABLE, NULL );
01124            if ( ret == -1 ) {
01125               /* Should never happen */
01126               PAPIERROR("ioctl(PERF_EVENT_IOC_ENABLE) returned an error: ",
01127                 strerror( errno ));
01128               return PAPI_ESYS;
01129            }
01130         }
01131      }
01132       }
01133    }
01134 
01135    /* point PAPI to the values we read */
01136    *events = pe_ctl->counts;
01137 
01138    SUBDBG("EXIT: *events: %p\n", *events);
01139    return PAPI_OK;
01140 }

Here is the call graph for this function:

int _pe_reset ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Definition at line 887 of file perf_event.c.

00888 {
00889    int i, ret;
00890    pe_control_t *pe_ctl = ( pe_control_t *) ctl;
00891 
00892    ( void ) ctx;             /*unused */
00893 
00894    /* We need to reset all of the events, not just the group leaders */
00895    for( i = 0; i < pe_ctl->num_events; i++ ) {
00896       ret = ioctl( pe_ctl->events[i].event_fd, PERF_EVENT_IOC_RESET, NULL );
00897       if ( ret == -1 ) {
00898      PAPIERROR("ioctl(%d, PERF_EVENT_IOC_RESET, NULL) "
00899            "returned error, Linux says: %s",
00900            pe_ctl->events[i].event_fd, strerror( errno ) );
00901      return PAPI_ESYS;
00902       }
00903    }
00904 
00905    return PAPI_OK;
00906 }

Here is the call graph for this function:

Here is the caller graph for this function:

int _pe_shutdown_thread ( hwd_context_t ctx  ) 

Definition at line 873 of file perf_event.c.

00874 {
00875     pe_context_t *pe_ctx = ( pe_context_t *) ctx;
00876 
00877     pe_ctx->initialized=0;
00878 
00879     return PAPI_OK;
00880 }

int _pe_start ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Definition at line 1144 of file perf_event.c.

01145 {
01146    int ret;
01147    int i;
01148    int did_something = 0;
01149    pe_context_t *pe_ctx = ( pe_context_t *) ctx;
01150    pe_control_t *pe_ctl = ( pe_control_t *) ctl;
01151 
01152    /* Reset the counters first.  Is this necessary? */
01153    ret = _pe_reset( pe_ctx, pe_ctl );
01154    if ( ret ) {
01155       return ret;
01156    }
01157 
01158    /* Enable all of the group leaders                */
01159    /* All group leaders have a group_leader_fd of -1 */
01160    for( i = 0; i < pe_ctl->num_events; i++ ) {
01161       if (pe_ctl->events[i].group_leader_fd == -1) {
01162      SUBDBG("ioctl(enable): fd: %d\n", pe_ctl->events[i].event_fd);
01163      ret=ioctl( pe_ctl->events[i].event_fd, PERF_EVENT_IOC_ENABLE, NULL) ;
01164 
01165      /* ioctls always return -1 on failure */
01166          if (ret == -1) {
01167             PAPIERROR("ioctl(PERF_EVENT_IOC_ENABLE) failed");
01168             return PAPI_ESYS;
01169      }
01170 
01171      did_something++;
01172       }
01173    }
01174 
01175    if (!did_something) {
01176       PAPIERROR("Did not enable any counters");
01177       return PAPI_EBUG;
01178    }
01179 
01180    pe_ctx->state |= PERF_EVENTS_RUNNING;
01181 
01182    return PAPI_OK;
01183 
01184 }

Here is the call graph for this function:

int _pe_stop ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Definition at line 1188 of file perf_event.c.

01189 {
01190     SUBDBG( "ENTER: ctx: %p, ctl: %p\n", ctx, ctl);
01191 
01192    int ret;
01193    int i;
01194    pe_context_t *pe_ctx = ( pe_context_t *) ctx;
01195    pe_control_t *pe_ctl = ( pe_control_t *) ctl;
01196 
01197    /* Just disable the group leaders */
01198    for ( i = 0; i < pe_ctl->num_events; i++ ) {
01199       if ( pe_ctl->events[i].group_leader_fd == -1 ) {
01200      ret=ioctl( pe_ctl->events[i].event_fd, PERF_EVENT_IOC_DISABLE, NULL);
01201      if ( ret == -1 ) {
01202         PAPIERROR( "ioctl(%d, PERF_EVENT_IOC_DISABLE, NULL) "
01203                "returned error, Linux says: %s",
01204                pe_ctl->events[i].event_fd, strerror( errno ) );
01205         return PAPI_EBUG;
01206      }
01207       }
01208    }
01209 
01210    pe_ctx->state &= ~PERF_EVENTS_RUNNING;
01211 
01212     SUBDBG( "EXIT:\n");
01213    return PAPI_OK;
01214 }

Here is the call graph for this function:

int _pe_write ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long *  from 
)

Definition at line 912 of file perf_event.c.

00914 {
00915    ( void ) ctx;             /*unused */
00916    ( void ) ctl;             /*unused */
00917    ( void ) from;            /*unused */
00918    /*
00919     * Counters cannot be written.  Do we need to virtualize the
00920     * counters so that they can be written, or perhaps modify code so that
00921     * they can be written? FIXME ?
00922     */
00923 
00924     return PAPI_ENOSUPP;
00925 }


Generated on 26 Jan 2016 for PAPI by  doxygen 1.6.1