Skip to content

Commit

Permalink
More QtConcurrent cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed May 19, 2021
1 parent c09d0cc commit 6054d5f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
1 change: 0 additions & 1 deletion src/logdata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ target_include_directories(logdata PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include/

target_link_libraries(logdata PUBLIC
Qt5::Core
Qt5::Concurrent
roaring
tbb_wrapper
named_type
Expand Down
86 changes: 42 additions & 44 deletions src/logdata/src/logdataworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@
* along with klogg. If not, see <http://www.gnu.org/licenses/>.
*/

#include <chrono>
#include <cmath>
#include <string_view>
#include <thread>

#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QtConcurrent>

#include <tbb/flow_graph.h>

#include "configuration.h"
#include "log.h"
#include "logdata.h"
#include "logdataworker.h"
#include "progress.h"
#include "readablesize.h"

#include <chrono>
#include <cmath>

#include <string_view>

#include <tbb/flow_graph.h>
#include "logdataworker.h"

constexpr int IndexingBlockSize = 1 * 1024 * 1024;

Expand Down Expand Up @@ -402,57 +403,54 @@ void IndexOperation::doIndex( LineOffset initialPosition )
tbb::flow::graph indexingGraph;
using BlockData = std::pair<LineOffset::UnderlyingType, QByteArray>;

QThreadPool localThreadPool;
localThreadPool.setMaxThreadCount( 1 );

std::thread ioThread;
auto blockReaderAsync = tbb::flow::async_node<tbb::flow::continue_msg, BlockData>(
indexingGraph, tbb::flow::serial,
[ this, &localThreadPool, &file, &ioDuration ]( const auto&, auto& gateway ) {
[ this, &ioThread, &file, &ioDuration ]( const auto&, auto& gateway ) {
gateway.reserve_wait();

QtConcurrent::run(
&localThreadPool, [ this, &file, &ioDuration, gw = std::ref( gateway ) ] {
while ( !file.atEnd() ) {
ioThread = std::thread( [ this, &file, &ioDuration, gw = std::ref( gateway ) ] {
while ( !file.atEnd() ) {

if ( interruptRequest_ ) {
break;
}
if ( interruptRequest_ ) {
break;
}

BlockData blockData{ file.pos(),
QByteArray{ IndexingBlockSize, Qt::Uninitialized } };
BlockData blockData{ file.pos(),
QByteArray{ IndexingBlockSize, Qt::Uninitialized } };

clock::time_point ioT1 = clock::now();
const auto readBytes = static_cast<int>(
file.read( blockData.second.data(), blockData.second.size() ) );
clock::time_point ioT1 = clock::now();
const auto readBytes = static_cast<int>(
file.read( blockData.second.data(), blockData.second.size() ) );

if ( readBytes < 0 ) {
LOG_ERROR << "Reading past the end of file";
break;
}
if ( readBytes < 0 ) {
LOG_ERROR << "Reading past the end of file";
break;
}

if ( readBytes < blockData.second.size() ) {
blockData.second.resize( readBytes );
}
if ( readBytes < blockData.second.size() ) {
blockData.second.resize( readBytes );
}

clock::time_point ioT2 = clock::now();
clock::time_point ioT2 = clock::now();

ioDuration += duration_cast<milliseconds>( ioT2 - ioT1 );
ioDuration += duration_cast<milliseconds>( ioT2 - ioT1 );

LOG_DEBUG << "Sending block " << blockData.first << " size "
<< blockData.second.size();
LOG_DEBUG << "Sending block " << blockData.first << " size "
<< blockData.second.size();

while ( !gw.get().try_put( blockData ) ) {
QThread::msleep( 1 );
}
while ( !gw.get().try_put( blockData ) ) {
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
}
}

auto lastBlock = std::make_pair( -1, QByteArray{} );
while ( !gw.get().try_put( lastBlock ) ) {
QThread::msleep( 1 );
}
auto lastBlock = std::make_pair( -1, QByteArray{} );
while ( !gw.get().try_put( lastBlock ) ) {
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
}

gw.get().release_wait();
} );
gw.get().release_wait();
} );
} );

auto blockPrefetcher = tbb::flow::limiter_node<BlockData>( indexingGraph, prefetchBufferSize );
Expand Down Expand Up @@ -508,7 +506,7 @@ void IndexOperation::doIndex( LineOffset initialPosition )
file.seek( state.pos );
blockReaderAsync.try_put( tbb::flow::continue_msg{} );
indexingGraph.wait_for_all();
localThreadPool.waitForDone();
ioThread.join();

IndexingData::MutateAccessor scopedAccessor{ indexing_data_.get() };

Expand Down
57 changes: 29 additions & 28 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
#include <QProgressDialog>
#include <QRect>
#include <QScrollBar>
#include <QtConcurrent>
#include <QtCore>
#include <utility>

Expand Down Expand Up @@ -494,7 +493,7 @@ void AbstractLogView::keyPressEvent( QKeyEvent* keyEvent )
const auto altModifier = keyEvent->modifiers().testFlag( Qt::AltModifier );
const auto noModifier = keyEvent->modifiers() == Qt::NoModifier;

const auto jumpToBottomLine = [this]() {
const auto jumpToBottomLine = [ this ]() {
disableFollow();
const auto line = LineNumber( logData_->getNbLine().get() ) - 1_lcount;
selection_.selectLine( line );
Expand Down Expand Up @@ -1094,14 +1093,14 @@ void AbstractLogView::saveToFile()

progressDialog.setRange( 0, static_cast<int>( offsets.size() + 1 ) );
connect( &progressDialog, &QProgressDialog::canceled,
[&interruptRequest]() { interruptRequest.set(); } );
[ &interruptRequest ]() { interruptRequest.set(); } );

tbb::flow::graph saveFileGraph;
using LinesData = std::pair<std::vector<QString>, bool>;
auto lineReader = tbb::flow::input_node<LinesData>(
saveFileGraph,
[this, &offsets, &interruptRequest, &progressDialog, offsetIndex = 0u,
finalLine = false]( tbb::flow_control& fc ) mutable -> LinesData {
[ this, &offsets, &interruptRequest, &progressDialog, offsetIndex = 0u,
finalLine = false ]( tbb::flow_control& fc ) mutable -> LinesData {
if ( !interruptRequest && offsetIndex < offsets.size() ) {
const auto& offset = offsets.at( offsetIndex );
LinesData lines{ logData_->getLines( offset.first, offset.second ), true };
Expand All @@ -1128,8 +1127,8 @@ void AbstractLogView::saveToFile()

auto lineWriter = tbb::flow::function_node<LinesData, tbb::flow::continue_msg>(
saveFileGraph, 1,
[&interruptRequest, &codec, &saveFile, &progressDialog,
linesCount = 0u]( const LinesData& lines ) mutable {
[ &interruptRequest, &codec, &saveFile, &progressDialog,
linesCount = 0u ]( const LinesData& lines ) mutable {
if ( !lines.second ) {
if ( !interruptRequest ) {
saveFile.commit();
Expand Down Expand Up @@ -1462,7 +1461,7 @@ void AbstractLogView::jumpToEndOfLine()

// Search the longest line in the selection
const auto max_length = std::accumulate(
selection.cbegin(), selection.cend(), 0_length, [this]( auto currentMax, auto line ) {
selection.cbegin(), selection.cend(), 0_length, [ this ]( auto currentMax, auto line ) {
return qMax( currentMax, logData_->getLineLength( LineNumber( line ) ) );
} );
horizontalScrollBar()->setValue( max_length.get() - getNbVisibleCols() );
Expand All @@ -1482,7 +1481,7 @@ void AbstractLogView::jumpToRightOfScreen()

std::transform(
visibleLinesNumbers.begin(), visibleLinesNumbers.end(), std::back_inserter( lineLengths ),
[this]( const auto& line ) { return logData_->getLineLength( LineNumber( line ) ); } );
[ this ]( const auto& line ) { return logData_->getLineLength( LineNumber( line ) ); } );

const auto max_length = *std::max_element( lineLengths.begin(), lineLengths.end() );
horizontalScrollBar()->setValue( max_length.get() - getNbVisibleCols() );
Expand Down Expand Up @@ -1562,58 +1561,59 @@ void AbstractLogView::createMenu()
{
copyAction_ = new QAction( tr( "&Copy" ), this );
// No text as this action title depends on the type of selection
connect( copyAction_, &QAction::triggered, [this]( auto ) { this->copy(); } );
connect( copyAction_, &QAction::triggered, [ this ]( auto ) { this->copy(); } );

markAction_ = new QAction( tr( "&Mark" ), this );
connect( markAction_, &QAction::triggered, [this]( auto ) { this->markSelected(); } );
connect( markAction_, &QAction::triggered, [ this ]( auto ) { this->markSelected(); } );

saveToFileAction_ = new QAction( tr( "Save to file" ), this );
connect( saveToFileAction_, &QAction::triggered, [this]( auto ) { this->saveToFile(); } );
connect( saveToFileAction_, &QAction::triggered, [ this ]( auto ) { this->saveToFile(); } );

// For '#' and '*', shortcuts doesn't seem to work but
// at least it displays them in the menu, we manually handle those keys
// as keys event anyway (in keyPressEvent).
findNextAction_ = new QAction( tr( "Find &next" ), this );
findNextAction_->setShortcut( Qt::Key_Asterisk );
findNextAction_->setStatusTip( tr( "Find the next occurrence" ) );
connect( findNextAction_, &QAction::triggered, [this]( auto ) { this->findNextSelected(); } );
connect( findNextAction_, &QAction::triggered, [ this ]( auto ) { this->findNextSelected(); } );

findPreviousAction_ = new QAction( tr( "Find &previous" ), this );
findPreviousAction_->setShortcut( tr( "/" ) );
findPreviousAction_->setStatusTip( tr( "Find the previous occurrence" ) );
connect( findPreviousAction_, &QAction::triggered,
[this]( auto ) { this->findPreviousSelected(); } );
[ this ]( auto ) { this->findPreviousSelected(); } );

addToSearchAction_ = new QAction( tr( "&Add to search" ), this );
addToSearchAction_->setStatusTip( tr( "Add the selection to the current search" ) );
connect( addToSearchAction_, &QAction::triggered, [this]( auto ) { this->addToSearch(); } );
connect( addToSearchAction_, &QAction::triggered, [ this ]( auto ) { this->addToSearch(); } );

replaceSearchAction_ = new QAction( tr( "&Replace search" ), this );
replaceSearchAction_->setStatusTip( tr( "Replace the search expression with the selection" ) );
connect( replaceSearchAction_, &QAction::triggered, [this]( auto ) { this->replaceSearch(); } );
connect( replaceSearchAction_, &QAction::triggered,
[ this ]( auto ) { this->replaceSearch(); } );

setSearchStartAction_ = new QAction( tr( "Set search start" ), this );
connect( setSearchStartAction_, &QAction::triggered,
[this]( auto ) { this->setSearchStart(); } );
[ this ]( auto ) { this->setSearchStart(); } );

setSearchEndAction_ = new QAction( tr( "Set search end" ), this );
connect( setSearchEndAction_, &QAction::triggered, [this]( auto ) { this->setSearchEnd(); } );
connect( setSearchEndAction_, &QAction::triggered, [ this ]( auto ) { this->setSearchEnd(); } );

clearSearchLimitAction_ = new QAction( tr( "Clear search limits" ), this );
connect( clearSearchLimitAction_, &QAction::triggered,
[this]( auto ) { this->clearSearchLimits(); } );
[ this ]( auto ) { this->clearSearchLimits(); } );

setSelectionStartAction_ = new QAction( tr( "Set selection start" ), this );
connect( setSelectionStartAction_, &QAction::triggered,
[this]( auto ) { this->setSelectionStart(); } );
[ this ]( auto ) { this->setSelectionStart(); } );

setSelectionEndAction_ = new QAction( tr( "Set selection end" ), this );
connect( setSelectionEndAction_, &QAction::triggered,
[this]( auto ) { this->setSelectionEnd(); } );
[ this ]( auto ) { this->setSelectionEnd(); } );

saveDefaultSplitterSizesAction_ = new QAction( tr( "Save splitter position" ), this );
connect( saveDefaultSplitterSizesAction_, &QAction::triggered,
[this]( auto ) { emit saveDefaultSplitterSizes(); } );
[ this ]( auto ) { emit saveDefaultSplitterSizes(); } );

popupMenu_ = new QMenu( this );
highlightersMenu_ = popupMenu_->addMenu( "Highlighters" );
Expand Down Expand Up @@ -1660,9 +1660,9 @@ void AbstractLogView::considerMouseHovering( int x_pos, int y_pos )

void AbstractLogView::updateScrollBars()
{
verticalScrollBar()->setRange(
0,
qMax( 0, static_cast<int>( logData_->getNbLine().get() - getNbVisibleLines().get() + 1 ) ) );
verticalScrollBar()->setRange( 0,
qMax( 0, static_cast<int>( logData_->getNbLine().get()
- getNbVisibleLines().get() + 1 ) ) );

const int hScrollMaxValue
= qMax( 0, static_cast<int>( logData_->getMaxLength().get() ) - getNbVisibleCols() + 1 );
Expand Down Expand Up @@ -1709,7 +1709,8 @@ void AbstractLogView::drawTextArea( QPaintDevice* paint_device )
if ( firstLine_ > lines_in_file )
firstLine_ = LineNumber( lines_in_file.get() ? lines_in_file.get() - 1 : 0 );

const auto nbLines = qMin( getNbVisibleLines(), lines_in_file - LinesCount( firstLine_.get() ) );
const auto nbLines
= qMin( getNbVisibleLines(), lines_in_file - LinesCount( firstLine_.get() ) );

const int bottomOfTextPx = static_cast<int>( nbLines.get() ) * fontHeight;

Expand Down Expand Up @@ -1761,7 +1762,7 @@ void AbstractLogView::drawTextArea( QPaintDevice* paint_device )
leftMarginPx_ = contentStartPosX + SEPARATOR_WIDTH;

const auto searchStartIndex = lineIndex( searchStart_ );
const auto searchEndIndex = [this] {
const auto searchEndIndex = [ this ] {
auto index = lineIndex( searchEnd_ );
if ( searchEnd_ + 1_lcount != displayLineNumber( index ) ) {
// in filtered view lineIndex for "past the end" returns last line
Expand Down Expand Up @@ -1812,7 +1813,7 @@ void AbstractLogView::drawTextArea( QPaintDevice* paint_device )
std::vector<HighlightedMatch> allHighlights;
allHighlights.reserve( highlighterMatches.size() );
std::transform( highlighterMatches.begin(), highlighterMatches.end(),
std::back_inserter( allHighlights ), [&logLine]( const auto& match ) {
std::back_inserter( allHighlights ), [ &logLine ]( const auto& match ) {
const auto prefix = logLine.leftRef( match.startColumn() );
const auto expandedPrefixLength = untabify( prefix ).length();
auto startDelta = expandedPrefixLength - prefix.length();
Expand Down

0 comments on commit 6054d5f

Please sign in to comment.