Serenity Operating System
at portability 109 lines 3.1 kB view raw
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 Process* s_process; 44static u32 s_pid; 45 46String& executable_path() 47{ 48 static String* path; 49 if (!path) 50 path = new String; 51 return *path; 52} 53 54u32 pid() 55{ 56 return s_pid; 57} 58 59void start(Process& process) 60{ 61 s_process = &process; 62 63 executable_path() = process.executable()->absolute_path().impl(); 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 s_process = nullptr; 91} 92 93void did_exec(const String& new_executable_path) 94{ 95 executable_path() = new_executable_path; 96 s_next_slot_index = 0; 97} 98 99void for_each_sample(Function<void(Sample&)> callback) 100{ 101 for (size_t i = 0; i < s_next_slot_index; ++i) { 102 auto& sample = sample_slot(i); 103 callback(sample); 104 } 105} 106 107} 108 109}