summaryrefslogtreecommitdiff
path: root/src/readahead/readahead-analyze.c
blob: 9c328a5f58f12a5ac21979a8ffbc95902e1ca4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2012 Auke Kok <auke-jan.h.kok@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>

#include "readahead-common.h"

int main_analyze(const char *pack_path)
{
        char line[1024];
        char path[PATH_MAX];
        FILE *pack;
        int a;
        int missing = 0;
        off_t size;
        long tsize = 0;
        uint64_t inode;
        uint32_t b;
        uint32_t c;
        struct stat st;

        if (!pack_path)
                pack_path = "/.readahead";

        pack = fopen(pack_path, "r");
        if (!pack) {
                fprintf(stderr, "Pack file missing\n");
                return EXIT_FAILURE;
        }

        if (!(fgets(line, sizeof(line), pack))) {
                fprintf(stderr, "Pack file corrupt\n");
                return EXIT_FAILURE;
        }

        if (!strstr(line, READAHEAD_PACK_FILE_VERSION)) {
                fprintf(stderr, "Pack file version incompatible with this parser\n");
                return EXIT_FAILURE;
        }

        if ((a = getc(pack)) == EOF) {
                fprintf(stderr, "Pack file corrupt\n");
                return 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;

                if (fread(&inode, sizeof(inode), 1, pack) != 1) {
                        fprintf(stderr, "Pack file corrupt\n");
                        return EXIT_FAILURE;
                }

                while (true) {
                        if (fread(&b, sizeof(b), 1, pack) != 1  ||
                            fread(&c, sizeof(c), 1, pack) != 1) {
                                fprintf(stderr, "Pack file corrupt\n");
                                return 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 * page_size();

                        tsize += size;

                        fprintf(stdout, "  %4d%% (%2d) %12ld: %s\n",
                                sections ? (int)(size / st.st_size * 100.0) : 100,
                                sections ? sections : 1,
                                (unsigned long)size,
                                path);
                } else {
                        fprintf(stdout, "  %4dp (%2d) %12s: %s (MISSING)\n",
                                sections ? pages : -1,
                                sections ? sections : 1,
                                "???",
                                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);

        return EXIT_SUCCESS;
}