From a32bbd2571c67c883ce1af83b45c3fcc60ffa250 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:25:53 +0100 Subject: [PATCH] DPL: look up AliEn replicas without starting cling On an alien:// input the AOD reader started ROOT's interpreter even with the native backend. TGrid::Connect goes through the plugin manager, which runs P010_TJAlien.C, and the replica lookup gets its results as a TMap of TObjString: hashing a TPair calls TPair::Class() and initialises cling. That costs about 0.27 s and 57 MB per reader process. - connectToGrid() creates the TJAlien instance directly instead of TGrid::Connect, and sets gErrorIgnoreLevel to kPrint if it is unset so that ROOT's error handler does not read it from the environment through gROOT. It replaces the four TGrid::Connect calls. - gridReplicas() uses TJAlien::CommandRaw, which returns the results as plain strings, when JAliEn-ROOT provides it (TJALIEN_HAS_COMMAND_RAW). Otherwise it uses gGrid->Command as before. - gridReplicas() splits the LFN by hand instead of with a TUrl. A TUrl reads Url.Special from gEnv, which goes through gROOT and starts the interpreter once TROOT exists (seen with gdb in a grid run on Linux). The path is the same as TUrl::GetFile for LFNs. - The replicas the reader gets are logged at debug level. Measured with a reader-only workflow on an alien:// AO2D: the reader no longer loads libCling or any .pcm, and its footprint goes from 126-131 MB to 44-75 MB. The replicas are identical to those from TGrid::Command. The driver still opens the file with ROOT for metadata discovery, so it still starts cling. --- .../AnalysisSupport/src/AODInputFile.cxx | 70 +++++++++++++++---- Framework/AnalysisSupport/src/AODInputFile.h | 4 ++ .../AnalysisSupport/src/DataInputDirector.cxx | 2 +- Framework/AnalysisSupport/src/Plugin.cxx | 4 +- 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Framework/AnalysisSupport/src/AODInputFile.cxx b/Framework/AnalysisSupport/src/AODInputFile.cxx index 0e369e8d6824a..a4432de39f8d9 100644 --- a/Framework/AnalysisSupport/src/AODInputFile.cxx +++ b/Framework/AnalysisSupport/src/AODInputFile.cxx @@ -14,16 +14,18 @@ #include "AODNative/Errors.h" #include "Framework/Logger.h" #include +#include #include #include #include #include #include #include -#include #include +#include #include #if __has_include() +#include #include #endif @@ -105,24 +107,72 @@ std::string RootAODInputFile::monitoringDetails() const return ""; } +void connectToGrid() +{ + if (gGrid) { + return; + } +#if __has_include() + // What TGrid::Connect("alien://") does, minus the plugin handler, which starts the interpreter. + // So does ROOT's default error handler on the first message (TJAlien prints one), when it + // reads Root.ErrorIgnoreLevel through gROOT: set its default, Print, beforehand. + if (gErrorIgnoreLevel == kUnset) { + gErrorIgnoreLevel = kPrint; + } + new TJAlien("alien://", "", "", ""); +#else + TGrid::Connect("alien://"); +#endif +} + std::vector gridReplicas(std::string const& lfn) { - TUrl url(lfn.c_str()); - if (url.GetAnchor() && url.GetAnchor()[0] != '\0') { + // alien://[host]/path[?options][#member], split by hand: a TUrl reads + // Url.Special from gEnv, which goes through gROOT and starts the interpreter. + // Archive members are not for the native reader. + std::string_view path = lfn; + if (!path.starts_with("alien://") || path.find('#') != std::string_view::npos) { return {}; } + path.remove_prefix(std::string_view("alien://").size()); + path = path.substr(0, path.find('?')); + // After a host, TUrl keeps the path without its leading '/'. + auto slash = path.find('/'); + path = slash == std::string_view::npos ? std::string_view{} : path.substr(slash == 0 ? 0 : slash + 1); if (!gGrid) { - TGrid::Connect("alien://"); + connectToGrid(); } if (!gGrid) { return {}; } // As TJAlienFile::Open does for reading. - std::unique_ptr result(gGrid->Command(fmt::format("access read {}", url.GetFile()).c_str())); + auto command = fmt::format("access read {}", path); + std::vector replicas; + auto add = [&replicas](std::string const& pfn, std::string const& envelope, std::string const& se) { + if (!pfn.empty() && !envelope.empty() && pfn.find('#') == std::string::npos) { + replicas.push_back({pfn + "?&authz=" + envelope, se}); + } + }; +#ifdef TJALIEN_HAS_COMMAND_RAW + // Plain strings rather than a TGridResult of TMaps, whose hashing starts the interpreter. + if (auto* jalien = dynamic_cast(gGrid)) { + TJAlien::RawResult results; + std::map metadata; + jalien->CommandRaw(command.c_str(), results, metadata); + for (auto const& entry : results) { + auto value = [&entry](std::string const& key) -> std::string { + auto it = std::ranges::find(entry, key, &std::pair::first); + return it != entry.end() ? it->second : ""; + }; + add(value("url"), value("envelope"), value("se")); + } + return replicas; + } +#endif + std::unique_ptr result(gGrid->Command(command.c_str())); if (!result) { return {}; } - std::vector replicas; for (auto* object : *result) { auto* map = dynamic_cast(object); if (!map) { @@ -132,12 +182,7 @@ std::vector gridReplicas(std::string const& lfn) auto* v = map->GetValue(key); return v ? v->GetName() : ""; }; - auto pfn = value("url"); - auto envelope = value("envelope"); - if (pfn.empty() || envelope.empty() || pfn.find('#') != std::string::npos) { - continue; - } - replicas.push_back({pfn + "?&authz=" + envelope, value("se")}); + add(value("url"), value("envelope"), value("se")); } return replicas; } @@ -157,6 +202,7 @@ std::shared_ptr openNativeAOD(std::string const& filename, RootObj // is retried and then continues on the next replica (FailoverByteSource). std::vector list; for (auto const& replica : replicas) { + LOGP(debug, "Replica of \"{}\": {} {}", filename, replica.se, replica.url.substr(0, replica.url.find("?&authz="))); list.push_back({replica.url, replica.se}); } auto source = o2::aod::native::ByteSource::openReplicas(std::move(list), options, [filename](std::string const& message) { diff --git a/Framework/AnalysisSupport/src/AODInputFile.h b/Framework/AnalysisSupport/src/AODInputFile.h index f62bd118a2310..0fb345e769f9e 100644 --- a/Framework/AnalysisSupport/src/AODInputFile.h +++ b/Framework/AnalysisSupport/src/AODInputFile.h @@ -60,6 +60,10 @@ struct GridReplica { std::string se; }; +/// Connects gGrid to AliEn, unless it is already, without going through +/// ROOT's plugin manager when JAliEn-ROOT is linked. +void connectToGrid(); + /// Asks the AliEn catalogue (through gGrid, connecting if needed) for the /// replicas to read @a lfn from, in the order it proposes. Empty if there are /// none, or for a file inside an archive (alien://...#member), which only ROOT reads. diff --git a/Framework/AnalysisSupport/src/DataInputDirector.cxx b/Framework/AnalysisSupport/src/DataInputDirector.cxx index 25eef8ecf1fe5..90f2242c96016 100644 --- a/Framework/AnalysisSupport/src/DataInputDirector.cxx +++ b/Framework/AnalysisSupport/src/DataInputDirector.cxx @@ -154,7 +154,7 @@ void DataInputDescriptor::addFileNameHolder(FileNameHolder fn) fn.fileName.erase(0, 7); } else if (!mAlienSupport && fn.fileName.rfind("alien://", 0) == 0 && !gGrid) { LOGP(debug, "AliEn file requested. Enabling support."); - TGrid::Connect("alien://"); + connectToGrid(); mAlienSupport = true; } diff --git a/Framework/AnalysisSupport/src/Plugin.cxx b/Framework/AnalysisSupport/src/Plugin.cxx index b0a5b0b8001cf..ba25f2343c13a 100644 --- a/Framework/AnalysisSupport/src/Plugin.cxx +++ b/Framework/AnalysisSupport/src/Plugin.cxx @@ -213,7 +213,7 @@ FileAtLevel openParent(FileAtLevel const& child, ConfigParamRegistry& registry) } if (parentFilename.starts_with("alien://") && !gGrid) { - TGrid::Connect("alien://"); + connectToGrid(); } auto parentFile = openForDiscovery(parentFilename); @@ -252,7 +252,7 @@ struct DiscoverMetadataInAOD : o2::framework::ConfigDiscoveryPlugin { file.close(); } if (filename.rfind("alien://", 0) == 0 && !gGrid) { - TGrid::Connect("alien://"); + connectToGrid(); } LOGP(info, "Loading metadata from file {} in PID {}", filename, getpid()); auto currentFile = openForDiscovery(filename);