You are not logged in.

#1 2013-03-29 15:46:09

jlindgren
Member
Registered: 2011-02-27
Posts: 260

Calculate total installed package sizes (including dependencies)

Sometimes it's nice to know what packages are hogging the most space on your drive.  I thought there must be a utility to do this already, but searching for one didn't bring up anything promising, so here is a quick C program to do it.  You'd compile and run it something like this:

$ gcc -std=c99 -Wall -O2 -o pkgsize pkgsize.c -lalpm `pkg-config --cflags --libs glib-2.0`
$ ./pkgsize `pacman -Qqet` > pkgsizes.csv

Then you could load up the .csv file in e.g. LibreOffice.  Just a bit of the output on my machine as a sample (sorted in LibreOffice):

kdesdk-kcachegrind,1148818432
massif-visualizer,984382464
libreoffice-calc,944943104
libreoffice-writer,936892416
wxmaxima,834814976
skype,782191616

So the biggest packages are KCachegrind and Massif-Visualizer (because of the KDE dependencies), followed by LibreOffice.

And the code:

#include <stdio.h>
#include <stdlib.h>

#include <alpm.h>
#include <glib.h>

void abort_msg (const char * msg)
{
   fprintf (stderr, "%s\n", msg);
   abort ();
}

void add_pkg_deps (GHashTable * hash, alpm_pkg_t * pkg, alpm_list_t * cache)
{
   const char * pkgname = alpm_pkg_get_name (pkg);

   if (g_hash_table_contains (hash, pkgname))
      return;

   g_hash_table_insert (hash, (void *) pkgname, pkg);

   alpm_list_t * deps = alpm_pkg_get_depends (pkg);
   while (deps)
   {
      alpm_depend_t * dep = alpm_list_getdata (deps);

      alpm_pkg_t * prov = alpm_find_satisfier (cache, dep->name);
      if (! prov)
         abort_msg ("alpm_find_satisfier() failed");

      add_pkg_deps (hash, prov, cache);

      deps = alpm_list_next (deps);
   }
}

int main (int argc, const char * * argv)
{
   alpm_handle_t * pm = alpm_initialize ("/", "/var/lib/pacman", NULL);
   if (! pm)
      abort_msg ("alpm_initialize() failed");

   alpm_db_t * db = alpm_option_get_localdb (pm);
   if (! db)
      abort_msg ("alpm_option_get_localdb() failed");

   alpm_list_t * cache = alpm_db_get_pkgcache (db);
   if (! cache)
      abort_msg ("alpm_db_get_pkgcache() failed");

   for (int i = 1; i < argc; i ++)
   {
      const char * pkgname = argv[i];

      alpm_pkg_t * pkg = alpm_db_get_pkg (db, pkgname);
      if (! pkg)
         abort_msg ("alpm_db_get_pkg() failed");

      GHashTable * hash = g_hash_table_new (g_str_hash, g_str_equal);
      add_pkg_deps (hash, pkg, cache);

      GHashTableIter iter;
      long long totalsize = 0;

      g_hash_table_iter_init (& iter, hash);
      while (g_hash_table_iter_next (& iter, NULL, (void * *) & pkg))
         totalsize += alpm_pkg_get_isize (pkg);

      g_hash_table_destroy (hash);

      printf ("%s,%lld\n", pkgname, totalsize);
   }

   alpm_release (pm);

   return 0;
}

Offline

#2 2013-03-29 19:15:57

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Calculate total installed package sizes (including dependencies)

Allan has created something similar: bigpkg https://aur.archlinux.org/packages/bigpkg/


Edit: The output is somewhat different

$ cat pkgsizes.csv | tr , ' '| sort -nk 2 | tail
wine 285632512
armh 306441216
linux 346865664
clickosquare 390063104
openbox-menu 390992896
qiv 395260928
mplayer2 423473152
flashplugin 430296064
firefox 483309568
dwb-hg 527251456
$ ./bigpkg | tail
 55.5 MB:    festival-us
 61.7 MB:    mplayer2
 68.1 MB:    xnviewmp
 69.8 MB:    firefox
 74.0 MB:    dwb-hg
 94.6 MB:    libtool
111.4 MB:    linux
126.9 MB:    wine
175.8 MB:    ocaml-findlib

because bigpkg https://bbs.archlinux.org/viewtopic.php?id=73098

Allan wrote:

works by calculating the size of a package as its actual size plus the sum of its share of its dependencies (for each dependency, add the dep size divided by the number of packages needing that dep).

Last edited by karol (2013-03-29 19:32:14)

Offline

Board footer

Powered by FluxBB