2 #include "TRACE/tracemf.h"
3 #define TRACE_NAME (app_name_ + "_procfile_metric").c_str()
5 #include "artdaq-utilities/Plugins/MetricMacros.hh"
6 #include "fhiclcpp/ParameterSet.h"
10 #include <boost/thread.hpp>
27 std::unordered_map<std::string, std::string> value_map_;
29 boost::thread thread_;
49 explicit ProcFileMetric(fhicl::ParameterSet
const& config, std::string
const& app_name, std::string
const& metric_name)
51 , pipe_(
pset.get<std::string>(
"pipe",
"/tmp/eventQueueStat"))
54 METLOG(TLVL_DEBUG + 33) <<
"ProcFileMetric ctor";
55 auto names =
pset.get<std::vector<std::string>>(
"names", std::vector<std::string>());
57 for (
const auto& name : names)
59 value_map_[name] =
"";
62 int sts = mkfifo(pipe_.c_str(), 0777);
63 if (sts != 0) { perror(
"ProcFileMetric mkfifo"); }
64 METLOG(TLVL_DEBUG + 35) <<
"ProcFileMetric mkfifo()" << pipe_ <<
" sts=" << sts;
73 METLOG(TLVL_DEBUG + 36) <<
"~ProcFileMetric";
81 std::string
getLibName()
const override {
return "procFile"; }
88 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& ,
const std::chrono::system_clock::time_point& )
override
90 if (value_map_.count(name) != 0u)
92 METLOG(TLVL_DEBUG + 37) <<
"sendMetric_ setting value=" << value;
93 value_map_[name] = value;
104 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit,
const std::chrono::system_clock::time_point& time)
override
106 sendMetric_(name, std::to_string(value), unit, time);
116 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit,
const std::chrono::system_clock::time_point& time)
override
118 sendMetric_(name, std::to_string(value), unit, time);
128 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit,
const std::chrono::system_clock::time_point& time)
override
130 sendMetric_(name, std::to_string(value), unit, time);
140 void sendMetric_(
const std::string& name,
const uint64_t& value,
const std::string& unit,
const std::chrono::system_clock::time_point& time)
override
142 sendMetric_(name, std::to_string(value), unit, time);
154 boost::thread::attributes attrs;
155 attrs.set_stack_size(4096 * 2000);
160 catch (boost::exception
const& e)
162 TLOG(TLVL_ERROR) <<
"Creating ProcFile Metric thread failed! e: " << boost::diagnostic_information(e);
163 std::cerr <<
"Creating ProcFile Metric thread failed! e: " << boost::diagnostic_information(e) << std::endl;
178 METLOG(TLVL_DEBUG + 36) <<
"stopMetrics_ before open " << pipe_;
179 int fd = open(pipe_.c_str(), O_RDONLY | O_NONBLOCK);
182 perror(
"stopMetrics_ open(\"r\")");
185 METLOG(TLVL_DEBUG + 35) <<
"stopMetrics_ between open and unlink" << pipe_ <<
" fd=" << fd;
186 unlink(pipe_.c_str());
187 METLOG(TLVL_DEBUG + 36) <<
"stopMetrics_ unlinked " << pipe_;
190 read(fd, buf,
sizeof(buf));
194 METLOG(TLVL_DEBUG + 36) <<
"stopMetrics_ after close " << pipe_;
195 if (thread_.joinable())
209 METLOG(TLVL_DEBUG + 36) <<
"writePipe before open";
210 int fd = open(pipe_.c_str(), O_WRONLY);
212 for (
const auto& value : value_map_)
214 METLOG(TLVL_DEBUG + 35) <<
"writePipe open fd=" << fd <<
" name=" << value.first <<
" value=" << value.second;
215 str += value.first +
": " + value.second +
"\n";
218 int sts = write(fd, str.c_str(), str.size());
219 METLOG(TLVL_DEBUG + 36) <<
"writePipe write complete sts=" << sts;
221 METLOG(TLVL_DEBUG + 36) <<
"writePipe after close -- about to usleep";
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
void sendMetric_(const std::string &name, const std::string &value, const std::string &, const std::chrono::system_clock::time_point &) override
Set the value to be written to the pipe when it is opened by a reader.
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
std::string getLibName() const override
Get the "library name" of this Metric.
void stopMetrics_() override
Open the pipe for reading to allow the metric-sending thread to end gracefully.
fhicl::ParameterSet pset
The ParameterSet used to configure the MetricPlugin.
void startMetrics_() override
Start the metric-sending thread.
void writePipe()
Wait for the pipe to be opened and then write the current value to it.
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
void sendMetric_(const std::string &name, const int &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Set the value to be written to the pipe when it is opened by a reader.
~ProcFileMetric() override
ProcFileMetric Destructor.
ProcFileMetric(fhicl::ParameterSet const &config, std::string const &app_name, std::string const &metric_name)
ProcFileMetric Constructor.
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Set the value to be written to the pipe when it is opened by a reader.
void sendMetric_(const std::string &name, const float &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Set the value to be written to the pipe when it is opened by a reader.
void sendMetric_(const std::string &name, const double &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Set the value to be written to the pipe when it is opened by a reader.
A MetricPlugin which writes a long unsigned int metric with a given name to a given pipe...