2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include "Metrics.h"
|
|
|
|
|
|
|
|
namespace Metrics {
|
2023-03-22 16:16:17 +00:00
|
|
|
std::atomic<Metrics*> Metrics::singleton = nullptr;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
const char* to_string(ArgTiming t) {
|
2022-12-19 14:37:51 +00:00
|
|
|
switch (t) {
|
|
|
|
case ENTRY:
|
|
|
|
return "entry";
|
|
|
|
case EXIT:
|
|
|
|
return "exit";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
Metrics::Metrics(ObjectIntrospection::options opts, const std::string& savePath)
|
2022-12-19 14:37:51 +00:00
|
|
|
: opts(opts) {
|
|
|
|
writer = std::fstream(savePath, std::ios_base::out);
|
|
|
|
writer << "{ \"metrics\": [" << std::endl;
|
|
|
|
|
|
|
|
// set the singleton to this once fully constructed
|
|
|
|
Metrics::singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Metrics::~Metrics() {
|
|
|
|
writer << "]}" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Metrics::save(std::string object) {
|
2023-03-22 16:16:17 +00:00
|
|
|
Metrics* m = singleton.load();
|
2022-12-19 14:37:51 +00:00
|
|
|
std::lock_guard<std::mutex> guard(m->writerLock);
|
|
|
|
|
|
|
|
if (m->hasWritten) {
|
|
|
|
m->writer << ',' << object << std::endl;
|
|
|
|
} else {
|
|
|
|
m->hasWritten = true;
|
|
|
|
m->writer << object << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 18:02:37 +01:00
|
|
|
void Metrics::saveArg(const char* name,
|
|
|
|
const char* argName,
|
|
|
|
ArgTiming timing,
|
2022-12-19 14:37:51 +00:00
|
|
|
size_t size) {
|
|
|
|
std::string out = "{\"type\": \"size\", \"traceName\": \"";
|
|
|
|
out += name;
|
|
|
|
out += "\", \"argName\": \"";
|
|
|
|
out += argName;
|
|
|
|
out += "\", \"timing\": \"";
|
|
|
|
out += to_string(timing);
|
|
|
|
out += "\", \"size\": ";
|
|
|
|
out += std::to_string(size);
|
|
|
|
out += '}';
|
|
|
|
|
|
|
|
save(out);
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
void Metrics::saveDuration(const char* name,
|
2022-12-19 14:37:51 +00:00
|
|
|
std::chrono::milliseconds duration) {
|
|
|
|
std::string out = "{\"type\": \"duration\", \"traceName\": \"";
|
|
|
|
out += name;
|
|
|
|
out += "\", \"duration\": ";
|
|
|
|
out += std::to_string(duration.count());
|
|
|
|
out += '}';
|
|
|
|
|
|
|
|
save(out);
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
Tracing::Tracing(const char* name, bool enabled)
|
2022-12-19 14:37:51 +00:00
|
|
|
: name(name), enabled(enabled) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Tracing::~Tracing() {
|
|
|
|
if (isTimingEnabled()) {
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::high_resolution_clock::now() - startTime);
|
|
|
|
saveDuration(duration);
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
for (auto const& exitFunc : exitFuncs)
|
2022-12-19 14:37:51 +00:00
|
|
|
exitFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tracing::isTimingEnabled() {
|
|
|
|
return enabled || Metrics::isEnabled();
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
bool Tracing::isArgEnabled(const char* argName, ArgTiming timing) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return enabled || Metrics::isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tracing::start() {
|
|
|
|
if (isTimingEnabled()) {
|
|
|
|
startTime = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
void Tracing::saveArg(const char* argName, ArgTiming timing, size_t size) {
|
2022-12-19 14:37:51 +00:00
|
|
|
Metrics::saveArg(name, argName, timing, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tracing::saveDuration(std::chrono::milliseconds duration) {
|
|
|
|
Metrics::saveDuration(name, duration);
|
|
|
|
}
|
|
|
|
} // namespace Metrics
|