<commit_before>//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// 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.
//*****************************************************************************

#include <iostream>
#include <map>
#include <sstream>
#include <string>

#include "chrome_trace.hpp"
#include "ngraph/env_util.hpp"
#include "ngraph/log.hpp"

using namespace std;
using namespace ngraph;

static bool read_tracing_env_var()
{
    static const bool is_enabled = getenv_bool("NGRAPH_ENABLE_TRACING");

    return is_enabled;
}

mutex runtime::event::Manager::s_file_mutex;
bool runtime::event::Manager::s_tracing_enabled = read_tracing_env_var();

runtime::event::Duration::Duration(const string& name, const string& category, const string& args)
{
    if (Manager::is_tracing_enabled())
    {
        m_start = Manager::get_current_microseconds();
        m_stop = 0;
        m_name = name;
        m_category = category;
        m_args = args;
    }
}

void runtime::event::Duration::stop()
{
    if (Manager::is_tracing_enabled())
    {
        m_stop = Manager::get_current_microseconds();
    }
}

void runtime::event::Duration::write()
{
    if (Manager::is_tracing_enabled())
    {
        size_t stop_time = (m_stop != 0 ? m_stop : Manager::get_current_microseconds());

        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }

        Manager::get_output_stream() <<
            R"({"name":")" << m_name << R"(","cat":")" << m_category << R"(","ph":"X","pid":)"
                                     << Manager::get_process_id() << R"(,"tid":)"
                                     << Manager::get_thread_id() <<
            R"(,"ts":)" << m_start << R"(,"dur":)" << (stop_time - m_start);
        if (!m_args.empty())
        {
            out <<
                R"(,"args":)" << m_args;
        }
        out << "}";
    }
}

runtime::event::Object::Object(const string& name, const string& args)
    : m_name{name}
    , m_id{static_cast<size_t>(chrono::high_resolution_clock::now().time_since_epoch().count())}
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }
        out << R"({"name":")" << m_name << R"(","ph":"N","id":")" << m_id <<
            R"(","ts":)" << Manager::get_current_microseconds() <<
            R"(,"pid":)" << Manager::get_process_id() << R"(,"tid":)" << Manager::get_thread_id();
        if (!args.empty())
        {
            out <<
                R"(,"args":)" << args;
        }
        out << "}";

        write_snapshot(out, args);
    }
}

void runtime::event::Object::snapshot(const string& args)
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }
        write_snapshot(out, args);
    }
}

void runtime::event::Object::write_snapshot(ostream& out, const string& args)
{
    out << R"({"name":")" << m_name << R"(","ph":"O","id":")" << m_id <<
        R"(","ts":)" << Manager::get_current_microseconds() <<
        R"(,"pid":)" << Manager::get_process_id() << R"(,"tid":)" << Manager::get_thread_id();
    if (!args.empty())
    {
        out <<
            R"(,"args":)" << args;
    }
    out << "}";
}

void runtime::event::Object::destroy()
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }
        out << R"({"name":")" << m_name << R"(","ph":"D","id":")" << m_id <<
            R"(","ts":)" << Manager::get_current_microseconds() <<
            R"(,"pid":)" << Manager::get_process_id() << R"(,"tid":)" << Manager::get_thread_id()
            << "}";
    }
}

void runtime::event::Manager::open(const string& path)
{
    ofstream& out = get_output_stream();
    if (out.is_open() == false)
    {
        out.open(path, ios_base::trunc);
        out << "[\n";
    }
}

