reg_search 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #! /bin/bash
  2. ########################################################################
  3. #
  4. # File: reg_search
  5. # Author: Janis Johnson <janis187@us.ibm.com>
  6. # Date: 2002/12/15
  7. #
  8. # Search for a small time interval within a range of dates in which
  9. # results for a test changed, using a binary search. The functionality
  10. # for getting sources, building the component to test, and running the
  11. # test are in other scripts that are run from here. Before the search
  12. # begins, we verify that we get the expected behavior for the first and
  13. # last dates.
  14. #
  15. # Define these in a file whose name is the argument to this script:
  16. # LOW_DATE: Date string recognized by the date command (local time).
  17. # HIGH_DATE: Date string recognized by the date command (local time).
  18. # REG_UPDATE: Pathname of script to update your source tree; returns
  19. # zero for success, nonzero for failure.
  20. # REG_BUILD: Pathname of script to build enough of the product to run
  21. # the test; returns zero for success, nonzero for failure.
  22. # REG_TEST: Pathname of script to run the test; returns 1 if we
  23. # should search later dates, 0 if we should search earlier
  24. # dates.
  25. # Optional:
  26. # DELTA: Search to an interval within this many seconds; default
  27. # is one hour (although 300 works well).
  28. # REG_FINISH Pathname of script to call at the end with the two final
  29. # dates as arguments.
  30. # SKIP_LOW If 1, skip verifying the low date of the range;
  31. # define this only if you're restarting and have already
  32. # tested the low date.
  33. # SKIP_HIGH If 1, skip verifying the high date of the range;
  34. # define this only if you're restarting and have already
  35. # tested the high date.
  36. # FIRST_MID Use this as the first midpoint, to avoid a midpoint that
  37. # is known not to build.
  38. # HAS_CHANGES Pathname of script to report whether the current date has
  39. # no differences from one of the ends of the current range
  40. # to skip unnecessary build and testing; default is "true".
  41. # VERBOSITY Default is 0, to print only errors and final message.
  42. # DATE_IN_MSG If set to anything but 0, include the time and date in
  43. # messages.
  44. #
  45. #
  46. #
  47. # Copyright (c) 2002, 2003, 2005, 2009, 2010 Free Software Foundation, Inc.
  48. #
  49. # This file is free software; you can redistribute it and/or modify
  50. # it under the terms of the GNU General Public License as published by
  51. # the Free Software Foundation; either version 3 of the License, or
  52. # (at your option) any later version.
  53. #
  54. # This program is distributed in the hope that it will be useful,
  55. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  57. # GNU General Public License for more details.
  58. #
  59. # You should have received a copy of the GNU General Public License
  60. # along with this program; see the file COPYING3. If not see
  61. # <http://www.gnu.org/licenses/>.
  62. #
  63. ########################################################################
  64. ########################################################################
  65. # Functions
  66. ########################################################################
  67. # Issue a message if its verbosity level is high enough.
  68. msg() {
  69. test ${1} -gt ${VERBOSITY} && return
  70. if [ "x${DATE_IN_MSG}" = "x" ]; then
  71. echo "${2}"
  72. else
  73. echo "`${DATE}` ${2}"
  74. fi
  75. }
  76. # Issue an error message and exit with a non-zero status. If there
  77. # is a valid current range whose end points have been tested, report
  78. # it so the user can start again from there.
  79. error() {
  80. msg 0 "error: ${1}"
  81. test ${VALID_RANGE} -eq 1 && \
  82. echo "current range:"
  83. echo "LOW_DATE=\"${LATER_THAN}\""
  84. echo "HIGH_DATE=\"${EARLIER_THAN}\""
  85. exit 1
  86. }
  87. # Turn seconds since the epoch into a date we can use with source
  88. # control tools and report to the user.
  89. make_date() {
  90. MADE_DATE=`${DATE} -u +"%Y-%m-%d %H:%M %Z" --date "1970-01-01 ${1} seconds"` \
  91. || error "make_date: date command failed"
  92. }
  93. # Build the components to test using sources as of a particular date and
  94. # run a test case. Pass each of the scripts the date that we're
  95. # testing; the first one needs it, the others can ignore it if they want.
  96. process_date() {
  97. TEST_DATE="${1}"
  98. ${REG_UPDATE} "${TEST_DATE}" || error "source update failed for ${TEST_DATE}"
  99. # If we're already in a valid range, skip this date if there are no
  100. # differences from either end of the range and adjust LATER.
  101. if [ ${VALID_RANGE} = 1 ]; then
  102. ${HAS_CHANGES} "${TEST_DATE}" "${LATER_THAN}" "${EARLIER_THAN}"
  103. RET=$?
  104. case ${RET} in
  105. 0) ;;
  106. 1) LATER=1; return;;
  107. 2) LATER=0; return;;
  108. *) error "process_date: unexpected return value from ${HAS_CHANGES}";;
  109. esac
  110. fi
  111. ${REG_BUILD} "${TEST_DATE}" || error "build failed for ${TEST_DATE}"
  112. ${REG_TEST} "${TEST_DATE}"
  113. LATER=$?
  114. }
  115. # Perform a binary search on dates within the range specified by
  116. # the arguments, bounded by the number of seconds in DELTA.
  117. search_dates() {
  118. let LOW=$1
  119. let HIGH=$2
  120. let DIFF=HIGH-LOW
  121. # Get the date in the middle of the range; MID is in seconds since
  122. # the epoch, DATE is readable by humans and tools. The user can
  123. # override the initial mid date if it is known to have problems,
  124. # e.g., if a build fails for that date.
  125. if [ ${FIRST_MID} -ne 0 ]; then
  126. let MID=${FIRST_MID}
  127. else
  128. let MID=LOW/2+HIGH/2
  129. fi
  130. while [ ${DIFF} -ge ${DELTA} ]; do
  131. make_date ${MID}
  132. TEST_DATE="${MADE_DATE}"
  133. # Test it.
  134. process_date "${TEST_DATE}"
  135. # Narrow the search based on the outcome of testing DATE.
  136. if [ ${LATER} -eq 1 ]; then
  137. msg 1 "search dates later than \"${TEST_DATE}\""
  138. LATER_THAN="${TEST_DATE}"
  139. let LOW=MID
  140. else
  141. msg 1 "search dates earlier than \"${TEST_DATE}\""
  142. EARLIER_THAN="${TEST_DATE}"
  143. let HIGH=MID
  144. fi
  145. let DIFF=HIGH-LOW
  146. let MID=LOW/2+HIGH/2
  147. done
  148. }
  149. ########################################################################
  150. # Main program (so to speak)
  151. ########################################################################
  152. # If DATE isn't defined, use the default date command; the configuration
  153. # file can override this.
  154. if [ "x${DATE}" = "x" ]; then
  155. DATE=date
  156. fi
  157. # The error function uses this.
  158. VALID_RANGE=0
  159. # Process the configuration file.
  160. if [ $# != 1 ]; then
  161. echo Usage: $0 config_file
  162. exit 1
  163. fi
  164. CONFIG=${1}
  165. if [ ! -f ${CONFIG} ]; then
  166. error "configuration file ${CONFIG} does not exist"
  167. fi
  168. # OK, the config file exists. Source it, make sure required parameters
  169. # are defined and their files exist, and give default values to optional
  170. # parameters.
  171. . ${CONFIG}
  172. test "x${REG_UPDATE}" = "x" && error "REG_UPDATE is not defined"
  173. test "x${REG_BUILD}" = "x" && error "REG_BUILD is not defined"
  174. test "x${REG_TEST}" = "x" && error "REG_TEST is not defined"
  175. test -x ${REG_TEST} || error "REG_TEST is not an executable file"
  176. test "x${SKIP_LOW}" = "x" && SKIP_LOW=0
  177. test "x${SKIP_HIGH}" = "x" && SKIP_HIGH=0
  178. test "x${DELTA}" = "x" && DELTA=3600
  179. test "x${VERBOSITY}" = "x" && VERBOSITY=0
  180. test "x${HAS_CHANGES}" = "x" && HAS_CHANGES=true
  181. test "x${REG_FINISH}" = "x" && REG_FINISH=true
  182. msg 2 "LOW_DATE = ${LOW_DATE}"
  183. msg 2 "HIGH_DATE = ${HIGH_DATE}"
  184. msg 2 "REG_UPDATE = ${REG_UPDATE}"
  185. msg 2 "REG_BUILD = ${REG_BUILD}"
  186. msg 2 "REG_TEST = ${REG_TEST}"
  187. msg 2 "SKIP_LOW = ${SKIP_LOW}"
  188. msg 2 "SKIP_HIGH = ${SKIP_HIGH}"
  189. msg 2 "FIRST_MID = ${FIRST_MID}"
  190. msg 2 "VERBOSITY = ${VERBOSITY}"
  191. msg 2 "DELTA = ${DELTA}"
  192. # Verify that DELTA is at least two minutes.
  193. test ${DELTA} -lt 120 && \
  194. error "DELTA is ${DELTA}, must be at least 120 (two minutes)"
  195. # Change the dates into seconds since the epoch. This uses an extension
  196. # in GNU date.
  197. LOW_DATE=`${DATE} +%s --date "${LOW_DATE}"` || \
  198. error "date command failed for \"${LOW_DATE}\""
  199. HIGH_DATE=`${DATE} +%s --date "${HIGH_DATE}"` || \
  200. error "date command failed for \"${LOW_DATE}\""
  201. # If FIRST_MID was defined, convert it and make sure it's in the range.
  202. if [ "x${FIRST_MID}" != "x" ]; then
  203. FIRST_MID=`${DATE} +%s --date "${FIRST_MID}"` || \
  204. error "date command failed for \"${FIRST_MID}\""
  205. test ${FIRST_MID} -le ${LOW_DATE} && \
  206. error "FIRST_MID date is earlier than LOW_DATE"
  207. test ${FIRST_MID} -ge ${HIGH_DATE} && \
  208. error "FIRST_MID is later than HIGH_DATE"
  209. else
  210. FIRST_MID=0
  211. fi
  212. # Keep track of the bounds of the range where the test behavior changes,
  213. # using a human-readable version of each date.
  214. make_date ${LOW_DATE}
  215. LATER_THAN="${MADE_DATE}"
  216. make_date ${HIGH_DATE}
  217. EARLIER_THAN="${MADE_DATE}"
  218. msg 2 "LATER_THAN = ${LATER_THAN}"
  219. msg 2 "EARLIER_THAN = ${EARLIER_THAN}"
  220. # Verify that the range isn't backwards.
  221. test ${LOW_DATE} -lt ${HIGH_DATE} || error "date range is backwards"
  222. # Verify that the first and last date in the range get the results we
  223. # expect. If not, quit, because any of several things could be wrong.
  224. if [ ${SKIP_LOW} -eq 0 ]; then
  225. process_date "${LATER_THAN}"
  226. test ${LATER} -ne 1 && \
  227. error "unexpected result for low date ${LATER_THAN}"
  228. msg 1 "result for low date is as expected"
  229. fi
  230. if [ ${SKIP_HIGH} -eq 0 ]; then
  231. process_date "${EARLIER_THAN}"
  232. test ${LATER} -ne 0 && \
  233. error "unexpected result for high date ${EARLIER_THAN}"
  234. msg 1 "result for high date is as expected"
  235. fi
  236. # Search within the range, now that we know that the end points are valid.
  237. VALID_RANGE=1
  238. search_dates ${LOW_DATE} ${HIGH_DATE}
  239. # Report the range that's left to investigate.
  240. echo "Continue search between ${LATER_THAN} and ${EARLIER_THAN}"
  241. # Invoke the optional script to report additional information about
  242. # changes between the two dates.
  243. ${REG_FINISH} "${LATER_THAN}" "${EARLIER_THAN}"