示例#1
0
int
_papi_load_preset_table( char *pmu_str, int pmu_type, int cidx)
{

  (void) cidx;  /* We'll use this later */

    char pmu_name[PAPI_MIN_STR_LEN];
    char line[LINE_MAX];
    char name[PATH_MAX] = "builtin papi_events_table";
    char *tmp_papi_events_table = NULL;
    char *tmpn;
    FILE *table;
    int ret;
    unsigned int event_idx;
    int invalid_event;
    int line_no = 1, derived = 0, insert = 0, preset = 0;
    int get_presets = 0;   /* only get PRESETS after CPU is identified      */
    int found_presets = 0; /* only terminate search after PRESETS are found */
	                   /* this allows support for synonyms for CPU names*/

    SUBDBG("ENTER\n");

    /* copy the pmu identifier, stripping commas if found */
    tmpn = pmu_name;
    while ( *pmu_str ) {
       if ( *pmu_str != ',' ) *tmpn++ = *pmu_str;
       pmu_str++;
    }
    *tmpn = '\0';

    /* try the environment variable first */
    if ( ( tmpn = getenv( "PAPI_CSV_EVENT_FILE" ) ) && 
         ( strlen( tmpn ) != 0 ) ) {
       sprintf( name, "%s", tmpn );
       table = fopen( name, "r" );
    }
    /* if no valid environment variable, look for built-in table */
    else if ( papi_events_table ) {
       tmp_papi_events_table = papi_events_table;
       table = NULL;
    }
    /* if no env var and no built-in, search for default file */
    else {
#ifdef PAPI_DATADIR
       sprintf( name, "%s/%s", PAPI_DATADIR, PAPI_EVENT_FILE );
#else
       sprintf( name, "%s", PAPI_EVENT_FILE );
#endif
       table = open_event_table( name );
    }

    /* if no valid file or built-in table, bail */
    if ( table == NULL && tmp_papi_events_table == NULL ) {
       PAPIERROR( "fopen(%s): %s, please set the PAPI_CSV_EVENT_FILE "
                  "env. variable", name, strerror( errno ) );
       return PAPI_ESYS;
    }

    /* at this point either a valid file pointer or built-in table pointer */
    while ( get_event_line( line, table, &tmp_papi_events_table ) ) {
       char *t;
       int i;

       t = trim_string( strtok( line, "," ) );

       /* Skip blank lines */
       if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) continue;

       /* Skip comments */
       if ( t[0] == '#' ) {
	  goto nextline;
       } 

       if ( strcasecmp( t, "CPU" ) == 0 ) {
	  SUBDBG( "CPU token found on line %d\n", line_no );

	  if ( get_presets != 0 && found_presets != 0 ) {
	     SUBDBG( "Ending preset scanning at line %d of %s.\n", 
                     line_no, name );

	     get_presets=0; found_presets=0;
				
	  }
			
	  t = trim_string( strtok( NULL, "," ) );
	  if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
	     PAPIERROR( "Expected name after CPU token at line %d of %s "
			"-- ignoring", line_no, name );
	     goto nextline;
	  }

	  SUBDBG( "Examining CPU (%s) vs. (%s)\n", t, pmu_name );

	  if ( strcasecmp( t, pmu_name ) == 0 ) {
	     int type;

	     SUBDBG( "Found CPU %s at line %d of %s.\n", t, line_no, name );

	     t = trim_string( strtok( NULL, "," ) );
	     if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
		SUBDBG("No additional qualifier found, matching on string.\n");

		get_presets = 1;
	     } else if ( ( sscanf( t,"%d",&type )==1) && (type==pmu_type) ) {
                SUBDBG( "Found CPU %s type %d at line %d of %s.\n",
			pmu_name, type, line_no, name );
		get_presets = 1;
	     } else {
		SUBDBG( "Additional qualifier match failed %d vs %d.\n",
			pmu_type, type );

	     }
	  }
       } else if ( strcasecmp( t, "PRESET" ) == 0 ) {

          if ( get_presets == 0 ) goto nextline;

	  found_presets = 1;
	  t = trim_string( strtok( NULL, "," ) );

	  if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
			       
             PAPIERROR( "Expected name after PRESET token at line %d of %s "
			"-- ignoring", line_no, name );
	     goto nextline;
	  }

	  SUBDBG( "Examining preset %s\n", t );

	  if ( find_preset_code( t, &preset ) != PAPI_OK ) {
	     PAPIERROR ( "Invalid preset name %s after PRESET token "
			 "at line %d of %s -- ignoring",
			 t, line_no, name );
	     goto nextline;
	  }

	  SUBDBG( "Found 0x%08x for %s\n", preset, t );

	  t = trim_string( strtok( NULL, "," ) );
	  if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
	     PAPIERROR( "Expected derived type after PRESET token at "
                        "line %d of %s -- ignoring", line_no, name );
	     goto nextline;
	  }

	  if ( _papi_hwi_derived_type( t, &derived ) != PAPI_OK ) {
	     PAPIERROR( "Invalid derived name %s after PRESET token at "
			"line %d of %s -- ignoring",
			t, line_no, name );
	     goto nextline;
	  }

	  /****************************************/
	  /* Have a preset, let's start assigning */
	  /****************************************/

	  SUBDBG( "Found %d for %s\n", derived, t );
	  SUBDBG( "Adding 0x%x,%d to preset search table.\n", 
		  preset, derived );
	  
	  insert=preset&PAPI_PRESET_AND_MASK;

	  /* _papi_hwi_presets[insert].event_code = preset; */
	  _papi_hwi_presets[insert].derived_int = derived;

	  /* Derived support starts here */
	  /* Special handling for postfix */
	  if ( derived == DERIVED_POSTFIX ) {
	     t = trim_string( strtok( NULL, "," ) );
	     if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
		PAPIERROR( "Expected Operation string after derived type "
			   "DERIVED_POSTFIX at line %d of %s -- ignoring",
			   line_no, name );
		goto nextline;
	     }

	     SUBDBG( "Saving PostFix operations %s\n", t );

	     _papi_hwi_presets[insert].postfix=strdup(t);
	  }
			
	  /* All derived terms collected here */
	  i = 0;
	  invalid_event=0;
	  do {
	     t = trim_string( strtok( NULL, "," ) );
	     if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) break;
	     if ( strcasecmp( t, "NOTE" ) == 0 ) break;
	     _papi_hwi_presets[insert].name[i]=strdup(t);

	     SUBDBG( "Adding term (%d) %s to preset event 0x%x.\n", 
		     i, t, preset );

	     SUBDBG("Looking up: %s\n",t);

	     ret=_papi_hwd[cidx]->ntv_name_to_code(t, &event_idx);

	     if (ret==PAPI_OK) {
		SUBDBG("Found %x\n",event_idx);
		_papi_hwi_presets[insert].code[i]=event_idx;
	     }
	     else {
		PAPIERROR("papi_preset: Error finding event %s",t);
		invalid_event=1;
	     }

	  } while ( ++i < PAPI_MAX_COUNTER_TERMS );

	  if (invalid_event) {
	    /* We signify a valid preset if count > 0 */
	     _papi_hwi_presets[insert].count=0;
	  } else {
	     _papi_hwi_presets[insert].count=i;
	  }

	  /* End of derived support */

	  if ( i == 0 ) {
	     PAPIERROR( "Expected PFM event after DERIVED token at "
			"line %d of %s -- ignoring", line_no, name );
	     goto nextline;
	  }
	  if ( i == PAPI_MAX_COUNTER_TERMS ) {
	     t = trim_string( strtok( NULL, "," ) );
	  }
			
	  /* Handle optional NOTEs */
	  if ( t && ( strcasecmp( t, "NOTE" ) == 0 ) ) {
	     SUBDBG( "%s found on line %d\n", t, line_no );

	     /* read the rest of the line */
	     t = trim_note( strtok( NULL, "" ) );
	
	     if ( ( t == NULL ) || ( strlen( t ) == 0 ) ) {
		PAPIERROR( "Expected Note string at line %d of %s\n",
			   line_no, name );
	     }
	     else {
	        _papi_hwi_presets[insert].note = strdup( t );
		SUBDBG( "NOTE: --%s-- found on line %d\n", t, line_no );
	     }
	  }

       } else {
	  PAPIERROR( "Unrecognized token %s at line %d of %s -- ignoring", 
		     t, line_no, name );
	  goto nextline;
       }
