perf tool: Add method for creating perf_clock to tod reference

Uses posix clock to get a perf_clock timestamp. Requires either kernel
side support or module to be loaded. CLOCK_PERF set to 14 for future
compatibility. Current max CLOCK value is 11 (CLOCK_TAI) out of a max
range of 0-15. CLOCK_TRACE has 15, so use 14 for CLOCK_PERF.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf record: Add time-of-day option

Invoked the perf-session method to get a perf_clock to time-of-day
reference.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf script: add time-of-day option for displaying events

Leverages reference time stored in session header to convert perf_clock
timestamps to time-of-day. Default format is %H:%M:%S and the format can
be changed via the --tod argument to any string recognized by strftime.

This version does *not* use any tracepoints to keep the correlation
between perf_clock and time-of-day up to date. Time discrepancies can
happen if ntpd or a user changes the wallclock.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf sched timehist: Use time-of-day for timestamps if possible

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Add option to show time-of-day in output

Signed-off-by: David Ahern <dsahern@gmail.com>
Add README.ahern with explanation of time-of-day and module code

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Add support for syscalls vs raw_syscalls

Older kernels (e.g., RHEL6) do system call tracing with
syscalls:sys_{enter,exit} rather than raw_syscalls. Update perf-trace to
detect lack of raw_syscalls support and try syscalls.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Fix crash on RHEL6

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Fix summary percentage when processing files

Getting a divide by 0 when events are processed from a file:
   perf trace -i perf.data -s
   ...
   dnsmasq (1684), 10 events, inf%, 0.000 msec

The problem is that the event count is not incremented as events are
processed. With this patch:
   perf trace -i perf.data -s
   ...
   dnsmasq (1684), 10 events, 8.9%, 0.000 msec

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Handle legacy syscalls

Currently the code skips the first field with the expectation that it is
'nr'. But older kernels do not have the 'nr' field:

    field:int nr;   offset:8;   size:4; signed:1;

Change perf-trace to drop the field if it exists after parsing the format
file.

This fixes the off-by-one problem with older kernels (e.g., RHEL6). e.g,
perf-trace shows this for write:

    3.684 ( 0.007 ms): write(buf: 2, count: 140737077958816  ) = 27

where 2 is really the fd, the huge number is really the buf address, etc.
With this patch you get the more appropriate:

   3.016 ( 0.008 ms): write(fd: 2, buf: 0x7fff9a40f780, count: 26) = 26

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Improve print of read and write syscalls

Second argument is an address -- either buffer or iov. Print as hex.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Improve print of open syscalls

First argument is a buffer with a pathname. Print address as hex.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf tool: Add method to return if DSO is 64-bit

Signed-off-by: David Ahern <dsahern@gmail.com>
perf tool: Add functions for setting and retrieving audit machine

Needed for properly setting machine type based on thread (e.g., 32-bit
versus 64-bit task).

Signed-off-by: David Ahern <dsahern@gmail.com>
perf tool: Add audit machine hooks to threads

Bitness (32-bit or 64-bit) is a process property which translates to
thread in perf. Type is needed for converting system calls from nr
to name.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Refactoring to support multiple machine types

perf trace is currently hardcoded to resolve all syscalls based on the
machine type determined by audit_detect_machine (which is based on uname
lookup). However, this fails to properly handle mixed environments --
namely, 32-bit apps on a 64-bit kernel.

Refactor the code to allow multiple machine types to be used. Prep for
the next patch.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Use new audit hooks to set machine type

Machine type is a per-thread attribute, so use the new audit hooks to
lookup machine type to use for id to syscall translations.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf trace: Allow user to specify machine type

Allow user to specify machine type for converting syscall ids to names.
perf-trace should autodetect the machine type (e.g., i386, x86_64, etc),
but this option allows a user to force it.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf tool: Move callchain option parsing to library code

Code used to parse callchain options is common to perf-record and perf-top
and will be used by perf-trace in an upcoming patch. Since it is common code
it should be in a library / util file rather than the record command.

Code move only; no functional changes. The code in callchain.c is indentical
to what was in builtin-record.c with two exceptions: strings split across
lines was moved to a common line to make checkpatch happy.

Signed-off-by: David Ahern <dsahern@gmail.com>
perf tools: Fix bug for perf kvm report without guestmount.

Currently, if we use perf kvm --guestkallsyms --guestmodules report,
we can not get the perf information from perf data file. The all sample
are shown as unknown.

Reproducing steps:
	# perf kvm --guestkallsyms /tmp/kallsyms --guestmodules /tmp/modules record -a sleep 1
	[ perf record: Woken up 1 times to write data ]
	[ perf record: Captured and wrote 0.624 MB perf.data.guest (~27260 samples) ]
	# perf kvm --guestkallsyms /tmp/kallsyms --guestmodules /tmp/modules report |grep %
	   100.00%  [guest/6471]  [unknown]         [g] 0xffffffff8164f330

This bug was introduced by 207b57926 (perf kvm: Fix regression with guest machine creation).
In original code, it uses perf_session__find_machine(), it means we deliver symbol to machine
which has the same pid, if no machine found, deliver it to *default* guest. But if we use
perf_session__findnew_machine() here, if no machine was found, new machine with pid will be built
and added. Then the default guest which with pid == 0 will never get a symbol.

And because the new machine initialized here has no kernel map created, the symbol delivered to
it will be marked as "unknown".

This patch here is to revert commit 207b57926 and fix the SEGFAULT bug in another way.

Verification steps:
	# ./perf kvm --guestkallsyms /home/kallsyms --guestmodules /home/modules record -a sleep 1
	[ perf record: Woken up 1 times to write data ]
	[ perf record: Captured and wrote 0.651 MB perf.data.guest (~28437 samples) ]
	# ./perf kvm --guestkallsyms /home/kallsyms --guestmodules /home/modules report |grep %
	    22.64%    :6471  [guest.kernel.kallsyms]  [g] update_rq_clock.part.70
	    19.99%    :6471  [guest.kernel.kallsyms]  [g] d_free
	    18.46%    :6471  [guest.kernel.kallsyms]  [g] bio_phys_segments
	    16.25%    :6471  [guest.kernel.kallsyms]  [g] dequeue_task
	    12.78%    :6471  [guest.kernel.kallsyms]  [g] __switch_to
	     7.91%    :6471  [guest.kernel.kallsyms]  [g] scheduler_tick
	     1.75%    :6471  [guest.kernel.kallsyms]  [g] native_apic_mem_write
	     0.21%    :6471  [guest.kernel.kallsyms]  [g] apic_timer_interrupt

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
Acked-by: David Ahern <dsahern@gmail.com>