Belle II Software  release-05-02-19
MetadataService.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/core/MetadataService.h>
12 #include <framework/core/ProcessStatistics.h>
13 #include <framework/core/Environment.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/dataobjects/FileMetaData.h>
16 #include <framework/gearbox/Unit.h>
17 #include <framework/utilities/FileSystem.h>
18 #include <framework/utilities/Utils.h>
19 #include <fstream>
20 
21 using namespace Belle2;
22 
24 {
25  m_json["basf2_apiversion"] = 1;
26 }
27 
29 {
30  static MetadataService instance;
31  return instance;
32 }
33 
34 void MetadataService::addRootOutputFile(const std::string& fileName, const FileMetaData* metaData)
35 {
36  if (m_fileName.empty()) return;
37  if (!FileSystem::isFile(fileName)) return;
38 
39  nlohmann::json file_json = {{"type", "RootOutput"}, {"filename", fileName}};
40 
41  if (metaData) {
42  file_json["metadata"] = nlohmann::json::parse(metaData->getJsonStr());
43  }
44 
45  try {
46  std::string check = Utils::getCommandOutput("b2file-check", {"--json", fileName});
47  file_json.merge_patch(nlohmann::json::parse(check));
48  } catch (...) {}
49 
50  file_json["checksums"]["md5"] = FileSystem::calculateMD5(fileName);
51  file_json["checksums"]["adler32"] = FileSystem::calculateAdler32(fileName);
52  // no sha256 yet
53 
54  m_json["output_files"].push_back(file_json);
55 
56  writeJson();
57 }
58 
59 void MetadataService::addRootNtupleFile(const std::string& fileName)
60 {
61  if (m_fileName.empty()) return;
62  if (!FileSystem::isFile(fileName)) return;
63 
64  nlohmann::json file_json = {{"type", "RootNtuple"}, {"filename", fileName}};
65 
66  // no metadata and no check
67 
68  file_json["checksums"]["md5"] = FileSystem::calculateMD5(fileName);
69  file_json["checksums"]["adler32"] = FileSystem::calculateAdler32(fileName);
70  // no sha256 yet
71 
72  m_json["output_files"].push_back(file_json);
73 
74  writeJson();
75 }
76 
77 void MetadataService::addBasf2Status(const std::string& message)
78 {
79  if (m_fileName.empty()) return;
80  auto& status = m_json["basf2_status"];
81  status["elapsed_walltime_sec"] = (Utils::getClock() - m_basf2StartTime) / Unit::s;
82  status["resident_memory_mb"] = Utils::getRssMemoryKB() / 1024;
84  if (processStatistics.isValid()) {
85  const auto& stats = processStatistics->getGlobal();
86  status["runs_processed"] = int(stats.getCalls(ModuleStatistics::EStatisticCounters::c_BeginRun));
87  status["events_processed"] = int(stats.getCalls());
88  }
89  if ((status.count("total_events") == 0) || (status["total_events"] == 0)) {
90  status["total_events"] = Environment::Instance().getNumberOfEvents();
91  }
95  status["finished"] = false;
96  status["message"] = message;
97 
98  writeJson();
99 }
100 
102 {
103  if (m_fileName.empty()) return;
104 
106 
107  if (processStatistics.isValid()) {
108 
109  std::vector<ModuleStatistics> modulesSortedByIndex(processStatistics->getAll());
110  sort(modulesSortedByIndex.begin(), modulesSortedByIndex.end(), [](const ModuleStatistics & a, const ModuleStatistics & b) { return a.getIndex() < b.getIndex(); });
111 
112  for (const ModuleStatistics& stats : modulesSortedByIndex) {
113  m_json["modules_calls"][stats.getName()] = int(stats.getCalls(ModuleStatistics::EStatisticCounters::c_Event));
114  }
115  }
116 
117  writeJson();
118 }
119 
121 {
122  m_json["basf2_status"]["finished"] = true;
123  m_json["basf2_status"]["success"] = success;
124 
125  writeJson();
126 }
127 
129 {
130  if (m_fileName.empty()) return;
131  std::ofstream jsonFile(m_fileName.c_str());
132  jsonFile << m_json.dump(2) << std::endl;
133 }
Belle2::Utils::getRssMemoryKB
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition: Utils.cc:76
Belle2::Unit::s
static const double s
[second]
Definition: Unit.h:105
Belle2::MetadataService::m_basf2StartTime
double m_basf2StartTime
the start time of basf2
Definition: MetadataService.h:84
Belle2::FileSystem::isFile
static bool isFile(const std::string &filename)
Check if filename points to an existing file.
Definition: FileSystem.cc:46
Belle2::LogConfig::c_Fatal
@ c_Fatal
Fatal: for situations were the program execution can not be continued.
Definition: LogConfig.h:41
Belle2::MetadataService::m_fileName
std::string m_fileName
The name of the json file.
Definition: MetadataService.h:82
Belle2::MetadataService::addModuleCallsCount
void addModuleCallsCount()
Add the metadata of number of calls of all modules.
Definition: MetadataService.cc:101
Belle2::MetadataService::addRootNtupleFile
void addRootNtupleFile(const std::string &fileName)
Add the metadata of a root ntuple file.
Definition: MetadataService.cc:59
Belle2::FileMetaData::getJsonStr
std::string getJsonStr() const
Get a json representation.
Definition: FileMetaData.cc:182
Belle2::MetadataService::addBasf2Status
void addBasf2Status(const std::string &message="")
Add metadata of basf2 status.
Definition: MetadataService.cc:77
Belle2::MetadataService::addRootOutputFile
void addRootOutputFile(const std::string &fileName, const FileMetaData *metaData=nullptr)
Add the metadata of a root output file.
Definition: MetadataService.cc:34
Belle2::Utils::getCommandOutput
std::string getCommandOutput(const std::string &command, const std::vector< std::string > &arguments={}, bool searchPath=true)
Execute a shell command and return its output.
Definition: Utils.cc:92
Belle2::FileMetaData
Metadata information about a file.
Definition: FileMetaData.h:39
Belle2::MetadataService::finishBasf2
void finishBasf2(bool success=true)
Add metadata for basf2 completion.
Definition: MetadataService.cc:120
Belle2::FileSystem::calculateMD5
static std::string calculateMD5(const std::string &filename)
Calculate the MD5 checksum of a given file.
Definition: FileSystem.cc:79
Belle2::FileSystem::calculateAdler32
static std::string calculateAdler32(const std::string &filename)
Calculate the Adler-32 checksum of a given file.
Definition: FileSystem.cc:87
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::LogConfig::c_Error
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition: LogConfig.h:40
Belle2::MetadataService::m_json
nlohmann::json m_json
The json object.
Definition: MetadataService.h:83
Belle2::Environment::getNumberOfEvents
unsigned int getNumberOfEvents() const
Return the number of events, from either input or EventInfoSetter, or -n command line override (if le...
Definition: Environment.cc:42
Belle2::LogSystem::Instance
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:33
Belle2::DataStore::c_Persistent
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:62
Belle2::Utils::getClock
double getClock()
Return current value of the real-time clock.
Definition: Utils.cc:58
Belle2::MetadataService::writeJson
void writeJson()
Serialize the current json content to the json file.
Definition: MetadataService.cc:128
Belle2::LogConfig::c_Warning
@ c_Warning
Warning: for potential problems that the user should pay attention to.
Definition: LogConfig.h:39
Belle2::MetadataService
This class provides a service for writing metadata about the basf2 execution and about output files t...
Definition: MetadataService.h:37
Belle2::Environment::Instance
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:31
Belle2::MetadataService::Instance
static MetadataService & Instance()
Static method to get a reference to the MetadataService instance.
Definition: MetadataService.cc:28
Belle2::MetadataService::MetadataService
MetadataService()
Constructor.
Definition: MetadataService.cc:23
Belle2::LogSystem::getMessageCounter
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
Definition: LogSystem.cc:165
Belle2::ModuleStatistics
Keep track of time and memory consumption during processing.
Definition: ModuleStatistics.h:36
Belle2::StoreObjPtr::isValid
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:120