nextline:
       line_no++;
    }

    if ( table ) {
       fclose( table );
    }
	
    return PAPI_OK;
}
static int load_preset_table(char *pmu_name, int pmu_type, pfm_preset_search_entry_t *here)
{
  char line[LINE_MAX];
  char name[PATH_MAX] = "builtin papi_events_table";
  char *tmp_papi_events_table = NULL;
  char *tmpn;
  FILE *table;
  int line_no = 1, derived = 0, insert = 0, preset = 0;
  int get_presets = 0;   /* only get PRESETS after CPU is identified */
  int found_presets = 0; /* only terminate search after PRESETS are found */
						 /* this allows support for synonyms for CPU names */

#ifdef SHOW_LOADS
  SUBDBG("%p\n",here);
#endif

/*  here[0].preset = PAPI_TOT_CYC;
  here[0].derived = NOT_DERIVED;
  here[1].preset = PAPI_TOT_INS;
  here[1].derived = NOT_DERIVED;
*/
  /* try the environment variable first */
  if ((tmpn = getenv("PAPI_PERFMON_EVENT_FILE")) && (strlen(tmpn) != 0)) {
    sprintf(name,"%s",tmpn);
    table = fopen(name,"r");
  }
  /* if no valid environment variable, look for built-in table */
  else if (papi_events_table) {
	  tmp_papi_events_table = papi_events_table;
	  table = NULL;
  }
  /* if no env var and no built-in, search for default file */
  else {
#ifdef PAPI_DATADIR
    sprintf(name,"%s/%s",PAPI_DATADIR,PAPI_EVENT_FILE);
#else
    sprintf(name,"%s",PAPI_EVENT_FILE);
#endif
	table = open_event_table(name);
  }
  /* if no valid file or built-in table, bail */
  if (table == NULL && tmp_papi_events_table == NULL) {
    PAPIERROR("fopen(%s): %s, please set the PAPI_PERFMON_EVENT_FILE env. variable",name,strerror(errno));
    return(PAPI_ESYS);
  }

  /* at this point either a valid file pointer or built-in table pointer */
  while (get_event_line(line, table, &tmp_papi_events_table)) {
      char *t;
	  int i;
      t = trim_string(strtok(line,","));
      if ((t == NULL) || (strlen(t) == 0))
	continue;
      if (t[0] == '#')
	{
/*	  SUBDBG("Comment found on line %d\n",line_no); */
	  goto nextline;
	}
      else if (strcasecmp(t,"CPU") == 0)
	{
#ifdef SHOW_LOADS
	  SUBDBG("CPU token found on line %d\n",line_no);
#endif
	  if (get_presets != 0 && found_presets != 0)
	    {
#ifdef SHOW_LOADS
	      SUBDBG("Ending preset scanning at line %d of %s.\n",line_no,name);
#endif
	      goto done;
	    }
	  t = trim_string(strtok(NULL,","));
	  if ((t == NULL) || (strlen(t) == 0))
	    {
	      PAPIERROR("Expected name after CPU token at line %d of %s -- ignoring",line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	  SUBDBG("Examining CPU (%s) vs. (%s)\n",t,pmu_name);
#endif
	  if (strcasecmp(t, pmu_name) == 0)
	    {
	      int type;

#ifdef SHOW_LOADS
	      SUBDBG("Found CPU %s at line %d of %s.\n",t,line_no,name);
#endif
	      t = trim_string(strtok(NULL,","));
	      if ((t == NULL) || (strlen(t) == 0))
		{
#ifdef SHOW_LOADS
		  SUBDBG("No additional qualifier found, matching on string.\n");
#endif
		  get_presets = 1;
		}
	      else if ((sscanf(t,"%d",&type) == 1) && (type == pmu_type))
		{
#ifdef SHOW_LOADS
		  SUBDBG("Found CPU %s type %d at line %d of %s.\n",pmu_name,type,line_no,name);
#endif
		  get_presets = 1;
		}
	      else
#ifdef SHOW_LOADS
		SUBDBG("Additional qualifier match failed %d vs %d.\n",pmu_type,type)
#endif
	      ;
	    }
	}
      else if (strcasecmp(t,"PRESET") == 0)
	{
#ifdef SHOW_LOADS
	  SUBDBG("PRESET token found on line %d\n",line_no);
#endif
	  if (get_presets == 0)
	    goto nextline;
	  found_presets = 1;
	  t = trim_string(strtok(NULL,","));
	  if ((t == NULL) || (strlen(t) == 0))
	    {
	      printf("Expected name after PRESET token at line %d of %s -- ignoring",line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	  SUBDBG("Examining preset %s\n",t);
#endif
	  if (find_preset_code(t,&preset) != PAPI_OK)
	    {
	      PAPIERROR("Invalid preset name %s after PRESET token at line %d of %s -- ignoring",t,line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	  SUBDBG("Found 0x%08x for %s\n",preset,t);
#endif
	  t = trim_string(strtok(NULL,","));
	  if ((t == NULL) || (strlen(t) == 0))
	    {
	      PAPIERROR("Expected derived type after PRESET token at line %d of %s -- ignoring",line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	  SUBDBG("Examining derived %s\n",t);
#endif
	  if (_papi_hwi_derived_type(t,&derived) != PAPI_OK)
	    {
	      PAPIERROR("Invalid derived name %s after PRESET token at line %d of %s -- ignoring",t,line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	  SUBDBG("Found %d for %s\n",derived,t);
	  SUBDBG("Adding 0x%x,%d to preset search table.\n",preset,derived);
#endif
	  here[insert].preset = preset;
	  here[insert].derived = derived;
#ifdef SHOW_LOADS
	  SUBDBG("%d Adding 0x%x,%d to preset search table.\n",insert, here[insert].preset,here[insert].derived);
#endif

	  /* Derived support starts here */
	  /* Special handling for postfix */
	  if (derived == DERIVED_POSTFIX) {
	    t = trim_string(strtok(NULL,","));
	    if ((t == NULL) || (strlen(t) == 0)) {
	      PAPIERROR("Expected Operation string after derived type DERIVED_POSTFIX at line %d of %s -- ignoring",line_no,name);
	      goto nextline;
	    }
#ifdef SHOW_LOADS
	    SUBDBG("Saving PostFix operations %s\n",t);
#endif
	    here[insert].operation = strdup(t);
	  }
	  /* All derived terms collected here */
	  i = 0;
	  do {
	    t = trim_string(strtok(NULL,","));
	    if ((t == NULL) || (strlen(t) == 0)) break;
	    if (strcasecmp(t,"NOTE") == 0) break;
	    here[insert].findme[i] = strdup(t);
#ifdef SHOW_LOADS
	    SUBDBG("Adding term (%d) %s to preset event 0x%x.\n",i,t,preset);
#endif
	  } while (++i < MAX_COUNTER_TERMS);
	  /* End of derived support */

	  if (i == 0) {
	    PAPIERROR("Expected PFM event after DERIVED token at line %d of %s -- ignoring",line_no,name);
	    goto nextline;
	  }
	  if (i == MAX_COUNTER_TERMS)
	    t = trim_string(strtok(NULL,","));

	  /* Handle optional NOTEs */
	  if (t && (strcasecmp(t,"NOTE") == 0)) {
#ifdef SHOW_LOADS
	    SUBDBG("%s found on line %d\n",t,line_no);
#endif
	    t = trim_note(strtok(NULL,"")); /* read the rest of the line */
	    if ((t == NULL) || (strlen(t) == 0))
	      PAPIERROR("Expected Note string at line %d of %s\n",line_no,name);
	    else {
	      here[insert].note = strdup(t);
#ifdef SHOW_LOADS
	      SUBDBG("NOTE: --%s-- found on line %d\n",t,line_no);
#endif
	    }
	  }

	  insert++;
	}
      else
	{
	  PAPIERROR("Unrecognized token %s at line %d of %s -- ignoring",t,line_no,name);
	  goto nextline;
	}
    nextline:
      line_no++;
    }
 done:
  if (table)
    fclose(table);
  return(insert);
}