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
18#include <functional>
19#include <memory>
20#include <mutex>
21#include <queue>
22
23namespace ROOT {
24
25class TBufferMergerFile;
26
27/**
28 * \class TBufferMerger TBufferMerger.hxx
29 * \ingroup IO
30 *
31 * TBufferMerger is a class to facilitate writing data in
32 * parallel from multiple threads, while writing to a single
33 * output file. Its purpose is similar to TParallelMergingFile,
34 * but instead of using processes that connect to a network
35 * socket, TBufferMerger uses threads that each write to a
36 * TBufferMergerFile, which in turn push data into a queue
37 * managed by the TBufferMerger.
38 */
39
41public:
42 /** Constructor
43 * @param name Output file name
44 * @param option Output file creation options
45 * @param compress Output file compression level
46 */
47 TBufferMerger(const char *name, Option_t *option = "RECREATE",
49
50 /** Constructor
51 * @param output Output \c TFile
52 */
53 TBufferMerger(std::unique_ptr<TFile> output);
54
55 /** Destructor */
56 virtual ~TBufferMerger();
57
58 /** Returns a TBufferMergerFile to which data can be written.
59 * At the end, all TBufferMergerFiles get merged into the output file.
60 * The user is responsible to "cd" into the file to associate objects
61 * such as histograms or trees to it.
62 *
63 * After the creation of this file, the user must reset the kMustCleanup
64 * bit on any objects attached to it and take care of their deletion, as
65 * there is a possibility that a race condition will happen that causes
66 * a crash if ROOT manages these objects.
67 */
68 std::shared_ptr<TBufferMergerFile> GetFile();
69
70 /** Returns the number of buffers currently in the queue. */
71 size_t GetQueueSize() const;
72
73 /** Returns the number of bytes currently buffered (i.e. in the queue). */
74 size_t GetBuffered() const
75 {
76 return fBuffered;
77 }
78
79 /** Returns the current value of the auto save setting in bytes (default = 0). */
80 size_t GetAutoSave() const;
81
82 /** Returns the current merge options. */
83 const char* GetMergeOptions();
84
85 /** By default, TBufferMerger will call TFileMerger::PartialMerge() for each
86 * buffer pushed onto its merge queue. This function lets the user change
87 * this behaviour by telling TBufferMerger to accumulate at least size
88 * bytes in memory before performing a partial merge and flushing to disk.
89 * This can be useful to avoid an excessive amount of work to happen in the
90 * output thread, as the number of TTree headers (which require compression)
91 * written to disk can be reduced.
92 */
93 void SetAutoSave(size_t size);
94
95 /** Sets the merge options. SetMergeOptions("fast") will disable
96 * recompression of input data into the output if they have different
97 * compression settings.
98 * @param options TFileMerger/TFileMergeInfo merge options
99 */
100 void SetMergeOptions(const TString& options);
101
102 /** Indicates that any TTree objects in the file should be skipped
103 * and thus that steps that are specific to TTree can be skipped */
105 {
106 fMerger.SetNotrees(notrees);
107 }
108
109 /** Returns whether the file has been marked as not containing any TTree objects
110 * and thus that steps that are specific to TTree can be skipped */
112 {
113 return fMerger.GetNotrees();
114 }
115
116 /** Indicates that the temporary keys (corresponding to the object held by the directories
117 * of the TMemFile) should be compressed or not. Those object are stored in the TMemFile
118 * (and thus possibly compressed) when a thread push its data forward (by calling
119 * TBufferMergerFile::Write) and the queue is being processed by another.
120 * Once the TMemFile is picked (by any thread to be merged), *after* taking the
121 * TBufferMerger::fMergeMutex, those object are read back (and thus possibly uncompressed)
122 * and then used by merging.
123 * In order word, the compression of those objects/keys is only usefull to reduce the size
124 * in memory (of the TMemFile) and does not affect (at all) the compression factor of the end
125 * result.
126 */
127 void SetCompressTemporaryKeys(Bool_t request_compression = true)
128 {
129 fCompressTemporaryKeys = request_compression;
130 }
131
132 /** Returns whether to compressed the TKey in the TMemFile for the object held by
133 * the TDirectories. See TBufferMerger::SetCompressTemporaryKeys for more details.
134 */
136 {
138 }
139
140 friend class TBufferMergerFile;
141
142private:
143 /** TBufferMerger has no default constructor */
145
146 /** TBufferMerger has no copy constructor */
148
149 /** TBufferMerger has no copy operator */
151
152 void Init(std::unique_ptr<TFile>);
153
154 void MergeImpl();
155
156 void Merge();
157 void Push(TBufferFile *buffer);
158 bool TryMerge(TBufferMergerFile *memfile);
159
160 bool fCompressTemporaryKeys{false}; //< Enable compression of the TKeys in the TMemFile (save memory at the expense of time, end result is unchanged)
161 size_t fAutoSave{0}; //< AutoSave only every fAutoSave bytes
162 std::atomic<size_t> fBuffered{0}; //< Number of bytes currently buffered
163 TFileMerger fMerger{false, false}; //< TFileMerger used to merge all buffers
164 std::mutex fMergeMutex; //< Mutex used to lock fMerger
165 mutable std::mutex fQueueMutex; //< Mutex used to lock fQueue
166 std::queue<TBufferFile *> fQueue; //< Queue to which data is pushed and merged
167 std::vector<std::weak_ptr<TBufferMergerFile>> fAttachedFiles; //< Attached files
168};
169
170/**
171 * \class TBufferMergerFile TBufferMerger.hxx
172 * \ingroup IO
173 *
174 * A TBufferMergerFile is similar to a TMemFile, but when data
175 * is written to it, it is appended to the TBufferMerger queue.
176 * The TBufferMerger merges all data into the output file on disk.
177 */
178
180private:
181 TBufferMerger &fMerger; //< TBufferMerger this file is attached to
182
183 /** Constructor. Can only be called by TBufferMerger.
184 * @param m Merger this file is attached to. */
186
187 /** TBufferMergerFile has no default constructor. */
189
190 /** TBufferMergerFile has no copy constructor. */
192
193 /** TBufferMergerFile has no copy operator */
195
196 friend class TBufferMerger;
197
198public:
199 /** Destructor */
201
202 using TMemFile::Write;
203
204 /** Write data into a TBufferFile and append it to TBufferMerger.
205 * @param name Name
206 * @param opt Options
207 * @param bufsize Buffer size
208 * This function must be called before the TBufferMergerFile gets destroyed,
209 * or no data is appended to the TBufferMerger.
210 */
211 virtual Int_t Write(const char *name = nullptr, Int_t opt = 0, Int_t bufsize = 0) override;
212
214};
215
216} // namespace ROOT
217
218#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
Option_t Option_t option
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.
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 file has been marked as not containing any TTree objects and thus that steps that...
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
const char * name
Definition TDirectory.h:308
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:139
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
@ kUseCompiledDefault
Use the compile-time default setting.
Definition Compression.h:50
TMarker m
Definition textangle.C:8
static void output()