Skip to content

Commit

Permalink
fix: After upgrading to version 1071, dde-file-manager monitor failed…
Browse files Browse the repository at this point in the history
… and memory continued to grow

Infocache adds threads to handle the cache time of Fileinfo and FileWatcher, and exceeds the online limit

Log: After upgrading to version 1071, dde-file-manager monitor failed and memory continued to grow
Bug: https://pms.uniontech.com/bug-view-268167.html
  • Loading branch information
liyigang1 committed Aug 30, 2024
1 parent 0f1e987 commit 7b0aeb7
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 42 deletions.
29 changes: 24 additions & 5 deletions include/dfm-base/utils/infocache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,28 @@ class CacheWorker : public QObject
public Q_SLOTS:
void cacheInfo(const QUrl url, const FileInfoPointer info);
void removeCaches(const QList<QUrl> urls);
void updateInfoTime(const QUrl url);
void dealRemoveInfo();
void removeInfosTime(const QList<QUrl> urls);
void disconnectWatcher(const QMap<QUrl, FileInfoPointer> infos);

private:
explicit CacheWorker(QObject *parent = nullptr);
};

// 计算和统计fileinfo、filewatcher超过缓存数量和超过时间移除cache的处理
class TimeToUpdateCache : public QObject
{
Q_OBJECT
friend class InfoCacheController;

public:
~TimeToUpdateCache() override;
public Q_SLOTS:
void updateInfoTime(const QUrl url);
void dealRemoveInfo();
void updateWatcherTime(const QList<QUrl> &urls, const bool add);
private:
explicit TimeToUpdateCache(QObject *parent = nullptr);
};

class InfoCachePrivate;
class InfoCache : public QObject
{
Expand All @@ -48,6 +61,7 @@ class InfoCache : public QObject
QScopedPointer<InfoCachePrivate> d;
friend class CacheWorker;
friend class InfoCacheController;
friend class TimeToUpdateCache;

public:
virtual ~InfoCache() override;
Expand All @@ -56,7 +70,6 @@ class InfoCache : public QObject
void cacheRemoveCaches(const QList<QUrl> &key);
void cacheDisconnectWatcher(const QMap<QUrl, FileInfoPointer> infos);
void cacheUpdateInfoTime(const QUrl url);
void cacheRemoveInfosTime(const QList<QUrl> urls);

private:
explicit InfoCache(QObject *parent = nullptr);
Expand All @@ -68,14 +81,18 @@ class InfoCache : public QObject
void cacheInfo(const QUrl url, const FileInfoPointer info);
void disconnectWatcher(const QMap<QUrl, FileInfoPointer> infos);
void removeCaches(const QList<QUrl> urls);
void updateSortTimeWorker(const QUrl url);
bool updateSortTimeWorker(const QUrl url);
void timeRemoveCache();
void removeInfosTimeWorker(const QList<QUrl> urls);
void updateSortTimeWatcherWorker(const QList<QUrl> &urls, const bool add);

private Q_SLOTS:
void fileAttributeChanged(const QUrl url);
void removeCache(const QUrl url);
void refreshFileInfo(const QUrl &url);
private:
void addWatcherTimeInfo(const QList<QUrl> &urls);
void removeWatcherTimeInfo(const QList<QUrl> &urls);
};

class InfoCacheController : public QObject
Expand All @@ -85,6 +102,8 @@ class InfoCacheController : public QObject
QSharedPointer<QThread> thread { nullptr };
QSharedPointer<CacheWorker> worker { nullptr };
QSharedPointer<QTimer> removeTimer { nullptr }; // 移除缓存的
QSharedPointer<QThread> threadUpdate { nullptr };
QSharedPointer<TimeToUpdateCache> workerUpdate { nullptr };
public:
virtual ~InfoCacheController() override;
static InfoCacheController &instance();
Expand Down
132 changes: 132 additions & 0 deletions include/dfm-base/utils/threadcontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,137 @@ class DThreadMap
QMap<DKey, DValue> myMap; // 当前的QMap
QMutex mutable mutex; // 当前的锁
};

