Skip to contentSkip to conversation

Support CPU list parsing in xentrace.

PATCH New Konrad Rzeszutek Wilk 3 participants 9 messages Apr 4, 2015 – Apr 23, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 Support CPU list parsing in xentrace.

Hey George and Ian,

Since v3 [http://lists.xen.org/archives/html/xen-devel/2015-03/msg02986.html]:
- Rebased code on George's patch.
- Added support for 'all' in CPU parsing.

In v2 [http://lists.xen.org/archives/html/xen-devel/2014-06/msg01835.html]:
- Redid the code per George's feedback.
- Expanded the xc_cpumap_* calls so that we have an 'setbit' variant.

The purpose of these patches is to allow users of xentrace to narrow down a specific CPU without having to figure out a bit mask. They fix the limitation of the bit mask which is it can only do up to 32-bits
- which on large machines (say 120CPUs), you can't selectively trace anything past 32CPUs.

The code expands the -c parameter where you can do -c <starting cpu>-<end cpu>, or -c <cpu1>,<cpu2>, or a combination of them, or "all". There is also the mode of automatic detection, such as: -c -,<cpu2> (so it will assume up to cpu2 - so 0,1, and 2 CPU), or the inverse: -c <cpu2>- (which will figure the max cpus and do it from cpu2 up to maximum cpu).

This along with 'xl vcpu-list' makes it extremely easy to trace a specific guest (if pinned).

You can still use the -c 0x<some hex value> option if you prefer.

The patches are also at my git tree:

git://xenbits.xen.org/people/konradwilk/xen.git xentrace.v4

 tools/libxc/include/xenctrl.h |  11 ++-
 tools/libxc/xc_misc.c         |  30 ++++++
 tools/libxc/xc_tbuf.c         |  33 +++++--
 tools/xentrace/xentrace.8     |  34 ++++++-
 tools/xentrace/xentrace.c     | 212 ++++++++++++++++++++++++++++++++++++++----
 5 files changed, 289 insertions(+), 31 deletions(-)

George Dunlap (1):
libxc/xentrace: Use xc_cpumap_t for xc_tbuf_set_cpu_mask

Konrad Rzeszutek Wilk (2):
libxl/cpumap: Add xc_cpumap_[setcpu, clearcpu, testcpu] to complement xc_cpumap_alloc.
xentrace: Implement cpu mask range parsing of human values (-c).

#1 lore raw

PATCH v4 1/3 libxl/cpumap: Add xc_cpumap_[setcpu, clearcpu, testcpu] to complement xc_cpumap_alloc.

We export the xc_cpumap_alloc but not the bit operations. One could include 'xc_bitops.h' but that is naughty - so instead we just export the proper functions to do it on the xc_cpumap_t typedef.

----
v2: Use our own macro to make sure ARM is not affected negatively

---
 tools/libxc/include/xenctrl.h |  9 +++++++++
 tools/libxc/xc_misc.c         | 30 ++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
tools/libxc/include/xenctrl.h +9 -0
index 44c7ac0..0b08747 100644
@@ -394,6 +394,15 @@ int xc_get_cpumap_size(xc_interface *xch);
 /* allocate a cpumap */
 xc_cpumap_t xc_cpumap_alloc(xc_interface *xch);
 
+/* clear an CPU from the cpumap. */
+void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map);
+
+/* set an CPU in the cpumap. */
+void xc_cpumap_setcpu(int cpu, xc_cpumap_t map);
+
+/* Test whether the CPU in cpumap is set. */
+int xc_cpumap_testcpu(int cpu, xc_cpumap_t map);
+
 /*
  * NODEMAP handling
  */
tools/libxc/xc_misc.c +30 -0
index be68291..e113385 100644
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include "xc_bitops.h"
 #include "xc_private.h"
 #include <xen/hvm/hvm_op.h>
 
@@ -93,6 +94,35 @@ xc_cpumap_t xc_cpumap_alloc(xc_interface *xch)
     return calloc(1, sz);
 }
 
