1/*
2 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "JSCast.h"
29#include "Structure.h"
30#include <wtf/MonotonicTime.h>
31#include <wtf/StackTrace.h>
32
33namespace JSC {
34
35struct CellProfile {
36 enum Liveness {
37 Unknown,
38 Dead,
39 Live
40 };
41
42 CellProfile(HeapCell* cell, HeapCell::Kind kind, Liveness liveness)
43 : m_cell(cell)
44 , m_kind(kind)
45 , m_liveness(liveness)
46 , m_timestamp(MonotonicTime::now())
47 {
48 if (isJSCellKind(m_kind) && m_liveness != Dead)
49 m_className = jsCell()->structure()->classInfo()->className;
50 }
51
52 CellProfile(CellProfile&& other)
53 : m_cell(other.m_cell)
54 , m_kind(other.m_kind)
55 , m_liveness(other.m_liveness)
56 , m_timestamp(other.m_timestamp)
57 , m_className(other.m_className)
58 , m_stackTrace(WTFMove(other.m_stackTrace))
59 { }
60
61 HeapCell* cell() const { return m_cell; }
62 JSCell* jsCell() const
63 {
64 ASSERT(isJSCell());
65 return static_cast<JSCell*>(m_cell);
66 }
67
68 bool isJSCell() const { return isJSCellKind(m_kind); }
69
70 HeapCell::Kind kind() const { return m_kind; }
71
72 bool isLive() const { return m_liveness == Live; }
73 bool isDead() const { return m_liveness == Dead; }
74
75 void setIsLive() { m_liveness = Live; }
76 void setIsDead() { m_liveness = Dead; }
77
78 MonotonicTime timestamp() const { return m_timestamp; }
79
80 const char* className() const { return m_className; }
81
82 StackTrace* stackTrace() const { return m_stackTrace.get(); }
83 void setStackTrace(StackTrace* trace) { m_stackTrace = std::unique_ptr<StackTrace>(trace); }
84
85private:
86 HeapCell* m_cell;
87 HeapCell::Kind m_kind;
88 Liveness m_liveness { Unknown };
89 MonotonicTime m_timestamp;
90 const char* m_className { nullptr };
91 std::unique_ptr<StackTrace> m_stackTrace;
92};
93
94} // namespace JSC
95