Program Listing for File tracing.hpp

Return to documentation for file (include/yaets/tracing.hpp)

// Copyright 2024 Intelligent Robotics Lab
//
// 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.

#ifndef YAETS__TRACING_HPP_
#define YAETS__TRACING_HPP_

#include <chrono>
#include <string>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <thread>
#include <atomic>
#include <unordered_map>
#include <memory>

namespace yaets
{

#if defined(__GNUC__) || defined(__clang__)
    #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __PRETTY_FUNCTION__)
#elif defined(_MSC_VER)
    #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCSIG__)
#else
    #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCTION__)
#endif


#define SHARED_TRACE_INIT(session, id) \
  yaets::TraceRegistry::getInstance().registerTrace(id, session)

#define SHARED_TRACE_START(id) \
  yaets::TraceRegistry::getInstance().startTrace(id)

#define SHARED_TRACE_END(id) \
  yaets::TraceRegistry::getInstance().endTrace(id)


struct TraceEvent
{
  std::string trace_name;
  std::chrono::nanoseconds start_time;
  std::chrono::nanoseconds end_time;
};

class TraceSession
{
public:
  explicit TraceSession(const std::string & filename);

  ~TraceSession();

  void stop();

  void register_trace(
    const std::string & trace_name,
    const std::chrono::nanoseconds & start_time,
    const std::chrono::nanoseconds & end_time);

private:
  std::queue<TraceEvent> trace_queue;
  std::mutex queue_mutex;
  std::condition_variable cv;
  std::thread consumer_thread;
  std::atomic<bool> running;
  std::string filename_;
  std::chrono::nanoseconds session_start_time_;

  void trace_consumer();
};

class TraceGuard
{
public:
  TraceGuard(TraceSession & session, const std::string & trace_name);

  ~TraceGuard();

  std::chrono::nanoseconds get_start_time() const
  {
    return start_time_;
  }

private:
  TraceSession & session_;
  std::string trace_name_;
  std::chrono::nanoseconds start_time_;

protected:
  std::string extract_trace_name(const std::string & function_signature);
};


class NamedSharedTrace
{
public:
  static const size_t TRACE_SIZE_INIT = 100;

  NamedSharedTrace(TraceSession & session, const std::string & trace_name);

  void start();

  void end();

private:
  TraceSession & session_;
  std::string trace_name_;
  std::vector<std::chrono::nanoseconds> start_times_;

  std::atomic<size_t> counter_push_;
  std::atomic<size_t> counter_pop_;
  size_t elements_;
  std::mutex trace_mutex_;
};


class TraceRegistry
{
public:
  static TraceRegistry & getInstance()
  {
    static TraceRegistry instance;
    return instance;
  }

  void registerTrace(const std::string & id, TraceSession & session);

  void startTrace(const std::string & id);

  void endTrace(const std::string & id);

private:
  std::unordered_map<std::string, std::unique_ptr<NamedSharedTrace>> traces_;
  std::mutex mutex_;

  // Private constructors for singleton pattern
  TraceRegistry() = default;
  ~TraceRegistry() = default;

  // Disable copy construction and assignment
  TraceRegistry(const TraceRegistry &) = delete;
  TraceRegistry & operator=(const TraceRegistry &) = delete;
};

}  // namespace yaets

#endif  // YAETS__TRACING_HPP_