mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
shared: remove use of libshared
This moves everything into other places and simple includes the files in the cases that it is necessary. In the future, we can rewrite sysprofd to use GDBus and add GetProcFile() to allow for client-side processing of kallsyms.
This commit is contained in:
@ -1,19 +1,25 @@
|
||||
libsysprof_capture_headers = [
|
||||
'sp-address.h',
|
||||
'sp-clock.h',
|
||||
'sp-capture-condition.h',
|
||||
'sp-capture-cursor.h',
|
||||
'sp-capture-reader.h',
|
||||
'sp-capture-types.h',
|
||||
'sp-capture-writer.h',
|
||||
'sp-platform.h',
|
||||
'sysprof-capture.h',
|
||||
'sysprof-version-macros.h',
|
||||
]
|
||||
|
||||
libsysprof_capture_sources = [
|
||||
'sp-address.c',
|
||||
'sp-capture-condition.c',
|
||||
'sp-capture-cursor.c',
|
||||
'sp-capture-reader.c',
|
||||
'sp-capture-writer.c',
|
||||
'sp-capture-util.c',
|
||||
'sp-capture-writer.c',
|
||||
'sp-clock.c',
|
||||
'sp-platform.c',
|
||||
]
|
||||
|
||||
configure_file(
|
||||
@ -25,7 +31,6 @@ configure_file(
|
||||
|
||||
libsysprof_capture_deps = [
|
||||
dependency('glib-2.0', version: glib_req_version),
|
||||
libshared_dep,
|
||||
]
|
||||
|
||||
install_headers(libsysprof_capture_headers, subdir: sysprof_header_subdir)
|
||||
|
||||
6
src/libsysprof-capture/sp-address-fallback.h
Normal file
6
src/libsysprof-capture/sp-address-fallback.h
Normal file
@ -0,0 +1,6 @@
|
||||
#define PERF_CONTEXT_GUEST_USER (-2560)
|
||||
#define PERF_CONTEXT_GUEST_KERNEL (-2176)
|
||||
#define PERF_CONTEXT_GUEST (-2048)
|
||||
#define PERF_CONTEXT_USER (-512)
|
||||
#define PERF_CONTEXT_KERNEL (-128)
|
||||
#define PERF_CONTEXT_HV (-32)
|
||||
97
src/libsysprof-capture/sp-address.c
Normal file
97
src/libsysprof-capture/sp-address.c
Normal file
@ -0,0 +1,97 @@
|
||||
/* sp-address.c
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#ifdef __linux__
|
||||
# include <linux/perf_event.h>
|
||||
#else
|
||||
# include "sp-address-fallback.h"
|
||||
#endif
|
||||
|
||||
#include "sp-address.h"
|
||||
|
||||
gboolean
|
||||
sp_address_is_context_switch (SpAddress address,
|
||||
SpAddressContext *context)
|
||||
{
|
||||
SpAddressContext dummy;
|
||||
|
||||
if (context == NULL)
|
||||
context = &dummy;
|
||||
|
||||
switch (address)
|
||||
{
|
||||
case PERF_CONTEXT_HV:
|
||||
*context = SP_ADDRESS_CONTEXT_HYPERVISOR;
|
||||
return TRUE;
|
||||
|
||||
case PERF_CONTEXT_KERNEL:
|
||||
*context = SP_ADDRESS_CONTEXT_KERNEL;
|
||||
return TRUE;
|
||||
|
||||
case PERF_CONTEXT_USER:
|
||||
*context = SP_ADDRESS_CONTEXT_USER;
|
||||
return TRUE;
|
||||
|
||||
case PERF_CONTEXT_GUEST:
|
||||
*context = SP_ADDRESS_CONTEXT_GUEST;
|
||||
return TRUE;
|
||||
|
||||
case PERF_CONTEXT_GUEST_KERNEL:
|
||||
*context = SP_ADDRESS_CONTEXT_GUEST_KERNEL;
|
||||
return TRUE;
|
||||
|
||||
case PERF_CONTEXT_GUEST_USER:
|
||||
*context = SP_ADDRESS_CONTEXT_GUEST_USER;
|
||||
return TRUE;
|
||||
|
||||
default:
|
||||
*context = SP_ADDRESS_CONTEXT_NONE;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
const gchar *
|
||||
sp_address_context_to_string (SpAddressContext context)
|
||||
{
|
||||
switch (context)
|
||||
{
|
||||
case SP_ADDRESS_CONTEXT_HYPERVISOR:
|
||||
return "- - hypervisor - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_KERNEL:
|
||||
return "- - kernel - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_USER:
|
||||
return "- - user - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_GUEST:
|
||||
return "- - guest - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_GUEST_KERNEL:
|
||||
return "- - guest kernel - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_GUEST_USER:
|
||||
return "- - guest user - -";
|
||||
|
||||
case SP_ADDRESS_CONTEXT_NONE:
|
||||
default:
|
||||
return "- - unknown - -";
|
||||
}
|
||||
}
|
||||
60
src/libsysprof-capture/sp-address.h
Normal file
60
src/libsysprof-capture/sp-address.h
Normal file
@ -0,0 +1,60 @@
|
||||
/* sp-address.h
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sysprof-version-macros.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef guint64 SpAddress;
|
||||
|
||||
G_STATIC_ASSERT (sizeof (SpAddress) >= sizeof (gpointer));
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SP_ADDRESS_CONTEXT_NONE = 0,
|
||||
SP_ADDRESS_CONTEXT_HYPERVISOR,
|
||||
SP_ADDRESS_CONTEXT_KERNEL,
|
||||
SP_ADDRESS_CONTEXT_USER,
|
||||
SP_ADDRESS_CONTEXT_GUEST,
|
||||
SP_ADDRESS_CONTEXT_GUEST_KERNEL,
|
||||
SP_ADDRESS_CONTEXT_GUEST_USER,
|
||||
} SpAddressContext;
|
||||
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
gboolean sp_address_is_context_switch (SpAddress address,
|
||||
SpAddressContext *context);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
const gchar *sp_address_context_to_string (SpAddressContext context);
|
||||
|
||||
static inline gint
|
||||
sp_address_compare (SpAddress a,
|
||||
SpAddress b)
|
||||
{
|
||||
if (a < b)
|
||||
return -1;
|
||||
else if (a == b)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
G_END_DECLS
|
||||
54
src/libsysprof-capture/sp-clock.c
Normal file
54
src/libsysprof-capture/sp-clock.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* sp-clock.c
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "sp-clock.h"
|
||||
|
||||
gint sp_clock = -1;
|
||||
|
||||
void
|
||||
sp_clock_init (void)
|
||||
{
|
||||
static const gint clock_ids[] = {
|
||||
CLOCK_MONOTONIC,
|
||||
CLOCK_MONOTONIC_RAW,
|
||||
#ifdef __linux__
|
||||
CLOCK_MONOTONIC_COARSE,
|
||||
CLOCK_REALTIME_COARSE,
|
||||
#endif
|
||||
CLOCK_REALTIME,
|
||||
};
|
||||
|
||||
if (sp_clock != -1)
|
||||
return;
|
||||
|
||||
for (guint i = 0; i < G_N_ELEMENTS (clock_ids); i++)
|
||||
{
|
||||
struct timespec ts;
|
||||
int clock_id = clock_ids [i];
|
||||
|
||||
if (0 == clock_gettime (clock_id, &ts))
|
||||
{
|
||||
sp_clock = clock_id;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
g_assert_not_reached ();
|
||||
}
|
||||
59
src/libsysprof-capture/sp-clock.h
Normal file
59
src/libsysprof-capture/sp-clock.h
Normal file
@ -0,0 +1,59 @@
|
||||
/* sp-clock.h
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "sysprof-version-macros.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef gint SpClock;
|
||||
typedef gint64 SpTimeStamp;
|
||||
typedef gint32 SpTimeSpan;
|
||||
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
SpClock sp_clock;
|
||||
|
||||
static inline SpTimeStamp
|
||||
sp_clock_get_current_time (void)
|
||||
{
|
||||
struct timespec ts;
|
||||
SpClock clock = sp_clock;
|
||||
|
||||
if G_UNLIKELY (clock == -1)
|
||||
clock = CLOCK_MONOTONIC;
|
||||
clock_gettime (clock, &ts);
|
||||
|
||||
return (ts.tv_sec * G_GINT64_CONSTANT (1000000000)) + ts.tv_nsec;
|
||||
}
|
||||
|
||||
static inline SpTimeSpan
|
||||
sp_clock_get_relative_time (SpTimeStamp epoch)
|
||||
{
|
||||
return sp_clock_get_current_time () - epoch;
|
||||
}
|
||||
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
void sp_clock_init (void);
|
||||
|
||||
G_END_DECLS
|
||||
69
src/libsysprof-capture/sp-platform.c
Normal file
69
src/libsysprof-capture/sp-platform.c
Normal file
@ -0,0 +1,69 @@
|
||||
/* sp-platform.c
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <christian@hergert.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sp-platform.h"
|
||||
|
||||
/**
|
||||
* sp_memfd_create:
|
||||
* @name: (nullable): A descriptive name for the memfd or %NULL
|
||||
*
|
||||
* Creates a new memfd using the memfd_create syscall if available.
|
||||
* Otherwise, a tmpfile is used. Currently, no attempt is made to
|
||||
* ensure the tmpfile is on a tmpfs backed mount.
|
||||
*
|
||||
* Returns: An fd if successful; otherwise -1 and errno is set.
|
||||
*/
|
||||
int
|
||||
sp_memfd_create (const gchar *name)
|
||||
{
|
||||
#ifdef __NR_memfd_create
|
||||
if (name == NULL)
|
||||
name = "[sysprof]";
|
||||
return syscall (__NR_memfd_create, name, 0);
|
||||
#else
|
||||
gchar *name_used = NULL;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* TODO: It would be nice to ensure tmpfs
|
||||
*
|
||||
* It is not strictly required that the preferred temporary directory
|
||||
* will be mounted as tmpfs. We should look through the mounts and ensure
|
||||
* that the tmpfile we open is on tmpfs so that we get anonymous backed
|
||||
* pages after unlinking.
|
||||
*/
|
||||
fd = g_file_open_tmp (NULL, &name_used, NULL);
|
||||
|
||||
if (name_used != NULL)
|
||||
{
|
||||
g_unlink (name_used);
|
||||
g_free (name_used);
|
||||
}
|
||||
|
||||
return fd;
|
||||
#endif
|
||||
}
|
||||
29
src/libsysprof-capture/sp-platform.h
Normal file
29
src/libsysprof-capture/sp-platform.h
Normal file
@ -0,0 +1,29 @@
|
||||
/* sp-platform.h
|
||||
*
|
||||
* Copyright 2016-2019 Christian Hergert <christian@hergert.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
int sp_memfd_create (const gchar *desc);
|
||||
|
||||
G_END_DECLS
|
||||
@ -32,7 +32,6 @@ G_BEGIN_DECLS
|
||||
# include "sp-capture-reader.h"
|
||||
# include "sp-capture-writer.h"
|
||||
# include "sp-clock.h"
|
||||
# include "sp-error.h"
|
||||
# include "sysprof-version.h"
|
||||
# include "sysprof-version-macros.h"
|
||||
|
||||
|
||||
@ -20,10 +20,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined (SYSPROF_CAPTURE_INSIDE) && !defined (SYSPROF_CAPTURE_COMPILATION)
|
||||
# error "Only <sysprof-capture.h> can be included directly."
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "sysprof-version.h"
|
||||
|
||||
@ -20,10 +20,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(SYSPROF_CAPTURE_INSIDE) && !defined(SYSPROF_CAPTURE_COMPILATION)
|
||||
# error "Only <sysprof-capture.h> can be included directly."
|
||||
#endif
|
||||
|
||||
/**
|
||||
* SECTION:sysprof-version
|
||||
* @short_description: sysprof version checking
|
||||
|
||||
Reference in New Issue
Block a user