public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Willgerodt, Felix" <felix.willgerodt@intel.com>
To: "Metzger, Markus T" <markus.t.metzger@intel.com>
Cc: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH v5 09/10] btrace, python: Enable ptwrite filter registration.
Date: Wed, 13 Jul 2022 08:49:32 +0000	[thread overview]
Message-ID: <MN2PR11MB45665F69DF0FE8A31FF777FB8E899@MN2PR11MB4566.namprd11.prod.outlook.com> (raw)
In-Reply-To: <DM8PR11MB57494F93F51EC81E56CB2746DE869@DM8PR11MB5749.namprd11.prod.outlook.com>

> -----Original Message-----
> From: Metzger, Markus T <markus.t.metzger@intel.com>
> Sent: Dienstag, 12. Juli 2022 14:24
> To: Willgerodt, Felix <felix.willgerodt@intel.com>
> Cc: gdb-patches@sourceware.org
> Subject: RE: [PATCH v5 09/10] btrace, python: Enable ptwrite filter
> registration.
> 
> Hello Felix,
> 
> >> Why to we call the void * parameter ptw_filter instead of the usual
> context?
> >> We probably want to call the callback itself ptw_filter and the void *
> >> argument
> >> context.
> >>
> >> We also seem to mix the terms ptwrite callback and ptwrite filter.
> >
> >I think the problem here is that both are callbacks. The first one
> >(ptw_callback_fun)
> >is used in btrace.c to call python/py-record-btrace.c:recpy_call_filter, or
> another
> >extension language that would provide this functionality,
> >see extension.c:apply_ext_lang_ptwrite_filter.
> >The second one (ptw_filter) is what recpy_call_filter will use to do the
> python call
> >via
> >PyObject_CallFunctionObjArgs().
> >
> >So we call a callback with another callback as the argument (which is the
> actual
> >ptw_filter).
> >Therefore the current naming seems correct to me. As the ptw_filter to me
> >clearly is
> >the void *.
> 
> That's from python's perspective.  The fact that we pass the python object
> that
> implements the python filter to the btrace callback is a detail of the python
> implementation, though.
> 
> From btrace's perspective, this is the ptwrite callback/filter and the void * is
> whatever context that callback needs passed as argument.  I'm fine to call
> the
> function ptw_callback but the context shouldn't be called ptwrite_filter.
> 
> In proper C++, we'd probably have a single member for a callable object that
> may store its context inside.  Would this even work for python?
>
> >I am open to suggestions, but calling the actual filter function context
> >and the
> >gdb internal callback the filter seems wrong to me.
> 
> Not from btrace's perspective and the code is in btrace.  Python just uses
> it in a particular way.

I get your argumentation. I still think ptw_filter is perfectly fine. As that is
what it will always be, even for other extension languages. I see it more
from a "global GDB" perspective, rather than "btrace vs python" or from a
"callback concept" POV.
Regardless, I will call it to ptw_context in the next revision. Is that ok?


> >> >+def _update_filter_dict(thread_list):
> >> >+    """Helper function to update the filter dict.
> >> >+
> >> >+    Discards filter copies of threads that already exited and registers
> >> >+    copies of the filter for new threads."""
> >> >+    # thread_list[x].ptid returns the tuple (pid, lwp, tid)
> >> >+    lwp_list = [i.ptid[1] for i in thread_list]
> >> >+
> >> >+    # clean-up old filters
> >> >+    for key in _ptwrite_filter.keys():
> >> >+      if key not in lwp_list and key != "global":
> >> >+        _ptwrite_filter.pop(key)
> >> >+
> >> >+    # Register filter for new threads
> >> >+    for key in lwp_list:
> >> >+        if key not in _ptwrite_filter.keys():
> >> >+            _ptwrite_filter[key] = deepcopy(_ptwrite_filter["global"])
> >>
> >> This function is called two times: once after we cleared all filters, and
> >> once when looking up the filter for a given thread.  The first time, we
> >> know that there are no existing filters; the second time, we are really
> >> only interested in a single filter.
> >>
> >> Wouldn't it suffice to lookup the filter in get_filter() and, if it doesn't
> >> exist, create a new one?
> >
> >Yes, we could get rid of the call to _update_filter_dict() in register_filter().
> >The main reason I added it was to clean the obsolete filters whenever
> >possible. I don't see a clear performance advantage if we would remove
> >the call (without having a thread exit notification).
> >We need to clean up the same amount of filters at some point.
> >
> >> That leaves removing obsolete filters.  Could this be done with some
> >> thread notification?
> >
> >IIRC, you suggested this previously. I replied that there is no python API
> >that I am aware of that can do this. The python events API doesn't expose
> >thread exited events.
> 
> I keep stumbling over this.
> 
> When looking up a filter, we are clearly only interested in one thread.
> Just looking up that one and creating it when it is missing seems a lot
> more straight forward.
> 
> Lacking a thread exit notification, we could still add a _prune_filters
> function that we call every now and then that just removes filters for
> exited threads.
> 
> Does that sound reasonable?  We'd need to find good places to call
> it from.

To me that is kind of what I have implemented now. Just not with a
separate _prune_filters() and doing it in "two good places".