template<class DKey, class DValue>
class DThreadHash
{
public:
DThreadHash<DKey, DValue>()
: myHash()
{
}
/*!
* \brief insert 插入一个模板类型到map
*
* \param Key 模板类引用key
*
* \param Value 模板类引用value
*
* \return
*/
inline void insert(const DKey &key, const DValue &value)
{
QMutexLocker lk(&mutex);
myHash.insert(key, value);
}
/*!
* \brief remove 从map中移除所有的模板类型
*
* \param Key 模板类引用key
*
* \return
*/
inline void remove(const DKey &key)
{
QMutexLocker lk(&mutex);
myHash.remove(key);
}
/*!
* \brief contains map中是否包含key对应的键值对
*
* Key 模板类引用key
*
* \return bool 是否包含key对应的键值对
*/
inline DValue value(const DKey &key)
{
QMutexLocker lk(&mutex);
return myHash.value(key);
}
/*!
* \brief contains map中是否包含key对应的键值对
*
* Key 模板类引用key
*
* \return bool 是否包含key对应的键值对
*/
inline bool contains(const DKey &key)
{
QMutexLocker lk(&mutex);
return myHash.contains(key);
}
/*!
* \brief begin 当前map的开始迭代器
*
* \param null
*
* \return QMap<Key, Value>::iterator 当前map的开始迭代器
*/
inline typename QMap<DKey, DValue>::iterator begin()
{
QMutexLocker lk(&mutex);
return myHash.begin();
}
/*!
* \brief begin 当前map的结束迭代器
*
* \param null
*
* \return QMap<Key, Value>::iterator 当前map的结束迭代器
*/
inline typename QMap<DKey, DValue>::iterator end()
{
QMutexLocker lk(&mutex);
return myHash.end();
}
/*!
* \brief erase 去掉map中当前的迭代器
*
* \param QMap<Key, Value>::iterator map的迭代器
*
* \return QMap<Key, Value>::iterator 当前迭代器的下一个迭代器
*/
inline typename QMap<DKey, DValue>::iterator erase(typename QMap<DKey, DValue>::iterator it)
{
QMutexLocker lk(&mutex);
return myHash.erase(it);
}
/*!
* \brief count map的总个数
*
* \return int map的总个数
*/
inline int count()
{
QMutexLocker lk(&mutex);
return myHash.count();
}
/*!
* \brief clear 清理整个map
*
* \return
*/
inline void clear()
{
QMutexLocker lk(&mutex);
return myHash.clear();
}

inline QList<DKey> keys()
{
QMutexLocker lk(&mutex);
return myHash.keys();
}

inline QHash<DKey, DValue> hash() const
{
QMutexLocker lk(&mutex);
return myHash;
}

private:
QHash<DKey, DValue> myHash; // 当前的QMap
QMutex mutable mutex; // 当前的锁
};
}
#endif // THREADCONTAINER_H
3 changes: 2 additions & 1 deletion include/dfm-base/utils/watchercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WatcherCache : public QObject
friend InfoCache;
Q_SIGNALS:
void fileDelete(const QUrl &url);
void updateWatcherTime(const QList<QUrl> &url, const bool add);

public:
static WatcherCache &instance();
Expand All @@ -31,7 +32,7 @@ class WatcherCache : public QObject
QSharedPointer<AbstractFileWatcher> getCacheWatcher(const QUrl &url);

void cacheWatcher(const QUrl &url, const QSharedPointer<AbstractFileWatcher> &watcher);
void removeCacheWatcher(const QUrl &url);
void removeCacheWatcher(const QUrl &url, const bool isEmit = true);
void removeCacheWatcherByParent(const QUrl &parent);
bool cacheDisable(const QString &scheme);
void setCacheDisbale(const QString &scheme, bool disbale = true);
Expand Down
Loading

0 comments on commit 7b0aeb7

Please sign in to comment.