Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/stefanha/tags/tracing-pull-requ…
Browse files Browse the repository at this point in the history
…est' into staging

# gpg: Signature made Mon 17 Jul 2017 13:11:17 BST
# gpg:                using RSA key 0x9CA4ABB381AB73C8
# gpg: Good signature from "Stefan Hajnoczi <[email protected]>"
# gpg:                 aka "Stefan Hajnoczi <[email protected]>"
# Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35  775A 9CA4 ABB3 81AB 73C8

* remotes/stefanha/tags/tracing-pull-request:
  trace: update old trace events in docs
  trace: [trivial] Statically enable all guest events
  trace: [tcg, trivial] Re-align generated code
  trace: [tcg] Do not generate TCG code to trace dynamically-disabled events
  exec: [tcg] Use different TBs according to the vCPU's dynamic tracing state
  trace: [tcg] Delay changes to dynamic state when translating
  trace: Allocate cpu->trace_dstate in place

Signed-off-by: Peter Maydell <[email protected]>
  • Loading branch information
pm215 committed Jul 17, 2017
2 parents ca4e667 + 304187c commit 5a477a7
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 74 deletions.
8 changes: 6 additions & 2 deletions accel/tcg/cpu-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ struct tb_desc {
CPUArchState *env;
tb_page_addr_t phys_page1;
uint32_t flags;
uint32_t trace_vcpu_dstate;
};

static bool tb_cmp(const void *p, const void *d)
Expand All @@ -291,6 +292,7 @@ static bool tb_cmp(const void *p, const void *d)
tb->page_addr[0] == desc->phys_page1 &&
tb->cs_base == desc->cs_base &&
tb->flags == desc->flags &&
tb->trace_vcpu_dstate == desc->trace_vcpu_dstate &&
!atomic_read(&tb->invalid)) {
/* check next page if needed */
if (tb->page_addr[1] == -1) {
Expand Down Expand Up @@ -319,10 +321,11 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
desc.env = (CPUArchState *)cpu->env_ptr;
desc.cs_base = cs_base;
desc.flags = flags;
desc.trace_vcpu_dstate = *cpu->trace_dstate;
desc.pc = pc;
phys_pc = get_page_addr_code(desc.env, pc);
desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
h = tb_hash_func(phys_pc, pc, flags);
h = tb_hash_func(phys_pc, pc, flags, *cpu->trace_dstate);
return qht_lookup(&tcg_ctx.tb_ctx.htable, tb_cmp, &desc, h);
}

Expand All @@ -342,7 +345,8 @@ static inline TranslationBlock *tb_find(CPUState *cpu,
cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
tb = atomic_rcu_read(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]);
if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
tb->flags != flags)) {
tb->flags != flags ||
tb->trace_vcpu_dstate != *cpu->trace_dstate)) {
tb = tb_htable_lookup(cpu, pc, cs_base, flags);
if (!tb) {

Expand Down
11 changes: 9 additions & 2 deletions accel/tcg/translate-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "exec/tb-hash.h"
#include "translate-all.h"
#include "qemu/bitmap.h"
#include "qemu/error-report.h"
#include "qemu/timer.h"
#include "qemu/main-loop.h"
#include "exec/log.h"
Expand Down Expand Up @@ -112,6 +113,11 @@ typedef struct PageDesc {
#define V_L2_BITS 10
#define V_L2_SIZE (1 << V_L2_BITS)

/* Make sure all possible CPU event bits fit in tb->trace_vcpu_dstate */
QEMU_BUILD_BUG_ON(CPU_TRACE_DSTATE_MAX_EVENTS >
sizeof(((TranslationBlock *)0)->trace_vcpu_dstate)
* BITS_PER_BYTE);

/*
* L1 Mapping properties
*/
Expand Down Expand Up @@ -1071,7 +1077,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)

/* remove the TB from the hash list */
phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
h = tb_hash_func(phys_pc, tb->pc, tb->flags);
h = tb_hash_func(phys_pc, tb->pc, tb->flags, tb->trace_vcpu_dstate);
qht_remove(&tcg_ctx.tb_ctx.htable, tb, h);

/* remove the TB from the page list */
Expand Down Expand Up @@ -1216,7 +1222,7 @@ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
}

/* add in the hash table */
h = tb_hash_func(phys_pc, tb->pc, tb->flags);
h = tb_hash_func(phys_pc, tb->pc, tb->flags, tb->trace_vcpu_dstate);
qht_insert(&tcg_ctx.tb_ctx.htable, tb, h);

#ifdef DEBUG_TB_CHECK
Expand Down Expand Up @@ -1262,6 +1268,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
tb->cs_base = cs_base;
tb->flags = flags;
tb->cflags = cflags;
tb->trace_vcpu_dstate = *cpu->trace_dstate;
tb->invalid = false;

#ifdef CONFIG_PROFILER
Expand Down
3 changes: 1 addition & 2 deletions docs/devel/tracing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ for debugging, profiling, and observing execution.

2. Create a file with the events you want to trace:

echo bdrv_aio_readv > /tmp/events
echo bdrv_aio_writev >> /tmp/events
echo memory_region_ops_read >/tmp/events

3. Run the virtual machine to produce a trace file:

Expand Down
3 changes: 3 additions & 0 deletions include/exec/exec-all.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ struct TranslationBlock {
#define CF_USE_ICOUNT 0x20000
#define CF_IGNORE_ICOUNT 0x40000 /* Do not generate icount code */

/* Per-vCPU dynamic tracing state used to generate this TB */
uint32_t trace_vcpu_dstate;

uint16_t invalid;

void *tc_ptr; /* pointer to the translated code */
Expand Down
7 changes: 5 additions & 2 deletions include/exec/tb-hash-xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* contiguous in memory.
*/
static inline
uint32_t tb_hash_func5(uint64_t a0, uint64_t b0, uint32_t e)
uint32_t tb_hash_func6(uint64_t a0, uint64_t b0, uint32_t e, uint32_t f)
{
uint32_t v1 = TB_HASH_XX_SEED + PRIME32_1 + PRIME32_2;
uint32_t v2 = TB_HASH_XX_SEED + PRIME32_2;
Expand Down Expand Up @@ -78,11 +78,14 @@ uint32_t tb_hash_func5(uint64_t a0, uint64_t b0, uint32_t e)
v4 *= PRIME32_1;

h32 = rol32(v1, 1) + rol32(v2, 7) + rol32(v3, 12) + rol32(v4, 18);
h32 += 20;
h32 += 24;

h32 += e * PRIME32_3;
h32 = rol32(h32, 17) * PRIME32_4;

h32 += f * PRIME32_3;
h32 = rol32(h32, 17) * PRIME32_4;

h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
Expand Down
5 changes: 3 additions & 2 deletions include/exec/tb-hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
#endif /* CONFIG_SOFTMMU */

static inline
uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulong pc, uint32_t flags)
uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulong pc, uint32_t flags,
uint32_t trace_vcpu_dstate)
{
return tb_hash_func5(phys_pc, pc, flags);
return tb_hash_func6(phys_pc, pc, flags, trace_vcpu_dstate);
}

#endif
12 changes: 6 additions & 6 deletions include/qom/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
struct qemu_work_item;

#define CPU_UNSET_NUMA_NODE_ID -1
#define CPU_TRACE_DSTATE_MAX_EVENTS 32

/**
* CPUState:
Expand Down Expand Up @@ -301,6 +302,8 @@ struct qemu_work_item;
* @kvm_fd: vCPU file descriptor for KVM.
* @work_mutex: Lock to prevent multiple access to queued_work_*.
* @queued_work_first: First asynchronous work pending.
* @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
* to @trace_dstate).
* @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
*
* State of one CPU core or thread.
Expand Down Expand Up @@ -370,12 +373,9 @@ struct CPUState {
struct KVMState *kvm_state;
struct kvm_run *kvm_run;

/*
* Used for events with 'vcpu' and *without* the 'disabled' properties.
* Dynamically allocated based on bitmap requried to hold up to
* trace_get_vcpu_event_count() entries.
*/
unsigned long *trace_dstate;
/* Used for events with 'vcpu' and *without* the 'disabled' properties */
DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);

/* TODO Move common fields from CPUArchState here. */
int cpu_index; /* used by alpha TCG */
Expand Down
8 changes: 0 additions & 8 deletions qom/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ static void cpu_common_unrealizefn(DeviceState *dev, Error **errp)

static void cpu_common_initfn(Object *obj)
{
uint32_t count;
CPUState *cpu = CPU(obj);
CPUClass *cc = CPU_GET_CLASS(obj);

Expand All @@ -395,18 +394,11 @@ static void cpu_common_initfn(Object *obj)
QTAILQ_INIT(&cpu->breakpoints);
QTAILQ_INIT(&cpu->watchpoints);

count = trace_get_vcpu_event_count();
if (count) {
cpu->trace_dstate = bitmap_new(count);
}

cpu_exec_initfn(cpu);
}

static void cpu_common_finalize(Object *obj)
{
CPUState *cpu = CPU(obj);
g_free(cpu->trace_dstate);
}

static int64_t cpu_common_get_arch_id(CPUState *cpu)
Expand Down
3 changes: 2 additions & 1 deletion scripts/tracetool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -268,6 +268,7 @@ def formats(self):
return self._FMT.findall(self.fmt)

QEMU_TRACE = "trace_%(name)s"
QEMU_TRACE_NOCHECK = "_nocheck__" + QEMU_TRACE
QEMU_TRACE_TCG = QEMU_TRACE + "_tcg"
QEMU_DSTATE = "_TRACE_%(NAME)s_DSTATE"
QEMU_EVENT = "_TRACE_%(NAME)s_EVENT"
Expand Down
4 changes: 2 additions & 2 deletions scripts/tracetool/backend/dtrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -46,6 +46,6 @@ def generate_h_begin(events, group):


def generate_h(event, group):
out(' QEMU_%(uppername)s(%(argnames)s);',
out(' QEMU_%(uppername)s(%(argnames)s);',
uppername=event.name.upper(),
argnames=", ".join(event.args.names()))
20 changes: 10 additions & 10 deletions scripts/tracetool/backend/ftrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ def generate_h(event, group):
if len(event.args) > 0:
argnames = ", " + argnames

out(' {',
' char ftrace_buf[MAX_TRACE_STRLEN];',
' int unused __attribute__ ((unused));',
' int trlen;',
' if (trace_event_get_state(%(event_id)s)) {',
' trlen = snprintf(ftrace_buf, MAX_TRACE_STRLEN,',
' "%(name)s " %(fmt)s "\\n" %(argnames)s);',
' trlen = MIN(trlen, MAX_TRACE_STRLEN - 1);',
' unused = write(trace_marker_fd, ftrace_buf, trlen);',
' }',
out(' {',
' char ftrace_buf[MAX_TRACE_STRLEN];',
' int unused __attribute__ ((unused));',
' int trlen;',
' if (trace_event_get_state(%(event_id)s)) {',
' trlen = snprintf(ftrace_buf, MAX_TRACE_STRLEN,',
' "%(name)s " %(fmt)s "\\n" %(argnames)s);',
' trlen = MIN(trlen, MAX_TRACE_STRLEN - 1);',
' unused = write(trace_marker_fd, ftrace_buf, trlen);',
' }',
' }',
name=event.name,
args=event.args,
event_id="TRACE_" + event.name.upper(),
Expand Down
19 changes: 10 additions & 9 deletions scripts/tracetool/backend/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -35,14 +35,15 @@ def generate_h(event, group):
else:
cond = "trace_event_get_state(%s)" % ("TRACE_" + event.name.upper())

out(' if (%(cond)s) {',
' struct timeval _now;',
' gettimeofday(&_now, NULL);',
' qemu_log_mask(LOG_TRACE, "%%d@%%zd.%%06zd:%(name)s " %(fmt)s "\\n",',
' getpid(),',
' (size_t)_now.tv_sec, (size_t)_now.tv_usec',
' %(argnames)s);',
' }',
out(' if (%(cond)s) {',
' struct timeval _now;',
' gettimeofday(&_now, NULL);',
' qemu_log_mask(LOG_TRACE,',
' "%%d@%%zd.%%06zd:%(name)s " %(fmt)s "\\n",',
' getpid(),',
' (size_t)_now.tv_sec, (size_t)_now.tv_usec',
' %(argnames)s);',
' }',
cond=cond,
name=event.name,
fmt=event.fmt.rstrip("\n"),
Expand Down
4 changes: 2 additions & 2 deletions scripts/tracetool/backend/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -37,7 +37,7 @@ def generate_h_begin(events, group):


def generate_h(event, group):
out(' _simple_%(api)s(%(args)s);',
out(' _simple_%(api)s(%(args)s);',
api=event.api(),
args=", ".join(event.args.names()))

Expand Down
6 changes: 3 additions & 3 deletions scripts/tracetool/backend/syslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def generate_h(event, group):
else:
cond = "trace_event_get_state(%s)" % ("TRACE_" + event.name.upper())

out(' if (%(cond)s) {',
' syslog(LOG_INFO, "%(name)s " %(fmt)s %(argnames)s);',
' }',
out(' if (%(cond)s) {',
' syslog(LOG_INFO, "%(name)s " %(fmt)s %(argnames)s);',
' }',
cond=cond,
name=event.name,
fmt=event.fmt.rstrip("\n"),
Expand Down
4 changes: 2 additions & 2 deletions scripts/tracetool/backend/ust.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -35,6 +35,6 @@ def generate_h(event, group):
if len(event.args) > 0:
argnames = ", " + argnames

out(' tracepoint(qemu, %(name)s%(tp_args)s);',
out(' tracepoint(qemu, %(name)s%(tp_args)s);',
name=event.name,
tp_args=argnames)
26 changes: 19 additions & 7 deletions scripts/tracetool/format/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = "Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2016, Lluís Vilanova <[email protected]>"
__copyright__ = "Copyright 2012-2017, Lluís Vilanova <[email protected]>"
__license__ = "GPL version 2 or (at your option) any later version"

__maintainer__ = "Stefan Hajnoczi"
Expand Down Expand Up @@ -49,6 +49,19 @@ def generate(events, backend, group):
backend.generate_begin(events, group)

for e in events:
# tracer without checks
out('',
'static inline void %(api)s(%(args)s)',
'{',
api=e.api(e.QEMU_TRACE_NOCHECK),
args=e.args)

if "disable" not in e.properties:
backend.generate(e, group)

out('}')

# tracer wrapper with checks (per-vCPU tracing)
if "vcpu" in e.properties:
trace_cpu = next(iter(e.args))[1]
cond = "trace_event_get_vcpu_state(%(cpu)s,"\
Expand All @@ -63,16 +76,15 @@ def generate(events, backend, group):
'static inline void %(api)s(%(args)s)',
'{',
' if (%(cond)s) {',
' %(api_nocheck)s(%(names)s);',
' }',
'}',
api=e.api(),
api_nocheck=e.api(e.QEMU_TRACE_NOCHECK),
args=e.args,
names=", ".join(e.args.names()),
cond=cond)

if "disable" not in e.properties:
backend.generate(e, group)

out(' }',
'}')

backend.generate_end(events, group)

out('#endif /* TRACE_%s_GENERATED_TRACERS_H */' % group.upper())
Loading

0 comments on commit 5a477a7

Please sign in to comment.