PAPI 7.1.0.0
Loading...
Searching...
No Matches
cupti_utils.c
Go to the documentation of this file.
1
7#include <string.h>
8#include "papi_memory.h"
9
10#include "cupti_utils.h"
11#include "htable.h"
12#include "lcuda_debug.h"
13
14#define ADDED_EVENTS_INITIAL_CAPACITY 64
15
16int cuptiu_event_table_create_init_capacity(int capacity, int sizeof_rec, cuptiu_event_table_t **pevt_table)
17{
19 if (evt_table == NULL) {
20 goto fn_fail;
21 }
22 evt_table->sizeof_rec = sizeof_rec;
23 evt_table->capacity = capacity;
24 evt_table->count = 0;
25 evt_table->evts = papi_calloc (evt_table->capacity, evt_table->sizeof_rec);
26 if (evt_table->evts == NULL) {
28 ERRDBG("Error allocating memory for dynamic event table.\n");
29 goto fn_fail;
30 }
31 if (htable_init(&(evt_table->htable)) != HTABLE_SUCCESS) {
33 goto fn_fail;
34 }
35 *pevt_table = evt_table;
36 return PAPI_OK;
37fn_fail:
38 *pevt_table = NULL;
39 return PAPI_ENOMEM;
40}
41
42int cuptiu_event_table_create(int sizeof_rec, cuptiu_event_table_t **pevt_table)
43{
45}
46
48{
49 if (evt_idx >= (int) evt_table->count) {
50 *record = NULL;
51 return PAPI_EINVAL;
52 }
53 if (evt_idx == -1) {
54 evt_idx = evt_table->count - 1;
55 }
56 *record = evt_table->evts + evt_idx * evt_table->sizeof_rec;
57 return PAPI_OK;
58}
59
61{
62 int papi_errno = PAPI_OK;
63 evt_table->capacity *= 2;
64 evt_table->evts = papi_realloc(evt_table->evts, evt_table->capacity * evt_table->sizeof_rec);
65 if (evt_table == NULL) {
66 ERRDBG("Failed to expand event_table array.\n");
67 papi_errno = PAPI_ENOMEM;
68 goto fn_exit;
69 }
70 /* Rehash all the table entries */
71 unsigned int i;
72 cuptiu_event_t *evt_rec;
73 for (i=0; i<evt_table->count; i++) {
74 papi_errno = cuptiu_event_table_get_item(evt_table, i, &evt_rec);
75 if (papi_errno != PAPI_OK) {
76 papi_errno = PAPI_EINVAL;
77 goto fn_exit;
78 }
79 if (HTABLE_SUCCESS != htable_insert(evt_table->htable, evt_rec->name, evt_rec)) {
80 papi_errno = PAPI_ENOMEM;
81 goto fn_exit;
82 }
83 }
84fn_exit:
85 return papi_errno;
86}
87
88int cuptiu_event_table_insert_record(cuptiu_event_table_t *evt_table, const char *evt_name, unsigned int evt_code, int evt_pos)
89{
90 int papi_errno = PAPI_OK;
91
92 /* Allocate twice the space if running out */
93 if (evt_table->count >= evt_table->capacity) {
94 papi_errno = reallocate_array(evt_table);
95 if (papi_errno != PAPI_OK) {
96 goto fn_exit;
97 }
98 }
99 /* Insert record in array */
100 cuptiu_event_t *evt_rec = evt_table->evts + evt_table->count * evt_table->sizeof_rec;
101 strcpy(evt_rec->name, evt_name);
102 evt_rec->evt_code = evt_code;
103 evt_rec->evt_pos = evt_pos;
104 /* Insert entry in string hash table */
105 if (HTABLE_SUCCESS != htable_insert(evt_table->htable, evt_name, evt_rec)) {
106 return PAPI_ENOMEM;
107 }
108 evt_table->count ++;
109fn_exit:
110 return papi_errno;
111}
112
114{
115 int papi_errno = PAPI_OK;
116 if (count <= 0 || count > (int) src->count) {
117 papi_errno = PAPI_EINVAL;
118 goto fn_fail;
119 }
120 cuptiu_event_table_t *target;
121 papi_errno = cuptiu_event_table_create_init_capacity(count, src->sizeof_rec, &target);
122 if (papi_errno != PAPI_OK) {
123 goto fn_fail;
124 }
125 int i;
126 cuptiu_event_t *evt_rec;
127 for (i = 0; i < count; i++) {
128 papi_errno = cuptiu_event_table_get_item(src, idcs[i], &evt_rec);
129 if (papi_errno != PAPI_OK) {
131 goto fn_fail;
132 }
133 papi_errno = cuptiu_event_table_insert_record(target, evt_rec->name, evt_rec->evt_code, evt_rec->evt_pos);
134 if (papi_errno != PAPI_OK) {
136 goto fn_fail;
137 }
138 }
139 *pevt_names = target;
140fn_exit:
141 return papi_errno;
142fn_fail:
143 *pevt_names = NULL;
144 goto fn_exit;
145}
146
148{
149 int papi_errno;
150 cuptiu_event_t *evt_rec = NULL;
151 papi_errno = htable_find(evt_table->htable, evt_name, (void **) &evt_rec);
152 if (papi_errno == HTABLE_SUCCESS) {
153 *found_rec = evt_rec;
154 return PAPI_OK;
155 }
156 return PAPI_ENOEVNT;
157}
158
160{
161 cuptiu_event_table_t *evt_table = *pevt_table;
162 if (evt_table == NULL)
163 return;
164 if (evt_table->evts) {
165 papi_free(evt_table->evts);
166 evt_table->evts = NULL;
167 }
168 if (evt_table->htable) {
169 htable_shutdown(evt_table->htable);
170 evt_table->htable = NULL;
171 }
172 papi_free(evt_table);
173 *pevt_table = NULL;
174}
175
176int cuptiu_files_search_in_path(const char *file_name, const char *search_path, char **file_paths)
177{
178 char path[PATH_MAX];
179 char command[PATH_MAX];
180 snprintf(command, PATH_MAX, "find %s -name %s", search_path, file_name);
181
182 FILE *fp;
183 fp = popen(command, "r");
184 if (fp == NULL) {
185 ERRDBG("Failed to run system command find using popen.\n");
186 return -1;
187 }
188
189 int count = 0;
190 while (fgets(path, PATH_MAX, fp) != NULL) {
191 path[strcspn(path, "\n")] = 0;
192 file_paths[count] = strdup(path);
193 count++;
194 if (count >= CUPTIU_MAX_FILES) {
195 break;
196 }
197 }
198
199 pclose(fp);
200 if (count == 0) {
201 ERRDBG("%s not found in path PAPI_CUDA_ROOT.\n", file_name);
202 }
203 return count;
204}
int i
static long count
static int htable_insert(void *handle, const char *key, void *in)
Definition: cuda/htable.h:92
static int htable_shutdown(void *handle)
Definition: cuda/htable.h:76
#define HTABLE_SUCCESS
Definition: cuda/htable.h:17
static int htable_find(void *handle, const char *key, void **out)
Definition: cuda/htable.h:161
static int htable_init(void **handle)
Definition: cuda/htable.h:55
#define ADDED_EVENTS_INITIAL_CAPACITY
Definition: cupti_utils.c:14
void cuptiu_event_table_destroy(cuptiu_event_table_t **pevt_table)
Definition: cupti_utils.c:159
int cuptiu_event_table_get_item(cuptiu_event_table_t *evt_table, int evt_idx, cuptiu_event_t **record)
Definition: cupti_utils.c:47
int cuptiu_event_table_create_init_capacity(int capacity, int sizeof_rec, cuptiu_event_table_t **pevt_table)
Definition: cupti_utils.c:16
int cuptiu_event_table_select_by_idx(cuptiu_event_table_t *src, int count, int *idcs, cuptiu_event_table_t **pevt_names)
Definition: cupti_utils.c:113
int cuptiu_files_search_in_path(const char *file_name, const char *search_path, char **file_paths)
Definition: cupti_utils.c:176
int cuptiu_event_table_insert_record(cuptiu_event_table_t *evt_table, const char *evt_name, unsigned int evt_code, int evt_pos)
Definition: cupti_utils.c:88
static int reallocate_array(cuptiu_event_table_t *evt_table)
Definition: cupti_utils.c:60
int cuptiu_event_table_find_name(cuptiu_event_table_t *evt_table, const char *evt_name, cuptiu_event_t **found_rec)
Definition: cupti_utils.c:147
int cuptiu_event_table_create(int sizeof_rec, cuptiu_event_table_t **pevt_table)
Definition: cupti_utils.c:42
#define CUPTIU_MAX_FILES
Definition: cupti_utils.h:38
char * evt_name(evstock *stock, int index)
Definition: eventstock.c:193
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENOEVNT
Definition: f90papi.h:139
#define PAPI_EINVAL
Definition: f90papi.h:115
#define PAPI_ENOMEM
Definition: f90papi.h:16
#define ERRDBG(format, args...)
Definition: lcuda_debug.h:30
#define papi_calloc(a, b)
Definition: papi_memory.h:37
#define papi_free(a)
Definition: papi_memory.h:35
#define papi_malloc(a)
Definition: papi_memory.h:34
#define papi_realloc(a, b)
Definition: papi_memory.h:36
static FILE * fp
char name[PAPI_2MAX_STR_LEN]
Definition: cupti_utils.h:13
unsigned int evt_pos
Definition: cupti_utils.h:15
unsigned int evt_code
Definition: cupti_utils.h:14
unsigned int sizeof_rec
Definition: cupti_utils.h:21
unsigned int count
Definition: cupti_utils.h:22
unsigned int capacity
Definition: cupti_utils.h:23