2021-06-01 05:21:09 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef TEST_TRACEEVENT_EXE
|
|
|
|
#include <iostream>
|
|
|
|
#else
|
|
|
|
#include <Log.hpp>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The base class for objects generating Trace Events when enabled.
|
|
|
|
//
|
2022-08-22 15:00:05 -05:00
|
|
|
// It depends on the embedding process what is done to the Trace Events generated. In the WSD
|
2021-06-01 05:21:09 -05:00
|
|
|
// process they are written to the Trace Event log file as generated (as buffered by the C++
|
|
|
|
// library). In the Kit process they are buffered and then sent to the WSD process for writing to
|
|
|
|
// the same log file. In the TraceEvent test program they are written out to stdout.
|
|
|
|
|
|
|
|
class TraceEvent
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static void emitInstantEvent(const std::string& name, const std::string& args);
|
|
|
|
|
2024-03-10 08:04:16 -05:00
|
|
|
const std::string _args;
|
2024-03-10 08:16:10 -05:00
|
|
|
int _pid; //< -1 when not recording, or done recording.
|
2024-03-10 08:04:16 -05:00
|
|
|
|
2021-06-01 05:21:09 -05:00
|
|
|
protected:
|
|
|
|
static std::atomic<bool> recordingOn; // True during recoding/emission
|
|
|
|
thread_local static int threadLocalNesting; // For use only by the ProfileZone derived class
|
|
|
|
|
2024-03-10 08:16:10 -05:00
|
|
|
/// Reset the pid when done recording.
|
|
|
|
void reset() { _pid = -1; }
|
|
|
|
|
2024-03-10 08:04:16 -05:00
|
|
|
int pid() const { return _pid; }
|
2024-03-10 08:16:10 -05:00
|
|
|
|
2024-03-10 08:04:16 -05:00
|
|
|
const std::string& args() const { return _args; }
|
2024-03-10 07:47:44 -05:00
|
|
|
|
2021-06-02 01:15:54 -05:00
|
|
|
static long getThreadId()
|
|
|
|
{
|
|
|
|
#ifdef TEST_TRACEEVENT_EXE
|
|
|
|
static thread_local int threadId = 0;
|
|
|
|
static std::atomic<int> threadCounter(1);
|
|
|
|
|
|
|
|
if (!threadId)
|
|
|
|
threadId = threadCounter++;
|
|
|
|
return threadId;
|
|
|
|
#else
|
|
|
|
return Util::getThreadId();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-01 05:21:09 -05:00
|
|
|
static std::string createArgsString(const std::map<std::string, std::string>& args)
|
|
|
|
{
|
|
|
|
if (!recordingOn)
|
2021-06-02 01:15:54 -05:00
|
|
|
return "0";
|
2021-06-01 05:21:09 -05:00
|
|
|
|
2024-03-11 07:16:05 -05:00
|
|
|
std::string result;
|
|
|
|
result.reserve(args.size() * 64);
|
|
|
|
|
|
|
|
result += '{';
|
2021-06-01 05:21:09 -05:00
|
|
|
bool first = true;
|
2023-06-09 16:15:07 -05:00
|
|
|
for (const auto& i : args)
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
result += ',';
|
|
|
|
result += '"';
|
|
|
|
result += i.first;
|
|
|
|
result += "\":\"";
|
|
|
|
result += i.second;
|
|
|
|
result += '"';
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
result += '}';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:47:44 -05:00
|
|
|
explicit TraceEvent(std::string args)
|
|
|
|
: _args(std::move(args))
|
|
|
|
, _pid(recordingOn ? getpid() : -1)
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:47:44 -05:00
|
|
|
virtual ~TraceEvent() = default;
|
|
|
|
|
2021-06-01 05:21:09 -05:00
|
|
|
public:
|
|
|
|
static void startRecording();
|
|
|
|
static void stopRecording();
|
2021-06-18 06:53:51 -05:00
|
|
|
static bool isRecordingOn()
|
|
|
|
{
|
|
|
|
return recordingOn;
|
|
|
|
}
|
2021-06-01 05:21:09 -05:00
|
|
|
|
|
|
|
static void emitInstantEvent(const std::string& name)
|
|
|
|
{
|
|
|
|
emitInstantEvent(name, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emitInstantEvent(const std::string& name, const std::map<std::string, std::string>& args)
|
|
|
|
{
|
|
|
|
emitInstantEvent(name, createArgsString(args));
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:53:51 -05:00
|
|
|
// These methods need to be implemented separately in the WSD and Kit processes. (WSD writes the
|
2021-06-17 05:31:27 -05:00
|
|
|
// actual Trace Event log file, Kit just forwards the Trace Events to WSD for output.)
|
2021-06-18 06:53:51 -05:00
|
|
|
|
|
|
|
// This should do its thing if Trace Event generation is enabled, even if not turned on. Used
|
|
|
|
// for metadata that will be needed by a Trace Event viewer if Trace Event generation is turned
|
|
|
|
// on later during the process life-time.
|
|
|
|
static void emitOneRecordingIfEnabled(const std::string &recording);
|
|
|
|
|
|
|
|
// Unless Trace Event generation is enabled and turned on, this should do nothing.
|
2021-06-17 05:31:27 -05:00
|
|
|
static void emitOneRecording(const std::string &recording);
|
|
|
|
|
2021-06-01 05:21:09 -05:00
|
|
|
TraceEvent(const TraceEvent&) = delete;
|
|
|
|
void operator=(const TraceEvent&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamedEvent : public TraceEvent
|
|
|
|
{
|
|
|
|
const std::string _name;
|
|
|
|
|
2024-03-10 08:04:16 -05:00
|
|
|
protected:
|
2024-03-10 07:47:44 -05:00
|
|
|
explicit NamedEvent(std::string name)
|
|
|
|
: NamedEvent(std::move(name), std::string())
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:47:44 -05:00
|
|
|
NamedEvent(std::string name, std::string args)
|
|
|
|
: TraceEvent(std::move(args))
|
|
|
|
, _name(std::move(name))
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:47:44 -05:00
|
|
|
NamedEvent(std::string name, const std::map<std::string, std::string>& args)
|
|
|
|
: NamedEvent(std::move(name), createArgsString(args))
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
}
|
2024-03-10 08:04:16 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
const std::string& name() const { return _name; }
|
2021-06-01 05:21:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProfileZone : public NamedEvent
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> _createTime;
|
|
|
|
int _nesting;
|
|
|
|
|
|
|
|
void emitRecording();
|
|
|
|
|
2024-03-10 07:47:44 -05:00
|
|
|
ProfileZone(std::string name, std::string args)
|
|
|
|
: NamedEvent(std::move(name), std::move(args))
|
2021-06-01 05:21:09 -05:00
|
|
|
, _nesting(-1)
|
|
|
|
{
|
|
|
|
if (recordingOn)
|
|
|
|
{
|
|
|
|
// Use system_clock as that matches the clock_gettime(CLOCK_REALTIME) that core uses.
|
|
|
|
_createTime = std::chrono::system_clock::now();
|
|
|
|
|
|
|
|
_nesting = threadLocalNesting++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 08:16:10 -05:00
|
|
|
void emitIfRecording()
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
2024-03-10 08:04:16 -05:00
|
|
|
if (pid() > 0)
|
2021-06-01 05:21:09 -05:00
|
|
|
{
|
|
|
|
threadLocalNesting--;
|
|
|
|
|
|
|
|
if (_nesting != threadLocalNesting)
|
|
|
|
{
|
|
|
|
#ifdef TEST_TRACEEVENT_EXE
|
2024-03-10 08:04:16 -05:00
|
|
|
std::cerr << "Incorrect ProfileZone nesting for " << name() << "\n";
|
2021-06-01 05:21:09 -05:00
|
|
|
#else
|
2024-03-10 08:04:16 -05:00
|
|
|
LOG_WRN("Incorrect ProfileZone nesting for " << name());
|
2021-06-01 05:21:09 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emitRecording();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 08:16:10 -05:00
|
|
|
public:
|
|
|
|
ProfileZone(std::string name, const std::map<std::string, std::string>& arguments)
|
|
|
|
: ProfileZone(std::move(name), createArgsString(arguments))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-12 03:43:42 -05:00
|
|
|
explicit ProfileZone(std::string name)
|
|
|
|
: ProfileZone(std::move(name), std::string())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-10 08:16:10 -05:00
|
|
|
explicit ProfileZone(const char* id)
|
|
|
|
: ProfileZone(id, std::string())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProfileZone() { emitIfRecording(); }
|
|
|
|
|
2021-06-01 05:21:09 -05:00
|
|
|
ProfileZone(const ProfileZone&) = delete;
|
|
|
|
void operator=(const ProfileZone&) = delete;
|
2024-03-10 08:16:10 -05:00
|
|
|
|
|
|
|
void end()
|
|
|
|
{
|
|
|
|
emitIfRecording();
|
|
|
|
reset(); // So we don't re-emit on destruction.
|
|
|
|
}
|
2021-06-01 05:21:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|