You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
11 KiB

3 months ago
  1. #pragma once
  2. /*
  3. tests/constructor_stats.h -- framework for printing and tracking object
  4. instance lifetimes in example/test code.
  5. Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca>
  6. All rights reserved. Use of this source code is governed by a
  7. BSD-style license that can be found in the LICENSE file.
  8. This header provides a few useful tools for writing examples or tests that want to check and/or
  9. display object instance lifetimes. It requires that you include this header and add the following
  10. function calls to constructors:
  11. class MyClass {
  12. MyClass() { ...; print_default_created(this); }
  13. ~MyClass() { ...; print_destroyed(this); }
  14. MyClass(const MyClass &c) { ...; print_copy_created(this); }
  15. MyClass(MyClass &&c) { ...; print_move_created(this); }
  16. MyClass(int a, int b) { ...; print_created(this, a, b); }
  17. MyClass &operator=(const MyClass &c) { ...; print_copy_assigned(this); }
  18. MyClass &operator=(MyClass &&c) { ...; print_move_assigned(this); }
  19. ...
  20. }
  21. You can find various examples of these in several of the existing testing .cpp files. (Of course
  22. you don't need to add any of the above constructors/operators that you don't actually have, except
  23. for the destructor).
  24. Each of these will print an appropriate message such as:
  25. ### MyClass @ 0x2801910 created via default constructor
  26. ### MyClass @ 0x27fa780 created 100 200
  27. ### MyClass @ 0x2801910 destroyed
  28. ### MyClass @ 0x27fa780 destroyed
  29. You can also include extra arguments (such as the 100, 200 in the output above, coming from the
  30. value constructor) for all of the above methods which will be included in the output.
  31. For testing, each of these also keeps track the created instances and allows you to check how many
  32. of the various constructors have been invoked from the Python side via code such as:
  33. from pybind11_tests import ConstructorStats
  34. cstats = ConstructorStats.get(MyClass)
  35. print(cstats.alive())
  36. print(cstats.default_constructions)
  37. Note that `.alive()` should usually be the first thing you call as it invokes Python's garbage
  38. collector to actually destroy objects that aren't yet referenced.
  39. For everything except copy and move constructors and destructors, any extra values given to the
  40. print_...() function is stored in a class-specific values list which you can retrieve and inspect
  41. from the ConstructorStats instance `.values()` method.
  42. In some cases, when you need to track instances of a C++ class not registered with pybind11, you
  43. need to add a function returning the ConstructorStats for the C++ class; this can be done with:
  44. m.def("get_special_cstats", &ConstructorStats::get<SpecialClass>, py::return_value_policy::reference)
  45. Finally, you can suppress the output messages, but keep the constructor tracking (for
  46. inspection/testing in python) by using the functions with `print_` replaced with `track_` (e.g.
  47. `track_copy_created(this)`).
  48. */
  49. #include "pybind11_tests.h"
  50. #include <unordered_map>
  51. #include <list>
  52. #include <typeindex>
  53. #include <sstream>
  54. class ConstructorStats {
  55. protected:
  56. std::unordered_map<void*, int> _instances; // Need a map rather than set because members can shared address with parents
  57. std::list<std::string> _values; // Used to track values (e.g. of value constructors)
  58. public:
  59. int default_constructions = 0;
  60. int copy_constructions = 0;
  61. int move_constructions = 0;
  62. int copy_assignments = 0;
  63. int move_assignments = 0;
  64. void copy_created(void *inst) {
  65. created(inst);
  66. copy_constructions++;
  67. }
  68. void move_created(void *inst) {
  69. created(inst);
  70. move_constructions++;
  71. }
  72. void default_created(void *inst) {
  73. created(inst);
  74. default_constructions++;
  75. }
  76. void created(void *inst) {
  77. ++_instances[inst];
  78. }
  79. void destroyed(void *inst) {
  80. if (--_instances[inst] < 0)
  81. throw std::runtime_error("cstats.destroyed() called with unknown "
  82. "instance; potential double-destruction "
  83. "or a missing cstats.created()");
  84. }
  85. static void gc() {
  86. // Force garbage collection to ensure any pending destructors are invoked:
  87. #if defined(PYPY_VERSION)
  88. PyObject *globals = PyEval_GetGlobals();
  89. PyObject *result = PyRun_String(
  90. "import gc\n"
  91. "for i in range(2):"
  92. " gc.collect()\n",
  93. Py_file_input, globals, globals);
  94. if (result == nullptr)
  95. throw py::error_already_set();
  96. Py_DECREF(result);
  97. #else
  98. py::module::import("gc").attr("collect")();
  99. #endif
  100. }
  101. int alive() {
  102. gc();
  103. int total = 0;
  104. for (const auto &p : _instances)
  105. if (p.second > 0)
  106. total += p.second;
  107. return total;
  108. }
  109. void value() {} // Recursion terminator
  110. // Takes one or more values, converts them to strings, then stores them.
  111. template <typename T, typename... Tmore> void value(const T &v, Tmore &&...args) {
  112. std::ostringstream oss;
  113. oss << v;
  114. _values.push_back(oss.str());
  115. value(std::forward<Tmore>(args)...);
  116. }
  117. // Move out stored values
  118. py::list values() {
  119. py::list l;
  120. for (const auto &v : _values) l.append(py::cast(v));
  121. _values.clear();
  122. return l;
  123. }
  124. // Gets constructor stats from a C++ type index
  125. static ConstructorStats& get(std::type_index type) {
  126. static std::unordered_map<std::type_index, ConstructorStats> all_cstats;
  127. return all_cstats[type];
  128. }
  129. // Gets constructor stats from a C++ type
  130. template <typename T> static ConstructorStats& get() {
  131. #if defined(PYPY_VERSION)
  132. gc();
  133. #endif
  134. return get(typeid(T));
  135. }
  136. // Gets constructor stats from a Python class
  137. static ConstructorStats& get(py::object class_) {
  138. auto &internals = py::detail::get_internals();
  139. const std::type_index *t1 = nullptr, *t2 = nullptr;
  140. try {
  141. auto *type_info = internals.registered_types_py.at((PyTypeObject *) class_.ptr()).at(0);
  142. for (auto &p : internals.registered_types_cpp) {
  143. if (p.second == type_info) {
  144. if (t1) {
  145. t2 = &p.first;
  146. break;
  147. }
  148. t1 = &p.first;
  149. }
  150. }
  151. }
  152. catch (std::out_of_range) {}
  153. if (!t1) throw std::runtime_error("Unknown class passed to ConstructorStats::get()");
  154. auto &cs1 = get(*t1);
  155. // If we have both a t1 and t2 match, one is probably the trampoline class; return whichever
  156. // has more constructions (typically one or the other will be 0)
  157. if (t2) {
  158. auto &cs2 = get(*t2);
  159. int cs1_total = cs1.default_constructions + cs1.copy_constructions + cs1.move_constructions + (int) cs1._values.size();
  160. int cs2_total = cs2.default_constructions + cs2.copy_constructions + cs2.move_constructions + (int) cs2._values.size();
  161. if (cs2_total > cs1_total) return cs2;
  162. }
  163. return cs1;
  164. }
  165. };
  166. // To track construction/destruction, you need to call these methods from the various
  167. // constructors/operators. The ones that take extra values record the given values in the
  168. // constructor stats values for later inspection.
  169. template <class T> void track_copy_created(T *inst) { ConstructorStats::get<T>().copy_created(inst); }
  170. template <class T> void track_move_created(T *inst) { ConstructorStats::get<T>().move_created(inst); }
  171. template <class T, typename... Values> void track_copy_assigned(T *, Values &&...values) {
  172. auto &cst = ConstructorStats::get<T>();
  173. cst.copy_assignments++;
  174. cst.value(std::forward<Values>(values)...);
  175. }
  176. template <class T, typename... Values> void track_move_assigned(T *, Values &&...values) {
  177. auto &cst = ConstructorStats::get<T>();
  178. cst.move_assignments++;
  179. cst.value(std::forward<Values>(values)...);
  180. }
  181. template <class T, typename... Values> void track_default_created(T *inst, Values &&...values) {
  182. auto &cst = ConstructorStats::get<T>();
  183. cst.default_created(inst);
  184. cst.value(std::forward<Values>(values)...);
  185. }
  186. template <class T, typename... Values> void track_created(T *inst, Values &&...values) {
  187. auto &cst = ConstructorStats::get<T>();
  188. cst.created(inst);
  189. cst.value(std::forward<Values>(values)...);
  190. }
  191. template <class T, typename... Values> void track_destroyed(T *inst) {
  192. ConstructorStats::get<T>().destroyed(inst);
  193. }
  194. template <class T, typename... Values> void track_values(T *, Values &&...values) {
  195. ConstructorStats::get<T>().value(std::forward<Values>(values)...);
  196. }
  197. /// Don't cast pointers to Python, print them as strings
  198. inline const char *format_ptrs(const char *p) { return p; }
  199. template <typename T>
  200. py::str format_ptrs(T *p) { return "{:#x}"_s.format(reinterpret_cast<std::uintptr_t>(p)); }
  201. template <typename T>
  202. auto format_ptrs(T &&x) -> decltype(std::forward<T>(x)) { return std::forward<T>(x); }
  203. template <class T, typename... Output>
  204. void print_constr_details(T *inst, const std::string &action, Output &&...output) {
  205. py::print("###", py::type_id<T>(), "@", format_ptrs(inst), action,
  206. format_ptrs(std::forward<Output>(output))...);
  207. }
  208. // Verbose versions of the above:
  209. template <class T, typename... Values> void print_copy_created(T *inst, Values &&...values) { // NB: this prints, but doesn't store, given values
  210. print_constr_details(inst, "created via copy constructor", values...);
  211. track_copy_created(inst);
  212. }
  213. template <class T, typename... Values> void print_move_created(T *inst, Values &&...values) { // NB: this prints, but doesn't store, given values
  214. print_constr_details(inst, "created via move constructor", values...);
  215. track_move_created(inst);
  216. }
  217. template <class T, typename... Values> void print_copy_assigned(T *inst, Values &&...values) {
  218. print_constr_details(inst, "assigned via copy assignment", values...);
  219. track_copy_assigned(inst, values...);
  220. }
  221. template <class T, typename... Values> void print_move_assigned(T *inst, Values &&...values) {
  222. print_constr_details(inst, "assigned via move assignment", values...);
  223. track_move_assigned(inst, values...);
  224. }
  225. template <class T, typename... Values> void print_default_created(T *inst, Values &&...values) {
  226. print_constr_details(inst, "created via default constructor", values...);
  227. track_default_created(inst, values...);
  228. }
  229. template <class T, typename... Values> void print_created(T *inst, Values &&...values) {
  230. print_constr_details(inst, "created", values...);
  231. track_created(inst, values...);
  232. }
  233. template <class T, typename... Values> void print_destroyed(T *inst, Values &&...values) { // Prints but doesn't store given values
  234. print_constr_details(inst, "destroyed", values...);
  235. track_destroyed(inst);
  236. }
  237. template <class T, typename... Values> void print_values(T *inst, Values &&...values) {
  238. print_constr_details(inst, ":", values...);
  239. track_values(inst, values...);
  240. }