PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
darwin-common.c
Go to the documentation of this file.
1 #include <unistd.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <err.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <sys/utsname.h>
10 
11 #include <time.h>
12 #include <sys/time.h>
13 #include <sys/times.h>
14 
15 
16 #include "papi.h"
17 #include "papi_internal.h"
18 #include "papi_vector.h"
19 
20 #include "darwin-memory.h"
21 #include "darwin-common.h"
22 
23 #include "x86_cpuid_info.h"
24 
26 
27 /* The locks used by Darwin */
28 
29 #if defined(USE_PTHREAD_MUTEXES)
30 pthread_mutex_t _papi_hwd_lock_data[PAPI_MAX_LOCK];
31 #else
32 volatile unsigned int _papi_hwd_lock_data[PAPI_MAX_LOCK];
33 #endif
34 
35 
36 static int _darwin_init_locks(void) {
37 
38  int i;
39 
40  for ( i = 0; i < PAPI_MAX_LOCK; i++ ) {
41 #if defined(USE_PTHREAD_MUTEXES)
42  pthread_mutex_init(&_papi_hwd_lock_data[i],NULL);
43 #else
45 #endif
46  }
47 
48  return PAPI_OK;
49 }
50 
51 
52 int
53 _darwin_detect_hypervisor(char *virtual_vendor_name) {
54 
55  int retval=0;
56 
57 #if defined(__i386__)||defined(__x86_64__)
58  retval=_x86_detect_hypervisor(virtual_vendor_name);
59 #else
60  (void) virtual_vendor_name;
61 #endif
62 
63  return retval;
64 }
65 
66 
67 #define _PATH_SYS_SYSTEM "/sys/devices/system"
68 #define _PATH_SYS_CPU0 _PATH_SYS_SYSTEM "/cpu/cpu0"
69 
70 static char pathbuf[PATH_MAX] = "/";
71 
72 
73 static char *
74 search_cpu_info( FILE * f, char *search_str, char *line )
75 {
76  /* This function courtesy of Rudolph Berrendorf! */
77  /* See the home page for the German version of PAPI. */
78  char *s;
79 
80  while ( fgets( line, 256, f ) != NULL ) {
81  if ( strstr( line, search_str ) != NULL ) {
82  /* ignore all characters in line up to : */
83  for ( s = line; *s && ( *s != ':' ); ++s );
84  if ( *s )
85  return s;
86  }
87  }
88  return NULL;
89 }
90 
91 
92 
93 int
94 _darwin_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
95 {
96 
97  int mib[4];
98  size_t len;
99  char buffer[BUFSIZ];
100  long long ll;
101 
102  /* "sysctl -a" shows lots of info we can get on OSX */
103 
104  /**********/
105  /* Vendor */
106  /**********/
107  len = 3;
108  sysctlnametomib("machdep.cpu.vendor", mib, &len);
109 
110  len = BUFSIZ;
111  if (sysctl(mib, 3, &buffer, &len, NULL, 0) == -1) {
112  return PAPI_ESYS;
113  }
114  strncpy( hwinfo->vendor_string,buffer,len);
115 
116  hwinfo->vendor = PAPI_VENDOR_INTEL;
117 
118 
119  /**************/
120  /* Model Name */
121  /**************/
122  len = 3;
123  sysctlnametomib("machdep.cpu.brand_string", mib, &len);
124 
125  len = BUFSIZ;
126  if (sysctl(mib, 3, &buffer, &len, NULL, 0) == -1) {
127  return PAPI_ESYS;
128  }
129  strncpy( hwinfo->model_string,buffer,len);
130 
131  /************/
132  /* Revision */
133  /************/
134  len = 3;
135  sysctlnametomib("machdep.cpu.stepping", mib, &len);
136 
137  len = BUFSIZ;
138  if (sysctl(mib, 3, &buffer, &len, NULL, 0) == -1) {
139  return PAPI_ESYS;
140  }
141 
142  hwinfo->cpuid_stepping=buffer[0];
143  hwinfo->revision=(float)(hwinfo->cpuid_stepping);
144 
145  /**********/
146  /* Family */
147  /**********/
148  len = 3;
149  sysctlnametomib("machdep.cpu.family", mib, &len);
150 
151  len = BUFSIZ;
152  if (sysctl(mib, 3, &buffer, &len, NULL, 0) == -1) {
153  return PAPI_ESYS;
154  }
155 
156  hwinfo->cpuid_family=buffer[0];
157 
158  /**********/
159  /* Model */
160  /**********/
161  len = 3;
162  sysctlnametomib("machdep.cpu.model", mib, &len);
163 
164  len = BUFSIZ;
165  if (sysctl(mib, 3, &buffer, &len, NULL, 0) == -1) {
166  return PAPI_ESYS;
167  }
168 
169  hwinfo->cpuid_model=buffer[0];
170  hwinfo->model=hwinfo->cpuid_model;
171 
172  /*************/
173  /* Frequency */
174  /*************/
175  len = 2;
176  sysctlnametomib("hw.cpufrequency_max", mib, &len);
177 
178  len = 8;
179  if (sysctl(mib, 2, &ll, &len, NULL, 0) == -1) {
180  return PAPI_ESYS;
181  }
182 
183  hwinfo->cpu_max_mhz=(int)(ll/(1000*1000));
184 
185  len = 2;
186  sysctlnametomib("hw.cpufrequency_min", mib, &len);
187 
188  len = 8;
189  if (sysctl(mib, 2, &ll, &len, NULL, 0) == -1) {
190  return PAPI_ESYS;
191  }
192 
193  hwinfo->cpu_min_mhz=(int)(ll/(1000*1000));
194 
195  /**********/
196  /* ncpu */
197  /**********/
198  len = 2;
199  sysctlnametomib("hw.ncpu", mib, &len);
200 
201  len = BUFSIZ;
202  if (sysctl(mib, 2, &buffer, &len, NULL, 0) == -1) {
203  return PAPI_ESYS;
204  }
205 
206  hwinfo->totalcpus=buffer[0];
207 
208 
209  return PAPI_OK;
210 }
211 
212 
213 int
215 
216  int retval;
217 
218  char maxargs[PAPI_HUGE_STR_LEN];
219  pid_t pid;
220 
221  int cpuinfo_mhz,sys_min_khz,sys_max_khz;
222 
223  /* Software info */
224 
225  /* Path and args */
226 
227  pid = getpid( );
228  if ( pid < 0 ) {
229  PAPIERROR( "getpid() returned < 0" );
230  return PAPI_ESYS;
231  }
232  mdi->pid = pid;
233 
234 #if 0
235  sprintf( maxargs, "/proc/%d/exe", ( int ) pid );
236  if ( readlink( maxargs, mdi->exe_info.fullname, PAPI_HUGE_STR_LEN ) < 0 ) {
237  PAPIERROR( "readlink(%s) returned < 0", maxargs );
238  return PAPI_ESYS;
239  }
240 
241  /* Careful, basename can modify it's argument */
242 
243  strcpy( maxargs, mdi->exe_info.fullname );
244  strcpy( mdi->exe_info.address_info.name, basename( maxargs ) );
245 
246  SUBDBG( "Executable is %s\n", mdi->exe_info.address_info.name );
247  SUBDBG( "Full Executable is %s\n", mdi->exe_info.fullname );
248 
249  /* Executable regions, may require reading /proc/pid/maps file */
250 
251  retval = _darwin_update_shlib_info( mdi );
252  SUBDBG( "Text: Start %p, End %p, length %d\n",
255  ( int ) ( mdi->exe_info.address_info.text_end -
257  SUBDBG( "Data: Start %p, End %p, length %d\n",
260  ( int ) ( mdi->exe_info.address_info.data_end -
262  SUBDBG( "Bss: Start %p, End %p, length %d\n",
265  ( int ) ( mdi->exe_info.address_info.bss_end -
267 #endif
268  /* PAPI_preload_option information */
269 
270  strcpy( mdi->preload_info.lib_preload_env, "LD_PRELOAD" );
271  mdi->preload_info.lib_preload_sep = ' ';
272  strcpy( mdi->preload_info.lib_dir_env, "LD_LIBRARY_PATH" );
273  mdi->preload_info.lib_dir_sep = ':';
274 
275  /* Hardware info */
276 
277  retval = _darwin_get_cpu_info( &mdi->hw_info, &cpuinfo_mhz );
278  if ( retval ) {
279  return retval;
280  }
281 
282  /* Set Up Memory */
283 
284  retval = _darwin_get_memory_info( &mdi->hw_info, mdi->hw_info.model );
285  if ( retval )
286  return retval;
287 
288  SUBDBG( "Found %d %s(%d) %s(%d) CPUs at %d Mhz.\n",
289  mdi->hw_info.totalcpus,
290  mdi->hw_info.vendor_string,
291  mdi->hw_info.vendor,
292  mdi->hw_info.model_string,
293  mdi->hw_info.model,
294  mdi->hw_info.cpu_max_mhz);
295 
296  /* Get virtualization info */
298 
299  return PAPI_OK;
300 }
301 
302 int
304 
305  int major=0,minor=0,sub=0;
306  char *ptr;
307  struct utsname uname_buffer;
308 
309  /* Initialize the locks */
311 
312  /* Get the kernel info */
313  uname(&uname_buffer);
314 
315  SUBDBG("Native kernel version %s\n",uname_buffer.release);
316 
317  strncpy(_papi_os_info.name,uname_buffer.sysname,PAPI_MAX_STR_LEN);
318 
319  strncpy(_papi_os_info.version,uname_buffer.release,PAPI_MAX_STR_LEN);
320 
321  ptr=strtok(_papi_os_info.version,".");
322  if (ptr!=NULL) major=atoi(ptr);
323 
324  ptr=strtok(NULL,".");
325  if (ptr!=NULL) minor=atoi(ptr);
326 
327  ptr=strtok(NULL,".");
328  if (ptr!=NULL) sub=atoi(ptr);
329 
330  // _papi_os_info.os_version=LINUX_VERSION(major,minor,sub);
331 
332  _papi_os_info.itimer_sig = PAPI_INT_MPX_SIGNAL;
333  _papi_os_info.itimer_num = PAPI_INT_ITIMER;
334  _papi_os_info.itimer_ns = PAPI_INT_MPX_DEF_US * 1000;
335  _papi_os_info.itimer_res_ns = 1;
336  _papi_os_info.clock_ticks = sysconf( _SC_CLK_TCK );
337 
338  /* Get Darwin-specific system info */
340 
341  return PAPI_OK;
342 }
343 
344 
345 static inline long long
346 get_cycles( void )
347 {
348  long long ret = 0;
349 #ifdef __x86_64__
350  do {
351  unsigned int a, d;
352  asm volatile ( "rdtsc":"=a" ( a ), "=d"( d ) );
353  ( ret ) = ( ( long long ) a ) | ( ( ( long long ) d ) << 32 );
354  }
355  while ( 0 );
356 #else
357  __asm__ __volatile__( "rdtsc":"=A"( ret ): );
358 #endif
359  return ret;
360 }
361 
362 long long
364 {
365  long long retval;
366 
367  retval = get_cycles( );
368 
369  return retval;
370 }
371 
372 
373 long long
375 {
376 
377  long long retval;
378 
379  struct timeval buffer;
380  gettimeofday( &buffer, NULL );
381  retval = ( long long ) buffer.tv_sec * ( long long ) 1000000;
382  retval += ( long long ) ( buffer.tv_usec );
383 
384  return retval;
385 }
386 
387 
388 long long
390 {
391 
392  long long retval;
393 
394  struct tms buffer;
395 
396  times( &buffer );
397 
398  SUBDBG( "user %d system %d\n", ( int ) buffer.tms_utime,
399  ( int ) buffer.tms_stime );
400  retval = ( long long ) ( ( buffer.tms_utime + buffer.tms_stime ) *
401  1000000 / sysconf( _SC_CLK_TCK ));
402 
403  /* NOT CLOCKS_PER_SEC as in the headers! */
404 
405  return retval;
406 }
407 
408 
409 
410 
411 
414  .get_dmem_info = _darwin_get_dmem_info,
415  .get_real_cycles = _darwin_get_real_cycles,
416  .update_shlib_info = _darwin_update_shlib_info,
417  .get_system_info = _darwin_get_system_info,
418 
419  .get_real_usec = _darwin_get_real_usec_gettimeofday,
420  .get_virt_usec = _darwin_get_virt_usec_times,
421 
422 };
char name[PAPI_HUGE_STR_LEN]
Definition: papi.h:691
#define PAPI_HUGE_STR_LEN
Definition: fpapi.h:42
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int atoi()
long long _darwin_get_real_usec_gettimeofday(void)
double f(double a)
Definition: cpi.c:23
int _darwin_detect_hypervisor(char *virtual_vendor_name)
Definition: darwin-common.c:53
#define PAPI_INT_MPX_DEF_US
Definition: papi_internal.h:65
Hardware info structure.
Definition: papi.h:775
caddr_t text_end
Definition: papi.h:693
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
int cpu_min_mhz
Definition: papi.h:792
long long _darwin_get_real_cycles(void)
return PAPI_OK
Definition: linux-nvml.c:458
caddr_t bss_start
Definition: papi.h:696
PAPI_preload_info_t preload_info
PAPI_exe_info_t exe_info
void
Definition: iozone.c:18627
long long _darwin_get_virt_usec_times(void)
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
Return codes and api definitions.
int _darwin_update_shlib_info(papi_mdi_t *mdi)
Definition: darwin-memory.c:76
int cpuid_stepping
Definition: papi.h:789
#define PAPI_MAX_LOCK
Definition: papi_lock.h:18
char name[PAPI_MAX_STR_LEN]
long long ret
Definition: iozone.c:1346
char lib_preload_env[PAPI_MAX_STR_LEN]
Definition: papi.h:617
papi_os_vector_t _papi_os_vector
Definition: aix.c:1288
float revision
Definition: papi.h:786
static int _darwin_init_locks(void)
Definition: darwin-common.c:36
int i
Definition: fileop.c:140
int _darwin_get_system_info(papi_mdi_t *mdi)
PAPI_os_info_t _papi_os_info
Definition: aix.c:1210
static int pid
char virtual_vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:796
char lib_preload_sep
Definition: papi.h:618
caddr_t text_start
Definition: papi.h:692
PAPI_address_map_t address_info
Definition: papi.h:704
int _darwin_get_cpu_info(PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz)
Definition: darwin-common.c:94
static char * search_cpu_info(FILE *f, char *search_str, char *line)
Definition: darwin-common.c:74
int cpuid_model
Definition: papi.h:788
volatile unsigned int _papi_hwd_lock_data[PAPI_MAX_LOCK]
Definition: darwin-common.c:32
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
#define PAPI_INT_ITIMER
Definition: papi_internal.h:54
s
Definition: iozone.c:20289
long long
Definition: iozone.c:19827
void PAPIERROR(char *format,...)
char version[PAPI_MAX_STR_LEN]
int cpuid_family
Definition: papi.h:787
#define PAPI_INT_MPX_SIGNAL
Definition: papi_internal.h:52
#define PAPI_ESYS
Definition: fpapi.h:108
char vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:783
papi_mdi_t _papi_hwi_system_info
Definition: papi_internal.c:57
strcpy(filename, default_filename)
PAPI_hw_info_t hw_info
#define PAPI_VENDOR_INTEL
Definition: papi.h:346
caddr_t data_start
Definition: papi.h:694
int _x86_detect_hypervisor(char *vendor_name)
char * buffer
Definition: iozone.c:1366
int vendor
Definition: papi.h:782
int _papi_hwi_init_os(void)
Definition: aix.c:1213
#define MUTEX_OPEN
Definition: perfctr-ppc64.h:75
#define PATH_MAX
Definition: fileop.c:68
int model
Definition: papi.h:784
int cpu_max_mhz
Definition: papi.h:791
int
Definition: iozone.c:18528
caddr_t bss_end
Definition: papi.h:697
static long long get_cycles(void)
caddr_t data_end
Definition: papi.h:695
int totalcpus
Definition: papi.h:781
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:785
char fullname[PAPI_HUGE_STR_LEN]
Definition: papi.h:703
int virtualized
Definition: papi.h:795
int _darwin_get_memory_info(PAPI_hw_info_t *hwinfo, int cpu_type)
Definition: darwin-memory.c:65
ssize_t retval
Definition: libasync.c:338
char lib_dir_sep
Definition: papi.h:620
int(* get_memory_info)(PAPI_hw_info_t *, int)
Definition: papi_vector.h:69
char lib_dir_env[PAPI_MAX_STR_LEN]
Definition: papi.h:619
int _darwin_get_dmem_info(PAPI_dmem_info_t *d)
Definition: darwin-memory.c:13
static char pathbuf[PATH_MAX]
Definition: darwin-common.c:70
char * ptr
Definition: iozone.c:23586