-
Notifications
You must be signed in to change notification settings - Fork 2
/
ppmonitor.c
87 lines (65 loc) · 1.78 KB
/
ppmonitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- indent-tabs-mode: nil -*- */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <papi.h>
#include <monitor.h>
#ifdef PROCESS_ONLY
#error "Please compile libmonitor with thread support."
#endif
struct measurement {
unsigned tid;
long long real_nsec;
long long user_nsec;
struct measurement *next;
};
static pthread_mutex_t measurements_lock = PTHREAD_MUTEX_INITIALIZER;
struct measurement *perfpiece_measurements = NULL;
int perfpiece_active_p = 0;
int libppmonitor_loaded_p = 0;
void free_perfpiece_measurements()
{
struct measurement *m, *next;
for (m = perfpiece_measurements; m != NULL; m = next) {
next = m->next;
free(m);
}
perfpiece_measurements = NULL;
}
void monitor_init_library(void)
{
libppmonitor_loaded_p = 1;
monitor_opt_error = 0;
}
void monitor_init_process(char *process, int *argc, char **argv, unsigned tid)
{
PAPI_library_init(PAPI_VER_CURRENT);
PAPI_thread_init(pthread_self);
}
void *monitor_init_thread(unsigned tid)
{
struct measurement *m;
if (!perfpiece_active_p)
return NULL;
PAPI_register_thread();
m = malloc(sizeof(struct measurement));
m->tid = tid;
m->user_nsec = PAPI_get_virt_nsec();
m->real_nsec = PAPI_get_real_nsec();
/* Add to the 'perfpiece_measurements' linked list. */
pthread_mutex_lock(&measurements_lock);
m->next = perfpiece_measurements;
perfpiece_measurements = m;
pthread_mutex_unlock(&measurements_lock);
return m;
}
void monitor_fini_thread(void *ptr)
{
struct measurement *m;
if (!perfpiece_active_p)
return;
m = (struct measurement *) ptr;
m->real_nsec = PAPI_get_real_nsec() - m->real_nsec;
m->user_nsec = PAPI_get_virt_nsec() - m->user_nsec;
PAPI_unregister_thread();
}