File: ft_main.cpp

package info (click to toggle)
apitrace 11.1%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,648 kB
  • sloc: cpp: 183,110; python: 33,685; ansic: 25,073; sh: 143; makefile: 88
file content (279 lines) | stat: -rw-r--r-- 8,706 bytes parent folder | download | duplicates (2)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*********************************************************************
 *
 * Copyright 2020 Collabora Ltd
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *********************************************************************/

#include "ft_frametrimmer.hpp"

#include "os_time.hpp"
#include "trace_parser.hpp"
#include "retrace.hpp"
#include "retrace_swizzle.hpp"

#include "trace_callset.hpp"
#include "trace_parser.hpp"
#include "trace_writer.hpp"

#include <limits.h> // for CHAR_MAX
#include <getopt.h>
#include <memory>
#include <queue>

using namespace frametrim;

struct trim_options {
    /* Frames to be included in trace. */
    trace::CallSet setupframes;

    /* Frames to keep replayable */
    trace::CallSet frames;

    unsigned top_frame_call_counts;
    bool keep_all_states;

    /* Output filename */
    std::string output;
};

static const char *synopsis = "Create a new, retracable trace containing only the specified frames.";

static void
usage(void)
{
    std::cout
            << "usage: frametrim [OPTIONS] TRACE_FILE...\n"
            << synopsis << "\n"
                           "\n"
                           "    -h, --help               Show detailed help for trim options and exit\n"
                           "    -f, --frames=FRAME       Frame the trace should be reduced to.\n"
                           "    -s, --setupframes=FRAME  Frame that are kept in the trace but but without the end-of-frame command.\n"
                           "    -t, --top-calls-per-frame=NUMBER Print NUMBER of frames with the top amount of OpenGL calls\n"
                           "    -k, --keep-all-states    Keep all state calls in the trace (This may help with textures that are created by using FBO\n"
                           "    -o, --output=TRACE_FILE  Output trace file\n"
               ;
}

enum {
    FRAMES_OPT = 'f',
    SETUPFRAMES_OPT = 's'
};

const static char *
shortOptions = "t:hko:f:s:x";

bool operator < (std::pair<unsigned, unsigned>& lhs, std::pair<unsigned, unsigned>& rhs)
{
    return lhs.first < rhs.first;
}

const static struct option
longOptions[] = {
    {"help", no_argument, 0, 'h'},
    {"top-calls-per-frame", required_argument, 0, 't'},
    {"frames", required_argument, 0, 'f'},
    {"setupframes", required_argument, 0, 's'},
    {"keep-all-states", no_argument, 0, 'k'},
    {"output", required_argument, 0, 'o'},
    {0, 0, 0, 0}
};

static int trim_to_frame(const char *filename,
                         const struct trim_options& options)
{

    trace::Parser p;
    unsigned frame;

    if (!p.open(filename)) {
        std::cerr << "error: failed to open " << filename << "\n";
        return 1;
    }

    if (options.frames.getLast() < options.setupframes.getLast() &&
        !options.setupframes.empty()) {
        std::cerr << "error: last frame to keep ("
                  << options.frames.getLast()
                  << ") must be larger than last key frame"
                  << options.setupframes.getLast() << "\n";
        return 1;
    }

    auto out_filename = options.output;

    /* Prepare output file and writer for output. */
    if (options.output.empty()) {
        os::String base(filename);
        base.trimExtension();

        out_filename = std::string(base.str()) + std::string("-trim.trace");
    }

    FrameTrimmer trimmer(options.keep_all_states);

    frame = 0;
    uint64_t callid = 0;
    std::unique_ptr<trace::Call> call(p.parse_call());
    std::priority_queue<std::pair<unsigned, unsigned>> calls_in_frame;

    unsigned ncalls = 0;
    unsigned ncalls2 = 0;

    unsigned calls_in_this_frame = 0;
    while (call) {
        /* There's no use doing any work past the last call and frame
        * requested by the user. */
        if (frame > options.frames.getLast()) {
            break;
        }

        Frametype ft = ft_none;
        if (options.setupframes.contains(frame, call->flags))
            ft = ft_key_frame;
        if (options.frames.contains(frame, call->flags))
            ft = ft_retain_frame;

        trimmer.call(*call, ft);

        if (call->flags & trace::CALL_FLAG_END_FRAME) {
            if (options.top_frame_call_counts > 0) {
                ncalls += calls_in_this_frame;
                ncalls2 += calls_in_this_frame * calls_in_this_frame;
                calls_in_frame.push(std::make_pair(calls_in_this_frame, frame));
            }
            calls_in_this_frame = 0;
            frame++;
        }

        callid++;
        if (!(callid & 0xff))
            std::cerr << "\rScanning frame:" << frame
                      << " type:" << ft
                      << " call:" << call->no;

        call.reset(p.parse_call());
        ++calls_in_this_frame;
    }
    trimmer.finalize();
    std::cerr << "\nDone scanning frames\n";

    trace::Writer writer;
    if (!writer.open(out_filename.c_str(), p.getVersion(), p.getProperties())) {
        std::cerr << "error: failed to create " << out_filename << "\n";
        return 2;
    }

    auto call_ids = trimmer.getUniqueCallIds();
    std::cerr << "Write output file\n";

    p.close();
    p.open(filename);
    call.reset(p.parse_call());

    std::cerr << "Copying " << call_ids.size() << " calls\n";

    while (1) {
        while (call && call_ids.find(call->no) == call_ids.end())
            call.reset(p.parse_call());

        if (!call)
            break;

        writer.writeCall(call.get());
        call.reset(p.parse_call());
    }

    if (options.top_frame_call_counts) {
        unsigned count = options.top_frame_call_counts;
        std::cerr << "Calls per frame:\n";
        while (count-- && !calls_in_frame.empty()) {
            auto fc = calls_in_frame.top();
            calls_in_frame.pop();
            std::cerr << "  Frame[" << fc.second << "] = " << fc.first << "\n";
        }
    }

    return 0;
}


int main(int argc, char **argv)
{
    struct trim_options options;
    options.frames = trace::CallSet(trace::FREQUENCY_NONE);
    options.top_frame_call_counts = false;
    options.keep_all_states = false;

    int opt;
    while ((opt = getopt_long(argc, argv, shortOptions, longOptions, nullptr)) != -1) {
        switch (opt) {
        case 'h':
            usage();
            return 0;
        case FRAMES_OPT:
            options.frames.merge(optarg);
            break;
        case SETUPFRAMES_OPT:
            options.setupframes.merge(optarg);
            break;
        case 'o':
            options.output = optarg;
            break;
        case 't':
            options.top_frame_call_counts = atoi(optarg);
            break;
        case 'k':
            options.keep_all_states = true;
            break;
        default:
            std::cerr << "error: unexpected option `" << (char)opt << "`\n";
            usage();
            return 1;
        }
    }

    if (optind >= argc) {
        std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
        usage();
        return 1;
    }

    if (argc > optind + 1) {
        std::cerr << "error: extraneous arguments:";
        for (int i = optind + 1; i < argc; i++) {
            std::cerr << " " << argv[i];
        }
        std::cerr << "\n";
        usage();
        return 1;
    }

    std::cerr << "gltrim may not retain all information required to recreate\n"
              << "the target frame(s) as expected, it is advised to check the\n"
              << "results and add setup frames and additional states if needed.\n"
                 "For further details see frametrim/frametrim.markdown in the "
                 "source code.\n";

    return trim_to_frame(argv[optind], options);
}