1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Replayer - Main.cpp
19 *
20 * 1. Get flags from command line
21 * 2. Commit actions or settings based on the flags
22 * 3. Initalize a replayer object with the filename passed in
23 * 4. Replay
24 * 5. Exit successfully or print error statement
25 */
26
27#include <Replayer.h>
28
29#include <csignal>
30#include <iostream>
31#include <stdlib.h>
32#include <unistd.h>
33
34using namespace android;
35
36void printHelpMenu() {
37    std::cout << "SurfaceReplayer options:\n";
38    std::cout << "Usage: surfacereplayer [OPTIONS...] <TRACE FILE>\n";
39    std::cout << "  File path must be absolute" << std::endl << std::endl;
40
41    std::cout << "  -m  Stops the replayer at the start of the trace and switches ";
42                 "to manual replay\n";
43
44    std::cout << "\n  -t [Number of Threads]  Specifies the number of threads to be used while "
45                 "replaying (default is " << android::DEFAULT_THREADS << ")\n";
46
47    std::cout << "\n  -s [Timestamp]  Specify at what timestamp should the replayer switch "
48                 "to manual replay\n";
49
50    std::cout << "  -n  Ignore timestamps and run through trace as fast as possible\n";
51
52    std::cout << "  -l  Indefinitely loop the replayer\n";
53
54    std::cout << "  -h  Display help menu\n";
55
56    std::cout << std::endl;
57}
58
59int main(int argc, char** argv) {
60    std::string filename;
61    bool loop = false;
62    bool wait = true;
63    bool pauseBeginning = false;
64    int numThreads = DEFAULT_THREADS;
65    long stopHere = -1;
66
67    int opt = 0;
68    while ((opt = getopt(argc, argv, "mt:s:nlh?")) != -1) {
69        switch (opt) {
70            case 'm':
71                pauseBeginning = true;
72                break;
73            case 't':
74                numThreads = atoi(optarg);
75                break;
76            case 's':
77                stopHere = atol(optarg);
78                break;
79            case 'n':
80                wait = false;
81                break;
82            case 'l':
83                loop = true;
84                break;
85            case 'h':
86            case '?':
87                printHelpMenu();
88                exit(0);
89            default:
90                std::cerr << "Invalid argument...exiting" << std::endl;
91                printHelpMenu();
92                exit(0);
93        }
94    }
95
96    char** input = argv + optind;
97    if (input[0] == NULL) {
98        std::cerr << "No trace file provided...exiting" << std::endl;
99        abort();
100    }
101    filename.assign(input[0]);
102
103    status_t status = NO_ERROR;
104    do {
105        android::Replayer r(filename, pauseBeginning, numThreads, wait, stopHere);
106        status = r.replay();
107    } while(loop);
108
109    if (status == NO_ERROR) {
110        std::cout << "Successfully finished replaying trace" << std::endl;
111    } else {
112        std::cerr << "Trace replayer returned error: " << status << std::endl;
113    }
114
115    return 0;
116}
117