PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
freebsd.c
Go to the documentation of this file.
1 /****************************/
2 /* THIS IS OPEN SOURCE CODE */
3 /****************************/
4 
5 /*
6 * File: freebsd.c
7 * Author: Harald Servat
8 * redcrash@gmail.com
9 */
10 
11 #include <sys/types.h>
12 #include <sys/resource.h>
13 #include <sys/sysctl.h>
14 #include <sys/utsname.h>
15 
16 #include "papi.h"
17 
18 #include "papi_internal.h"
19 
20 #include "papi_lock.h"
21 #include "freebsd.h"
22 #include "papi_vector.h"
23 
24 #include "map.h"
25 
26 #include "freebsd-memory.h"
27 #include "x86_cpuid_info.h"
28 
29 /* Global values referenced externally */
31 
32 /* Advance Declarations */
34 long long _papi_freebsd_get_real_cycles(void);
35 int _papi_freebsd_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len);
36 
37 
38 /* For debugging */
39 
40 static void show_counter(char *string, int id, char *name,
41  const char *function, char *file, int line) {
42 
43 #if defined(DEBUG)
44  pmc_value_t tmp_value;
45  int ret = pmc_read (id, &tmp_value);
46 
47  fprintf(stderr,"%s\n",string);
48  if (ret < 0) {
49  fprintf (stderr, "DEBUG: Unable to read counter %s (ID: %08x) "
50  "on routine %s (file: %s, line: %d)\n",
51  name, id, function,file,line);
52  } else {
53  fprintf (stderr, "DEBUG: Read counter %s (ID: %08x) - "
54  "value %llu on routine %s (file: %s, line: %d)\n",
55  name, id, (long long unsigned int)tmp_value,
56  function, file, line);
57  }
58 #else
59  (void) string; (void)name;
60  (void)id; (void)function; (void)file; (void)line;
61 #endif
62 }
63 
64 
66 
67 
68 /*
69  * This function is an internal function and not exposed and thus
70  * it can be called anything you want as long as the information
71  * is setup in _papi_freebsd_init_component. Below is some, but not
72  * all of the values that will need to be setup. For a complete
73  * list check out papi_mdi_t, though some of the values are setup
74  * and used above the component level.
75  */
76 int init_mdi(void)
77 {
78  const struct pmc_cpuinfo *info;
79 
80  SUBDBG("Entering\n");
81 
82  /* Initialize PMC library */
83  if (pmc_init() < 0)
84  return PAPI_ESYS;
85 
86  if (pmc_cpuinfo (&info) != 0)
87  return PAPI_ESYS;
88 
89  if (info != NULL)
90  {
91  /* Get CPU clock rate from HW.CLOCKRATE sysctl value, and
92  MODEL from HW.MODEL */
93  int mib[5];
94  size_t len;
95  int hw_clockrate;
96  char hw_model[PAPI_MAX_STR_LEN];
97 
98 #if !defined(__i386__) && !defined(__amd64__)
99  Context.use_rdtsc = FALSE;
100 #else
101  /* Ok, I386s/AMD64s can use RDTSC. But be careful, if the cpufreq
102  module is loaded, then CPU frequency can vary and this method
103  does not work properly! We'll use use_rdtsc to know if this
104  method is available */
105  len = 5;
106  Context.use_rdtsc = sysctlnametomib ("dev.cpufreq.0.%driver", mib, &len) == -1;
107 #endif
108 
109  len = 3;
110  if (sysctlnametomib ("hw.clockrate", mib, &len) == -1)
111  return PAPI_ESYS;
112  len = sizeof(hw_clockrate);
113  if (sysctl (mib, 2, &hw_clockrate, &len, NULL, 0) == -1)
114  return PAPI_ESYS;
115 
116  len = 3;
117  if (sysctlnametomib ("hw.model", mib, &len) == -1)
118  return PAPI_ESYS;
119  len = PAPI_MAX_STR_LEN;
120  if (sysctl (mib, 2, &hw_model, &len, NULL, 0) == -1)
121  return PAPI_ESYS;
122 
123  /*strcpy (_papi_hwi_system_info.hw_info.vendor_string, pmc_name_of_cputype(info->pm_cputype));*/
124  sprintf (_papi_hwi_system_info.hw_info.vendor_string, "%s (TSC:%c)", pmc_name_of_cputype(info->pm_cputype), Context.use_rdtsc?'Y':'N');
126  _papi_hwi_system_info.hw_info.mhz = (float) hw_clockrate;
129  _papi_hwi_system_info.hw_info.ncpu = info->pm_ncpu;
131  _papi_hwi_system_info.hw_info.totalcpus = info->pm_ncpu;
132  /* Right now, PMC states that TSC is an additional counter. However
133  it's only available as a system-wide counter and this requires
134  root access */
135  _papi_freebsd_vector.cmp_info.num_cntrs = info->pm_npmc - 1;
136 
137  if ( strstr(pmc_name_of_cputype(info->pm_cputype), "INTEL"))
139  else if ( strstr(pmc_name_of_cputype(info->pm_cputype), "AMD"))
141  else
142  fprintf(stderr,"We didn't actually find a supported vendor...\n\n\n");
143  }
144  else
145  return PAPI_ESYS;
146 
147  return 1;
148 }
149 
150 
152 {
153  const struct pmc_cpuinfo *info;
154 
155  SUBDBG("Entering\n");
156 
157  if (pmc_cpuinfo (&info) != 0)
158  return PAPI_ESYS;
159 
161 
162  if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_P6") == 0)
163  Context.CPUtype = CPU_P6;
164 
165  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_PII") == 0)
166  Context.CPUtype = CPU_P6_2;
167  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_PIII") == 0)
168  Context.CPUtype = CPU_P6_3;
169  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_CL") == 0)
170  Context.CPUtype = CPU_P6_C;
171  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_PM") == 0)
172  Context.CPUtype = CPU_P6_M;
173  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "AMD_K7") == 0)
174  Context.CPUtype = CPU_K7;
175  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "AMD_K8") == 0)
176  Context.CPUtype = CPU_K8;
177  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_PIV") == 0)
178  Context.CPUtype = CPU_P4;
179  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_ATOM") == 0)
180  Context.CPUtype = CPU_ATOM;
181  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_CORE") == 0)
182  Context.CPUtype = CPU_CORE;
183  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_CORE2") == 0)
184  Context.CPUtype = CPU_CORE2;
185  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_CORE2EXTREME") == 0)
186  Context.CPUtype = CPU_CORE2EXTREME;
187  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_COREI7") == 0)
188  Context.CPUtype = CPU_COREI7;
189  else if (strcmp(pmc_name_of_cputype(info->pm_cputype), "INTEL_WESTMERE") == 0)
190  Context.CPUtype = CPU_COREWESTMERE;
191  else
192  /* Unknown processor! */
193  Context.CPUtype = CPU_UNKNOWN;
194 
195 
196  _papi_freebsd_vector.cmp_info.num_native_events = freebsd_number_of_events (Context.CPUtype);
197  _papi_freebsd_vector.cmp_info.attach = 0;
198 
199  _papi_load_preset_table((char *)pmc_name_of_cputype(info->pm_cputype),
200  0,cidx);
201 
202  return 0;
203 }
204 
205 /*
206  * Component setup and shutdown
207  */
208 
209 /* Initialize hardware counters, setup the function vector table
210  * and get hardware information, this routine is called when the
211  * PAPI process is initialized (IE PAPI_library_init)
212  */
214 {
215  (void)cidx;
216 
217  int retval;
218 
219  SUBDBG("Entering\n");
220 
221  /* Internal function, doesn't necessarily need to be a function */
222  retval=init_presets(cidx);
223 
224  return retval;
225 }
226 
227 
228 
229 
230 /*
231  * This is called whenever a thread is initialized
232  */
234 {
235  (void)ctx;
236  SUBDBG("Entering\n");
237  return PAPI_OK;
238 }
239 
241 {
242  (void)ctx;
243  SUBDBG("Entering\n");
244  return PAPI_OK;
245 }
246 
248 {
249  SUBDBG("Entering\n");
250  return PAPI_OK;
251 }
252 
253 
254 /*
255  * Control of counters (Reading/Writing/Starting/Stopping/Setup)
256  * functions
257  */
259 {
260  /* We will default to gather counters in USER|KERNEL mode */
261  SUBDBG("Entering\n");
263  ptr->pmcs = NULL;
264  ptr->counters = NULL;
265  ptr->n_counters = 0;
266  return PAPI_OK;
267 }
268 
270 {
271  char name[1024];
272  int i;
273  int res;
274  (void)ctx;
275 
276  SUBDBG("Entering\n");
277 
278  /* We're going to store which counters are being used in this EventSet.
279  As this ptr structure can be reused within many PAPI_add_event calls,
280  and domain can change we will reconstruct the table of counters
281  (ptr->counters) everytime where here.
282  */
283  if (ptr->counters != NULL && ptr->n_counters > 0)
284  {
285  for (i = 0; i < ptr->n_counters; i++)
286  if (ptr->counters[i] != NULL)
287  free (ptr->counters[i]);
288  free (ptr->counters);
289  }
290  if (ptr->pmcs != NULL)
291  free (ptr->pmcs);
292  if (ptr->values != NULL)
293  free (ptr->values);
294  if (ptr->caps != NULL)
295  free (ptr->caps);
296 
297  ptr->n_counters = count;
298  ptr->pmcs = (pmc_id_t*) malloc (sizeof(pmc_id_t)*count);
299  ptr->caps = (uint32_t*) malloc (sizeof(uint32_t)*count);
300  ptr->values = (pmc_value_t*) malloc (sizeof(pmc_value_t)*count);
301  ptr->counters = (char **) malloc (sizeof(char*)*count);
302  for (i = 0; i < count; i++)
303  ptr->counters[i] = NULL;
304 
305  for (i = 0; i < count; i++)
306  {
307  res = _papi_freebsd_ntv_code_to_name (native[i].ni_event, name, sizeof(name));
308  if (res != PAPI_OK)
309  return res;
310 
311  native[i].ni_position = i;
312 
313  /* Domains can be applied to canonical events in libpmc (not "generic") */
314  if (Context.CPUtype != CPU_UNKNOWN)
315  {
317  {
318  /* PMC defaults domain to OS & User. So simply copy the name of the counter */
319  ptr->counters[i] = strdup (name);
320  if (ptr->counters[i] == NULL)
321  return PAPI_ESYS;
322  }
323  else if (ptr->hwc_domain == PAPI_DOM_USER)
324  {
325  /* This is user-domain case. Just add unitmask=usr */
326  ptr->counters[i] = malloc ((strlen(name)+strlen(",usr")+1)*sizeof(char));
327  if (ptr->counters[i] == NULL)
328  return PAPI_ESYS;
329  sprintf (ptr->counters[i], "%s,usr", name);
330  }
331  else /* if (ptr->hwc_domain == PAPI_DOM_KERNEL) */
332  {
333  /* This is the last case. Just add unitmask=os */
334  ptr->counters[i] = malloc ((strlen(name)+strlen(",os")+1)*sizeof(char));
335  if (ptr->counters[i] == NULL)
336  return PAPI_ESYS;
337  sprintf (ptr->counters[i], "%s,os", name);
338  }
339  }
340  else
341  {
342  /* PMC defaults domain to OS & User. So simply copy the name of the counter */
343  ptr->counters[i] = strdup (name);
344  if (ptr->counters[i] == NULL)
345  return PAPI_ESYS;
346  }
347  }
348 
349  return PAPI_OK;
350 }
351 
353 {
354  int i, ret;
355  (void)ctx;
356 
357  SUBDBG("Entering\n");
358 
359  for (i = 0; i < ctrl->n_counters; i++)
360  {
361  if ((ret = pmc_allocate (ctrl->counters[i], PMC_MODE_TC, 0, PMC_CPU_ANY, &(ctrl->pmcs[i]))) < 0)
362  {
363 #if defined(DEBUG)
364  /* This shouldn't happen, it's tested previously on _papi_freebsd_allocate_registers */
365  fprintf (stderr, "DEBUG: %s FAILED to allocate '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
366 #endif
367  return PAPI_ESYS;
368  }
369  if ((ret = pmc_capabilities (ctrl->pmcs[i],&(ctrl->caps[i]))) < 0)
370  {
371 #if defined(DEBUG)
372  fprintf (stderr, "DEBUG: %s FAILED to get capabilites for '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
373 #endif
374  ctrl->caps[i] = 0;
375  }
376 #if defined(DEBUG)
377  fprintf (stderr, "DEBUG: %s got counter '%s' is %swrittable! [%d of %d]\n", FUNC, ctrl->counters[i], (ctrl->caps[i]&PMC_CAP_WRITE)?"":"NOT", i+1, ctrl->n_counters);
378 #endif
379  if ((ret = pmc_start (ctrl->pmcs[i])) < 0)
380  {
381 #if defined(DEBUG)
382  fprintf (stderr, "DEBUG: %s FAILED to start '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
383 #endif
384  return PAPI_ESYS;
385  }
386  }
387  return PAPI_OK;
388 }
389 
391 {
392  int i, ret;
393  (void)ctx;
394  (void)flags;
395 
396  SUBDBG("Entering\n");
397 
398  for (i = 0; i < ctrl->n_counters; i++)
399  if ((ret = pmc_read (ctrl->pmcs[i], &(ctrl->values[i]))) < 0)
400  {
401 #if defined(DEBUG)
402  fprintf (stderr, "DEBUG: %s FAILED to read '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
403 #endif
404  return PAPI_ESYS;
405  }
406  *events = (long long *)ctrl->values;
407 
408 #if defined(DEBUG)
409  for (i = 0; i < ctrl->n_counters; i++)
410  fprintf (stderr, "DEBUG: %s counter '%s' has value %lld\n",
411  FUNC, ctrl->counters[i], (long long)ctrl->values[i]);
412 #endif
413  return PAPI_OK;
414 }
415 
417 {
418  int i, ret;
419  (void)ctx;
420 
421  SUBDBG("Entering\n");
422 
423  for (i = 0; i < ctrl->n_counters; i++)
424  {
425  if ((ret = pmc_stop (ctrl->pmcs[i])) < 0)
426  {
427 #if defined(DEBUG)
428  fprintf (stderr, "DEBUG: %s FAILED to stop '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
429 #endif
430  return PAPI_ESYS;
431  }
432  if ((ret = pmc_release (ctrl->pmcs[i])) < 0)
433  {
434 #if defined(DEBUG)
435  /* This shouldn't happen, it's tested previously on _papi_freebsd_allocate_registers */
436  fprintf (stderr, "DEBUG: %s FAILED to release '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
437 #endif
438  return PAPI_ESYS;
439  }
440  }
441  return PAPI_OK;
442 }
443 
445 {
446  int i, ret;
447  (void)ctx;
448 
449  SUBDBG("Entering\n");
450 
451  for (i = 0; i < ctrl->n_counters; i++)
452  {
453  /* Can we write on the counters? */
454  if (ctrl->caps[i] & PMC_CAP_WRITE)
455  {
456  show_counter("DEBUG: _papi_freebsd_reset is about "
457  "to stop the counter i+1",
458  ctrl->pmcs[i],ctrl->counters[i],
459  __FUNCTION__,__FILE__,__LINE__);
460 
461  if ((ret = pmc_stop (ctrl->pmcs[i])) < 0)
462  {
463 #if defined(DEBUG)
464  fprintf (stderr, "DEBUG: %s FAILED to stop '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
465 #endif
466  return PAPI_ESYS;
467  }
468 
469  show_counter(
470  "DEBUG: _papi_freebsd_reset is about "
471  "to write the counter i+1\n",
472  ctrl->pmcs[i],ctrl->counters[i],
473  __FUNCTION__,__FILE__,__LINE__);
474 
475  if ((ret = pmc_write (ctrl->pmcs[i], 0)) < 0)
476  {
477 #if defined(DEBUG)
478  fprintf (stderr, "DEBUG: %s FAILED to write '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
479 #endif
480  return PAPI_ESYS;
481  }
482 
483  show_counter("DEBUG: _papi_freebsd_reset is about to "
484  "start the counter %i+1",
485  ctrl->pmcs[i],ctrl->counters[i],
486  __FUNCTION__,__FILE__,__LINE__);
487 
488  if ((ret = pmc_start (ctrl->pmcs[i])) < 0)
489  {
490 #if defined(DEBUG)
491  fprintf (stderr, "DEBUG: %s FAILED to start '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
492 #endif
493  return PAPI_ESYS;
494  }
495 
496  show_counter("DEBUG: _papi_freebsd_reset after "
497  "starting the counter i+1",
498  ctrl->pmcs[i],ctrl->counters[i],
499  __FUNCTION__,__FILE__,__LINE__);
500 
501  }
502  else
503  return PAPI_ECMP;
504  }
505  return PAPI_OK;
506 }
507 
508 int _papi_freebsd_write(hwd_context_t *ctx, hwd_control_state_t *ctrl, long long *from)
509 {
510  int i, ret;
511  (void)ctx;
512 
513  SUBDBG("Entering\n");
514 
515  for (i = 0; i < ctrl->n_counters; i++)
516  {
517  /* Can we write on the counters? */
518  if (ctrl->caps[i] & PMC_CAP_WRITE)
519  {
520  if ((ret = pmc_stop (ctrl->pmcs[i])) < 0)
521  {
522 #if defined(DEBUG)
523  fprintf (stderr, "DEBUG: %s FAILED to stop '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
524 #endif
525  return PAPI_ESYS;
526  }
527  if ((ret = pmc_write (ctrl->pmcs[i], from[i])) < 0)
528  {
529 #if defined(DEBUG)
530  fprintf (stderr, "DEBUG: %s FAILED to write '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
531 #endif
532  return PAPI_ESYS;
533  }
534  if ((ret = pmc_start (ctrl->pmcs[i])) < 0)
535  {
536 #if defined(DEBUG)
537  fprintf (stderr, "DEBUG: %s FAILED to stop '%s' [%d of %d] ERROR = %d\n", FUNC, ctrl->counters[i], i+1, ctrl->n_counters, ret);
538 #endif
539  return PAPI_ESYS;
540  }
541  }
542  else
543  return PAPI_ECMP;
544  }
545  return PAPI_OK;
546 }
547 
548 /*
549  * Overflow and profile functions
550  */
551 void _papi_freebsd_dispatch_timer(int signal, hwd_siginfo_t * info, void *context)
552 {
553  (void)signal;
554  (void)info;
555  (void)context;
556  /* Real function would call the function below with the proper args
557  * _papi_hwi_dispatch_overflow_signal(...);
558  */
559  SUBDBG("Entering\n");
560  return;
561 }
562 
564 {
565  (void)master;
566  (void)ESI;
567  SUBDBG("Entering\n");
568  return PAPI_OK;
569 }
570 
572 {
573  (void)ESI;
574  (void)EventIndex;
575  (void)threshold;
576  SUBDBG("Entering\n");
577  return PAPI_OK;
578 }
579 
580 int _papi_freebsd_set_profile(EventSetInfo_t *ESI, int EventIndex, int threashold)
581 {
582  (void)ESI;
583  (void)EventIndex;
584  (void)threashold;
585  SUBDBG("Entering\n");
586  return PAPI_OK;
587 }
588 
589 /*
590  * Functions for setting up various options
591  */
592 
593 /*
594  * This function has to set the bits needed to count different domains
595  * In particular: PAPI_DOM_USER, PAPI_DOM_KERNEL PAPI_DOM_OTHER
596  * By default return PAPI_EINVAL if none of those are specified
597  * and PAPI_OK with success
598  * PAPI_DOM_USER is only user context is counted
599  * PAPI_DOM_KERNEL is only the Kernel/OS context is counted
600  * PAPI_DOM_OTHER is Exception/transient mode (like user TLB misses)
601  * PAPI_DOM_ALL is all of the domains
602  */
604 {
605  int found = 0;
606 
607  SUBDBG("Entering\n");
608  /* libpmc supports USER/KERNEL mode only when counters are native */
609  if (Context.CPUtype != CPU_UNKNOWN)
610  {
611  if (domain & (PAPI_DOM_USER|PAPI_DOM_KERNEL))
612  {
613  cntrl->hwc_domain = domain & (PAPI_DOM_USER|PAPI_DOM_KERNEL);
614  found = 1;
615  }
616  return found?PAPI_OK:PAPI_EINVAL;
617  }
618  else
619  return PAPI_ECMP;
620 }
621 
622 
623 /* This function sets various options in the component
624  * The valid codes being passed in are PAPI_SET_DEFDOM,
625  * PAPI_SET_DOMAIN, PAPI_SETDEFGRN, PAPI_SET_GRANUL * and PAPI_SET_INHERIT
626  */
628 {
629  (void)ctx;
630  SUBDBG("Entering\n");
631  switch (code)
632  {
633  case PAPI_DOMAIN:
634  case PAPI_DEFDOM:
635  /*return _papi_freebsd_set_domain(&option->domain.ESI->machdep, option->domain.domain);*/
636  return _papi_freebsd_set_domain(option->domain.ESI->ctl_state, option->domain.domain);
637  case PAPI_GRANUL:
638  case PAPI_DEFGRN:
639  return PAPI_ECMP;
640  default:
641  return PAPI_EINVAL;
642  }
643 }
644 
645 
646 /*
647  * Timing Routines
648  * These functions should return the highest resolution timers available.
649  */
651 {
652  /* Hey, I've seen somewhere a define called __x86_64__! Should I support it? */
653 #if !defined(__i386__) && !defined(__amd64__)
654  /* This will surely work, but with low precision and high overhead */
655  struct rusage res;
656 
657  SUBDBG("Entering\n");
658  if ((getrusage(RUSAGE_SELF, &res) == -1))
659  return PAPI_ESYS;
660  return (res.ru_utime.tv_sec * 1000000) + res.ru_utime.tv_usec;
661 #else
662  SUBDBG("Entering\n");
663  if (Context.use_rdtsc)
664  {
666  }
667  else
668  {
669  struct rusage res;
670  if ((getrusage(RUSAGE_SELF, &res) == -1))
671  return PAPI_ESYS;
672  return (res.ru_utime.tv_sec * 1000000) + res.ru_utime.tv_usec;
673  }
674 #endif
675 }
676 
677 
679 {
680  /* Hey, I've seen somewhere a define called __x86_64__! Should I support it? */
681 #if !defined(__i386__) && !defined(__amd64__)
682  SUBDBG("Entering\n");
683  /* This will surely work, but with low precision and high overhead */
685 #else
686  SUBDBG("Entering\n");
687  if (Context.use_rdtsc)
688  {
689  long long cycles;
690  __asm __volatile(".byte 0x0f, 0x31" : "=A" (cycles));
691  return cycles;
692  }
693  else
694  {
696  }
697 #endif
698 }
699 
700 
701 
703 {
704  struct rusage res;
705 
706  SUBDBG("Entering\n");
707 
708  if ((getrusage(RUSAGE_SELF, &res) == -1))
709  return PAPI_ESYS;
710  return (res.ru_utime.tv_sec * 1000000) + res.ru_utime.tv_usec;
711 }
712 
713 /*
714  * Native Event functions
715  */
716 
717 
718 int _papi_freebsd_ntv_enum_events(unsigned int *EventCode, int modifier)
719 {
720  int res;
721  char name[1024];
722  unsigned int nextCode = 1 + *EventCode;
723 
724  SUBDBG("Entering\n");
725 
726  if (modifier==PAPI_ENUM_FIRST) {
727 
728  *EventCode=0;
729 
730  return PAPI_OK;
731  }
732 
733  if (modifier==PAPI_ENUM_EVENTS) {
734 
735  res = _papi_freebsd_ntv_code_to_name(nextCode, name, sizeof(name));
736  if (res != PAPI_OK) {
737  return res;
738  } else {
739  *EventCode = nextCode;
740  }
741  return PAPI_OK;
742  }
743 
744  return PAPI_ENOEVNT;
745 
746 }
747 
748 int _papi_freebsd_ntv_name_to_code(char *name, unsigned int *event_code) {
749 
750  SUBDBG("Entering\n");
751 
752  int i;
753 
754  for(i = 0; i < _papi_freebsd_vector.cmp_info.num_native_events; i++) {
755  if (strcmp (name, _papi_hwd_native_info[Context.CPUtype].info[i].name) == 0) {
756  *event_code = i;
757  return PAPI_OK;
758  }
759  }
760  return PAPI_ENOEVNT;
761 }
762 
763 int _papi_freebsd_ntv_code_to_name(unsigned int EventCode, char *ntv_name,
764  int len)
765 {
766  SUBDBG("Entering\n");
767 
768  int nidx;
769 
770  nidx = EventCode & PAPI_NATIVE_AND_MASK;
771 
772  if (nidx >= _papi_freebsd_vector.cmp_info.num_native_events) {
773  return PAPI_ENOEVNT;
774  }
775 
776  strncpy (ntv_name,
777  _papi_hwd_native_info[Context.CPUtype].info[nidx].name, len);
778  if (strlen(_papi_hwd_native_info[Context.CPUtype].info[nidx].name) > (size_t)len-1) {
779  return PAPI_EBUF;
780  }
781  return PAPI_OK;
782 }
783 
784 int _papi_freebsd_ntv_code_to_descr(unsigned int EventCode, char *descr, int len)
785 {
786  SUBDBG("Entering\n");
787  int nidx;
788 
789  nidx = EventCode & PAPI_NATIVE_AND_MASK;
790  if (nidx >= _papi_freebsd_vector.cmp_info.num_native_events) {
791  return PAPI_ENOEVNT;
792  }
793 
794  strncpy (descr, _papi_hwd_native_info[Context.CPUtype].info[nidx].description, len);
795  if (strlen(_papi_hwd_native_info[Context.CPUtype].info[nidx].description) > (size_t)len-1) {
796  return PAPI_EBUF;
797  }
798  return PAPI_OK;
799 }
800 
801 
802 /*
803  * Counter Allocation Functions, only need to implement if
804  * the component needs smart counter allocation.
805  */
806 
807 /* Here we'll check if PMC can provide all the counters the user want */
809 {
810  char name[1024];
811  int failed, allocated_counters, i, j, ret;
812  pmc_id_t *pmcs;
813 
814  SUBDBG("Entering\n");
815 
816  failed = 0;
817  pmcs = (pmc_id_t*) malloc(sizeof(pmc_id_t)*ESI->NativeCount);
818  if (pmcs != NULL)
819  {
820  allocated_counters = 0;
821  /* Check if we can allocate all the counters needed */
822  for (i = 0; i < ESI->NativeCount; i++)
823  {
824  ret = _papi_freebsd_ntv_code_to_name (ESI->NativeInfoArray[i].ni_event, name, sizeof(name));
825  if (ret != PAPI_OK)
826  return ret;
827  if ( (ret = pmc_allocate (name, PMC_MODE_TC, 0, PMC_CPU_ANY, &pmcs[i])) < 0)
828  {
829 #if defined(DEBUG)
830  fprintf (stderr, "DEBUG: %s FAILED to allocate '%s' (%#08x) [%d of %d] ERROR = %d\n", FUNC, name, ESI->NativeInfoArray[i].ni_event, i+1, ESI->NativeCount, ret);
831 #endif
832  failed = 1;
833  break;
834  }
835  else
836  {
837 #if defined(DEBUG)
838  fprintf (stderr, "DEBUG: %s SUCCEEDED allocating '%s' (%#08x) [%d of %d]\n", FUNC, name, ESI->NativeInfoArray[i].ni_event, i+1, ESI->NativeCount);
839 #endif
840  allocated_counters++;
841  }
842  }
843  /* Free the counters */
844  for (j = 0; j < allocated_counters; j++)
845  pmc_release (pmcs[j]);
846  free (pmcs);
847  }
848  else
849  failed = 1;
850 
851  return failed?PAPI_ECNFLCT:PAPI_OK;
852 }
853 
854 /*
855  * Shared Library Information and other Information Functions
856  */
858  SUBDBG("Entering\n");
859  (void)mdi;
860  return PAPI_OK;
861 }
862 
863 
864 
865 int
866 _papi_freebsd_detect_hypervisor(char *virtual_vendor_name) {
867 
868  int retval=0;
869 
870 #if defined(__i386__)||defined(__x86_64__)
871  retval=_x86_detect_hypervisor(virtual_vendor_name);
872 #else
873  (void) virtual_vendor_name;
874 #endif
875 
876  return retval;
877 }
878 
879 
880 
881 int
883 
884  int retval;
885 
886  retval=_freebsd_get_memory_info(&mdi->hw_info, mdi->hw_info.model );
887 
888  /* Get virtualization info */
890 
891 
892  return PAPI_OK;
893 
894 }
895 
896 int
898 
899  struct utsname uname_buffer;
900 
901  /* Internal function, doesn't necessarily need to be a function */
902  init_mdi();
903 
904  uname(&uname_buffer);
905 
906  strncpy(_papi_os_info.name,uname_buffer.sysname,PAPI_MAX_STR_LEN);
907 
908  strncpy(_papi_os_info.version,uname_buffer.release,PAPI_MAX_STR_LEN);
909 
910  _papi_os_info.itimer_sig = PAPI_INT_MPX_SIGNAL;
911  _papi_os_info.itimer_num = PAPI_INT_ITIMER;
912  _papi_os_info.itimer_ns = PAPI_INT_MPX_DEF_US * 1000; /* Not actually supported */
913  _papi_os_info.itimer_res_ns = 1;
914 
916 
917  return PAPI_OK;
918 }
919 
920 papi_vector_t _papi_freebsd_vector = {
921  .cmp_info = {
922  /* default component information (unspecified values are initialized to 0) */
923  .name = "FreeBSD",
924  .description = "FreeBSD CPU counters",
925  .default_domain = PAPI_DOM_USER,
926  .available_domains = PAPI_DOM_USER | PAPI_DOM_KERNEL,
927  .default_granularity = PAPI_GRN_THR,
928  .available_granularities = PAPI_GRN_THR,
929 
930  .hardware_intr = 1,
931  .kernel_multiplex = 1,
932  .kernel_profile = 1,
933  .num_mpx_cntrs = HWPMC_NUM_COUNTERS, /* ?? */
934  .hardware_intr_sig = PAPI_INT_SIGNAL,
935 
936  /* component specific cmp_info initializations */
937  .fast_real_timer = 1,
938  .fast_virtual_timer = 0,
939  .attach = 0,
940  .attach_must_ptrace = 0,
941  } ,
942  .size = {
943  .context = sizeof( hwd_context_t ),
944  .control_state = sizeof( hwd_control_state_t ),
945  .reg_value = sizeof( hwd_register_t ),
946  .reg_alloc = sizeof( hwd_reg_alloc_t )
947  },
948 
949  .dispatch_timer = _papi_freebsd_dispatch_timer,
950  .start = _papi_freebsd_start,
951  .stop = _papi_freebsd_stop,
952  .read = _papi_freebsd_read,
953  .reset = _papi_freebsd_reset,
954  .write = _papi_freebsd_write,
955  .stop_profiling = _papi_freebsd_stop_profiling,
956  .init_component = _papi_freebsd_init_component,
957  .init_thread = _papi_freebsd_init_thread,
958  .init_control_state = _papi_freebsd_init_control_state,
959  .update_control_state = _papi_freebsd_update_control_state,
960  .ctl = _papi_freebsd_ctl,
961  .set_overflow = _papi_freebsd_set_overflow,
962  .set_profile = _papi_freebsd_set_profile,
963  .set_domain = _papi_freebsd_set_domain,
964 
965  .ntv_enum_events = _papi_freebsd_ntv_enum_events,
966  .ntv_name_to_code = _papi_freebsd_ntv_name_to_code,
967  .ntv_code_to_name = _papi_freebsd_ntv_code_to_name,
968  .ntv_code_to_descr = _papi_freebsd_ntv_code_to_descr,
969 
970  .allocate_registers = _papi_freebsd_allocate_registers,
971 
972  .shutdown_thread = _papi_freebsd_shutdown_thread,
973  .shutdown_component = _papi_freebsd_shutdown_component,
974 };
975 
978  .get_real_cycles = _papi_freebsd_get_real_cycles,
979  .get_real_usec = _papi_freebsd_get_real_usec,
980  .get_virt_usec = _papi_freebsd_get_virt_usec,
981  .update_shlib_info = _papi_freebsd_update_shlib_info,
982  .get_system_info = _papi_freebsd_get_system_info,
983 };
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:625
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
#define FUNC
Definition: papi_debug.h:50
Definition: map.h:28
int _papi_freebsd_stop(hwd_context_t *ctx, hwd_control_state_t *ctrl)
Definition: freebsd.c:416
Native_Event_LabelDescription_t * info
Definition: map.h:47
int _papi_freebsd_ntv_enum_events(unsigned int *EventCode, int modifier)
Definition: freebsd.c:718
static void show_counter(char *string, int id, char *name, const char *function, char *file, int line)
Definition: freebsd.c:40
#define PAPI_INT_MPX_DEF_US
Definition: papi_internal.h:65
int _papi_freebsd_set_overflow(EventSetInfo_t *ESI, int EventIndex, int threshold)
Definition: freebsd.c:571
long long flags
Definition: iozone.c:12330
unsigned int attach
Definition: papi.h:657
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
int _papi_freebsd_write(hwd_context_t *ctx, hwd_control_state_t *ctrl, long long *from)
Definition: freebsd.c:508
#define PAPI_ENOEVNT
Definition: fpapi.h:112
int cpu_min_mhz
Definition: papi.h:792
papi_vector_t _papi_freebsd_vector
Definition: freebsd.c:33
pmc_value_t * values
Definition: freebsd.h:58
void init_freebsd_libpmc_mappings(void)
Definition: map.c:19
Definition: map.h:30
int _papi_freebsd_stop_profiling(ThreadInfo_t *master, EventSetInfo_t *ESI)
Definition: freebsd.c:563
for(i=0;i<=max_y;i++)
Definition: iozone.c:11615
long long _papi_freebsd_get_real_usec(void)
Definition: freebsd.c:650
Native_Event_Info_t _papi_hwd_native_info[CPU_LAST+1]
Definition: map.c:17
int _papi_freebsd_shutdown_component(void)
Definition: freebsd.c:247
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
#define PAPI_DOM_KERNEL
Definition: fpapi.h:22
unsigned * caps
Definition: freebsd.h:56
void
Definition: iozone.c:18627
return PAPI_EINVAL
Definition: linux-nvml.c:408
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
pmc_id_t * pmcs
Definition: freebsd.h:57
int _papi_freebsd_set_profile(EventSetInfo_t *ESI, int EventIndex, int threashold)
Definition: freebsd.c:580
#define PAPI_ECNFLCT
Definition: fpapi.h:113
Return codes and api definitions.
char name[PAPI_MAX_STR_LEN]
int freebsd_number_of_events(int processortype)
Definition: map.c:40
Definition: map.h:31
long long ret
Definition: iozone.c:1346
int _papi_freebsd_update_control_state(hwd_control_state_t *ptr, NativeInfo_t *native, int count, hwd_context_t *ctx)
Definition: freebsd.c:269
float mhz
Definition: papi.h:802
Definition: map.h:26
int _papi_freebsd_init_control_state(hwd_control_state_t *ptr)
Definition: freebsd.c:258
papi_os_vector_t _papi_os_vector
Definition: aix.c:1288
int init_mdi(void)
Definition: freebsd.c:76
int i
Definition: fileop.c:140
#define FALSE
PAPI_os_info_t _papi_os_info
Definition: aix.c:1210
int _papi_freebsd_reset(hwd_context_t *ctx, hwd_control_state_t *ctrl)
Definition: freebsd.c:444
Definition: map.h:27
int init_presets(int cidx)
Definition: freebsd.c:151
char virtual_vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:796
long long found
Definition: libasync.c:735
Definition: map.h:25
free(dummyfile[xx])
static int cidx
Definition: event_info.c:40
int _papi_freebsd_init_component(int cidx)
Definition: freebsd.c:213
Definition: map.h:22
#define hwd_context_t
#define PAPI_ECMP
Definition: fpapi.h:109
Definition: map.h:23
long long _papi_freebsd_get_virt_usec(void)
Definition: freebsd.c:702
static int native
Definition: event_info.c:39
#define PAPI_DEFDOM
Definition: fpapi.h:49
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
#define PAPI_INT_ITIMER
Definition: papi_internal.h:54
EventSetInfo_t * ESI
int nnodes
Definition: papi.h:780
#define PAPI_DOMAIN
Definition: fpapi.h:50
char events[MAX_EVENTS][BUFSIZ]
char version[PAPI_MAX_STR_LEN]
int _papi_freebsd_ntv_name_to_code(char *name, unsigned int *event_code)
Definition: freebsd.c:748
#define PAPI_DEFGRN
Definition: fpapi.h:51
#define PAPI_INT_SIGNAL
Definition: papi_internal.h:53
int _papi_load_preset_table(char *pmu_str, int pmu_type, int cidx)
Definition: papi_preset.c:299
#define PAPI_GRANUL
Definition: fpapi.h:52
Definition: map.h:24
int _papi_freebsd_get_system_info(papi_mdi_t *mdi)
Definition: freebsd.c:882
char ** counters
Definition: freebsd.h:59
NativeInfo_t * NativeInfoArray
int _freebsd_get_memory_info(PAPI_hw_info_t *hw_info, int id)
#define PAPI_INT_MPX_SIGNAL
Definition: papi_internal.h:52
void _papi_freebsd_dispatch_timer(int signal, hwd_siginfo_t *info, void *context)
Definition: freebsd.c:551
#define PAPI_ESYS
Definition: fpapi.h:108
int threshold
int _papi_freebsd_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len)
Definition: freebsd.c:763
char vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:783
papi_mdi_t _papi_hwi_system_info
Definition: papi_internal.c:57
int _papi_freebsd_shutdown_thread(hwd_context_t *ctx)
Definition: freebsd.c:240
strcpy(filename, default_filename)
PAPI_hw_info_t hw_info
static hwd_libpmc_context_t Context
Definition: freebsd.c:65
#define PAPI_VENDOR_INTEL
Definition: papi.h:346
int _x86_detect_hypervisor(char *vendor_name)
int _papi_freebsd_update_shlib_info(papi_mdi_t *mdi)
Definition: freebsd.c:857
int vendor
Definition: papi.h:782
int _papi_freebsd_start(hwd_context_t *ctx, hwd_control_state_t *ctrl)
Definition: freebsd.c:352
Definition: map.h:33
int _papi_hwi_init_os(void)
Definition: aix.c:1213
int _papi_freebsd_read(hwd_context_t *ctx, hwd_control_state_t *ctrl, long long **events, int flags)
Definition: freebsd.c:390
int model
Definition: papi.h:784
char * name
Definition: iozone.c:23648
int _papi_freebsd_init_thread(hwd_context_t *ctx)
Definition: freebsd.c:233
Definition: map.h:29
int cpu_max_mhz
Definition: papi.h:791
int _papi_freebsd_ntv_code_to_descr(unsigned int EventCode, char *descr, int len)
Definition: freebsd.c:784
int _papi_freebsd_set_domain(hwd_control_state_t *cntrl, int domain)
Definition: freebsd.c:603
int _papi_freebsd_ctl(hwd_context_t *ctx, int code, _papi_int_option_t *option)
Definition: freebsd.c:627
#define PAPI_EBUF
Definition: fpapi.h:125
#define PAPI_NATIVE_AND_MASK
int totalcpus
Definition: papi.h:781
_papi_int_domain_t domain
long long _papi_freebsd_get_real_cycles(void)
Definition: freebsd.c:678
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:785
int _papi_freebsd_get_dmem_info(PAPI_dmem_info_t *d)
int _papi_freebsd_allocate_registers(EventSetInfo_t *ESI)
Definition: freebsd.c:808
#define PAPI_DOM_USER
Definition: fpapi.h:21
int(* get_dmem_info)(PAPI_dmem_info_t *)
Definition: papi_vector.h:70
int virtualized
Definition: papi.h:795
hwd_control_state_t * ctl_state
long j
Definition: iozone.c:19135
int _papi_freebsd_detect_hypervisor(char *virtual_vendor_name)
Definition: freebsd.c:866
ssize_t retval
Definition: libasync.c:338
#define hwd_register_t
#define PAPI_GRN_THR
Definition: fpapi.h:67
signal(SIGINT, signal_handler)
if(gettimeofday(&tp,(struct timezone *) NULL)==-1) perror("gettimeofday")
#define PAPI_VENDOR_AMD
Definition: papi.h:347
int ncpu
Definition: papi.h:776
char * ptr
Definition: iozone.c:23586
Definition: map.h:21