PAPIC:PAPI attach.3
From PAPIDocs
(Redirected from PAPIC:PAPI detach.3)
Jump to: navigation, search


  • PAPI_attach - attach PAPI event set to the specified thread id
  • PAPI_detach - detach PAPI event set from previously specified thread id and restore to executing thread


C Interface

#include <papi.h>
int PAPI_attach(int  EventSet, unsigned long  tid );
int PAPI_detach(int  EventSet );

Fortran Interface



PAPI_attach() and PAPI_detach() are wrapper functions that access PAPI_set_opt() (3) to allow PAPI to monitor performance counts on a thread other than the one currently executing. This is sometimes referred to as third party monitoring. PAPI_attach() connects the specified EventSet to the specifed thread; PAPI_detach() breaks that connection and restores the EventSet to the original executing thread.


EventSet -- an integer handle for a PAPI Event Set as created by PAPI_create_eventset (3)

tid -- a thread id as obtained from, for example, PAPI_list_threads (3) or PAPI_thread_id (3).


PAPI_ESBSTR This feature is unsupported on this substrate.

PAPI_EINVAL One or more of the arguments is invalid.

PAPI_ENOEVST The event set specified does not exist.

PAPI_EISRUN The event set is currently counting events.


int EventSet = PAPI_NULL;
unsigned long pid;
pid = fork();
if (pid <= 0)
if (PAPI_create_eventset(&EventSet) != PAPI_OK)
/* Add Total Instructions Executed to our EventSet */
if (PAPI_add_event(EventSet, PAPI_TOT_INS) != PAPI_OK)
/* Attach this EventSet to the forked process */
if (PAPI_attach(EventSet, pid) != PAPI_OK)


There are no known bugs in these functions.

See Also

PAPI_set_opt(3), PAPI_list_threads(3), PAPI_thread_id(3), PAPI_thread_init(3)