anarule.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 IAnalyserOptions & 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 < options.queryOption(watOptMinRowsPerNode))
  40. return false;
  41. stat_type rowsMaxSkew = outputEdge->getStatRaw(StNumRowsProcessed, StSkewMax);
  42. if (rowsMaxSkew > options.queryOption(watOptSkewThreshold))
  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. };
  62. class IoSkewRule : public AActivityRule
  63. {
  64. public:
  65. IoSkewRule(StatisticKind _stat, const char * _category) : stat(_stat), category(_category)
  66. {
  67. assertex((stat==StTimeDiskReadIO)||(stat==StTimeDiskWriteIO)||(stat==StTimeSpillElapsed));
  68. }
  69. virtual bool isCandidate(IWuActivity & activity) const override
  70. {
  71. if (stat == StTimeDiskReadIO)
  72. {
  73. switch(activity.getAttr(WaKind))
  74. {
  75. case TAKdiskread:
  76. case TAKspillread:
  77. case TAKdisknormalize:
  78. case TAKdiskaggregate:
  79. case TAKdiskcount:
  80. case TAKdiskgroupaggregate:
  81. case TAKindexread:
  82. case TAKindexnormalize:
  83. case TAKindexaggregate:
  84. case TAKindexcount:
  85. case TAKindexgroupaggregate:
  86. case TAKcsvread:
  87. return true;
  88. }
  89. }
  90. else if (stat == StTimeDiskWriteIO)
  91. {
  92. switch(activity.getAttr(WaKind))
  93. {
  94. case TAKdiskwrite:
  95. case TAKspillwrite:
  96. case TAKindexwrite:
  97. case TAKcsvwrite:
  98. return true;
  99. }
  100. }
  101. else if (stat == StTimeSpillElapsed)
  102. {
  103. switch(activity.getAttr(WaKind))
  104. {
  105. case TAKspillread:
  106. case TAKspillwrite:
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. virtual bool check(PerformanceIssue & result, IWuActivity & activity, const IAnalyserOptions & options) override
  113. {
  114. stat_type ioAvg = activity.getStatRaw(stat, StAvgX);
  115. stat_type ioMaxSkew = activity.getStatRaw(stat, StSkewMax);
  116. if (ioMaxSkew > options.queryOption(watOptSkewThreshold))
  117. {
  118. stat_type timeMaxLocalExecute = activity.getStatRaw(StTimeLocalExecute, StMaxX);
  119. stat_type timeAvgLocalExecute = activity.getStatRaw(StTimeLocalExecute, StAvgX);
  120. stat_type cost;
  121. //If one node didn't spill then it is possible the skew caused all the lost time
  122. unsigned actkind = activity.getAttr(WaKind);
  123. if ((actkind==TAKspillread||actkind==TAKspillwrite) && activity.getStatRaw(stat, StMinX) == 0)
  124. cost = timeMaxLocalExecute;
  125. else
  126. cost = (timeMaxLocalExecute - timeAvgLocalExecute);
  127. result.set(ANA_IOSKEW_CHILDRECORDS_ID, cost, "Significant skew in records causes uneven %s time", category);
  128. updateInformation(result, activity);
  129. return true;
  130. }
  131. return false;
  132. }
  133. protected:
  134. StatisticKind stat;
  135. const char * category;
  136. };
  137. class KeyedJoinExcessRejectedRowsRule : public ActivityKindRule
  138. {
  139. public:
  140. KeyedJoinExcessRejectedRowsRule() : ActivityKindRule(TAKkeyedjoin) {}
  141. virtual bool check(PerformanceIssue & result, IWuActivity & activity, const IAnalyserOptions & options) override
  142. {
  143. stat_type preFiltered = activity.getStatRaw(StNumPreFiltered);
  144. if (preFiltered)
  145. {
  146. IWuEdge * inputEdge = activity.queryInput(0);
  147. stat_type rowscnt = inputEdge->getStatRaw(StNumRowsProcessed);
  148. if (rowscnt)
  149. {
  150. stat_type preFilteredPer = statPercent( (double) preFiltered * 100.0 / rowscnt );
  151. if (preFilteredPer > options.queryOption(watPreFilteredKJThreshold))
  152. {
  153. IWuActivity * inputActivity = inputEdge->querySource();
  154. // Use input activity as the basis of cost because the rows generated from input activity is being filtered out
  155. stat_type timeAvgLocalExecute = inputActivity->getStatRaw(StTimeLocalExecute, StAvgX);
  156. stat_type cost = statPercentageOf(timeAvgLocalExecute, preFilteredPer);
  157. result.set(ANA_KJ_EXCESS_PREFILTER_ID, cost, "Large number of rows from left dataset rejected in keyed join");
  158. updateInformation(result, activity);
  159. return true;
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. };
  166. void gatherRules(CIArrayOf<AActivityRule> & rules)
  167. {
  168. rules.append(*new DistributeSkewRule);
  169. rules.append(*new IoSkewRule(StTimeDiskReadIO, "disk read"));
  170. rules.append(*new IoSkewRule(StTimeDiskWriteIO, "disk write"));
  171. rules.append(*new IoSkewRule(StTimeSpillElapsed, "spill"));
  172. rules.append(*new KeyedJoinExcessRejectedRowsRule);
  173. }