File: bootio.cpp

package info (click to toggle)
android-platform-tools 29.0.6-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 365,224 kB
  • sloc: cpp: 1,049,638; java: 460,532; ansic: 375,452; asm: 301,257; xml: 134,509; python: 92,731; perl: 62,008; sh: 26,753; makefile: 3,210; javascript: 3,172; yacc: 1,403; lex: 455; awk: 368; ruby: 183; sql: 140
file content (162 lines) | stat: -rw-r--r-- 5,142 bytes parent folder | download | duplicates (6)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// The bootio tool provides options to collect I/O stats for processes during boot.

#include <vector>
#include <getopt.h>
#include <unistd.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <log/log.h>

#include "bootio_collector.h"

namespace android {

#define LOG_ROOT           "/data/misc/bootio"
#define LOG_START_FILE      LOG_ROOT"/start"
#define SELF_IO            "/proc/self/io"

static const int LOG_TIMEOUT_INDEX = 0;
static const int LOG_SAMPLES_INDEX = 1;
static const int LOG_MAX_TIMEOUT = 120;
static const int LOG_MAX_SAMPLES = 30;

void ShowHelp(const char *cmd) {
    fprintf(stderr, "Usage: %s [options]\n", cmd);
    fprintf(stderr,
            "options include:\n"
                    "  -h, --help            Show this help\n"
                    "  -p, --print           Dump the boot io data to the console\n"
                    "\nNo options will start data collection process.\n");
}

void PrintBootIo() {
    printf("Boot I/O:\n");
    printf("------------\n");
    std::unique_ptr <BootioCollector> collector(new BootioCollector(LOG_ROOT));
    if (collector.get() == NULL) {
        LOG(ERROR) << "Failed to create data collector";
        return;
    }
    collector->Print();
}

void StartDataCollection() {
    if (access(SELF_IO, F_OK) == -1) {
        LOG(ERROR) << "Kernel doesn't support I/O profiling.";
        printf("Kernel doesn't support I/O profiling.");
        return;
    }

    int timeout = 0;
    int samples = 0;

    std::string start;
    android::base::ReadFileToString(LOG_START_FILE, &start);

    if (!start.empty()) {
        std::vector <std::string> components = android::base::Split(start, " ");
        if (components.size() != 2) {
            LOG(ERROR) << "Invalid value in start file." << start;
            return;
        }
        timeout = atoi(components.at(LOG_TIMEOUT_INDEX).c_str());
        samples = atoi(components.at(LOG_SAMPLES_INDEX).c_str());
    } else {
        LOG(INFO) << "No profiling requested. Exiting";
        printf("Boot I/O: no profiling requested. Exiting.\n");
        return;
    }
    if (timeout <= 0 || samples <= 0) {
        LOG(ERROR) << "Boot I/O: failed to parse string:" << start;
        printf("Boot I/O: failed to parse string: %s\n", start.c_str());
        return;
    }
    if (samples > timeout || samples > LOG_MAX_SAMPLES || timeout > LOG_MAX_TIMEOUT) {
        LOG(ERROR) << "Bad values for bootio. timeout=" << timeout <<
        " samples=" << samples << " Max timeout=" << LOG_MAX_TIMEOUT <<
        " Max samples=" << LOG_MAX_SAMPLES;
        return;
    }
    LOG(INFO) << "Boot I/O: collecting data. samples=" << samples << "timeout=" << timeout;
    printf("Boot I/O: collecting data\ntimeout=%d, samples=%d\n",
           timeout, samples);
    std::unique_ptr <BootioCollector> collector(new BootioCollector(LOG_ROOT));
    if (collector.get() == NULL) {
        LOG(ERROR) << "Failed to create data collector";
        return;
    }
    collector->StartDataCollection(timeout, samples);
}

}

int main(int argc, char **argv) {
    android::base::InitLogging(argv);

    LOG(INFO) << "Bootio started";

    int optionIndex = 0;
    static const struct option longOptions[] = {
            {"help",  no_argument, NULL, 'h'},
            {"print", no_argument, NULL, 'p'},
            {NULL,    0,           NULL, 0}
    };

    int opt = 0;
    bool startCollection = true;
    while ((opt = getopt_long(argc, argv, "hlpr:", longOptions, &optionIndex)) != -1) {
        switch (opt) {
            case 0: {
                const std::string option_name = longOptions[optionIndex].name;
                LOG(ERROR) << "Invalid option: " << option_name;
                break;
            }

            case 'h': {
                android::ShowHelp(argv[0]);
                startCollection = false;
                break;
            }

            case 'p': {
                android::PrintBootIo();
                startCollection = false;
                break;
            }

            default: {
                DCHECK_EQ(opt, '?');

                // |optopt| is an external variable set by getopt representing
                // the value of the invalid option.
                LOG(ERROR) << "Invalid option: " << optopt;
                android::ShowHelp(argv[0]);
                return EXIT_FAILURE;
            }
        }
    }

    if (startCollection) {
        android::StartDataCollection();
    }

    return 0;
}