public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* How can I check creator of probe point?
@ 2014-10-14  2:16 Gioh Kim
  2014-10-16  6:00 ` Masami Hiramatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Gioh Kim @ 2014-10-14  2:16 UTC (permalink / raw)
  To: Ananth N Mavinakayanahalli, Anil S Keshavamurthy, David S. Miller,
	Masami Hiramatsu, LKML


Hi,

I am trying to find a way to make statistics for memory allocation of my device driver.
I want to know how much memory it allocates and how many times it calls kmalloc().

So I am considering to use kprobe but I think it doesn't provide a way to identify who makes the probe point.
Can I distinguish kmalloc() calling only from my driver?

For example I think it could be like this:

diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c
index 1041b67..5322e0a 100644
--- a/samples/kprobes/kretprobe_example.c
+++ b/samples/kprobes/kretprobe_example.c
@@ -32,6 +32,7 @@ MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"

 /* per-instance private data */
 struct my_data {
+       unsigned long signature;
        ktime_t entry_stamp;
 };

@@ -43,8 +44,10 @@ static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
        if (!current->mm)
                return 1;       /* Skip kernel threads */

-       data = (struct my_data *)ri->data;
-       data->entry_stamp = ktime_get();
+       if (signature == 0xabcdabcd) {
+               data = (struct my_data *)ri->data;
+               data->entry_stamp = ktime_get();
+       }
        return 0;
 }

@@ -60,10 +63,12 @@ static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
        s64 delta;
        ktime_t now;

-       now = ktime_get();
-       delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
-       printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
-                       func_name, retval, (long long)delta);
+       if (signature == 0xabcdabcd) {
+               now = ktime_get();
+               delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
+               printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
+                      func_name, retval, (long long)delta);
+       }
        return 0;
 }


-- 
Thanks,
Gioh Kim

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: How can I check creator of probe point?
  2014-10-14  2:16 How can I check creator of probe point? Gioh Kim
@ 2014-10-16  6:00 ` Masami Hiramatsu
  2014-10-16  6:45   ` Gioh Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2014-10-16  6:00 UTC (permalink / raw)
  To: Gioh Kim
  Cc: Ananth N Mavinakayanahalli, Anil S Keshavamurthy, David S. Miller,
	LKML

Hello Gioh,

If you can use ftrace and perftool, you can also put probes on
caller site. perf-probe will give you source-code level probes.
Or, just trace kmalloc event with ftrace stacktrace option, which
gives you caller information so that you can filter your driver
by postprocessing.

Thank you,

(2014/10/14 11:16), Gioh Kim wrote:
> 
> Hi,
> 
> I am trying to find a way to make statistics for memory allocation of my device driver.
> I want to know how much memory it allocates and how many times it calls kmalloc().
> 
> So I am considering to use kprobe but I think it doesn't provide a way to identify who makes the probe point.
> Can I distinguish kmalloc() calling only from my driver?
> 
> For example I think it could be like this:
> 
> diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c
> index 1041b67..5322e0a 100644
> --- a/samples/kprobes/kretprobe_example.c
> +++ b/samples/kprobes/kretprobe_example.c
> @@ -32,6 +32,7 @@ MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
> 
>  /* per-instance private data */
>  struct my_data {
> +       unsigned long signature;
>         ktime_t entry_stamp;
>  };
> 
> @@ -43,8 +44,10 @@ static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
>         if (!current->mm)
>                 return 1;       /* Skip kernel threads */
> 
> -       data = (struct my_data *)ri->data;
> -       data->entry_stamp = ktime_get();
> +       if (signature == 0xabcdabcd) {
> +               data = (struct my_data *)ri->data;
> +               data->entry_stamp = ktime_get();
> +       }
>         return 0;
>  }
> 
> @@ -60,10 +63,12 @@ static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
>         s64 delta;
>         ktime_t now;
> 
> -       now = ktime_get();
> -       delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
> -       printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
> -                       func_name, retval, (long long)delta);
> +       if (signature == 0xabcdabcd) {
> +               now = ktime_get();
> +               delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
> +               printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
> +                      func_name, retval, (long long)delta);
> +       }
>         return 0;
>  }
> 
> 


-- 
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How can I check creator of probe point?
  2014-10-16  6:00 ` Masami Hiramatsu
@ 2014-10-16  6:45   ` Gioh Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Gioh Kim @ 2014-10-16  6:45 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ananth N Mavinakayanahalli, Anil S Keshavamurthy, David S. Miller,
	LKML

I'm going to try them.
Thanks a lot.


2014-10-16 오후 3:00, Masami Hiramatsu 쓴 글:
> Hello Gioh,
> 
> If you can use ftrace and perftool, you can also put probes on
> caller site. perf-probe will give you source-code level probes.
> Or, just trace kmalloc event with ftrace stacktrace option, which
> gives you caller information so that you can filter your driver
> by postprocessing.
> 
> Thank you,
> 
> (2014/10/14 11:16), Gioh Kim wrote:
>>
>> Hi,
>>
>> I am trying to find a way to make statistics for memory allocation of my device driver.
>> I want to know how much memory it allocates and how many times it calls kmalloc().
>>
>> So I am considering to use kprobe but I think it doesn't provide a way to identify who makes the probe point.
>> Can I distinguish kmalloc() calling only from my driver?
>>
>> For example I think it could be like this:
>>
>> diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c
>> index 1041b67..5322e0a 100644
>> --- a/samples/kprobes/kretprobe_example.c
>> +++ b/samples/kprobes/kretprobe_example.c
>> @@ -32,6 +32,7 @@ MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
>>
>>   /* per-instance private data */
>>   struct my_data {
>> +       unsigned long signature;
>>          ktime_t entry_stamp;
>>   };
>>
>> @@ -43,8 +44,10 @@ static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
>>          if (!current->mm)
>>                  return 1;       /* Skip kernel threads */
>>
>> -       data = (struct my_data *)ri->data;
>> -       data->entry_stamp = ktime_get();
>> +       if (signature == 0xabcdabcd) {
>> +               data = (struct my_data *)ri->data;
>> +               data->entry_stamp = ktime_get();
>> +       }
>>          return 0;
>>   }
>>
>> @@ -60,10 +63,12 @@ static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
>>          s64 delta;
>>          ktime_t now;
>>
>> -       now = ktime_get();
>> -       delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
>> -       printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
>> -                       func_name, retval, (long long)delta);
>> +       if (signature == 0xabcdabcd) {
>> +               now = ktime_get();
>> +               delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
>> +               printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
>> +                      func_name, retval, (long long)delta);
>> +       }
>>          return 0;
>>   }
>>
>>
> 
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-16  6:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-14  2:16 How can I check creator of probe point? Gioh Kim
2014-10-16  6:00 ` Masami Hiramatsu
2014-10-16  6:45   ` Gioh Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox