1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
*/
// C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// KernelShark
#include "libkshark.h"
#include "libkshark-tepdata.h"
const char *default_file = "trace.dat";
int main(int argc, char **argv)
{
size_t i, sd, n_rows, n_tasks, n_evts, count;
struct kshark_context *kshark_ctx;
struct kshark_data_stream *stream;
struct kshark_entry **data = NULL;
int *pids, *evt_ids;
char *entry_str;
/* Create a new kshark session. */
kshark_ctx = NULL;
if (!kshark_instance(&kshark_ctx))
return 1;
/* Open a trace data file produced by trace-cmd. */
if (argc > 1)
sd = kshark_open(kshark_ctx, argv[1]);
else
sd = kshark_open(kshark_ctx, default_file);
if (sd < 0) {
kshark_free(kshark_ctx);
return 1;
}
/* Load the content of the file into an array of entries. */
n_rows = kshark_load_entries(kshark_ctx, sd, &data);
/* Filter the trace data coming from trace-cmd. */
n_tasks = kshark_get_task_pids(kshark_ctx, sd, &pids);
stream = kshark_get_data_stream(kshark_ctx, sd);
for (i = 0; i < n_tasks; ++i) {
char *task_str =
kshark_comm_from_pid(sd, pids[i]);
if (strcmp(task_str, "trace-cmd") == 0)
kshark_filter_add_id(kshark_ctx, sd,
KS_HIDE_TASK_FILTER,
pids[i]);
free(task_str);
}
free(pids);
/*
* Set the Filter Mask. In this case we want to avoid showing the
* filterd entris in text format.
*/
kshark_ctx->filter_mask = KS_TEXT_VIEW_FILTER_MASK;
kshark_ctx->filter_mask |= KS_EVENT_VIEW_FILTER_MASK;
kshark_filter_stream_entries(kshark_ctx, sd, data, n_rows);
/* Print to the screen the first 10 visible entries. */
count = 0;
i = 0;
for (i = 0; i < n_rows; ++i) {
if (data[i]->visible & KS_TEXT_VIEW_FILTER_MASK) {
entry_str = kshark_dump_entry(data[i]);
puts(entry_str);
free(entry_str);
if (++count > 10)
break;
}
++i;
}
puts("\n\n");
/* Show only "sched" events. */
n_evts = stream->n_events;
evt_ids = kshark_get_all_event_ids(kshark_ctx->stream[sd]);
for (i = 0; i < n_evts; ++i) {
char *event_str =
kshark_event_from_id(sd, evt_ids[i]);
if (strstr(event_str, "sched/"))
kshark_filter_add_id(kshark_ctx, sd,
KS_SHOW_EVENT_FILTER,
evt_ids[i]);
free(event_str);
}
kshark_filter_stream_entries(kshark_ctx, sd, data, n_rows);
/* Print to the screen the first 10 visible entries. */
count = 0;
i = 0;
for (i = 0; i < n_rows; ++i) {
if (data[i]->visible & KS_TEXT_VIEW_FILTER_MASK) {
entry_str = kshark_dump_entry(data[i]);
puts(entry_str);
free(entry_str);
if (++count > 10)
break;
}
++i;
}
puts("\n\n");
/* Clear all filters. */
kshark_filter_clear(kshark_ctx, sd, KS_HIDE_TASK_FILTER);
kshark_filter_clear(kshark_ctx, sd, KS_SHOW_EVENT_FILTER);
/* Use the Advanced filter to do event content based filtering. */
kshark_tep_add_filter_str(stream, "sched/sched_wakeup:target_cpu>1");
/* The Advanced filter requires reloading the data. */
for (i = 0; i < n_rows; ++i)
free(data[i]);
n_rows = kshark_load_entries(kshark_ctx, sd, &data);
count = 0;
for (i = 0; i < n_rows; ++i) {
if (data[i]->visible & KS_EVENT_VIEW_FILTER_MASK) {
entry_str = kshark_dump_entry(data[i]);
puts(entry_str);
free(entry_str);
if (++count > 10)
break;
}
}
/* Free the memory. */
for (i = 0; i < n_rows; ++i)
free(data[i]);
free(data);
/* Close the file. */
kshark_close(kshark_ctx, sd);
/* Close the session. */
kshark_free(kshark_ctx);
return 0;
}
|