PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
linux-memory.c File Reference
Include dependency graph for linux-memory.c:

Go to the source code of this file.

Functions

int _linux_get_dmem_info (PAPI_dmem_info_t *d)
 
int generic_get_memory_info (PAPI_hw_info_t *hw_info)
 
int _linux_get_memory_info (PAPI_hw_info_t *hwinfo, int cpu_type)
 
int _linux_update_shlib_info (papi_mdi_t *mdi)
 

Function Documentation

int _linux_get_dmem_info ( PAPI_dmem_info_t d)

Definition at line 48 of file linux-memory.c.

49 {
50  char fn[PATH_MAX], tmp[PATH_MAX];
51  FILE *f;
52  int ret;
53  long long sz = 0, lck = 0, res = 0, shr = 0, stk = 0, txt = 0, dat =
54  0, dum = 0, lib = 0, hwm = 0;
55 
56  sprintf( fn, "/proc/%ld/status", ( long ) getpid( ) );
57  f = fopen( fn, "r" );
58  if ( f == NULL ) {
59  PAPIERROR( "fopen(%s): %s\n", fn, strerror( errno ) );
60  return PAPI_ESYS;
61  }
62  while ( 1 ) {
63  if ( fgets( tmp, PATH_MAX, f ) == NULL )
64  break;
65  if ( strspn( tmp, "VmSize:" ) == strlen( "VmSize:" ) ) {
66  sscanf( tmp + strlen( "VmSize:" ), "%lld", &sz );
67  d->size = sz;
68  continue;
69  }
70  if ( strspn( tmp, "VmHWM:" ) == strlen( "VmHWM:" ) ) {
71  sscanf( tmp + strlen( "VmHWM:" ), "%lld", &hwm );
72  d->high_water_mark = hwm;
73  continue;
74  }
75  if ( strspn( tmp, "VmLck:" ) == strlen( "VmLck:" ) ) {
76  sscanf( tmp + strlen( "VmLck:" ), "%lld", &lck );
77  d->locked = lck;
78  continue;
79  }
80  if ( strspn( tmp, "VmRSS:" ) == strlen( "VmRSS:" ) ) {
81  sscanf( tmp + strlen( "VmRSS:" ), "%lld", &res );
82  d->resident = res;
83  continue;
84  }
85  if ( strspn( tmp, "VmData:" ) == strlen( "VmData:" ) ) {
86  sscanf( tmp + strlen( "VmData:" ), "%lld", &dat );
87  d->heap = dat;
88  continue;
89  }
90  if ( strspn( tmp, "VmStk:" ) == strlen( "VmStk:" ) ) {
91  sscanf( tmp + strlen( "VmStk:" ), "%lld", &stk );
92  d->stack = stk;
93  continue;
94  }
95  if ( strspn( tmp, "VmExe:" ) == strlen( "VmExe:" ) ) {
96  sscanf( tmp + strlen( "VmExe:" ), "%lld", &txt );
97  d->text = txt;
98  continue;
99  }
100  if ( strspn( tmp, "VmLib:" ) == strlen( "VmLib:" ) ) {
101  sscanf( tmp + strlen( "VmLib:" ), "%lld", &lib );
102  d->library = lib;
103  continue;
104  }
105  }
106  fclose( f );
107 
108  sprintf( fn, "/proc/%ld/statm", ( long ) getpid( ) );
109  f = fopen( fn, "r" );
110  if ( f == NULL ) {
111  PAPIERROR( "fopen(%s): %s\n", fn, strerror( errno ) );
112  return PAPI_ESYS;
113  }
114  ret =
115  fscanf( f, "%lld %lld %lld %lld %lld %lld %lld", &dum, &dum, &shr, &dum,
116  &dum, &dat, &dum );
117  if ( ret != 7 ) {
118  PAPIERROR( "fscanf(7 items): %d\n", ret );
119  fclose(f);
120  return PAPI_ESYS;
121  }
122  d->pagesize = getpagesize( );
123  d->shared = ( shr * d->pagesize ) / 1024;
124  fclose( f );
125 
126  return PAPI_OK;
127 }
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int errno
double f(double a)
Definition: cpi.c:23
return PAPI_OK
Definition: linux-nvml.c:497
long long heap
Definition: papi.h:879
fclose(thread_wqfd)
long long ret
Definition: iozone.c:1346
long long resident
Definition: papi.h:874
long long pagesize
Definition: papi.h:882
#define PAPI_ESYS
Definition: papi.h:255
long long locked
Definition: papi.h:880
void PAPIERROR(char *format,...)
long long library
Definition: papi.h:878
long long shared
Definition: papi.h:876
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
#define PATH_MAX
Definition: fileop.c:68
long long text
Definition: papi.h:877
long long stack
Definition: papi.h:881
long long tmp
Definition: iozone.c:12031
long long size
Definition: papi.h:873
long long high_water_mark
Definition: papi.h:875
int sz
Definition: fileop.c:80

Here is the call graph for this function:

int _linux_get_memory_info ( PAPI_hw_info_t hwinfo,
int  cpu_type 
)

Definition at line 1110 of file linux-memory.c.

1111 {
1112  ( void ) cpu_type; /*unused */
1113  int retval = PAPI_OK;
1114 
1115 #if defined(__i386__)||defined(__x86_64__)
1116  x86_get_memory_info( hwinfo );
1117 #elif defined(__ia64__)
1118  ia64_get_memory_info( hwinfo );
1119 #elif defined(__powerpc__)
1120  ppc64_get_memory_info( hwinfo );
1121 #elif defined(__sparc__)
1122  sparc_get_memory_info( hwinfo );
1123 #elif defined(__arm__)
1124  #warning "WARNING! linux_get_memory_info() does nothing on ARM32!"
1125  generic_get_memory_info (hwinfo);
1126 #else
1127  generic_get_memory_info (hwinfo);
1128 #endif
1129 
1130  return retval;
1131 }
return PAPI_OK
Definition: linux-nvml.c:497
void
Definition: iozone.c:18627
int generic_get_memory_info(PAPI_hw_info_t *hw_info)
Definition: linux-memory.c:900
ssize_t retval
Definition: libasync.c:338

Here is the call graph for this function:

Here is the caller graph for this function:

int _linux_update_shlib_info ( papi_mdi_t mdi)

Definition at line 1134 of file linux-memory.c.

