Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 46 additions & 27 deletions src/libperf.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,19 @@ libperf_initialize(pid_t pid, int cpu)

struct libperf_data *pd = malloc(sizeof(struct libperf_data));

if (pd == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
if (pd == NULL) {
goto error;
}

if (pid == -1)
if (pid == -1) {
pid = gettid();
}

pd->group = -1;

for (i = 0; i < __LIBPERF_ARRAY_SIZE(pd->fds); i++)
for (i = 0; i < __LIBPERF_ARRAY_SIZE(pd->fds); i++) {
pd->fds[i] = -1;
}

pd->pid = pid;
pd->cpu = cpu;
Expand All @@ -178,11 +178,9 @@ libperf_initialize(pid_t pid, int cpu)
struct perf_event_attr *attrs =
malloc(nr_counters * sizeof(struct perf_event_attr));

if(attrs == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
if(attrs == NULL) {
goto error;
}

memcpy(attrs, default_attrs, sizeof(default_attrs));
pd->attrs = attrs;
Expand All @@ -192,29 +190,46 @@ libperf_initialize(pid_t pid, int cpu)
open(logname, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

assert(fd != -1);
pd->log = fdopen(fd, "a");
if (fd == -1) {
goto error;
}

assert(pd->log != NULL);
pd->log = fdopen(fd, "a");

for (i = 0; i < nr_counters; i++)
{
attrs[i].size = sizeof(struct perf_event_attr);
attrs[i].inherit = 1; /* default */
attrs[i].disabled = 1; /* disable them now... */
attrs[i].enable_on_exec = 0;
pd->fds[i] = sys_perf_event_open(&attrs[i], pid, cpu, -1, 0);
if (pd->fds[i] < 0)
{
fprintf(stderr, "At event %d/%d\n", i, nr_counters);
perror("sys_perf_event_open");
exit(EXIT_FAILURE);
if (pd->log == NULL) {
goto error;
}

for (i = 0; i < nr_counters; i++) {
attrs[i].size = sizeof(struct perf_event_attr);
attrs[i].inherit = 1; /* default */
attrs[i].disabled = 1; /* disable them now... */
attrs[i].enable_on_exec = 0;
pd->fds[i] = sys_perf_event_open(&attrs[i], pid, cpu, -1, 0);
if (pd->fds[i] < 0) {
goto close;
}
}

pd->wall_start = rdclock();
return pd;

close:
close(fd);
fclose(pd->log);
for (i = 0; i < nr_counters; i++) {
close(pd->fds[i]);
}
error:
if (attrs) {
free(attrs);
attrs = NULL;
}
if (pd) {
free(pd);
pd = NULL;
}
return NULL;
}

/* thread safe */
Expand Down Expand Up @@ -319,6 +334,10 @@ libperf_unit_test(void *n)
{
struct libperf_data *pd = libperf_initialize(0, -1);

if (!pd) {
return -1;
}

char *x = malloc(1024 * 1024 * 1024L);

unsigned long int i;
Expand Down
4 changes: 4 additions & 0 deletions src/libperf_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ int
main(int argc, char *argv[])
{
struct libperf_data *pd = libperf_initialize(-1, -1); /* init lib */
if (!pd) {
printf("Error initializing libperf\n");
return -1;
}

libperf_enablecounter(pd, LIBPERF_COUNT_HW_INSTRUCTIONS); /* enable HW counter */

Expand Down