Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <Kernel/KBuffer.h>
30#include <Kernel/KResult.h>
31
32namespace Kernel {
33
34struct [[gnu::packed]] MallocPerformanceEvent
35{
36 size_t size;
37 FlatPtr ptr;
38};
39
40struct [[gnu::packed]] FreePerformanceEvent
41{
42 size_t size;
43 FlatPtr ptr;
44};
45
46struct [[gnu::packed]] PerformanceEvent
47{
48 u8 type { 0 };
49 u8 stack_size { 0 };
50 u64 timestamp;
51 union {
52 MallocPerformanceEvent malloc;
53 FreePerformanceEvent free;
54 } data;
55 FlatPtr stack[32];
56};
57
58class PerformanceEventBuffer {
59public:
60 PerformanceEventBuffer();
61
62 KResult append(int type, FlatPtr arg1, FlatPtr arg2);
63
64 size_t capacity() const { return m_buffer.size() / sizeof(PerformanceEvent); }
65 size_t count() const { return m_count; }
66 const PerformanceEvent& at(size_t index) const
67 {
68 return const_cast<PerformanceEventBuffer&>(*this).at(index);
69 }
70
71 KBuffer to_json(pid_t, const String& executable_path) const;
72
73private:
74 PerformanceEvent& at(size_t index);
75
76 size_t m_count { 0 };
77 KBuffer m_buffer;
78};
79
80}