Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#351: pause article cache loop when cache is empty
Browse files Browse the repository at this point in the history
to improve CPU usage when idle
  • Loading branch information
hugbug committed Jan 11, 2019
1 parent b9c4c5b commit a329c65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 22 additions & 2 deletions daemon/nntp/ArticleWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2014-2017 Andrey Prygunkov <[email protected]>
* Copyright (C) 2014-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -775,6 775,11 @@ CachedSegmentData ArticleCache::Alloc(int size)
{
g_DiskState->WriteCacheFlag();
}
if (!m_allocated)
{
// Resume Run(), the notification arrives later, after releasing m_allocMutex
m_allocCond.NotifyAll();
}
m_allocated = size;
}
}
Expand Down Expand Up @@ -821,13 826,19 @@ void ArticleCache::Run()
bool justFlushed = false;
while (!IsStopped() || m_allocated > 0)
{
if ((justFlushed || resetCounter >= 1000 || IsStopped() ||
if ((justFlushed || resetCounter >= 1000 || IsStopped() ||
(g_Options->GetDirectWrite() && m_allocated >= fillThreshold)) &&
m_allocated > 0)
{
justFlushed = CheckFlush(m_allocated >= fillThreshold);
resetCounter = 0;
}
else if (!m_allocated)
{
Guard guard(m_allocMutex);
m_allocCond.Wait(m_allocMutex, [&]{ return IsStopped() || m_allocated > 0; });
resetCounter = 0;
}
else
{
usleep(5 * 1000);
Expand All @@ -836,6 847,15 @@ void ArticleCache::Run()
}
}

void ArticleCache::Stop()
{
Thread::Stop();

// Resume Run() to exit it
Guard guard(m_allocMutex);
m_allocCond.NotifyAll();
}

bool ArticleCache::CheckFlush(bool flushEverything)
{
debug("Checking cache, Allocated: %i, FlushEverything: %i", (int)m_allocated, (int)flushEverything);
Expand Down
4 changes: 3 additions & 1 deletion daemon/nntp/ArticleWriter.h
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2014-2017 Andrey Prygunkov <[email protected]>
* Copyright (C) 2014-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -95,6 95,7 @@ class ArticleCache : public Thread
};

virtual void Run();
virtual void Stop();
CachedSegmentData Alloc(int size);
bool Realloc(CachedSegmentData* segment, int newSize);
void Free(CachedSegmentData* segment);
Expand All @@ -111,6 112,7 @@ class ArticleCache : public Thread
Mutex m_flushMutex;
Mutex m_contentMutex;
FileInfo* m_fileInfo = nullptr;
ConditionVar m_allocCond;

bool CheckFlush(bool flushEverything);
};
Expand Down

0 comments on commit a329c65

Please sign in to comment.