anarule.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2019 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 "workunit.hpp"
  15. #include "anarule.hpp"
  16. #include "commonext.hpp"
  17. class ActivityKindRule : public AActivityRule
  18. {
  19. public:
  20. ActivityKindRule(ThorActivityKind _kind) : kind(_kind) {}
  21. virtual bool isCandidate(IWuActivity & activity) const override
  22. {
  23. return (activity.getAttr(WaKind) == kind);
  24. }
  25. protected:
  26. ThorActivityKind kind;
  27. };
  28. //--------------------------------------------------------------------------------------------------------------------
  29. class DistributeSkewRule : public ActivityKindRule
  30. {
  31. public:
  32. DistributeSkewRule() : ActivityKindRule(TAKhashdistribute) {}
  33. virtual bool check(PerformanceIssue & result, IWuActivity & activity, const WuAnalyseOptions & options) override
  34. {
  35. IWuEdge * outputEdge = activity.queryOutput(0);
  36. if (!outputEdge)
  37. return false;
  38. stat_type rowsAvg = outputEdge->getStatRaw(StNumRowsProcessed, StAvgX);
  39. if (rowsAvg < rowsThreshold)
  40. return false;
  41. stat_type rowsMaxSkew = outputEdge->getStatRaw(StNumRowsProcessed, StSkewMax);
  42. if (rowsMaxSkew > options.skewThreshold)
  43. {
  44. // Use downstream activity time to calculate approximate cost
  45. IWuActivity * targetActivity = outputEdge->queryTarget();
  46. assertex(targetActivity);
  47. stat_type timeMaxLocalExecute = targetActivity->getStatRaw(StTimeLocalExecute, StMaxX);
  48. stat_type timeAvgLocalExecute = targetActivity->getStatRaw(StTimeLocalExecute, StAvgX);
  49. // Consider ways to improve this cost calculation further
  50. stat_type cost = timeMaxLocalExecute - timeAvgLocalExecute;
  51. IWuEdge * inputEdge = activity.queryInput(0);
  52. if (inputEdge && (inputEdge->getStatRaw(StNumRowsProcessed, StSkewMax) < rowsMaxSkew))
  53. result.set(ANA_DISTRIB_SKEW_INPUT_ID, cost, "DISTRIBUTE output skew is worse than input skew");
  54. else
  55. result.set(ANA_DISTRIB_SKEW_OUTPUT_ID, cost, "Significant skew in DISTRIBUTE output");
  56. updateInformation(result, activity);
  57. return true;
  58. }
  59. return false;
  60. }
  61. protected:
  62. static const stat_type rowsThreshold = 100; // avg rows per node.
  63. };
  64. class IoSkewRule : public AActivityRule
  65. {
  66. public:
  67. IoSkewRule(StatisticKind _stat, const char * _category) : stat(_stat), category(_category)
  68. {
  69. assertex((stat==StTimeDiskReadIO)||(stat==StTimeDiskWriteIO)||(stat==StTimeSpillElapsed));
  70. }
  71. virtual bool isCandidate(IWuActivity & activity) const override
  72. {
  73. if (stat == StTimeDiskReadIO)
  74. {
  75. switch(activity.getAttr(WaKind))
  76. {
  77. case TAKdiskread:
  78. case TAKspillread:
  79. case TAKdisknormalize:
  80. case TAKdiskaggregate:
  81. case TAKdiskcount:
  82. case TAKdiskgroupaggregate:
  83. case TAKindexread:
  84. case TAKindexnormalize:
  85. case TAKindexaggregate:
  86. case TAKindexcount:
  87. case TAKindexgroupaggregate:
  88. case TAKcsvread:
  89. return true;
  90. }
  91. }
  92. else if (stat == StTimeDiskWriteIO)
  93. {
  94. switch(activity.getAttr(WaKind))
  95. {
  96. case TAKdiskwrite:
  97. case TAKspillwrite:
  98. case TAKindexwrite:
  99. case TAKcsvwrite:
  100. return true;
  101. }
  102. }
  103. else if (stat == StTimeSpillElapsed)
  104. {
  105. switch(activity.getAttr(WaKind))
  106. {
  107. case TAKspillread:
  108. case TAKspillwrite:
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. virtual bool check(PerformanceIssue & result, IWuActivity & activity, const WuAnalyseOptions & options) override
  115. {
  116. stat_type ioAvg = activity.getStatRaw(stat, StAvgX);
  117. stat_type ioMaxSkew = activity.getStatRaw(stat, StSkewMax);
  118. if (ioMaxSkew > options.skewThreshold)
  119. {
  120. stat_type timeMaxLocalExecute = activity.getStatRaw(StTimeLocalExecute, StMaxX);
  121. stat_type timeAvgLocalExecute = activity.getStatRaw(StTimeLocalExecute, StAvgX);
  122. stat_type cost;
  123. //If one node didn't spill then it is possible the skew caused all the lost time
  124. unsigned actkind = activity.getAttr(WaKind);
  125. if ((actkind==TAKspillread||actkind==TAKspillwrite) && activity.getStatRaw(stat, StMinX) == 0)
  126. cost = timeMaxLocalExecute;
  127. else
  128. cost = (timeMaxLocalExecute - timeAvgLocalExecute);
  129. IWuEdge * edge = activity.queryInput(0);
  130. if (!edge)
  131. edge = activity.queryOutput(0);
  132. auto edgeMaxSkew = edge ? edge->getStatRaw(StNumRowsProcessed, StSkewMax) : 0;
  133. // If difference between ioSkew and edgeMaxSkew > 0.05%, then child record likely to have caused skew
  134. if (ioMaxSkew > edgeMaxSkew && (ioMaxSkew-edgeMaxSkew) > ioMaxSkew/200)
  135. result.set(ANA_IOSKEW_RECORDS_ID, cost, "Significant skew in child records causes uneven %s time", category);
  136. else
  137. result.set(ANA_IOSKEW_CHILDRECORDS_ID, cost, "Significant skew in records causes uneven %s time", category);
  138. updateInformation(result, activity);
  139. return true;
  140. }
  141. return false;
  142. }
  143. protected:
  144. StatisticKind stat;
  145. const char * category;
  146. };
  147. void gatherRules(CIArrayOf<AActivityRule> & rules)
  148. {
  149. rules.append(*new DistributeSkewRule);
  150. rules.append(*new IoSkewRule(StTimeDiskReadIO, "disk read"));
  151. rules.append(*new IoSkewRule(StTimeDiskWriteIO, "disk write"));
  152. rules.append(*new IoSkewRule(StTimeSpillElapsed, "spill"));
  153. }