2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_JIT_INCREF_PROFILE_H_
18 #define incl_HPHP_JIT_INCREF_PROFILE_H_
20 #include <folly/Format.h>
21 #include <folly/Optional.h>
23 #include "hphp/runtime/vm/jit/target-profile.h"
25 namespace HPHP
{ namespace jit
{
27 ///////////////////////////////////////////////////////////////////////////////
30 * Profile the frequency of the 3 possible different behaviors for an IncRef
31 * instruction. Each execution must fall into exactly one of these categories:
34 * the type was uncounted
36 * the type was refcounted and the value was persistent (so no inc happened)
38 * the count for the value was incremented.
41 struct IncRefProfile
{
42 uint16_t uncounted() const {
43 return total
- refcounted
;
46 uint16_t persistent() const {
47 return refcounted
- incremented
;
50 float percent(uint16_t value
) const {
51 return total
? 100.0 * value
/ total
: 0.0;
54 std::string
toString() const {
55 return folly::sformat(
56 "total: {:4}\n uncounted: {:4} ({:.1f}%),\n persistent: {:4} ({:.1f}%),\n"
57 " incremented: {:4} ({:.1f}%)",
59 uncounted(), percent(uncounted()),
60 persistent(), percent(persistent()),
61 incremented
, percent(incremented
)
65 // overflow handling isn't statistically correct; but its better
66 // than overflowing, and we're expecting threads to all have similar
68 static void reduce(IncRefProfile
& a
, const IncRefProfile
& b
) {
69 auto const total
= static_cast<uint32_t>(a
.total
+ b
.total
);
70 if (total
> std::numeric_limits
<uint16_t>::max()) {
71 auto scale
= [&] (uint16_t& x
, uint64_t y
) {
72 x
= (x
+ y
) * std::numeric_limits
<uint16_t>::max() / total
;
74 a
.total
= std::numeric_limits
<uint16_t>::max();
75 scale(a
.refcounted
, b
.refcounted
);
76 scale(a
.incremented
, b
.incremented
);
79 a
.refcounted
+= b
.refcounted
;
80 a
.incremented
+= b
.incremented
;
85 * The total number of times this IncRef was executed.
89 * The number of times this IncRef made it past the refcounted check.
93 * The number of times this IncRef actually incremented the refcount.
98 ///////////////////////////////////////////////////////////////////////////////