Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/DotNetZip/Zlib/ParallelDeflateOutputStream.cs @ 15945

Last change on this file since 15945 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 56.3 KB
Line 
1//#define Trace
2
3// ParallelDeflateOutputStream.cs
4// ------------------------------------------------------------------
5//
6// A DeflateStream that does compression only, it uses a
7// divide-and-conquer approach with multiple threads to exploit multiple
8// CPUs for the DEFLATE computation.
9//
10// last saved: <2011-July-31 14:49:40>
11//
12// ------------------------------------------------------------------
13//
14// Copyright (c) 2009-2011 by Dino Chiesa
15// All rights reserved!
16//
17// This code module is part of DotNetZip, a zipfile class library.
18//
19// ------------------------------------------------------------------
20//
21// This code is licensed under the Microsoft Public License.
22// See the file License.txt for the license details.
23// More info on: http://dotnetzip.codeplex.com
24//
25// ------------------------------------------------------------------
26
27using System;
28using System.Collections.Generic;
29using System.Threading;
30using System.IO;
31
32
33namespace OfficeOpenXml.Packaging.Ionic.Zlib
34{
35    internal class WorkItem
36    {
37        public byte[] buffer;
38        public byte[] compressed;
39        public int crc;
40        public int index;
41        public int ordinal;
42        public int inputBytesAvailable;
43        public int compressedBytesAvailable;
44        public ZlibCodec compressor;
45
46        public WorkItem(int size,
47                        Ionic.Zlib.CompressionLevel compressLevel,
48                        CompressionStrategy strategy,
49                        int ix)
50        {
51            this.buffer= new byte[size];
52            // alloc 5 bytes overhead for every block (margin of safety= 2)
53            int n = size + ((size / 32768)+1) * 5 * 2;
54            this.compressed = new byte[n];
55            this.compressor = new ZlibCodec();
56            this.compressor.InitializeDeflate(compressLevel, false);
57            this.compressor.OutputBuffer = this.compressed;
58            this.compressor.InputBuffer = this.buffer;
59            this.index = ix;
60        }
61    }
62
63    /// <summary>
64    ///   A class for compressing streams using the
65    ///   Deflate algorithm with multiple threads.
66    /// </summary>
67    ///
68    /// <remarks>
69    /// <para>
70    ///   This class performs DEFLATE compression through writing.  For
71    ///   more information on the Deflate algorithm, see IETF RFC 1951,
72    ///   "DEFLATE Compressed Data Format Specification version 1.3."
73    /// </para>
74    ///
75    /// <para>
76    ///   This class is similar to <see cref="Ionic.Zlib.DeflateStream"/>, except
77    ///   that this class is for compression only, and this implementation uses an
78    ///   approach that employs multiple worker threads to perform the DEFLATE.  On
79    ///   a multi-cpu or multi-core computer, the performance of this class can be
80    ///   significantly higher than the single-threaded DeflateStream, particularly
81    ///   for larger streams.  How large?  Anything over 10mb is a good candidate
82    ///   for parallel compression.
83    /// </para>
84    ///
85    /// <para>
86    ///   The tradeoff is that this class uses more memory and more CPU than the
87    ///   vanilla DeflateStream, and also is less efficient as a compressor. For
88    ///   large files the size of the compressed data stream can be less than 1%
89    ///   larger than the size of a compressed data stream from the vanialla
90    ///   DeflateStream.  For smaller files the difference can be larger.  The
91    ///   difference will also be larger if you set the BufferSize to be lower than
92    ///   the default value.  Your mileage may vary. Finally, for small files, the
93    ///   ParallelDeflateOutputStream can be much slower than the vanilla
94    ///   DeflateStream, because of the overhead associated to using the thread
95    ///   pool.
96    /// </para>
97    ///
98    /// </remarks>
99    /// <seealso cref="Ionic.Zlib.DeflateStream" />
100    public class ParallelDeflateOutputStream : System.IO.Stream
101    {
102
103        private static readonly int IO_BUFFER_SIZE_DEFAULT = 64 * 1024;  // 128k
104        private static readonly int BufferPairsPerCore = 4;
105
106        private System.Collections.Generic.List<WorkItem> _pool;
107        private bool                        _leaveOpen;
108        private bool                        emitting;
109        private System.IO.Stream            _outStream;
110        private int                         _maxBufferPairs;
111        private int                         _bufferSize = IO_BUFFER_SIZE_DEFAULT;
112        private AutoResetEvent              _newlyCompressedBlob;
113        //private ManualResetEvent            _writingDone;
114        //private ManualResetEvent            _sessionReset;
115        private object                      _outputLock = new object();
116        private bool                        _isClosed;
117        private bool                        _firstWriteDone;
118        private int                         _currentlyFilling;
119        private int                         _lastFilled;
120        private int                         _lastWritten;
121        private int                         _latestCompressed;
122        private int                         _Crc32;
123        private Ionic.Crc.CRC32             _runningCrc;
124        private object                      _latestLock = new object();
125        private System.Collections.Generic.Queue<int>     _toWrite;
126        private System.Collections.Generic.Queue<int>     _toFill;
127        private Int64                       _totalBytesProcessed;
128        private Ionic.Zlib.CompressionLevel _compressLevel;
129        private volatile Exception          _pendingException;
130        private bool                        _handlingException;
131        private object                      _eLock = new Object();  // protects _pendingException
132
133        // This bitfield is used only when Trace is defined.
134        //private TraceBits _DesiredTrace = TraceBits.Write | TraceBits.WriteBegin |
135        //TraceBits.WriteDone | TraceBits.Lifecycle | TraceBits.Fill | TraceBits.Flush |
136        //TraceBits.Session;
137
138        //private TraceBits _DesiredTrace = TraceBits.WriteBegin | TraceBits.WriteDone | TraceBits.Synch | TraceBits.Lifecycle  | TraceBits.Session ;
139
140        private TraceBits _DesiredTrace =
141            TraceBits.Session |
142            TraceBits.Compress |
143            TraceBits.WriteTake |
144            TraceBits.WriteEnter |
145            TraceBits.EmitEnter |
146            TraceBits.EmitDone |
147            TraceBits.EmitLock |
148            TraceBits.EmitSkip |
149            TraceBits.EmitBegin;
150
151        /// <summary>
152        /// Create a ParallelDeflateOutputStream.
153        /// </summary>
154        /// <remarks>
155        ///
156        /// <para>
157        ///   This stream compresses data written into it via the DEFLATE
158        ///   algorithm (see RFC 1951), and writes out the compressed byte stream.
159        /// </para>
160        ///
161        /// <para>
162        ///   The instance will use the default compression level, the default
163        ///   buffer sizes and the default number of threads and buffers per
164        ///   thread.
165        /// </para>
166        ///
167        /// <para>
168        ///   This class is similar to <see cref="Ionic.Zlib.DeflateStream"/>,
169        ///   except that this implementation uses an approach that employs
170        ///   multiple worker threads to perform the DEFLATE.  On a multi-cpu or
171        ///   multi-core computer, the performance of this class can be
172        ///   significantly higher than the single-threaded DeflateStream,
173        ///   particularly for larger streams.  How large?  Anything over 10mb is
174        ///   a good candidate for parallel compression.
175        /// </para>
176        ///
177        /// </remarks>
178        ///
179        /// <example>
180        ///
181        /// This example shows how to use a ParallelDeflateOutputStream to compress
182        /// data.  It reads a file, compresses it, and writes the compressed data to
183        /// a second, output file.
184        ///
185        /// <code>
186        /// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
187        /// int n= -1;
188        /// String outputFile = fileToCompress + ".compressed";
189        /// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
190        /// {
191        ///     using (var raw = System.IO.File.Create(outputFile))
192        ///     {
193        ///         using (Stream compressor = new ParallelDeflateOutputStream(raw))
194        ///         {
195        ///             while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
196        ///             {
197        ///                 compressor.Write(buffer, 0, n);
198        ///             }
199        ///         }
200        ///     }
201        /// }
202        /// </code>
203        /// <code lang="VB">
204        /// Dim buffer As Byte() = New Byte(4096) {}
205        /// Dim n As Integer = -1
206        /// Dim outputFile As String = (fileToCompress &amp; ".compressed")
207        /// Using input As Stream = File.OpenRead(fileToCompress)
208        ///     Using raw As FileStream = File.Create(outputFile)
209        ///         Using compressor As Stream = New ParallelDeflateOutputStream(raw)
210        ///             Do While (n &lt;&gt; 0)
211        ///                 If (n &gt; 0) Then
212        ///                     compressor.Write(buffer, 0, n)
213        ///                 End If
214        ///                 n = input.Read(buffer, 0, buffer.Length)
215        ///             Loop
216        ///         End Using
217        ///     End Using
218        /// End Using
219        /// </code>
220        /// </example>
221        /// <param name="stream">The stream to which compressed data will be written.</param>
222        public ParallelDeflateOutputStream(System.IO.Stream stream)
223            : this(stream, CompressionLevel.Default, CompressionStrategy.Default, false)
224        {
225        }
226
227        /// <summary>
228        ///   Create a ParallelDeflateOutputStream using the specified CompressionLevel.
229        /// </summary>
230        /// <remarks>
231        ///   See the <see cref="ParallelDeflateOutputStream(System.IO.Stream)"/>
232        ///   constructor for example code.
233        /// </remarks>
234        /// <param name="stream">The stream to which compressed data will be written.</param>
235        /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
236        public ParallelDeflateOutputStream(System.IO.Stream stream, CompressionLevel level)
237            : this(stream, level, CompressionStrategy.Default, false)
238        {
239        }
240
241        /// <summary>
242        /// Create a ParallelDeflateOutputStream and specify whether to leave the captive stream open
243        /// when the ParallelDeflateOutputStream is closed.
244        /// </summary>
245        /// <remarks>
246        ///   See the <see cref="ParallelDeflateOutputStream(System.IO.Stream)"/>
247        ///   constructor for example code.
248        /// </remarks>
249        /// <param name="stream">The stream to which compressed data will be written.</param>
250        /// <param name="leaveOpen">
251        ///    true if the application would like the stream to remain open after inflation/deflation.
252        /// </param>
253        public ParallelDeflateOutputStream(System.IO.Stream stream, bool leaveOpen)
254            : this(stream, CompressionLevel.Default, CompressionStrategy.Default, leaveOpen)
255        {
256        }
257
258        /// <summary>
259        /// Create a ParallelDeflateOutputStream and specify whether to leave the captive stream open
260        /// when the ParallelDeflateOutputStream is closed.
261        /// </summary>
262        /// <remarks>
263        ///   See the <see cref="ParallelDeflateOutputStream(System.IO.Stream)"/>
264        ///   constructor for example code.
265        /// </remarks>
266        /// <param name="stream">The stream to which compressed data will be written.</param>
267        /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
268        /// <param name="leaveOpen">
269        ///    true if the application would like the stream to remain open after inflation/deflation.
270        /// </param>
271        public ParallelDeflateOutputStream(System.IO.Stream stream, CompressionLevel level, bool leaveOpen)
272            : this(stream, CompressionLevel.Default, CompressionStrategy.Default, leaveOpen)
273        {
274        }
275
276        /// <summary>
277        /// Create a ParallelDeflateOutputStream using the specified
278        /// CompressionLevel and CompressionStrategy, and specifying whether to
279        /// leave the captive stream open when the ParallelDeflateOutputStream is
280        /// closed.
281        /// </summary>
282        /// <remarks>
283        ///   See the <see cref="ParallelDeflateOutputStream(System.IO.Stream)"/>
284        ///   constructor for example code.
285        /// </remarks>
286        /// <param name="stream">The stream to which compressed data will be written.</param>
287        /// <param name="level">A tuning knob to trade speed for effectiveness.</param>
288        /// <param name="strategy">
289        ///   By tweaking this parameter, you may be able to optimize the compression for
290        ///   data with particular characteristics.
291        /// </param>
292        /// <param name="leaveOpen">
293        ///    true if the application would like the stream to remain open after inflation/deflation.
294        /// </param>
295        public ParallelDeflateOutputStream(System.IO.Stream stream,
296                                           CompressionLevel level,
297                                           CompressionStrategy strategy,
298                                           bool leaveOpen)
299        {
300            TraceOutput(TraceBits.Lifecycle | TraceBits.Session, "-------------------------------------------------------");
301            TraceOutput(TraceBits.Lifecycle | TraceBits.Session, "Create {0:X8}", this.GetHashCode());
302            _outStream = stream;
303            _compressLevel= level;
304            Strategy = strategy;
305            _leaveOpen = leaveOpen;
306            this.MaxBufferPairs = 16; // default
307        }
308
309
310        /// <summary>
311        ///   The ZLIB strategy to be used during compression.
312        /// </summary>
313        ///
314        public CompressionStrategy Strategy
315        {
316            get;
317            private set;
318        }
319
320        /// <summary>
321        ///   The maximum number of buffer pairs to use.
322        /// </summary>
323        ///
324        /// <remarks>
325        /// <para>
326        ///   This property sets an upper limit on the number of memory buffer
327        ///   pairs to create.  The implementation of this stream allocates
328        ///   multiple buffers to facilitate parallel compression.  As each buffer
329        ///   fills up, this stream uses <see
330        ///   cref="System.Threading.ThreadPool.QueueUserWorkItem(WaitCallback)">
331        ///   ThreadPool.QueueUserWorkItem()</see>
332        ///   to compress those buffers in a background threadpool thread. After a
333        ///   buffer is compressed, it is re-ordered and written to the output
334        ///   stream.
335        /// </para>
336        ///
337        /// <para>
338        ///   A higher number of buffer pairs enables a higher degree of
339        ///   parallelism, which tends to increase the speed of compression on
340        ///   multi-cpu computers.  On the other hand, a higher number of buffer
341        ///   pairs also implies a larger memory consumption, more active worker
342        ///   threads, and a higher cpu utilization for any compression. This
343        ///   property enables the application to limit its memory consumption and
344        ///   CPU utilization behavior depending on requirements.
345        /// </para>
346        ///
347        /// <para>
348        ///   For each compression "task" that occurs in parallel, there are 2
349        ///   buffers allocated: one for input and one for output.  This property
350        ///   sets a limit for the number of pairs.  The total amount of storage
351        ///   space allocated for buffering will then be (N*S*2), where N is the
352        ///   number of buffer pairs, S is the size of each buffer (<see
353        ///   cref="BufferSize"/>).  By default, DotNetZip allocates 4 buffer
354        ///   pairs per CPU core, so if your machine has 4 cores, and you retain
355        ///   the default buffer size of 128k, then the
356        ///   ParallelDeflateOutputStream will use 4 * 4 * 2 * 128kb of buffer
357        ///   memory in total, or 4mb, in blocks of 128kb.  If you then set this
358        ///   property to 8, then the number will be 8 * 2 * 128kb of buffer
359        ///   memory, or 2mb.
360        /// </para>
361        ///
362        /// <para>
363        ///   CPU utilization will also go up with additional buffers, because a
364        ///   larger number of buffer pairs allows a larger number of background
365        ///   threads to compress in parallel. If you find that parallel
366        ///   compression is consuming too much memory or CPU, you can adjust this
367        ///   value downward.
368        /// </para>
369        ///
370        /// <para>
371        ///   The default value is 16. Different values may deliver better or
372        ///   worse results, depending on your priorities and the dynamic
373        ///   performance characteristics of your storage and compute resources.
374        /// </para>
375        ///
376        /// <para>
377        ///   This property is not the number of buffer pairs to use; it is an
378        ///   upper limit. An illustration: Suppose you have an application that
379        ///   uses the default value of this property (which is 16), and it runs
380        ///   on a machine with 2 CPU cores. In that case, DotNetZip will allocate
381        ///   4 buffer pairs per CPU core, for a total of 8 pairs.  The upper
382        ///   limit specified by this property has no effect.
383        /// </para>
384        ///
385        /// <para>
386        ///   The application can set this value at any time, but it is effective
387        ///   only before the first call to Write(), which is when the buffers are
388        ///   allocated.
389        /// </para>
390        /// </remarks>
391        public int MaxBufferPairs
392        {
393            get
394            {
395                return _maxBufferPairs;
396            }
397            set
398            {
399                if (value < 4)
400                    throw new ArgumentException("MaxBufferPairs",
401                                                "Value must be 4 or greater.");
402                _maxBufferPairs = value;
403            }
404        }
405
406        /// <summary>
407        ///   The size of the buffers used by the compressor threads.
408        /// </summary>
409        /// <remarks>
410        ///
411        /// <para>
412        ///   The default buffer size is 128k. The application can set this value
413        ///   at any time, but it is effective only before the first Write().
414        /// </para>
415        ///
416        /// <para>
417        ///   Larger buffer sizes implies larger memory consumption but allows
418        ///   more efficient compression. Using smaller buffer sizes consumes less
419        ///   memory but may result in less effective compression.  For example,
420        ///   using the default buffer size of 128k, the compression delivered is
421        ///   within 1% of the compression delivered by the single-threaded <see
422        ///   cref="Ionic.Zlib.DeflateStream"/>.  On the other hand, using a
423        ///   BufferSize of 8k can result in a compressed data stream that is 5%
424        ///   larger than that delivered by the single-threaded
425        ///   <c>DeflateStream</c>.  Excessively small buffer sizes can also cause
426        ///   the speed of the ParallelDeflateOutputStream to drop, because of
427        ///   larger thread scheduling overhead dealing with many many small
428        ///   buffers.
429        /// </para>
430        ///
431        /// <para>
432        ///   The total amount of storage space allocated for buffering will be
433        ///   (N*S*2), where N is the number of buffer pairs, and S is the size of
434        ///   each buffer (this property). There are 2 buffers used by the
435        ///   compressor, one for input and one for output.  By default, DotNetZip
436        ///   allocates 4 buffer pairs per CPU core, so if your machine has 4
437        ///   cores, then the number of buffer pairs used will be 16. If you
438        ///   accept the default value of this property, 128k, then the
439        ///   ParallelDeflateOutputStream will use 16 * 2 * 128kb of buffer memory
440        ///   in total, or 4mb, in blocks of 128kb.  If you set this property to
441        ///   64kb, then the number will be 16 * 2 * 64kb of buffer memory, or
442        ///   2mb.
443        /// </para>
444        ///
445        /// </remarks>
446        public int BufferSize
447        {
448            get { return _bufferSize;}
449            set
450            {
451                if (value < 1024)
452                    throw new ArgumentOutOfRangeException("BufferSize",
453                                                          "BufferSize must be greater than 1024 bytes");
454                _bufferSize = value;
455            }
456        }
457
458        /// <summary>
459        /// The CRC32 for the data that was written out, prior to compression.
460        /// </summary>
461        /// <remarks>
462        /// This value is meaningful only after a call to Close().
463        /// </remarks>
464        public int Crc32 { get { return _Crc32; } }
465
466
467        /// <summary>
468        /// The total number of uncompressed bytes processed by the ParallelDeflateOutputStream.
469        /// </summary>
470        /// <remarks>
471        /// This value is meaningful only after a call to Close().
472        /// </remarks>
473        public Int64 BytesProcessed { get { return _totalBytesProcessed; } }
474
475
476        private void _InitializePoolOfWorkItems()
477        {
478            _toWrite = new Queue<int>();
479            _toFill = new Queue<int>();
480            _pool = new System.Collections.Generic.List<WorkItem>();
481            int nTasks = BufferPairsPerCore * Environment.ProcessorCount;
482            nTasks = Math.Min(nTasks, _maxBufferPairs);
483            for(int i=0; i < nTasks; i++)
484            {
485                _pool.Add(new WorkItem(_bufferSize, _compressLevel, Strategy, i));
486                _toFill.Enqueue(i);
487            }
488
489            _newlyCompressedBlob = new AutoResetEvent(false);
490            _runningCrc = new Ionic.Crc.CRC32();
491            _currentlyFilling = -1;
492            _lastFilled = -1;
493            _lastWritten = -1;
494            _latestCompressed = -1;
495        }
496
497
498
499
500        /// <summary>
501        ///   Write data to the stream.
502        /// </summary>
503        ///
504        /// <remarks>
505        ///
506        /// <para>
507        ///   To use the ParallelDeflateOutputStream to compress data, create a
508        ///   ParallelDeflateOutputStream with CompressionMode.Compress, passing a
509        ///   writable output stream.  Then call Write() on that
510        ///   ParallelDeflateOutputStream, providing uncompressed data as input.  The
511        ///   data sent to the output stream will be the compressed form of the data
512        ///   written.
513        /// </para>
514        ///
515        /// <para>
516        ///   To decompress data, use the <see cref="Ionic.Zlib.DeflateStream"/> class.
517        /// </para>
518        ///
519        /// </remarks>
520        /// <param name="buffer">The buffer holding data to write to the stream.</param>
521        /// <param name="offset">the offset within that data array to find the first byte to write.</param>
522        /// <param name="count">the number of bytes to write.</param>
523        public override void Write(byte[] buffer, int offset, int count)
524        {
525            bool mustWait = false;
526
527            // This method does this:
528            //   0. handles any pending exceptions
529            //   1. write any buffers that are ready to be written,
530            //   2. fills a work buffer; when full, flip state to 'Filled',
531            //   3. if more data to be written,  goto step 1
532
533            if (_isClosed)
534                throw new InvalidOperationException();
535
536            // dispense any exceptions that occurred on the BG threads
537            if (_pendingException != null)
538            {
539                _handlingException = true;
540                var pe = _pendingException;
541                _pendingException = null;
542                throw pe;
543            }
544
545            if (count == 0) return;
546
547            if (!_firstWriteDone)
548            {
549                // Want to do this on first Write, first session, and not in the
550                // constructor.  We want to allow MaxBufferPairs to
551                // change after construction, but before first Write.
552                _InitializePoolOfWorkItems();
553                _firstWriteDone = true;
554            }
555
556
557            do
558            {
559                // may need to make buffers available
560                EmitPendingBuffers(false, mustWait);
561
562                mustWait = false;
563                // use current buffer, or get a new buffer to fill
564                int ix = -1;
565                if (_currentlyFilling >= 0)
566                {
567                    ix = _currentlyFilling;
568                    TraceOutput(TraceBits.WriteTake,
569                                "Write    notake   wi({0}) lf({1})",
570                                ix,
571                                _lastFilled);
572                }
573                else
574                {
575                    TraceOutput(TraceBits.WriteTake, "Write    take?");
576                    if (_toFill.Count == 0)
577                    {
578                        // no available buffers, so... need to emit
579                        // compressed buffers.
580                        mustWait = true;
581                        continue;
582                    }
583
584                    ix = _toFill.Dequeue();
585                    TraceOutput(TraceBits.WriteTake,
586                                "Write    take     wi({0}) lf({1})",
587                                ix,
588                                _lastFilled);
589                    ++_lastFilled; // TODO: consider rollover?
590                }
591
592                WorkItem workitem = _pool[ix];
593
594                int limit = ((workitem.buffer.Length - workitem.inputBytesAvailable) > count)
595                    ? count
596                    : (workitem.buffer.Length - workitem.inputBytesAvailable);
597
598                workitem.ordinal = _lastFilled;
599
600                TraceOutput(TraceBits.Write,
601                            "Write    lock     wi({0}) ord({1}) iba({2})",
602                            workitem.index,
603                            workitem.ordinal,
604                            workitem.inputBytesAvailable
605                            );
606
607                // copy from the provided buffer to our workitem, starting at
608                // the tail end of whatever data we might have in there currently.
609                Buffer.BlockCopy(buffer,
610                                 offset,
611                                 workitem.buffer,
612                                 workitem.inputBytesAvailable,
613                                 limit);
614
615                count -= limit;
616                offset += limit;
617                workitem.inputBytesAvailable += limit;
618                if (workitem.inputBytesAvailable == workitem.buffer.Length)
619                {
620                    // No need for interlocked.increment: the Write()
621                    // method is documented as not multi-thread safe, so
622                    // we can assume Write() calls come in from only one
623                    // thread.
624                    TraceOutput(TraceBits.Write,
625                                "Write    QUWI     wi({0}) ord({1}) iba({2}) nf({3})",
626                                workitem.index,
627                                workitem.ordinal,
628                                workitem.inputBytesAvailable );
629
630                    if (!ThreadPool.QueueUserWorkItem( _DeflateOne, workitem ))
631                        throw new Exception("Cannot enqueue workitem");
632
633                    _currentlyFilling = -1; // will get a new buffer next time
634                }
635                else
636                    _currentlyFilling = ix;
637
638                if (count > 0)
639                    TraceOutput(TraceBits.WriteEnter, "Write    more");
640            }
641            while (count > 0);  // until no more to write
642
643            TraceOutput(TraceBits.WriteEnter, "Write    exit");
644            return;
645        }
646
647
648
649        private void _FlushFinish()
650        {
651            // After writing a series of compressed buffers, each one closed
652            // with Flush.Sync, we now write the final one as Flush.Finish,
653            // and then stop.
654            byte[] buffer = new byte[128];
655            var compressor = new ZlibCodec();
656            int rc = compressor.InitializeDeflate(_compressLevel, false);
657            compressor.InputBuffer = null;
658            compressor.NextIn = 0;
659            compressor.AvailableBytesIn = 0;
660            compressor.OutputBuffer = buffer;
661            compressor.NextOut = 0;
662            compressor.AvailableBytesOut = buffer.Length;
663            rc = compressor.Deflate(FlushType.Finish);
664
665            if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
666                throw new Exception("deflating: " + compressor.Message);
667
668            if (buffer.Length - compressor.AvailableBytesOut > 0)
669            {
670                TraceOutput(TraceBits.EmitBegin,
671                            "Emit     begin    flush bytes({0})",
672                            buffer.Length - compressor.AvailableBytesOut);
673
674                _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
675
676                TraceOutput(TraceBits.EmitDone,
677                            "Emit     done     flush");
678            }
679
680            compressor.EndDeflate();
681
682            _Crc32 = _runningCrc.Crc32Result;
683        }
684
685
686        private void _Flush(bool lastInput)
687        {
688            if (_isClosed)
689                throw new InvalidOperationException();
690
691            if (emitting) return;
692
693            // compress any partial buffer
694            if (_currentlyFilling >= 0)
695            {
696                WorkItem workitem = _pool[_currentlyFilling];
697                _DeflateOne(workitem);
698                _currentlyFilling = -1; // get a new buffer next Write()
699            }
700
701            if (lastInput)
702            {
703                EmitPendingBuffers(true, false);
704                _FlushFinish();
705            }
706            else
707            {
708                EmitPendingBuffers(false, false);
709            }
710        }
711
712
713
714        /// <summary>
715        /// Flush the stream.
716        /// </summary>
717        public override void Flush()
718        {
719            if (_pendingException != null)
720            {
721                _handlingException = true;
722                var pe = _pendingException;
723                _pendingException = null;
724                throw pe;
725            }
726            if (_handlingException)
727                return;
728
729            _Flush(false);
730        }
731
732
733        /// <summary>
734        /// Close the stream.
735        /// </summary>
736        /// <remarks>
737        /// You must call Close on the stream to guarantee that all of the data written in has
738        /// been compressed, and the compressed data has been written out.
739        /// </remarks>
740        public override void Close()
741        {
742            TraceOutput(TraceBits.Session, "Close {0:X8}", this.GetHashCode());
743
744            if (_pendingException != null)
745            {
746                _handlingException = true;
747                var pe = _pendingException;
748                _pendingException = null;
749                throw pe;
750            }
751
752            if (_handlingException)
753                return;
754
755            if (_isClosed) return;
756
757            _Flush(true);
758
759            if (!_leaveOpen)
760                _outStream.Close();
761
762            _isClosed= true;
763        }
764
765
766
767        // workitem 10030 - implement a new Dispose method
768
769        /// <summary>Dispose the object</summary>
770        /// <remarks>
771        ///   <para>
772        ///     Because ParallelDeflateOutputStream is IDisposable, the
773        ///     application must call this method when finished using the instance.
774        ///   </para>
775        ///   <para>
776        ///     This method is generally called implicitly upon exit from
777        ///     a <c>using</c> scope in C# (<c>Using</c> in VB).
778        ///   </para>
779        /// </remarks>
780        new public void Dispose()
781        {
782            TraceOutput(TraceBits.Lifecycle, "Dispose  {0:X8}", this.GetHashCode());
783            Close();
784            _pool = null;
785            Dispose(true);
786        }
787
788
789
790        /// <summary>The Dispose method</summary>
791        /// <param name="disposing">
792        ///   indicates whether the Dispose method was invoked by user code.
793        /// </param>
794        protected override void Dispose(bool disposing)
795        {
796            base.Dispose(disposing);
797        }
798
799
800        /// <summary>
801        ///   Resets the stream for use with another stream.
802        /// </summary>
803        /// <remarks>
804        ///   Because the ParallelDeflateOutputStream is expensive to create, it
805        ///   has been designed so that it can be recycled and re-used.  You have
806        ///   to call Close() on the stream first, then you can call Reset() on
807        ///   it, to use it again on another stream.
808        /// </remarks>
809        ///
810        /// <param name="stream">
811        ///   The new output stream for this era.
812        /// </param>
813        ///
814        /// <example>
815        /// <code>
816        /// ParallelDeflateOutputStream deflater = null;
817        /// foreach (var inputFile in listOfFiles)
818        /// {
819        ///     string outputFile = inputFile + ".compressed";
820        ///     using (System.IO.Stream input = System.IO.File.OpenRead(inputFile))
821        ///     {
822        ///         using (var outStream = System.IO.File.Create(outputFile))
823        ///         {
824        ///             if (deflater == null)
825        ///                 deflater = new ParallelDeflateOutputStream(outStream,
826        ///                                                            CompressionLevel.Best,
827        ///                                                            CompressionStrategy.Default,
828        ///                                                            true);
829        ///             deflater.Reset(outStream);
830        ///
831        ///             while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
832        ///             {
833        ///                 deflater.Write(buffer, 0, n);
834        ///             }
835        ///         }
836        ///     }
837        /// }
838        /// </code>
839        /// </example>
840        public void Reset(Stream stream)
841        {
842            TraceOutput(TraceBits.Session, "-------------------------------------------------------");
843            TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", this.GetHashCode(), _firstWriteDone);
844
845            if (!_firstWriteDone) return;
846
847            // reset all status
848            _toWrite.Clear();
849            _toFill.Clear();
850            foreach (var workitem in _pool)
851            {
852                _toFill.Enqueue(workitem.index);
853                workitem.ordinal = -1;
854            }
855
856            _firstWriteDone = false;
857            _totalBytesProcessed = 0L;
858            _runningCrc = new Ionic.Crc.CRC32();
859            _isClosed= false;
860            _currentlyFilling = -1;
861            _lastFilled = -1;
862            _lastWritten = -1;
863            _latestCompressed = -1;
864            _outStream = stream;
865        }
866
867
868
869
870        private void EmitPendingBuffers(bool doAll, bool mustWait)
871        {
872            // When combining parallel deflation with a ZipSegmentedStream, it's
873            // possible for the ZSS to throw from within this method.  In that
874            // case, Close/Dispose will be called on this stream, if this stream
875            // is employed within a using or try/finally pair as required. But
876            // this stream is unaware of the pending exception, so the Close()
877            // method invokes this method AGAIN.  This can lead to a deadlock.
878            // Therefore, failfast if re-entering.
879
880            if (emitting) return;
881            emitting = true;
882            if (doAll || mustWait)
883                _newlyCompressedBlob.WaitOne();
884
885            do
886            {
887                int firstSkip = -1;
888                int millisecondsToWait = doAll ? 200 : (mustWait ? -1 : 0);
889                int nextToWrite = -1;
890
891                do
892                {
893                    if (Monitor.TryEnter(_toWrite, millisecondsToWait))
894                    {
895                        nextToWrite = -1;
896                        try
897                        {
898                            if (_toWrite.Count > 0)
899                                nextToWrite = _toWrite.Dequeue();
900                        }
901                        finally
902                        {
903                            Monitor.Exit(_toWrite);
904                        }
905
906                        if (nextToWrite >= 0)
907                        {
908                            WorkItem workitem = _pool[nextToWrite];
909                            if (workitem.ordinal != _lastWritten + 1)
910                            {
911                                // out of order. requeue and try again.
912                                TraceOutput(TraceBits.EmitSkip,
913                                            "Emit     skip     wi({0}) ord({1}) lw({2}) fs({3})",
914                                            workitem.index,
915                                            workitem.ordinal,
916                                            _lastWritten,
917                                            firstSkip);
918
919                                lock(_toWrite)
920                                {
921                                    _toWrite.Enqueue(nextToWrite);
922                                }
923
924                                if (firstSkip == nextToWrite)
925                                {
926                                    // We went around the list once.
927                                    // None of the items in the list is the one we want.
928                                    // Now wait for a compressor to signal again.
929                                    _newlyCompressedBlob.WaitOne();
930                                    firstSkip = -1;
931                                }
932                                else if (firstSkip == -1)
933                                    firstSkip = nextToWrite;
934
935                                continue;
936                            }
937
938                            firstSkip = -1;
939
940                            TraceOutput(TraceBits.EmitBegin,
941                                        "Emit     begin    wi({0}) ord({1})              cba({2})",
942                                        workitem.index,
943                                        workitem.ordinal,
944                                        workitem.compressedBytesAvailable);
945
946                            _outStream.Write(workitem.compressed, 0, workitem.compressedBytesAvailable);
947                            _runningCrc.Combine(workitem.crc, workitem.inputBytesAvailable);
948                            _totalBytesProcessed += workitem.inputBytesAvailable;
949                            workitem.inputBytesAvailable = 0;
950
951                            TraceOutput(TraceBits.EmitDone,
952                                        "Emit     done     wi({0}) ord({1})              cba({2}) mtw({3})",
953                                        workitem.index,
954                                        workitem.ordinal,
955                                        workitem.compressedBytesAvailable,
956                                        millisecondsToWait);
957
958                            _lastWritten = workitem.ordinal;
959                            _toFill.Enqueue(workitem.index);
960
961                            // don't wait next time through
962                            if (millisecondsToWait == -1) millisecondsToWait = 0;
963                        }
964                    }
965                    else
966                        nextToWrite = -1;
967
968                } while (nextToWrite >= 0);
969
970            //} while (doAll && (_lastWritten != _latestCompressed));
971            } while (doAll && (_lastWritten != _latestCompressed || _lastWritten != _lastFilled));
972
973            emitting = false;
974        }
975
976
977
978#if OLD
979        private void _PerpetualWriterMethod(object state)
980        {
981            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod START");
982
983            try
984            {
985                do
986                {
987                    // wait for the next session
988                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(begin) PWM");
989                    _sessionReset.WaitOne();
990                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(done)  PWM");
991
992                    if (_isDisposed) break;
993
994                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.Reset()        PWM");
995                    _sessionReset.Reset();
996
997                    // repeatedly write buffers as they become ready
998                    WorkItem workitem = null;
999                    Ionic.Zlib.CRC32 c= new Ionic.Zlib.CRC32();
1000                    do
1001                    {
1002                        workitem = _pool[_nextToWrite % _pc];
1003                        lock(workitem)
1004                        {
1005                            if (_noMoreInputForThisSegment)
1006                                TraceOutput(TraceBits.Write,
1007                                               "Write    drain    wi({0}) stat({1}) canuse({2})  cba({3})",
1008                                               workitem.index,
1009                                               workitem.status,
1010                                               (workitem.status == (int)WorkItem.Status.Compressed),
1011                                               workitem.compressedBytesAvailable);
1012
1013                            do
1014                            {
1015                                if (workitem.status == (int)WorkItem.Status.Compressed)
1016                                {
1017                                    TraceOutput(TraceBits.WriteBegin,
1018                                                   "Write    begin    wi({0}) stat({1})              cba({2})",
1019                                                   workitem.index,
1020                                                   workitem.status,
1021                                                   workitem.compressedBytesAvailable);
1022
1023                                    workitem.status = (int)WorkItem.Status.Writing;
1024                                    _outStream.Write(workitem.compressed, 0, workitem.compressedBytesAvailable);
1025                                    c.Combine(workitem.crc, workitem.inputBytesAvailable);
1026                                    _totalBytesProcessed += workitem.inputBytesAvailable;
1027                                    _nextToWrite++;
1028                                    workitem.inputBytesAvailable= 0;
1029                                    workitem.status = (int)WorkItem.Status.Done;
1030
1031                                    TraceOutput(TraceBits.WriteDone,
1032                                                   "Write    done     wi({0}) stat({1})              cba({2})",
1033                                                   workitem.index,
1034                                                   workitem.status,
1035                                                   workitem.compressedBytesAvailable);
1036
1037
1038                                    Monitor.Pulse(workitem);
1039                                    break;
1040                                }
1041                                else
1042                                {
1043                                    int wcycles = 0;
1044                                    // I've locked a workitem I cannot use.
1045                                    // Therefore, wake someone else up, and then release the lock.
1046                                    while (workitem.status != (int)WorkItem.Status.Compressed)
1047                                    {
1048                                        TraceOutput(TraceBits.WriteWait,
1049                                                       "Write    waiting  wi({0}) stat({1}) nw({2}) nf({3}) nomore({4})",
1050                                                       workitem.index,
1051                                                       workitem.status,
1052                                                       _nextToWrite, _nextToFill,
1053                                                       _noMoreInputForThisSegment );
1054
1055                                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
1056                                            break;
1057
1058                                        wcycles++;
1059
1060                                        // wake up someone else
1061                                        Monitor.Pulse(workitem);
1062                                        // release and wait
1063                                        Monitor.Wait(workitem);
1064
1065                                        if (workitem.status == (int)WorkItem.Status.Compressed)
1066                                            TraceOutput(TraceBits.WriteWait,
1067                                                           "Write    A-OK     wi({0}) stat({1}) iba({2}) cba({3}) cyc({4})",
1068                                                           workitem.index,
1069                                                           workitem.status,
1070                                                           workitem.inputBytesAvailable,
1071                                                           workitem.compressedBytesAvailable,
1072                                                           wcycles);
1073                                    }
1074
1075                                    if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
1076                                        break;
1077
1078                                }
1079                            }
1080                            while (true);
1081                        }
1082
1083                        if (_noMoreInputForThisSegment)
1084                            TraceOutput(TraceBits.Write,
1085                                           "Write    nomore  nw({0}) nf({1}) break({2})",
1086                                           _nextToWrite, _nextToFill, (_nextToWrite == _nextToFill));
1087
1088                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
1089                            break;
1090
1091                    } while (true);
1092
1093
1094                    // Finish:
1095                    // After writing a series of buffers, closing each one with
1096                    // Flush.Sync, we now write the final one as Flush.Finish, and
1097                    // then stop.
1098                    byte[] buffer = new byte[128];
1099                    ZlibCodec compressor = new ZlibCodec();
1100                    int rc = compressor.InitializeDeflate(_compressLevel, false);
1101                    compressor.InputBuffer = null;
1102                    compressor.NextIn = 0;
1103                    compressor.AvailableBytesIn = 0;
1104                    compressor.OutputBuffer = buffer;
1105                    compressor.NextOut = 0;
1106                    compressor.AvailableBytesOut = buffer.Length;
1107                    rc = compressor.Deflate(FlushType.Finish);
1108
1109                    if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
1110                        throw new Exception("deflating: " + compressor.Message);
1111
1112                    if (buffer.Length - compressor.AvailableBytesOut > 0)
1113                    {
1114                        TraceOutput(TraceBits.WriteBegin,
1115                                       "Write    begin    flush bytes({0})",
1116                                       buffer.Length - compressor.AvailableBytesOut);
1117
1118                        _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
1119
1120                        TraceOutput(TraceBits.WriteBegin,
1121                                       "Write    done     flush");
1122                    }
1123
1124                    compressor.EndDeflate();
1125
1126                    _Crc32 = c.Crc32Result;
1127
1128                    // signal that writing is complete:
1129                    TraceOutput(TraceBits.Synch, "Synch    _writingDone.Set()           PWM");
1130                    _writingDone.Set();
1131                }
1132                while (true);
1133            }
1134            catch (System.Exception exc1)
1135            {
1136                lock(_eLock)
1137                {
1138                    // expose the exception to the main thread
1139                    if (_pendingException!=null)
1140                        _pendingException = exc1;
1141                }
1142            }
1143
1144            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod FINIS");
1145        }
1146#endif
1147
1148
1149
1150
1151        private void _DeflateOne(Object wi)
1152        {
1153            // compress one buffer
1154            WorkItem workitem = (WorkItem) wi;
1155            try
1156            {
1157                int myItem = workitem.index;
1158                Ionic.Crc.CRC32 crc = new Ionic.Crc.CRC32();
1159
1160                // calc CRC on the buffer
1161                crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable);
1162
1163                // deflate it
1164                DeflateOneSegment(workitem);
1165
1166                // update status
1167                workitem.crc = crc.Crc32Result;
1168                TraceOutput(TraceBits.Compress,
1169                            "Compress          wi({0}) ord({1}) len({2})",
1170                            workitem.index,
1171                            workitem.ordinal,
1172                            workitem.compressedBytesAvailable
1173                            );
1174
1175                lock(_latestLock)
1176                {
1177                    if (workitem.ordinal > _latestCompressed)
1178                        _latestCompressed = workitem.ordinal;
1179                }
1180                lock (_toWrite)
1181                {
1182                    _toWrite.Enqueue(workitem.index);
1183                }
1184                _newlyCompressedBlob.Set();
1185            }
1186            catch (System.Exception exc1)
1187            {
1188                lock(_eLock)
1189                {
1190                    // expose the exception to the main thread
1191                    if (_pendingException!=null)
1192                        _pendingException = exc1;
1193                }
1194            }
1195        }
1196
1197
1198
1199
1200        private bool DeflateOneSegment(WorkItem workitem)
1201        {
1202            ZlibCodec compressor = workitem.compressor;
1203            int rc= 0;
1204            compressor.ResetDeflate();
1205            compressor.NextIn = 0;
1206
1207            compressor.AvailableBytesIn = workitem.inputBytesAvailable;
1208
1209            // step 1: deflate the buffer
1210            compressor.NextOut = 0;
1211            compressor.AvailableBytesOut =  workitem.compressed.Length;
1212            do
1213            {
1214                compressor.Deflate(FlushType.None);
1215            }
1216            while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
1217
1218            // step 2: flush (sync)
1219            rc = compressor.Deflate(FlushType.Sync);
1220
1221            workitem.compressedBytesAvailable= (int) compressor.TotalBytesOut;
1222            return true;
1223        }
1224
1225
1226        [System.Diagnostics.ConditionalAttribute("Trace")]
1227        private void TraceOutput(TraceBits bits, string format, params object[] varParams)
1228        {
1229            if ((bits & _DesiredTrace) != 0)
1230            {
1231                lock(_outputLock)
1232                {
1233                    int tid = Thread.CurrentThread.GetHashCode();
1234#if !SILVERLIGHT
1235                    Console.ForegroundColor = (ConsoleColor) (tid % 8 + 8);
1236#endif
1237                    Console.Write("{0:000} PDOS ", tid);
1238                    Console.WriteLine(format, varParams);
1239#if !SILVERLIGHT
1240                    Console.ResetColor();
1241#endif
1242                }
1243            }
1244        }
1245
1246
1247        // used only when Trace is defined
1248        [Flags]
1249        enum TraceBits : uint
1250        {
1251            None         = 0,
1252            NotUsed1     = 1,
1253            EmitLock     = 2,
1254            EmitEnter    = 4,    // enter _EmitPending
1255            EmitBegin    = 8,    // begin to write out
1256            EmitDone     = 16,   // done writing out
1257            EmitSkip     = 32,   // writer skipping a workitem
1258            EmitAll      = 58,   // All Emit flags
1259            Flush        = 64,
1260            Lifecycle    = 128,  // constructor/disposer
1261            Session      = 256,  // Close/Reset
1262            Synch        = 512,  // thread synchronization
1263            Instance     = 1024, // instance settings
1264            Compress     = 2048,  // compress task
1265            Write        = 4096,    // filling buffers, when caller invokes Write()
1266            WriteEnter   = 8192,    // upon entry to Write()
1267            WriteTake    = 16384,    // on _toFill.Take()
1268            All          = 0xffffffff,
1269        }
1270
1271
1272
1273        /// <summary>
1274        /// Indicates whether the stream supports Seek operations.
1275        /// </summary>
1276        /// <remarks>
1277        /// Always returns false.
1278        /// </remarks>
1279        public override bool CanSeek
1280        {
1281            get { return false; }
1282        }
1283
1284
1285        /// <summary>
1286        /// Indicates whether the stream supports Read operations.
1287        /// </summary>
1288        /// <remarks>
1289        /// Always returns false.
1290        /// </remarks>
1291        public override bool CanRead
1292        {
1293            get {return false;}
1294        }
1295
1296        /// <summary>
1297        /// Indicates whether the stream supports Write operations.
1298        /// </summary>
1299        /// <remarks>
1300        /// Returns true if the provided stream is writable.
1301        /// </remarks>
1302        public override bool CanWrite
1303        {
1304            get { return _outStream.CanWrite; }
1305        }
1306
1307        /// <summary>
1308        /// Reading this property always throws a NotSupportedException.
1309        /// </summary>
1310        public override long Length
1311        {
1312            get { throw new NotSupportedException(); }
1313        }
1314
1315        /// <summary>
1316        /// Returns the current position of the output stream.
1317        /// </summary>
1318        /// <remarks>
1319        ///   <para>
1320        ///     Because the output gets written by a background thread,
1321        ///     the value may change asynchronously.  Setting this
1322        ///     property always throws a NotSupportedException.
1323        ///   </para>
1324        /// </remarks>
1325        public override long Position
1326        {
1327            get { return _outStream.Position; }
1328            set { throw new NotSupportedException(); }
1329        }
1330
1331        /// <summary>
1332        /// This method always throws a NotSupportedException.
1333        /// </summary>
1334        /// <param name="buffer">
1335        ///   The buffer into which data would be read, IF THIS METHOD
1336        ///   ACTUALLY DID ANYTHING.
1337        /// </param>
1338        /// <param name="offset">
1339        ///   The offset within that data array at which to insert the
1340        ///   data that is read, IF THIS METHOD ACTUALLY DID
1341        ///   ANYTHING.
1342        /// </param>
1343        /// <param name="count">
1344        ///   The number of bytes to write, IF THIS METHOD ACTUALLY DID
1345        ///   ANYTHING.
1346        /// </param>
1347        /// <returns>nothing.</returns>
1348        public override int Read(byte[] buffer, int offset, int count)
1349        {
1350            throw new NotSupportedException();
1351        }
1352
1353        /// <summary>
1354        /// This method always throws a NotSupportedException.
1355        /// </summary>
1356        /// <param name="offset">
1357        ///   The offset to seek to....
1358        ///   IF THIS METHOD ACTUALLY DID ANYTHING.
1359        /// </param>
1360        /// <param name="origin">
1361        ///   The reference specifying how to apply the offset....  IF
1362        ///   THIS METHOD ACTUALLY DID ANYTHING.
1363        /// </param>
1364        /// <returns>nothing. It always throws.</returns>
1365        public override long Seek(long offset, System.IO.SeekOrigin origin)
1366        {
1367            throw new NotSupportedException();
1368        }
1369
1370        /// <summary>
1371        /// This method always throws a NotSupportedException.
1372        /// </summary>
1373        /// <param name="value">
1374        ///   The new value for the stream length....  IF
1375        ///   THIS METHOD ACTUALLY DID ANYTHING.
1376        /// </param>
1377        public override void SetLength(long value)
1378        {
1379            throw new NotSupportedException();
1380        }
1381
1382    }
1383
1384}
1385
1386
Note: See TracBrowser for help on using the repository browser.