MythTV  master
autoexpire.cpp
Go to the documentation of this file.
1 // System headers
2 #include <sys/stat.h>
3 #ifdef __linux__
4 # include <sys/vfs.h>
5 #else // if !__linux__
6 # include <sys/param.h>
7 # ifndef _WIN32
8 # include <sys/mount.h>
9 # endif // _WIN32
10 #endif // !__linux__
11 
12 // POSIX headers
13 #include <unistd.h>
14 
15 // C headers
16 #include <cstdlib>
17 
18 // C++ headers
19 #include <iostream>
20 #include <algorithm>
21 
22 // Qt headers
23 #include <QDateTime>
24 #include <QFileInfo>
25 #include <QList>
26 
27 // MythTV headers
28 #include "filesysteminfo.h"
29 #include "autoexpire.h"
30 #include "programinfo.h"
31 #include "mythcorecontext.h"
32 #include "mythdb.h"
33 #include "mythdate.h"
34 #include "storagegroup.h"
35 #include "remoteutil.h"
36 #include "remoteencoder.h"
37 #include "encoderlink.h"
39 #include "mainserver.h"
40 #include "compat.h"
41 #include "mythlogging.h"
42 #include "tv_rec.h"
43 
44 #define LOC QString("AutoExpire: ")
45 #define LOC_ERR QString("AutoExpire Error: ")
46 
47 extern AutoExpire *expirer;
48 
52 #define SPACE_TOO_BIG_KB (3*1024*1024)
53 
56 {
57  RunProlog();
58  m_parent->RunExpirer();
59  RunEpilog();
60 }
61 
71 AutoExpire::AutoExpire(QMap<int, EncoderLink *> *tvList) :
72  m_encoderList(tvList),
73  m_expireThread(new ExpireThread(this)),
74  m_expireThreadRun(true)
75 {
78 }
79 
84 {
85  {
86  QMutexLocker locker(&m_instanceLock);
87  m_expireThreadRun = false;
88  m_instanceCond.wakeAll();
89  }
90 
91  {
92  QMutexLocker locker(&m_updateLock);
93  m_updateQueue.clear();
94  }
95 
96  if (m_expireThread)
97  {
100  delete m_expireThread;
101  m_expireThread = nullptr;
102  }
103 }
104 
110 uint64_t AutoExpire::GetDesiredSpace(int fsID) const
111 {
112  QMutexLocker locker(&m_instanceLock);
113  if (m_desiredSpace.contains(fsID))
114  return m_desiredSpace[fsID];
115  return 0;
116 }
117 
122 {
123  LOG(VB_FILE, LOG_INFO, LOC + "CalcParams()");
124 
125  QList<FileSystemInfo> fsInfos;
126 
127  m_instanceLock.lock();
128  if (m_mainServer)
129  {
130  // The scheduler relies on something forcing the mainserver
131  // fsinfos cache to get updated periodically. Currently, that
132  // is done here. Don't remove or change this invocation
133  // without handling that issue too. It is done this way
134  // because the scheduler thread can't afford to be blocked by
135  // an unresponsive, remote filesystem and the autoexpirer
136  // thread can.
137  m_mainServer->GetFilesystemInfos(fsInfos, false);
138  }
139  m_instanceLock.unlock();
140 
141  if (fsInfos.empty())
142  {
143  LOG(VB_GENERAL, LOG_ERR, LOC + "Filesystem Info cache is empty, unable "
144  "to calculate necessary parameters.");
145  return;
146  }
147 
148  uint64_t maxKBperMin = 0;
149  uint64_t extraKB = static_cast<uint64_t>
150  (gCoreContext->GetNumSetting("AutoExpireExtraSpace", 0))
151  << 20;
152 
153  QMap<int, uint64_t> fsMap;
154  QMap<int, vector<int> > fsEncoderMap;
155 
156  // We use this copying on purpose. The used_encoders map ensures
157  // that every encoder writes only to one fs.
158  // Copying the data minimizes the time the lock is held.
159  m_instanceLock.lock();
160  QMap<int, int>::const_iterator ueit = m_usedEncoders.cbegin();
161  while (ueit != m_usedEncoders.cend())
162  {
163  fsEncoderMap[*ueit].push_back(ueit.key());
164  ++ueit;
165  }
166  m_instanceLock.unlock();
167 
168  QList<FileSystemInfo>::iterator fsit;
169  for (fsit = fsInfos.begin(); fsit != fsInfos.end(); ++fsit)
170  {
171  if (fsMap.contains(fsit->getFSysID()))
172  continue;
173 
174  fsMap[fsit->getFSysID()] = 0;
175  uint64_t thisKBperMin = 0;
176 
177  // append unknown recordings to all fsIDs
178  for (auto unknownfs : qAsConst(fsEncoderMap[-1]))
179  fsEncoderMap[fsit->getFSysID()].push_back(unknownfs);
180 
181  if (fsEncoderMap.contains(fsit->getFSysID()))
182  {
183  LOG(VB_FILE, LOG_INFO,
184  QString("fsID #%1: Total: %2 GB Used: %3 GB Free: %4 GB")
185  .arg(fsit->getFSysID())
186  .arg(fsit->getTotalSpace() / 1024.0 / 1024.0, 7, 'f', 1)
187  .arg(fsit->getUsedSpace() / 1024.0 / 1024.0, 7, 'f', 1)
188  .arg(fsit->getFreeSpace() / 1024.0 / 1024.0, 7, 'f', 1));
189 
190  for (auto cardid : qAsConst(fsEncoderMap[fsit->getFSysID()]))
191  {
192  EncoderLink *enc = *(m_encoderList->constFind(cardid));
193 
194  if (!enc->IsConnected() || !enc->IsBusy())
195  {
196  // remove encoder since it can't write to any file system
197  LOG(VB_FILE, LOG_INFO, LOC +
198  QString("Cardid %1: is not recording, removing it "
199  "from used list.").arg(cardid));
200  m_instanceLock.lock();
201  m_usedEncoders.remove(cardid);
202  m_instanceLock.unlock();
203  continue;
204  }
205 
206  uint64_t maxBitrate = enc->GetMaxBitrate();
207  if (maxBitrate==0)
208  maxBitrate = 19500000LL;
209  thisKBperMin += (maxBitrate*((uint64_t)15))>>11;
210  LOG(VB_FILE, LOG_INFO, QString(" Cardid %1: max bitrate "
211  "%2 Kb/sec, fsID %3 max is now %4 KB/min")
212  .arg(enc->GetInputID())
213  .arg(enc->GetMaxBitrate() >> 10)
214  .arg(fsit->getFSysID())
215  .arg(thisKBperMin));
216  }
217  }
218  fsMap[fsit->getFSysID()] = thisKBperMin;
219 
220  if (thisKBperMin > maxKBperMin)
221  {
222  LOG(VB_FILE, LOG_INFO,
223  QString(" Max of %1 KB/min for fsID %2 is higher "
224  "than the existing Max of %3 so we'll use this Max instead")
225  .arg(thisKBperMin).arg(fsit->getFSysID()).arg(maxKBperMin));
226  maxKBperMin = thisKBperMin;
227  }
228  }
229 
230  // Determine frequency to run autoexpire so it doesn't have to free
231  // too much space
232  uint expireFreq = 15;
233  if (maxKBperMin > 0)
234  {
235  expireFreq = SPACE_TOO_BIG_KB / (maxKBperMin + maxKBperMin/3);
236  expireFreq = std::max(3U, std::min(expireFreq, 15U));
237  }
238 
239  double expireMinGB = ((maxKBperMin + maxKBperMin/3)
240  * expireFreq + extraKB) >> 20;
241  LOG(VB_GENERAL, LOG_NOTICE, LOC +
242  QString("CalcParams(): Max required Free Space: %1 GB w/freq: %2 min")
243  .arg(expireMinGB, 0, 'f', 1).arg(expireFreq));
244 
245  // lock class and save these parameters.
246  m_instanceLock.lock();
247  m_desiredFreq = expireFreq;
248  // write per file system needed space back, use safety of 33%
249  QMap<int, uint64_t>::iterator it = fsMap.begin();
250  while (it != fsMap.end())
251  {
252  m_desiredSpace[it.key()] = (*it + *it/3) * expireFreq + extraKB;
253  ++it;
254  }
255  m_instanceLock.unlock();
256 }
257 
267 {
268  QElapsedTimer timer;
269  QDateTime curTime;
270  QDateTime next_expire = MythDate::current().addSecs(60);
271 
272  QMutexLocker locker(&m_instanceLock);
273 
274  // wait a little for main server to come up and things to settle down
275  Sleep(20 * 1000);
276 
277  timer.start();
278 
279  while (m_expireThreadRun)
280  {
281  TVRec::s_inputsLock.lockForRead();
282 
283  curTime = MythDate::current();
284  // recalculate auto expire parameters
285  if (curTime >= next_expire)
286  {
287  m_updateLock.lock();
288  while (!m_updateQueue.empty())
289  {
290  UpdateEntry ue = m_updateQueue.dequeue();
291  if (ue.m_encoder > 0)
293  }
294  m_updateLock.unlock();
295 
296  locker.unlock();
297  CalcParams();
298  locker.relock();
299  if (!m_expireThreadRun)
300  break;
301  }
302  timer.restart();
303 
305 
306  // Expire Short LiveTV files for this backend every 2 minutes
307  if ((curTime.time().minute() % 2) == 0)
309 
310  // Expire normal recordings depending on frequency calculated
311  if (curTime >= next_expire)
312  {
313  LOG(VB_FILE, LOG_INFO, LOC + "Running now!");
314  next_expire =
315  MythDate::current().addSecs(m_desiredFreq * 60);
316 
318 
319  int maxAge = gCoreContext->GetNumSetting("DeletedMaxAge", 0);
320  if (maxAge > 0)
322  else if (maxAge == 0)
324 
326 
328  }
329 
330  TVRec::s_inputsLock.unlock();
331 
332  Sleep(60 * 1000 - timer.elapsed());
333  }
334 }
335 
342 void AutoExpire::Sleep(int sleepTime)
343 {
344  if (sleepTime <= 0)
345  return;
346 
347  QDateTime little_tm = MythDate::current().addMSecs(sleepTime);
348  int timeleft = sleepTime;
349  while (m_expireThreadRun && (timeleft > 0))
350  {
351  m_instanceCond.wait(&m_instanceLock, timeleft);
352  timeleft = MythDate::current().secsTo(little_tm) * 1000;
353  }
354 }
355 
360 {
361  pginfolist_t expireList;
362 
363  LOG(VB_FILE, LOG_INFO, LOC + QString("ExpireLiveTV(%1)").arg(type));
364  FillDBOrdered(expireList, type);
365  SendDeleteMessages(expireList);
366  ClearExpireList(expireList);
367 }
368 
373 {
374  pginfolist_t expireList;
375 
376  LOG(VB_FILE, LOG_INFO, LOC + QString("ExpireOldDeleted()"));
377  FillDBOrdered(expireList, emOldDeletedPrograms);
378  SendDeleteMessages(expireList);
379  ClearExpireList(expireList);
380 }
381 
386 {
387  pginfolist_t expireList;
388 
389  LOG(VB_FILE, LOG_INFO, LOC + QString("ExpireQuickDeleted()"));
391  SendDeleteMessages(expireList);
392  ClearExpireList(expireList);
393 }
394 
400 {
401  pginfolist_t expireList;
402  pginfolist_t deleteList;
403  QList<FileSystemInfo> fsInfos;
404  QList<FileSystemInfo>::iterator fsit;
405 
406  LOG(VB_FILE, LOG_INFO, LOC + "ExpireRecordings()");
407 
408  if (m_mainServer)
409  m_mainServer->GetFilesystemInfos(fsInfos, true);
410 
411  if (fsInfos.empty())
412  {
413  LOG(VB_GENERAL, LOG_ERR, LOC + "Filesystem Info cache is empty, unable "
414  "to determine what Recordings to expire");
415 
416  return;
417  }
418 
419  FillExpireList(expireList);
420 
421  QMap <int, bool> truncateMap;
423  query.prepare("SELECT DISTINCT rechost, recdir "
424  "FROM inuseprograms "
425  "WHERE recusage = 'truncatingdelete' "
426  "AND lastupdatetime > DATE_ADD(NOW(), INTERVAL -2 MINUTE);");
427 
428  if (!query.exec())
429  {
430  MythDB::DBError(LOC + "ExpireRecordings", query);
431  }
432  else
433  {
434  while (query.next())
435  {
436  QString rechost = query.value(0).toString();
437  QString recdir = query.value(1).toString();
438 
439  LOG(VB_FILE, LOG_INFO, LOC +
440  QString("%1:%2 has an in-progress truncating delete.")
441  .arg(rechost).arg(recdir));
442 
443  for (fsit = fsInfos.begin(); fsit != fsInfos.end(); ++fsit)
444  {
445  if ((fsit->getHostname() == rechost) &&
446  (fsit->getPath() == recdir))
447  {
448  truncateMap[fsit->getFSysID()] = true;
449  break;
450  }
451  }
452  }
453  }
454 
455  QMap <int, bool> fsMap;
456  for (fsit = fsInfos.begin(); fsit != fsInfos.end(); ++fsit)
457  {
458  if (fsMap.contains(fsit->getFSysID()))
459  continue;
460 
461  fsMap[fsit->getFSysID()] = true;
462 
463  LOG(VB_FILE, LOG_INFO,
464  QString("fsID #%1: Total: %2 GB Used: %3 GB Free: %4 GB")
465  .arg(fsit->getFSysID())
466  .arg(fsit->getTotalSpace() / 1024.0 / 1024.0, 7, 'f', 1)
467  .arg(fsit->getUsedSpace() / 1024.0 / 1024.0, 7, 'f', 1)
468  .arg(fsit->getFreeSpace() / 1024.0 / 1024.0, 7, 'f', 1));
469 
470  if ((fsit->getTotalSpace() == -1) || (fsit->getUsedSpace() == -1))
471  {
472  LOG(VB_FILE, LOG_ERR, LOC +
473  QString("fsID #%1 has invalid info, AutoExpire cannot run for "
474  "this filesystem. Continuing on to next...")
475  .arg(fsit->getFSysID()));
476  LOG(VB_FILE, LOG_INFO, QString("Directories on filesystem ID %1:")
477  .arg(fsit->getFSysID()));
478  QList<FileSystemInfo>::iterator fsit2;
479  for (fsit2 = fsInfos.begin(); fsit2 != fsInfos.end(); ++fsit2)
480  {
481  if (fsit2->getFSysID() == fsit->getFSysID())
482  {
483  LOG(VB_FILE, LOG_INFO, QString(" %1:%2")
484  .arg(fsit2->getHostname()).arg(fsit2->getPath()));
485  }
486  }
487 
488  continue;
489  }
490 
491  if (truncateMap.contains(fsit->getFSysID()))
492  {
493  LOG(VB_FILE, LOG_INFO,
494  QString(" fsid %1 has a truncating delete in progress, "
495  "AutoExpire cannot run for this filesystem until the "
496  "delete has finished. Continuing on to next...")
497  .arg(fsit->getFSysID()));
498  continue;
499  }
500 
501  if (std::max((int64_t)0LL, fsit->getFreeSpace()) <
502  m_desiredSpace[fsit->getFSysID()])
503  {
504  LOG(VB_FILE, LOG_INFO,
505  QString(" Not Enough Free Space! We want %1 MB")
506  .arg(m_desiredSpace[fsit->getFSysID()] / 1024));
507 
508  QMap<QString, int> dirList;
509  QList<FileSystemInfo>::iterator fsit2;
510 
511  LOG(VB_FILE, LOG_INFO,
512  QString(" Directories on filesystem ID %1:")
513  .arg(fsit->getFSysID()));
514 
515  for (fsit2 = fsInfos.begin(); fsit2 != fsInfos.end(); ++fsit2)
516  {
517  if (fsit2->getFSysID() == fsit->getFSysID())
518  {
519  LOG(VB_FILE, LOG_INFO, QString(" %1:%2")
520  .arg(fsit2->getHostname()).arg(fsit2->getPath()));
521  dirList[fsit2->getHostname() + ":" + fsit2->getPath()] = 1;
522  }
523  }
524 
525  LOG(VB_FILE, LOG_INFO,
526  " Searching for files expirable in these directories");
527  QString myHostName = gCoreContext->GetHostName();
528  auto it = expireList.begin();
529  while ((it != expireList.end()) &&
530  (std::max((int64_t)0LL, fsit->getFreeSpace()) <
531  m_desiredSpace[fsit->getFSysID()]))
532  {
533  ProgramInfo *p = *it;
534  ++it;
535 
536  LOG(VB_FILE, LOG_INFO, QString(" Checking %1 => %2")
537  .arg(p->toString(ProgramInfo::kRecordingKey))
538  .arg(p->GetTitle()));
539 
540  if (!p->IsLocal())
541  {
542  bool foundFile = false;
543  auto eit = m_encoderList->constBegin();
544  while (eit != m_encoderList->constEnd())
545  {
546  EncoderLink *el = *eit;
547  eit++;
548 
549  if ((p->GetHostname() == el->GetHostName()) ||
550  ((p->GetHostname() == myHostName) &&
551  (el->IsLocal())))
552  {
553  if (el->IsConnected())
554  foundFile = el->CheckFile(p);
555 
556  eit = m_encoderList->constEnd();
557  }
558  }
559 
560  if (!foundFile && (p->GetHostname() != myHostName))
561  {
562  // Wasn't found so check locally
563  QString file = GetPlaybackURL(p);
564 
565  if (file.startsWith("/"))
566  {
567  p->SetPathname(file);
568  p->SetHostname(myHostName);
569  foundFile = true;
570  }
571  }
572 
573  if (!foundFile)
574  {
575  LOG(VB_FILE, LOG_ERR, LOC +
576  QString(" ERROR: Can't find file for %1")
577  .arg(p->toString(ProgramInfo::kRecordingKey)));
578  continue;
579  }
580  }
581 
582  QFileInfo vidFile(p->GetPathname());
583  if (dirList.contains(p->GetHostname() + ':' + vidFile.path()))
584  {
585  fsit->setUsedSpace(fsit->getUsedSpace()
586  - (p->GetFilesize() / 1024));
587  deleteList.push_back(p);
588 
589  LOG(VB_FILE, LOG_INFO,
590  QString(" FOUND file expirable. "
591  "%1 is located at %2 which is on fsID #%3. "
592  "Adding to deleteList. After deleting we "
593  "should have %4 MB free on this filesystem.")
594  .arg(p->toString(ProgramInfo::kRecordingKey))
595  .arg(p->GetPathname()).arg(fsit->getFSysID())
596  .arg(fsit->getFreeSpace() / 1024));
597  }
598  }
599  }
600  }
601 
602  SendDeleteMessages(deleteList);
603 
604  ClearExpireList(deleteList, false);
605  ClearExpireList(expireList);
606 }
607 
612 {
613  QString msg;
614 
615  if (deleteList.empty())
616  {
617  LOG(VB_FILE, LOG_INFO, LOC + "SendDeleteMessages. Nothing to expire.");
618  return;
619  }
620 
621  LOG(VB_FILE, LOG_INFO, LOC +
622  "SendDeleteMessages, cycling through deleteList.");
623  auto it = deleteList.begin();
624  while (it != deleteList.end())
625  {
626  msg = QString("%1Expiring %2 MB for %3 => %4")
627  .arg(VERBOSE_LEVEL_CHECK(VB_FILE, LOG_ANY) ? " " : "")
628  .arg(((*it)->GetFilesize() >> 20))
629  .arg((*it)->toString(ProgramInfo::kRecordingKey))
630  .arg((*it)->toString(ProgramInfo::kTitleSubtitle));
631 
632  LOG(VB_GENERAL, LOG_NOTICE, msg);
633 
634  // send auto expire message to backend's event thread.
635  MythEvent me(QString("AUTO_EXPIRE %1 %2").arg((*it)->GetChanID())
636  .arg((*it)->GetRecordingStartTime(MythDate::ISODate)));
637  gCoreContext->dispatch(me);
638 
639  ++it; // move on to next program
640  }
641 }
642 
649 {
650  QMap<QString, int> maxEpisodes;
651  QMap<QString, int>::Iterator maxIter;
652  QMap<QString, int> episodeParts;
653  QString episodeKey;
654 
656  query.prepare("SELECT recordid, maxepisodes, title "
657  "FROM record WHERE maxepisodes > 0 "
658  "ORDER BY recordid ASC, maxepisodes DESC");
659 
660  if (query.exec() && query.isActive() && query.size() > 0)
661  {
662  LOG(VB_FILE, LOG_INFO, LOC +
663  QString("Found %1 record profiles using max episode expiration")
664  .arg(query.size()));
665  while (query.next())
666  {
667  LOG(VB_FILE, LOG_INFO, QString(" %1 (%2 for rec id %3)")
668  .arg(query.value(2).toString())
669  .arg(query.value(1).toInt())
670  .arg(query.value(0).toInt()));
671  maxEpisodes[query.value(0).toString()] = query.value(1).toInt();
672  }
673  }
674 
675  LOG(VB_FILE, LOG_INFO, LOC +
676  "Checking episode count for each recording profile using max episodes");
677  for (maxIter = maxEpisodes.begin(); maxIter != maxEpisodes.end(); ++maxIter)
678  {
679  query.prepare("SELECT chanid, starttime, title, progstart, progend, "
680  "duplicate "
681  "FROM recorded "
682  "WHERE recordid = :RECID AND preserve = 0 "
683  "AND recgroup NOT IN ('LiveTV', 'Deleted') "
684  "ORDER BY starttime DESC;");
685  query.bindValue(":RECID", maxIter.key());
686 
687  if (!query.exec() || !query.isActive())
688  {
689  MythDB::DBError("AutoExpire query failed!", query);
690  continue;
691  }
692 
693  LOG(VB_FILE, LOG_INFO, QString(" Recordid %1 has %2 recordings.")
694  .arg(maxIter.key())
695  .arg(query.size()));
696  if (query.size() > 0)
697  {
698  int found = 1;
699  while (query.next())
700  {
701  uint chanid = query.value(0).toUInt();
702  QDateTime startts = MythDate::as_utc(query.value(1).toDateTime());
703  QString title = query.value(2).toString();
704  QDateTime progstart = MythDate::as_utc(query.value(3).toDateTime());
705  QDateTime progend = MythDate::as_utc(query.value(4).toDateTime());
706  int duplicate = query.value(5).toInt();
707 
708  episodeKey = QString("%1_%2_%3")
709  .arg(chanid)
710  .arg(progstart.toString(Qt::ISODate))
711  .arg(progend.toString(Qt::ISODate));
712 
713  if ((!IsInDontExpireSet(chanid, startts)) &&
714  (!episodeParts.contains(episodeKey)) &&
715  (found > *maxIter))
716  {
717  QString msg =
718  QString("%1Deleting %2 at %3 => %4. "
719  "Too many episodes, we only want to keep %5.")
720  .arg(VERBOSE_LEVEL_CHECK(VB_FILE, LOG_ANY) ?
721  " " : "")
722  .arg(chanid).arg(startts.toString(Qt::ISODate))
723  .arg(title).arg(*maxIter);
724 
725  LOG(VB_GENERAL, LOG_NOTICE, msg);
726 
727  // allow re-record if auto expired
728  RecordingInfo recInfo(chanid, startts);
729  if (gCoreContext->GetBoolSetting("RerecordWatched", false) ||
730  !recInfo.IsWatched())
731  {
732  recInfo.ForgetHistory();
733  }
734  msg = QString("DELETE_RECORDING %1 %2")
735  .arg(chanid)
736  .arg(startts.toString(Qt::ISODate));
737 
738  MythEvent me(msg);
739  gCoreContext->dispatch(me);
740  }
741  else
742  {
743  // keep track of shows we haven't expired so we can
744  // make sure we don't expire another part of the same
745  // episode.
746  if (episodeParts.contains(episodeKey))
747  {
748  episodeParts[episodeKey] = episodeParts[episodeKey] + 1;
749  }
750  else
751  {
752  episodeParts[episodeKey] = 1;
753  if( duplicate )
754  found++;
755  }
756  }
757  }
758  }
759  }
760 }
761 
767 {
768  int expMethod = gCoreContext->GetNumSetting("AutoExpireMethod", 1);
769 
770  ClearExpireList(expireList);
771 
773 
774  switch(expMethod)
775  {
776  case emOldestFirst:
779  FillDBOrdered(expireList, expMethod);
780  break;
781  // default falls through so list is empty so no AutoExpire
782  }
783 }
784 
788 void AutoExpire::PrintExpireList(const QString& expHost)
789 {
790  pginfolist_t expireList;
791 
792  FillExpireList(expireList);
793 
794  QString msg = "MythTV AutoExpire List ";
795  if (expHost != "ALL")
796  msg += QString("for '%1' ").arg(expHost);
797  msg += "(programs listed in order of expiration)";
798  std::cout << msg.toLocal8Bit().constData() << std::endl;
799 
800  for (auto *first : expireList)
801  {
802  if (expHost != "ALL" && first->GetHostname() != expHost)
803  continue;
804 
805  QString title = first->toString(ProgramInfo::kTitleSubtitle);
806  title = title.leftJustified(39, ' ', true);
807 
808  QString outstr = QString("%1 %2 MB %3 [%4]")
809  .arg(title)
810  .arg(QString::number(first->GetFilesize() >> 20)
811  .rightJustified(5, ' ', true))
812  .arg(first->GetRecordingStartTime(MythDate::ISODate)
813  .leftJustified(24, ' ', true))
814  .arg(QString::number(first->GetRecordingPriority())
815  .rightJustified(3, ' ', true));
816  QByteArray out = outstr.toLocal8Bit();
817 
818  std::cout << out.constData() << std::endl;
819  }
820 
821  ClearExpireList(expireList);
822 }
823 
827 void AutoExpire::GetAllExpiring(QStringList &strList)
828 {
829  QMutexLocker lockit(&m_instanceLock);
830  pginfolist_t expireList;
831 
833 
837  FillDBOrdered(expireList, gCoreContext->GetNumSetting("AutoExpireMethod",
838  emOldestFirst));
839 
840  strList << QString::number(expireList.size());
841 
842  for (auto & info : expireList)
843  info->ToStringList(strList);
844 
845  ClearExpireList(expireList);
846 }
847 
852 {
853  QMutexLocker lockit(&m_instanceLock);
854  pginfolist_t expireList;
855 
857 
861  FillDBOrdered(expireList, gCoreContext->GetNumSetting("AutoExpireMethod",
862  emOldestFirst));
863 
864  for (auto & info : expireList)
865  list.push_back( new ProgramInfo( *info ));
866 
867  ClearExpireList(expireList);
868 }
869 
873 void AutoExpire::ClearExpireList(pginfolist_t &expireList, bool deleteProg)
874 {
875  ProgramInfo *pginfo = nullptr;
876  while (!expireList.empty())
877  {
878  if (deleteProg)
879  pginfo = expireList.back();
880 
881  expireList.pop_back();
882 
883  if (deleteProg)
884  delete pginfo;
885  }
886 }
887 
892 void AutoExpire::FillDBOrdered(pginfolist_t &expireList, int expMethod)
893 {
894  QString where;
895  QString orderby;
896  QString msg;
897  int maxAge = 0;
898 
899  switch (expMethod)
900  {
901  default:
902  case emOldestFirst:
903  msg = "Adding programs expirable in Oldest First order";
904  where = "autoexpire > 0";
905  if (gCoreContext->GetBoolSetting("AutoExpireWatchedPriority", false))
906  orderby = "recorded.watched DESC, ";
907  orderby += "starttime ASC";
908  break;
910  msg = "Adding programs expirable in Lowest Priority First order";
911  where = "autoexpire > 0";
912  if (gCoreContext->GetBoolSetting("AutoExpireWatchedPriority", false))
913  orderby = "recorded.watched DESC, ";
914  orderby += "recorded.recpriority ASC, starttime ASC";
915  break;
917  msg = "Adding programs expirable in Weighted Time Priority order";
918  where = "autoexpire > 0";
919  if (gCoreContext->GetBoolSetting("AutoExpireWatchedPriority", false))
920  orderby = "recorded.watched DESC, ";
921  orderby += QString("DATE_ADD(starttime, INTERVAL '%1' * "
922  "recorded.recpriority DAY) ASC")
923  .arg(gCoreContext->GetNumSetting("AutoExpireDayPriority", 3));
924  break;
926  msg = "Adding Short LiveTV programs in starttime order";
927  where = "recgroup = 'LiveTV' "
928  "AND endtime < DATE_ADD(starttime, INTERVAL '30' SECOND) "
929  "AND endtime <= DATE_ADD(NOW(), INTERVAL '-5' MINUTE) ";
930  orderby = "starttime ASC";
931  break;
933  msg = "Adding LiveTV programs in starttime order";
934  where = QString("recgroup = 'LiveTV' "
935  "AND endtime <= DATE_ADD(NOW(), INTERVAL '-%1' DAY) ")
936  .arg(gCoreContext->GetNumSetting("AutoExpireLiveTVMaxAge", 1));
937  orderby = "starttime ASC";
938  break;
940  if ((maxAge = gCoreContext->GetNumSetting("DeletedMaxAge", 0)) <= 0)
941  return;
942  msg = QString("Adding programs deleted more than %1 days ago")
943  .arg(maxAge);
944  where = QString("recgroup = 'Deleted' "
945  "AND lastmodified <= DATE_ADD(NOW(), INTERVAL '-%1' DAY) ")
946  .arg(maxAge);
947  orderby = "starttime ASC";
948  break;
950  if (gCoreContext->GetNumSetting("DeletedMaxAge", 0) != 0)
951  return;
952  msg = QString("Adding programs deleted more than 5 minutes ago");
953  where = QString("recgroup = 'Deleted' "
954  "AND lastmodified <= DATE_ADD(NOW(), INTERVAL '-5' MINUTE) ");
955  orderby = "lastmodified ASC";
956  break;
958  msg = "Adding deleted programs in FIFO order";
959  where = "recgroup = 'Deleted'";
960  orderby = "lastmodified ASC";
961  break;
962  }
963 
964  LOG(VB_FILE, LOG_INFO, LOC + "FillDBOrdered: " + msg);
965 
967  QString querystr = QString(
968  "SELECT recorded.chanid, starttime "
969  "FROM recorded "
970  "LEFT JOIN channel ON recorded.chanid = channel.chanid "
971  "WHERE %1 AND deletepending = 0 "
972  "ORDER BY autoexpire DESC, %2").arg(where).arg(orderby);
973 
974  query.prepare(querystr);
975 
976  if (!query.exec())
977  return;
978 
979  while (query.next())
980  {
981  uint chanid = query.value(0).toUInt();
982  QDateTime recstartts = MythDate::as_utc(query.value(1).toDateTime());
983 
984  if (IsInDontExpireSet(chanid, recstartts))
985  {
986  LOG(VB_FILE, LOG_INFO, LOC +
987  QString(" Skipping %1 at %2 because it is in Don't Expire "
988  "List")
989  .arg(chanid).arg(recstartts.toString(Qt::ISODate)));
990  }
991  else if (IsInExpireList(expireList, chanid, recstartts))
992  {
993  LOG(VB_FILE, LOG_INFO, LOC +
994  QString(" Skipping %1 at %2 because it is already in Expire "
995  "List")
996  .arg(chanid).arg(recstartts.toString(Qt::ISODate)));
997  }
998  else
999  {
1000  auto *pginfo = new ProgramInfo(chanid, recstartts);
1001  if (pginfo->GetChanID())
1002  {
1003  LOG(VB_FILE, LOG_INFO, LOC + QString(" Adding %1 at %2")
1004  .arg(chanid).arg(recstartts.toString(Qt::ISODate)));
1005  expireList.push_back(pginfo);
1006  }
1007  else
1008  {
1009  LOG(VB_FILE, LOG_INFO, LOC +
1010  QString(" Skipping %1 at %2 "
1011  "because it could not be loaded from the DB")
1012  .arg(chanid).arg(recstartts.toString(Qt::ISODate)));
1013  delete pginfo;
1014  }
1015  }
1016  }
1017 }
1018 
1030 void AutoExpire::Update(int encoder, int fsID, bool immediately)
1031 {
1032  if (!expirer)
1033  return;
1034 
1035  if (encoder > 0)
1036  {
1037  QString msg = QString("Cardid %1: is starting a recording on")
1038  .arg(encoder);
1039  if (fsID == -1)
1040  msg.append(" an unknown fsID soon.");
1041  else
1042  msg.append(QString(" fsID %2 soon.").arg(fsID));
1043  LOG(VB_FILE, LOG_INFO, LOC + msg);
1044  }
1045 
1046  if (immediately)
1047  {
1048  if (encoder > 0)
1049  {
1050  expirer->m_instanceLock.lock();
1051  expirer->m_usedEncoders[encoder] = fsID;
1052  expirer->m_instanceLock.unlock();
1053  }
1054  expirer->CalcParams();
1055  expirer->m_instanceCond.wakeAll();
1056  }
1057  else
1058  {
1059  expirer->m_updateLock.lock();
1060  expirer->m_updateQueue.append(UpdateEntry(encoder, fsID));
1061  expirer->m_updateLock.unlock();
1062  }
1063 }
1064 
1066 {
1067  m_dontExpireSet.clear();
1068 
1070  query.prepare(
1071  "SELECT chanid, starttime, lastupdatetime, recusage, hostname "
1072  "FROM inuseprograms");
1073 
1074  if (!query.exec() || !query.next())
1075  return;
1076 
1077  LOG(VB_FILE, LOG_INFO, LOC + "Adding Programs to 'Do Not Expire' List");
1078  QDateTime curTime = MythDate::current();
1079 
1080  do
1081  {
1082  uint chanid = query.value(0).toUInt();
1083  QDateTime recstartts = MythDate::as_utc(query.value(1).toDateTime());
1084  QDateTime lastupdate = MythDate::as_utc(query.value(2).toDateTime());
1085 
1086  if (lastupdate.secsTo(curTime) < 2 * 60 * 60)
1087  {
1088  QString key = QString("%1_%2")
1089  .arg(chanid).arg(recstartts.toString(Qt::ISODate));
1090  m_dontExpireSet.insert(key);
1091  LOG(VB_FILE, LOG_INFO, QString(" %1 at %2 in use by %3 on %4")
1092  .arg(chanid)
1093  .arg(recstartts.toString(Qt::ISODate))
1094  .arg(query.value(3).toString())
1095  .arg(query.value(4).toString()));
1096  }
1097  }
1098  while (query.next());
1099 }
1100 
1102  uint chanid, const QDateTime &recstartts) const
1103 {
1104  QString key = QString("%1_%2")
1105  .arg(chanid).arg(recstartts.toString(Qt::ISODate));
1106 
1107  return (m_dontExpireSet.find(key) != m_dontExpireSet.end());
1108 }
1109 
1111  const pginfolist_t &expireList, uint chanid, const QDateTime &recstartts)
1112 {
1113  return std::any_of(expireList.cbegin(), expireList.cend(),
1114  [chanid,&recstartts](auto *info)
1115  { return ((info->GetChanID() == chanid) &&
1116  (info->GetRecordingStartTime() == recstartts)); } );
1117 }
1118 
1119 /* vim: set expandtab tabstop=4 shiftwidth=4: */
filesysteminfo.h
AutoExpire::m_instanceCond
QWaitCondition m_instanceCond
Definition: autoexpire.h:120
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:204
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:203
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:288
AutoExpire::PrintExpireList
void PrintExpireList(const QString &expHost="ALL")
Prints a summary of the files that can be deleted.
Definition: autoexpire.cpp:788
mythdb.h
MainServer::GetFilesystemInfos
void GetFilesystemInfos(QList< FileSystemInfo > &fsInfos, bool useCache=true)
Definition: mainserver.cpp:5359
MythDate::as_utc
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:23
AutoExpire::IsInExpireList
static bool IsInExpireList(const pginfolist_t &expireList, uint chanid, const QDateTime &recstartts)
Definition: autoexpire.cpp:1110
AutoExpire::Update
static void Update(int encoder, int fsID, bool immediately)
This is used to update the global AutoExpire instance "expirer".
Definition: autoexpire.cpp:1030
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:636
ExpireThread::m_parent
QPointer< AutoExpire > m_parent
Definition: autoexpire.h:47
RecordingInfo
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:35
AutoExpire::ClearExpireList
static void ClearExpireList(pginfolist_t &expireList, bool deleteProg=true)
Clears expireList, freeing any ProgramInfo's if necessary.
Definition: autoexpire.cpp:873
RecordingInfo::ForgetHistory
void ForgetHistory(void)
Forget the recording of a program so it will be recorded again.
Definition: recordinginfo.cpp:1378
tvList
QMap< int, EncoderLink * > tvList
Definition: backendcontext.cpp:7
AutoExpire::Sleep
void Sleep(int sleepTime)
Sleeps for sleepTime milliseconds; unless the expire thread is told to quit.
Definition: autoexpire.cpp:342
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:17
ProgramInfo::kTitleSubtitle
@ kTitleSubtitle
Definition: programinfo.h:500
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
arg
arg(title).arg(filename).arg(doDelete))
AutoExpire
Used to expire recordings to make space for new recordings.
Definition: autoexpire.h:61
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
AutoExpire::ExpireOldDeleted
void ExpireOldDeleted(void)
This expires deleted programs older than DeletedMaxAge.
Definition: autoexpire.cpp:372
emLowestPriorityFirst
@ emLowestPriorityFirst
Definition: autoexpire.h:29
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MThread::RunProlog
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:198
AutoExpire::ExpireEpisodesOverMax
void ExpireEpisodesOverMax(void)
This deletes programs exceeding the maximum number of episodes of that program desired.
Definition: autoexpire.cpp:648
AutoExpire::GetAllExpiring
void GetAllExpiring(QStringList &strList)
Gets the full list of programs that can expire in expiration order.
Definition: autoexpire.cpp:827
ExpireThread::run
void run(void) override
This calls AutoExpire::RunExpirer() from within a new thread.
Definition: autoexpire.cpp:55
AutoExpire::m_desiredSpace
QMap< int, int64_t > m_desiredSpace
Definition: autoexpire.h:116
build_compdb.file
file
Definition: build_compdb.py:55
AutoExpire::m_updateLock
QMutex m_updateLock
Definition: autoexpire.h:125
remoteutil.h
AutoExpire::m_desiredFreq
uint m_desiredFreq
Definition: autoexpire.h:113
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
true
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
emOldDeletedPrograms
@ emOldDeletedPrograms
Definition: autoexpire.h:33
AutoExpire::m_instanceLock
QMutex m_instanceLock
Definition: autoexpire.h:119
ProgramInfo::IsWatched
bool IsWatched(void) const
Definition: programinfo.h:479
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
fileserverutil.h
mythdate.h
autoexpire.h
expirer
AutoExpire * expirer
Definition: backendcontext.cpp:8
programinfo.h
mythlogging.h
AutoExpire::m_expireThreadRun
bool m_expireThreadRun
Definition: autoexpire.h:114
UpdateEntry::m_encoder
int m_encoder
Definition: autoexpire.h:54
hardwareprofile.config.p
p
Definition: config.py:33
AutoExpire::m_usedEncoders
QMap< int, int > m_usedEncoders
Definition: autoexpire.h:117
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
compat.h
AutoExpire::ExpireRecordings
void ExpireRecordings(void)
This expires normal recordings.
Definition: autoexpire.cpp:399
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:178
AutoExpire::IsInDontExpireSet
bool IsInDontExpireSet(uint chanid, const QDateTime &recstartts) const
Definition: autoexpire.cpp:1101
GetPlaybackURL
QString GetPlaybackURL(ProgramInfo *pginfo, bool storePath)
Definition: fileserverutil.cpp:45
pginfolist_t
std::vector< ProgramInfo * > pginfolist_t
Definition: autoexpire.h:24
emNormalLiveTVPrograms
@ emNormalLiveTVPrograms
Definition: autoexpire.h:32
MThread::RunEpilog
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:211
ExpireThread
Definition: autoexpire.h:41
storagegroup.h
TVRec::s_inputsLock
static QReadWriteLock s_inputsLock
Definition: tv_rec.h:426
LOC
#define LOC
Definition: autoexpire.cpp:44
AutoExpire::SendDeleteMessages
static void SendDeleteMessages(pginfolist_t &deleteList)
This sends delete message to main event thread.
Definition: autoexpire.cpp:611
uint
unsigned int uint
Definition: compat.h:141
AutoExpire::AutoExpire
AutoExpire()=default
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
AutoExpire::FillExpireList
void FillExpireList(pginfolist_t &expireList)
Uses the "AutoExpireMethod" setting in the database to fill the list of files that are deletable.
Definition: autoexpire.cpp:766
AutoExpire::m_dontExpireSet
QSet< QString > m_dontExpireSet
Definition: autoexpire.h:111
emNormalDeletedPrograms
@ emNormalDeletedPrograms
Definition: autoexpire.h:34
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:933
UpdateEntry
Definition: autoexpire.h:51
AutoExpire::RunExpirer
void RunExpirer(void)
This contains the main loop for the auto expire process.
Definition: autoexpire.cpp:266
MythCoreContext::GetBoolSetting
bool GetBoolSetting(const QString &key, bool defaultval=false)
Definition: mythcorecontext.cpp:927
AutoExpire::CalcParams
void CalcParams(void)
Calculates how much space needs to be cleared, and how often.
Definition: autoexpire.cpp:121
VERBOSE_LEVEL_CHECK
#define VERBOSE_LEVEL_CHECK(_MASK_, _LEVEL_)
Definition: mythlogging.h:14
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:68
AutoExpire::FillDBOrdered
void FillDBOrdered(pginfolist_t &expireList, int expMethod)
Creates a list of programs to delete using the database to order list.
Definition: autoexpire.cpp:892
AutoExpire::m_expireThread
ExpireThread * m_expireThread
Definition: autoexpire.h:112
mythcorecontext.h
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:14
emOldestFirst
@ emOldestFirst
Definition: autoexpire.h:28
AutoExpire::~AutoExpire
~AutoExpire() override
AutoExpire destructor stops auto delete thread if it is running.
Definition: autoexpire.cpp:83
emShortLiveTVPrograms
@ emShortLiveTVPrograms
Definition: autoexpire.h:31
dirList
QStringList dirList
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1172
tv_rec.h
AutoExpire::m_mainServer
MainServer * m_mainServer
Definition: autoexpire.h:122
emQuickDeletedPrograms
@ emQuickDeletedPrograms
Definition: autoexpire.h:35
remoteencoder.h
mainserver.h
AutoExpire::ExpireQuickDeleted
void ExpireQuickDeleted(void)
This expires deleted programs within a few minutes.
Definition: autoexpire.cpp:385
SPACE_TOO_BIG_KB
#define SPACE_TOO_BIG_KB
If calculated desired space for 10 min freq is > SPACE_TOO_BIG_KB then we use 5 min expire frequency.
Definition: autoexpire.cpp:52
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:859
AutoExpire::ExpireLiveTV
void ExpireLiveTV(int type)
This expires LiveTV programs.
Definition: autoexpire.cpp:359
AutoExpire::GetDesiredSpace
uint64_t GetDesiredSpace(int fsID) const
Used by the scheduler to select the next recording dir.
Definition: autoexpire.cpp:110
MThread::wait
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:305
AutoExpire::m_encoderList
QMap< int, EncoderLink * > * m_encoderList
Definition: autoexpire.h:88
query
MSqlQuery query(MSqlQuery::InitCon())
AutoExpire::m_updateQueue
QQueue< UpdateEntry > m_updateQueue
Definition: autoexpire.h:126
MythCoreContext::dispatch
void dispatch(const MythEvent &event)
Definition: mythcorecontext.cpp:1734
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
AutoExpire::UpdateDontExpireSet
void UpdateDontExpireSet(void)
Definition: autoexpire.cpp:1065
emWeightedTimePriority
@ emWeightedTimePriority
Definition: autoexpire.h:30
ProgramInfo::kRecordingKey
@ kRecordingKey
Definition: programinfo.h:501
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
UpdateEntry::m_fsID
int m_fsID
Definition: autoexpire.h:57