2020-02-02 19:26:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-02 19:26:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Kernel/KBuffer.h>
|
|
|
|
#include <Kernel/KResult.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-01-11 08:52:18 +00:00
|
|
|
class KBufferBuilder;
|
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
struct [[gnu::packed]] MallocPerformanceEvent {
|
2020-02-02 19:26:27 +00:00
|
|
|
size_t size;
|
2020-03-08 09:36:51 +00:00
|
|
|
FlatPtr ptr;
|
2020-02-02 19:26:27 +00:00
|
|
|
};
|
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
struct [[gnu::packed]] FreePerformanceEvent {
|
2020-02-02 19:26:27 +00:00
|
|
|
size_t size;
|
2020-03-08 09:36:51 +00:00
|
|
|
FlatPtr ptr;
|
2020-02-02 19:26:27 +00:00
|
|
|
};
|
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
struct [[gnu::packed]] PerformanceEvent {
|
2020-02-02 19:26:27 +00:00
|
|
|
u8 type { 0 };
|
|
|
|
u8 stack_size { 0 };
|
2021-01-11 08:52:18 +00:00
|
|
|
u32 tid { 0 };
|
2020-02-02 19:26:27 +00:00
|
|
|
u64 timestamp;
|
|
|
|
union {
|
|
|
|
MallocPerformanceEvent malloc;
|
|
|
|
FreePerformanceEvent free;
|
|
|
|
} data;
|
2021-04-25 11:41:21 +00:00
|
|
|
static constexpr size_t max_stack_frame_count = 64;
|
2021-01-11 08:52:18 +00:00
|
|
|
FlatPtr stack[max_stack_frame_count];
|
2020-02-02 19:26:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PerformanceEventBuffer {
|
|
|
|
public:
|
2021-03-02 15:55:54 +00:00
|
|
|
static OwnPtr<PerformanceEventBuffer> try_create_with_size(size_t buffer_size);
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2020-03-08 09:36:51 +00:00
|
|
|
KResult append(int type, FlatPtr arg1, FlatPtr arg2);
|
2021-01-17 09:53:18 +00:00
|
|
|
KResult append_with_eip_and_ebp(u32 eip, u32 ebp, int type, FlatPtr arg1, FlatPtr arg2);
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2021-01-11 08:52:18 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
size_t capacity() const { return m_buffer->size() / sizeof(PerformanceEvent); }
|
2020-02-02 19:26:27 +00:00
|
|
|
size_t count() const { return m_count; }
|
|
|
|
const PerformanceEvent& at(size_t index) const
|
|
|
|
{
|
|
|
|
return const_cast<PerformanceEventBuffer&>(*this).at(index);
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:01:02 +00:00
|
|
|
bool to_json(KBufferBuilder&) const;
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2021-03-02 18:01:02 +00:00
|
|
|
void add_process(const Process&);
|
2021-03-02 16:19:35 +00:00
|
|
|
|
2020-02-02 19:26:27 +00:00
|
|
|
private:
|
2021-03-02 15:55:54 +00:00
|
|
|
explicit PerformanceEventBuffer(NonnullOwnPtr<KBuffer>);
|
|
|
|
|
2021-03-02 18:01:02 +00:00
|
|
|
struct SampledProcess {
|
|
|
|
ProcessID pid;
|
|
|
|
String executable;
|
|
|
|
HashTable<ThreadID> threads;
|
|
|
|
|
|
|
|
struct Region {
|
|
|
|
String name;
|
|
|
|
Range range;
|
|
|
|
};
|
|
|
|
Vector<Region> regions;
|
|
|
|
};
|
|
|
|
|
2021-03-02 16:19:35 +00:00
|
|
|
template<typename Serializer>
|
|
|
|
bool to_json_impl(Serializer&) const;
|
|
|
|
|
2020-02-02 19:26:27 +00:00
|
|
|
PerformanceEvent& at(size_t index);
|
|
|
|
|
|
|
|
size_t m_count { 0 };
|
2021-03-02 15:55:54 +00:00
|
|
|
NonnullOwnPtr<KBuffer> m_buffer;
|
2021-03-02 18:01:02 +00:00
|
|
|
|
|
|
|
HashMap<ProcessID, NonnullOwnPtr<SampledProcess>> m_processes;
|
2020-02-02 19:26:27 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|