PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
coretemp_freebsd.c
Go to the documentation of this file.
1 /****************************/
2 /* THIS IS OPEN SOURCE CODE */
3 /****************************/
4 
22 #include <sys/types.h>
23 #include <sys/resource.h>
24 #include <sys/sysctl.h>
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <inttypes.h>
30 
31 /* Headers required by PAPI */
32 #include "papi.h"
33 #include "papi_internal.h"
34 #include "papi_vector.h"
35 #include "papi_memory.h"
36 
37 #define CORETEMP_MAX_COUNTERS 32 /* Can we tune this dynamically? */
38 #define TRUE (1==1)
39 #define FALSE (1!=1)
40 #define UNREFERENCED(x) (void)x
41 
42 /* Structure that stores private information for each event */
43 typedef struct coretemp_register
44 {
45  int mib[4];
46  /* Access to registers through these MIBs + sysctl (3) call */
47 
48  unsigned int selector;
52 
54 typedef struct coretemp_native_event_entry
55 {
58  char description[PAPI_MAX_STR_LEN];
60 
61 /* This structure is used when doing register allocation
62  it possibly is not necessary when there are no
63  register constraints */
64 typedef struct coretemp_reg_alloc
65 {
68 
69 /* Holds control flags, usually out-of band configuration of the hardware */
70 typedef struct coretemp_control_state
71 {
75 
76 /* Holds per-thread information */
77 typedef struct coretemp_context
78 {
81 
84 
86 static int CORETEMP_NUM_EVENTS = 0;
87 
88 
89 /********************************************************************/
90 /* Below are the functions required by the PAPI component interface */
91 /********************************************************************/
92 
95 {
96  int mib[4];
97  size_t len;
98  UNREFERENCED(ctx);
99 
100  SUBDBG("coretemp_init_thread %p...\n", ctx);
101 
102 #if 0
103  /* what does this do? VMW */
104 
105  len = 4;
106  if (sysctlnametomib ("dev.coretemp.0.%driver", mib, &len) == -1)
107  return PAPI_ECMP;
108 #endif
109 
110  return PAPI_OK;
111 }
112 
113 
119 {
120  int ret;
121  int i;
122  int mib[4];
123  size_t len;
124  char tmp[128];
125 
126  SUBDBG("coretemp_init_component...\n");
127 
128  /* Count the number of cores (counters) that have sensors allocated */
129  i = 0;
131  sprintf (tmp, "dev.coretemp.%d.%%driver", i);
132  len = 4;
133  ret = sysctlnametomib (tmp, mib, &len);
134  while (ret != -1)
135  {
137  i++;
138  sprintf (tmp, "dev.coretemp.%d.%%driver", i);
139  len = 4;
140  ret = sysctlnametomib (tmp, mib, &len);
141  }
142 
143  if (CORETEMP_NUM_EVENTS == 0)
144  return PAPI_OK;
145 
146  /* Allocate memory for the our event table */
147  coretemp_native_table = (coretemp_native_event_entry_t *)
149  if (coretemp_native_table == NULL)
150  {
151  perror( "malloc():Could not get memory for coretemp events table" );
152  return PAPI_ENOMEM;
153  }
154 
155  /* Allocate native events internal structures */
156  for (i = 0; i < CORETEMP_NUM_EVENTS; i++)
157  {
158  /* Event name */
159  sprintf (coretemp_native_table[i].name, "CORETEMP_CPU_%d", i);
160 
161  /* Event description */
162  sprintf (coretemp_native_table[i].description, "CPU On-Die Thermal Sensor #%d", i);
163 
164  /* Event extra bits -> save MIB to faster access later */
165  sprintf (tmp, "dev.cpu.%d.temperature", i);
166  len = 4;
167  if (sysctlnametomib (tmp, coretemp_native_table[i].resources.mib, &len) == -1)
168  return PAPI_ECMP;
169 
170  coretemp_native_table[i].resources.selector = i+1;
171  }
172 
173  return PAPI_OK;
174 }
175 
176 
179 {
180  int i;
181 
182  SUBDBG("coretemp_init_control_state... %p\n", ctrl);
184 
185  for (i = 0; i < CORETEMP_MAX_COUNTERS; i++)
186  c->added[i] = FALSE;
187 
188  return PAPI_OK;
189 }
190 
191 
196 int coretemp_ntv_enum_events (unsigned int *EventCode, int modifier)
197 {
198 
199  switch ( modifier )
200  {
201  /* return EventCode of first event */
202  case PAPI_ENUM_FIRST:
203  *EventCode = 0;
204  return PAPI_OK;
205  break;
206 
207  /* return EventCode of passed-in Event */
208  case PAPI_ENUM_EVENTS:
209  {
210  int index = *EventCode;
211 
212  if ( index < CORETEMP_NUM_EVENTS - 1 )
213  {
214  *EventCode = *EventCode + 1;
215  return PAPI_OK;
216  }
217  else
218  return PAPI_ENOEVNT;
219  break;
220  }
221 
222  default:
223  return PAPI_EINVAL;
224  }
225 
226  return PAPI_EINVAL;
227 }
228 
234 int coretemp_ntv_code_to_name (unsigned int EventCode, char *name, int len)
235 {
236  int index = EventCode;
237 
238  strncpy( name, coretemp_native_table[index].name, len );
239 
240  return PAPI_OK;
241 }
242 
248 int coretemp_ntv_code_to_descr (unsigned int EventCode, char *name, int len)
249 {
250  int index = EventCode;
251 
252  strncpy( name, coretemp_native_table[index].description, len );
253 
254  return PAPI_OK;
255 }
256 
259 int coretemp_ntv_code_to_bits (unsigned int EventCode, hwd_register_t * bits)
260 {
261  UNREFERENCED(EventCode);
262  UNREFERENCED(bits);
263 
264  return PAPI_OK;
265 }
266 
269  NativeInfo_t * native, int count, hwd_context_t * ctx )
270 {
271  int i, index;
273  UNREFERENCED(ctx);
274 
275  SUBDBG("coretemp_update_control_state %p %p...\n", ptr, ctx);
276 
277  for (i = 0; i < count; i++)
278  {
279  index = native[i].ni_event;
280  native[i].ni_position = coretemp_native_table[index].resources.selector - 1;
281  c->added[native[i].ni_position] = TRUE;
282 
283  SUBDBG ("\nnative[%i].ni_position = coretemp_native_table[%i].resources.selector-1 = %i;\n",
284  i, index, native[i].ni_position );
285  }
286 
287  return PAPI_OK;
288 }
289 
292 {
293  UNREFERENCED(ctx);
294  UNREFERENCED(ctrl);
295 
296  SUBDBG( "coretemp_start %p %p...\n", ctx, ctrl );
297 
298  /* Nothing to be done */
299 
300  return PAPI_OK;
301 }
302 
303 
306 {
307  UNREFERENCED(ctx);
308  UNREFERENCED(ctrl);
309 
310  SUBDBG("coretemp_stop %p %p...\n", ctx, ctrl);
311 
312  /* Nothing to be done */
313 
314  return PAPI_OK;
315 }
316 
317 
320  long_long ** events, int flags)
321 {
322  int i;
324  UNREFERENCED(ctx);
325  UNREFERENCED(flags);
326 
327  SUBDBG("coretemp_read... %p %d\n", ctx, flags);
328 
329  for (i = 0; i < CORETEMP_MAX_COUNTERS; i++)
330  if (c->added[i])
331  {
332  int tmp;
333  size_t len = sizeof(tmp);
334 
335  if (sysctl (coretemp_native_table[i].resources.mib, 4, &tmp, &len, NULL, 0) == -1)
336  c->counters[i] = 0;
337  else
338  c->counters[i] = tmp/10;
339  /* Coretemp module returns temperature in tenths of kelvin
340  Kelvin are useful to avoid negative values... but will have
341  negative temperatures ??? */
342  }
343 
344  *events = c->counters;
345 
346  return PAPI_OK;
347 }
348 
350 /* otherwise, the updated state is written to ESI->hw_start */
352  long_long events[] )
353 {
354  UNREFERENCED(ctx);
355  UNREFERENCED(events);
356  UNREFERENCED(ctrl);
357 
358  SUBDBG("coretemp_write... %p %p\n", ctx, ctrl);
359 
360  /* These sensor counters cannot be writtn */
361 
362  return PAPI_OK;
363 }
364 
365 
368 {
369  UNREFERENCED(ctx);
370  UNREFERENCED(ctrl);
371 
372  SUBDBG("coretemp_reset ctx=%p ctrl=%p...\n", ctx, ctrl);
373 
374  /* These sensors cannot be reseted */
375 
376  return PAPI_OK;
377 }
378 
381 {
382 
383  SUBDBG( "coretemp_shutdown_component... %p\n");
384 
385  /* Last chance to clean up */
386  papi_free (coretemp_native_table);
387 
388  return PAPI_OK;
389 }
390 
391 
392 
398 int coretemp_ctl (hwd_context_t * ctx, int code, _papi_int_option_t * option)
399 {
400  UNREFERENCED(ctx);
401  UNREFERENCED(code);
402  UNREFERENCED(option);
403 
404  SUBDBG( "coretemp_ctl... %p %d %p\n", ctx, code, option );
405 
406  /* FIXME. This should maybe set up more state, such as which counters are active and */
407  /* counter mappings. */
408 
409  return PAPI_OK;
410 }
411 
421 int coretemp_set_domain (hwd_control_state_t * cntrl, int domain)
422 {
423  UNREFERENCED(cntrl);
424 
425  SUBDBG ("coretemp_set_domain... %p %d\n", cntrl, domain);
426 
427  if (PAPI_DOM_ALL & domain)
428  {
429  SUBDBG( " PAPI_DOM_ALL \n" );
430  return PAPI_OK;
431  }
432  return PAPI_EINVAL ;
433 
434 }
435 
436 
439  .cmp_info = {
440  /* default component information (unspecified values are initialized to 0) */
441  .name = "coretemp_freebsd",
442  .short_name = "coretemp",
443  .version = "5.0",
444  .num_mpx_cntrs = CORETEMP_MAX_COUNTERS,
445  .num_cntrs = CORETEMP_MAX_COUNTERS,
446  .default_domain = PAPI_DOM_ALL,
447  .available_domains = PAPI_DOM_ALL,
448  .default_granularity = PAPI_GRN_SYS,
449  .available_granularities = PAPI_GRN_SYS,
450  .hardware_intr_sig = PAPI_INT_SIGNAL,
451 
452  /* component specific cmp_info initializations */
453  .fast_real_timer = 0,
454  .fast_virtual_timer = 0,
455  .attach = 0,
456  .attach_must_ptrace = 0,
457  }
458  ,
459 
460  /* sizes of framework-opaque component-private structures */
461  .size = {
462  .context = sizeof ( coretemp_context_t ),
463  .control_state = sizeof ( coretemp_control_state_t ),
464  .reg_value = sizeof ( coretemp_register_t ),
465  .reg_alloc = sizeof ( coretemp_reg_alloc_t ),
466  }
467  ,
468  /* function pointers in this component */
469  .init_thread = coretemp_init_thread,
470  .init_component = coretemp_init_component,
471  .init_control_state = coretemp_init_control_state,
472  .start = coretemp_start,
473  .stop = coretemp_stop,
474  .read = coretemp_read,
475  .write = coretemp_write,
476  .shutdown_component = coretemp_shutdown_component,
477  .ctl = coretemp_ctl,
478 
479  .update_control_state = coretemp_update_control_state,
480  .set_domain = coretemp_set_domain,
481  .reset = coretemp_reset,
482 
483  .ntv_enum_events = coretemp_ntv_enum_events,
484  .ntv_code_to_name = coretemp_ntv_code_to_name,
485  .ntv_code_to_descr = coretemp_ntv_code_to_descr,
486  .ntv_code_to_bits = coretemp_ntv_code_to_bits,
487 };
488 
int coretemp_init_control_state(hwd_control_state_t *ctrl)
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:626
int coretemp_ntv_code_to_descr(unsigned int EventCode, char *name, int len)
#define PAPI_ENOEVNT
Definition: papi.h:258
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
long long flags
Definition: iozone.c:12330
int coretemp_init_component()
#define papi_free(a)
Definition: papi_memory.h:35
int coretemp_reset(hwd_context_t *ctx, hwd_control_state_t *ctrl)
#define papi_malloc(a)
Definition: papi_memory.h:34
int coretemp_ntv_code_to_bits(unsigned int EventCode, hwd_register_t *bits)
int coretemp_ctl(hwd_context_t *ctx, int code, _papi_int_option_t *option)
coretemp_register_t resources
#define PAPI_DOM_ALL
Definition: papi.h:301
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
return PAPI_EINVAL
Definition: linux-nvml.c:408
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
double c
Definition: multiplex.c:22
Return codes and api definitions.
char events[MAX_EVENTS][BUFSIZ]
long long ret
Definition: iozone.c:1346
int added[CORETEMP_MAX_COUNTERS]
long_long counters[CORETEMP_MAX_COUNTERS]
int coretemp_init_thread(hwd_context_t *ctx)
int i
Definition: fileop.c:140
int coretemp_set_domain(hwd_control_state_t *cntrl, int domain)
#define FALSE
int coretemp_stop(hwd_context_t *ctx, hwd_control_state_t *ctrl)
int coretemp_start(hwd_context_t *ctx, hwd_control_state_t *ctrl)
coretemp_register_t ra_bits
static int native
Definition: event_info.c:39
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int coretemp_ntv_code_to_name(unsigned int EventCode, char *name, int len)
int coretemp_read(hwd_context_t *ctx, hwd_control_state_t *ctrl, long_long **events, int flags)
#define PAPI_ECMP
Definition: papi.h:254
#define PAPI_INT_SIGNAL
Definition: papi_internal.h:53
int coretemp_shutdown_component(void)
#define PAPI_ENOMEM
Definition: papi.h:252
coretemp_control_state_t state
int coretemp_update_control_state(hwd_control_state_t *ptr, NativeInfo_t *native, int count, hwd_context_t *ctx)
#define TRUE
static int CORETEMP_NUM_EVENTS
char * name
Definition: iozone.c:23648
#define CORETEMP_MAX_COUNTERS
#define long_long
Definition: papi.h:550
papi_vector_t _coretemp_freebsd_vector
#define PAPI_MAX_STR_LEN
Definition: papi.h:463
static coretemp_native_event_entry_t * coretemp_native_table
int coretemp_write(hwd_context_t *ctx, hwd_control_state_t *ctrl, long_long events[])
#define PAPI_GRN_SYS
Definition: papi.h:364
long long tmp
Definition: iozone.c:12031
int coretemp_ntv_enum_events(unsigned int *EventCode, int modifier)
#define UNREFERENCED(x)
char * ptr
Definition: iozone.c:23586