2 #include "artdaq-utilities/Plugins/MetricMacros.hh"
3 #include "fhiclcpp/ParameterSet.h"
4 #include "messagefacility/MessageLogger/MessageLogger.h"
5 #define TRACE_NAME "procFile_metric"
10 #include <boost/thread.hpp>
27 std::unordered_map<std::string, std::string> value_map_;
29 boost::thread thread_;
48 explicit ProcFileMetric(fhicl::ParameterSet
const& config, std::string
const& app_name)
50 , pipe_(
pset.get<std::string>(
"pipe",
"/tmp/eventQueueStat"))
53 auto names =
pset.get<std::vector<std::string>>(
"names", std::vector<std::string>());
55 for (
const auto& name : names)
57 value_map_[name] =
"";
60 int sts = mkfifo(pipe_.c_str(), 0777);
61 if (sts != 0) { perror(
"ProcFileMetric mkfifo"); }
62 TLOG(10) <<
"ProcFileMetric mkfifo()" << pipe_ <<
" sts=" << sts;
71 TLOG(11) <<
"~ProcFileMetric";
79 std::string
getLibName()
const override {
return "procFile"; }
86 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& )
override
88 if (value_map_.count(name) != 0u)
90 TLOG(12) <<
"sendMetric_ setting value=" << value;
91 value_map_[name] = value;
101 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
112 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
123 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
134 void sendMetric_(
const std::string& name,
const uint64_t& value,
const std::string& unit)
override
148 boost::thread::attributes attrs;
149 attrs.set_stack_size(4096 * 2000);
154 catch (boost::exception
const& e)
156 std::cerr <<
"Creating ProcFile Metric thread failed! e: " << boost::diagnostic_information(e) << std::endl;
171 TLOG(11) <<
"stopMetrics_ before open " << pipe_;
172 int fd = open(pipe_.c_str(), O_RDONLY | O_NONBLOCK);
175 perror(
"stopMetrics_ open(\"r\")");
178 TLOG(10) <<
"stopMetrics_ between open and unlink" << pipe_ <<
" fd=" << fd;
179 unlink(pipe_.c_str());
180 TLOG(11) <<
"stopMetrics_ unlinked " << pipe_;
183 read(fd, buf,
sizeof(buf));
187 TLOG(11) <<
"stopMetrics_ after close " << pipe_;
188 if (thread_.joinable())
202 TLOG(11) <<
"writePipe before open";
203 int fd = open(pipe_.c_str(), O_WRONLY);
205 for (
const auto& value : value_map_)
207 TLOG(10) <<
"writePipe open fd=" << fd <<
" name=" << value.first <<
" value=" << value.second;
208 str += value.first +
": " + value.second +
"\n";
211 int sts = write(fd, str.c_str(), str.size());
212 TLOG(11) <<
"writePipe write complete sts=" << sts;
214 TLOG(11) <<
"writePipe after close -- about to usleep";
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit) override
Set the value to be written to the pipe when it is opened by a reader.
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
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 sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Set the value to be written to the pipe when it is opened by a reader.
void startMetrics_() override
Start the metric-sending thread.
ProcFileMetric(fhicl::ParameterSet const &config, std::string const &app_name)
ProcFileMetric Constructor.
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.
~ProcFileMetric() override
ProcFileMetric Destructor.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Set the value to be written to the pipe when it is opened by a reader.
void sendMetric_(const std::string &name, const std::string &value, const std::string &) 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...
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Set the value to be written to the pipe when it is opened by a reader.