Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Add kernel tracing instrumentation #65

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unistd.h>
#include "log.h"
#include "private.h"
#include "trace.h"

/* Plane allocation algorithm
*
Expand Down Expand Up @@ -673,6 +674,10 @@ liftoff_output_apply(struct liftoff_output *output, drmModeAtomicReq *req,

update_layers_priority(device);

liftoff_tracer_mark(&device->tracer,
"output_apply (begin_ctx=%d)",
device->page_flip_counter);

ret = reuse_previous_alloc(output, req, flags);
if (ret == 0) {
log_reuse(output);
Expand Down Expand Up @@ -775,5 +780,9 @@ liftoff_output_apply(struct liftoff_output *output, drmModeAtomicReq *req,

mark_layers_clean(output);

liftoff_tracer_mark(&device->tracer,
"output_apply (end_ctx=%d)",
device->page_flip_counter);

return 0;
}
11 changes: 11 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ liftoff_device_create(int drm_fd)
liftoff_list_init(&device->planes);
liftoff_list_init(&device->outputs);

liftoff_tracer_init(&device->tracer);

device->drm_fd = dup(drm_fd);
if (device->drm_fd < 0) {
liftoff_log_errno(LIFTOFF_ERROR, "dup");
Expand Down Expand Up @@ -59,6 +61,7 @@ liftoff_device_destroy(struct liftoff_device *device)
return;
}

liftoff_tracer_finish(&device->tracer);
close(device->drm_fd);
liftoff_list_for_each_safe(plane, tmp, &device->planes, link) {
liftoff_plane_destroy(plane);
Expand Down Expand Up @@ -97,13 +100,21 @@ device_test_commit(struct liftoff_device *device, drmModeAtomicReq *req,

device->test_commit_counter++;

liftoff_tracer_mark(&device->tracer,
"device_test_commit (begin_ctx=%d)",
device->test_commit_counter);

flags &= ~DRM_MODE_PAGE_FLIP_EVENT;
do {
ret = drmModeAtomicCommit(device->drm_fd, req,
DRM_MODE_ATOMIC_TEST_ONLY | flags,
NULL);
} while (ret == -EINTR || ret == -EAGAIN);

liftoff_tracer_mark(&device->tracer,
"device_test_commit (end_ctx=%d)",
device->test_commit_counter);

if (ret != 0 && ret != -EINVAL && ret != -ERANGE) {
liftoff_log(LIFTOFF_ERROR, "drmModeAtomicCommit: %s",
strerror(-ret));
Expand Down
2 changes: 2 additions & 0 deletions include/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <libliftoff.h>
#include "list.h"
#include "log.h"
#include "trace.h"

/* Layer priority is assigned depending on the number of updates during a
* given number of page-flips */
Expand All @@ -18,6 +19,7 @@ struct liftoff_device {
uint32_t *crtcs;
size_t crtcs_len;

struct liftoff_tracer tracer;
int page_flip_counter;
int test_commit_counter;
};
Expand Down
21 changes: 21 additions & 0 deletions include/trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef TRACE_H
#define TRACE_H

#include <stdio.h>
#include "log.h"

struct liftoff_tracer {
FILE *f;
};

int
liftoff_tracer_init(struct liftoff_tracer *tracer);

void
liftoff_tracer_finish(struct liftoff_tracer *tracer);

void
liftoff_tracer_mark(struct liftoff_tracer *tracer, const char *format, ...)
_LIFTOFF_ATTRIB_PRINTF(2, 3);

#endif
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ liftoff_lib = library(
'log.c',
'output.c',
'plane.c',
'trace.c',
),
include_directories: liftoff_inc,
version: meson.project_version(),
Expand Down
39 changes: 39 additions & 0 deletions trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <errno.h>
#include "trace.h"

int
liftoff_tracer_init(struct liftoff_tracer *tracer)
{
tracer->f = fopen("/sys/kernel/tracing/trace_marker", "w");
if (tracer->f == NULL) {
return -errno;
}
liftoff_log(LIFTOFF_DEBUG, "Kernel tracing is enabled");
return 0;
}

void
liftoff_tracer_finish(struct liftoff_tracer *tracer)
{
if (tracer->f != NULL) {
fclose(tracer->f);
}
}

void
liftoff_tracer_mark(struct liftoff_tracer *tracer, const char *format, ...)
{
if (tracer->f == NULL) {
return;
}

fprintf(tracer->f, "libliftoff: ");

va_list args;
va_start(args, format);
vfprintf(tracer->f, format, args);
va_end(args);

fprintf(tracer->f, "\n");
fflush(tracer->f);
}