Skip to contentSkip to conversation

kvmtrace: powerpc support and timestamps for KVM_TRACE

PATCH New [email protected] 2 participants 11 messages Jul 10, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover kvmtrace: powerpc support and timestamps for KVM_TRACE

From: Christian Ehrhardt <[email protected]>

Chaning kvmtrace code to use timestamp and add powerpc support.

This is the revised series including the feedback from the earlier submission this week. I now also added the userspace changes which completes the series.

The series contains nine patches (4 generic / 5 powerpc code):

Subject: [PATCH 1/9] kvmtrace: Remove use of bit fields in kvm trace structure v3
Introducing a endian save way to store the flags event,tcs, num_data
Subject: [PATCH 2/9] kvmtrace: replace get_cycles with getnstimeofday
use a cross host comparable timestamp source and make it 32/64 bit save
Subject: [PATCH 3/9] kvmtrace: rename cycles to timestamp
it is no more tracking cycles, so rename variables and change comments
Subject: [PATCH 3/9] kvmppc: kvmtrace: enable KVM_TRACE building for powerpc
enable KVM_TRACE for powerpc in Kconfig/Makefile
Subject: [PATCH 4/9] kvmppc: kvmtrace: adds trace points for ppc tlb activity v2
use KVM_TRACE to track tlb/shadow tlb activity on embedded powerpc
Subject: [PATCH 5/9] kvmppc: kvmtrace: trace powerpc instruction emulation
use KVM_TRACE to track instruction emulation overhead
Subject: [PATCH 7/9] kvm-userspace: kvmtrace_format: add ppc instruction emulation
extension to kvmtrace_format to handle complex event types e.g. ppc instructon emulation
Subject: [PATCH 8/9] kvm-userspace: kvmtrace_format: add statistic section
adding statistic creation/prsentation to the kvmtrace data processing
Subject: [PATCH 9/9] kvm-userspace: kvmtrace: rename cycles to timestamp
it is no more tracking cycles, so rename variables and change comments

---

[diffstat]

 arch/powerpc/kvm/emulate.c   |    2
 b/arch/powerpc/kvm/44x_tlb.c |   15 ++
 b/arch/powerpc/kvm/Kconfig   |   11 +
 b/arch/powerpc/kvm/Makefile  |    6
 b/arch/powerpc/kvm/emulate.c |    4
 b/include/linux/kvm.h        |   17 ++
 b/user/formats               |   16 --
 b/user/kvmtrace_format       |  276 ++++++++++++++++++++++++++++++++++++++++---
 b/virt/kvm/kvm_trace.c       |   19 +-
 include/linux/kvm.h          |   10 +
 user/formats                 |   50 +++----
 user/kvmtrace_format         |  114 +++++++++++++----
 virt/kvm/kvm_trace.c         |   29 ++--
 13 files changed, 461 insertions(+), 108 deletions(-)
#1 lore raw

PATCH 1/9 kvmtrace: Remove use of bit fields in kvm trace structure v3

From: Christian Ehrhardt <[email protected]>
From: Jerone Young <[email protected]>

This patch fixes kvmtrace use on big endian systems. When using bit fields the compiler will lay data out in the wrong order expected when laid down into a file.
This fixes it by using one variable instead of using bit fields.

Updates in v3:
- fixed macro definition bug in v2
- ensured in macro operator order
- fixed whitespace/indent issues
- removed superfluous initialization

---

[diffstat]

 include/linux/kvm.h  |   17 ++++++++++++++---
 virt/kvm/kvm_trace.c |   19 ++++++++++---------
 2 files changed, 24 insertions(+), 12 deletions(-)

[diff]

include/linux/kvm.h +14 -3
@@ -311,9 +311,13 @@
 
 /* This structure represents a single trace buffer record. */
 struct kvm_trace_rec {
-	__u32 event:28;
-	__u32 extra_u32:3;
-	__u32 cycle_in:1;
+	/* variable rec_val
+	 * is split into:
+	 * bits 0 - 27  -> event id
+	 * bits 28 -30  -> number of extra data args of size u32
+	 * bits 31      -> binary indicator for if tsc is in record
+	 */
+	__u32 rec_val;
 	__u32 pid;
 	__u32 vcpu_id;
 	union {
@@ -326,6 +330,13 @@
 		} nocycle;
 	} u;
 } __attribute__((packed));
+
+#define TRACE_REC_EVENT_ID(val) \
+		(0x0fffffff & (val))
+#define TRACE_REC_NUM_DATA_ARGS(val) \
+		(0x70000000 & ((val) << 28))
+#define TRACE_REC_TCS(val) \
+		(0x80000000 & ((val) << 31))
 
 #define KVMIO 0xAE
 
virt/kvm/kvm_trace.c +10 -9
@@ -54,12 +54,13 @@
 	struct kvm_trace *kt = kvm_trace;
 	struct kvm_trace_rec rec;
 	struct kvm_vcpu *vcpu;
-	int    i, extra, size;
+	int    i, size;
+	u32    extra;
 
 	if (unlikely(kt->trace_state != KVM_TRACE_STATE_RUNNING))
 		return;
 
-	rec.event	= va_arg(*args, u32);
+	rec.rec_val	= TRACE_REC_EVENT_ID(va_arg(*args, u32));
 	vcpu		= va_arg(*args, struct kvm_vcpu *);
 	rec.pid		= current->tgid;
 	rec.vcpu_id	= vcpu->vcpu_id;
@@ -67,21 +68,21 @@
 	extra   	= va_arg(*args, u32);
 	WARN_ON(!(extra <= KVM_TRC_EXTRA_MAX));
 	extra 		= min_t(u32, extra, KVM_TRC_EXTRA_MAX);
-	rec.extra_u32   = extra;
 
-	rec.cycle_in 	= p->cycle_in;
-
-	if (rec.cycle_in) {
+	rec.rec_val |= TRACE_REC_TCS(p->cycle_in)
+			| TRACE_REC_NUM_DATA_ARGS(extra);
+	
+	if (p->cycle_in) {
 		rec.u.cycle.cycle_u64 = get_cycles();
 
-		for (i = 0; i < rec.extra_u32; i++)
+		for (i = 0; i < extra; i++)
 			rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
 	} else {
-		for (i = 0; i < rec.extra_u32; i++)
+		for (i = 0; i < extra; i++)
 			rec.u.nocycle.extra_u32[i] = va_arg(*args, u32);
 	}
 
-	size = calc_rec_size(rec.cycle_in, rec.extra_u32 * sizeof(u32));
+	size = calc_rec_size(p->cycle_in, extra * sizeof(u32));
 	relay_write(kt->rchan, &rec, size);
 }
 
#2 lore raw

PATCH 2/9 kvmtrace: replace get_cycles with getnstimeofday

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

The current kvmtrace code uses get_cycles() while the interpretation would be easier using using nanoseconds. Getnstimeofday should give the same accuracy as get_cycles on all architectures but at a better unit (e.g. comparable between two hosts with different frequencies.

The implementation of kvmtrace uses a 64 bit cycle variable while get_cycles only provided a unsigned long which is != 64 bit on 32 bit architectures. Since this patch introduced getnstimeofday we can addtionally merge the tv_sec portion of the struct timespec returned to use the full 64 bit of the trace interface and therefor avoid some wraparounds. This merge is done always if the two values of a struct timespec fit into the u64.

An alternative might be ktime_t instead of u64 and using as Sheng Yang suggested and ktime_get(), I had no time to test&compare that yet. Any comments or requirements from others what to prefer here ?

---

[diffstat]

 kvm_trace.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

[diff]

virt/kvm/kvm_trace.c +8 -1
@@ -56,6 +56,7 @@
 	struct kvm_vcpu *vcpu;
 	int    i, size;
 	u32    extra;
+	struct timespec tv;
 
 	if (unlikely(kt->trace_state != KVM_TRACE_STATE_RUNNING))
 		return;
@@ -73,7 +74,13 @@
 			| TRACE_REC_NUM_DATA_ARGS(extra);
 	
 	if (p->cycle_in) {
-		rec.u.cycle.cycle_u64 = get_cycles();
+		getnstimeofday(&tv);
+		/* compress both struct tv values into u64 on 32 bit archs */
+		if (sizeof(tv) = sizeof(u64))
+			rec.u.cycle.cycle_u64 = 
+				(((u64)tv.tv_sec) << 32) | tv.tv_nsec;
+		else
+			rec.u.cycle.cycle_u64 = tv.tv_nsec;
 
 		for (i = 0; i < extra; i++)
 			rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
#4 lore raw

PATCH 3/9 kvmtrace: rename cycles to timestamp

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

Due to the fact that the cycle counter is now completely replaced by the more generic time based code (getnstimeofday) this patch renames all occurances of the old cycle code to make it clear that this is now a timestamp.

---

[diffstat]

 include/linux/kvm.h  |    6 +++---
 virt/kvm/kvm_trace.c |   20 ++++++++++----------
 2 files changed, 13 insertions(+), 13 deletions(-)

[diff]

include/linux/kvm.h +3 -3
@@ -322,12 +322,12 @@
 	__u32 vcpu_id;
 	union {
 		struct {
-			__u64 cycle_u64;
+			__u64 timestamp_u64;
 			__u32 extra_u32[KVM_TRC_EXTRA_MAX];
-		} cycle;
+		} timestamp;
 		struct {
 			__u32 extra_u32[KVM_TRC_EXTRA_MAX];
-		} nocycle;
+		} notimestamp;
 	} u;
 } __attribute__((packed));
 
virt/kvm/kvm_trace.c +10 -10
@@ -35,16 +35,16 @@
 struct kvm_trace_probe {
 	const char *name;
 	const char *format;
-	u32 cycle_in;
+	u32 timestamp_in;
 	marker_probe_func *probe_func;
 };
 
-static inline int calc_rec_size(int cycle, int extra)
+static inline int calc_rec_size(int timestamp, int extra)
 {
 	int rec_size = KVM_TRC_HEAD_SIZE;
 
 	rec_size += extra;
-	return cycle ? rec_size += KVM_TRC_CYCLE_SIZE : rec_size;
+	return timestamp ? rec_size += KVM_TRC_CYCLE_SIZE : rec_size;
 }
 
 static void kvm_add_trace(void *probe_private, void *call_data,
@@ -70,26 +70,26 @@
 	WARN_ON(!(extra <= KVM_TRC_EXTRA_MAX));
 	extra 		= min_t(u32, extra, KVM_TRC_EXTRA_MAX);
 
-	rec.rec_val |= TRACE_REC_TCS(p->cycle_in)
+	rec.rec_val |= TRACE_REC_TCS(p->timestamp_in)
 			| TRACE_REC_NUM_DATA_ARGS(extra);
 	
-	if (p->cycle_in) {
+	if (p->timestamp_in) {
 		getnstimeofday(&tv);
 		/* compress both struct tv values into u64 on 32 bit archs */
 		if (sizeof(tv) = sizeof(u64))
-			rec.u.cycle.cycle_u64 = 
+			rec.u.timestamp.timestamp_u64 = 
 				(((u64)tv.tv_sec) << 32) | tv.tv_nsec;
 		else
-			rec.u.cycle.cycle_u64 = tv.tv_nsec;
+			rec.u.timestamp.timestamp_u64 = tv.tv_nsec;
 
 		for (i = 0; i < extra; i++)
-			rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
+			rec.u.timestamp.extra_u32[i] = va_arg(*args, u32);
 	} else {
 		for (i = 0; i < extra; i++)
-			rec.u.nocycle.extra_u32[i] = va_arg(*args, u32);
+			rec.u.notimestamp.extra_u32[i] = va_arg(*args, u32);
 	}
 
-	size = calc_rec_size(p->cycle_in, extra * sizeof(u32));
+	size = calc_rec_size(p->timestamp_in, extra * sizeof(u32));
 	relay_write(kt->rchan, &rec, size);
 }
 
#5 lore raw

PATCH 3/9 kvmppc: kvmtrace: enable KVM_TRACE building for powerpc

In reply to [email protected]

From: Jerone Young <[email protected]>

This patch enables KVM_TRACE to build for PowerPC arch. This means just adding sections to Kconfig and Makefile.

---

[diffstat]

 Kconfig  |   11 +++++++++++
 Makefile |    6 ++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

[diff]

arch/powerpc/kvm/Kconfig +11 -0
@@ -37,6 +37,17 @@ config KVM_BOOKE_HOST
 	  Provides host support for KVM on Book E PowerPC processors. Currently
 	  this works on 440 processors only.
 
+config KVM_TRACE
+	bool "KVM trace support"
+	depends on KVM && MARKERS && SYSFS
+	select RELAY
+	select DEBUG_FS
+	default n
+	---help---
+	  This option allows reading a trace of kvm-related events through
+	  relayfs.  Note the ABI is not considered stable and will be
+	  modified in future updates.
+
 source drivers/virtio/Kconfig
 
 endif # VIRTUALIZATION
arch/powerpc/kvm/Makefile +4 -2
@@ -4,9 +4,11 @@
 
 EXTRA_CFLAGS += -Ivirt/kvm -Iarch/powerpc/kvm
 
-common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o)
+common-objs-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o)
 
-kvm-objs := $(common-objs) powerpc.o emulate.o booke_guest.o
+common-objs-$(CONFIG_KVM_TRACE)  += $(addprefix ../../../virt/kvm/, kvm_trace.o)
+
+kvm-objs := $(common-objs-y) powerpc.o emulate.o booke_guest.o
 obj-$(CONFIG_KVM) += kvm.o
 
 AFLAGS_booke_interrupts.o := -I$(obj)
#6 lore raw

PATCH 4/9 kvmppc: kvmtrace: adds trace points for ppc tlb activity v2

In reply to [email protected]

From: Jerone Young <[email protected]>
From: Christian Ehrhardt <[email protected]>

This patch adds trace points to track powerpc TLB activities using the KVM_TRACE infrastructure.
Update to v1:
- fixed wrong indents reported by checkpatch

---

[diffstat]

 arch/powerpc/kvm/44x_tlb.c |   15 ++++++++++++++-
 arch/powerpc/kvm/emulate.c |    4 ++++
 include/linux/kvm.h        |    3 +++
 3 files changed, 21 insertions(+), 1 deletion(-)

[diff]

arch/powerpc/kvm/44x_tlb.c +14 -1
@@ -19,6 +19,7 @@
 
 #include <linux/types.h>
 #include <linux/string.h>
+#include <linux/kvm.h>
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <asm/mmu-44x.h>
@@ -175,6 +176,10 @@
 	stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
 	stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
 	                                            vcpu->arch.msr & MSR_PR);
+
+	KVMTRACE_5D(STLB_WRITE, vcpu, victim,
+			stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
+			handler);
 }
 
 void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, u64 eaddr, u64 asid)
@@ -203,6 +208,9 @@
 
 		kvmppc_44x_shadow_release(vcpu, i);
 		stlbe->word0 = 0;
+		KVMTRACE_5D(STLB_INVAL, vcpu, i,
+				stlbe->tid, stlbe->word0, stlbe->word1,
+				stlbe->word2, handler);
 	}
 	up_write(&current->mm->mmap_sem);
 }
@@ -216,8 +224,13 @@
 	/* XXX Replace loop with fancy data structures. */
 	down_write(&current->mm->mmap_sem);
 	for (i = 0; i <= tlb_44x_hwater; i++) {
+		struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
+
 		kvmppc_44x_shadow_release(vcpu, i);
-		vcpu->arch.shadow_tlb[i].word0 = 0;
+		stlbe->word0 = 0;
+		KVMTRACE_5D(STLB_INVAL, vcpu, i,
+				stlbe->tid, stlbe->word0, stlbe->word1,
+				stlbe->word2, handler);
 	}
 	up_write(&current->mm->mmap_sem);
 }
arch/powerpc/kvm/emulate.c +4 -0
@@ -169,6 +169,10 @@
 		 * are mapped on the fly. */
 		kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags);
 	}
+
+	KVMTRACE_5D(GTLB_WRITE, vcpu, index,
+			tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2,
+			handler);
 
 	return EMULATE_DONE;
 }
include/linux/kvm.h +3 -0
@@ -470,5 +470,8 @@
 #define KVM_TRC_LMSW             (KVM_TRC_HANDLER + 0x13)
 #define KVM_TRC_APIC_ACCESS      (KVM_TRC_HANDLER + 0x14)
 #define KVM_TRC_TDP_FAULT        (KVM_TRC_HANDLER + 0x15)
+#define KVM_TRC_GTLB_WRITE       (KVM_TRC_HANDLER + 0x16)
+#define KVM_TRC_STLB_WRITE       (KVM_TRC_HANDLER + 0x17)
+#define KVM_TRC_STLB_INVAL       (KVM_TRC_HANDLER + 0x18)
 
 #endif
#7 lore raw

PATCH 5/9 kvmppc: kvmtrace: trace powerpc instruction emulation

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

This patch adds a trace point for the instruction emulation on embedded powerpc utilizing the KVM_TRACE interface.

---

[diffstat]

 arch/powerpc/kvm/emulate.c |    2 ++
 include/linux/kvm.h        |    1 +
 2 files changed, 3 insertions(+)

[diff]

arch/powerpc/kvm/emulate.c +2 -0
@@ -769,6 +769,8 @@
 		break;
 	}
 
+	KVMTRACE_3D(PPC_INSTR, vcpu, inst, vcpu->arch.pc, emulated, entryexit);
+
 	if (advance)
 		vcpu->arch.pc += 4; /* Advance past emulated instruction. */
 
include/linux/kvm.h +1 -0
@@ -473,5 +473,6 @@
 #define KVM_TRC_GTLB_WRITE       (KVM_TRC_HANDLER + 0x16)
 #define KVM_TRC_STLB_WRITE       (KVM_TRC_HANDLER + 0x17)
 #define KVM_TRC_STLB_INVAL       (KVM_TRC_HANDLER + 0x18)
+#define KVM_TRC_PPC_INSTR        (KVM_TRC_HANDLER + 0x19)
 
 #endif
#8 lore raw

PATCH 7/9 kvm-userspace: kvmtrace_format: add ppc instruction emulation

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

This patch adds the handling of the ppc instruction emulation trace records. Due to the fact that those are more complex than the classic "formats" file way this patch adds a check of the event id and maps to the internal handler function if needed (other complex trace records that might appear in future can hook up there too).
Additionally this fixes the ppc tlb trace record definitions in the formats file now that the revised kernel patch series is submitted.

---

[diffstat]

 formats         |   16 +--
 kvmtrace_format |  276 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 268 insertions(+), 24 deletions(-)

[diff]

user/formats +6 -10
@@ -23,13 +23,9 @@
 0x00020013  %(tsc)d (+%(reltsc)8d)  LMSW          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ value = 0x%(1)08x ]
 0x00020014  %(tsc)d (+%(reltsc)8d)  APIC_ACCESS   vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ offset = 0x%(1)08x ]
 0x00020015  %(tsc)d (+%(reltsc)8d)  TDP_FAULT     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ errorcode = 0x%(1)08x, virt = 0x%(3)08x %(2)08x ]
-# ppc: context switch
-0x00020016  %(tsc)d (+%(reltsc)8d)  CONT_SWITCH   vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
-# ppc: tlb write
-0x00020017  %(tsc)d (+%(reltsc)8d)  TLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
-# ppc: tlb invalidate
-0x00020018  %(tsc)d (+%(reltsc)8d)  TLB_INVAL     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
-# ppc: guest TLB write
-0x00020019  %(tsc)d (+%(reltsc)8d)  GTLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
-# ppc: shadow TLB write
-0x00020020  %(tsc)d (+%(reltsc)8d)  STLB_WRITE   vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
+# ppc: tlb traces
+0x00020016  GTLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
+0x00020017  STLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
+0x00020018  STLB_INVAL    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
+# ppc: instruction emulation - this type is handled more complex in kvmtrace_format, but listed to show the eventid and transported data
+#0x00020019  %(tsc)d (+%(reltsc)8d)  PPC_INSTR     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ instr = 0x%(1)08x, pc = 0x%(2)08x, emul = 0x%(3)08x, nsec = %(4)08d ]
user/kvmtrace_format +262 -14

288 lines not on this page · raw message

#9 lore raw

PATCH 8/9 kvm-userspace: kvmtrace_format: add statistic section

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

Usually people don't want to read thousands of trace log lines to interpret the data, a condensed statistic about the traced events is usually better to read.
This patch adds a new command line switch -s that tells kvmtrace_format to generate statistics while processing the trace records. Those statistics are then printed at the end of the output.
This patch contains a statistic function for the ppc instruction emulation. An example output might look like that:

mnemonic + count
---------------+-----------

        wrteei |       1260
         mfmsr |        977
         mtspr |        895
         wrtee |        742
         mfspr |        534
           rfi |        179
         mtmsr |         90
           lbz |         53
           stb |         28
           sum =       4758

[...] more detailed statistics about spr, dcr and tlb usage

---

[diffstat]

 kvmtrace_format |   73 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 67 insertions(+), 6 deletions(-)

[diff]

user/kvmtrace_format +67 -6
@@ -4,7 +4,7 @@
 
 # Program for reformatting trace buffer output according to user-supplied rules
 
-import re, sys, string, signal, struct, os, getopt
+import re, sys, string, signal, struct, os, getopt, operator
 
 def usage():
     print >> sys.stderr, \
@@ -29,6 +29,12 @@
           this script may not be able to keep up with the output of kvmtrace
           if it is piped directly.  In these circumstances you should have
           kvmtrace output to a file for processing off-line.
+
+          kvmtrace_format has the following additional switches
+          -c mhz - specifies the mhz of the traced machine used to convert
+                   cycle data in trace records into time
+          -s     - if this switch is set additional trace statistics are
+                   created and printed at the end of the output
           """
     sys.exit(1)
 
@@ -60,6 +66,33 @@
     interrupted = 1
 
 # ppc instruction decoding for event type 0x00020019 (PPC_INSTR)
+# some globals for statistic summaries
+stat_ppc_instr_mnemonic = {};
+stat_ppc_instr_spr = {};
+stat_ppc_instr_dcr = {};
+stat_ppc_instr_tlb = {};
+
+def ppc_instr_print_summary(sortedlist, colname):
+	print "\n\n%14s + %10s" % (colname, "count")
+	print "%s" % (15*"-"+"+"+11*"-")
+	sum = 0
+	for value, key in sortedlist:
+		sum += key
+		print "%14s | %10d" % (value, key)
+	print "%14s = %10d" % ("sum", sum)
+
+
+def ppc_instr_summary():
+	# don't print empty statistics
+        if stat_ppc_instr_mnemonic:
+		ppc_instr_print_summary(sorted(stat_ppc_instr_mnemonic.iteritems(), key=operator.itemgetter(1), reverse=True), "mnemonic")
+        if stat_ppc_instr_spr:
+		ppc_instr_print_summary(sorted(stat_ppc_instr_spr.iteritems(), key=operator.itemgetter(1), reverse=True), "mnemonic-spr")
+        if stat_ppc_instr_dcr:
+		ppc_instr_print_summary(sorted(stat_ppc_instr_dcr.iteritems(), key=operator.itemgetter(1), reverse=True), "mnemonic-dcr")
+        if stat_ppc_instr_tlb:
+		ppc_instr_print_summary(sorted(stat_ppc_instr_tlb.iteritems(), key=operator.itemgetter(1), reverse=True), "mnemonic-tlb")
+
 def get_op(instr):
         return (instr >> 26);
 
@@ -293,28 +326,53 @@
 			return "UNKNOWN"
 
 def get_special(instr):
+	name = get_name(instr);
+	if stat_ppc_instr_mnemonic.has_key(name):
+		stat_ppc_instr_mnemonic[name] += 1
+	else:
+		stat_ppc_instr_mnemonic[name] = 1
+
 	if get_op(instr) = 31:
 		if (get_xop(instr) = 339) or (get_xop(instr) = 467):
 			sprn = get_sprn(instr);
-			return ("- sprn 0x%03x %8s" % (sprn, get_sprn_name(sprn)))
+			sprn_name = get_sprn_name(sprn);
+			stat_idx = name+"-"+sprn_name
+			if stat_ppc_instr_spr.has_key(stat_idx):
+				stat_ppc_instr_spr[stat_idx] += 1
+			else:
+				stat_ppc_instr_spr[stat_idx] = 1
+			return ("- sprn 0x%03x %8s" % (sprn, sprn_name))
 		elif (get_xop(instr) = 323 ) or (get_xop(instr) = 451):
-			return ("- dcrn 0x%03x" % get_dcrn(instr))
+			dcrn = get_dcrn(instr);
+			stat_idx = name+"-"+("%04X"%dcrn)
+			if stat_ppc_instr_dcr.has_key(stat_idx):
+				stat_ppc_instr_dcr[stat_idx] += 1
+			else:
+				stat_ppc_instr_dcr[stat_idx] = 1
+			return ("- dcrn 0x%03x" % dcrn)
 		elif (get_xop(instr) = 978 ) or (get_xop(instr) = 451):
-			return ("- ws -> %8s" % get_tlbwe_type(instr))
+			tlbwe_type = get_tlbwe_type(instr)
+			stat_idx = name+"-"+tlbwe_type
+			if stat_ppc_instr_tlb.has_key(stat_idx):
+				stat_ppc_instr_tlb[stat_idx] += 1
+			else:
+				stat_ppc_instr_tlb[stat_idx] = 1
+			return ("- ws -> %8s" % tlbwe_type)
 	return ""
 
 ##### Main code
 
 mhz = 0
+summary = False
 
 if len(sys.argv) < 2:
     usage()
 
 try:
-    opts, arg = getopt.getopt(sys.argv[1:], "c:" )
-
+    opts, arg = getopt.getopt(sys.argv[1:], "sc:" )
     for opt in opts:
         if opt[0] = '-c' : mhz = int(opt[1])
+        if opt[0] = '-s' : summary = True
 
 except getopt.GetoptError:
     usage()
@@ -471,3 +529,6 @@
                         print args
 
     except IOError, struct.error: sys.exit()
+
+if summary:
+	ppc_instr_summary()
#10 lore raw

PATCH 9/9 kvm-userspace: kvmtrace: rename cycles to timestamp

In reply to [email protected]

From: Christian Ehrhardt <[email protected]>

Due to the fact that the cycle counter is now completely replaced by the more generic time based code (getnstimeofday) this patch renames all occurances of the old cycle code to make it clear that this is now a timestamp. Due to some values now growing a big larger (relts) the patch also increases the print width of those values.

---

[diffstat]

 formats         |   50 +++++++++++++++++++++++++-------------------------
 kvmtrace_format |   41 +++++++++++++++++------------------------
 2 files changed, 42 insertions(+), 49 deletions(-)

[diff]

user/formats +25 -25
@@ -1,31 +1,31 @@
-0x00000000  %(tsc)d (+%(reltsc)8d)  unknown (0x%(event)016x) vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ 0x%(1)08x 0x%(2)08x 0x%(3)08x 0x%(4)08x 0x%(5)08x ]
+0x00000000  %(ts)d (+%(relts)12d)  unknown (0x%(event)016x) vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ 0x%(1)08x 0x%(2)08x 0x%(3)08x 0x%(4)08x 0x%(5)08x ]
 
-0x00010001  %(tsc)d (+%(reltsc)8d)  VMENTRY       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
-0x00010002  %(tsc)d (+%(reltsc)8d)  VMEXIT        vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ exitcode = 0x%(1)08x, rip = 0x%(3)08x %(2)08x ]
-0x00020001  %(tsc)d (+%(reltsc)8d)  PAGE_FAULT    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ errorcode = 0x%(1)08x, virt = 0x%(3)08x %(2)08x ]
-0x00020002  %(tsc)d (+%(reltsc)8d)  INJ_VIRQ      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
-0x00020003  %(tsc)d (+%(reltsc)8d)  REDELIVER_EVT vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
-0x00020004  %(tsc)d (+%(reltsc)8d)  PEND_INTR     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
-0x00020005  %(tsc)d (+%(reltsc)8d)  IO_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ port = 0x%(1)04x, size = %(2)d ]
-0x00020006  %(tsc)d (+%(reltsc)8d)  IO_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ port = 0x%(1)04x, size = %(2)d ]
-0x00020007  %(tsc)d (+%(reltsc)8d)  CR_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ CR# = %(1)d, value = 0x%(3)08x %(2)08x ]
-0x00020008  %(tsc)d (+%(reltsc)8d)  CR_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ CR# = %(1)d, value = 0x%(3)08x %(2)08x ]
-0x00020009  %(tsc)d (+%(reltsc)8d)  DR_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ DR# = %(1)d, value = 0x%(2)08x ]
-0x0002000A  %(tsc)d (+%(reltsc)8d)  DR_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ DR# = %(1)d, value = 0x%(2)08x ]
-0x0002000B  %(tsc)d (+%(reltsc)8d)  MSR_READ      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ MSR# = 0x%(1)08x, data = 0x%(3)08x %(2)08x ]
-0x0002000C  %(tsc)d (+%(reltsc)8d)  MSR_WRITE     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ MSR# = 0x%(1)08x, data = 0x%(3)08x %(2)08x ]
-0x0002000D  %(tsc)d (+%(reltsc)8d)  CPUID         vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ func = 0x%(1)08x, eax = 0x%(2)08x, ebx = 0x%(3)08x, ecx = 0x%(4)08x edx = 0x%(5)08x]
-0x0002000E  %(tsc)d (+%(reltsc)8d)  INTR          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
-0x0002000F  %(tsc)d (+%(reltsc)8d)  NMI           vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
-0x00020010  %(tsc)d (+%(reltsc)8d)  VMMCALL       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ func = 0x%(1)08x ]
-0x00020011  %(tsc)d (+%(reltsc)8d)  HLT           vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
-0x00020012  %(tsc)d (+%(reltsc)8d)  CLTS          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
-0x00020013  %(tsc)d (+%(reltsc)8d)  LMSW          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ value = 0x%(1)08x ]
-0x00020014  %(tsc)d (+%(reltsc)8d)  APIC_ACCESS   vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ offset = 0x%(1)08x ]
-0x00020015  %(tsc)d (+%(reltsc)8d)  TDP_FAULT     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ errorcode = 0x%(1)08x, virt = 0x%(3)08x %(2)08x ]
+0x00010001  %(ts)d (+%(relts)12d)  VMENTRY       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
+0x00010002  %(ts)d (+%(relts)12d)  VMEXIT        vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ exitcode = 0x%(1)08x, rip = 0x%(3)08x %(2)08x ]
+0x00020001  %(ts)d (+%(relts)12d)  PAGE_FAULT    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ errorcode = 0x%(1)08x, virt = 0x%(3)08x %(2)08x ]
+0x00020002  %(ts)d (+%(relts)12d)  INJ_VIRQ      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
+0x00020003  %(ts)d (+%(relts)12d)  REDELIVER_EVT vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
+0x00020004  %(ts)d (+%(relts)12d)  PEND_INTR     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
+0x00020005  %(ts)d (+%(relts)12d)  IO_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ port = 0x%(1)04x, size = %(2)d ]
+0x00020006  %(ts)d (+%(relts)12d)  IO_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ port = 0x%(1)04x, size = %(2)d ]
+0x00020007  %(ts)d (+%(relts)12d)  CR_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ CR# = %(1)d, value = 0x%(3)08x %(2)08x ]
+0x00020008  %(ts)d (+%(relts)12d)  CR_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ CR# = %(1)d, value = 0x%(3)08x %(2)08x ]
+0x00020009  %(ts)d (+%(relts)12d)  DR_READ       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ DR# = %(1)d, value = 0x%(2)08x ]
+0x0002000A  %(ts)d (+%(relts)12d)  DR_WRITE      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ DR# = %(1)d, value = 0x%(2)08x ]
+0x0002000B  %(ts)d (+%(relts)12d)  MSR_READ      vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ MSR# = 0x%(1)08x, data = 0x%(3)08x %(2)08x ]
+0x0002000C  %(ts)d (+%(relts)12d)  MSR_WRITE     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ MSR# = 0x%(1)08x, data = 0x%(3)08x %(2)08x ]
+0x0002000D  %(ts)d (+%(relts)12d)  CPUID         vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ func = 0x%(1)08x, eax = 0x%(2)08x, ebx = 0x%(3)08x, ecx = 0x%(4)08x edx = 0x%(5)08x]
+0x0002000E  %(ts)d (+%(relts)12d)  INTR          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ vector = 0x%(1)02x ]
+0x0002000F  %(ts)d (+%(relts)12d)  NMI           vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
+0x00020010  %(ts)d (+%(relts)12d)  VMMCALL       vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ func = 0x%(1)08x ]
+0x00020011  %(ts)d (+%(relts)12d)  HLT           vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
+0x00020012  %(ts)d (+%(relts)12d)  CLTS          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x
+0x00020013  %(ts)d (+%(relts)12d)  LMSW          vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ value = 0x%(1)08x ]
+0x00020014  %(ts)d (+%(relts)12d)  APIC_ACCESS   vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ offset = 0x%(1)08x ]
+0x00020015  %(ts)d (+%(relts)12d)  TDP_FAULT     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ errorcode = 0x%(1)08x, virt = 0x%(3)08x %(2)08x ]
 # ppc: tlb traces
 0x00020016  GTLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
 0x00020017  STLB_WRITE    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
 0x00020018  STLB_INVAL    vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ index = 0x%(1)08x, tid = 0x%(2)08x, word1=0x%(3)08x, word2=0x%(4)08x, word3=0x%(5)08x ]
 # ppc: instruction emulation - this type is handled more complex in kvmtrace_format, but listed to show the eventid and transported data
-#0x00020019  %(tsc)d (+%(reltsc)8d)  PPC_INSTR     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ instr = 0x%(1)08x, pc = 0x%(2)08x, emul = 0x%(3)08x, nsec = %(4)08d ]
+#0x00020019  %(ts)d (+%(relts)12d)  PPC_INSTR     vcpu = 0x%(vcpu)08x  pid = 0x%(pid)08x [ instr = 0x%(1)08x, pc = 0x%(2)08x, emul = 0x%(3)08x, nsec = %(4)08d ]
user/kvmtrace_format +17 -24

112 lines not on this page · raw message

#3 lore raw

Re: [PATCH 2/9] kvmtrace: replace get_cycles with getnstimeofday

In reply to [email protected]
Quoted 42 lines · [email protected] wrote:

From: Christian Ehrhardt <[email protected]>

The current kvmtrace code uses get_cycles() while the interpretation would be easier using using nanoseconds. Getnstimeofday should give the same accuracy as get_cycles on all architectures but at a better unit (e.g. comparable between two hosts with different frequencies.

The implementation of kvmtrace uses a 64 bit cycle variable while get_cycles only provided a unsigned long which is != 64 bit on 32 bit architectures. Since this patch introduced getnstimeofday we can addtionally merge the tv_sec portion of the struct timespec returned to use the full 64 bit of the trace interface and therefor avoid some wraparounds. This merge is done always if the two values of a struct timespec fit into the u64.

An alternative might be ktime_t instead of u64 and using as Sheng Yang suggested and ktime_get(), I had no time to test&compare that yet. Any comments or requirements from others what to prefer here ?

---

[diffstat]

 kvm_trace.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

[diff]

virt/kvm/kvm_trace.c +1 -0
@@ -56,6 +56,7 @@
 	struct kvm_vcpu *vcpu;
 	int    i, size;
 	u32    extra;
+	struct timespec tv;
 
 	if (unlikely(kt->trace_state != KVM_TRACE_STATE_RUNNING))
 		return;
@@ -73,7 +74,13 @@
 			| TRACE_REC_NUM_DATA_ARGS(extra);
 
 	if (p->cycle_in) {

virt/kvm/kvm_trace.c line 85

Quoted 10 lines
virt/kvm/kvm_trace.c +7 -1
-		rec.u.cycle.cycle_u64 = get_cycles();
+		getnstimeofday(&tv);
+		/* compress both struct tv values into u64 on 32 bit archs */
+		if (sizeof(tv) = sizeof(u64))
+			rec.u.cycle.cycle_u64 = 
+				(((u64)tv.tv_sec) << 32) | tv.tv_nsec;
+		else
+			rec.u.cycle.cycle_u64 = tv.tv_nsec;
 (*args, u32);
 

Surely, different results on 32-bit and 64-bit aren't what's wanted. I guess ktime_get() is better.

Signature · 1 line
error compiling committee.c: too many arguments to function