Improve/debug the searching logic
cover Improve/debug the searching logic
Two patches aiming to resolve the known issues in the searching inside the trace data.
Yordan Karadzhov (2):
kernel-shark-qt: Avoid race condition when reading data
kernel-shark-qt: Implement State machine for searching in the data
kernel-shark-qt/src/CMakeLists.txt | 2 + kernel-shark-qt/src/KsModels.cpp | 70 ++++-- kernel-shark-qt/src/KsModels.hpp | 39 ++-- kernel-shark-qt/src/KsSearchFSM.cpp | 232 ++++++++++++++++++++ kernel-shark-qt/src/KsSearchFSM.hpp | 209 ++++++++++++++++++ kernel-shark-qt/src/KsTraceViewer.cpp | 241 +++++++-------------- kernel-shark-qt/src/KsTraceViewer.hpp | 32 ++- kernel-shark-qt/src/libkshark.c | 74 ++++--- kernel-shark-qt/src/libkshark.h | 3 - kernel-shark-qt/src/plugins/sched_events.c | 6 +- 10 files changed, 650 insertions(+), 258 deletions(-) create mode 100644 kernel-shark-qt/src/KsSearchFSM.cpp create mode 100644 kernel-shark-qt/src/KsSearchFSM.hpp
Signature · 1 line
2.17.1
PATCH 1/2 kernel-shark-qt: Avoid race condition when reading data
We know that the data reading operations are not thread-safe. A permanent solution for the problem is being worked on. For the time being, this patch provides a naive temporary fix by slapping mutexes all over the code, which will slow things down a bit.
--- kernel-shark-qt/src/libkshark.c | 74 +++++++++++++--------- kernel-shark-qt/src/libkshark.h | 3 - kernel-shark-qt/src/plugins/sched_events.c | 6 +- 3 files changed, 47 insertions(+), 36 deletions(-)
kernel-shark-qt/src/libkshark.c +44 -30
@@ -960,31 +960,6 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,return -ENOMEM ; } -/** -* @brief A thread-safe read of a record from a specific offset. -* -* @param kshark_ctx: Input location for the session context pointer. -* @param offset: the offset into the file to find the record. -* -* @returns The returned pevent_record must be freed. -*/ -struct tep_record *kshark_read_at (struct kshark_context *kshark_ctx, -uint64_t offset) -{ -/* -* Calling tracecmd_read_at() is not thread-safe. Use a mutex to -* protect the access. -*/ -pthread_mutex_lock (&kshark_ctx->input_mutex); - -struct tep_record *data =tracecmd_read_at (kshark_ctx->handle, - offset,NULL ); - -pthread_mutex_unlock (&kshark_ctx->input_mutex); - -return data; -} -static const char *kshark_get_latency (struct tep_handle *pe,struct tep_record *record) {
@@ -1046,10 +1021,18 @@ int kshark_get_pid_easy(struct kshark_entry *entry)/* * The entry has been touched by a plugin callback function. * Because of this we do not trust the value of "entry->pid". +* +* Currently the data reading operations are not thread-safe. +* Use a mutex to protect the access. */ - data =kshark_read_at (kshark_ctx, entry->offset); +pthread_mutex_lock (&kshark_ctx->input_mutex); + + data =tracecmd_read_at (kshark_ctx->handle, entry->offset, +NULL ); pid =tep_data_pid (kshark_ctx->pevent, data);free_record (data); + +pthread_mutex_unlock (&kshark_ctx->input_mutex); }return pid;
@@ -1106,10 +1089,18 @@ const char *kshark_get_latency_easy(struct kshark_entry *entry)if (entry->event_id <0 )return NULL ; - data =kshark_read_at (kshark_ctx, entry->offset); +/* +* Currently the data reading operations are not thread-safe. +* Use a mutex to protect the access. +*/ +pthread_mutex_lock (&kshark_ctx->input_mutex); + + data =tracecmd_read_at (kshark_ctx->handle, entry->offset,NULL ); lat =kshark_get_latency (kshark_ctx->pevent, data);free_record (data); +pthread_mutex_unlock (&kshark_ctx->input_mutex); +return lat; }
@@ -1142,10 +1133,18 @@ int kshark_get_event_id_easy(struct kshark_entry *entry)* The entry has been touched by a plugin callback function. * Because of this we do not trust the value of * "entry->event_id". +* +* Currently the data reading operations are not thread-safe. +* Use a mutex to protect the access. */ - data =kshark_read_at (kshark_ctx, entry->offset); +pthread_mutex_lock (&kshark_ctx->input_mutex); + + data =tracecmd_read_at (kshark_ctx->handle, entry->offset, +NULL ); event_id =tep_data_type (kshark_ctx->pevent, data);free_record (data); + +pthread_mutex_unlock (&kshark_ctx->input_mutex); }return (event_id ==-1 )? -EFAULT : event_id;
@@ -1184,7 +1183,14 @@ const char *kshark_get_event_name_easy(struct kshark_entry *entry) } } +/* +* Currently the data reading operations are not thread-safe. +* Use a mutex to protect the access. +*/ +pthread_mutex_lock (&kshark_ctx->input_mutex); event =tep_data_event_from_type (kshark_ctx->pevent, event_id); +pthread_mutex_unlock (&kshark_ctx->input_mutex); +if (event)return event->name;
@@ -1223,8 +1229,13 @@ const char *kshark_get_info_easy(struct kshark_entry *entry) } } - data =kshark_read_at (kshark_ctx, entry->offset); +/* +* Currently the data reading operations are not thread-safe. +* Use a mutex to protect the access. +*/ +pthread_mutex_lock (&kshark_ctx->input_mutex); + data =tracecmd_read_at (kshark_ctx->handle, entry->offset,NULL ); event_id =tep_data_type (kshark_ctx->pevent, data); event =tep_data_event_from_type (kshark_ctx->pevent, event_id);if (event)
@@ -1232,6 +1243,8 @@ const char *kshark_get_info_easy(struct kshark_entry *entry)free_record (data); +pthread_mutex_unlock (&kshark_ctx->input_mutex); +return info; }
@@ -1315,7 +1328,8 @@ char* kshark_dump_entry(const struct kshark_entry *entry)struct tep_event_format *event;struct tep_record *data; - data =kshark_read_at (kshark_ctx, entry->offset); + data =tracecmd_read_at (kshark_ctx->handle, entry->offset, +NULL ); event =tep_data_event_from_type (kshark_ctx->pevent, entry->event_id);
kernel-shark-qt/src/libkshark.h +0 -3
@@ -183,9 +183,6 @@ 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); -/** Bit masks used to control the visibility of the entry after filtering. */ enum kshark_filter_masks {/**
kernel-shark-qt/src/plugins/sched_events.c +3 -3
@@ -184,7 +184,7 @@ bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,if (plugin_ctx->sched_wakeup_event && e->event_id == plugin_ctx->sched_wakeup_event->id) { - record =kshark_read_at (kshark_ctx, e->offset); + record =tracecmd_read_at (kshark_ctx->handle, e->offset,NULL );/* We only want those that actually woke up the task. */ ret = tep_read_number_field(plugin_ctx->sched_wakeup_success_field,
@@ -196,7 +196,7 @@ bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,if (plugin_ctx->sched_wakeup_new_event && e->event_id == plugin_ctx->sched_wakeup_new_event->id) { - record =kshark_read_at (kshark_ctx, e->offset); + record =tracecmd_read_at (kshark_ctx->handle, e->offset,NULL );/* We only want those that actually woke up the task. */ ret = tep_read_number_field(plugin_ctx->sched_wakeup_new_success_field,
@@ -238,7 +238,7 @@ bool plugin_switch_match_rec_pid(struct kshark_context *kshark_ctx, e->event_id == plugin_ctx->sched_switch_event->id) {struct tep_record *record; - record =kshark_read_at (kshark_ctx, e->offset); + record =tracecmd_read_at (kshark_ctx->handle, e->offset,NULL ); ret =tep_read_number_field (plugin_ctx->sched_switch_prev_state_field, record->data, &val);
Signature · 1 line
2.17.1
PATCH 2/2 kernel-shark-qt: Implement State machine for searching in the data
In reply to Yordan KaradzhovThis patch was originally motivated by the request to make it possible to continue searching from the place when the search has been stopped by the user. However because the implementation of this feature made the original code quite messy (ugly), I decided to reorganize the way the searching is controlled and add a KsSearchFSM class that implements a Finite-State Machine by following the "State" design pattern. KsSearchFSM class now encapsulates the searching logic.
--- kernel-shark-qt/src/CMakeLists.txt | 2 + kernel-shark-qt/src/KsModels.cpp | 70 +++++--- kernel-shark-qt/src/KsModels.hpp | 39 +++-- kernel-shark-qt/src/KsSearchFSM.cpp | 232 +++++++++++++++++++++++++ kernel-shark-qt/src/KsSearchFSM.hpp | 209 ++++++++++++++++++++++ kernel-shark-qt/src/KsTraceViewer.cpp | 241 +++++++++----------------- kernel-shark-qt/src/KsTraceViewer.hpp | 32 ++-- 7 files changed, 603 insertions(+), 222 deletions(-) create mode 100644 kernel-shark-qt/src/KsSearchFSM.cpp create mode 100644 kernel-shark-qt/src/KsSearchFSM.hpp
kernel-shark-qt/src/CMakeLists.txt +2 -0
@@ -34,6 +34,7 @@ if (Qt5Widgets_FOUND AND Qt5Network_FOUND) set (ks-guiLib_hdr KsUtils.hpp KsModels.hpp KsGLWidget.hpp + KsSearchFSM.hpp KsDualMarker.hpp KsWidgetsLib.hpp KsTraceGraph.hpp
@@ -49,6 +50,7 @@ if (Qt5Widgets_FOUND AND Qt5Network_FOUND) KsModels.cpp KsSession.cpp KsGLWidget.cpp + KsSearchFSM.cpp KsDualMarker.cpp KsWidgetsLib.cpp KsTraceGraph.cpp
kernel-shark-qt/src/KsModels.cpp +49 -21
131 lines not on this page · raw message
kernel-shark-qt/src/KsModels.hpp +20 -19
@@ -26,9 +26,7 @@ // KernelShark #include "libkshark.h" #include "libkshark-model.h" - -/** Matching condition function type. To be user for searching. */ -typedef bool (*condition_func)(const QString &, const QString &); +#include "KsSearchFSM.hpp" enum class DualMarkerState;
@@ -87,7 +85,7 @@ public: size_t search(int column, const QString &searchText, - condition_func cond, + search_condition_func cond, QList<size_t> *matchList); /** Table columns Identifiers. */
@@ -162,14 +160,16 @@ public: size_t search(int column, const QString &searchText, - condition_func cond, + search_condition_func cond, QList<int> *matchList, QProgressBar *pb = nullptr, QLabel *l = nullptr); + size_t search(KsSearchFSM *sm, QList<int> *matchList); + QList<int> searchMap(int column, const QString &searchText, - condition_func cond, + search_condition_func cond, int first, int last, bool notify);
@@ -183,9 +183,6 @@ public: _searchStop = false; } - /** Stop the serch for all threads. */ - void searchStop() {_searchStop = true;} - /** * Use the "row" index in the Proxy model to retrieve the "row" index * in the source model.
@@ -196,6 +193,9 @@ public: return this->data(this->index(r, 0)).toInt(); } + /** Get the source model. */ + KsViewModel *source() {return _source;} + /** * A condition variable used to notify the main thread to update the * search progressbar.
@@ -205,24 +205,25 @@ public: /** A mutex used by the condition variable. */ std::mutex _mutex; + /** A flag used to stop the serch for all threads. */ + bool _searchStop; + private: int _searchProgress; - bool _searchStop; - /** Trace data array. */ kshark_entry **_data; KsViewModel *_source; - void _search(int column, - const QString &searchText, - condition_func cond, - QList<int> *matchList, - int first, int last, - QProgressBar *pb, - QLabel *l, - bool notify); + size_t _search(int column, + const QString &searchText, + search_condition_func cond, + QList<int> *matchList, + int first, int last, + QProgressBar *pb, + QLabel *l, + bool notify); }; /**
kernel-shark-qt/src/KsSearchFSM.cpp new file +232 -0
232 lines not on this page · raw message
kernel-shark-qt/src/KsSearchFSM.hpp new file +209 -0
209 lines not on this page · raw message
kernel-shark-qt/src/KsTraceViewer.cpp +78 -163
427 lines not on this page · raw message
kernel-shark-qt/src/KsTraceViewer.hpp +13 -19
@@ -18,6 +18,7 @@ // KernelShark #include "KsUtils.hpp" #include "KsModels.hpp" +#include "KsSearchFSM.hpp" #include "KsDualMarker.hpp" /**
@@ -67,6 +68,8 @@ public: void clearSelection(); + int selectedRow(); + void update(KsDataStore *data); signals:
@@ -100,24 +103,10 @@ private: QLabel _labelSearch, _labelGrFollows; - QComboBox _columnComboBox; - - QComboBox _selectComboBox; - - QLineEdit _searchLineEdit; - - QPushButton _prevButton, _nextButton, _searchStopButton; - - QAction *_pbAction, *_searchStopAction; + KsSearchFSM _searchFSM; QCheckBox _graphFollowsCheckBox; - QProgressBar _searchProgBar; - - QLabel _searchCountLabel; - - bool _searchDone; - bool _graphFollows; QList<int> _matchList;
@@ -139,11 +128,10 @@ private: void _resizeToContents(); - size_t _searchItems(int column, const QString &searchText, - condition_func cond); + size_t _searchItems(); void _searchItemsMapReduce(int column, const QString &searchText, - condition_func cond); + search_condition_func cond); void _searchEditText(const QString &);
@@ -161,13 +149,19 @@ private: void _searchStop(); + void _searchContinue(); + void _clicked(const QModelIndex& i); void _onCustomContextMenu(const QPoint &); void _setSearchIterator(int row); - int _getSelectedDataRow(); + bool _searchDone() + { + return _searchFSM.getState() == search_state_t::Done_s || + _searchFSM.getState() == search_state_t::Paused_s; + } private slots: void _searchEdit(int);
Signature · 1 line
2.17.1