retry.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* Copyright (C) 2008-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include "libitm_i.h"
  23. // The default TM method used when starting a new transaction. Initialized
  24. // in number_of_threads_changed() below.
  25. // Access to this variable is always synchronized with help of the serial
  26. // lock, except one read access that happens in decide_begin_dispatch() before
  27. // a transaction has become active (by acquiring the serial lock in read or
  28. // write mode). The default_dispatch is only changed and initialized in
  29. // serial mode. Transactions stay active when they restart (see beginend.cc),
  30. // thus decide_retry_strategy() can expect default_dispatch to be unmodified.
  31. // See decide_begin_dispatch() for further comments.
  32. static std::atomic<GTM::abi_dispatch*> default_dispatch;
  33. // The default TM method as requested by the user, if any.
  34. static GTM::abi_dispatch* default_dispatch_user = 0;
  35. void
  36. GTM::gtm_thread::decide_retry_strategy (gtm_restart_reason r)
  37. {
  38. struct abi_dispatch *disp = abi_disp ();
  39. this->restart_reason[r]++;
  40. this->restart_total++;
  41. if (r == RESTART_INIT_METHOD_GROUP)
  42. {
  43. // A re-initializations of the method group has been requested. Switch
  44. // to serial mode, initialize, and resume normal operation.
  45. if ((state & STATE_SERIAL) == 0)
  46. {
  47. // We have to eventually re-init the method group. Therefore,
  48. // we cannot just upgrade to a write lock here because this could
  49. // fail forever when other transactions execute in serial mode.
  50. // However, giving up the read lock then means that a change of the
  51. // method group could happen in-between, so check that we're not
  52. // re-initializing without a need.
  53. // ??? Note that we can still re-initialize too often, but avoiding
  54. // that would increase code complexity, which seems unnecessary
  55. // given that re-inits should be very infrequent.
  56. serial_lock.read_unlock(this);
  57. serial_lock.write_lock();
  58. if (disp->get_method_group()
  59. == default_dispatch.load(memory_order_relaxed)
  60. ->get_method_group())
  61. // Still the same method group.
  62. disp->get_method_group()->reinit();
  63. serial_lock.write_unlock();
  64. // Also, we're making the transaction inactive, so when we become
  65. // active again, some other thread might have changed the default
  66. // dispatch, so we run the same code as for the first execution
  67. // attempt.
  68. disp = decide_begin_dispatch(prop);
  69. set_abi_disp(disp);
  70. }
  71. else
  72. // We are a serial transaction already, which makes things simple.
  73. disp->get_method_group()->reinit();
  74. return;
  75. }
  76. bool retry_irr = (r == RESTART_SERIAL_IRR);
  77. bool retry_serial = (retry_irr || this->restart_total > 100);
  78. // We assume closed nesting to be infrequently required, so just use
  79. // dispatch_serial (with undo logging) if required.
  80. if (r == RESTART_CLOSED_NESTING)
  81. retry_serial = true;
  82. if (retry_serial)
  83. {
  84. // In serialirr_mode we can succeed with the upgrade to
  85. // write-lock but fail the trycommit. In any case, if the
  86. // write lock is not yet held, grab it. Don't do this with
  87. // an upgrade, since we've no need to preserve the state we
  88. // acquired with the read.
  89. // Note that we will be restarting with either dispatch_serial or
  90. // dispatch_serialirr, which are compatible with all TM methods; if
  91. // we would retry with a different method, we would have to first check
  92. // whether the default dispatch or the method group have changed. Also,
  93. // the caller must have rolled back the previous transaction, so we
  94. // don't have to worry about things such as privatization.
  95. if ((this->state & STATE_SERIAL) == 0)
  96. {
  97. this->state |= STATE_SERIAL;
  98. serial_lock.read_unlock (this);
  99. serial_lock.write_lock ();
  100. }
  101. // We can retry with dispatch_serialirr if the transaction
  102. // doesn't contain an abort and if we don't need closed nesting.
  103. if ((this->prop & pr_hasNoAbort) && (r != RESTART_CLOSED_NESTING))
  104. retry_irr = true;
  105. }
  106. // Note that we can just use serial mode here without having to switch
  107. // TM method sets because serial mode is compatible with all of them.
  108. if (retry_irr)
  109. {
  110. this->state = (STATE_SERIAL | STATE_IRREVOCABLE);
  111. disp = dispatch_serialirr ();
  112. set_abi_disp (disp);
  113. }
  114. else if (retry_serial)
  115. {
  116. disp = dispatch_serial();
  117. set_abi_disp (disp);
  118. }
  119. }
  120. // Decides which TM method should be used on the first attempt to run this
  121. // transaction. Acquires the serial lock and sets transaction state
  122. // according to the chosen TM method.
  123. GTM::abi_dispatch*
  124. GTM::gtm_thread::decide_begin_dispatch (uint32_t prop)
  125. {
  126. abi_dispatch* dd;
  127. // TODO Pay more attention to prop flags (eg, *omitted) when selecting
  128. // dispatch.
  129. // ??? We go irrevocable eagerly here, which is not always good for
  130. // performance. Don't do this?
  131. if ((prop & pr_doesGoIrrevocable) || !(prop & pr_instrumentedCode))
  132. dd = dispatch_serialirr();
  133. else
  134. {
  135. // Load the default dispatch. We're not an active transaction and so it
  136. // can change concurrently but will still be some valid dispatch.
  137. // Relaxed memory order is okay because we expect each dispatch to be
  138. // constructed properly already (at least that its closed_nesting() and
  139. // closed_nesting_alternatives() will return sensible values). It is
  140. // harmless if we incorrectly chose the serial or serialirr methods, and
  141. // for all other methods we will acquire the serial lock in read mode
  142. // and load the default dispatch again.
  143. abi_dispatch* dd_orig = default_dispatch.load(memory_order_relaxed);
  144. dd = dd_orig;
  145. // If we might need closed nesting and the default dispatch has an
  146. // alternative that supports closed nesting, use it.
  147. // ??? We could choose another TM method that we know supports closed
  148. // nesting but isn't the default (e.g., dispatch_serial()). However, we
  149. // assume that aborts that need closed nesting are infrequent, so don't
  150. // choose a non-default method until we have to actually restart the
  151. // transaction.
  152. if (!(prop & pr_hasNoAbort) && !dd->closed_nesting()
  153. && dd->closed_nesting_alternative())
  154. dd = dd->closed_nesting_alternative();
  155. if (!(dd->requires_serial() & STATE_SERIAL))
  156. {
  157. // The current dispatch is supposedly a non-serial one. Become an
  158. // active transaction and verify this. Relaxed memory order is fine
  159. // because the serial lock itself will have established
  160. // happens-before for any change to the selected dispatch.
  161. serial_lock.read_lock (this);
  162. if (default_dispatch.load(memory_order_relaxed) == dd_orig)
  163. return dd;
  164. // If we raced with a concurrent modification of default_dispatch,
  165. // just fall back to serialirr. The dispatch choice might not be
  166. // up-to-date anymore, but this is harmless.
  167. serial_lock.read_unlock (this);
  168. dd = dispatch_serialirr();
  169. }
  170. }
  171. // We are some kind of serial transaction.
  172. serial_lock.write_lock();
  173. state = dd->requires_serial();
  174. return dd;
  175. }
  176. void
  177. GTM::gtm_thread::set_default_dispatch(GTM::abi_dispatch* disp)
  178. {
  179. abi_dispatch* dd = default_dispatch.load(memory_order_relaxed);
  180. if (dd == disp)
  181. return;
  182. if (dd)
  183. {
  184. // If we are switching method groups, initialize and shut down properly.
  185. if (dd->get_method_group() != disp->get_method_group())
  186. {
  187. dd->get_method_group()->fini();
  188. disp->get_method_group()->init();
  189. }
  190. }
  191. else
  192. disp->get_method_group()->init();
  193. default_dispatch.store(disp, memory_order_relaxed);
  194. }
  195. static GTM::abi_dispatch*
  196. parse_default_method()
  197. {
  198. const char *env = getenv("ITM_DEFAULT_METHOD");
  199. GTM::abi_dispatch* disp = 0;
  200. if (env == NULL)
  201. return 0;
  202. while (isspace((unsigned char) *env))
  203. ++env;
  204. if (strncmp(env, "serialirr_onwrite", 17) == 0)
  205. {
  206. disp = GTM::dispatch_serialirr_onwrite();
  207. env += 17;
  208. }
  209. else if (strncmp(env, "serialirr", 9) == 0)
  210. {
  211. disp = GTM::dispatch_serialirr();
  212. env += 9;
  213. }
  214. else if (strncmp(env, "serial", 6) == 0)
  215. {
  216. disp = GTM::dispatch_serial();
  217. env += 6;
  218. }
  219. else if (strncmp(env, "gl_wt", 5) == 0)
  220. {
  221. disp = GTM::dispatch_gl_wt();
  222. env += 5;
  223. }
  224. else if (strncmp(env, "ml_wt", 5) == 0)
  225. {
  226. disp = GTM::dispatch_ml_wt();
  227. env += 5;
  228. }
  229. else if (strncmp(env, "htm", 3) == 0)
  230. {
  231. disp = GTM::dispatch_htm();
  232. env += 3;
  233. }
  234. else
  235. goto unknown;
  236. while (isspace((unsigned char) *env))
  237. ++env;
  238. if (*env == '\0')
  239. return disp;
  240. unknown:
  241. GTM::GTM_error("Unknown TM method in environment variable "
  242. "ITM_DEFAULT_METHOD\n");
  243. return 0;
  244. }
  245. // Gets notifications when the number of registered threads changes. This is
  246. // used to initialize the method set choice and trigger straightforward choice
  247. // adaption.
  248. // This must be called only by serial threads.
  249. void
  250. GTM::gtm_thread::number_of_threads_changed(unsigned previous, unsigned now)
  251. {
  252. if (previous == 0)
  253. {
  254. // No registered threads before, so initialize.
  255. static bool initialized = false;
  256. if (!initialized)
  257. {
  258. initialized = true;
  259. // Check for user preferences here.
  260. default_dispatch = 0;
  261. default_dispatch_user = parse_default_method();
  262. }
  263. }
  264. else if (now == 0)
  265. {
  266. // No registered threads anymore. The dispatch based on serial mode do
  267. // not have any global state, so this effectively shuts down properly.
  268. set_default_dispatch(dispatch_serialirr());
  269. }
  270. if (now == 1)
  271. {
  272. // Only one thread, so use a serializing method.
  273. // ??? If we don't have a fast serial mode implementation, it might be
  274. // better to use the global lock method set here.
  275. if (default_dispatch_user && default_dispatch_user->supports(now))
  276. set_default_dispatch(default_dispatch_user);
  277. else
  278. set_default_dispatch(dispatch_serialirr());
  279. }
  280. else if (now > 1 && previous <= 1)
  281. {
  282. // More than one thread, use the default method.
  283. if (default_dispatch_user && default_dispatch_user->supports(now))
  284. set_default_dispatch(default_dispatch_user);
  285. else
  286. {
  287. // If HTM is available, use it by default with serial mode as
  288. // fallback. Otherwise, use ml_wt because it probably scales best.
  289. abi_dispatch* a;
  290. #ifdef USE_HTM_FASTPATH
  291. if (htm_available())
  292. a = dispatch_htm();
  293. else
  294. #endif
  295. a = dispatch_ml_wt();
  296. if (a->supports(now))
  297. set_default_dispatch(a);
  298. else
  299. // Serial-irrevocable mode always works.
  300. set_default_dispatch(dispatch_serialirr());
  301. }
  302. }
  303. }