mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Abhishek Sagar" <sagar.abhishek@gmail.com>
To: "Jim Keniston" <jkenisto@us.ibm.com>
Cc: "Srinivasa Ds" <srinivasa@in.ibm.com>,
	linux-kernel@vger.kernel.org, prasanna@in.ibm.com,
	davem@davemloft.net, anil.s.keshavamurthy@intel.com,
	"Ananth N Mavinakayanahalli" <ananth@in.ibm.com>
Subject: Re: [PATCH][RFC] kprobes: Add user entry-handler in kretprobes
Date: Sat, 17 Nov 2007 00:23:57 +0530	[thread overview]
Message-ID: <863e9df20711161053q7f6ae3d5vb19b23d2ce6b20f7@mail.gmail.com> (raw)
In-Reply-To: <1195171644.3804.124.camel@dyn9047018096.beaverton.ibm.com>

On Nov 16, 2007 5:37 AM, Jim Keniston <jkenisto@us.ibm.com> wrote:
> On Thu, 2007-11-15 at 20:30 +0530, Abhishek Sagar wrote:
> > On Nov 15, 2007 4:21 AM, Jim Keniston <jkenisto@us.ibm.com> wrote:
> > > 2. Simplify the task of correlating data (e.g., timestamps) between
> > > function entry and function return.
> >
> > Would adding of data and len fields in ri help? Instead of "pouching"
> > data in one go at registration time, this would let user handlers do
> > the allocation
>
> Yes and no.  Adding just a data field -- void*, or maybe unsigned long
> long so it's big enought to accommodate big timestamps -- would be a big
> improvement on your current proposal.  That would save the user the
> drudgery of mapping the ri pointer to his/her per-instance data.
> There's plenty of precedent for passing "private_data" values to
> callbacks.
>
> I don't think a len field would help much.  If such info were needed, it
> could be stored in the data structure pointed to by the data field.
>
> I still don't think "letting [i.e., requiring that] user handlers do the
> allocation" is a win.  I'm still interested to see how this plays out in
> real examples.
>
> > and allow them to use different kinds of data
> > structures per-instance.
>
> I haven't been able to think of any scenarios where this would be
> useful.  A "data pouch" could always contain a union, FWIW.

I'm inlining a sample module which uses a data pointer in ri. I didn't
go for a timestamp because it's not reliable. Some platforms might
simply not have any h/w timestamp counters. For the same reason a lot
of platforms (on ARM, say) have their sched_clock() mapped on jiffies.
This may prevent timestamps from being distinct across function entry
and exit. Plus a data pointer looks pretty harmless.

--- test module ---

#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/ktime.h>

#define PRINT_DELAY (10 * HZ)

/* This module calculates the total time and instances of func being called
 * across all cpu's. An average is calculated every 10 seconds and displayed.
 * Only one function instance per-task is monitored. This cuts out the
 * possibility of measuring time for recursive and nested function
 * invocations.
 *
 * Note: If compiling as a standalone module, make sure sched_clock() is
 * exported in the kernel. */

/* per-task data */
struct prof_data {
	struct task_struct *task;
	struct list_head list;
	unsigned long long entry_stamp;
};

static const char *func = "sys_open";
static spinlock_t time_lock;
static ktime_t total_time;
static unsigned long hits;
static LIST_HEAD(data_nodes); /* list of per-task data */
static struct delayed_work work_print;

static struct prof_data *get_per_task_data(struct task_struct *tsk)
{
	struct prof_data *p;

	/* lookup prof_data corresponding to tsk */
	list_for_each_entry(p, &data_nodes, list) {
		if (p->task == tsk)
			return p;
	}
	return NULL;
}

/* called with kretprobe_lock held */
static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
	struct prof_data *stats;

	stats = get_per_task_data(current);
	if (stats)
		return 1; /* recursive/nested call */

	stats = kmalloc(sizeof(struct prof_data), GFP_ATOMIC);
	if (!stats)
		return 1;

	stats->entry_stamp = sched_clock();
	stats->task = current;
	INIT_LIST_HEAD(&stats->list);
	list_add(&stats->list, &data_nodes);
	ri->data = stats;
	return 0;
}