But I just realized that having it in get_filter() would only improve
performance if someone would call get_filter() from python directly.
Which probably isn't a scenario worth optimizing for.

Are you okay with changing get_filter and inlining _update_filter_dict?

--- a/gdb/python/lib/gdb/ptwrite.py
+++ b/gdb/python/lib/gdb/ptwrite.py
@@ -29,25 +29,6 @@ def default_filter(payload, ip):
 _ptwrite_filter = {"global" : default_filter}
 
 
-def _update_filter_dict(thread_list):
-    """Helper function to update the filter dict.
-
-    Discards filter copies of threads that already exited and registers
-    copies of the filter for new threads."""
-    # thread_list[x].ptid returns the tuple (pid, lwp, tid)
-    lwp_list = [i.ptid[1] for i in thread_list]
-
-    # Clean-up old filters
-    for key in _ptwrite_filter.keys():
-      if key not in lwp_list and key != "global":
-        _ptwrite_filter.pop(key)
-
-    # Register filter for new threads
-    for key in lwp_list:
-        if key not in _ptwrite_filter.keys():
-            _ptwrite_filter[key] = deepcopy(_ptwrite_filter["global"])
-
-
 def _clear_traces(thread_list):
     """Helper function to clear the trace of all threads in THREAD_LIST."""
     current_thread = gdb.selected_thread()
@@ -74,12 +55,26 @@ def register_filter(filter):
     _ptwrite_filter.clear()
     _ptwrite_filter["global"] = filter
 
-    _update_filter_dict(thread_list)
+    # thread_list[x].ptid returns the tuple (pid, lwp, tid).
+    lwp_list = [i.ptid[1] for i in thread_list]
+
+    # Clean-up old filters.
+    for key in _ptwrite_filter.keys():
+      if key not in lwp_list and key != "global":
+        _ptwrite_filter.pop(key)
+
+    # Register filter for new threads.
+    for key in lwp_list:
+        if key not in _ptwrite_filter.keys():
+            _ptwrite_filter[key] = deepcopy(_ptwrite_filter["global"])
 
 
 def get_filter():
     """Returns the filters of the current thread."""
-    thread_list = gdb.Inferior.threads(gdb.selected_inferior())
-    _update_filter_dict(thread_list)
+    key = gdb.selected_thread().ptid[1]
+
+    # This could be a new thread.
+    if key not in _ptwrite_filter.keys():
+        _ptwrite_filter[key] = deepcopy(_ptwrite_filter["global"])
 
-    return _ptwrite_filter[gdb.selected_thread().ptid[1]]
+    return _ptwrite_filter[key]

Thanks,
Felix
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

  reply	other threads:[~2022-07-13  8:49 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-22 11:43 [PATCH v5 00/10] Extensions for PTWRITE Felix Willgerodt
2022-06-22 11:43 ` [PATCH v5 01/10] btrace: Introduce auxiliary instructions Felix Willgerodt
2022-06-28  9:10   ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 02/10] btrace: Enable auxiliary instructions in record instruction-history Felix Willgerodt
2022-06-28  9:10   ` Metzger, Markus T
2022-06-28 11:28     ` Willgerodt, Felix
2022-06-29 10:43       ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 03/10] btrace: Enable auxiliary instructions in record function-call-history Felix Willgerodt
2022-06-28  9:10   ` Metzger, Markus T
2022-09-19  8:59     ` Willgerodt, Felix
2022-06-22 11:43 ` [PATCH v5 04/10] btrace: Handle stepping and goto for auxiliary instructions Felix Willgerodt
2022-06-28  9:11   ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 05/10] python: Introduce gdb.RecordAuxiliary class Felix Willgerodt
2022-06-28  9:11   ` Metzger, Markus T
2022-07-11 12:48     ` Willgerodt, Felix
2022-06-22 11:43 ` [PATCH v5 06/10] python: Add clear() to gdb.Record Felix Willgerodt
2022-06-28  9:11   ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 07/10] btrace, gdbserver: Add ptwrite to btrace_config_pt Felix Willgerodt
2022-06-28  9:11   ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 08/10] btrace, linux: Enable ptwrite packets Felix Willgerodt
2022-06-28  9:12   ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 09/10] btrace, python: Enable ptwrite filter registration Felix Willgerodt
2022-06-28 13:59   ` Metzger, Markus T
2022-07-11 12:48     ` Willgerodt, Felix
2022-07-12 12:23       ` Metzger, Markus T
2022-07-13  8:49         ` Willgerodt, Felix [this message]
2022-07-13 15:20           ` Metzger, Markus T
2022-07-26 14:08             ` Willgerodt, Felix
2022-09-14  8:37               ` Metzger, Markus T
2022-06-22 11:43 ` [PATCH v5 10/10] btrace: Extend ptwrite event decoding Felix Willgerodt
2022-06-29 13:35   ` Metzger, Markus T
2022-09-19  8:59     ` Willgerodt, Felix

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=MN2PR11MB45665F69DF0FE8A31FF777FB8E899@MN2PR11MB4566.namprd11.prod.outlook.com \
    --to=felix.willgerodt@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=markus.t.metzger@intel.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).