1135 {
1136 
1137  char fname[PAPI_HUGE_STR_LEN];
1138  unsigned long t_index = 0, d_index = 0, b_index = 0, counting = 1;
1139  char buf[PAPI_HUGE_STR_LEN + PAPI_HUGE_STR_LEN], perm[5], dev[16];
1140  char mapname[PAPI_HUGE_STR_LEN], lastmapname[PAPI_HUGE_STR_LEN];
1141  unsigned long begin = 0, end = 0, size = 0, inode = 0, foo = 0;
1142  PAPI_address_map_t *tmp = NULL;
1143  FILE *f;
1144 
1145  memset( fname, 0x0, sizeof ( fname ) );
1146  memset( buf, 0x0, sizeof ( buf ) );
1147  memset( perm, 0x0, sizeof ( perm ) );
1148  memset( dev, 0x0, sizeof ( dev ) );
1149  memset( mapname, 0x0, sizeof ( mapname ) );
1150  memset( lastmapname, 0x0, sizeof ( lastmapname ) );
1151 
1152  sprintf( fname, "/proc/%ld/maps", ( long ) mdi->pid );
1153  f = fopen( fname, "r" );
1154 
1155  if ( !f ) {
1156  PAPIERROR( "fopen(%s) returned < 0", fname );
1157  return PAPI_OK;
1158  }
1159 
1160  again:
1161  while ( !feof( f ) ) {
1162  begin = end = size = inode = foo = 0;
1163  if ( fgets( buf, sizeof ( buf ), f ) == 0 )
1164  break;
1165  /* If mapname is null in the string to be scanned, we need to detect that */
1166  if ( strlen( mapname ) )
1167  strcpy( lastmapname, mapname );
1168  else
1169  lastmapname[0] = '\0';
1170  /* If mapname is null in the string to be scanned, we need to detect that */
1171  mapname[0] = '\0';
1172  sscanf( buf, "%lx-%lx %4s %lx %s %ld %s", &begin, &end, perm, &foo, dev,
1173  &inode, mapname );
1174  size = end - begin;
1175 
1176  /* the permission string looks like "rwxp", where each character can
1177  * be either the letter, or a hyphen. The final character is either
1178  * p for private or s for shared. */
1179 
1180  if ( counting ) {
1181  if ( ( perm[2] == 'x' ) && ( perm[0] == 'r' ) && ( inode != 0 ) ) {
1182  if ( strcmp( mdi->exe_info.fullname, mapname )
1183  == 0 ) {
1185  ( caddr_t ) begin;
1187  ( caddr_t ) ( begin + size );
1188  }
1189  t_index++;
1190  } else if ( ( perm[0] == 'r' ) && ( perm[1] == 'w' ) &&
1191  ( inode != 0 )
1192  &&
1193  ( strcmp
1194  ( mdi->exe_info.fullname,
1195  mapname ) == 0 ) ) {
1197  ( caddr_t ) begin;
1199  ( caddr_t ) ( begin + size );
1200  d_index++;
1201  } else if ( ( perm[0] == 'r' ) && ( perm[1] == 'w' ) &&
1202  ( inode == 0 )
1203  &&
1204  ( strcmp
1205  ( mdi->exe_info.fullname,
1206  lastmapname ) == 0 ) ) {
1208  ( caddr_t ) begin;
1210  ( caddr_t ) ( begin + size );
1211  b_index++;
1212  }
1213  } else if ( !counting ) {
1214  if ( ( perm[2] == 'x' ) && ( perm[0] == 'r' ) && ( inode != 0 ) ) {
1215  if ( strcmp( mdi->exe_info.fullname, mapname )
1216  != 0 ) {
1217  t_index++;
1218  tmp[t_index - 1].text_start = ( caddr_t ) begin;
1219  tmp[t_index - 1].text_end = ( caddr_t ) ( begin + size );
1220  strncpy( tmp[t_index - 1].name, mapname, PAPI_MAX_STR_LEN );
1221  }
1222  } else if ( ( perm[0] == 'r' ) && ( perm[1] == 'w' ) &&
1223  ( inode != 0 ) ) {
1224  if ( ( strcmp
1225  ( mdi->exe_info.fullname,
1226  mapname ) != 0 )
1227  && ( t_index > 0 ) &&
1228  ( tmp[t_index - 1].data_start == 0 ) ) {
1229  tmp[t_index - 1].data_start = ( caddr_t ) begin;
1230  tmp[t_index - 1].data_end = ( caddr_t ) ( begin + size );
1231  }
1232  } else if ( ( perm[0] == 'r' ) && ( perm[1] == 'w' ) &&
1233  ( inode == 0 ) ) {
1234  if ( ( t_index > 0 ) && ( tmp[t_index - 1].bss_start == 0 ) ) {
1235  tmp[t_index - 1].bss_start = ( caddr_t ) begin;
1236  tmp[t_index - 1].bss_end = ( caddr_t ) ( begin + size );
1237  }
1238  }
1239  }
1240  }
1241 
1242  if ( counting ) {
1243  /* When we get here, we have counted the number of entries in the map
1244  for us to allocate */
1245 
1246  tmp =
1247  ( PAPI_address_map_t * ) papi_calloc( t_index,
1248  sizeof
1249  ( PAPI_address_map_t ) );
1250  if ( tmp == NULL ) {
1251  PAPIERROR( "Error allocating shared library address map" );
1252  fclose(f);
1253  return PAPI_ENOMEM;
1254  }
1255  t_index = 0;
1256  rewind( f );
1257  counting = 0;
1258  goto again;
1259  } else {
1260  if ( mdi->shlib_info.map )
1261  papi_free( mdi->shlib_info.map );
1262  mdi->shlib_info.map = tmp;
1263  mdi->shlib_info.count = t_index;
1264 
1265  fclose( f );
1266  }
1267 
1268  return PAPI_OK;
1269 }
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
double f(double a)
Definition: cpi.c:23
#define papi_free(a)
Definition: papi_memory.h:35
caddr_t text_end
Definition: papi.h:698
return PAPI_OK
Definition: linux-nvml.c:497
caddr_t bss_start
Definition: papi.h:701
PAPI_exe_info_t exe_info
fclose(thread_wqfd)
#define PAPI_HUGE_STR_LEN
Definition: papi.h:467
PAPI_shlib_info_t shlib_info
char buf[200]
Definition: iozone.c:19609
char *long long size
Definition: iozone.c:12023
caddr_t text_start
Definition: papi.h:697
PAPI_address_map_t address_info
Definition: papi.h:709
pthread_attr_t foo
Definition: iozone.c:18592
get the executable&#39;s address space info
Definition: papi.h:695
void PAPIERROR(char *format,...)
#define PAPI_ENOMEM
Definition: papi.h:254
strcpy(filename, default_filename)
caddr_t data_start
Definition: papi.h:699
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
char * name
Definition: iozone.c:23648
PAPI_address_map_t * map
Definition: papi.h:714
caddr_t bss_end
Definition: papi.h:702
caddr_t data_end
Definition: papi.h:700
#define PAPI_MAX_STR_LEN
Definition: papi.h:465
char fullname[PAPI_HUGE_STR_LEN]
Definition: papi.h:708
long long tmp
Definition: iozone.c:12031
rewind(w_traj_fd)
#define papi_calloc(a, b)
Definition: papi_memory.h:37
void begin()

Here is the call graph for this function:

Here is the caller graph for this function:

int generic_get_memory_info ( PAPI_hw_info_t hw_info)

Definition at line 900 of file linux-memory.c.

901 {
902 
903  int type=0,level,result;
904  int size,line_size,associativity,sets;
905  DIR *dir;
906  FILE *fff;
907  char filename[BUFSIZ],type_string[BUFSIZ];
908  struct dirent *d;
909  int max_level=0;
910  int level_count=0,last_level=-1,level_index=0;
911 
912  PAPI_mh_level_t *L = hw_info->mem_hierarchy.level;
913 
914  /* open Linux cache dir */
915  /* assume all CPUs same as cpu0. */
916  /* Not necessarily a good assumption */
917 
918  dir=opendir("/sys/devices/system/cpu/cpu0/cache");
919  if (dir==NULL) {
920  goto unrecoverable_error;
921  }
922 
923  while(1) {
924  d = readdir(dir);
925  if (d==NULL) break;
926 
927  if (strncmp(d->d_name, "index", 5)) continue;
928 
929  MEMDBG("Found %s\n",d->d_name);
930 
931  /*************/
932  /* Get level */
933  /*************/
934  sprintf(filename,
935  "/sys/devices/system/cpu/cpu0/cache/%s/level",
936  d->d_name);
937  fff=fopen(filename,"r");
938  if (fff==NULL) {
939  MEMDBG("Cannot open level.\n");
940  goto unrecoverable_error;
941  }
942 
943  result=fscanf(fff,"%d",&level);
944  fclose(fff);
945  if (result!=1) {
946  MEMDBG("Could not read cache level\n");
947  goto unrecoverable_error;
948  }
949 
950  /* Index arrays from 0 */
951  level_index=level-1;
952 
953  if (level!=last_level) {
954  level_count=0;
955  last_level=level;
956  } else {
957  level_count++;
958  }
959 
960  if (level_count>=PAPI_MH_MAX_LEVELS) {
961  MEMDBG("Exceeded maximum levels %d\n",
963  break;
964  }
965 
966  /************/
967  /* Get type */
968  /************/
969  sprintf(filename,
970  "/sys/devices/system/cpu/cpu0/cache/%s/type",d->d_name);
971  fff=fopen(filename,"r");
972  if (fff==NULL) {
973  MEMDBG("Cannot open type\n");
974  goto unrecoverable_error;
975  }
976  result=fscanf(fff,"%s",type_string);
977  fclose(fff);
978  if (result!=1) {
979  MEMDBG("Could not read cache type\n");
980  goto unrecoverable_error;
981  }
982  if (!strcmp(type_string,"Data")) {
983  type=PAPI_MH_TYPE_DATA;
984  }
985  if (!strcmp(type_string,"Instruction")) {
986  type=PAPI_MH_TYPE_INST;
987  }
988  if (!strcmp(type_string,"Unified")) {
990  }
991  L[level_index].cache[level_count].type=type;
992 
993  /*************/
994  /* Get Size */
995  /*************/
996  sprintf(filename,
997  "/sys/devices/system/cpu/cpu0/cache/%s/size",d->d_name);
998  fff=fopen(filename,"r");
999  if (fff==NULL) {
1000  MEMDBG("Cannot open size\n");
1001  goto unrecoverable_error;
1002  }
1003  result=fscanf(fff,"%d",&size);
1004  fclose(fff);
1005  if (result!=1) {
1006  MEMDBG("Could not read cache size\n");
1007  goto unrecoverable_error;
1008  }
1009 
1010  /* Linux reports in kB, PAPI expects in Bytes */
1011  L[level_index].cache[level_count].size=size*1024;
1012 
1013  /*************/
1014  /* Line Size */
1015  /*************/
1016  sprintf(filename,
1017  "/sys/devices/system/cpu/cpu0/cache/%s/coherency_line_size",
1018  d->d_name);
1019  fff=fopen(filename,"r");
1020  if (fff==NULL) {
1021  MEMDBG("Cannot open linesize\n");
1022  goto unrecoverable_error;
1023  }
1024  result=fscanf(fff,"%d",&line_size);
1025  fclose(fff);
1026  if (result!=1) {
1027  MEMDBG("Could not read cache line-size\n");
1028  goto unrecoverable_error;
1029  }
1030  L[level_index].cache[level_count].line_size=line_size;
1031 
1032 
1033  /*********************/
1034  /* Get Associativity */
1035  /*********************/
1036  sprintf(filename,
1037  "/sys/devices/system/cpu/cpu0/cache/%s/ways_of_associativity",
1038  d->d_name);
1039  fff=fopen(filename,"r");
1040  if (fff==NULL) {
1041  MEMDBG("Cannot open associativity\n");
1042  goto unrecoverable_error;
1043  }
1044  result=fscanf(fff,"%d",&associativity);
1045  fclose(fff);
1046  if (result!=1) {
1047  MEMDBG("Could not read cache associativity\n");
1048  goto unrecoverable_error;
1049  }
1050  L[level_index].cache[level_count].associativity=associativity;
1051 
1052  /************/
1053  /* Get Sets */
1054  /************/
1055  sprintf(filename,
1056  "/sys/devices/system/cpu/cpu0/cache/%s/number_of_sets",
1057  d->d_name);
1058  fff=fopen(filename,"r");
1059  if (fff==NULL) {
1060  MEMDBG("Cannot open sets\n");
1061  goto unrecoverable_error;
1062  }
1063  result=fscanf(fff,"%d",&sets);
1064  fclose(fff);
1065 
1066  if (result!=1) {
1067  MEMDBG("Could not read cache sets\n");
1068  goto unrecoverable_error;
1069  }
1070  L[level_index].cache[level_count].num_lines=sets;
1071 
1072 
1073  if (((size*1024)/line_size/associativity)!=sets) {
1074  MEMDBG("Warning! sets %d != expected %d\n",
1075  sets,((size*1024)/line_size/associativity));
1076  }
1077 
1078  MEMDBG("\tL%d %s cache\n",level,type_string);
1079  MEMDBG("\t%d kilobytes\n",size);
1080  MEMDBG("\t%d byte linesize\n",line_size);
1081  MEMDBG("\t%d-way associative\n",associativity);
1082  MEMDBG("\t%d lines\n",sets);
1083  MEMDBG("\tUnknown inclusivity\n");
1084  MEMDBG("\tUnknown replacement algorithm\n");
1085  MEMDBG("\tUnknown if victim cache\n");
1086 
1087  if (level>max_level) max_level=level;
1088 
1089  if (level>=PAPI_MAX_MEM_HIERARCHY_LEVELS) {
1090  MEMDBG("Exceeded maximum cache level %d\n",
1092  break;
1093  }
1094  }
1095 
1096  hw_info->mem_hierarchy.levels = max_level;
1097 
1098  return 0;
1099 
1100 unrecoverable_error:
1101 
1102  /* Just say we have no cache */
1103  hw_info->mem_hierarchy.levels = 0;
1104 
1105  return 0;
1106 }
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int levels
Definition: papi.h:774
#define PAPI_MH_TYPE_INST
Definition: papi.h:730
PAPI_mh_info_t mem_hierarchy
Definition: papi.h:799
char filename[MAXNAMESIZE]
Definition: iozone.c:1360
fclose(thread_wqfd)
FILE * fff[MAX_EVENTS]
#define PAPI_MH_TYPE_DATA
Definition: papi.h:731
#define PAPI_MH_MAX_LEVELS
Definition: papi.h:745
PAPI_mh_cache_info_t cache[PAPI_MH_MAX_LEVELS]
Definition: papi.h:768
char *long long size
Definition: iozone.c:12023
PAPI_mh_level_t level[PAPI_MAX_MEM_HIERARCHY_LEVELS]
Definition: papi.h:775
#define MEMDBG(format, args...)
Definition: papi_debug.h:70
#define PAPI_MH_TYPE_UNIFIED
Definition: papi.h:734
#define PAPI_MAX_MEM_HIERARCHY_LEVELS
Definition: papi.h:746

Here is the call graph for this function:

Here is the caller graph for this function: