|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#include "TPCDistortionMapFile.h" |
| 13 | +#include "Framework/InputRecord.h" |
| 14 | +#include "Framework/Logger.h" |
| 15 | +#include "Framework/ProcessingContext.h" |
| 16 | +#include "TPCCalibration/CorrMapParam.h" |
| 17 | +#include "TPCSimulation/Digitizer.h" |
| 18 | +#include "TPCSpaceCharge/SpaceCharge.h" |
| 19 | +#include <Compression.h> |
| 20 | +#include <TFile.h> |
| 21 | +#include <TNamed.h> |
| 22 | +#include <TStopwatch.h> |
| 23 | +#include <fcntl.h> |
| 24 | +#include <sys/file.h> |
| 25 | +#include <unistd.h> |
| 26 | +#include <filesystem> |
| 27 | +#include <memory> |
| 28 | +#include <fmt/format.h> |
| 29 | + |
| 30 | +using namespace o2::tpc; |
| 31 | +using SC = o2::tpc::SpaceCharge<float>; |
| 32 | + |
| 33 | +namespace |
| 34 | +{ |
| 35 | +constexpr const char* ObjectName = "ccdb_object"; |
| 36 | +constexpr const char* KeyName = "tpc_distortion_key"; |
| 37 | +constexpr int KeyFormat = 1; ///< version of the file content |
| 38 | + |
| 39 | +/// lock on a file, shared or exclusive, released at destruction |
| 40 | +class FileLock |
| 41 | +{ |
| 42 | + public: |
| 43 | + FileLock(const std::string& path, int operation) |
| 44 | + { |
| 45 | + mFD = ::open(path.c_str(), O_CREAT | O_RDWR, 0644); |
| 46 | + if (mFD >= 0 && ::flock(mFD, operation) != 0) { |
| 47 | + ::close(mFD); |
| 48 | + mFD = -1; |
| 49 | + } |
| 50 | + } |
| 51 | + ~FileLock() |
| 52 | + { |
| 53 | + if (mFD >= 0) { |
| 54 | + ::flock(mFD, LOCK_UN); |
| 55 | + ::close(mFD); |
| 56 | + } |
| 57 | + } |
| 58 | + bool ok() const { return mFD >= 0; } |
| 59 | + FileLock(const FileLock&) = delete; |
| 60 | + FileLock& operator=(const FileLock&) = delete; |
| 61 | + |
| 62 | + private: |
| 63 | + int mFD = -1; |
| 64 | +}; |
| 65 | + |
| 66 | +std::string getETag(o2::framework::ProcessingContext& pc, const char* binding) |
| 67 | +{ |
| 68 | + const auto& headers = pc.inputs().get<o2::framework::CCDBMetadataExtractor>(binding); |
| 69 | + const auto it = headers.find("ETag"); |
| 70 | + return it == headers.end() ? std::string{} : it->second; |
| 71 | +} |
| 72 | + |
| 73 | +std::unique_ptr<SC> readMap(const std::string& path, const std::string& key, bool warn) |
| 74 | +{ |
| 75 | + if (!std::filesystem::exists(path)) { |
| 76 | + return nullptr; |
| 77 | + } |
| 78 | + TFile file(path.c_str(), "READ"); |
| 79 | + std::unique_ptr<TNamed> storedKey(file.IsZombie() ? nullptr : file.Get<TNamed>(KeyName)); |
| 80 | + if (!storedKey || key != storedKey->GetTitle()) { |
| 81 | + if (warn) { |
| 82 | + LOGP(warning, "TPC: {} was written for other distortion inputs and will be replaced", path); |
| 83 | + } |
| 84 | + return nullptr; |
| 85 | + } |
| 86 | + return std::unique_ptr<SC>(file.Get<SC>(ObjectName)); |
| 87 | +} |
| 88 | + |
| 89 | +bool writeMap(const std::string& path, const std::string& key, const SC& sc) |
| 90 | +{ |
| 91 | + // written under another name and renamed, so that readers never see a partial file |
| 92 | + const auto tmpPath = fmt::format("{}.{}.tmp", path, ::getpid()); |
| 93 | + bool ok = false; |
| 94 | + { |
| 95 | + TFile file(tmpPath.c_str(), "RECREATE", "", ROOT::RCompressionSetting::EDefaults::kUseAnalysis); |
| 96 | + if (!file.IsZombie()) { |
| 97 | + TNamed named(KeyName, key.c_str()); |
| 98 | + ok = file.WriteObjectAny(&sc, "o2::tpc::SpaceCharge<float>", ObjectName) > 0 && file.WriteObject(&named, KeyName) > 0; |
| 99 | + file.Close(); |
| 100 | + ok = ok && !file.TestBit(TFile::kWriteError); |
| 101 | + } |
| 102 | + } |
| 103 | + std::error_code ec; |
| 104 | + if (ok) { |
| 105 | + std::filesystem::rename(tmpPath, path, ec); |
| 106 | + } else { |
| 107 | + std::filesystem::remove(tmpPath, ec); |
| 108 | + } |
| 109 | + return ok && !ec; |
| 110 | +} |
| 111 | +} // namespace |
| 112 | + |
| 113 | +void TPCDistortionMapFile::prepare(o2::framework::ProcessingContext& pc, Digitizer& digitizer, float meanLumi, float meanLumiDerivative, |
| 114 | + const std::function<void()>& compute) |
| 115 | +{ |
| 116 | + const auto etagDist = getETag(pc, "tpcdistortions"); |
| 117 | + const auto etagDeriv = getETag(pc, "tpcdistortionsderiv"); |
| 118 | + if (etagDist.empty() || etagDeriv.empty()) { |
| 119 | + LOGP(warning, "TPC: no ETag for the distortion maps, the scaled distortions are computed and not shared through {}", mPath); |
| 120 | + compute(); |
| 121 | + return; |
| 122 | + } |
| 123 | + const auto key = fmt::format("format {} dist {} deriv {} lumiInst {} meanLumi {} meanLumiDerivative {}", KeyFormat, etagDist, etagDeriv, |
| 124 | + CorrMapParam::Instance().lumiInst, meanLumi, meanLumiDerivative); |
| 125 | + if (key == mKey) { |
| 126 | + return; // the digitizer holds them already |
| 127 | + } |
| 128 | + |
| 129 | + const auto lockPath = mPath + ".lock"; |
| 130 | + TStopwatch timer; |
| 131 | + auto read = [&](bool warn) { |
| 132 | + auto sc = readMap(mPath, key, warn); |
| 133 | + if (sc) { |
| 134 | + digitizer.setScaledSCDistortions(std::move(sc)); |
| 135 | + LOGP(info, "TPC: scaled distortions read from {} in {:.1f} s", mPath, timer.RealTime()); |
| 136 | + mKey = key; |
| 137 | + } |
| 138 | + return mKey == key; |
| 139 | + }; |
| 140 | + { |
| 141 | + // digitizers that find the file read it concurrently |
| 142 | + FileLock lock(lockPath, LOCK_SH); |
| 143 | + if (!lock.ok()) { |
| 144 | + LOGP(warning, "TPC: cannot lock {}, the scaled distortions are computed and not shared", lockPath); |
| 145 | + compute(); |
| 146 | + return; |
| 147 | + } |
| 148 | + if (read(false)) { |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | + // one digitizer computes and writes, the others wait and then read |
| 153 | + FileLock lock(lockPath, LOCK_EX); |
| 154 | + if (!lock.ok()) { |
| 155 | + LOGP(warning, "TPC: cannot lock {}, the scaled distortions are computed and not shared", lockPath); |
| 156 | + compute(); |
| 157 | + return; |
| 158 | + } |
| 159 | + if (!read(true)) { |
| 160 | + compute(); |
| 161 | + timer.Start(); |
| 162 | + if (writeMap(mPath, key, *digitizer.getSCDistortions())) { |
| 163 | + LOGP(info, "TPC: scaled distortions written to {} in {:.1f} s", mPath, timer.RealTime()); |
| 164 | + mKey = key; |
| 165 | + } else { |
| 166 | + LOGP(warning, "TPC: cannot write the scaled distortions to {}", mPath); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments