trace-cmd: Add sqldump
RFC PATCH 1/1 trace-cmd: Add sqldump
From: "Steven Rostedt (VMware)" <[email protected]>
In order to be able to test sql commands on the command line to convert to histogram logic, I wanted a real sql database to play with that contain events. I decided to modify trace-cmd to create a sqldump command that would dump the contents of the trace.dat file into a mysqldump file format.
It looks at all the events in the trace.dat file that has instances, and then creates a table for each event.
$ trace-cmd sqldump > dump $ mysql events < dump
MariaDB [events]> show tables;
+-----------------------------+ | Tables_in_events | +-----------------------------+ | sched_migrate_task | | sched_move_numa | | sched_process_exec | | sched_process_exit | | sched_stat_runtime | | sched_switch | | sched_wake_idle_without_ipi | | sched_wakeup | | sched_waking | +-----------------------------+
9 rows in set (0.001 sec)
MariaDB [events]> select * from sched_move_numa;
+-------------------+-------------+--------------+----------------------+------------+-------+-------+-------+---------+---------+---------+---------+ | common_timestamp | common_type | common_flags | common_preempt_count | common_pid | pid | tgid | ngid | src_cpu | src_nid | dst_cpu | dst_nid | +-------------------+-------------+--------------+----------------------+------------+-------+-------+-------+---------+---------+---------+---------+ | 14943901721165973 | 305 | 0 | 0 | 27451 | 27451 | 27451 | 27451 | 5 | 1 | 22 | 0 | | 14943901722548756 | 305 | 0 | 0 | 3684 | 3684 | 3684 | 3684 | 23 | 1 | 22 | 0 | | 14943901779987828 | 305 | 0 | 0 | 13693 | 13693 | 13677 | 13693 | 7 | 1 | 22 | 0 | +-------------------+-------------+--------------+----------------------+------------+-------+-------+-------+---------+---------+---------+---------+
3 rows in set (0.001 sec)
I then tried this (which would be what we would convert to a histogram string):
MariaDB [events]> select first.end_time as start_time, next.common_timestamp as end_time, first.pid,
(next.common_timestamp - first.end_time) as delta2,
(next.common_timestamp - first.end_time + first.delta) as total_delta
from (select start.common_timestamp as start_time, end.common_timestamp as end_time, start.pid,
(end.common_timestamp - start.common_timestamp) as delta
from sched_waking as start
join sched_switch as end
on start.pid = end.next_pid and start.common_timestamp < end.common_timestamp limit 20) as first
join sched_switch as next
on first.pid = next.prev_pid and first.end_time < next.common_timestamp limit 20;
+-------------------+-------------------+-------+---------+-------------+ | start_time | end_time | pid | delta2 | total_delta | +-------------------+-------------------+-------+---------+-------------+ | 14943901178093512 | 14943901178103431 | 22 | 9919 | 14926 | | 14943901178148697 | 14943901178198198 | 9164 | 49501 | 71413 | | 14943901178148777 | 14943901178215204 | 8024 | 66427 | 82776 | | 14943901178166361 | 14943901178251623 | 10398 | 85262 | 98307 | | 14943901178337032 | 14943901178348191 | 3550 | 11159 | 17083 | | 14943901178148697 | 14943901178671539 | 9164 | 522842 | 544754 | | 14943901178645272 | 14943901178671539 | 9164 | 26267 | 544754 | | 14943901178645272 | 14943901178671539 | 9164 | 26267 | 41732 | | 14943901178148697 | 14943901179165293 | 9164 | 1016596 | 1038508 | | 14943901178645272 | 14943901179165293 | 9164 | 520021 | 1038508 | | 14943901178645272 | 14943901179165293 | 9164 | 520021 | 535486 | | 14943901179144479 | 14943901179165293 | 9164 | 20814 | 1038508 | | 14943901179144479 | 14943901179165293 | 9164 | 20814 | 535486 | | 14943901179144479 | 14943901179165293 | 9164 | 20814 | 28893 | | 14943901179161559 | 14943901179173582 | 13770 | 12023 | 17585 | | 14943901178337032 | 14943901179440909 | 3550 | 1103877 | 1109801 | | 14943901179429686 | 14943901179440909 | 3550 | 11223 | 1109801 | | 14943901179429686 | 14943901179440909 | 3550 | 11223 | 27031 | | 14943901179464159 | 14943901179528026 | 13366 | 63867 | 108018 | | 14943901179496693 | 14943901179528477 | 13367 | 31784 | 44695 | +-------------------+-------------------+-------+---------+-------------+
20 rows in set (0.037 sec)
Well, the output isn't exactly what we want as it will match all start rows with the same pid and a timestamp less than the end event. But for our purposes it is enough.
What the above syntax does is to match three events:
sched_waking -> sched_switch (pid scheduling in) -> sched_switch (pid scheduling out).
And gives two deltas: One between the first and second event, and the total time.
The trace-cmd start --sql '...' of the above should create the following:
# echo 'table u64 start_time u64 end_time pid_t pid u64 delta u64 total_delta' > synthetic_events # echo 'first u64 start_time u64 end_time pid_t pid u64 delta' >> synthetic_events # echo 'hist:keys=pid:start=common_timestamp' > events/sched/sched_waking/trigger # echo 'hist:keys=next_pid:start2=$start,delta=common_timestamp-$start:onmatch(sched.sched_waking).trace(first,$start2,common_timestamp,next_pid,$delta)' > events/sched/sched_switch/trigger # echo 'hist:keys=prev_pid:start3=$start2,delta2=$delta,delta3=common_timestamp-$start2:onmatch(synthetic.first).trace(table,$start3,common_timestamp,prev_pid,$delta3,$delta2)' >> events/sched/sched_switch/trigger
Note, the above will eventually work with:
trace-cmd start --sql 'select first.end_time as start_time, next.common_timestamp as end_time, first.pid,
(next.common_timestamp - first.end_time) as delta2,
(next.common_timestamp - first.end_time + first.delta) as total_delta
from (select start.common_timestamp as start_time, end.common_timestamp as end_time, start.pid,
(end.common_timestamp - start.common_timestamp) as delta
from sched_waking as start
join sched_switch as end
on start.pid = end.next_pid) as first
join sched_switch as next
on first.pid = next.prev_pid'
Or broken into two statements (probably start with this):
trace-cmd start \
--sql '(select start.common_timestamp as start_time, end.common_timestamp as end_time, start.pid,
(end.common_timestamp - start.common_timestamp) as delta
from sched_waking as start
join sched_switch as end
on start.pid = end.next_pid) as first' \
\
--sql '(select first.end_time as start_time, next.common_timestamp as end_time, first.pid,
(next.common_timestamp - first.end_time) as delta2,
(next.common_timestamp - first.end_time + first.delta) as total_delta
from first
join sched_switch as next
on first.pid = next.prev_pid) as table'
That is, the above is a very complex one to test the algorithm with. Normally, someone would probably only do the first sql statement:
trace-cmd start \
--sql '(select start.common_timestamp as start_time, end.common_timestamp as end_time, start.pid,
(end.common_timestamp - start.common_timestamp) as delta
from sched_waking as start
join sched_switch as end
on start.pid = end.next_pid) as first'
Which would generate:
# echo 'first u64 start_time u64 end_time pid_t pid u64 delta' >> synthetic_events # echo 'hist:keys=pid:start=common_timestamp' > events/sched/sched_waking/trigger # echo 'hist:keys=next_pid:start2=$start,delta=common_timestamp-$start:onmatch(sched.sched_waking).trace(first,$start2,common_timestamp,next_pid,$delta)' > events/sched/sched_switch/trigger
And although I noticed that we can't use "start_time" and "end_time" later in the statement with the mysql database, I think we may be able to use it for our use case. That is:
trace-cmd start \
--sql '(select start.common_timestamp as start_time, end.common_timestamp as end_time, start.pid,
(start_time - end_time) as delta <<<--- easier to read and write
from sched_waking as start
join sched_switch as end
on start.pid = end.next_pid) as first'
Now for the sqldump patch.
--- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 6 + tracecmd/trace-cmd.c | 1 + tracecmd/trace-read.c | 50 ++++-- tracecmd/trace-sqldump.c | 268 +++++++++++++++++++++++++++++++++ 5 files changed, 316 insertions(+), 10 deletions(-) create mode 100644 tracecmd/trace-sqldump.c
tracecmd/Makefile +1 -0
@@ -31,6 +31,7 @@ TRACE_CMD_OBJS += trace-show.o TRACE_CMD_OBJS += trace-list.o TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-dump.o +TRACE_CMD_OBJS += trace-sqldump.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o
tracecmd/include/trace-local.h +6 -0
@@ -103,6 +103,12 @@ int trace_record_agent(struct tracecmd_msg_handle *msg_handle, int cpus, int *fds, int argc, char **argv, bool use_fifos); +int trace_sql_record (struct tracecmd_input *handle,struct tep_record *record); + +int trace_sql_init (struct tracecmd_input *handle); + +int trace_sql_finish (struct tracecmd_input *handle); +struct hook_list ; void trace_init_profile(struct tracecmd_input *handle, struct hook_list *hooks,
tracecmd/trace-cmd.c +1 -0
@@ -76,6 +76,7 @@ struct command {*/ struct command commands[] = { {"report" , trace_report}, + {"sqldump" , trace_report}, {"snapshot" , trace_snapshot}, {"hist" , trace_hist}, {"mem" , trace_mem},
tracecmd/trace-read.c +40 -10
105 lines not on this page · raw message
tracecmd/trace-sqldump.c new file +268 -0
268 lines not on this page · raw message
Signature · 1 line
2.20.1