Rename RefcountProfile to IncRefProfile
[hiphop-php.git] / hphp / runtime / vm / jit / incref-profile.h
blob22ade7f0ab7181bc05239aabd149cea4350b4a77
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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:
33 * 1) Uncounted:
34 * the type was uncounted
35 * 2) Persistent:
36 * the type was refcounted and the value was persistent (so no inc happened)
37 * 3) Incremented:
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}%)",
58 total,
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
67 // distributions.
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);
77 } else {
78 a.total += b.total;
79 a.refcounted += b.refcounted;
80 a.incremented += b.incremented;
85 * The total number of times this IncRef was executed.
87 uint16_t total;
89 * The number of times this IncRef made it past the refcounted check.
91 uint16_t refcounted;
93 * The number of times this IncRef actually incremented the refcount.
95 uint16_t incremented;
98 ///////////////////////////////////////////////////////////////////////////////
102 #endif