Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TBufferMerger.hxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Philippe Canal, Witold Pokorski, and Guilherme Amadio
3
4/*************************************************************************
5 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TBufferMerger
13#define ROOT_TBufferMerger
14
15#include "TFileMerger.h"
16#include "TMemFile.h"
17#include "RConfig.h" /// R__DEPRECATED
18
19#include <functional>
20#include <memory>
21#include <mutex>
22#include <queue>
23
24namespace ROOT {
25
26class TBufferMergerFile;
27
28/**
29 * \class TBufferMerger TBufferMerger.hxx
30 * \ingroup IO
31 *
32 * TBufferMerger is a class to facilitate writing data in
33 * parallel from multiple threads, while writing to a single
34 * output file. Its purpose is similar to TParallelMergingFile,
35 * but instead of using processes that connect to a network
36 * socket, TBufferMerger uses threads that each write to a
37 * TBufferMergerFile, which in turn push data into a queue
38 * managed by the TBufferMerger.
39 */
40
42public:
43 /** Constructor
44 * @param name Output file name
45 * @param option Output file creation options
46 * @param compress Output file compression level
47 */
48 TBufferMerger(const char *name, Option_t *option = "RECREATE",
50
51 /** Constructor
52 * @param output Output \c TFile
53 */
54 TBufferMerger(std::unique_ptr<TFile> output);
55
56 /** Destructor */
57 virtual ~TBufferMerger();
58
59 /** Returns a TBufferMergerFile to which data can be written.
60 * At the end, all TBufferMergerFiles get merged into the output file.
61 * The user is responsible to "cd" into the file to associate objects
62 * such as histograms or trees to it.
63 *
64 * After the creation of this file, the user must reset the kMustCleanup
65 * bit on any objects attached to it and take care of their deletion, as
66 * there is a possibility that a race condition will happen that causes
67 * a crash if ROOT manages these objects.
68 */
69 std::shared_ptr<TBufferMergerFile> GetFile();
70
71 /** Returns the number of buffers currently in the queue. */
72 size_t GetQueueSize() const;
73
74 /** Returns the number of bytes currently buffered (i.e. in the queue). */
75 size_t GetBuffered() const
76 {
77 return fBuffered;
78 }
79
80 /** Returns the current value of the auto save setting in bytes (default = 0). */
81 size_t GetAutoSave() const;
82
83 /** Returns the current merge options. */
84 const char* GetMergeOptions();
85
86 /** By default, TBufferMerger will call TFileMerger::PartialMerge() for each
87 * buffer pushed onto its merge queue. This function lets the user change
88 * this behaviour by telling TBufferMerger to accumulate at least size
89 * bytes in memory before performing a partial merge and flushing to disk.
90 * This can be useful to avoid an excessive amount of work to happen in the
91 * output thread, as the number of TTree headers (which require compression)
92 * written to disk can be reduced.
93 */
94 void SetAutoSave(size_t size);
95
96 /** Sets the merge options. SetMergeOptions("fast") will disable
97 * recompression of input data into the output if they have different
98 * compression settings.
99 * @param options TFileMerger/TFileMergeInfo merge options
100 */
101 void SetMergeOptions(const TString& options);
102
103 /** Indicates that any TTree objects in the file should be skipped
104 * and thus that steps that are specific to TTree can be skipped */
106 {
107 fMerger.SetNotrees(notrees);
108 }
109
110 /** Returns whether the the file has been marked as not containing any TTree objects
111 * and thus that steps that are specific to TTree can be skipped */
113 {
114 return fMerger.GetNotrees();
115 }
116
117 /** Indicates that the temporary keys (corresponding to the object held by the directories
118 * of the TMemFile) should be compressed or not. Those object are stored in the TMemFile
119 * (and thus possibly compressed) when a thread push its data forward (by calling
120 * TBufferMergerFile::Write) and the queue is being processed by another.
121 * Once the TMemFile is picked (by any thread to be merged), *after* taking the
122 * TBufferMerger::fMergeMutex, those object are read back (and thus possibly uncompressed)
123 * and then used by merging.
124 * In order word, the compression of those objects/keys is only usefull to reduce the size
125 * in memory (of the TMemFile) and does not affect (at all) the compression factor of the end
126 * result.
127 */
128 void SetCompressTemporaryKeys(Bool_t request_compression = true)
129 {
130 fCompressTemporaryKeys = request_compression;
131 }
132
133 /** Returns whether to compressed the TKey in the TMemFile for the object held by
134 * the TDirectories. See TBufferMerger::SetCompressTemporaryKeys for more details.
135 */
137 {
139 }
140
141 friend class TBufferMergerFile;
142
143private:
144 /** TBufferMerger has no default constructor */
146
147 /** TBufferMerger has no copy constructor */
149
150 /** TBufferMerger has no copy operator */
152
153 void Init(std::unique_ptr<TFile>);
154
155 void MergeImpl();
156
157 void Merge();
158 void Push(TBufferFile *buffer);
159 bool TryMerge(TBufferMergerFile *memfile);
160
161 bool fCompressTemporaryKeys{false}; //< Enable compression of the TKeys in the TMemFile (save memory at the expense of time, end result is unchanged)
162 size_t fAutoSave{0}; //< AutoSave only every fAutoSave bytes
163 std::atomic<size_t> fBuffered{0}; //< Number of bytes currently buffered
164 TFileMerger fMerger{false, false}; //< TFileMerger used to merge all buffers
165 std::mutex fMergeMutex; //< Mutex used to lock fMerger
166 mutable std::mutex fQueueMutex; //< Mutex used to lock fQueue
167 std::queue<TBufferFile *> fQueue; //< Queue to which data is pushed and merged
168 std::vector<std::weak_ptr<TBufferMergerFile>> fAttachedFiles; //< Attached files
169};
170
171/**
172 * \class TBufferMergerFile TBufferMerger.hxx
173 * \ingroup IO
174 *
175 * A TBufferMergerFile is similar to a TMemFile, but when data
176 * is written to it, it is appended to the TBufferMerger queue.
177 * The TBufferMerger merges all data into the output file on disk.
178 */
179
181private:
182 TBufferMerger &fMerger; //< TBufferMerger this file is attached to
183
184 /** Constructor. Can only be called by TBufferMerger.
185 * @param m Merger this file is attached to. */
187
188 /** TBufferMergerFile has no default constructor. */
190
191 /** TBufferMergerFile has no copy constructor. */
193
194 /** TBufferMergerFile has no copy operator */
196
197 friend class TBufferMerger;
198
199public:
200 /** Destructor */
202
203 using TMemFile::Write;
204
205 /** Write data into a TBufferFile and append it to TBufferMerger.
206 * @param name Name
207 * @param opt Options
208 * @param bufsize Buffer size
209 * This function must be called before the TBufferMergerFile gets destroyed,
210 * or no data is appended to the TBufferMerger.
211 */
212 virtual Int_t Write(const char *name = nullptr, Int_t opt = 0, Int_t bufsize = 0) override;
213
215};
216
217namespace Experimental {
219 6, 28, "Please use ROOT::TBufferMerger instead of ROOT::Experimental::TBufferMerger.") = ::ROOT::TBufferMerger;
221 R__DEPRECATED(6, 28, "Please use ROOT::TBufferMergerFile instead of ROOT::Experimental::TBufferMergerFile.") =
223} // namespace Experimental
224
225} // namespace ROOT
226
227#endif
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:516
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const Bool_t kFALSE
Definition RtypesCore.h:101
const char Option_t
Definition RtypesCore.h:66
char name[80]
Definition TGX11.cxx:110
A TBufferMergerFile is similar to a TMemFile, but when data is written to it, it is appended to the T...
TBufferMergerFile()
TBufferMergerFile has no default constructor.
TBufferMergerFile(const TBufferMergerFile &)
TBufferMergerFile has no copy constructor.
TBufferMergerFile & operator=(const TBufferMergerFile &)
TBufferMergerFile has no copy operator.
virtual Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) override
Write data into a TBufferFile and append it to TBufferMerger.
ClassDefOverride(TBufferMergerFile, 0)
TBufferMerger is a class to facilitate writing data in parallel from multiple threads,...
size_t GetAutoSave() const
Returns the current value of the auto save setting in bytes (default = 0).
std::atomic< size_t > fBuffered
Bool_t GetNotrees() const
Returns whether the the file has been marked as not containing any TTree objects and thus that steps ...
bool TryMerge(TBufferMergerFile *memfile)
std::vector< std::weak_ptr< TBufferMergerFile > > fAttachedFiles
void SetAutoSave(size_t size)
By default, TBufferMerger will call TFileMerger::PartialMerge() for each buffer pushed onto its merge...
std::queue< TBufferFile * > fQueue
virtual ~TBufferMerger()
Destructor.
TBufferMerger(const TBufferMerger &)
TBufferMerger has no copy constructor.
void SetNotrees(Bool_t notrees=kFALSE)
Indicates that any TTree objects in the file should be skipped and thus that steps that are specific ...
void Push(TBufferFile *buffer)
size_t GetBuffered() const
Returns the number of bytes currently buffered (i.e.
std::shared_ptr< TBufferMergerFile > GetFile()
Returns a TBufferMergerFile to which data can be written.
TBufferMerger & operator=(const TBufferMerger &)
TBufferMerger has no copy operator.
Bool_t GetCompressTemporaryKeys() const
Returns whether to compressed the TKey in the TMemFile for the object held by the TDirectories.
void SetCompressTemporaryKeys(Bool_t request_compression=true)
Indicates that the temporary keys (corresponding to the object held by the directories of the TMemFil...
const char * GetMergeOptions()
Returns the current merge options.
TBufferMerger()
TBufferMerger has no default constructor.
void SetMergeOptions(const TString &options)
Sets the merge options.
void Init(std::unique_ptr< TFile >)
size_t GetQueueSize() const
Returns the number of buffers currently in the queue.
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
virtual Int_t Write(const char *=nullptr, Int_t=0, Int_t=0) override
Write this object to the current directory.
Definition TDirectory.h:264
This class provides file copy and merging services.
Definition TFileMerger.h:30
Bool_t GetNotrees() const
virtual void SetNotrees(Bool_t notrees=kFALSE)
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:19
Basic string class.
Definition TString.h:136
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
@ kUseCompiledDefault
Use the compile-time default setting.
Definition Compression.h:50
auto * m
Definition textangle.C:8
static void output(int code)
Definition gifencode.c:226