Skip to content

Instantly share code, notes, and snippets.

@WOnder93
Created July 18, 2016 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WOnder93/40035e2721c35673b28ed4a92a0b2732 to your computer and use it in GitHub Desktop.
Save WOnder93/40035e2721c35673b28ed4a92a0b2732 to your computer and use it in GitHub Desktop.
Profiling microframework for the Linux kernel.
#ifndef _CRYPTO_PROFILING_H
#define _CRYPTO_PROFILING_H
#ifdef USE_PROFILING
#include <linux/ktime.h>
#ifndef PROFILING_STACK_SIZE
#define PROFILING_STACK_SIZE 64
#endif
struct prof_entry {
const char *probe_name;
u64 duration;
u64 last_start;
};
struct prof_ctx {
u64 overhead;
unsigned int size;
struct prof_entry stack[PROFILING_STACK_SIZE];
};
static inline void prof_push(struct prof_ctx *ctx, const char *name)
{
u64 start = ktime_to_ns(ktime_get());
struct prof_entry *parent, *self = &ctx->stack[ctx->size];
BUG_ON(ctx->size >= PROFILING_STACK_SIZE);
if (likely(ctx->size > 0)) {
parent = self - 1;
parent->duration += start - parent->last_start;
}
++ctx->size;
self->probe_name = name;
self->duration = 0;
self->last_start = ktime_to_ns(ktime_get());
}
static inline u64 prof_pop(struct prof_ctx *ctx)
{
u64 duration, start = ktime_to_ns(ktime_get());
struct prof_entry *self, *parent;
unsigned int i;
BUG_ON(ctx->size == 0);
--ctx->size;
self = &ctx->stack[ctx->size];
duration = self->duration + (start - self->last_start);
if (likely(duration >= ctx->overhead)) {
duration -= ctx->overhead;
}
if (likely(self->probe_name != NULL)) {
printk("[PROF] %20lu ns | ", (unsigned long int)duration);
for (i = 0; i < ctx->size; i++) {
printk(" ");
}
printk("%s\n", self->probe_name);
}
if (likely(ctx->size > 0)) {
parent = self - 1;
parent->duration += duration;
parent->last_start = ktime_to_ns(ktime_get());
}
return duration;
}
static inline void prof_init(struct prof_ctx *ctx)
{
ctx->overhead = 0;
ctx->size = 0;
/* measure overhead: */
prof_push(ctx, NULL);
ctx->overhead = prof_pop(ctx);
}
#else
struct prof_ctx { };
static inline void prof_init(struct prof_ctx *ctx) { }
static inline void prof_push(struct prof_ctx *ctx, const char *name) { }
static inline u64 prof_pop(struct prof_ctx *ctx) { return 0; }
#endif /* USE_PROFILING */
#endif /* _CRYPTO_PROFILING_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment