Pages

Tuesday, October 18, 2011

Ubuntu : update-apt-xapi takes lot of cpu usage

Problem : The process "update-apt-xapi" causes increase in CPU utilization to 100%

There are 2 ways to check this problem :

1.  One is to go to the System Monitor and click on the Processes tab. Sort by %CPU Used and the offending process should pop to the top.

2. The other way is to executed a terminal and type “top” w/o the quotes. Top is a great little application that lists the top users of CPU time, plus gives a bunch of great info at the top of the terminal screen like total uptime, CPU info, process statistic

What is update-apt-xapi?
The actual name of the package is "update-apt-xapian-index". It helps in maintaing an index of packages, and this helps speed up searching for packages in Synaptic, and possibly in other package managers as well. 

Solutions :

There are 3 possible solution to solve this problem :

Note: Use method 2 and 3 only if 1 does not works.


1. Edit /etc/cron.weekly/apt-xapian-index

This method will do following things :
a) Run process with the lowest system priority. 
b) Update the index and not built it again (faster)

i) sudo gedit /etc/cron.weekly/apt-xapian-index

ii) replace the line
nice $IONICE -c3 $CMD --quiet
by
nice -n 19 $IONICE -c 3 $CMD --update --quiet

iii) Save And exit And you are done.

In depth detail of changing  cron file can be found here:(Not important)
----------------------------------------------------------------------------------------------------------------------------------
Cron is a utility for scheduling tasks to run at certain times. System tasks run weekly are, unsurprisingly, stored in the /etc/cron.weekly directory. You can also set up personal tasks to run pretty much whenever you want. For that, have a look at man crontab.

Looking at the internals of the code we're using here, the first line, the "crunchbang" line (#!), tells the system what executable to use to run the contents -- in this case, /bin/sh, or your basic shell.

The next two lines establish two shorthand variables. Variables in shell scripts are generally defined in ALL CAPS for easy readability. This is more of a best practice than any hard-and-fast requirement. When referenced later in the script, the variable names are prefixed with a $. Here, CMD is simply shorthand for the path to the update-apt-xapian-index binary, and IONICE is shorthand for the path to the ionice utility for getting or setting a process's I/O scheduling class and priority.

In the if statements, the -x checks to see if the next argument exists, so if [ -x $CMD ] will check to see if /usr/sbin/update-apt-xapian-index exists in the filesystem.

nice -n is basically how you assign a priority to a process. An important caveat, however, is that nice is just that -- a high nice value (up to a maximum of -n 19) means the process is nice and gets out of the way, and a low nice value (down to a minimum of -n -20) means the process is *not* nice and barges to the front of the line to be the first to use system resources. Niceness defaults to 10 if not otherwise specified, and apparently the default update-apt-xapian-index setup does not specify any value.

ionice is new in this fix. It works along similar lines, affecting a process's input/output niceness, only using the flag -c for "class". The ionice man page describes -c:

Quote:
-c class
The scheduling class. 0 for none, 1 for real time, 2 for best-effort, 3 for idle.
Finally, we have the two options passed to update-apt-xapian-index itself, --update and --quiet. --quiet just tells it to not generate much text, only outputting for fatal errors, which makes sense for a background process. --update is new here in this fix together with the nice value and the ionice prioritization, and is a real kicker: it tellsupdate-apt-xapian-index to only update those items in the index that have actually changed. This seems like a no-brainer, since the index includes *every* package installed in the system, but unfortunately the default update-apt-xapian-index setup in a fresh install of Jaunty, Karmic, or Lucid all leave this option out, meaning thatupdate-apt-xapian-index will rebuild the ENTIRE package index every time it runs. No wonder it eats up so much memory and CPU time! With --update, it should take much less resources and much less time.
---------------------------------------------------------------------------------------------------------------------------------- 

2.  Making the weekly update script non-executable 

It turns out that it installs itself to run weekly by adding a script to /etc/cron.weekly/. That’s easy to fix simply by making the script non-executable through file permission change:

sudo chmod 644 /etc/cron.weekly/apt-xapian-index

3.  Remove update-apt-xapi package
sudo apt-get autoremove --purge apt-xapian-index 
sudo apt-get autoremove --purge 

Note : Purging apt-xapian-index causes the quick search feature in Synaptic to become inoperative.

2 comments:

  1. Thanks, working solution (#1) !

    ReplyDelete
  2. I just created a fake package to get rid of apt-xapian-index without having to edit system configuration files, see here.

    https://github.com/rdiez/Tools/tree/master/FakeReplacementForAptXapianIndex

    ReplyDelete