2021-11-05 02:34:35 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2022-05-26 23:08:16 +01:00
|
|
|
#include "Luau/Common.h"
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2023-07-07 21:10:48 +01:00
|
|
|
#include <memory>
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2024-09-27 19:58:21 +01:00
|
|
|
#include <string.h>
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
LUAU_FASTFLAG(DebugLuauTimeTracing)
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace TimeTrace
|
|
|
|
{
|
2022-04-07 22:29:01 +01:00
|
|
|
double getClock();
|
2021-11-05 02:34:35 +00:00
|
|
|
uint32_t getClockMicroseconds();
|
2022-04-07 22:29:01 +01:00
|
|
|
} // namespace TimeTrace
|
|
|
|
} // namespace Luau
|
|
|
|
|
|
|
|
#if defined(LUAU_ENABLE_TIME_TRACE)
|
2021-11-05 02:34:35 +00:00
|
|
|
|
2022-04-07 22:29:01 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace TimeTrace
|
|
|
|
{
|
2021-11-05 02:34:35 +00:00
|
|
|
struct Token
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
const char* category;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class EventType : uint8_t
|
|
|
|
{
|
|
|
|
Enter,
|
|
|
|
Leave,
|
|
|
|
|
|
|
|
ArgName,
|
|
|
|
ArgValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Event
|
|
|
|
{
|
|
|
|
EventType type;
|
|
|
|
uint16_t token;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t microsec; // 1 hour trace limit
|
|
|
|
uint32_t dataPos;
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GlobalContext;
|
|
|
|
struct ThreadContext;
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
std::shared_ptr<GlobalContext> getGlobalContext();
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
uint16_t createToken(GlobalContext& context, const char* name, const char* category);
|
|
|
|
uint32_t createThread(GlobalContext& context, ThreadContext* threadContext);
|
|
|
|
void releaseThread(GlobalContext& context, ThreadContext* threadContext);
|
|
|
|
void flushEvents(GlobalContext& context, uint32_t threadId, const std::vector<Event>& events, const std::vector<char>& data);
|
|
|
|
|
|
|
|
struct ThreadContext
|
|
|
|
{
|
|
|
|
ThreadContext()
|
|
|
|
: globalContext(getGlobalContext())
|
|
|
|
{
|
2023-07-07 21:10:48 +01:00
|
|
|
threadId = createThread(*globalContext, this);
|
2021-11-05 02:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ThreadContext()
|
|
|
|
{
|
|
|
|
if (!events.empty())
|
|
|
|
flushEvents();
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
releaseThread(*globalContext, this);
|
2021-11-05 02:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void flushEvents()
|
|
|
|
{
|
2023-07-07 21:10:48 +01:00
|
|
|
static uint16_t flushToken = createToken(*globalContext, "flushEvents", "TimeTrace");
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
events.push_back({EventType::Enter, flushToken, {getClockMicroseconds()}});
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
TimeTrace::flushEvents(*globalContext, threadId, events, data);
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
events.clear();
|
|
|
|
data.clear();
|
|
|
|
|
|
|
|
events.push_back({EventType::Leave, 0, {getClockMicroseconds()}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void eventEnter(uint16_t token)
|
|
|
|
{
|
|
|
|
eventEnter(token, getClockMicroseconds());
|
|
|
|
}
|
|
|
|
|
|
|
|
void eventEnter(uint16_t token, uint32_t microsec)
|
|
|
|
{
|
|
|
|
events.push_back({EventType::Enter, token, {microsec}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void eventLeave()
|
|
|
|
{
|
|
|
|
eventLeave(getClockMicroseconds());
|
|
|
|
}
|
|
|
|
|
|
|
|
void eventLeave(uint32_t microsec)
|
|
|
|
{
|
|
|
|
events.push_back({EventType::Leave, 0, {microsec}});
|
|
|
|
|
|
|
|
if (events.size() > kEventFlushLimit)
|
|
|
|
flushEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void eventArgument(const char* name, const char* value)
|
|
|
|
{
|
|
|
|
uint32_t pos = uint32_t(data.size());
|
|
|
|
data.insert(data.end(), name, name + strlen(name) + 1);
|
|
|
|
events.push_back({EventType::ArgName, 0, {pos}});
|
|
|
|
|
|
|
|
pos = uint32_t(data.size());
|
|
|
|
data.insert(data.end(), value, value + strlen(value) + 1);
|
|
|
|
events.push_back({EventType::ArgValue, 0, {pos}});
|
|
|
|
}
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
std::shared_ptr<GlobalContext> globalContext;
|
2021-11-05 02:34:35 +00:00
|
|
|
uint32_t threadId;
|
|
|
|
std::vector<Event> events;
|
|
|
|
std::vector<char> data;
|
|
|
|
|
|
|
|
static constexpr size_t kEventFlushLimit = 8192;
|
|
|
|
};
|
|
|
|
|
2024-06-29 01:34:49 +01:00
|
|
|
using ThreadContextProvider = ThreadContext& (*)();
|
|
|
|
|
|
|
|
inline ThreadContextProvider& threadContextProvider()
|
|
|
|
{
|
|
|
|
static ThreadContextProvider handler = nullptr;
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2021-11-05 02:34:35 +00:00
|
|
|
ThreadContext& getThreadContext();
|
|
|
|
|
|
|
|
struct Scope
|
|
|
|
{
|
2022-03-31 22:01:51 +01:00
|
|
|
explicit Scope(uint16_t token)
|
|
|
|
: context(getThreadContext())
|
2021-11-05 02:34:35 +00:00
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauTimeTracing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context.eventEnter(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Scope()
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauTimeTracing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context.eventLeave();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadContext& context;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OptionalTailScope
|
|
|
|
{
|
2022-03-31 22:01:51 +01:00
|
|
|
explicit OptionalTailScope(uint16_t token, uint32_t threshold)
|
|
|
|
: context(getThreadContext())
|
2021-11-05 02:34:35 +00:00
|
|
|
, token(token)
|
|
|
|
, threshold(threshold)
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauTimeTracing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pos = uint32_t(context.events.size());
|
|
|
|
microsec = getClockMicroseconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
~OptionalTailScope()
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauTimeTracing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pos == context.events.size())
|
|
|
|
{
|
|
|
|
uint32_t curr = getClockMicroseconds();
|
|
|
|
|
|
|
|
if (curr - microsec > threshold)
|
|
|
|
{
|
|
|
|
context.eventEnter(token, microsec);
|
|
|
|
context.eventLeave(curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadContext& context;
|
|
|
|
uint16_t token;
|
|
|
|
uint32_t threshold;
|
|
|
|
uint32_t microsec;
|
|
|
|
uint32_t pos;
|
|
|
|
};
|
|
|
|
|
2022-03-31 22:01:51 +01:00
|
|
|
LUAU_NOINLINE uint16_t createScopeData(const char* name, const char* category);
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
} // namespace TimeTrace
|
|
|
|
} // namespace Luau
|
|
|
|
|
|
|
|
// Regular scope
|
2021-11-05 15:47:21 +00:00
|
|
|
#define LUAU_TIMETRACE_SCOPE(name, category) \
|
2022-03-31 22:01:51 +01:00
|
|
|
static uint16_t lttScopeStatic = Luau::TimeTrace::createScopeData(name, category); \
|
|
|
|
Luau::TimeTrace::Scope lttScope(lttScopeStatic)
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
// A scope without nested scopes that may be skipped if the time it took is less than the threshold
|
2021-11-05 15:47:21 +00:00
|
|
|
#define LUAU_TIMETRACE_OPTIONAL_TAIL_SCOPE(name, category, microsec) \
|
2022-03-31 22:01:51 +01:00
|
|
|
static uint16_t lttScopeStaticOptTail = Luau::TimeTrace::createScopeData(name, category); \
|
|
|
|
Luau::TimeTrace::OptionalTailScope lttScope(lttScopeStaticOptTail, microsec)
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
// Extra key/value data can be added to regular scopes
|
2021-11-05 15:47:21 +00:00
|
|
|
#define LUAU_TIMETRACE_ARGUMENT(name, value) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (FFlag::DebugLuauTimeTracing) \
|
2022-03-31 22:01:51 +01:00
|
|
|
lttScope.context.eventArgument(name, value); \
|
2021-11-05 02:34:35 +00:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define LUAU_TIMETRACE_SCOPE(name, category)
|
|
|
|
#define LUAU_TIMETRACE_OPTIONAL_TAIL_SCOPE(name, category, microsec)
|
|
|
|
#define LUAU_TIMETRACE_ARGUMENT(name, value) \
|
2021-11-05 15:47:21 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2021-11-05 02:34:35 +00:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#endif
|