Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 16.84 KB | None | 0 0
  1. From d2ce271a6f10b36356a57e2e89b677dbc9228c38 Mon Sep 17 00:00:00 2001
  2. From: glenvt18 <glenvt18@gmail.com>
  3. Date: Thu, 18 Aug 2016 16:44:18 +0300
  4. Subject: [PATCH v2] Device power saving feature
  5.  
  6. This patch introduces a feature which allows an idle device (a device
  7. which is not currently recording or streaming) to enter a power-down
  8. mode after some period of time. Given two timeout values,
  9. PowerdownTimeoutM and PowerdownWakeupH, it works like this: when a
  10. device becomes idle, it is kept powered up for PowerdownTimeoutM minutes
  11. doing, for instance, an EPG scan before it is powered down. If the
  12. device is still idle and has been powered down for PowerdownWakeupH
  13. hours it is powered up for PowerdownTimeoutM minutes and so on. When
  14. recording, streaming or a forced EPG scan starts, the device is powered
  15. up and it's idle timer is disabled. This implies that PowerdownTimeoutM
  16. should be enough for a full round of EPG scanning (20 seconds *
  17. number_of_transponders). Another option is to run EPG scans from cron
  18. (at night) and use SVDRP SCAN command.
  19.  
  20. Actual implementation of power saving facilities is left to a derived
  21. device class. In the case of a DVB device it is implemented by closing
  22. it's frontend device. For a DVB-S/S2 tuner this usually means powering
  23. the LNB off. My measurements show 3-4W power consumption drops per tuner
  24. for various DVB-S/S2 tuners. So, this feature (together with HDD
  25. spin-down) is especially valuable while running a headless 24/7 VDR
  26. server and/or using several tuners. A SATIP device can also implement
  27. power saving if it is supported by a server.
  28. ---
  29. config.c    |  9 ++++++
  30.  config.h    |  3 ++
  31.  device.c    | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
  32.  device.h    | 31 ++++++++++++++++++++
  33.  dvbdevice.c | 39 +++++++++++++++++++++++++
  34.  dvbdevice.h |  7 +++++
  35.  eitscan.c   |  7 ++++-
  36.  menu.c      |  9 +++++-
  37.  vdr.c       |  6 ++++
  38.  9 files changed, 203 insertions(+), 4 deletions(-)
  39.  
  40. diff --git a/config.c b/config.c
  41. index e5f5463..794c9f8 100644
  42. --- a/config.c
  43. +++ b/config.c
  44. @@ -395,6 +395,9 @@ cSetup::cSetup(void)
  45.    PositionerSpeed = 15;
  46.    PositionerSwing = 650;
  47.    PositionerLastLon = 0;
  48. +  PowerdownEnabled = 0;
  49. +  PowerdownTimeoutM = 15;
  50. +  PowerdownWakeupH = 4;
  51.    SetSystemTime = 0;
  52.    TimeSource = 0;
  53.    TimeTransponder = 0;
  54. @@ -622,6 +625,9 @@ bool cSetup::Parse(const char *Name, const char *Value)
  55.    else if (!strcasecmp(Name, "PositionerSpeed"))     PositionerSpeed    = atoi(Value);
  56.    else if (!strcasecmp(Name, "PositionerSwing"))     PositionerSwing    = atoi(Value);
  57.    else if (!strcasecmp(Name, "PositionerLastLon"))   PositionerLastLon  = atoi(Value);
  58. +  else if (!strcasecmp(Name, "PowerdownEnabled"))    PowerdownEnabled   = atoi(Value);
  59. +  else if (!strcasecmp(Name, "PowerdownTimeoutM"))   PowerdownTimeoutM  = atoi(Value);
  60. +  else if (!strcasecmp(Name, "PowerdownWakeupH"))    PowerdownWakeupH   = atoi(Value);
  61.    else if (!strcasecmp(Name, "SetSystemTime"))       SetSystemTime      = atoi(Value);
  62.    else if (!strcasecmp(Name, "TimeSource"))          TimeSource         = cSource::FromString(Value);
  63.    else if (!strcasecmp(Name, "TimeTransponder"))     TimeTransponder    = atoi(Value);
  64. @@ -753,6 +759,9 @@ bool cSetup::Save(void)
  65.    Store("PositionerSpeed",    PositionerSpeed);
  66.    Store("PositionerSwing",    PositionerSwing);
  67.    Store("PositionerLastLon",  PositionerLastLon);
  68. +  Store("PowerdownEnabled",   PowerdownEnabled);
  69. +  Store("PowerdownTimeoutM",  PowerdownTimeoutM);
  70. +  Store("PowerdownWakeupH",   PowerdownWakeupH);
  71.    Store("SetSystemTime",      SetSystemTime);
  72.    Store("TimeSource",         cSource::ToString(TimeSource));
  73.    Store("TimeTransponder",    TimeTransponder);
  74. diff --git a/config.h b/config.h
  75. index e5565da..7a73d9d 100644
  76. --- a/config.h
  77. +++ b/config.h
  78. @@ -273,6 +273,9 @@ public:
  79.    int PositionerSpeed;
  80.    int PositionerSwing;
  81.    int PositionerLastLon;
  82. +  int PowerdownEnabled;
  83. +  int PowerdownTimeoutM;
  84. +  int PowerdownWakeupH;
  85.    int SetSystemTime;
  86.    int TimeSource;
  87.    int TimeTransponder;
  88. diff --git a/device.c b/device.c
  89. index 542d120..adbe973 100644
  90. --- a/device.c
  91. +++ b/device.c
  92. @@ -104,6 +104,9 @@ cDevice::cDevice(void)
  93.    dvbSubtitleConverter = NULL;
  94.    autoSelectPreferredSubtitleLanguage = true;
  95.  
  96. +  idleTimerExpires = time(NULL) + Setup.PowerdownTimeoutM * 60;
  97. +  wakeupTimerExpires = 0;
  98. +
  99.    for (int i = 0; i < MAXRECEIVERS; i++)
  100.        receiver[i] = NULL;
  101.  
  102. @@ -745,6 +748,11 @@ bool cDevice::SwitchChannel(int Direction)
  103.    return result;
  104.  }
  105.  
  106. +// While switching to a channel, the device will be kept powered up
  107. +// for at least this number of seconds before a receiver is attached.
  108. +// Must be less than cEITScanner::ScanTimeout.
  109. +#define CHANNEL_SWITCH_POWERUP_TIMEOUT  10
  110. +
  111.  eSetChannelResult cDevice::SetChannel(const cChannel *Channel, bool LiveView)
  112.  {
  113.    cStatus::MsgChannelSwitch(this, 0, LiveView);
  114. @@ -778,6 +786,8 @@ eSetChannelResult cDevice::SetChannel(const cChannel *Channel, bool LiveView)
  115.          Result = scrNotAvailable;
  116.       }
  117.    else {
  118. +     // Power up the device
  119. +     PowerUp(CHANNEL_SWITCH_POWERUP_TIMEOUT);
  120.       // Stop section handling:
  121.       if (sectionHandler) {
  122.          sectionHandler->SetStatus(false);
  123. @@ -843,8 +853,11 @@ int cDevice::Occupied(void) const
  124.  
  125.  void cDevice::SetOccupied(int Seconds)
  126.  {
  127. -  if (Seconds >= 0)
  128. +  if (Seconds >= 0) {
  129.       occupiedTimeout = time(NULL) + min(Seconds, MAXOCCUPIEDTIMEOUT);
  130. +     // avoid short power-down/power-up cycles
  131. +     SetIdleTimer(true, Seconds + 30);
  132. +     }
  133.  }
  134.  
  135.  bool cDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
  136. @@ -1675,6 +1688,7 @@ bool cDevice::AttachReceiver(cReceiver *Receiver)
  137.              startScrambleDetection = time(NULL);
  138.              }
  139.           Start();
  140. +         SetIdleTimer(false);
  141.           return true;
  142.           }
  143.        }
  144. @@ -1708,8 +1722,10 @@ void cDevice::Detach(cReceiver *Receiver)
  145.             camSlot->Assign(NULL);
  146.          }
  147.       }
  148. -  if (!receiversLeft)
  149. +  if (!receiversLeft) {
  150.       Cancel(-1);
  151. +     SetIdleTimer(true);
  152. +     }
  153.  }
  154.  
  155.  void cDevice::DetachAll(int Pid)
  156. @@ -1731,6 +1747,82 @@ void cDevice::DetachAllReceivers(void)
  157.        Detach(receiver[i]);
  158.  }
  159.  
  160. +void cDevice::CheckIdle(void)
  161. +{
  162. +  if (!SupportsPowerDown() || !Setup.PowerdownEnabled)
  163. +     return;
  164. +  cMutexLock MutexLock(&mutexPowerSaving);
  165. +  if (idleTimerExpires != 0 && time(NULL) > idleTimerExpires) {
  166. +     // idle, powered up
  167. +     dsyslog("power saving: device %d idle timer expired", CardIndex() + 1);
  168. +     SetIdleTimer(false);
  169. +     if (Setup.PowerdownWakeupH != 0)
  170. +        wakeupTimerExpires = time(NULL) + Setup.PowerdownWakeupH * 3600;
  171. +     else
  172. +        dsyslog("power saving: waking up is disabled");
  173. +     if (!IsPoweredDown()) {
  174. +        dsyslog("power saving: powering device %d down", CardIndex() + 1);
  175. +        if (sectionHandler) {
  176. +           sectionHandler->SetStatus(false);
  177. +           sectionHandler->SetChannel(NULL);
  178. +           }
  179. +        PowerDownMode(true);
  180. +        }
  181. +     }
  182. +  if (wakeupTimerExpires != 0 && time(NULL) > wakeupTimerExpires) {
  183. +     // idle, powered down
  184. +     dsyslog("power saving: device %d wakeup timer expired", CardIndex() + 1);
  185. +     SetIdleTimer(true);
  186. +     if (IsPoweredDown()) {
  187. +        dsyslog("power saving: waking up device %d", CardIndex() + 1);
  188. +        PowerDownMode(false);
  189. +        }
  190. +     }
  191. +}
  192. +
  193. +void cDevice::SetIdleTimer(bool On, int ExtraTimeoutS)
  194. +{
  195. +  if (!SupportsPowerDown())
  196. +     return;
  197. +  cMutexLock MutexLock(&mutexPowerSaving);
  198. +  if (On) {
  199. +     int Tout = Setup.PowerdownTimeoutM * 60;
  200. +     time_t Now = time(NULL);
  201. +     if (ExtraTimeoutS > 0) {
  202. +        if (idleTimerExpires >= Now + ExtraTimeoutS)
  203. +           return;
  204. +        Tout = ExtraTimeoutS;
  205. +        }
  206. +     idleTimerExpires = Now + Tout;
  207. +     if (Setup.PowerdownEnabled)
  208. +        dsyslog("power saving: set device %d idle timer to %d sec", CardIndex() + 1, Tout);
  209. +     }
  210. +  else {
  211. +     idleTimerExpires = 0;
  212. +     if (Setup.PowerdownEnabled)
  213. +        dsyslog("power saving: disable device %d idle timer", CardIndex() + 1);
  214. +     }
  215. +  wakeupTimerExpires = 0;
  216. +}
  217. +
  218. +bool cDevice::PoweredDown(void)
  219. +{
  220. +  if (SupportsPowerDown() && Setup.PowerdownEnabled) {
  221. +     cMutexLock MutexLock(&mutexPowerSaving);
  222. +     return IsPoweredDown();
  223. +     }
  224. +  else
  225. +     return false;
  226. +}
  227. +
  228. +void cDevice::PowerUp(int ExtraTimeoutS)
  229. +{
  230. +  cMutexLock MutexLock(&mutexPowerSaving);
  231. +  SetIdleTimer(true, ExtraTimeoutS);
  232. +  if (SupportsPowerDown() && IsPoweredDown())
  233. +     PowerDownMode(false);
  234. +}
  235. +
  236.  // --- cTSBuffer -------------------------------------------------------------
  237.  
  238.  cTSBuffer::cTSBuffer(int File, int Size, int CardIndex)
  239. diff --git a/device.h b/device.h
  240. index 31ee303..cc40bb7 100644
  241. --- a/device.h
  242. +++ b/device.h
  243. @@ -821,6 +821,37 @@ public:
  244.         ///< Detaches all receivers from this device for this pid.
  245.    virtual void DetachAllReceivers(void);
  246.         ///< Detaches all receivers from this device.
  247. +
  248. +// Power saving facilities
  249. +
  250. +private:
  251. +  cMutex mutexPowerSaving;
  252. +  time_t idleTimerExpires, wakeupTimerExpires;
  253. +  void PowerUp(int ExtraTimeoutS);
  254. +       ///< If the device is powered down, powers it up and keeps it
  255. +       ///< powered up for at least ExtraTimeoutS seconds (see
  256. +       ///< cDevice::SetIdleTimer()).
  257. +public:
  258. +  void CheckIdle(void);
  259. +       ///< Should be called periodically in the main loop.
  260. +  bool PoweredDown(void);
  261. +       ///< Returns true if the device is powered down "logically", that is,
  262. +       ///< idle tasks like EPG scanning are disabled.
  263. +  void SetIdleTimer(bool On, int ExtraTimeoutS = 0);
  264. +       ///< Starts/disables the idle timer. This timer must be started when
  265. +       ///< a device gets idle and must be disabled when it is receiving.
  266. +       ///< If ExtraTimeoutS is greater than zero and On is true, a new timer
  267. +       ///< won't be set, but the device will be kept powered up for at least
  268. +       ///< ExtraTimeoutS seconds.
  269. +protected:
  270. +       ///< NOTE: IsTunedToTransponder() should return false if the
  271. +       ///< device is powered down.
  272. +  virtual bool IsPoweredDown(void) {return false;}
  273. +       ///< Returns true if the device is powered down "physically".
  274. +  virtual void PowerDownMode(bool On) {};
  275. +       ///< Actually powers the device down/up.
  276. +  virtual bool SupportsPowerDown() {return false;}
  277. +       ///< Returns true if a derived device supports power saving.
  278.    };
  279.  
  280.  /// Derived cDevice classes that can receive channels will have to provide
  281. diff --git a/dvbdevice.c b/dvbdevice.c
  282. index 63af52e..87555b7 100644
  283. --- a/dvbdevice.c
  284. +++ b/dvbdevice.c
  285. @@ -348,6 +348,8 @@ public:
  286.    const cPositioner *Positioner(void) const { return positioner; }
  287.    int GetSignalStrength(void) const;
  288.    int GetSignalQuality(void) const;
  289. +  bool IsPoweredDown(void) {return fd_frontend < 0;}
  290. +  void PowerDownMode(bool On);
  291.    };
  292.  
  293.  cMutex cDvbTuner::bondMutex;
  294. @@ -544,6 +546,8 @@ void cDvbTuner::ClearEventQueue(void) const
  295.  
  296.  bool cDvbTuner::GetFrontendStatus(fe_status_t &Status) const
  297.  {
  298. +  if (fd_frontend < 0)
  299. +     return false;
  300.    ClearEventQueue();
  301.    while (1) {
  302.          if (ioctl(fd_frontend, FE_READ_STATUS, &Status) != -1)
  303. @@ -559,6 +563,8 @@ bool cDvbTuner::GetFrontendStatus(fe_status_t &Status) const
  304.  
  305.  int cDvbTuner::GetSignalStrength(void) const
  306.  {
  307. +  if (fd_frontend < 0)
  308. +     return -1;
  309.    ClearEventQueue();
  310.    uint16_t Signal;
  311.    while (1) {
  312. @@ -1001,6 +1007,26 @@ void cDvbTuner::Action(void)
  313.          }
  314.  }
  315.  
  316. +void cDvbTuner::PowerDownMode(bool On)
  317. +{
  318. +  cMutexLock MutexLock(&mutex);
  319. +  if (On && fd_frontend >= 0) {
  320. +     isyslog("dvb tuner: power-down - closing frontend %d/%d", adapter, frontend);
  321. +     tunerStatus = tsIdle;
  322. +     close(fd_frontend);
  323. +     fd_frontend = -1;
  324. +     }
  325. +  if (!On && fd_frontend < 0) {
  326. +     cString Filename = cString::sprintf("%s/%s%d/%s%d",
  327. +        DEV_DVB_BASE, DEV_DVB_ADAPTER, adapter, DEV_DVB_FRONTEND, frontend);
  328. +     isyslog("dvb tuner: power-up - opening frontend %d/%d", adapter, frontend);
  329. +     fd_frontend = open(Filename, O_RDWR | O_NONBLOCK);
  330. +     if (fd_frontend < 0)
  331. +        esyslog("ERROR: can't open DVB device frontend %d/%d", adapter, frontend);
  332. +     tunerStatus = tsIdle;
  333. +     }
  334. +}
  335. +
  336.  // --- cDvbSourceParam -------------------------------------------------------
  337.  
  338.  class cDvbSourceParam : public cSourceParam {
  339. @@ -1712,6 +1738,19 @@ void cDvbDevice::DetachAllReceivers(void)
  340.    needsDetachBondedReceivers = false;
  341.  }
  342.  
  343. +bool cDvbDevice::IsPoweredDown(void)
  344. +{
  345. +  if (dvbTuner)
  346. +     return dvbTuner->IsPoweredDown();
  347. +  return false;
  348. +}
  349. +
  350. +void cDvbDevice::PowerDownMode(bool On)
  351. +{
  352. +  if (dvbTuner)
  353. +     dvbTuner->PowerDownMode(On);
  354. +}
  355. +
  356.  // --- cDvbDeviceProbe -------------------------------------------------------
  357.  
  358.  cList<cDvbDeviceProbe> DvbDeviceProbes;
  359. diff --git a/dvbdevice.h b/dvbdevice.h
  360. index 5ae4952..e21c652 100644
  361. --- a/dvbdevice.h
  362. +++ b/dvbdevice.h
  363. @@ -290,6 +290,13 @@ protected:
  364.    virtual void CloseDvr(void);
  365.    virtual bool GetTSPacket(uchar *&Data);
  366.    virtual void DetachAllReceivers(void);
  367. +
  368. +// Power saving facilities
  369. +
  370. +protected:
  371. +  virtual bool IsPoweredDown(void);
  372. +  virtual void PowerDownMode(bool On);
  373. +  virtual bool SupportsPowerDown() {return true;}
  374.    };
  375.  
  376.  // A plugin that implements a DVB device derived from cDvbDevice needs to create
  377. diff --git a/eitscan.c b/eitscan.c
  378. index 41ac25e..765055c 100644
  379. --- a/eitscan.c
  380. +++ b/eitscan.c
  381. @@ -144,7 +144,8 @@ void cEITScanner::Process(void)
  382.             bool AnyDeviceSwitched = false;
  383.             for (int i = 0; i < cDevice::NumDevices(); i++) {
  384.                 cDevice *Device = cDevice::GetDevice(i);
  385. -               if (Device && Device->ProvidesEIT()) {
  386. +               if (Device && Device->ProvidesEIT()
  387. +                     && (!Device->PoweredDown() || lastActivity == 0)) { // powered up or forced scan
  388.                    for (cScanData *ScanData = scanList->First(); ScanData; ScanData = scanList->Next(ScanData)) {
  389.                        const cChannel *Channel = ScanData->GetChannel();
  390.                        if (Channel) {
  391. @@ -165,6 +166,10 @@ void cEITScanner::Process(void)
  392.                                             }
  393.                                          }
  394.                                       //dsyslog("EIT scan: device %d  source  %-8s tp %5d", Device->DeviceNumber() + 1, *cSource::ToString(Channel->Source()), Channel->Transponder());
  395. +                                     if (lastActivity == 0)
  396. +                                        // forced scan - set idle timer for each channel switch;
  397. +                                        // this prevents powering down while scanning a transponder
  398. +                                        Device->SetIdleTimer(true, ScanTimeout + 5);
  399.                                       Device->SwitchChannel(Channel, false);
  400.                                       scanList->Del(ScanData);
  401.                                       AnyDeviceSwitched = true;
  402. diff --git a/menu.c b/menu.c
  403. index 569900c..5a89771 100644
  404. --- a/menu.c
  405. +++ b/menu.c
  406. @@ -3715,6 +3715,12 @@ void cMenuSetupLNB::Setup(void)
  407.       Add(new cMenuEditIntxItem(tr("Setup.LNB$Positioner speed (degrees/s)"), &data.PositionerSpeed, 1, 1800, 10));
  408.       }
  409.  
  410. +  Add(new cMenuEditBoolItem(tr("Setup.LNB$Enable power saving"), &data.PowerdownEnabled));
  411. +  if (data.PowerdownEnabled) {
  412. +     Add(new cMenuEditIntItem(tr("Setup.LNB$Power down an idle device after (min)"), &data.PowerdownTimeoutM));
  413. +     Add(new cMenuEditIntItem(tr("Setup.LNB$Wake up from power-down after (h)"), &data.PowerdownWakeupH));
  414. +     }
  415. +
  416.    SetCurrent(Get(current));
  417.    Display();
  418.  }
  419. @@ -3723,6 +3729,7 @@ eOSState cMenuSetupLNB::ProcessKey(eKeys Key)
  420.  {
  421.    int oldDiSEqC = data.DiSEqC;
  422.    int oldUsePositioner = data.UsePositioner;
  423. +  int oldPowerdownEnabled = data.PowerdownEnabled;
  424.    bool DeviceBondingsChanged = false;
  425.    if (Key == kOk) {
  426.       cString NewDeviceBondings = satCableNumbers.ToString();
  427. @@ -3731,7 +3738,7 @@ eOSState cMenuSetupLNB::ProcessKey(eKeys Key)
  428.       }
  429.    eOSState state = cMenuSetupBase::ProcessKey(Key);
  430.  
  431. -  if (Key != kNone && (data.DiSEqC != oldDiSEqC || data.UsePositioner != oldUsePositioner))
  432. +  if (Key != kNone && (data.DiSEqC != oldDiSEqC || data.UsePositioner != oldUsePositioner || data.PowerdownEnabled != oldPowerdownEnabled))
  433.       Setup();
  434.    else if (DeviceBondingsChanged)
  435.       cDvbDevice::BondDevices(data.DeviceBondings);
  436. diff --git a/vdr.c b/vdr.c
  437. index 6b0bf2b..c8de702 100644
  438. --- a/vdr.c
  439. +++ b/vdr.c
  440. @@ -1515,6 +1515,12 @@ int main(int argc, char *argv[])
  441.  
  442.          ReportEpgBugFixStats();
  443.  
  444. +        for (int i = 0; i < cDevice::NumDevices(); i++) {
  445. +           cDevice *d = cDevice::GetDevice(i);
  446. +           if (d)
  447. +              d->CheckIdle();
  448. +           }
  449. +
  450.          // Main thread hooks of plugins:
  451.          PluginManager.MainThreadHook();
  452.          }
  453. --
  454. 2.7.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement