Serenity Operating System
1/*
2 * Copyright (c) 2018-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#include <AK/Demangle.h>
28#include <AK/StringBuilder.h>
29#include <Kernel/FileSystem/Custody.h>
30#include <Kernel/KBuffer.h>
31#include <Kernel/KSyms.h>
32#include <Kernel/Process.h>
33#include <Kernel/Profiling.h>
34#include <LibELF/ELFLoader.h>
35
36namespace Kernel {
37
38namespace Profiling {
39
40static KBufferImpl* s_profiling_buffer;
41static size_t s_slot_count;
42static size_t s_next_slot_index;
43static u32 s_pid;
44
45String& executable_path()
46{
47 static String* path;
48 if (!path)
49 path = new String;
50 return *path;
51}
52
53u32 pid()
54{
55 return s_pid;
56}
57
58void start(Process& process)
59{
60 if (process.executable())
61 executable_path() = process.executable()->absolute_path().impl();
62 else
63 executable_path() = {};
64 s_pid = process.pid();
65
66 if (!s_profiling_buffer) {
67 s_profiling_buffer = RefPtr<KBufferImpl>(KBuffer::create_with_size(8 * MB).impl()).leak_ref();
68 s_profiling_buffer->region().commit();
69 s_slot_count = s_profiling_buffer->size() / sizeof(Sample);
70 }
71
72 s_next_slot_index = 0;
73}
74
75static Sample& sample_slot(size_t index)
76{
77 return ((Sample*)s_profiling_buffer->data())[index];
78}
79
80Sample& next_sample_slot()
81{
82 auto& slot = sample_slot(s_next_slot_index++);
83 if (s_next_slot_index >= s_slot_count)
84 s_next_slot_index = 0;
85 return slot;
86}
87
88void stop()
89{
90}
91
92void did_exec(const String& new_executable_path)
93{
94 executable_path() = new_executable_path;
95 s_next_slot_index = 0;
96}
97
98void for_each_sample(Function<void(Sample&)> callback)
99{
100 for (size_t i = 0; i < s_next_slot_index; ++i) {
101 auto& sample = sample_slot(i);
102 callback(sample);
103 }
104}
105
106}
107
108}