void runtime::event::Manager::close()
{
    ofstream& out = get_output_stream();
    if (out.is_open())
    {
        out << "\n]\n";
        out.close();<commit_msg>Change chrome trace to use string concat instead of streams (#4399)<commit_after>//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// 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.
//*****************************************************************************

#include <iostream>
#include <map>
#include <sstream>
#include <string>

#include "chrome_trace.hpp"
#include "ngraph/env_util.hpp"
#include "ngraph/log.hpp"

using namespace std;
using namespace ngraph;

static bool read_tracing_env_var()
{
    static const bool is_enabled = getenv_bool("NGRAPH_ENABLE_TRACING");

    return is_enabled;
}

mutex runtime::event::Manager::s_file_mutex;
bool runtime::event::Manager::s_tracing_enabled = read_tracing_env_var();

runtime::event::Duration::Duration(const string& name, const string& category, const string& args)
{
    if (Manager::is_tracing_enabled())
    {
        m_start = Manager::get_current_microseconds();
        m_stop = 0;
        m_name = name;
        m_category = category;
        m_args = args;
    }
}

void runtime::event::Duration::stop()
{
    if (Manager::is_tracing_enabled())
    {
        m_stop = Manager::get_current_microseconds();
    }
}

void runtime::event::Duration::write()
{
    if (Manager::is_tracing_enabled())
    {
        size_t stop_time = (m_stop != 0 ? m_stop : Manager::get_current_microseconds());

        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        string str;
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            str += ",\n";
        }

        str +=
            R"({"name":")" + m_name + R"(","cat":")" + m_category + R"(","ph":"X","pid":)" +
            Manager::get_process_id() + R"(,"tid":)" + Manager::get_thread_id() +
            R"(,"ts":)" + to_string(m_start) + R"(,"dur":)" + to_string(stop_time - m_start);
        if (!m_args.empty())
        {
            str += R"(,"args":)" + m_args;
        }
        str += "}";
        out << str;
    }
}

runtime::event::Object::Object(const string& name, const string& args)
    : m_name{name}
    , m_id{static_cast<size_t>(chrono::high_resolution_clock::now().time_since_epoch().count())}
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        string str;
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            str += ",\n";
        }
        str += R"({"name":")" + m_name + R"(","ph":"N","id":")" + to_string(m_id) +
               R"(","ts":)" + to_string(Manager::get_current_microseconds()) +
               R"(,"pid":)" + Manager::get_process_id() + R"(,"tid":)" + Manager::get_thread_id();
        if (!args.empty())
        {
            str += R"(,"args":)" + args;
        }
        str += "}";

        write_snapshot(out, args);
    }
}

void runtime::event::Object::snapshot(const string& args)
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }
        write_snapshot(out, args);
    }
}

void runtime::event::Object::write_snapshot(ostream& out, const string& args)
{
    string str = R"({"name":")" + m_name + R"(","ph":"O","id":")" + to_string(m_id) +
                 R"(","ts":)" + to_string(Manager::get_current_microseconds()) +
                 R"(,"pid":)" + Manager::get_process_id() + R"(,"tid":)" + Manager::get_thread_id();
    if (!args.empty())
    {
        str += R"(,"args":)" + args;
    }
    str += "}";
    out << str;
}

void runtime::event::Object::destroy()
{
    if (Manager::is_tracing_enabled())
    {
        lock_guard<mutex> lock(Manager::get_mutex());

        ofstream& out = runtime::event::Manager::get_output_stream();
        if (out.is_open() == false)
        {
            runtime::event::Manager::open();
        }
        else
        {
            Manager::get_output_stream() << ",\n";
        }
        string str = R"({"name":")" + m_name + R"(","ph":"D","id":")" + to_string(m_id) +
                     R"(","ts":)" + to_string(Manager::get_current_microseconds()) +
                     R"(,"pid":)" + Manager::get_process_id() + R"(,"tid":)" +
                     Manager::get_thread_id() + "}";
    }
}

void runtime::event::Manager::open(const string& path)
{
    ofstream& out = get_output_stream();
    if (out.is_open() == false)
    {
        out.open(path, ios_base::trunc);
        out << "[\n";
    }
}

void runtime::event::Manager::close()
{
    ofstream& out = get_output_stream();
    if (out.is_open())
    {
        out << "\n]\n";
        out.close();
{'fraction_non_alphanumeric': 0.20657823982792614, 'fraction_numerical': 0.002867897472665352, 'mean_word_length': 2.7147137150466047, 'pattern_counts': {'":': 56, '<': 149, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4304040', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3451, 'n_words': 663}