Skip to content

Commit 06951a1

Browse files
committed
profiler prototype
1 parent 09ad4bb commit 06951a1

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

include/plg/source_location.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace plg {
3434
const std::string_view function_name = __builtin_FUNCTION(),
3535
const std::string_view module_name = PLUGIFY_MODULE_NAME
3636
) noexcept
37-
#elif defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8))
37+
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 and __GNUC_MINOR__ >= 8))
3838
inline static constexpr source_location current(
3939
const std::size_t line = __builtin_LINE(),
4040
const std::size_t column = 0,

include/plugify/plugify.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "plugify/config.hpp"
44
#include "plugify/dependency_resolver.hpp"
55
#include "plugify/event_bus.hpp"
6+
#include "plugify/profiler.hpp"
67
#include "plugify/file_system.hpp"
78
#include "plugify/global.h"
89
#include "plugify/lifecycle.hpp"
@@ -45,6 +46,7 @@ namespace plugify {
4546

4647
// Service registration...
4748
PlugifyBuilder& WithLogger(std::shared_ptr<ILogger> logger);
49+
PlugifyBuilder& WithProfiler(std::shared_ptr<IProfiler> profiler);
4850
PlugifyBuilder& WithFileSystem(std::shared_ptr<IFileSystem> fs);
4951
PlugifyBuilder& WithAssemblyLoader(std::shared_ptr<IAssemblyLoader> loader);
5052
// PlugifyBuilder& WithConfigProvider(std::shared_ptr<IConfigProvider> provider);

include/plugify/profiler.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
5+
namespace plugify {
6+
struct ZoneHandle {
7+
uint64_t opaque = 0;
8+
9+
explicit operator bool() const { return opaque != 0; }
10+
};
11+
12+
struct ZoneDesc {
13+
std::string_view name;
14+
std::string_view function;
15+
std::string_view file;
16+
size_t line;
17+
uint32_t color;
18+
19+
ZoneDesc(std::string_view name,
20+
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) || (defined(_MSC_VER) && _MSC_VER > 1925)
21+
std::string_view function = __builtin_FUNCTION(),
22+
std::string_view file = __builtin_FILE(),
23+
size_t line = __builtin_LINE(),
24+
uint32_t color = 0
25+
#else
26+
std::string_view function = "",
27+
std::string_view file = "",
28+
size_t line = 0,
29+
uint32_t color = 0
30+
#endif
31+
)
32+
: name(name), function(function), file(file), line(line), color(color) {}
33+
};
34+
35+
class IProfiler {
36+
public:
37+
virtual ~IProfiler() = default;
38+
39+
// Frame boundary
40+
// Call once per application frame (or per plugin tick).
41+
virtual void MarkFrame(std::string_view name) = 0;
42+
43+
// CPU zones
44+
// BeginZone / EndZone must be balanced. Prefer the RAII
45+
// ScopedZone helper below — don't call these directly.
46+
virtual ZoneHandle BeginZone(const ZoneDesc& desc) = 0;
47+
virtual void EndZone(ZoneHandle handle) = 0;
48+
49+
// Memory tracking
50+
//virtual void TrackAlloc(void* ptr, size_t size, std::string_view pool = {}) = 0;
51+
//virtual void TrackFree(void* ptr, std::string_view pool = {}) = 0;
52+
53+
// Thread metadata
54+
virtual void SetThreadName(std::string_view name) = 0;
55+
56+
// Capability query
57+
virtual std::string_view GetName() const = 0; // "Tracy", "Optick", …
58+
virtual bool IsActive() const = 0;
59+
};
60+
61+
class ScopedZone {
62+
public:
63+
ScopedZone(IProfiler* profiler, const ZoneDesc& desc) : _profiler(profiler) {
64+
if (_profiler) _handle = _profiler->BeginZone(desc);
65+
}
66+
~ScopedZone() {
67+
if (_profiler && _handle) _profiler->EndZone(_handle);
68+
}
69+
70+
ScopedZone(const ScopedZone&) = delete;
71+
ScopedZone& operator=(const ScopedZone&) = delete;
72+
ScopedZone(ScopedZone&&) = delete;
73+
ScopedZone& operator=(ScopedZone&&) = delete;
74+
75+
private:
76+
IProfiler* _profiler = nullptr;
77+
ZoneHandle _handle = {};
78+
};
79+
}

src/core/plugify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,10 @@ PlugifyBuilder& PlugifyBuilder::WithLogger(std::shared_ptr<ILogger> logger) {
544544
_impl->services.RegisterInstance<ILogger>(std::move(logger));
545545
return *this;
546546
}
547+
PlugifyBuilder& PlugifyBuilder::WithProfiler(std::shared_ptr<IProfiler> profiler) {
548+
_impl->services.RegisterInstance<IProfiler>(std::move(profiler));
549+
return *this;
550+
}
547551

548552
PlugifyBuilder& PlugifyBuilder::WithFileSystem(std::shared_ptr<IFileSystem> fs) {
549553
_impl->services.RegisterInstance<IFileSystem>(std::move(fs));
@@ -591,6 +595,7 @@ PlugifyBuilder& PlugifyBuilder::WithEventBus(std::shared_ptr<IEventBus> bus) {
591595

592596
PlugifyBuilder& PlugifyBuilder::WithDefaults() {
593597
_impl->services.RegisterInstanceIfMissing<ILogger>(std::make_shared<ConsoleLogger>());
598+
//_impl->services.RegisterInstanceIfMissing<IProfiler>(std::make_shared<TracyProfiler>());
594599
_impl->services.RegisterInstanceIfMissing<IPlatformOps>(CreatePlatformOps());
595600
_impl->services.RegisterInstanceIfMissing<IEventBus>(std::make_shared<SimpleEventBus>());
596601
_impl->services.RegisterInstanceIfMissing<IFileSystem>(std::make_shared<ExtendedFileSystem>());

0 commit comments

Comments
 (0)