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