/* called with kretprobe_lock held */
static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
	unsigned long flags;
	struct prof_data *stats = (struct prof_data *)ri->data;
	u64 elapsed;

	BUG_ON(ri->data == NULL);
	elapsed = (long long)sched_clock() - (long long)stats->entry_stamp;

	/* update stats */
	spin_lock_irqsave(&time_lock, flags);
	++hits;
	total_time = ktime_add_ns(total_time, elapsed);
	spin_unlock_irqrestore(&time_lock, flags);

	list_del(&stats->list);
	kfree(stats);
	return 0;
}

static struct kretprobe my_kretprobe = {
	.handler = return_handler,
	.entry_handler = entry_handler,
};

/* called after every PRINT_DELAY seconds */
static void print_time(struct work_struct *work)
{
	unsigned long flags;
	s64 time_ns;
	struct timespec ts;

	BUG_ON(work != &work_print.work);
	spin_lock_irqsave(&time_lock, flags);
	time_ns = ktime_to_ns(total_time);
	do_div(time_ns, hits);
	spin_unlock_irqrestore(&time_lock, flags);

	ts = ns_to_timespec(time_ns);
	printk(KERN_DEBUG "Avg. running time of %s = %ld sec, %ld nsec\n",
	       func, ts.tv_sec, ts.tv_nsec);
	schedule_delayed_work(&work_print, PRINT_DELAY);
}

static int __init test_module_init(void)
{
	int ret;
	my_kretprobe.kp.symbol_name = (char *)func;

	spin_lock_init(&time_lock);
	if ((ret = register_kretprobe(&my_kretprobe)) < 0) {
		printk("Failed to register test kretprobe!\n");
		return -1;
	}
	printk("Kretprobe active on %s\n", my_kretprobe.kp.symbol_name);
	INIT_DELAYED_WORK(&work_print, print_time);
	schedule_delayed_work(&work_print, PRINT_DELAY);
	return 0;
}

static void __exit test_module_exit(void)
{
	unregister_kretprobe(&my_kretprobe);
	printk("kretprobe unregistered\n");
	printk("Missed probing %d instances of %s\n",
		my_kretprobe.nmissed, func);
}

module_init(test_module_init)
module_exit(test_module_exit)
MODULE_LICENSE("GPL");

--
Abhishek Sagar

  reply	other threads:[~2007-11-16 18:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <47389BEB.1000901@gmail.com>
2007-11-12 18:39 ` Abhishek Sagar
2007-11-13 10:47   ` Abhishek Sagar
2007-11-14  7:57     ` Srinivasa Ds
2007-11-14  8:49       ` Abhishek Sagar
2007-11-14 10:23         ` Srinivasa Ds
2007-11-14 13:30           ` Abhishek Sagar
2007-11-14 22:51             ` Jim Keniston
2007-11-15 13:16               ` Abhishek Sagar
2007-11-15 21:16                 ` Jim Keniston
2007-11-16 17:50                   ` Abhishek Sagar
2007-11-17  0:54                     ` Jim Keniston
2007-11-17 18:15                       ` Abhishek Sagar
2007-11-19 12:26                       ` Abhishek Sagar
2007-11-21  5:55                         ` Jim Keniston
2007-11-21 10:20                           ` Abhishek Sagar
2007-11-27  0:54                             ` Jim Keniston
2007-11-15 15:00               ` Abhishek Sagar
2007-11-16  0:07                 ` Jim Keniston
2007-11-16 18:53                   ` Abhishek Sagar [this message]
2007-11-16 23:09                     ` Jim Keniston
2007-11-17 17:09                       ` Abhishek Sagar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=863e9df20711161053q7f6ae3d5vb19b23d2ce6b20f7@mail.gmail.com \
    --to=sagar.abhishek@gmail.com \
    --cc=ananth@in.ibm.com \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=davem@davemloft.net \
    --cc=jkenisto@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=prasanna@in.ibm.com \
    --cc=srinivasa@in.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®