Bug #892
Power: tvheadend is constantly waking the system
100%
Description
I noticed when checking my NAS-box with "powertop" that tvheadend is constantly waking
up the system (wasting energy) even if its not in active use (neither recordings nor live tv).
Cn Avg residency P-states (frequencies) C0 (cpu running) ( 0.0%) polling 1.8ms ( 0.1%) C1 mwait 1.7ms (136.6%) Wakeups-from-idle per second : 797.2 interval: 5.0s no ACPI power usage estimate available Top causes for wakeups: 38.9% (936.0) tvheadend 28.6% (686.8) USB device 1-7 : MediaTV Digital Home (Sundtek) 18.9% (455.0) [ehci_hcd:usb1, uhci_hcd:usb2] <interrupt> 12.2% (294.6) mediasrv
An strace on tvheadend shows this activity over and over
[...] sendto(6, "\0\0\0\0", 4, MSG_NOSIGNAL, NULL, 0) = 4 recvfrom(6, "\1Fo\4\200\4\0\0\0\0\0", 11, MSG_WAITALL, NULL, NULL) = 11 recvfrom(6, "\0\0\0\0", 4, MSG_WAITALL, NULL, NULL) = 4 fcntl(6, F_SETLK, {type=F_UNLCK, whence=SEEK_CUR, start=0, len=0}) = 0 semop(720898, {{0, 0, SEM_UNDO}, {0, 1, SEM_UNDO}}, 2) = 0 semop(720898, {{0, -1, SEM_UNDO}}, 1) = 0 semop(688129, {{0, 0, SEM_UNDO}, {0, 1, SEM_UNDO}}, 2) = 0 semop(688129, {{0, -1, SEM_UNDO}}, 1) = 0 fcntl(6, F_SETLKW, {type=F_WRLCK, whence=SEEK_CUR, start=0, len=0}) = 0 sendto(6, "\1Go\2\200\2\0\0\0\0\0", 11, MSG_NOSIGNAL, NULL, 0) = 11 sendto(6, "\334\377", 2, MSG_NOSIGNAL, NULL, 0) = 2 recvfrom(6, "\1Go\2\200\2\0\0\0\0\0", 11, MSG_WAITALL, NULL, NULL) = 11 recvfrom(6, "\334\377", 2, MSG_WAITALL, NULL, NULL) = 2 fcntl(6, F_SETLK, {type=F_UNLCK, whence=SEEK_CUR, start=0, len=0}) = 0 semop(720898, {{0, 0, SEM_UNDO}, {0, 1, SEM_UNDO}}, 2) = 0 semop(720898, {{0, -1, SEM_UNDO}}, 1) = 0 semop(688129, {{0, 0, SEM_UNDO}, {0, 1, SEM_UNDO}}, 2) = 0 semop(688129, {{0, -1, SEM_UNDO}}, 1) = 0 fcntl(6, F_SETLKW, {type=F_WRLCK, whence=SEEK_CUR, start=0, len=0}) = 0 sendto(6, "\1Ho\2\200\2\0\0\0\0\0", 11, MSG_NOSIGNAL, NULL, 0) = 11 sendto(6, "\334\377", 2, MSG_NOSIGNAL, NULL, 0) = 2 recvfrom(6, "\1Ho\2\200\2\0\0\0\0\0", 11, MSG_WAITALL, NULL, NULL) = 11 recvfrom(6, "\334\377", 2, MSG_WAITALL, NULL, NULL) = 2 fcntl(6, F_SETLK, {type=F_UNLCK, whence=SEEK_CUR, start=0, len=0}) = 0 futex(0x7bb9e0, FUTEX_WAKE_PRIVATE, 1) = 1
When attaching gdb i noticed one thread that had constant activity:
Thread 11 (Thread 0x7f3255f5f700 (LWP 32028)): #0 0x00007f32584a85ad in nanosleep () from /lib/x86_64-linux-gnu/libc.so.6 #1 0x00007f32584d9984 in usleep () from /lib/x86_64-linux-gnu/libc.so.6 #2 0x00007f3259a4720e in __net_read () from /opt/lib/libmediaclient.so #3 0x00007f3259a3f5c5 in read () from /opt/lib/libmediaclient.so #4 0x000000000043c57c in read (aux=0x880c50) at /usr/include/bits/unistd.h: #5 dvb_adapter_input_dvr (aux=0x880c50) at /home/frank/tvheadend/src/dvb/dvb_adapter.c:533 #6 0x00007f3258a1ad8c in start_thread () from /lib/x86_64-linux-gnu/libpthread #7 0x00007f32584e104d in clone () from /lib/x86_64-linux-gnu/libc.so.6 #8 0x0000000000000000 in ?? ()
The code dvb_adapter_input_dvr() looks like this:
while(1) { r = read(fd, tsb, sizeof(tsb)); [...]
I understand that EPG-data comes in from DVB but still i wonder if tvheadend
really has to read from the dvb device all the time?
Files
History
Updated by Rene Herbrich over 12 years ago
Same here, installed tvheadend on my Synology NAS. If tvheadend is up and running, my nas does not hibernate.
As DVB Adapter I'm using a HDHomeRun device.
As a work-around I'd suggest to start the function above only if a client is connected or the webinterface
was opened in a browser? Addionally/alternatively you could run the function for let's say 15min at an predefined time?
(Don't know how long it takes the get all/most of the EPG-data?)
Updated by Adam Sutton over 12 years ago
- Status changed from New to Need feedback
- Assignee changed from Hein Rigolo to Adam Sutton
Is this still happening? I'd guess yes, but I might need to enlist some help in looking into it.
Updated by Eric Valette over 12 years ago
Adam Sutton wrote:
Is this still happening? I'd guess yes, but I might need to enlist some help in looking into it.
Yes it still happens and is for me a critical bug for tvheadend adoption on NAS. You cannot expect everyone will have 7/7 hard disk in their NAS due to pricing and this is also good to for the planet to put NAS is sleep mode.
If EIT is the only source for EPG, you have to choose between epg or sleep but as long as you get approximative EPG via XMLTV, you can relinquish the adapter and exit the main reading loop.
Updated by oo pepe over 12 years ago
This is an issue for me on my nas, too. Its too recource hungry. A configurable update interval would be nice. So tvheadend only updates the epg from the dvb stream every 'update interval' seconds. Another way would be to enable/disable epg from dvb globally, so it tvheadend only updates if the global option is set.
Updated by Frank Meerköter over 12 years ago
Yes, still happens. Its not so much a bug as its the way tvheadend is currently designed to work.
Updated by oo pepe over 12 years ago
Im trying to implement something here, but i need some help. Open file handles to the /dev/dvb/adapterX devices are used by the two threads dvb_adapter_input_dvr and dvb_table_input. Additionally a file handle to the frontend0 device is held. All those handles need to be closed to let the adapter go to sleep.
So the threads need to sleep until the adapter is needed again. This is not that hard to implement.
My question is how to know when the adapter is needed? Where is a good place to open/close the handles? Maybe an additional thread which monitors the usage of the adapter and closes/ opens the handles as they are needed? What is the best way to detirmine if there is nothing using the adapter anymore (no scheduled recordings or connected clients)?
Is somebody else working on this?
Updated by Adam Sutton over 12 years ago
Sorry guys,
I should have pointed out that for the most part this is likely to be fixed by PR-58. However I think it needs some tweaks before it can be merged into master.
However I'm not certain about that as I've not had a chance to test the specific use case reported here.
Adam
Updated by oo pepe over 12 years ago
I just tested it with my sundtek mediatv dvbc card and it didnt work. At least the led from the card is still on. If i close all handles the device goes off after 15 seconds.
Updated by oo pepe over 12 years ago
The sundtek support confirmed that all file handles to the sundtek mediatv dvb-c card need to be closed in order for powersaving mode to work. See http://support.sundtek.com/index.php/topic,1014.msg7092.html#msg7092 .
After the last handle is closed the powersaving mode starts after 15 seconds. See http://support.sundtek.com/index.php/topic,4.0.html and search for "powersaving".
They also state that bringing the device into powersaving mode increases the lifetime of the device, so supporting this in tvheadend would be very nice.
Updated by Adam Sutton about 12 years ago
- Status changed from Need feedback to Accepted
OK,
I've been digging a bit more and familiarising myself with some of the DVB code. I can see that the frontend and dvr interfaces are indeed help permanently open even if the device is inactive.
My current mod to ensure the mux scanner stops is not enough to resolve this. So I need to look in more detail at how best to solve this problem.
Adam
Updated by Adam Sutton about 12 years ago
Can someone test this, if they're still interested:
https://github.com/adamsutton/tvheadend/tree/test/idlescan
It's still just proof of concept and the code needs some tidying up to make sure its robust and efficient. But I just want to know if it allows the device to sleep when you're not doing anything.
Note: that you will need to ensure you disable idle scan, signal monitoring and all EPG OTA services.
Updated by oo pepe about 12 years ago
I tested the test/idlescan branch and here are my results:
After startup all handles to the device are closed and the powersaving mode begins. Yay :-)
When i then want to watch a channel it doesnt work:- The handle to /dev/dvb/adapter0/dvr0 does not open, whereas the handles to /dev/dvb/adapter0/frontend0 and /dev/dvb/adapter0/demux0 reopen just fine. I can see that with the driver utility "mediaclient --lc". See http://pastebin.com/xUE31bKe .
- After some seconds of not seeing a video tvheadend crashes. I think after unsubscribing the client. See http://pastebin.com/8dALC8yL .
Let me know if you need more/other log files.
Thank you for working on this issue!
Updated by Adam Sutton about 12 years ago
Well I guess that's progress of a fashion
It was an initial hack so I'm not completely surprised its not 100% reliable. Can you maybe check to see whether stuff works from the UI.
Just in case there is a problem (I've missed) with HTSP, as I can't test that at the moment.
Updated by oo pepe about 12 years ago
I did start the stream via the webbrowser ui. In the meantime ill try to see if i can find the issue myself.
Updated by Adam Sutton about 12 years ago
If you're digging in the code, you want to look in the new dvb_adapter_dvr_start() call (it might be _start_dvr()). It creates the pthread that will ultimately open the dvr device (callback ..._input_dvr).
Check if that's all happening properly. I may well have missed something, though I did run some basic tests on my own setup.
Adam
Updated by oo pepe about 12 years ago
I found the bug. In dvb_fe_stop you call pthread_join(tda->tda_dvr_thread, NULL);. In my case tda->tda_dvr_thread was not initialized, because i own a dvb-c card. Using this patch http://pastebin.com/GtPbRr1K the thread gets started if you have a dvb-s or a dvb-c card.
Maybe a check if tda->tda_dvr_thread is valid would be good, too.
Now i can watch tv without a problem and the card goes into powersave mode after some time :-)
What about EPG data? I would be nice to have epg while using the powersave mode. So wake up the card from time to time and fetch some epg data for a while and then go to sleep again. Is this another issue or can we cover this here, too?
Updated by Adam Sutton about 12 years ago
Well I did say it was a dirty hack that needed some reviewing But thanks for the input I'll do a proper review of the code tonight and tidy up and hopefully we can get it included at some point.
With regard to the EPG stuff, if there isn't an issue already there will be one soon as I've been talking about this with another user who has promised to submit one. If he doesn't feel free to do it yourself.
But I think it is a different (though related) issue.
Updated by Adam Sutton about 12 years ago
I have tidied the code up and moved to a new branch, please try this when you get a chance:
https://github.com/adamsutton/tvheadend/tree/issue/redmine-892
Will see about getting that merged shortly if it works. I have tested on my machine with recordings, but will test properly with live viewing channel switching etc.. when I get home.
Adam
Updated by oo pepe about 12 years ago
I tested the new branch and its working fine for me :-) I tested watching tv over the browser ui and via xbmc.
Updated by Adam Sutton about 12 years ago
- Status changed from Accepted to Resolved
That's good to hear. I need to get Andreas to look it over as I don't normally mess about with the DVB code (much).
But assuming he's ok with it, I'll get it merged to master.
Updated by Adam Sutton about 12 years ago
- Status changed from Resolved to Fixed
- % Done changed from 0 to 100
Applied in changeset commit:93c64b1cfdd19ffa62a966d9837dc6eee68a4ab8.
Updated by Piotr Kuchciak over 8 years ago
Hi
I use latest tvheadend 4.1 as package SPK on my synology DS713+. But i have a problem because when Tvheadend is running my NAS not go to Hibernation Disk HDD. When i disabled package Tvheadend, NAS with no problem go to Hibernation Disk HDD. Please help me...