[systemd-devel] [PATCH] readahead: Add tool to analyze the contents of the pack file.

Auke Kok auke-jan.h.kok at intel.com
Sat Jun 2 18:48:04 PDT 2012


This patch adds code to compile 'systemd-readahead-analyze' and install
it into $bindir.

Use this program to parse the contents of the readahead pack file, or
an arbitrary pack file and display which files are listed in it, and
how much of the files are requested to be readahead.

This code is not new - it's partially taken from sreadahead (formerly
maintained by Arjan van der Ven and me, and was originally written
by me), and adapted with the right bits to parse the systemd
readahead pack files, which are slightly different in format.

Signed-off-by: Auke Kok <auke-jan.h.kok at intel.com>
---
 Makefile.am                       |    6 ++
 src/readahead/readahead-analyze.c |  122 +++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+)
 create mode 100644 src/readahead/readahead-analyze.c

diff --git a/Makefile.am b/Makefile.am
index 597711e..ab5b67a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2407,6 +2407,9 @@ systemd_readahead_replay_LDADD = \
 	libsystemd-daemon.la \
 	libudev.la
 
+systemd_readahead_analyze_SOURCES = \
+	src/readahead/readahead-analyze.c
+
 pkginclude_HEADERS += \
 	src/systemd/sd-readahead.h
 
@@ -2414,6 +2417,9 @@ rootlibexec_PROGRAMS += \
 	systemd-readahead-collect \
 	systemd-readahead-replay
 
+bin_PROGRAMS += \
+	systemd-readahead-analyze
+
 dist_systemunit_DATA += \
 	units/systemd-readahead-drop.service \
 	units/systemd-readahead-done.timer
diff --git a/src/readahead/readahead-analyze.c b/src/readahead/readahead-analyze.c
new file mode 100644
index 0000000..944214b
--- /dev/null
+++ b/src/readahead/readahead-analyze.c
@@ -0,0 +1,122 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2012 Auke Kok <auke-jan.h.kok at intel.com>
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <linux/limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+
+
+int main(int argc, char *argv[])
+{
+        char line[1024];
+        char path[PATH_MAX];
+        FILE *pack;
+        int a;
+        int missing = 0;
+        off_t size;
+        long tsize = 0;
+        uint32_t b;
+        uint32_t c;
+        struct stat st;
+        int pagesize = getpagesize();
+
+        if (argc != 2)
+                snprintf(path, PATH_MAX, "/.readahead");
+        else
+                snprintf(path, PATH_MAX, "%s", argv[1]);
+
+        pack = fopen(path, "r");
+        if (!pack) {
+                fprintf(stderr, "Pack file missing\n");
+                exit(EXIT_FAILURE);
+        }
+
+        if (!(fgets(line, sizeof(line), pack))) {
+                fprintf(stderr, "Pack file corrupt\n");
+                exit(EXIT_FAILURE);
+        }
+
+        if ((a = getc(pack)) == EOF) {
+                fprintf(stderr, "Pack file corrupt\n");
+                exit(EXIT_FAILURE);
+        }
+
+        fprintf(stdout, "   pct  sections     size: path\n");
+        fprintf(stdout, "   ===  ========     ====: ====\n");
+
+        while(true) {
+                int pages = 0;
+                int sections = 0;
+
+                if (!fgets(path, sizeof(path), pack))
+                        break; /* done */
+
+                path[strlen(path)-1] = 0;
+
+                while (true) {
+                        if (fread(&b, sizeof(b), 1, pack) != 1  ||
+                            fread(&c, sizeof(c), 1, pack) != 1) {
+                                fprintf(stderr, "Pack file corrupt\n");
+                                exit(EXIT_FAILURE);
+                        }
+                        if ((b == 0) && (c == 0))
+                                break;
+
+                        /* Uncomment this to get all the chunks separately
+                        fprintf(stdout, " %d: %d %d\n", sections, b, c);
+                         */
+
+                        pages += (c - b);
+                        sections++;
+                }
+
+                if (stat(path, &st) == 0) {
+                        if (sections == 0)
+                                size = st.st_size;
+                        else
+                                size = pages * pagesize;
+
+                        tsize += size;
+                        if (sections)
+                                fprintf(stdout, "  %4d%% (%2d) %12ld: %s\n", (int)(size / st.st_size * 100.0), sections, (unsigned long)size, path);
+                        else
+                                fprintf(stdout, "  %4d%% (%2d) %12ld: %s\n", 100, 1, (unsigned long)size, path);
+                } else {
+                        fprintf(stdout, "  %4s%% (%2s) %12s: %s (MISSING)\n", "???", "??", "???", path);
+                        missing++;
+                }
+
+        }
+
+        fprintf(stdout, "\nHOST:    %s", line);
+        fprintf(stdout, "TYPE:    %c\n", a);
+        fprintf(stdout, "MISSING: %d\n", missing);
+        fprintf(stdout, "TOTAL:   %ld\n", tsize);
+
+        exit(EXIT_SUCCESS);
+}
-- 
1.7.10



More information about the systemd-devel mailing list