This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#351: pause article cache loop when cache is empty
to improve CPU usage when idle
- Loading branch information
Showing
2 changed files
with
25 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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); | ||
|
@@ -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); | ||
}; | ||
|