A few years ago when I was attending at UDS-M, the Kubuntu guys and I tried to understand why KDE took so long to start, we used bootchart and started debugging startkde & friends, after disabling lots of kded modules and a bunch of stuff we couldn’t get rid of the ~10 seconds marker.
Time has passed and no matter how fast your computer is we still have to wait a lot for KDE to start, is KDE slow? Is Qt Slow? Is C++ slow? All these questionings kept coming to my mind, and well lots of friends ask why is KDE slow to start, after it has started it’s fast. So the other day I decided to try Unity to know if all the FUD were true or not, and I got stunned with a desktop bloated with python scripts and stuff it was much faster than KDE to start, it’s really fast to load, after a few hours trying to use it and not being able to even move the panel to the bottom I gave up and installed Kubuntu again…
I keep seeing Martin (KWin maintainer) blogging about the milliseconds he was able to get rid of, and make KWin startup faster, still I can’t notice it, it’s like there is a 10 seconds timer for KSplash to go away…
So I decided to dissemble it, and created a thing called “sessionk” (yeah like systemd but with a K), the idea behind sessionk was to debug and understand all phases of startup and find who is being slow. The very first code started KWin and plasma but KRunner couldn’t launch anything, so I had to go deeper and deeper till I think now I fully understand what happens today:
- ligthdm launches X, DBus, startkde
- startkde do some X calls (using cmd line tools) to set mouse pointer, RandR
- startkde starts kdeinit with an option to load KCMInit modules (which do pretty much the same as the above, but the code is in KDE modules) – takes around 2 seconds
- kdeinit also auto launches kded which loads KCModules that have phase= 0 or 1
- startkde then starts ksmserver which only returns after the session is closed so startkde script continues and quits killing the X session
- ksmserver now launches the window manager, normally KWin, here is were ksmserver shows it’s age (around 13 years), it has a 4 seconds timer to make sure KWin will have enough time to start so that when the auto start phases start it’s loaded.
- after those 4 seconds ksmserver starts autostart phase 0, 1, 2 at these phases apps like plasma, nepomuk, polkit-kde are loaded
- after all those phases are reportedly to be finished ksmserver restores the user session (if the settings say so) lauching the apps there were open when the user left.
In short our code works but it’s showing it’s age, surelly KWin starts much faster than that, actually it hardly takes more than one second to start.
What needs improvements? Lots of stuff, even without those 4 seconds timer plasma still takes quite a while to load, with Plasma 2 it’s said that it’s starting almost instantly, but you can get that behavior in trade of removing some widgets :P another killer is kded4, if I load all the modules before Plasma and KWin, it delays startup by around 2 seconds and makes plasma to freeze a lot…
A suggestion would be to improve ksmserver, so I took a look at the job, and really I don’t feel I can make “safe” changes in it, I mean it’s 13 years old code that WORKS! If I change anything and break it your session is gone, startkde will return and X is closed. A second reason is it’s current license BSD, and I refuse to code using this license (won’t get into the license merit), a third reason is that I want to sessionk to be small with the fewer deps it can have (like removing the shutdown dialog from the main process), the fourth and last reason is that I don’t think ksmserver is doing a good job on restoring sessions, for instance Chromiun always complain that it was closed abruptally, it could be fixed but due to the code license, age and well the code isn’t easy for me to understand I decided to push sessionk a bit further, I might not replace ksmserver/startkde but it will surely show other components of KDE that we have a large room for improvements.
What sessionk does:
- lightdm launches X, DBus, sessionk
- sessionk setups some stuff, including shortcuts (which also have OSX shortcuts due to my Mac keyboard), when the shortcuts are loaded it loads kdeinit because of kglobalaccel
- kdeinit auto loads kded and klauncher (kded is loaded without any modules)
- when klauncher is loaded sessionk starts KWin and Plasma
- sessionk listen for X events to know if a desktop window was created, then it tells KDED to load all modules
- after all kded modules are loaded autostart phases 0, 1, 2 are loaded (which loads, policykit-kde, nepomuk …)
- restore the apps that were open on last session (TODO)
Videos say more than a thousand words…
Looking for code? Be careful as it’s still work in progress and might crash your session…
EDIT : Forgot to thank Kai Uwe Broulik for that awesome plasma zoom in effect :)