+/*
+ * xc_bitops.h has macros that do this as well - however xc_cpumap_t
+ * is an array of uint8 (1byte) while said macros expect an array
+ * of unsigned long (8 bytes). This is not a problem when setting
+ * an bit (as the computation will come with the same offset) - the
+ * issue is when clearing an bit. The aligment on ARM could affect
+ * other structures that are close in memory causing memory corruption.
+ * Hence our own macros that differ only in that we compute the
+ * size of the array elements (sizeof(*map)) as opposed to assuming
+ * it is unsigned long.
+ */
+#define BITS_PER_CPUMAP(map) (sizeof(*map) * 8)
+#define CPUMAP_ENTRY(cpu, map) ((map))[(cpu) / BITS_PER_CPUMAP(map)]
+#define CPUMAP_SHIFT(cpu, map) ((cpu) % BITS_PER_CPUMAP(map))
+void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map)
+{
+    CPUMAP_ENTRY(cpu, map) &= ~(1U << CPUMAP_SHIFT(cpu, map));
+}
+
+void xc_cpumap_setcpu(int cpu, xc_cpumap_t map)
+{
+    CPUMAP_ENTRY(cpu, map) |= (1U << CPUMAP_SHIFT(cpu, map));
+}
+
+int xc_cpumap_testcpu(int cpu, xc_cpumap_t map)
+{
+    return (CPUMAP_ENTRY(cpu, map) >> CPUMAP_SHIFT(cpu, map)) & 1;
+}
+
 xc_nodemap_t xc_nodemap_alloc(xc_interface *xch)
 {
     int sz;
Signature · 1 line
2.1.0
#2 lore raw

Re: [PATCH v4 1/3] libxl/cpumap: Add xc_cpumap_[setcpu, clearcpu, testcpu] to complement xc_cpumap_alloc.

Quoted 10 lines · Konrad Rzeszutek Wilk

+/*
+ * xc_bitops.h has macros that do this as well - however xc_cpumap_t
+ * is an array of uint8 (1byte) while said macros expect an array
+ * of unsigned long (8 bytes). This is not a problem when setting
+ * an bit (as the computation will come with the same offset) - the
+ * issue is when clearing an bit. The aligment on ARM could affect
+ * other structures that are close in memory causing memory corruption.
+ * Hence our own macros that differ only in that we compute the
+ * size of the array elements (sizeof(*map)) as opposed to assuming
+ * it is unsigned long.

I don't think that it is at all.

The issue is that when compiling a function which takes, say, an unsigned long * the compiler is entitled (via the calling conventions, ABI standards etc) to assume that the pointer will be correctly aligned for an unsigned long type and can therefore emit instructions which assume that (i.e. on ARM a ldr instruction assumes a 4 byte aligned pointer and can trap if that isn't true).

On the calling side the things normally work out, because when using the & operator on a variable the compiler already arranges that it is appropriately aligned and because the caller also knows that it needs to follow the calling conventions etc.

The issue with your previous patch was that you were casting a uint8_t * to an unsigned long *, which was effectively telling the compiler "I know what I'm doing and this is all completely fine" -- which if the uint8_t* wasn't 4 byte aligned (which it isn't required to be by the calling conventions etc AFAIK) isn't true and would violate the assumptions made by the compiler in the called function.

All this affects test and set equally, and it has nothing to do with memory corruption.

Does that make sense?

I don't think you need to go into so much detail in the comment, you can just say that xc_bitops.h assumes that the bitmask is word aligned but that cpumaps are only guaranteed to be byte aligned and so we need byte versions for architectures which do not support misaligned accesses (which is basically everyone but x86, although even on x86 it can be inefficient).

Quoted 22 lines

+ */

tools/libxc/xc_misc.c +18 -0
+#define BITS_PER_CPUMAP(map) (sizeof(*map) * 8)
+#define CPUMAP_ENTRY(cpu, map) ((map))[(cpu) / BITS_PER_CPUMAP(map)]
+#define CPUMAP_SHIFT(cpu, map) ((cpu) % BITS_PER_CPUMAP(map))
+void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map)
+{
+    CPUMAP_ENTRY(cpu, map) &= ~(1U << CPUMAP_SHIFT(cpu, map));
+}
+
+void xc_cpumap_setcpu(int cpu, xc_cpumap_t map)
+{
+    CPUMAP_ENTRY(cpu, map) |= (1U << CPUMAP_SHIFT(cpu, map));
+}
+
+int xc_cpumap_testcpu(int cpu, xc_cpumap_t map)
+{
+    return (CPUMAP_ENTRY(cpu, map) >> CPUMAP_SHIFT(cpu, map)) & 1;
+}
+
 xc_nodemap_t xc_nodemap_alloc(xc_interface *xch)
 {
     int sz;
#3 lore raw

PATCH v4 2/3 libxc/xentrace: Use xc_cpumap_t for xc_tbuf_set_cpu_mask

In reply to Konrad Rzeszutek Wilk

From: George Dunlap <[email protected]>

xentrace is the only caller at the moment. Split the cpu and event mask setting out into seperate functions, but leave the current limit of 32 bits for masks passed in from the command-line.

Based on a patch from Konrad Rzeszutek Wilk <[email protected]>

---
 tools/libxc/include/xenctrl.h |  2 +-
 tools/libxc/xc_tbuf.c         | 33 ++++++++++++++-------
 tools/xentrace/xentrace.c     | 68 ++++++++++++++++++++++++++++++++++++-------
 3 files changed, 81 insertions(+), 22 deletions(-)
tools/libxc/include/xenctrl.h +1 -1
index 0b08747..9fb1307 100644
@@ -1587,7 +1587,7 @@ int xc_tbuf_set_size(xc_interface *xch, unsigned long size);
  */
 int xc_tbuf_get_size(xc_interface *xch, unsigned long *size);
 
-int xc_tbuf_set_cpu_mask(xc_interface *xch, uint32_t mask);
+int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpumap_t mask);
 
 int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask);
 
tools/libxc/xc_tbuf.c +23 -10
index 8777492..a4de491 100644
@@ -113,15 +113,30 @@ int xc_tbuf_disable(xc_interface *xch)
     return tbuf_enable(xch, 0);
 }
 
-int xc_tbuf_set_cpu_mask(xc_interface *xch, uint32_t mask)
+int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpumap_t mask)
 {
     DECLARE_SYSCTL;
-    DECLARE_HYPERCALL_BUFFER(uint8_t, bytemap);
+    DECLARE_HYPERCALL_BOUNCE(mask, 0, XC_HYPERCALL_BUFFER_BOUNCE_IN);
     int ret = -1;
-    uint64_t mask64 = mask;
+    int bits, cpusize;
 
-    bytemap = xc_hypercall_buffer_alloc(xch, bytemap, sizeof(mask64));
-    if ( bytemap == NULL )
+    cpusize = xc_get_cpumap_size(xch);
+    if (cpusize <= 0)
+    {
+        PERROR("Could not get number of cpus");
+        return -1;
+    }
+
+    HYPERCALL_BOUNCE_SET_SIZE(mask, cpusize);
+
+    bits = xc_get_max_cpus(xch);
+    if (bits <= 0)
+    {
+        PERROR("Could not get number of bits");
+        return -1;
+    }
+
+    if ( xc_hypercall_bounce_pre(xch, mask) )
     {
         PERROR("Could not allocate memory for xc_tbuf_set_cpu_mask hypercall");
         goto out;
@@ -131,14 +146,12 @@ int xc_tbuf_set_cpu_mask(xc_interface *xch, uint32_t mask)
     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_set_cpu_mask;
 
-    bitmap_64_to_byte(bytemap, &mask64, sizeof (mask64) * 8);
-
-    set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, bytemap);
-    sysctl.u.tbuf_op.cpu_mask.nr_bits = sizeof(bytemap) * 8;
+    set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, mask);
+    sysctl.u.tbuf_op.cpu_mask.nr_bits = bits;
 
     ret = do_sysctl(xch, &sysctl);
 
-    xc_hypercall_buffer_free(xch, bytemap);
+    xc_hypercall_bounce_post(xch, mask);
 
  out:
     return ret;
tools/xentrace/xentrace.c +57 -11

90 lines not on this page · raw message

Signature · 1 line
2.1.0
Acked-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] libxc/xentrace: Use xc_cpumap_t for xc_tbuf_set_cpu_mask

Quoted 10 lines · Konrad Rzeszutek Wilk

From: George Dunlap <[email protected]>

xentrace is the only caller at the moment. Split the cpu and event mask setting out into seperate functions, but leave the current limit of 32 bits for masks passed in from the command-line.

Based on a patch from Konrad Rzeszutek Wilk <[email protected]>

#5 lore raw

PATCH v4 3/3 xentrace: Implement cpu mask range parsing of human values (-c).

In reply to Konrad Rzeszutek Wilk

Instead of just using -c 0x<some hex value> we can also use: -c <starting cpu>-<end cpu>, -c <cpu1>,<cpu2>, or a combination of them, or 'all' for all cpus.

This new format can include just singular CPUs: -c <cpu1>, or ranges without an start or end (and xentrace will figure out the values), such as: -c -<cpu2> (which will include cpu0, cpu1, and cpu2) or -c <cpu2>- (which will include cpu2 and up to MAX_CPUS).

That should make it easier to trace the right CPU if using this along with 'xl vcpu-list'.

The code has been lifted from the Linux kernel, see file lib/bitmap.c, function __bitmap_parselist.

To make the old behavior and the new function work, we check to see if the arguments have '0x' in them. If they do we use the old style parsing (limited to 32 CPUs). If that does not exist we use the new parsing.

---
[v4: Fix per George's review]
[v5: Redo per George's review]

---
 tools/xentrace/xentrace.8 |  34 +++++++-
 tools/xentrace/xentrace.c | 196 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 195 insertions(+), 35 deletions(-)
tools/xentrace/xentrace.8 +32 -2
index ac18e9f..7b3172b 100644
@@ -36,8 +36,38 @@ all new records to the output
 set the time, p, (in milliseconds) to sleep between polling the buffers
 for new data.
 .TP
-.B -c, --cpu-mask=c
-set bitmask of CPUs to trace. It is limited to 32-bits.
+.B -c, --cpu-mask=[\fIc\fP|\fICPU-LIST\fP|\fIall\fP]
+This can be: a hex value (of the form 0xNNNN...), or a set of cpu
+ranges as described below, or the string \fIall\fP. Hex values are limited
+to 32 bits. If not specified, the cpu-mask as set during bootup will be
+constructed. If using the \fICPU-LIST\fP it expects decimal numbers, which
+may be specified as follows:
+
+.RS 4
+.ie n .IP """0-3""" 4
+.el .IP "``0-3''" 4
+.IX Item "0-3"
+Trace only on CPUs 0 through 3
+.ie n .IP """0,2,5-7""" 4
+.el .IP "``0,2,5-7''" 4
+.IX Item "0,2,5-7"
+Trace only on CPUs 0, 2, and 5 through 7.
+.ie n .IP """-3""" 4
+.el .IP "``-3''" 4
+.IX Item "-3"
+Trace only on CPUs 0 through 3
+.ie n .IP """-3,7""" 4
+.el .IP "``-3,7''" 4
+.IX Item "-3,7"
+Trace only on CPUs 0 through 3 and 7
+.ie n .IP """3-""" 4
+.el .IP "``3-''" 4
+.IX Item "-3-"
+Trace only on CPUs 3 up to maximum numbers of CPUs the host has.
+.RE
+.Sp
+
+If using \fIall\fP it will use all of the CPUs the host has.
 .TP
 .B -e, --evt-mask=e
 set event capture mask. If not specified the TRC_ALL will be used.
tools/xentrace/xentrace.c +163 -33

248 lines not on this page · raw message

Signature · 1 line
2.1.0
#6 lore raw

Re: [PATCH v4 3/3] xentrace: Implement cpu mask range parsing of human values (-c).

Quoted 20 lines · Konrad Rzeszutek Wilk

Instead of just using -c 0x<some hex value> we can also use: -c <starting cpu>-<end cpu>, -c <cpu1>,<cpu2>, or a combination of them, or 'all' for all cpus.

This new format can include just singular CPUs: -c <cpu1>, or ranges without an start or end (and xentrace will figure out the values), such as: -c -<cpu2> (which will include cpu0, cpu1, and cpu2) or -c <cpu2>- (which will include cpu2 and up to MAX_CPUS).

That should make it easier to trace the right CPU if using this along with 'xl vcpu-list'.

The code has been lifted from the Linux kernel, see file lib/bitmap.c, function __bitmap_parselist.

To make the old behavior and the new function work, we check to see if the arguments have '0x' in them. If they do we use the old style parsing (limited to 32 CPUs). If that does not exist we use the new parsing.

Quoted 4 lines

---
[v4: Fix per George's review]
[v5: Redo per George's review]

I'll leave final word on this one to George.

(But, v5? title says v4, I'd say title was correct?)

#7 lore raw

Re: [PATCH v4 3/3] xentrace: Implement cpu mask range parsing of human values (-c).

Quoted 21 lines · Ian Campbell
Quoted 20 lines · Konrad Rzeszutek Wilk

Instead of just using -c 0x<some hex value> we can also use: -c <starting cpu>-<end cpu>, -c <cpu1>,<cpu2>, or a combination of them, or 'all' for all cpus.

This new format can include just singular CPUs: -c <cpu1>, or ranges without an start or end (and xentrace will figure out the values), such as: -c -<cpu2> (which will include cpu0, cpu1, and cpu2) or -c <cpu2>- (which will include cpu2 and up to MAX_CPUS).

That should make it easier to trace the right CPU if using this along with 'xl vcpu-list'.

The code has been lifted from the Linux kernel, see file lib/bitmap.c, function __bitmap_parselist.

To make the old behavior and the new function work, we check to see if the arguments have '0x' in them. If they do we use the old style parsing (limited to 32 CPUs). If that does not exist we use the new parsing.

Quoted 6 lines
Quoted 4 lines

---
[v4: Fix per George's review]
[v5: Redo per George's review]

I'll leave final word on this one to George.

OK.

George, I just reposted this one under v4.1 without any changes (see http://lists.xen.org/archives/html/xen-devel/2015-04/msg02702.html).

Please review this one or the other one.

Quoted 4 lines

(But, v5? title says v4, I'd say title was correct?)

#8 lore raw

Re: [PATCH v4 3/3] xentrace: Implement cpu mask range parsing of human values (-c).

Quoted 27 lines · Konrad Rzeszutek Wilk
Quoted 26 lines · Ian Campbell
Quoted 24 lines · Konrad Rzeszutek Wilk

Instead of just using -c 0x<some hex value> we can also use: -c <starting cpu>-<end cpu>, -c <cpu1>,<cpu2>, or a combination of them, or 'all' for all cpus.

This new format can include just singular CPUs: -c <cpu1>, or ranges without an start or end (and xentrace will figure out the values), such as: -c -<cpu2> (which will include cpu0, cpu1, and cpu2) or -c <cpu2>- (which will include cpu2 and up to MAX_CPUS).

That should make it easier to trace the right CPU if using this along with 'xl vcpu-list'.

The code has been lifted from the Linux kernel, see file lib/bitmap.c, function __bitmap_parselist.

To make the old behavior and the new function work, we check to see if the arguments have '0x' in them. If they do we use the old style parsing (limited to 32 CPUs). If that does not exist we use the new parsing.

---
[v4: Fix per George's review]
[v5: Redo per George's review]

Quoted 8 lines
Quoted 1 line

I'll leave final word on this one to George.

OK.

George, I just reposted this one under v4.1 without any changes (see http://lists.xen.org/archives/html/xen-devel/2015-04/msg02702.html).

Please review this one or the other one.

I had *just* downloaded and started to look at that patch. At least you caught me early. :-)

-George