Repository: wolfpld/tracy
File: import-chrome/src/import-chrome.cpp (C++)

Code before:
        std::ifstream is( input );
        if( !is.is_open() )
        {
            fprintf( stderr, "Cannot open input file!\n" );
            exit( 1 );
        }
        is >> j;
        is.close();
    }

    printf( "\33[2KParsing...\r" );
    fflush( stdout );

    struct PidTid {
      uint64_t tid;
      uint64_t pid;
      uint64_t both; // fake thread id, unique within Tracy
    };

    std::vector<PidTid> tids;
    std::vector<tracy::Worker::ImportEventTimeline> timeline;
    std::vector<tracy::Worker::ImportEventMessages> messages;
    std::vector<tracy::Worker::ImportEventPlots> plots;
    std::unordered_map<uint64_t, std::string> threadNames;

    const auto getTid = [&](uint64_t pid, uint64_t tid) -> uint64_t {
        for ( auto &pair : tids ) {
            if ( pair.pid == pid && pair.tid == tid ) {
                return pair.both;
            }
        }

        const auto result = tids.size();
        tids.emplace_back(PidTid {.tid=tid, .pid=pid, .both=result});
        return result;
    };

    if( j.is_object() && j.contains( "traceEvents" ) )
    {
        j = j["traceEvents"];
    }

    if( !j.is_array() )
    {
        fprintf( stderr, "Input must be either an array of events or an object containing an array of events under \"traceEvents\" key.\n" );
        exit( 1 );
    }

    for( auto& v : j )
    {
        const auto type = v["ph"].get<std::string>();

Review comment (suggestion):
This has to be better named. There are two different meanings for `tid`. At the same time, `tid` and `both` are used as aliases.

Code after:
        std::ifstream is( input );
        if( !is.is_open() )
        {
            fprintf( stderr, "Cannot open input file!\n" );
            exit( 1 );
        }
        is >> j;
        is.close();
    }

    printf( "\33[2KParsing...\r" );
    fflush( stdout );

    // encode a pair of "real pid, real tid" from a trace into a
    // pseudo thread ID living in the single namespace of Tracy threads.
    struct PidTidEncoder {
      uint64_t tid;
      uint64_t pid;
      uint64_t pseudo_tid; // fake thread id, unique within Tracy
    };

    std::vector<PidTidEncoder> tid_encoders;
    std::vector<tracy::Worker::ImportEventTimeline> timeline;
    std::vector<tracy::Worker::ImportEventMessages> messages;
    std::vector<tracy::Worker::ImportEventPlots> plots;
    std::unordered_map<uint64_t, std::string> threadNames;

    const auto getPseudoTid = [&](json& val) -> uint64_t {
        const auto real_tid = val["tid"].get<uint64_t>();

        if ( val.contains( "pid" ) ) {
            // there might be multiple processes so we allocate a pseudo-tid
            // for each pair (pid, real_tid)
            const auto pid = val["pid"].get<uint64_t>();

            for ( auto &pair : tid_encoders) {
              if ( pair.pid == pid && pair.tid == real_tid ) {
                return pair.pseudo_tid;
              }
            }

            const auto pseudo_tid = tid_encoders.size();
            tid_encoders.emplace_back(PidTidEncoder {real_tid, pid, pseudo_tid});
            return pseudo_tid;
        }
        else
        {
          return real_tid;
        }
    };