PAPI  5.0.1.0
linux-infiniband.c File Reference

This file has the source code for a component that enables PAPI-C to access hardware monitoring counters for InfiniBand devices through the OFED library. Since a new interface was introduced with OFED version 1.4 (released Dec 2008), the current InfiniBand component does not support OFED versions < 1.4. More...

Include dependency graph for linux-infiniband.c:

Go to the source code of this file.

Defines

#define infiniband_native_table   subscriptions
#define InitStruct(var, type)   type var; memset(&var, 0, sizeof(type))

Functions

static void init_ib_counter ()
static counter_infoaddCounter (const char *name, const char *desc, const char *unit)
static void addIBPort (const char *ca_name, umad_port_t *port)
static int init_ib_port (ib_port *portdata)
static int read_ib_counter ()
void host_read_values (long long *data)
static counter_infocounterFromName (const char *cntr)
static uint64_t host_subscribe (const char *cntr)
static string_listhost_listCounter (int num_counters1)
static void host_finalize ()
static void host_deleteStringList (string_list *to_delete)
int INFINIBAND_init_thread (hwd_context_t *ctx)
int INFINIBAND_init_component ()
int INFINIBAND_init_control_state (hwd_control_state_t *ctrl)
int INFINIBAND_start (hwd_context_t *ctx, hwd_control_state_t *ctrl)
int INFINIBAND_stop (hwd_context_t *ctx, hwd_control_state_t *ctrl)
int INFINIBAND_read (hwd_context_t *ctx, hwd_control_state_t *ctrl, long_long **events, int flags)
int INFINIBAND_shutdown_thread (hwd_context_t *ctx)
int INFINIBAND_ctl (hwd_context_t *ctx, int code, _papi_int_option_t *option)
int INFINIBAND_update_control_state (hwd_control_state_t *ptr, NativeInfo_t *native, int count, hwd_context_t *ctx)
int INFINIBAND_set_domain (hwd_control_state_t *cntrl, int domain)
int INFINIBAND_reset (hwd_context_t *ctx, hwd_control_state_t *ctrl)
int INFINIBAND_ntv_enum_events (unsigned int *EventCode, int modifier)
int INFINIBAND_ntv_code_to_name (unsigned int EventCode, char *name, int len)
int INFINIBAND_ntv_code_to_descr (unsigned int EventCode, char *name, int len)
int INFINIBAND_ntv_code_to_bits (unsigned int EventCode, hwd_register_t *bits)

Variables

struct ibmad_port * srcport
static ib_portid_t portid
static int ib_timeout = 0
static int ibportnum = 0
static counter_infosubscriptions [100]
static int is_initialized = 0
static int num_counters = 0
static int is_finalized = 0
static counter_inforoot_counter = NULL
static ib_portroot_ib_port = NULL
static ib_portactive_ib_port = NULL
long long _papi_hwd_infiniband_register_start [100]
long long _papi_hwd_infiniband_register [100]
papi_vector_t _infiniband_vector

Detailed Description

Author:
Heike Jagode (in collaboration with Michael Kluge, TU Dresden) jagode@eecs.utk.edu

InfiniBand component

Tested version of OFED: 1.4

Definition in file linux-infiniband.c.


Define Documentation

Definition at line 47 of file linux-infiniband.c.

#define InitStruct (   var,
  type 
)    type var; memset(&var, 0, sizeof(type))

Definition at line 49 of file linux-infiniband.c.


Function Documentation

static counter_info* addCounter ( const char *  name,
const char *  desc,
const char *  unit 
) [static]

add a counter to the list of available counters

Parameters:
namethe short name of the counter
desca longer description
unitthe unit for this counter

Definition at line 107 of file linux-infiniband.c.

