Skip to content

Commit 3a93115

Browse files
authored
Improve reference collector performance (#186)
* improve refference collector performance by keeping log file open * add missing copyright header
1 parent 0c57540 commit 3a93115

1 file changed

Lines changed: 84 additions & 75 deletions

File tree

‎src/ittnotify_refcol/itt_refcol_impl.c‎

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
Copyright (C) 2025 Intel Corporation
3+
4+
SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause
5+
*/
6+
17
#include <stdio.h>
28
#include <stdarg.h>
39
#include <stdlib.h>
@@ -22,7 +28,7 @@ enum {
2228
};
2329

2430
static struct ref_collector_logger {
25-
char* file_name;
31+
FILE* log_fp;
2632
uint8_t init_state;
2733
} g_ref_collector_logger = {NULL, 0};
2834

@@ -71,11 +77,73 @@ void ref_col_init()
7177
}
7278
free(log_file);
7379

74-
g_ref_collector_logger.file_name = file_name_buffer;
80+
g_ref_collector_logger.log_fp = fopen(file_name_buffer, "a");
81+
if (!g_ref_collector_logger.log_fp)
82+
{
83+
printf("ERROR: Cannot open log file: %s\n", file_name_buffer);
84+
return;
85+
}
86+
7587
g_ref_collector_logger.init_state = 1;
7688
}
7789
}
7890

91+
void ref_col_release()
92+
{
93+
if (g_ref_collector_logger.log_fp)
94+
{
95+
fclose(g_ref_collector_logger.log_fp);
96+
g_ref_collector_logger.log_fp = NULL;
97+
}
98+
99+
if (g_itt_global == NULL) return;
100+
101+
__itt_mutex_lock(&(g_itt_global->mutex));
102+
103+
__itt_domain *d = g_itt_global->domain_list;
104+
while (d)
105+
{
106+
__itt_domain *next = d->next;
107+
if (d->nameA) free((char*)d->nameA);
108+
free(d);
109+
d = next;
110+
}
111+
g_itt_global->domain_list = NULL;
112+
113+
__itt_string_handle *s = g_itt_global->string_list;
114+
while (s)
115+
{
116+
__itt_string_handle *next = s->next;
117+
if (s->strA) free((char*)s->strA);
118+
free(s);
119+
s = next;
120+
}
121+
g_itt_global->string_list = NULL;
122+
123+
__itt_counter_info_t *c = g_itt_global->counter_list;
124+
while (c)
125+
{
126+
__itt_counter_info_t *next = c->next;
127+
if (c->nameA) free((char*)c->nameA);
128+
if (c->domainA) free((char*)c->domainA);
129+
free(c);
130+
c = next;
131+
}
132+
g_itt_global->counter_list = NULL;
133+
134+
__itt_histogram *h = g_itt_global->histogram_list;
135+
while (h)
136+
{
137+
__itt_histogram *next = h->next;
138+
if (h->nameA) free((char*)h->nameA);
139+
free(h);
140+
h = next;
141+
}
142+
g_itt_global->histogram_list = NULL;
143+
144+
__itt_mutex_unlock(&(g_itt_global->mutex));
145+
}
146+
79147
static void fill_func_ptr_per_lib(__itt_global* p)
80148
{
81149
__itt_api_info* api_list = (__itt_api_info*)p->api_list_ptr;
@@ -98,6 +166,7 @@ ITT_EXTERN_C void ITTAPI __itt_api_init(__itt_global* p, __itt_group_id init_gro
98166
fill_func_ptr_per_lib(p);
99167
ref_col_init();
100168
g_itt_global = p;
169+
atexit(ref_col_release);
101170
}
102171
else
103172
{
@@ -107,30 +176,22 @@ ITT_EXTERN_C void ITTAPI __itt_api_init(__itt_global* p, __itt_group_id init_gro
107176

108177
void log_func_call(uint8_t log_level, const char* function_name, const char* message_format, ...)
109178
{
110-
if (g_ref_collector_logger.init_state)
111-
{
112-
FILE * pLogFile = NULL;
113-
char log_buffer[LOG_BUFFER_MAX_SIZE];
114-
uint32_t result_len = 0;
115-
va_list message_args;
116-
117-
result_len += sprintf(log_buffer, "[%s] %s(...) - ", log_level_str[log_level] ,function_name);
118-
va_start(message_args, message_format);
119-
vsnprintf(log_buffer + result_len, LOG_BUFFER_MAX_SIZE - result_len, message_format, message_args);
120-
121-
pLogFile = fopen(g_ref_collector_logger.file_name, "a");
122-
if (!pLogFile)
123-
{
124-
printf("ERROR: Cannot open file: %s\n", g_ref_collector_logger.file_name);
125-
return;
126-
}
127-
fprintf(pLogFile, "%s\n", log_buffer);
128-
fclose(pLogFile);
129-
}
130-
else
179+
if (!g_ref_collector_logger.init_state || !g_ref_collector_logger.log_fp)
131180
{
181+
printf("ERROR: Failed to log function call\n");
132182
return;
133183
}
184+
185+
char log_buffer[LOG_BUFFER_MAX_SIZE];
186+
uint32_t result_len = 0;
187+
va_list message_args;
188+
189+
result_len += sprintf(log_buffer, "[%s] %s(...) - ", log_level_str[log_level] ,function_name);
190+
va_start(message_args, message_format);
191+
vsnprintf(log_buffer + result_len, LOG_BUFFER_MAX_SIZE - result_len, message_format, message_args);
192+
va_end(message_args);
193+
194+
fprintf(g_ref_collector_logger.log_fp, "%s\n", log_buffer);
134195
}
135196

136197
#define LOG_FUNC_CALL_INFO(...) log_func_call(LOG_LVL_INFO, __FUNCTION__, __VA_ARGS__)
@@ -226,58 +287,6 @@ char* get_context_metadata_element(__itt_context_type type, void* metadata)
226287
return metadata_str;
227288
}
228289

229-
// Call this function to release allocated resources and avoid memory leaks
230-
void __itt_refcol_release()
231-
{
232-
if (g_itt_global == NULL)
233-
return;
234-
235-
__itt_mutex_lock(&(g_itt_global->mutex));
236-
237-
__itt_domain *d = g_itt_global->domain_list;
238-
while (d)
239-
{
240-
__itt_domain *next = d->next;
241-
if (d->nameA) free((char*)d->nameA);
242-
free(d);
243-
d = next;
244-
}
245-
g_itt_global->domain_list = NULL;
246-
247-
__itt_string_handle *s = g_itt_global->string_list;
248-
while (s)
249-
{
250-
__itt_string_handle *next = s->next;
251-
if (s->strA) free((char*)s->strA);
252-
free(s);
253-
s = next;
254-
}
255-
g_itt_global->string_list = NULL;
256-
257-
__itt_counter_info_t *c = g_itt_global->counter_list;
258-
while (c)
259-
{
260-
__itt_counter_info_t *next = c->next;
261-
if (c->nameA) free((char*)c->nameA);
262-
if (c->domainA) free((char*)c->domainA);
263-
free(c);
264-
c = next;
265-
}
266-
g_itt_global->counter_list = NULL;
267-
268-
__itt_histogram *h = g_itt_global->histogram_list;
269-
while (h)
270-
{
271-
__itt_histogram *next = h->next;
272-
if (h->nameA) free((char*)h->nameA);
273-
free(h);
274-
h = next;
275-
}
276-
g_itt_global->histogram_list = NULL;
277-
278-
__itt_mutex_unlock(&(g_itt_global->mutex));
279-
}
280-
281290
ITT_EXTERN_C __itt_domain* ITTAPI __itt_domain_create(const char *name)
282291
{
283292
if (g_itt_global == NULL || name == NULL)

0 commit comments

Comments
 (0)