-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed under LGPLv3 - see LICENSE file for details. | ||
|
||
#include "fzx/trace.hpp" | ||
|
||
namespace fzx { | ||
|
||
static struct timespec tsSub(struct timespec a, struct timespec b) | ||
{ | ||
struct timespec r; // NOLINT(cppcoreguidelines-pro-type-member-init) | ||
r.tv_sec = a.tv_sec - b.tv_sec; | ||
r.tv_nsec = a.tv_nsec - b.tv_nsec; | ||
if (r.tv_nsec < 0) { | ||
--r.tv_sec; | ||
r.tv_nsec += 1000000000L; | ||
} | ||
return r; | ||
} | ||
|
||
TraceTLS* Trace::createTracer(char name) | ||
{ | ||
auto p = std::make_unique<TraceTLS>(); | ||
p->mName = name; | ||
return mTracers.emplace_back(std::move(p)).get(); | ||
} | ||
|
||
void Trace::start() | ||
{ | ||
clock_gettime(CLOCK_MONOTONIC, &mTimestamp); | ||
} | ||
|
||
void Trace::dump(FILE* file) const | ||
{ | ||
// auto positions = std::make_unique<size_t[]>(mTracers.size()); | ||
// for (size_t i = 0; i < mTracers.size(); ++i) | ||
// positions[i] = 0; | ||
|
||
|
||
for (const auto& tracer : mTracers) { | ||
uint64_t prev = 0; | ||
for (const auto& event : tracer->mEvents) { | ||
auto ts = tsSub(event.mTimestamp, mTimestamp); | ||
uint64_t time = static_cast<uint64_t>(ts.tv_nsec) | ||
+ static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL; | ||
uint64_t diff = time - prev; | ||
uint64_t tms = time / 1000000ULL; | ||
uint64_t tus = time % 1000000ULL; | ||
uint64_t dus = diff % 1000000ULL; | ||
uint64_t dms = diff / 1000000ULL; | ||
fprintf(file, "%6zu.%06zu %c %6zu.%06zu %s\n", | ||
tms, tus, tracer->mName, dms, dus, event.mMessage); | ||
prev = time; | ||
} | ||
} | ||
fflush(file); | ||
} | ||
|
||
} // namespace fzx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed under LGPLv3 - see LICENSE file for details. | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
#include <cstdio> | ||
#include <ctime> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
#include <memory> | ||
|
||
#include "fzx/macros.hpp" | ||
|
||
#define FZX_TRACE | ||
|
||
namespace fzx { | ||
|
||
struct TraceEvent | ||
{ | ||
struct timespec mTimestamp; | ||
const char* mMessage; | ||
const char* mSource; | ||
}; | ||
|
||
struct TraceTLS | ||
{ | ||
TraceTLS() noexcept = default; | ||
|
||
char mName { 0 }; | ||
std::vector<TraceEvent> mEvents; | ||
|
||
void trace(const char* msg, const char* file) | ||
{ | ||
TraceEvent ev; // NOLINT(cppcoreguidelines-pro-type-member-init) | ||
clock_gettime(CLOCK_MONOTONIC, &ev.mTimestamp); | ||
ev.mMessage = msg; | ||
ev.mSource = file; | ||
mEvents.push_back(ev); | ||
} | ||
}; | ||
|
||
struct Trace | ||
{ | ||
Trace() noexcept = default; | ||
TraceTLS* createTracer(char name); | ||
void start(); | ||
void dump(FILE* file) const; | ||
|
||
std::vector<std::unique_ptr<TraceTLS>> mTracers; | ||
struct timespec mTimestamp { 0, 0 }; | ||
}; | ||
|
||
#ifdef FZX_TRACE | ||
# define TRACE(ptr, msg) (ptr)->trace(msg, __FILE__ ":" STR(__LINE__)) | ||
#else | ||
# define TRACE(ptr, msg) | ||
#endif | ||
|
||
} // namespace fzx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters