New/improved KernelShark plugins
v2 cover New/improved KernelShark plugins
This series of patches combines a development which originally aimed to make the drawing logic of the Sched event plugin more robust and easy to understand. It turned out that this is not possible without introducing custom KernelShark entries for "Missed events".
This is the second version of this series of patches. Major changes from v1 are:
[2/8] The red box showing the time the task was preempted is open only if the prev_state of the sched_switch event is "R" (val & 0xff == 0).
[3/8] The call of KsGraphModel::update() is moved from KsGLWidget::_makeGraphs to KsTraceGraph::_selfUpdate(). This is done because the first version of the patch can cause infinit recursion in som cases.
Yordan Karadzhov (8):
kernel-shark-qt: Reset the second pass hash when reloading Sched
plugin
kernel-shark-qt: Improve the plotting logic of the Sched event plugin
kernel-shark-qt: Update the visualization model when updating Graphs
kernel-shark-qt: Add "Missed events" custom kshark_entry
kernel-shark-qt: Add instrumentation for "Missed events" to the model
kernel-shark-qt: Add tot_count field to the model descriptor
kernel-shark-qt: Add "Missed events" plugin for KernelShark
kernel-shark-qt: Update Sched Events plugin
kernel-shark-qt/src/KsTraceGraph.cpp | 1 + kernel-shark-qt/src/libkshark-model.c | 67 +++++++ kernel-shark-qt/src/libkshark-model.h | 15 ++ kernel-shark-qt/src/libkshark.c | 196 +++++++++++++++---- kernel-shark-qt/src/libkshark.h | 16 ++ kernel-shark-qt/src/plugins/CMakeLists.txt | 6 +- kernel-shark-qt/src/plugins/MissedEvents.cpp | 149 ++++++++++++++ kernel-shark-qt/src/plugins/SchedEvents.cpp | 174 ++++++++-------- kernel-shark-qt/src/plugins/missed_events.c | 41 ++++ kernel-shark-qt/src/plugins/missed_events.h | 30 +++ kernel-shark-qt/src/plugins/sched_events.c | 65 ++++-- kernel-shark-qt/src/plugins/sched_events.h | 18 +- 12 files changed, 630 insertions(+), 148 deletions(-) create mode 100644 kernel-shark-qt/src/plugins/MissedEvents.cpp create mode 100644 kernel-shark-qt/src/plugins/missed_events.c create mode 100644 kernel-shark-qt/src/plugins/missed_events.h
Signature · 1 line
2.17.1
PATCH v2 1/8 kernel-shark-qt: Reset the second pass hash when reloading Sched plugin
When the plugin is reloaded, the trace data gets reloaded as well. Hence the second pass over the data has to be repeated.
--- kernel-shark-qt/src/plugins/SchedEvents.cpp | 15 ++++++--------- kernel-shark-qt/src/plugins/sched_events.c | 4 ++++ kernel-shark-qt/src/plugins/sched_events.h | 3 +++ 3 files changed, 13 insertions(+), 9 deletions(-)
kernel-shark-qt/src/plugins/SchedEvents.cpp +6 -9
@@ -193,8 +193,6 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, return; } -static std::unordered_set<int> secondPassDone; - /* * Ideally, the sched_switch has to be the last trace event recorded before the * task is preempted. Because of this, when the data is loaded (the first pass),
@@ -250,8 +248,6 @@ static void secondPass(kshark_entry **data, } } } - - secondPassDone.insert(pid); } /**
@@ -298,12 +294,13 @@ void plugin_draw(kshark_cpp_argv *argv_c, int pid, int draw_action) KS_TASK_COLLECTION_MARGIN); } - try { - if (secondPassDone.find(pid) == secondPassDone.end()) { - /* The second pass for this task is not done yet. */ - secondPass(argvCpp->_histo->data, col, pid); - } + if (!tracecmd_filter_id_find(plugin_ctx->second_pass_hash, pid)) { + /* The second pass for this task is not done yet. */ + secondPass(argvCpp->_histo->data, col, pid); + tracecmd_filter_id_add(plugin_ctx->second_pass_hash, pid); + } + try { pluginDraw(plugin_ctx, kshark_ctx, argvCpp->_histo, col, SchedEvent::Switch, pid,
kernel-shark-qt/src/plugins/sched_events.c +4 -0
@@ -79,6 +79,8 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx) plugin_ctx->sched_wakeup_new_success_field =tep_find_field (event,"success" ); + plugin_ctx->second_pass_hash =tracecmd_filter_id_hash_alloc (); +return true; }
@@ -279,6 +281,8 @@ static int plugin_sched_close(struct kshark_context *kshark_ctx) plugin_sched_action, plugin_draw); +tracecmd_filter_id_hash_free (plugin_ctx->second_pass_hash); +free (plugin_ctx); plugin_sched_context_handler =NULL ;
kernel-shark-qt/src/plugins/sched_events.h +3 -0
@@ -55,6 +55,9 @@ struct plugin_sched_context {* Pointer to the sched_wakeup_new_success_field format descriptor. */ struct tep_format_field *sched_wakeup_new_success_field; + +/** Hash of the tasks for which the second pass is already done. */ +struct tracecmd_filter_id *second_pass_hash; };int plugin_get_next_pid (struct tep_record *record);
Signature · 1 line
2.17.1
PATCH v2 2/8 kernel-shark-qt: Improve the plotting logic of the Sched event plugin
In reply to Yordan KaradzhovThis patch aims to make the plotting logic of the Sched event plugin more robust and easy to understand. It also provides a proper processing of the case when we have multiple Sched events in one bin. The red box showing the time the task was preempted is open only if the prev_state of the sched_switch event is "R".
--- kernel-shark-qt/src/plugins/SchedEvents.cpp | 160 +++++++++----------- kernel-shark-qt/src/plugins/sched_events.c | 61 ++++++-- kernel-shark-qt/src/plugins/sched_events.h | 15 +- 3 files changed, 132 insertions(+), 104 deletions(-)
kernel-shark-qt/src/plugins/SchedEvents.cpp +74 -86
@@ -54,9 +54,11 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, KsPlot::Graph *graph, KsPlot::PlotObjList *shapes) { + const kshark_entry *entryClose, *entryOpen; std::function<void(int)> ifSchedBack; KsPlot::Rectangle *rec = nullptr; int height = graph->getHeight() * .3; + ssize_t indexClose(0), indexOpen(0); auto openBox = [&] (const KsPlot::Point &p) {
@@ -67,7 +69,16 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, if (!rec) rec = new KsPlot::Rectangle; + if (e == SchedEvent::Switch) { + /* Red box. */ + rec->_color = KsPlot::Color(255, 0, 0); + } else { + /* Green box. */ + rec->_color = KsPlot::Color(0, 255, 0); + } + rec->setFill(false); + rec->setPoint(0, p.x() - 1, p.y() - height); rec->setPoint(1, p.x() - 1, p.y() - 1); };
@@ -77,7 +88,7 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, if (rec == nullptr) return; - int boxSize = rec->getPoint(0)->x; + int boxSize = p.x() - rec->getPoint(0)->x; if (boxSize < PLUGIN_MIN_BOX_SIZE) { /* This box is too small. Don't try to plot it. */ delete rec;
@@ -92,99 +103,78 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, rec = nullptr; }; - auto lamIfSchSwitchFront = [&] (int bin) - { - /* - * Starting from the first element in this bin, go forward - * in time until you find a trace entry that satisfies the - * condition defined by kshark_match_pid. + for (int bin = 0; bin < graph->size(); ++bin) { + /** + * Plotting the latencies makes sense only in the case of a + * deep zoom. Here we set a naive threshold based on the number + * of entries inside the current bin. This cut seems to work + * well in all cases I tested so far, but it may result in + * unexpected behavior with some unusual trace data-sets. + * TODO: find a better criteria for deciding when to start + * plotting latencies. */ - const kshark_entry *entryF = - ksmodel_get_entry_front(histo, bin, false, - kshark_match_pid, pid, - col, nullptr); - - if (entryF && - entryF->pid == pid && - plugin_ctx->sched_switch_event && - entryF->event_id == plugin_ctx->sched_switch_event->id) { - /* - * entryF is sched_switch_event. Close the box and add - * it to the list of shapes to be ploted. - */ - closeBox(graph->getBin(bin)._base); + if (ksmodel_bin_count(histo, bin) > PLUGIN_MAX_ENTRIES_PER_BIN) { + if (rec) { + delete rec; + rec = nullptr; + } + + continue; } - }; - auto lamIfSchWakeupBack = [&] (int bin) - { /* - * Starting from the last element in this bin, go backward + * Starting from the first element in this bin, go forward * in time until you find a trace entry that satisfies the - * condition defined by plugin_wakeup_match_pid. + * condition defined by kshark_match_pid. */ - const kshark_entry *entryB = - ksmodel_get_entry_back(histo, bin, false, - plugin_wakeup_match_pid, pid, - col, nullptr); + entryClose = ksmodel_get_entry_back(histo, bin, false, + plugin_switch_match_entry_pid, + pid, col, &indexClose); - if (entryB) { + if (e == SchedEvent::Switch) { /* - * entryB is a sched_wakeup_event. Open a - * green box here. + * Starting from the last element in this bin, go backward + * in time until you find a trace entry that satisfies the + * condition defined by plugin_switch_match_pid. */ - openBox(graph->getBin(bin)._base); - - /* Green */ - rec->_color = KsPlot::Color(0, 255, 0); - } - }; - - auto lamIfSchSwitchBack = [&] (int bin) - { - /* - * Starting from the last element in this bin, go backward - * in time until you find a trace entry that satisfies the - * condition defined by plugin_switch_match_pid. - */ - const kshark_entry *entryB = - ksmodel_get_entry_back(histo, bin, false, - plugin_switch_match_pid, pid, - col, nullptr); + entryOpen = + ksmodel_get_entry_back(histo, bin, false, + plugin_switch_match_rec_pid, + pid, col, &indexOpen); - if (entryB && entryB->pid != pid) { + } else { /* - * entryB is a sched_switch_event. Open a - * red box here. + * Starting from the last element in this bin, go backward + * in time until you find a trace entry that satisfies the + * condition defined by plugin_wakeup_match_pid. */ - openBox(graph->getBin(bin)._base); - - /* Red */ - rec->_color = KsPlot::Color(255, 0, 0); + entryOpen = + ksmodel_get_entry_back(histo, bin, false, + plugin_wakeup_match_rec_pid, + pid, col, &indexOpen); } - }; - - if (e == SchedEvent::Switch) - ifSchedBack = lamIfSchSwitchBack; - else - ifSchedBack = lamIfSchWakeupBack; - for (int bin = 0; bin < graph->size(); ++bin) { - /** - * Plotting the latencies makes sense only in the case of a - * deep zoom. Here we set a naive threshold based on the number - * of entries inside the current bin. This cut seems to work - * well in all cases I tested so far, but it may result in - * unexpected behavior with some unusual trace data-sets. - * TODO: find a better criteria for deciding when to start - * plotting latencies. - */ - if (ksmodel_bin_count(histo, bin) > PLUGIN_MAX_ENTRIES_PER_BIN) - continue; - - lamIfSchSwitchFront(bin); - - ifSchedBack(bin); + if (rec) { + if (entryClose) { + /* Close the box in this bin. */ + closeBox(graph->getBin(bin)._base); + if (entryOpen && indexClose < indexOpen) { + /* + * We have a Sched switch entry that + * comes after (in time) the closure of + * the previous box. We have to open a + * new box in this bin. + */ + openBox(graph->getBin(bin)._base); + } + } + } else { + if (entryOpen && + (!entryClose || indexClose < indexOpen)) { + /* Open a new box in this bin. */ + openBox(graph->getBin(bin)._base); + } + } } if (rec)
@@ -221,7 +211,8 @@ static void secondPass(kshark_entry **data, kshark_entry_request *req = kshark_entry_request_alloc(first, n, - plugin_switch_match_pid, pid, + plugin_switch_match_rec_pid, + pid, false, KS_GRAPH_VIEW_FILTER_MASK);
@@ -256,9 +247,6 @@ static void secondPass(kshark_entry **data, * @param argv_c: A C pointer to be converted to KsCppArgV (C++ struct). * @param pid: Process Id. * @param draw_action: Draw action identifier. - * - * @returns True if the Pid of the entry matches the value of "pid". - * Otherwise false. */ void plugin_draw(kshark_cpp_argv *argv_c, int pid, int draw_action) {
@@ -303,12 +291,12 @@ void plugin_draw(kshark_cpp_argv *argv_c, int pid, int draw_action) try { pluginDraw(plugin_ctx, kshark_ctx, argvCpp->_histo, col, - SchedEvent::Switch, pid, + SchedEvent::Wakeup, pid, argvCpp->_graph, argvCpp->_shapes); pluginDraw(plugin_ctx, kshark_ctx, argvCpp->_histo, col, - SchedEvent::Wakeup, pid, + SchedEvent::Switch, pid, argvCpp->_graph, argvCpp->_shapes); } catch (const std::exception &exc) { std::cerr << "Exception in SchedEvents\n" << exc.what();
kernel-shark-qt/src/plugins/sched_events.c +48 -13
121 lines not on this page · raw message
kernel-shark-qt/src/plugins/sched_events.h +10 -5
@@ -36,6 +36,9 @@ struct plugin_sched_context {/** Pointer to the sched_switch_comm_field format descriptor. */ struct tep_format_field *sched_switch_comm_field; +/** Pointer to the sched_switch_prev_state_field format descriptor. */ +struct tep_format_field *sched_switch_prev_state_field; +/** Pointer to the sched_wakeup_event object. */ struct tep_event_format *sched_wakeup_event;
@@ -62,13 +65,15 @@ struct plugin_sched_context {int plugin_get_next_pid (struct tep_record *record); -int plugin_get_wakeup_pid (struct tep_record *record); +bool plugin_wakeup_match_rec_pid (struct kshark_context *kshark_ctx, +struct kshark_entry *e,int pid); -bool plugin_wakeup_match_pid (struct kshark_context *kshark_ctx, -struct kshark_entry *e,int pid); +bool plugin_switch_match_rec_pid (struct kshark_context *kshark_ctx, +struct kshark_entry *e,int pid); -bool plugin_switch_match_pid (struct kshark_context *kshark_ctx, -struct kshark_entry *e,int pid); +bool plugin_switch_match_entry_pid (struct kshark_context *kshark_ctx, +struct kshark_entry *e, +int pid);void plugin_draw (struct kshark_cpp_argv *argv,int pid,int draw_action);
Signature · 1 line
2.17.1
PATCH v2 3/8 kernel-shark-qt: Update the visualization model when updating Graphs
In reply to Yordan KaradzhovUpdating the model when the Graph widget is doing self-update, guarantees that all previously plotted graphs are wiped out before plotting something new.
--- kernel-shark-qt/src/KsTraceGraph.cpp | 1 + 1 file changed, 1 insertion(+)
kernel-shark-qt/src/KsTraceGraph.cpp +1 -0
@@ -215,6 +215,7 @@ void KsTraceGraph::_selfUpdate() _updateGraphLegends(); _updateTimeLegends(); _markerReDraw(); + _glWindow.model()->update(); updateGeom(); }
Signature · 1 line
2.17.1
PATCH v2 4/8 kernel-shark-qt: Add "Missed events" custom kshark_entry
In reply to Yordan KaradzhovThe ring buffer used during the acquisition of the tracing data can overflow. In such a case a fraction of the data is lost. This patch introduces a custom kshark_entry used to indicate that some data has been overwrite (lost). Instruments for insertion and processing of "Missed events" entries are added.
--- kernel-shark-qt/src/libkshark.c | 196 ++++++++++++++++++++++++++------ kernel-shark-qt/src/libkshark.h | 16 +++ 2 files changed, 179 insertions(+), 33 deletions(-)
kernel-shark-qt/src/libkshark.c +163 -33
@@ -16,7 +16,6 @@#include <stdlib.h> #include <stdio.h> #include <assert.h> -#include <errno.h> // KernelShark #include "libkshark.h"
@@ -558,6 +557,52 @@ static void kshark_set_entry_values(struct kshark_context *kshark_ctx, entry->pid =tep_data_pid (kshark_ctx->pevent, record); } +/** Prior time offset of the "missed_events" entry. */ +#define ME_ENTRY_TIME_SHIFT 10 + +static void missed_events_action (struct kshark_context *kshark_ctx, +struct tep_record *record, +struct kshark_entry *entry) +{ +/* +* Use the offset field of the entry to store the number of missed +* events. +*/ + entry->offset = record->missed_events; + + entry->cpu = record->cpu; + +/* +* Position the "missed_events" entry a bit before (in time) +* the original record. +*/ + entry->ts = record->ts -ME_ENTRY_TIME_SHIFT ; + +/* All custom entries must have negative event Identifiers. */ + entry->event_id =KS_EVENT_OVERFLOW ; + + entry->visible =0xFF ; + + entry->pid =tep_data_pid (kshark_ctx->pevent, record); +} + +static const char *missed_events_dump (struct kshark_context *kshark_ctx, +const struct kshark_entry *entry, +bool get_info) +{ +int size =0 ; +static char *buffer; + +if (get_info) + size =asprintf (&buffer,"missed_events=%i" , (int ) entry->offset); +else + size =asprintf (&buffer,"missed_events" ); +if (size >0 ) +return buffer; + +return NULL ; +} +/** * rec_list is used to pass the data to the load functions. * The rec_list will contain the list of entries from the source,
@@ -649,6 +694,21 @@ static size_t get_records(struct kshark_context *kshark_ctx,struct kshark_entry *entry;int ret; +if (rec->missed_events) { +/* +* Insert a custom "missed_events" entry just +* befor this record. +*/ + entry = &temp_rec->entry; +missed_events_action (kshark_ctx, rec, entry); + + temp_next = &temp_rec->next; + ++count; + +/* Now allocate a new rec_list node and comtinue. */ + *temp_next = temp_rec =calloc (1 ,sizeof (*temp_rec)); + } + entry = &temp_rec->entry;kshark_set_entry_values (kshark_ctx, rec, entry);
@@ -1006,6 +1066,9 @@ const char *kshark_get_latency_easy(struct kshark_entry *entry)if (!kshark_instance (&kshark_ctx))return NULL ; +if (entry->event_id <0 ) +return NULL ; + data =kshark_read_at (kshark_ctx, entry->offset); lat =kshark_get_latency (kshark_ctx->pevent, data);free_record (data);
@@ -1048,7 +1111,7 @@ int kshark_get_event_id_easy(struct kshark_entry *entry)free_record (data); } -return event_id; +return (event_id ==-1 )?EFAULT : event_id; }/**
@@ -1070,11 +1133,20 @@ const char *kshark_get_event_name_easy(struct kshark_entry *entry)struct tep_event_format *event;int event_id =kshark_get_event_id_easy (entry); - -if (event_id <0 ) +if (event_id ==EFAULT )return NULL ;kshark_instance (&kshark_ctx); + +if (event_id <0 ) { +switch (event_id) { +case KS_EVENT_OVERFLOW : +return missed_events_dump (kshark_ctx, entry, false); +default : +return NULL ; + } + } + event =tep_data_event_from_type (kshark_ctx->pevent, event_id);if (event)return event->name;
@@ -1105,6 +1177,15 @@ const char *kshark_get_info_easy(struct kshark_entry *entry)if (!kshark_instance (&kshark_ctx))return NULL ; +if (entry->event_id <0 ) { +switch (entry->event_id) { +case KS_EVENT_OVERFLOW : +return missed_events_dump (kshark_ctx, entry, true); +default : +return NULL ; + } + } + data =kshark_read_at (kshark_ctx, entry->offset); event_id =tep_data_type (kshark_ctx->pevent, data);
@@ -1133,6 +1214,43 @@ void kshark_convert_nano(uint64_t time, uint64_t *sec, uint64_t *usec) *usec = (time - s *1000000000ULL ) /1000 ; } +/** +* @brief Dump into a string the content a custom entry. The function allocates +* a null terminated string and returns a pointer to this string. +* +* @param kshark_ctx: Input location for the session context pointer. +* @param entry: A Kernel Shark entry to be printed. +* @param info_func: +* +* @returns The returned string contains a semicolon-separated list of data +* fields. The user has to free the returned string. +*/ +char *kshark_dump_custom_entry (struct kshark_context *kshark_ctx, +const struct kshark_entry *entry, +kshark_custom_info_func info_func) +{ +const char *event_name, *task, *info; +char *entry_str; +int size =0 ; + + task =tep_data_comm_from_pid (kshark_ctx->pevent, entry->pid); + event_name =info_func (kshark_ctx, entry, false); + info =info_func (kshark_ctx, entry, true); + + size =asprintf (&entry_str,"%li; %s-%i; CPU %i; ; %s; %s" , + entry->ts, + task, + entry->pid, + entry->cpu, + event_name, + info); + +if (size >0 ) +return entry_str; + +return NULL ; +} +/** * @brief Dump into a string the content of one entry. The function allocates * a null terminated string and returns a pointer to this string. The
@@ -1147,48 +1265,60 @@ char* kshark_dump_entry(const struct kshark_entry *entry) {const char *event_name, *task, *lat, *info;struct kshark_context *kshark_ctx; -struct tep_record *data; -struct tep_event_format *event;char *temp_str, *entry_str; -int event_id, size =0 ; +int size =0 ; kshark_ctx =NULL ;if (!kshark_instance (&kshark_ctx) || !init_thread_seq ())return NULL ; - data =kshark_read_at (kshark_ctx, entry->offset); + task =tep_data_comm_from_pid (kshark_ctx->pevent, entry->pid); - event_id =tep_data_type (kshark_ctx->pevent, data); - event =tep_data_event_from_type (kshark_ctx->pevent, event_id); +if (entry->event_id >=0 ) { +struct tep_event_format *event; +struct tep_record *data; - event_name = event? event->name :"[UNKNOWN EVENT]" ; - task =tep_data_comm_from_pid (kshark_ctx->pevent, entry->pid); - lat =kshark_get_latency (kshark_ctx->pevent, data); + data =kshark_read_at (kshark_ctx, entry->offset); - size =asprintf (&temp_str,"%li %s-%i; CPU %i; %s;" , - entry->ts, - task, - entry->pid, - entry->cpu, - lat); + event =tep_data_event_from_type (kshark_ctx->pevent, + entry->event_id); - info =kshark_get_info (kshark_ctx->pevent, data, event); -if (size >0 ) { - size =asprintf (&entry_str,"%s %s; %s; 0x%x" , - temp_str, - event_name, - info, - entry->visible); + event_name = event? event->name :"[UNKNOWN EVENT]" ; + lat =kshark_get_latency (kshark_ctx->pevent, data); -free (temp_str); - } + size =asprintf (&temp_str,"%li; %s-%i; CPU %i; %s;" , + entry->ts, + task, + entry->pid, + entry->cpu, + lat); -free_record (data); + info =kshark_get_info (kshark_ctx->pevent, data, event); -if (size >0 ) -return entry_str; +if (size >0 ) { + size =asprintf (&entry_str,"%s %s; %s; 0x%x" , + temp_str, + event_name, + info, + entry->visible); -return NULL ; +free (temp_str); + } + +free_record (data); +if (size <1 ) + entry_str =NULL ; + }else { +switch (entry->event_id) { +case KS_EVENT_OVERFLOW : + entry_str =kshark_dump_custom_entry (kshark_ctx, entry, + missed_events_dump); +default : + entry_str =NULL ; + } + } + +return entry_str; }/**
@@ -1402,7 +1532,7 @@ get_entry(const struct kshark_entry_request *req,if (index) {if (e) - *index = (e->event_id >=0 )? i :KS_FILTERED_BIN ; + *index = (e->cpu !=KS_FILTERED_BIN )? i :KS_FILTERED_BIN ;else *index =KS_EMPTY_BIN ; }
kernel-shark-qt/src/libkshark.h +16 -0
@@ -15,6 +15,7 @@// C #include <stdint.h> #include <pthread.h> +#include <errno.h> // Json-C #include <json.h>
@@ -164,6 +165,18 @@ void kshark_convert_nano(uint64_t time, uint64_t *sec, uint64_t *usec);char *kshark_dump_entry (const struct kshark_entry *entry); +/** +* Custom entry info function type. To be user for dumping info for custom +* KernelShark entryes. +*/ +typedef const char *(kshark_custom_info_func )(struct kshark_context *, +const struct kshark_entry *, +bool ); + +char *kshark_dump_custom_entry (struct kshark_context *kshark_ctx, +const struct kshark_entry *entry, +kshark_custom_info_func info_func); +struct tep_record *kshark_read_at (struct kshark_context *kshark_ctx,uint64_t offset);
@@ -281,6 +294,9 @@ bool kshark_match_cpu(struct kshark_context *kshark_ctx,/** Filtered bin identifier. */ #define KS_FILTERED_BIN -2 +/** Overflow Event identifier. */ +#define KS_EVENT_OVERFLOW (-EOVERFLOW) +/** Matching condition function type. To be user for data requests */ typedef bool (matching_condition_func)(struct kshark_context*, struct kshark_entry*,
Signature · 1 line
2.17.1
PATCH v2 5/8 kernel-shark-qt: Add instrumentation for "Missed events" to the model
In reply to Yordan KaradzhovThis patch adds to the Visualization model instruments, used for finding and plotting "Missed events".
--- kernel-shark-qt/src/libkshark-model.c | 60 +++++++++++++++++++++++++++ kernel-shark-qt/src/libkshark-model.h | 12 ++++++ 2 files changed, 72 insertions(+)
kernel-shark-qt/src/libkshark-model.c +60 -0
@@ -1222,3 +1222,63 @@ bool ksmodel_task_visible_event_exist(struct kshark_trace_histo *histo,return true; } + +static bool match_cpu_missed_events (struct kshark_context *kshark_ctx, +struct kshark_entry *e,int cpu) +{ +return e->event_id == -EOVERFLOW && e->cpu == cpu; +} + +static bool match_pid_missed_events (struct kshark_context *kshark_ctx, +struct kshark_entry *e,int pid) +{ +return e->event_id == -EOVERFLOW && e->pid == pid; +} + +/** +* @brief In a given CPU and bin, start from the front end of the bin and go towards +* the back end, searching for a Missed Events entry. +* +* @param histo: Input location for the model descriptor. +* @param bin: Bin id. +* @param cpu: CPU Id. +* @param col: Optional input location for Data collection. +* @param index: Optional output location for the index of the requested +* entry inside the array. +* +* @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL. +*/ +const struct kshark_entry * +ksmodel_get_cpu_missed_events (struct kshark_trace_histo *histo, +int bin,int cpu, +struct kshark_entry_collection *col, +ssize_t *index) +{ +return ksmodel_get_entry_front (histo, bin, true, + match_cpu_missed_events, cpu, + col, index); +} + +/** +* @brief In a given task and bin, start from the front end of the bin and go towards +* the back end, searching for a Missed Events entry. +* +* @param histo: Input location for the model descriptor. +* @param bin: Bin id. +* @param pid: Process Id of the task. +* @param col: Optional input location for Data collection. +* @param index: Optional output location for the index of the requested +* entry inside the array. +* +* @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL. +*/ +const struct kshark_entry * +ksmodel_get_task_missed_events (struct kshark_trace_histo *histo, +int bin,int pid, +struct kshark_entry_collection *col, +ssize_t *index) +{ +return ksmodel_get_entry_front (histo, bin, true, + match_pid_missed_events, pid, + col, index); +}
kernel-shark-qt/src/libkshark-model.h +12 -0
@@ -148,6 +148,18 @@ bool ksmodel_task_visible_event_exist(struct kshark_trace_histo *histo,struct kshark_entry_collection *col, ssize_t *index); +const struct kshark_entry * +ksmodel_get_cpu_missed_events (struct kshark_trace_histo *histo, +int bin,int cpu, +struct kshark_entry_collection *col, +ssize_t *index); + +const struct kshark_entry * +ksmodel_get_task_missed_events (struct kshark_trace_histo *histo, +int bin,int pid, +struct kshark_entry_collection *col, +ssize_t *index); +static inline double ksmodel_bin_time (struct kshark_trace_histo *histo,int bin) {
Signature · 1 line
2.17.1
PATCH v2 6/8 kernel-shark-qt: Add tot_count field to the model descriptor
In reply to Yordan KaradzhovThe tot_count field of the Visualization model descriptor provides fast access to the total number of entries being currently visualized. The total count can be useful in various cases. For example the sched event plugin can use it for knowing when to plot wakeup latency.
--- kernel-shark-qt/src/libkshark-model.c | 7 +++++++ kernel-shark-qt/src/libkshark-model.h | 3 +++ 2 files changed, 10 insertions(+)
kernel-shark-qt/src/libkshark-model.c +7 -0
@@ -298,6 +298,7 @@ static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo) {int i =0 , prev_not_empty; + histo->tot_count =0 ;memset (&histo->bin_count[0 ],0 , (histo->n_bins) *sizeof (histo->bin_count[0 ]));/*
@@ -329,6 +330,10 @@ static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo) histo->bin_count[prev_not_empty] = histo->map[i] - histo->map[prev_not_empty]; +if (prev_not_empty !=LOB (histo)) + histo->tot_count += + histo->bin_count[prev_not_empty]; + prev_not_empty = i; } }
@@ -350,6 +355,8 @@ static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo) histo->bin_count[prev_not_empty] = histo->map[UOB (histo)] - histo->map[prev_not_empty]; } + + histo->tot_count += histo->bin_count[prev_not_empty]; }/**
kernel-shark-qt/src/libkshark-model.h +3 -0
@@ -51,6 +51,9 @@ struct kshark_trace_histo {/** Number of entries in each bin. */ size_t *bin_count; +/** Total number of entries in all bin except the overflow bins. */ +int tot_count; +/** * Lower edge of the time-window to be visualized. Only entries having * timestamp >= min will be visualized.
Signature · 1 line
2.17.1
PATCH v2 7/8 kernel-shark-qt: Add "Missed events" plugin for KernelShark
In reply to Yordan KaradzhovThe "Missed events" plugin displays a special marker that indicates the position (in time) of the missed (overwritten) data.
--- kernel-shark-qt/src/plugins/CMakeLists.txt | 6 +- kernel-shark-qt/src/plugins/MissedEvents.cpp | 149 +++++++++++++++++++ kernel-shark-qt/src/plugins/missed_events.c | 41 +++++ kernel-shark-qt/src/plugins/missed_events.h | 30 ++++ 4 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 kernel-shark-qt/src/plugins/MissedEvents.cpp create mode 100644 kernel-shark-qt/src/plugins/missed_events.c create mode 100644 kernel-shark-qt/src/plugins/missed_events.h
kernel-shark-qt/src/plugins/CMakeLists.txt +5 -1
@@ -24,7 +24,11 @@ BUILD_PLUGIN(NAME sched_events list(APPEND PLUGIN_LIST "sched_events default") # This plugin will be loaded by default # list(APPEND PLUGIN_LIST "sched_events") # This plugin isn't loaded by default -install(TARGETS sched_events +BUILD_PLUGIN(NAME missed_events + SOURCE missed_events.c MissedEvents.cpp) +list(APPEND PLUGIN_LIST "missed_events default") # This plugin will be loaded by default + +install(TARGETS sched_events missed_events LIBRARY DESTINATION /usr/local/lib/kshark/) set(PLUGINS ${PLUGIN_LIST} PARENT_SCOPE)
kernel-shark-qt/src/plugins/MissedEvents.cpp new file +149 -0
149 lines not on this page · raw message
kernel-shark-qt/src/plugins/missed_events.c new file +41 -0
41 lines not on this page · raw message
kernel-shark-qt/src/plugins/missed_events.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ + +/* +* Copyright (C) 2018 VMware Inc, Yordan Karadzhov <[email protected]> +*/ + +/** +* @file missed_events.h +* @brief Plugin for visualization of missed events due to overflow of the +* ring buffer. +*/ + +#ifndef _KS_PLUGIN_M_EVTS_H +#define _KS_PLUGIN_M_EVTS_H + +// KernelShark +#include "libkshark.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void draw_missed_events (struct kshark_cpp_argv *argv, +int pid,int draw_action); + +#ifdef __cplusplus +} +#endif + +#endif
Signature · 1 line
2.17.1
PATCH v2 8/8 kernel-shark-qt: Update Sched Events plugin
In reply to Yordan KaradzhovThis patch updates the Sched Events plugin to use the recently introduced "Missed events" entries and tot_count field of the Visualization model descriptor in its plotting logic. We need this update because the sched plugin would get confused because of missed events, where we could have missed events hiding wakeups, and only have the sched switch, or a wakeup with a missing sched switch would break the plotting of the latency.
--- kernel-shark-qt/src/plugins/SchedEvents.cpp | 43 ++++++++++----------- 1 file changed, 20 insertions(+), 23 deletions(-)
kernel-shark-qt/src/plugins/SchedEvents.cpp +20 -23
@@ -28,7 +28,7 @@ #define PLUGIN_MIN_BOX_SIZE 4 -#define PLUGIN_MAX_ENTRIES_PER_BIN 500 +#define PLUGIN_MAX_ENTRIES 10000 #define KS_TASK_COLLECTION_MARGIN 25
@@ -54,11 +54,11 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, KsPlot::Graph *graph, KsPlot::PlotObjList *shapes) { - const kshark_entry *entryClose, *entryOpen; + const kshark_entry *entryClose, *entryOpen, *entryME; + ssize_t indexClose(0), indexOpen(0), indexME(0); std::function<void(int)> ifSchedBack; KsPlot::Rectangle *rec = nullptr; int height = graph->getHeight() * .3; - ssize_t indexClose(0), indexOpen(0); auto openBox = [&] (const KsPlot::Point &p) {
@@ -104,24 +104,6 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, }; for (int bin = 0; bin < graph->size(); ++bin) { - /** - * Plotting the latencies makes sense only in the case of a - * deep zoom. Here we set a naive threshold based on the number - * of entries inside the current bin. This cut seems to work - * well in all cases I tested so far, but it may result in - * unexpected behavior with some unusual trace data-sets. - * TODO: find a better criteria for deciding when to start - * plotting latencies. - */ - if (ksmodel_bin_count(histo, bin) > PLUGIN_MAX_ENTRIES_PER_BIN) { - if (rec) { - delete rec; - rec = nullptr; - } - - continue; - } - /* * Starting from the first element in this bin, go forward * in time until you find a trace entry that satisfies the
@@ -131,6 +113,11 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, plugin_switch_match_entry_pid, pid, col, &indexClose); + entryME = ksmodel_get_task_missed_events(histo, + bin, pid, + col, + &indexME); + if (e == SchedEvent::Switch) { /* * Starting from the last element in this bin, go backward
@@ -155,10 +142,12 @@ static void pluginDraw(plugin_sched_context *plugin_ctx, } if (rec) { - if (entryClose) { + if (entryME || entryClose) { /* Close the box in this bin. */ closeBox(graph->getBin(bin)._base); - if (entryOpen && indexClose < indexOpen) { + if (entryOpen && + indexME < indexOpen && + indexClose < indexOpen) { /* * We have a Sched switch entry that * comes after (in time) the closure of
@@ -288,6 +277,14 @@ void plugin_draw(kshark_cpp_argv *argv_c, int pid, int draw_action) tracecmd_filter_id_add(plugin_ctx->second_pass_hash, pid); } + /* + * Plotting the latencies makes sense only in the case of a deep zoom. + * Here we set a threshold based on the total number of entries being + * visualized by the model. + * Don't be afraid to play with different values for this threshold. + */ + if (argvCpp->_histo->tot_count > PLUGIN_MAX_ENTRIES) + return; try { pluginDraw(plugin_ctx, kshark_ctx, argvCpp->_histo, col,
Signature · 1 line
2.17.1