8 #define USE_POSIX_TIME
\r
9 #define USE_POSIX_TIME2
\r
12 #ifdef USE_POSIX_TIME
\r
14 #ifdef USE_POSIX_TIME2
\r
15 #include <sys/time.h>
\r
31 #include "../../../../C/7zCrc.h"
\r
32 #include "../../../../C/Alloc.h"
\r
35 #include "../../../Windows/Synchronization.h"
\r
36 #include "../../../Windows/Thread.h"
\r
39 #include "../../../Windows/PropVariant.h"
\r
41 static const UInt32 kUncompressMinBlockSize =
\r
48 static const UInt32 kCrcBlockSize =
\r
55 static const UInt32 kAdditionalSize = (1 << 16);
\r
56 static const UInt32 kCompressedAdditionalSize = (1 << 10);
\r
57 static const UInt32 kMaxLzmaPropSize = 5;
\r
59 class CBaseRandomGenerator
\r
64 CBaseRandomGenerator() { Init(); }
\r
65 void Init() { A1 = 362436069; A2 = 521288629;}
\r
69 ((A1 = 36969 * (A1 & 0xffff) + (A1 >> 16)) << 16) +
\r
70 ((A2 = 18000 * (A2 & 0xffff) + (A2 >> 16)) );
\r
79 CBenchBuffer(): Buffer(0) {}
\r
80 virtual ~CBenchBuffer() { Free(); }
\r
86 bool Alloc(size_t bufferSize)
\r
88 if (Buffer != 0 && BufferSize == bufferSize)
\r
91 Buffer = (Byte *)::MidAlloc(bufferSize);
\r
92 BufferSize = bufferSize;
\r
93 return (Buffer != 0);
\r
97 class CBenchRandomGenerator: public CBenchBuffer
\r
99 CBaseRandomGenerator *RG;
\r
101 void Set(CBaseRandomGenerator *rg) { RG = rg; }
\r
102 UInt32 GetVal(UInt32 &res, int numBits)
\r
104 UInt32 val = res & (((UInt32)1 << numBits) - 1);
\r
108 UInt32 GetLen(UInt32 &res)
\r
110 UInt32 len = GetVal(res, 2);
\r
111 return GetVal(res, 1 + len);
\r
117 while (pos < BufferSize)
\r
119 UInt32 res = RG->GetRnd();
\r
121 if (GetVal(res, 1) == 0 || pos < 1024)
\r
122 Buffer[pos++] = (Byte)(res & 0xFF);
\r
126 len = 1 + GetLen(res);
\r
127 if (GetVal(res, 3) != 0)
\r
129 len += GetLen(res);
\r
132 UInt32 ppp = GetVal(res, 5) + 6;
\r
133 res = RG->GetRnd();
\r
136 rep0 = /* (1 << ppp) +*/ GetVal(res, ppp);
\r
137 res = RG->GetRnd();
\r
139 while (rep0 >= pos);
\r
143 for (UInt32 i = 0; i < len && pos < BufferSize; i++, pos++)
\r
144 Buffer[pos] = Buffer[pos - rep0];
\r
151 class CBenchmarkInStream:
\r
152 public ISequentialInStream,
\r
153 public CMyUnknownImp
\r
160 void Init(const Byte *data, size_t size)
\r
166 STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
\r
169 STDMETHODIMP CBenchmarkInStream::Read(void *data, UInt32 size, UInt32 *processedSize)
\r
171 size_t remain = Size - Pos;
\r
172 UInt32 kMaxBlockSize = (1 << 20);
\r
173 if (size > kMaxBlockSize)
\r
174 size = kMaxBlockSize;
\r
176 size = (UInt32)remain;
\r
177 for (UInt32 i = 0; i < size; i++)
\r
178 ((Byte *)data)[i] = Data[Pos + i];
\r
180 if(processedSize != NULL)
\r
181 *processedSize = size;
\r
185 class CBenchmarkOutStream:
\r
186 public ISequentialOutStream,
\r
187 public CBenchBuffer,
\r
188 public CMyUnknownImp
\r
193 // CBenchmarkOutStream(): _overflow(false) {}
\r
196 // _overflow = false;
\r
200 STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
\r
203 STDMETHODIMP CBenchmarkOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
\r
205 size_t curSize = BufferSize - Pos;
\r
206 if (curSize > size)
\r
208 memcpy(Buffer + Pos, data, curSize);
\r
209 Pos += (UInt32)curSize;
\r
210 if(processedSize != NULL)
\r
211 *processedSize = (UInt32)curSize;
\r
212 if (curSize != size)
\r
214 // _overflow = true;
\r
220 class CCrcOutStream:
\r
221 public ISequentialOutStream,
\r
222 public CMyUnknownImp
\r
227 void Init() { Crc = CRC_INIT_VAL; }
\r
228 STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
\r
231 STDMETHODIMP CCrcOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
\r
233 Crc = CrcUpdate(Crc, data, size);
\r
234 if (processedSize != NULL)
\r
235 *processedSize = size;
\r
239 static UInt64 GetTimeCount()
\r
241 #ifdef USE_POSIX_TIME
\r
242 #ifdef USE_POSIX_TIME2
\r
244 if (gettimeofday(&v, 0) == 0)
\r
245 return (UInt64)(v.tv_sec) * 1000000 + v.tv_usec;
\r
246 return (UInt64)time(NULL) * 1000000;
\r
252 LARGE_INTEGER value;
\r
253 if (::QueryPerformanceCounter(&value))
\r
254 return value.QuadPart;
\r
256 return GetTickCount();
\r
260 static UInt64 GetFreq()
\r
262 #ifdef USE_POSIX_TIME
\r
263 #ifdef USE_POSIX_TIME2
\r
270 LARGE_INTEGER value;
\r
271 if (::QueryPerformanceFrequency(&value))
\r
272 return value.QuadPart;
\r
278 #ifndef USE_POSIX_TIME
\r
279 static inline UInt64 GetTime64(const FILETIME &t) { return ((UInt64)t.dwHighDateTime << 32) | t.dwLowDateTime; }
\r
282 static UInt64 GetUserTime()
\r
284 #ifdef USE_POSIX_TIME
\r
287 FILETIME creationTime, exitTime, kernelTime, userTime;
\r
290 ::GetThreadTimes(::GetCurrentThread()
\r
292 ::GetProcessTimes(::GetCurrentProcess()
\r
294 , &creationTime, &exitTime, &kernelTime, &userTime) != 0)
\r
295 return GetTime64(userTime) + GetTime64(kernelTime);
\r
296 return (UInt64)GetTickCount() * 10000;
\r
300 static UInt64 GetUserFreq()
\r
302 #ifdef USE_POSIX_TIME
\r
303 return CLOCKS_PER_SEC;
\r
309 class CBenchProgressStatus
\r
312 NWindows::NSynchronization::CCriticalSection CS;
\r
317 void SetResult(HRESULT res)
\r
320 NWindows::NSynchronization::CCriticalSectionLock lock(CS);
\r
324 HRESULT GetResult()
\r
327 NWindows::NSynchronization::CCriticalSectionLock lock(CS);
\r
333 class CBenchProgressInfo:
\r
334 public ICompressProgressInfo,
\r
335 public CMyUnknownImp
\r
338 CBenchProgressStatus *Status;
\r
339 CBenchInfo BenchInfo;
\r
341 IBenchCallback *callback;
\r
342 CBenchProgressInfo(): callback(0) {}
\r
344 STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize);
\r
347 static void SetStartTime(CBenchInfo &bi)
\r
349 bi.GlobalFreq = GetFreq();
\r
350 bi.UserFreq = GetUserFreq();
\r
351 bi.GlobalTime = ::GetTimeCount();
\r
352 bi.UserTime = ::GetUserTime();
\r
355 static void SetFinishTime(const CBenchInfo &biStart, CBenchInfo &dest)
\r
357 dest.GlobalFreq = GetFreq();
\r
358 dest.UserFreq = GetUserFreq();
\r
359 dest.GlobalTime = ::GetTimeCount() - biStart.GlobalTime;
\r
360 dest.UserTime = ::GetUserTime() - biStart.UserTime;
\r
363 STDMETHODIMP CBenchProgressInfo::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
\r
365 HRESULT res = Status->GetResult();
\r
370 CBenchInfo info = BenchInfo;
\r
371 SetFinishTime(BenchInfo, info);
\r
372 if (Status->EncodeMode)
\r
374 info.UnpackSize = *inSize;
\r
375 info.PackSize = *outSize;
\r
376 res = callback->SetEncodeResult(info, false);
\r
380 info.PackSize = BenchInfo.PackSize + *inSize;
\r
381 info.UnpackSize = BenchInfo.UnpackSize + *outSize;
\r
382 res = callback->SetDecodeResult(info, false);
\r
385 Status->SetResult(res);
\r
389 static const int kSubBits = 8;
\r
391 static UInt32 GetLogSize(UInt32 size)
\r
393 for (int i = kSubBits; i < 32; i++)
\r
394 for (UInt32 j = 0; j < (1 << kSubBits); j++)
\r
395 if (size <= (((UInt32)1) << i) + (j << (i - kSubBits)))
\r
396 return (i << kSubBits) + j;
\r
397 return (32 << kSubBits);
\r
400 static void NormalizeVals(UInt64 &v1, UInt64 &v2)
\r
402 while (v1 > 1000000)
\r
409 UInt64 GetUsage(const CBenchInfo &info)
\r
411 UInt64 userTime = info.UserTime;
\r
412 UInt64 userFreq = info.UserFreq;
\r
413 UInt64 globalTime = info.GlobalTime;
\r
414 UInt64 globalFreq = info.GlobalFreq;
\r
415 NormalizeVals(userTime, userFreq);
\r
416 NormalizeVals(globalFreq, globalTime);
\r
419 if (globalTime == 0)
\r
421 return userTime * globalFreq * 1000000 / userFreq / globalTime;
\r
424 UInt64 GetRatingPerUsage(const CBenchInfo &info, UInt64 rating)
\r
426 UInt64 userTime = info.UserTime;
\r
427 UInt64 userFreq = info.UserFreq;
\r
428 UInt64 globalTime = info.GlobalTime;
\r
429 UInt64 globalFreq = info.GlobalFreq;
\r
430 NormalizeVals(userFreq, userTime);
\r
431 NormalizeVals(globalTime, globalFreq);
\r
432 if (globalFreq == 0)
\r
436 return userFreq * globalTime / globalFreq * rating / userTime;
\r
439 static UInt64 MyMultDiv64(UInt64 value, UInt64 elapsedTime, UInt64 freq)
\r
441 UInt64 elTime = elapsedTime;
\r
442 NormalizeVals(freq, elTime);
\r
445 return value * freq / elTime;
\r
448 UInt64 GetCompressRating(UInt32 dictionarySize, UInt64 elapsedTime, UInt64 freq, UInt64 size)
\r
450 UInt64 t = GetLogSize(dictionarySize) - (kBenchMinDicLogSize << kSubBits);
\r
451 UInt64 numCommandsForOne = 870 + ((t * t * 5) >> (2 * kSubBits));
\r
452 UInt64 numCommands = (UInt64)(size) * numCommandsForOne;
\r
453 return MyMultDiv64(numCommands, elapsedTime, freq);
\r
456 UInt64 GetDecompressRating(UInt64 elapsedTime, UInt64 freq, UInt64 outSize, UInt64 inSize, UInt32 numIterations)
\r
458 UInt64 numCommands = (inSize * 200 + outSize * 4) * numIterations;
\r
459 return MyMultDiv64(numCommands, elapsedTime, freq);
\r
462 struct CEncoderInfo;
\r
464 struct CEncoderInfo
\r
467 NWindows::CThread thread[2];
\r
469 CMyComPtr<ICompressCoder> encoder;
\r
470 CBenchProgressInfo *progressInfoSpec[2];
\r
471 CMyComPtr<ICompressProgressInfo> progressInfo[2];
\r
472 UInt32 NumIterations;
\r
477 struct CDecoderInfo
\r
479 CEncoderInfo *Encoder;
\r
480 UInt32 DecoderIndex;
\r
486 CDecoderInfo decodersInfo[2];
\r
488 CMyComPtr<ICompressCoder> decoders[2];
\r
489 HRESULT Results[2];
\r
490 CBenchmarkOutStream *outStreamSpec;
\r
491 CMyComPtr<ISequentialOutStream> outStream;
\r
492 IBenchCallback *callback;
\r
494 UInt32 kBufferSize;
\r
495 UInt32 compressedSize;
\r
496 CBenchRandomGenerator rg;
\r
497 CBenchmarkOutStream *propStreamSpec;
\r
498 CMyComPtr<ISequentialOutStream> propStream;
\r
499 HRESULT Init(UInt32 dictionarySize, UInt32 numThreads, CBaseRandomGenerator *rg);
\r
501 HRESULT Decode(UInt32 decoderIndex);
\r
503 CEncoderInfo(): outStreamSpec(0), callback(0), propStreamSpec(0) {}
\r
506 static THREAD_FUNC_DECL EncodeThreadFunction(void *param)
\r
508 CEncoderInfo *encoder = (CEncoderInfo *)param;
\r
510 alloca(encoder->AllocaSize);
\r
512 HRESULT res = encoder->Encode();
\r
513 encoder->Results[0] = res;
\r
515 encoder->progressInfoSpec[0]->Status->SetResult(res);
\r
519 static THREAD_FUNC_DECL DecodeThreadFunction(void *param)
\r
521 CDecoderInfo *decoder = (CDecoderInfo *)param;
\r
523 alloca(decoder->AllocaSize);
\r
525 CEncoderInfo *encoder = decoder->Encoder;
\r
526 encoder->Results[decoder->DecoderIndex] = encoder->Decode(decoder->DecoderIndex);
\r
530 HRESULT CreateEncoderThread()
\r
532 return thread[0].Create(EncodeThreadFunction, this);
\r
535 HRESULT CreateDecoderThread(int index, bool callbackMode
\r
537 , size_t allocaSize
\r
541 CDecoderInfo &decoder = decodersInfo[index];
\r
542 decoder.DecoderIndex = index;
\r
543 decoder.Encoder = this;
\r
545 decoder.AllocaSize = allocaSize;
\r
547 decoder.CallbackMode = callbackMode;
\r
548 return thread[index].Create(DecodeThreadFunction, &decoder);
\r
553 HRESULT CEncoderInfo::Init(UInt32 dictionarySize, UInt32 numThreads, CBaseRandomGenerator *rgLoc)
\r
556 kBufferSize = dictionarySize + kAdditionalSize;
\r
557 UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;
\r
558 if (!rg.Alloc(kBufferSize))
\r
559 return E_OUTOFMEMORY;
\r
561 crc = CrcCalc(rg.Buffer, rg.BufferSize);
\r
563 outStreamSpec = new CBenchmarkOutStream;
\r
564 if (!outStreamSpec->Alloc(kCompressedBufferSize))
\r
565 return E_OUTOFMEMORY;
\r
567 outStream = outStreamSpec;
\r
569 propStreamSpec = 0;
\r
572 propStreamSpec = new CBenchmarkOutStream;
\r
573 propStream = propStreamSpec;
\r
575 if (!propStreamSpec->Alloc(kMaxLzmaPropSize))
\r
576 return E_OUTOFMEMORY;
\r
577 propStreamSpec->Init();
\r
581 NCoderPropID::kDictionarySize,
\r
582 NCoderPropID::kNumThreads
\r
584 const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);
\r
585 PROPVARIANT props[kNumProps];
\r
586 props[0].vt = VT_UI4;
\r
587 props[0].ulVal = dictionarySize;
\r
589 props[1].vt = VT_UI4;
\r
590 props[1].ulVal = numThreads;
\r
593 CMyComPtr<ICompressSetCoderProperties> setCoderProperties;
\r
594 RINOK(encoder.QueryInterface(IID_ICompressSetCoderProperties, &setCoderProperties));
\r
595 if (!setCoderProperties)
\r
597 RINOK(setCoderProperties->SetCoderProperties(propIDs, props, kNumProps));
\r
599 CMyComPtr<ICompressWriteCoderProperties> writeCoderProperties;
\r
600 encoder.QueryInterface(IID_ICompressWriteCoderProperties, &writeCoderProperties);
\r
601 if (writeCoderProperties)
\r
603 RINOK(writeCoderProperties->WriteCoderProperties(propStream));
\r
609 HRESULT CEncoderInfo::Encode()
\r
611 CBenchmarkInStream *inStreamSpec = new CBenchmarkInStream;
\r
612 CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
\r
613 inStreamSpec->Init(rg.Buffer, rg.BufferSize);
\r
614 outStreamSpec->Init();
\r
616 RINOK(encoder->Code(inStream, outStream, 0, 0, progressInfo[0]));
\r
617 compressedSize = outStreamSpec->Pos;
\r
622 HRESULT CEncoderInfo::Decode(UInt32 decoderIndex)
\r
624 CBenchmarkInStream *inStreamSpec = new CBenchmarkInStream;
\r
625 CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
\r
626 CMyComPtr<ICompressCoder> &decoder = decoders[decoderIndex];
\r
628 CMyComPtr<ICompressSetDecoderProperties2> compressSetDecoderProperties;
\r
629 decoder.QueryInterface(IID_ICompressSetDecoderProperties2, &compressSetDecoderProperties);
\r
630 if (!compressSetDecoderProperties)
\r
633 CCrcOutStream *crcOutStreamSpec = new CCrcOutStream;
\r
634 CMyComPtr<ISequentialOutStream> crcOutStream = crcOutStreamSpec;
\r
636 CBenchProgressInfo *pi = progressInfoSpec[decoderIndex];
\r
637 pi->BenchInfo.UnpackSize = 0;
\r
638 pi->BenchInfo.PackSize = 0;
\r
640 for (UInt32 j = 0; j < NumIterations; j++)
\r
642 inStreamSpec->Init(outStreamSpec->Buffer, compressedSize);
\r
643 crcOutStreamSpec->Init();
\r
645 RINOK(compressSetDecoderProperties->SetDecoderProperties2(propStreamSpec->Buffer, propStreamSpec->Pos));
\r
646 UInt64 outSize = kBufferSize;
\r
647 RINOK(decoder->Code(inStream, crcOutStream, 0, &outSize, progressInfo[decoderIndex]));
\r
648 if (CRC_GET_DIGEST(crcOutStreamSpec->Crc) != crc)
\r
650 pi->BenchInfo.UnpackSize += kBufferSize;
\r
651 pi->BenchInfo.PackSize += compressedSize;
\r
657 static const UInt32 kNumThreadsMax = (1 << 16);
\r
659 struct CBenchEncoders
\r
661 CEncoderInfo *encoders;
\r
662 CBenchEncoders(UInt32 num): encoders(0) { encoders = new CEncoderInfo[num]; }
\r
663 ~CBenchEncoders() { delete []encoders; }
\r
667 DECL_EXTERNAL_CODECS_LOC_VARS
\r
668 UInt32 numThreads, UInt32 dictionarySize, IBenchCallback *callback)
\r
670 UInt32 numEncoderThreads =
\r
672 (numThreads > 1 ? numThreads / 2 : 1);
\r
676 UInt32 numSubDecoderThreads =
\r
678 (numThreads > 1 ? 2 : 1);
\r
682 if (dictionarySize < (1 << kBenchMinDicLogSize) || numThreads < 1 || numEncoderThreads > kNumThreadsMax)
\r
684 return E_INVALIDARG;
\r
687 CBenchEncoders encodersSpec(numEncoderThreads);
\r
688 CEncoderInfo *encoders = encodersSpec.encoders;
\r
692 for (i = 0; i < numEncoderThreads; i++)
\r
694 CEncoderInfo &encoder = encoders[i];
\r
695 encoder.callback = (i == 0) ? callback : 0;
\r
697 const UInt32 kLzmaId = 0x030101;
\r
698 RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS kLzmaId, encoder.encoder, true));
\r
699 if (!encoder.encoder)
\r
701 for (UInt32 j = 0; j < numSubDecoderThreads; j++)
\r
703 RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS kLzmaId, encoder.decoders[j], false));
\r
704 if (!encoder.decoders[j])
\r
709 CBaseRandomGenerator rg;
\r
711 for (i = 0; i < numEncoderThreads; i++)
\r
713 RINOK(encoders[i].Init(dictionarySize, numThreads, &rg));
\r
716 CBenchProgressStatus status;
\r
718 status.EncodeMode = true;
\r
720 for (i = 0; i < numEncoderThreads; i++)
\r
722 CEncoderInfo &encoder = encoders[i];
\r
723 for (int j = 0; j < 2; j++)
\r
725 encoder.progressInfo[j] = encoder.progressInfoSpec[j] = new CBenchProgressInfo;
\r
726 encoder.progressInfoSpec[j]->Status = &status;
\r
730 encoder.progressInfoSpec[0]->callback = callback;
\r
731 encoder.progressInfoSpec[0]->BenchInfo.NumIterations = numEncoderThreads;
\r
732 SetStartTime(encoder.progressInfoSpec[0]->BenchInfo);
\r
736 if (numEncoderThreads > 1)
\r
739 encoder.AllocaSize = (i * 16 * 21) & 0x7FF;
\r
741 RINOK(encoder.CreateEncoderThread())
\r
746 RINOK(encoder.Encode());
\r
750 if (numEncoderThreads > 1)
\r
751 for (i = 0; i < numEncoderThreads; i++)
\r
752 encoders[i].thread[0].Wait();
\r
759 SetFinishTime(encoders[0].progressInfoSpec[0]->BenchInfo, info);
\r
760 info.UnpackSize = 0;
\r
762 info.NumIterations = 1; // progressInfoSpec->NumIterations;
\r
763 for (i = 0; i < numEncoderThreads; i++)
\r
765 CEncoderInfo &encoder = encoders[i];
\r
766 info.UnpackSize += encoder.kBufferSize;
\r
767 info.PackSize += encoder.compressedSize;
\r
769 RINOK(callback->SetEncodeResult(info, true));
\r
773 status.EncodeMode = false;
\r
775 UInt32 numDecoderThreads = numEncoderThreads * numSubDecoderThreads;
\r
776 for (i = 0; i < numEncoderThreads; i++)
\r
778 CEncoderInfo &encoder = encoders[i];
\r
779 encoder.NumIterations = 2 + kUncompressMinBlockSize / encoder.kBufferSize;
\r
783 encoder.progressInfoSpec[0]->callback = callback;
\r
784 encoder.progressInfoSpec[0]->BenchInfo.NumIterations = numDecoderThreads;
\r
785 SetStartTime(encoder.progressInfoSpec[0]->BenchInfo);
\r
789 if (numDecoderThreads > 1)
\r
791 for (UInt32 j = 0; j < numSubDecoderThreads; j++)
\r
793 HRESULT res = encoder.CreateDecoderThread(j, (i == 0 && j == 0)
\r
795 , ((i * numSubDecoderThreads + j) * 16 * 21) & 0x7FF
\r
804 RINOK(encoder.Decode(0));
\r
808 HRESULT res = S_OK;
\r
809 if (numDecoderThreads > 1)
\r
810 for (i = 0; i < numEncoderThreads; i++)
\r
811 for (UInt32 j = 0; j < numSubDecoderThreads; j++)
\r
813 CEncoderInfo &encoder = encoders[i];
\r
814 encoder.thread[j].Wait();
\r
815 if (encoder.Results[j] != S_OK)
\r
816 res = encoder.Results[j];
\r
821 SetFinishTime(encoders[0].progressInfoSpec[0]->BenchInfo, info);
\r
824 if (numDecoderThreads > 1)
\r
825 for (i = 0; i < numEncoderThreads; i++)
\r
826 for (UInt32 j = 0; j < numSubDecoderThreads; j++)
\r
828 FILETIME creationTime, exitTime, kernelTime, userTime;
\r
829 if (::GetThreadTimes(encoders[i].thread[j], &creationTime, &exitTime, &kernelTime, &userTime) != 0)
\r
830 info.UserTime += GetTime64(userTime) + GetTime64(kernelTime);
\r
834 info.UnpackSize = 0;
\r
836 info.NumIterations = numSubDecoderThreads * encoders[0].NumIterations;
\r
837 for (i = 0; i < numEncoderThreads; i++)
\r
839 CEncoderInfo &encoder = encoders[i];
\r
840 info.UnpackSize += encoder.kBufferSize;
\r
841 info.PackSize += encoder.compressedSize;
\r
843 RINOK(callback->SetDecodeResult(info, false));
\r
844 RINOK(callback->SetDecodeResult(info, true));
\r
849 inline UInt64 GetLZMAUsage(bool multiThread, UInt32 dictionary)
\r
851 UInt32 hs = dictionary - 1;
\r
858 if (hs > (1 << 24))
\r
861 return ((hs + (1 << 16)) + (UInt64)dictionary * 2) * 4 + (UInt64)dictionary * 3 / 2 +
\r
862 (1 << 20) + (multiThread ? (6 << 20) : 0);
\r
865 UInt64 GetBenchMemoryUsage(UInt32 numThreads, UInt32 dictionary)
\r
867 const UInt32 kBufferSize = dictionary;
\r
868 const UInt32 kCompressedBufferSize = (kBufferSize / 2);
\r
869 UInt32 numSubThreads = (numThreads > 1) ? 2 : 1;
\r
870 UInt32 numBigThreads = numThreads / numSubThreads;
\r
871 return (kBufferSize + kCompressedBufferSize +
\r
872 GetLZMAUsage((numThreads > 1), dictionary) + (2 << 20)) * numBigThreads;
\r
875 static bool CrcBig(const void *data, UInt32 size, UInt32 numCycles, UInt32 crcBase)
\r
877 for (UInt32 i = 0; i < numCycles; i++)
\r
878 if (CrcCalc(data, size) != crcBase)
\r
886 NWindows::CThread Thread;
\r
899 static THREAD_FUNC_DECL CrcThreadFunction(void *param)
\r
901 CCrcInfo *p = (CCrcInfo *)param;
\r
902 p->Res = CrcBig(p->Data, p->Size, p->NumCycles, p->Crc);
\r
910 CCrcThreads(): Items(0), NumThreads(0) {}
\r
913 for (UInt32 i = 0; i < NumThreads; i++)
\r
925 static UInt32 CrcCalc1(const Byte *buf, UInt32 size)
\r
927 UInt32 crc = CRC_INIT_VAL;;
\r
928 for (UInt32 i = 0; i < size; i++)
\r
929 crc = CRC_UPDATE_BYTE(crc, buf[i]);
\r
930 return CRC_GET_DIGEST(crc);
\r
933 static void RandGen(Byte *buf, UInt32 size, CBaseRandomGenerator &RG)
\r
935 for (UInt32 i = 0; i < size; i++)
\r
936 buf[i] = (Byte)RG.GetRnd();
\r
939 static UInt32 RandGenCrc(Byte *buf, UInt32 size, CBaseRandomGenerator &RG)
\r
941 RandGen(buf, size, RG);
\r
942 return CrcCalc1(buf, size);
\r
945 bool CrcInternalTest()
\r
947 CBenchBuffer buffer;
\r
948 const UInt32 kBufferSize0 = (1 << 8);
\r
949 const UInt32 kBufferSize1 = (1 << 10);
\r
950 const UInt32 kCheckSize = (1 << 5);
\r
951 if (!buffer.Alloc(kBufferSize0 + kBufferSize1))
\r
953 Byte *buf = buffer.Buffer;
\r
955 for (i = 0; i < kBufferSize0; i++)
\r
957 UInt32 crc1 = CrcCalc1(buf, kBufferSize0);
\r
958 if (crc1 != 0x29058C73)
\r
960 CBaseRandomGenerator RG;
\r
961 RandGen(buf + kBufferSize0, kBufferSize1, RG);
\r
962 for (i = 0; i < kBufferSize0 + kBufferSize1 - kCheckSize; i++)
\r
963 for (UInt32 j = 0; j < kCheckSize; j++)
\r
964 if (CrcCalc1(buf + i, j) != CrcCalc(buf + i, j))
\r
969 HRESULT CrcBench(UInt32 numThreads, UInt32 bufferSize, UInt64 &speed)
\r
971 if (numThreads == 0)
\r
974 CBenchBuffer buffer;
\r
975 size_t totalSize = (size_t)bufferSize * numThreads;
\r
976 if (totalSize / numThreads != bufferSize)
\r
977 return E_OUTOFMEMORY;
\r
978 if (!buffer.Alloc(totalSize))
\r
979 return E_OUTOFMEMORY;
\r
981 Byte *buf = buffer.Buffer;
\r
982 CBaseRandomGenerator RG;
\r
983 UInt32 numCycles = (kCrcBlockSize) / ((bufferSize >> 2) + 1) + 1;
\r
987 CCrcThreads threads;
\r
988 if (numThreads > 1)
\r
990 threads.Items = new CCrcInfo[numThreads];
\r
992 for (i = 0; i < numThreads; i++)
\r
994 CCrcInfo &info = threads.Items[i];
\r
995 Byte *data = buf + (size_t)bufferSize * i;
\r
997 info.NumCycles = numCycles;
\r
998 info.Size = bufferSize;
\r
999 info.Crc = RandGenCrc(data, bufferSize, RG);
\r
1001 timeVal = GetTimeCount();
\r
1002 for (i = 0; i < numThreads; i++)
\r
1004 CCrcInfo &info = threads.Items[i];
\r
1005 RINOK(info.Thread.Create(CrcThreadFunction, &info));
\r
1006 threads.NumThreads++;
\r
1008 threads.WaitAll();
\r
1009 for (i = 0; i < numThreads; i++)
\r
1010 if (!threads.Items[i].Res)
\r
1016 UInt32 crc = RandGenCrc(buf, bufferSize, RG);
\r
1017 timeVal = GetTimeCount();
\r
1018 if (!CrcBig(buf, bufferSize, numCycles, crc))
\r
1021 timeVal = GetTimeCount() - timeVal;
\r
1025 UInt64 size = (UInt64)numCycles * totalSize;
\r
1026 speed = MyMultDiv64(size, timeVal, GetFreq());
\r