Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update.cpp « src « update - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62a30ebd5d401ae51a51fa09acc0b549ff1af096 (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
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
#ifdef _WIN32
#  include <windows.h>
#endif

#include <chrono>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "../../server/TracyFileRead.hpp"
#include "../../server/TracyFileWrite.hpp"
#include "../../server/TracyPrint.hpp"
#include "../../server/TracyVersion.hpp"
#include "../../server/TracyWorker.hpp"
#include "../../zstd/zstd.h"
#include "../../getopt/getopt.h"

#ifdef __APPLE__
#  define ftello64(x) ftello(x)
#elif defined _WIN32
#  define ftello64(x) _ftelli64(x)
#endif

void Usage()
{
    printf( "Usage: update [options] input.tracy output.tracy\n\n" );
    printf( "  -h: enable LZ4HC compression\n" );
    printf( "  -e: enable extreme LZ4HC compression (very slow)\n" );
    printf( "  -z level: use Zstd compression with given compression level\n" );
    printf( "  -d: build dictionary for frame images\n" );
    printf( "  -s flags: strip selected data from capture:\n" );
    printf( "      l: locks, m: messages, p: plots, M: memory, i: frame images\n" );
    printf( "      c: context switches, s: sampling data, C: symbol code, S: source cache\n" );
    printf( "  -c: scan for source files missing in cache and add if found\n" );
    exit( 1 );
}

int main( int argc, char** argv )
{
#ifdef _WIN32
    if( !AttachConsole( ATTACH_PARENT_PROCESS ) )
    {
        AllocConsole();
        SetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), 0x07 );
    }
#endif

    tracy::FileWrite::Compression clev = tracy::FileWrite::Compression::Fast;
    uint32_t events = tracy::EventType::All;
    int zstdLevel = 1;
    bool buildDict = false;
    bool cacheSource = false;
    int c;
    while( ( c = getopt( argc, argv, "hez:ds:c" ) ) != -1 )
    {
        switch( c )
        {
        case 'h':
            clev = tracy::FileWrite::Compression::Slow;
            break;
        case 'e':
            clev = tracy::FileWrite::Compression::Extreme;
            break;
        case 'z':
            clev = tracy::FileWrite::Compression::Zstd;
            zstdLevel = atoi( optarg );
            if( zstdLevel > ZSTD_maxCLevel() || zstdLevel < ZSTD_minCLevel() )
            {
                printf( "Available Zstd compression levels range: %i - %i\n", ZSTD_minCLevel(), ZSTD_maxCLevel() );
                exit( 1 );
            }
            break;
        case 'd':
            buildDict = true;
            break;
        case 's':
        {
            auto ptr = optarg;
            do
            {
                switch( *optarg )
                {
                case 'l':
                    events &= ~tracy::EventType::Locks;
                    break;
                case 'm':
                    events &= ~tracy::EventType::Messages;
                    break;
                case 'p':
                    events &= ~tracy::EventType::Plots;
                    break;
                case 'M':
                    events &= ~tracy::EventType::Memory;
                    break;
                case 'i':
                    events &= ~tracy::EventType::FrameImages;
                    break;
                case 'c':
                    events &= ~tracy::EventType::ContextSwitches;
                    break;
                case 's':
                    events &= ~tracy::EventType::Samples;
                    break;
                case 'C':
                    events &= ~tracy::EventType::SymbolCode;
                    break;
                case 'S':
                    events &= ~tracy::EventType::SourceCache;
                    break;
                default:
                    Usage();
                    break;
                }
            }
            while( *++optarg != '\0' );
            break;
        }
        case 'c':
            cacheSource = true;
            break;
        default:
            Usage();
            break;
        }
    }
    if( argc - optind != 2 ) Usage();

    const char* input = argv[optind];
    const char* output = argv[optind+1];

    printf( "Loading...\r" );
    fflush( stdout );
    auto f = std::unique_ptr<tracy::FileRead>( tracy::FileRead::Open( input ) );
    if( !f )
    {
        fprintf( stderr, "Cannot open input file!\n" );
        exit( 1 );
    }

    try
    {
        int64_t t;
        float ratio;
        int inVer;
        {
            const auto t0 = std::chrono::high_resolution_clock::now();
            tracy::Worker worker( *f, (tracy::EventType::Type)events, false );

#ifndef TRACY_NO_STATISTICS
            while( !worker.AreSourceLocationZonesReady() ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
#endif

            if( cacheSource ) worker.CacheSourceFiles();

            auto w = std::unique_ptr<tracy::FileWrite>( tracy::FileWrite::Open( output, clev, zstdLevel ) );
            if( !w )
            {
                fprintf( stderr, "Cannot open output file!\n" );
                exit( 1 );
            }
            printf( "Saving... \r" );
            fflush( stdout );
            worker.Write( *w, buildDict );
            w->Finish();
            const auto t1 = std::chrono::high_resolution_clock::now();
            const auto stats = w->GetCompressionStatistics();
            ratio = 100.f * stats.second / stats.first;
            inVer = worker.GetTraceVersion();
            t = std::chrono::duration_cast<std::chrono::nanoseconds>( t1 - t0 ).count();
        }

        FILE* in = fopen( input, "rb" );
        fseek( in, 0, SEEK_END );
        const auto inSize = ftello64( in );
        fclose( in );

        FILE* out = fopen( output, "rb" );
        fseek( out, 0, SEEK_END );
        const auto outSize = ftello64( out );
        fclose( out );

        printf( "%s (%i.%i.%i) {%s} -> %s (%i.%i.%i) {%s, %.2f%%}  %s, %.2f%% change\n",
            input, inVer >> 16, ( inVer >> 8 ) & 0xFF, inVer & 0xFF, tracy::MemSizeToString( inSize ),
            output, tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch, tracy::MemSizeToString( outSize ), ratio,
            tracy::TimeToString( t ), float( outSize ) / inSize * 100 );
    }
    catch( const tracy::UnsupportedVersion& e )
    {
        fprintf( stderr, "The file you are trying to open is from the future version.\n" );
        exit( 1 );
    }
    catch( const tracy::NotTracyDump& e )
    {
        fprintf( stderr, "The file you are trying to open is not a tracy dump.\n" );
        exit( 1 );
    }
    catch( const tracy::FileReadError& e )
    {
        fprintf( stderr, "The file you are trying to open cannot be mapped to memory.\n" );
        exit( 1 );
    }
    catch( const tracy::LegacyVersion& e )
    {
        fprintf( stderr, "The file you are trying to open is from a legacy version.\n" );
        exit( 1 );
    }

    return 0;
}