* [PATCH] perf/scripts: tuple was set from long in both branches in python_process_event()
@ 2010-03-22 17:22 Roel Kluin
2010-03-25 5:42 ` Tom Zanussi
0 siblings, 1 reply; 3+ messages in thread
From: Roel Kluin @ 2010-03-22 17:22 UTC (permalink / raw)
To: Tom Zanussi, Andrew Morton, LKML
Regardless of the sign, the tuple was set from a long.
Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
.../util/scripting-engines/trace-event-python.c | 13 ++++++++-----
1 files changed, 8 insertions(+), 5 deletions(-)
Was something like this intended?
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 33a414b..1d710b6 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
int size __unused,
unsigned long long nsecs, char *comm)
{
- PyObject *handler, *retval, *context, *t;
+ PyObject *handler, *retval, *context, *t, *obj;
static char handler_name[256];
struct format_field *field;
unsigned long long val;
@@ -256,16 +256,19 @@ static void python_process_event(int cpu, void *data,
offset &= 0xffff;
} else
offset = field->offset;
- PyTuple_SetItem(t, n++,
- PyString_FromString((char *)data + offset));
+ obj = PyString_FromString((char *)data + offset);
} else { /* FIELD_IS_NUMERIC */
val = read_size(data + field->offset, field->size);
if (field->flags & FIELD_IS_SIGNED) {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ obj = PyInt_FromLong(val);
} else {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ if (sizeof(val) > sizeof(Py_ssize_t))
+ val += 1ULL <<
+ (sizeof(Py_ssize_t) * 8);
+ obj = PyLong_FromUnsignedLongLong(val);
}
}
+ PyTuple_SetItem(t, n++, obj);
}
if (_PyTuple_Resize(&t, n) == -1)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] perf/scripts: tuple was set from long in both branches in python_process_event()
2010-03-22 17:22 [PATCH] perf/scripts: tuple was set from long in both branches in python_process_event() Roel Kluin
@ 2010-03-25 5:42 ` Tom Zanussi
0 siblings, 0 replies; 3+ messages in thread
From: Tom Zanussi @ 2010-03-25 5:42 UTC (permalink / raw)
To: Roel Kluin; +Cc: Andrew Morton, LKML
Hi,
On Mon, 2010-03-22 at 18:22 +0100, Roel Kluin wrote:
> Regardless of the sign, the tuple was set from a long.
>
> Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
> ---
> .../util/scripting-engines/trace-event-python.c | 13 ++++++++-----
> 1 files changed, 8 insertions(+), 5 deletions(-)
>
> Was something like this intended?
Yeah, something like that - thanks for noticing it and providing the
patch. After looking at the Python implementation, I modified your
patch as below...
Basically, Python wants to use a PyInt (which is internally a long) if
it can i.e. if the value will fit into that type. If not, it stores it
into a PyLong, which isn't actually a long, but an arbitrary-precision
integer variable.
The code below is similar to to what Python does internally, and it
seems to work as expected on the x86 and x86_64 sytems I tested it on.
Thanks,
Tom
Signed-off-by: Tom Zanussi <tzanussi@gmail.com>
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 33a414b..6a72f14 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
int size __unused,
unsigned long long nsecs, char *comm)
{
- PyObject *handler, *retval, *context, *t;
+ PyObject *handler, *retval, *context, *t, *obj;
static char handler_name[256];
struct format_field *field;
unsigned long long val;
@@ -256,16 +256,23 @@ static void python_process_event(int cpu, void *data,
offset &= 0xffff;
} else
offset = field->offset;
- PyTuple_SetItem(t, n++,
- PyString_FromString((char *)data + offset));
+ obj = PyString_FromString((char *)data + offset);
} else { /* FIELD_IS_NUMERIC */
val = read_size(data + field->offset, field->size);
if (field->flags & FIELD_IS_SIGNED) {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ if ((long long)val >= LONG_MIN &&
+ (long long)val <= LONG_MAX)
+ obj = PyInt_FromLong(val);
+ else
+ obj = PyLong_FromLongLong(val);
} else {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ if (val <= LONG_MAX)
+ obj = PyInt_FromLong(val);
+ else
+ obj = PyLong_FromUnsignedLongLong(val);
}
}
+ PyTuple_SetItem(t, n++, obj);
}
if (_PyTuple_Resize(&t, n) == -1)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH] perf/scripts: tuple was set from long in both branches in python_process_event()
@ 2010-04-02 4:58 Tom Zanussi
0 siblings, 0 replies; 3+ messages in thread
From: Tom Zanussi @ 2010-04-02 4:58 UTC (permalink / raw)
To: linux-kernel; +Cc: Roel Kluin, mingo, Frederic Weisbecker, rostedt
This is a fix to the signed/unsigned field handling in the Python
scripting engine, based on a patch from Roel Kluin.
Basically, Python wants to use a PyInt (which is internally a long) if
it can i.e. if the value will fit into that type. If not, it stores it
into a PyLong, which isn't actually a long, but an arbitrary-precision
integer variable.
The code below is similar to to what Python does internally, and it
seems to work as expected on the x86 and x86_64 sytems I tested it on.
Signed-off-by: Tom Zanussi <tzanussi@gmail.com>
---
.../util/scripting-engines/trace-event-python.c | 17 ++++++++++++-----
1 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 33a414b..6a72f14 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
int size __unused,
unsigned long long nsecs, char *comm)
{
- PyObject *handler, *retval, *context, *t;
+ PyObject *handler, *retval, *context, *t, *obj;
static char handler_name[256];
struct format_field *field;
unsigned long long val;
@@ -256,16 +256,23 @@ static void python_process_event(int cpu, void *data,
offset &= 0xffff;
} else
offset = field->offset;
- PyTuple_SetItem(t, n++,
- PyString_FromString((char *)data + offset));
+ obj = PyString_FromString((char *)data + offset);
} else { /* FIELD_IS_NUMERIC */
val = read_size(data + field->offset, field->size);
if (field->flags & FIELD_IS_SIGNED) {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ if ((long long)val >= LONG_MIN &&
+ (long long)val <= LONG_MAX)
+ obj = PyInt_FromLong(val);
+ else
+ obj = PyLong_FromLongLong(val);
} else {
- PyTuple_SetItem(t, n++, PyInt_FromLong(val));
+ if (val <= LONG_MAX)
+ obj = PyInt_FromLong(val);
+ else
+ obj = PyLong_FromUnsignedLongLong(val);
}
}
+ PyTuple_SetItem(t, n++, obj);
}
if (_PyTuple_Resize(&t, n) == -1)
--
1.6.4.GIT
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-04-02 4:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-22 17:22 [PATCH] perf/scripts: tuple was set from long in both branches in python_process_event() Roel Kluin
2010-03-25 5:42 ` Tom Zanussi
-- strict thread matches above, loose matches on Subject: below --
2010-04-02 4:58 Tom Zanussi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox