'last_cstate    integer,'
                'wake_reason    integer)')
 
+do_query(query, 'CREATE TABLE context_switches ('
+               'id             integer         NOT NULL        PRIMARY KEY,'
+               'machine_id     bigint,'
+               'time           bigint,'
+               'cpu            integer,'
+               'thread_out_id  bigint,'
+               'comm_out_id    bigint,'
+               'thread_in_id   bigint,'
+               'comm_in_id     bigint,'
+               'flags          integer)')
+
 # printf was added to sqlite in version 3.8.3
 sqlite_has_printf = False
 try:
        ' INNER JOIN selected_events ON selected_events.id = evsel_id'
        ' WHERE selected_events.name IN (\'cbr\',\'mwait\',\'exstop\',\'pwre\',\'pwrx\')')
 
+do_query(query, 'CREATE VIEW context_switches_view AS '
+       'SELECT '
+               'context_switches.id,'
+               'context_switches.machine_id,'
+               'context_switches.time,'
+               'context_switches.cpu,'
+               'th_out.pid AS pid_out,'
+               'th_out.tid AS tid_out,'
+               'comm_out.comm AS comm_out,'
+               'th_in.pid AS pid_in,'
+               'th_in.tid AS tid_in,'
+               'comm_in.comm AS comm_in,'
+               'CASE     WHEN context_switches.flags = 0 THEN \'in\''
+                       ' WHEN context_switches.flags = 1 THEN \'out\''
+                       ' WHEN context_switches.flags = 3 THEN \'out preempt\''
+                       ' ELSE context_switches.flags '
+               'END AS flags'
+       ' FROM context_switches'
+       ' INNER JOIN threads AS th_out ON th_out.id   = context_switches.thread_out_id'
+       ' INNER JOIN threads AS th_in  ON th_in.id    = context_switches.thread_in_id'
+       ' INNER JOIN comms AS comm_out ON comm_out.id = context_switches.comm_out_id'
+       ' INNER JOIN comms AS comm_in  ON comm_in.id  = context_switches.comm_in_id')
+
 do_query(query, 'END TRANSACTION')
 
 evsel_query = QSqlQuery(db)
 exstop_query.prepare("INSERT INTO exstop VALUES (?, ?)")
 pwrx_query = QSqlQuery(db)
 pwrx_query.prepare("INSERT INTO pwrx VALUES (?, ?, ?, ?)")
+context_switch_query = QSqlQuery(db)
+context_switch_query.prepare("INSERT INTO context_switches VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
 
 def trace_begin():
        printdate("Writing records...")
                drop("pwrx")
                if is_table_empty("cbr"):
                        drop("cbr")
+       if is_table_empty("context_switches"):
+               drop("context_switches")
 
        if (unhandled_count):
                printdate("Warning: ", unhandled_count, " unhandled events")
                pwrx(id, raw_buf)
        elif config == 5:
                cbr(id, raw_buf)
+
+def context_switch_table(*x):
+       bind_exec(context_switch_query, 9, x)