{
    counter_info *cntr, *last;

    cntr = ( counter_info * ) malloc( sizeof ( counter_info ) );
    if ( cntr == NULL ) {
        fprintf( stderr, "can not allocate memory for new counter\n" );
        exit( 1 );
    }
    cntr->name = strdup( name );
    cntr->description = strdup( desc );
    cntr->unit = strdup( unit );
    cntr->value = 0;
    cntr->next = NULL;

    if ( root_counter == NULL ) {
        root_counter = cntr;
    } else {
        last = root_counter;
        while ( last->next != NULL )
            last = last->next;
        last->next = cntr;
    }

    return cntr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void addIBPort ( const char *  ca_name,
umad_port_t *  port 
) [static]

add one IB port to the list of available ports and add the counters related to this port to the global counter list

Definition at line 140 of file linux-infiniband.c.

{
    ib_port *nwif, *last;
    char counter_name[512];

    nwif = ( ib_port * ) malloc( sizeof ( ib_port ) );

    if ( nwif == NULL ) {
        fprintf( stderr, "can not allocate memory for IB port description\n" );
        exit( 1 );
    }

    sprintf( counter_name, "%s_%d", ca_name, port->portnum );
    nwif->name = strdup( counter_name );

    sprintf( counter_name, "%s_%d_recv", ca_name, port->portnum );
    nwif->recv_cntr =
        addCounter( counter_name, "bytes received on this IB port", "bytes" );

    sprintf( counter_name, "%s_%d_send", ca_name, port->portnum );
    nwif->send_cntr =
        addCounter( counter_name, "bytes written to this IB port", "bytes" );

    nwif->port_rate = port->rate;
    nwif->is_initialized = 0;
    nwif->port_number = port->portnum;
    nwif->next = NULL;

    num_counters += 2;

    if ( root_ib_port == NULL ) {
        root_ib_port = nwif;
    } else {
        last = root_ib_port;
        while ( last->next != NULL )
            last = last->next;
        last->next = nwif;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static counter_info* counterFromName ( const char *  cntr) [static]

find the pointer for a counter_info structure based on the counter name

Definition at line 316 of file linux-infiniband.c.

{
    int loop = 0;
    char tmp[512];
    counter_info *local_cntr = root_counter;

    while ( local_cntr != NULL ) {
        if ( strcmp( cntr, local_cntr->name ) == 0 )
            return local_cntr;

        local_cntr = local_cntr->next;
        loop++;
    }

    gethostname( tmp, 512 );
    fprintf( stderr, "can not find host counter: %s on %s\n", cntr, tmp );
    fprintf( stderr, "we only have: " );
    local_cntr = root_counter;

    while ( local_cntr != NULL ) {
        fprintf( stderr, "'%s' ", local_cntr->name );
        local_cntr = local_cntr->next;
        loop++;
    }

    fprintf( stderr, "\n" );
    exit( 1 );
    /* never reached */
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void host_deleteStringList ( string_list to_delete) [static]

delete a list of strings

Definition at line 472 of file linux-infiniband.c.

{
    int loop;

    if ( to_delete->data != NULL ) {
        for ( loop = 0; loop < to_delete->count; loop++ )
            free( to_delete->data[loop] );

        free( to_delete->data );
    }

    free( to_delete );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void host_finalize ( void  ) [static]

finalizes the library

Definition at line 444 of file linux-infiniband.c.

{
    counter_info *cntr, *next;

    if ( is_finalized )
        return;

    cntr = root_counter;

    while ( cntr != NULL ) {
        next = cntr->next;
        free( cntr->name );
        free( cntr->description );
        free( cntr->unit );
        free( cntr );
        cntr = next;
    }

    root_counter = NULL;

    is_finalized = 1;
}

Here is the caller graph for this function:

static string_list* host_listCounter ( int  num_counters1) [static]

return a newly allocated list of strings containing all counter names

Definition at line 411 of file linux-infiniband.c.

{
    string_list *list;
    counter_info *cntr = root_counter;

    list = malloc( sizeof ( string_list ) );
    if ( list == NULL ) {
        fprintf( stderr, "unable to allocate memory for new string_list" );
        exit( 1 );
    }
    list->count = 0;
    list->data = ( char ** ) malloc( num_counters1 * sizeof ( char * ) );

    if ( list->data == NULL ) {
        fprintf( stderr,
                 "unable to allocate memory for %d pointers in a new string_list\n",
                 num_counters1 );
        exit( 1 );
    }

    while ( cntr != NULL ) {
        list->data[list->count++] = strdup( cntr->name );
        cntr = cntr->next;
    }

    return list;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void host_read_values ( long long *  data)

Definition at line 297 of file linux-infiniband.c.

{
    int loop;

    read_ib_counter(  );

    for ( loop = 0; loop < INFINIBAND_MAX_COUNTERS; loop++ ) {
        if ( subscriptions[loop] == NULL )
            break;

        data[loop] = subscriptions[loop]->value;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static uint64_t host_subscribe ( const char *  cntr) [static]

allow external code to subscribe to a counter based on the counter name

Definition at line 352 of file linux-infiniband.c.

{
    int loop;
    int len;
    char tmp_name[512];
    ib_port *aktp;

    counter_info *counter = counterFromName( cntr );

    for ( loop = 0; loop < INFINIBAND_MAX_COUNTERS; loop++ ) {
        if ( subscriptions[loop] == NULL ) {
            subscriptions[loop] = counter;
            counter->idx = loop;

            /* we have an IB counter if the name ends with _send or _recv and
               the prefix before that is in the ib_port list */
            if ( ( len = strlen( cntr ) ) > 5 ) {
                if ( strcmp( &cntr[len - 5], "_recv" ) == 0 ||
                     strcmp( &cntr[len - 5], "_send" ) == 0 ) {
                    /* look through all IB_counters */
                    strncpy( tmp_name, cntr, len - 5 );
                    tmp_name[len - 5] = 0;
                    aktp = root_ib_port;
                    // printf("looking for IB port '%s'\n", tmp_name);
                    while ( aktp != NULL ) {
                        if ( strcmp( aktp->name, tmp_name ) == 0 ) {
                            if ( !aktp->is_initialized ) {
                                init_ib_port( aktp );
                                active_ib_port = aktp;
                            }
                            return loop + 1;
                        }
                        /* name does not match, if this counter is
                           initialized, we can't have two active IB ports */
                        if ( aktp->is_initialized ) {
#if 0   /* not necessary with OFED version >= 1.4 */
                            fprintf( stderr,
                                     "unable to activate IB port monitoring for more than one port\n" );
                            exit( 1 );
#endif
                        }
                        aktp = aktp->next;
                    }
                }
            }
            return loop + 1;
        }
    }
    fprintf( stderr, "please subscribe only once to each counter\n" );
    exit( 1 );
    /* never reached */
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 637 of file linux-infiniband.c.

{
    ( void ) ctx;
    ( void ) code;
    ( void ) option;
    return ( PAPI_OK );
}

Definition at line 532 of file linux-infiniband.c.

{
    int i;

    for ( i = 0; i < INFINIBAND_MAX_COUNTERS; i++ ) {
        _papi_hwd_infiniband_register_start[i] = -1;
        _papi_hwd_infiniband_register[i] = -1;
    }

    return ( PAPI_OK );
}

Definition at line 550 of file linux-infiniband.c.

{
    ( void ) ctrl;
    return PAPI_OK;
}

Definition at line 495 of file linux-infiniband.c.

{
    string_list *counter_list = NULL;
    int i;
    int loop;

    /* initialize portid struct of type ib_portid_t to 0 */
    InitStruct( portid, ib_portid_t );

    if ( is_initialized )
        return PAPI_OK;

    is_initialized = 1;

    init_ib_counter(  );

    for ( loop = 0; loop < INFINIBAND_MAX_COUNTERS; loop++ )
        subscriptions[loop] = NULL;

    counter_list = host_listCounter( num_counters );

    for ( i = 0; i < counter_list->count; i++ )
        host_subscribe( counter_list->data[i] );

    ( ( INFINIBAND_context_t * ) ctx )->state.ncounter = counter_list->count;

    host_deleteStringList( counter_list );

    return PAPI_OK;
}

Here is the call graph for this function:

int INFINIBAND_ntv_code_to_bits ( unsigned int  EventCode,
hwd_register_t bits 
)

Definition at line 765 of file linux-infiniband.c.

{
    memcpy( ( INFINIBAND_register_t * ) bits,
            infiniband_native_table[EventCode],
            sizeof ( INFINIBAND_register_t ) );

    return PAPI_OK;
}
int INFINIBAND_ntv_code_to_descr ( unsigned int  EventCode,
char *  name,
int  len 
)

Definition at line 753 of file linux-infiniband.c.

{
    strncpy( name, infiniband_native_table[EventCode]->description, len );

    return PAPI_OK;
}
int INFINIBAND_ntv_code_to_name ( unsigned int  EventCode,
char *  name,
int  len 
)

Definition at line 741 of file linux-infiniband.c.

{
    strncpy( name, infiniband_native_table[EventCode]->name, len );

    return PAPI_OK;
}
int INFINIBAND_ntv_enum_events ( unsigned int *  EventCode,
int  modifier 
)

Definition at line 717 of file linux-infiniband.c.

{
    if ( modifier == PAPI_ENUM_FIRST ) {
        *EventCode = 0;
        return PAPI_OK;
    }

    if ( modifier == PAPI_ENUM_EVENTS ) {
        int index = *EventCode;

        if ( infiniband_native_table[index + 1] ) {
            *EventCode = *EventCode + 1;
            return ( PAPI_OK );
        } else
            return ( PAPI_ENOEVNT );
    } else
        return ( PAPI_EINVAL );
}
int INFINIBAND_read ( hwd_context_t ctx,
hwd_control_state_t ctrl,
long_long **  events,
int  flags 
)

Definition at line 600 of file linux-infiniband.c.

{
    int i;
    ( void ) flags;

    host_read_values( _papi_hwd_infiniband_register );

    for ( i = 0; i < ( ( INFINIBAND_context_t * ) ctx )->state.ncounter; i++ ) {
        ( ( INFINIBAND_control_state_t * ) ctrl )->counts[i] =
            _papi_hwd_infiniband_register[i] -
            _papi_hwd_infiniband_register_start[i];
    }

    *events = ( ( INFINIBAND_control_state_t * ) ctrl )->counts;
    return ( PAPI_OK );
}

Here is the call graph for this function:

int INFINIBAND_reset ( hwd_context_t ctx,
hwd_control_state_t ctrl 
)

Definition at line 706 of file linux-infiniband.c.

{
    INFINIBAND_start( ctx, ctrl );
    return ( PAPI_OK );
}

Here is the call graph for this function:

int INFINIBAND_set_domain ( hwd_control_state_t cntrl,
int  domain 
)

Definition at line 681 of file linux-infiniband.c.

{
    int found = 0;
    ( void ) cntrl;

    if ( PAPI_DOM_USER & domain )
        found = 1;

    if ( PAPI_DOM_KERNEL & domain )
        found = 1;

    if ( PAPI_DOM_OTHER & domain )
        found = 1;

    if ( !found )
        return ( PAPI_EINVAL );

    return ( PAPI_OK );
}

Definition at line 623 of file linux-infiniband.c.

{
    ( void ) ctx;
    host_finalize(  );
    return ( PAPI_OK );
}

Here is the call graph for this function:

int INFINIBAND_start ( hwd_context_t ctx,
hwd_control_state_t ctrl 
)

Definition at line 561 of file linux-infiniband.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int INFINIBAND_stop ( hwd_context_t ctx,
hwd_control_state_t ctrl 
)

Definition at line 579 of file linux-infiniband.c.

{
    int i;
    ( void ) ctx;

    host_read_values( _papi_hwd_infiniband_register );

    for ( i = 0; i < ( ( INFINIBAND_context_t * ) ctx )->state.ncounter; i++ ) {
        ( ( INFINIBAND_control_state_t * ) ctrl )->counts[i] =
            _papi_hwd_infiniband_register[i] -
            _papi_hwd_infiniband_register_start[i];
    }

    return ( PAPI_OK );
}

Here is the call graph for this function:

int INFINIBAND_update_control_state ( hwd_control_state_t ptr,
NativeInfo_t native,
int  count,
hwd_context_t ctx 
)

Definition at line 653 of file linux-infiniband.c.

{
    ( void ) ptr;
    ( void ) ctx;
    int i, index;

    for ( i = 0; i < count; i++ ) {
        index = native[i].ni_event;
        native[i].ni_position = index;
    }

    return ( PAPI_OK );
}
static void init_ib_counter ( ) [static]

use libumad to discover IB ports

Definition at line 63 of file linux-infiniband.c.

{
    char names[20][UMAD_CA_NAME_LEN];
    int n, i;
    char *ca_name;
    umad_ca_t ca;
    int r;
    int portnum;

    if ( umad_init(  ) < 0 ) {
        fprintf( stderr, "can't init UMAD library\n" );
        exit( 1 );
    }

    if ( ( n = umad_get_cas_names( ( void * ) names, UMAD_CA_NAME_LEN ) ) < 0 ) {
        fprintf( stderr, "can't list IB device names\n" );
        exit( 1 );
    }

    for ( i = 0; i < n; i++ ) {
        ca_name = names[i];

        if ( ( r = umad_get_ca( ca_name, &ca ) ) < 0 ) {
            fprintf( stderr, "can't read ca from IB device\n" );
            exit( 1 );
        }

        if ( !ca.node_type )
            continue;

        /* port numbers are '1' based in OFED */
        for ( portnum = 1; portnum <= ca.numports; portnum++ )
            addIBPort( ca.ca_name, ca.ports[portnum] );
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int init_ib_port ( ib_port portdata) [static]

initialize one IB port so that we are able to read values from it

Definition at line 185 of file linux-infiniband.c.

{
    int mgmt_classes[4] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS,
        IB_PERFORMANCE_CLASS
    };
    char *ca = 0;
    static uint8_t pc[1024];
    int mask = 0xFFFF;

    srcport = mad_rpc_open_port( ca, portdata->port_number, mgmt_classes, 4 );
    if ( !srcport ) {
        fprintf( stderr, "Failed to open '%s' port '%d'\n", ca,
                 portdata->port_number );
        exit( 1 );
    }

    if ( ib_resolve_self_via( &portid, &ibportnum, 0, srcport ) < 0 ) {
        fprintf( stderr, "can't resolve self port\n" );
        exit( 1 );
    }

    /* PerfMgt ClassPortInfo is a required attribute */
    /* might be redundant, could be left out for fast implementation */
    if ( !pma_query_via
         ( pc, &portid, ibportnum, ib_timeout, CLASS_PORT_INFO, srcport ) ) {
        fprintf( stderr, "classportinfo query\n" );
        exit( 1 );
    }

    if ( !performance_reset_via
         ( pc, &portid, ibportnum, mask, ib_timeout, IB_GSI_PORT_COUNTERS,
           srcport ) ) {
        fprintf( stderr, "perf reset\n" );
        exit( 1 );
    }

    /* read the initial values */
    mad_decode_field( pc, IB_PC_XMT_BYTES_F, &portdata->last_send_val );
    portdata->sum_send_val = 0;
    mad_decode_field( pc, IB_PC_RCV_BYTES_F, &portdata->last_recv_val );
    portdata->sum_recv_val = 0;

    portdata->is_initialized = 1;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int read_ib_counter ( ) [static]

read and reset IB counters (reset on demand)

Definition at line 237 of file linux-infiniband.c.

{
    uint32_t send_val;
    uint32_t recv_val;
    uint8_t pc[1024];
    /* 32 bit counter FFFFFFFF */
    uint32_t max_val = 4294967295;
    /* if it is bigger than this -> reset */
    uint32_t reset_limit = max_val * 0.7;
    int mask = 0xFFFF;

    if ( active_ib_port == NULL )
        return 0;

    /* reading cost ~70 mirco secs */
    if ( !pma_query_via
         ( pc, &portid, ibportnum, ib_timeout, IB_GSI_PORT_COUNTERS,
           srcport ) ) {
        fprintf( stderr, "perfquery\n" );
        exit( 1 );
    }

    mad_decode_field( pc, IB_PC_XMT_BYTES_F, &send_val );
    mad_decode_field( pc, IB_PC_RCV_BYTES_F, &recv_val );

    /* multiply the numbers read by 4 as the IB port counters are not
       counting bytes. they always count 32dwords. see man page of
       perfquery for details
       internally a uint64_t ia used to sum up the values */
    active_ib_port->sum_send_val +=
        ( send_val - active_ib_port->last_send_val ) * 4;
    active_ib_port->sum_recv_val +=
        ( recv_val - active_ib_port->last_recv_val ) * 4;

    active_ib_port->send_cntr->value = active_ib_port->sum_send_val;
    active_ib_port->recv_cntr->value = active_ib_port->sum_recv_val;

    if ( send_val > reset_limit || recv_val > reset_limit ) {
        /* reset cost ~70 mirco secs */
        if ( !performance_reset_via
             ( pc, &portid, ibportnum, mask, ib_timeout, IB_GSI_PORT_COUNTERS,
               srcport ) ) {
            fprintf( stderr, "perf reset\n" );
            exit( 1 );
        }

        mad_decode_field( pc, IB_PC_XMT_BYTES_F,
                          &active_ib_port->last_send_val );
        mad_decode_field( pc, IB_PC_RCV_BYTES_F,
                          &active_ib_port->last_recv_val );
    } else {
        active_ib_port->last_send_val = send_val;
        active_ib_port->last_recv_val = recv_val;
    }

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 52 of file linux-infiniband.c.

Definition at line 51 of file linux-infiniband.c.

ib_port* active_ib_port = NULL [static]

Definition at line 45 of file linux-infiniband.c.

int ib_timeout = 0 [static]

Definition at line 33 of file linux-infiniband.c.

int ibportnum = 0 [static]

Definition at line 34 of file linux-infiniband.c.

int is_finalized = 0 [static]

Definition at line 39 of file linux-infiniband.c.

int is_initialized = 0 [static]

Definition at line 37 of file linux-infiniband.c.

int num_counters = 0 [static]

Definition at line 38 of file linux-infiniband.c.

ib_portid_t portid [static]

Definition at line 32 of file linux-infiniband.c.

counter_info* root_counter = NULL [static]

Definition at line 42 of file linux-infiniband.c.

ib_port* root_ib_port = NULL [static]

Definition at line 44 of file linux-infiniband.c.

struct ibmad_port* srcport

Definition at line 31 of file linux-infiniband.c.

counter_info* subscriptions[100] [static]

Definition at line 36 of file linux-infiniband.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines