daftprogress.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jliball.hpp"
  14. #include "platform.h"
  15. #include <limits.h>
  16. #include "jlib.hpp"
  17. #include "jio.hpp"
  18. #include "jmutex.hpp"
  19. #include "jfile.hpp"
  20. #include "jsocket.hpp"
  21. #include "daclient.hpp"
  22. #include "fterror.hpp"
  23. #include "dadfs.hpp"
  24. #include "rmtspawn.hpp"
  25. #include "filecopy.hpp"
  26. #include "fttransform.hpp"
  27. #include "daft.hpp"
  28. #include "daftmc.hpp"
  29. #include "dalienv.hpp"
  30. #include "jprop.hpp"
  31. #include "jptree.hpp"
  32. #include "jlog.hpp"
  33. #include "daftprogress.hpp"
  34. DaftProgress::DaftProgress()
  35. {
  36. startTime = get_cycles_now();
  37. scale = 1;
  38. scaleUnit = "bytes";
  39. cycleToNanoScale = cycle_to_nanosec(1000000000) / 1000000000.0;
  40. numSamples = 0;
  41. nextSample = 0;
  42. totalNodes = 0;
  43. }
  44. void DaftProgress::formatTime(char * buffer, unsigned secs)
  45. {
  46. if (secs >= 5400)
  47. sprintf(buffer, "%d:%02d:%02d", secs/3600, (secs%3600)/60, secs%60);
  48. else if (secs >= 120)
  49. sprintf(buffer, "%dm %ds", secs/60, secs%60);
  50. else
  51. sprintf(buffer, "%d secs", secs);
  52. }
  53. void DaftProgress::onProgress(unsigned __int64 sizeDone, unsigned __int64 totalSize, unsigned numNodes)
  54. {
  55. cycle_t nowTime = get_cycles_now();
  56. savedTime[nextSample] = nowTime;
  57. savedSize[nextSample] = sizeDone;
  58. nextSample++;
  59. if (nextSample == MaxSamples)
  60. nextSample = 0;
  61. if (numSamples != MaxSamples)
  62. ++numSamples;
  63. if (sizeDone != startSize)
  64. {
  65. cycle_t elapsedTime = nowTime - startTime;
  66. cycle_t timeLeft = (cycle_t)(((double)elapsedTime) * (double)(totalSize - sizeDone) / (double)(sizeDone-startSize));
  67. unsigned __int64 msGone = cycle_to_nanosec(elapsedTime)/1000000;
  68. unsigned firstSample = (nextSample+MaxSamples-numSamples) % MaxSamples;
  69. offset_t recentSizeDelta = sizeDone - savedSize[firstSample];
  70. unsigned __int64 recentTimeDelta = cycle_to_nanosec(nowTime - savedTime[firstSample])/1000000;
  71. unsigned secsLeft = (unsigned)(timeLeft * cycleToNanoScale /1000000000);
  72. char temp[20];
  73. formatTime(temp, secsLeft);
  74. displayProgress((unsigned)(sizeDone*100/totalSize), secsLeft, temp,
  75. sizeDone/scale,totalSize/scale,scaleUnit,
  76. (unsigned)(msGone ? (sizeDone-startSize)/msGone : 0),
  77. (unsigned)(recentTimeDelta ? recentSizeDelta / recentTimeDelta : 0), numNodes);
  78. if (sizeDone == totalSize)
  79. {
  80. formatTime(temp, (unsigned)(msGone/1000));
  81. displaySummary(temp, (unsigned)((totalSize - startSize)/msGone));
  82. }
  83. }
  84. }
  85. void DaftProgress::setRange(unsigned __int64 sizeReadBefore, unsigned __int64 totalSize, unsigned _totalNodes)
  86. {
  87. cycle_t nowTime = get_cycles_now();
  88. numSamples = 1;
  89. nextSample = 1;
  90. savedTime[0] = nowTime;
  91. savedSize[0] = sizeReadBefore;
  92. startTime = nowTime; // reset start time when the job actually starts copying.
  93. startSize = sizeReadBefore;
  94. totalNodes = _totalNodes;
  95. if (totalSize >= 50000000)
  96. {
  97. scale = 1000000;
  98. scaleUnit = "MB";
  99. }
  100. else if (totalSize > 500000)
  101. {
  102. scale = 1000;
  103. scaleUnit = "KB";
  104. }
  105. else
  106. {
  107. scale = 1;
  108. scaleUnit = "bytes";
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. void DemoProgress::displayProgress(unsigned percentDone, unsigned secsLeft, const char * timeLeft,
  113. unsigned __int64 scaledDone, unsigned __int64 scaledTotal, const char * scale,
  114. unsigned kbPerSecondAve, unsigned kbPerSecondRate, unsigned numNodes)
  115. {
  116. LOG(MCdebugProgress, unknownJob, "Progress: %d%% done, %s left. (%" I64F "d/%" I64F "d%s @Ave(%dKB/s) Rate(%dKB/s) [%d/%d]",
  117. percentDone, timeLeft, scaledDone, scaledTotal, scale, kbPerSecondAve, kbPerSecondRate, numNodes, totalNodes);
  118. }
  119. void DemoProgress::displaySummary(const char * timeTaken, unsigned kbPerSecond)
  120. {
  121. LOG(MCdebugProgress, unknownJob, "Summary: Total time taken %s, Average transfer %dKB/sec", timeTaken, kbPerSecond);
  122. }