libsysprof: add API to add compressed file from data

This commit is contained in:
Christian Hergert
2023-07-19 18:29:12 -07:00
parent 233685d5ad
commit 90ed413c5a
4 changed files with 50 additions and 4 deletions

View File

@ -211,7 +211,7 @@ add_process_info (SysprofRecording *recording,
* in resolving symbols later on.
*/
mount_path = g_strdup_printf ("/proc/%u/mountinfo", pid);
_sysprof_recording_add_file_data (recording, mount_path, mountinfo, -1);
_sysprof_recording_add_file_data (recording, mount_path, mountinfo, -1, FALSE);
/* Ignore inodes from podman/toolbox because they appear to always be
* wrong. We'll have to rely on CRC/build-id instead.

View File

@ -41,7 +41,8 @@ DexFuture *_sysprof_recording_add_file (SysprofRecording *s
void _sysprof_recording_add_file_data (SysprofRecording *self,
const char *path,
const char *contents,
gssize length);
gssize length,
gboolean compress);
void _sysprof_recording_diagnostic (SysprofRecording *self,
const char *domain,
const char *format,

View File

@ -712,12 +712,43 @@ _sysprof_recording_add_file (SysprofRecording *self,
(GDestroyNotify)add_file_free);
}
static char *
do_compress (const char *data,
gsize length,
gsize *out_length)
{
g_autoptr(GZlibCompressor) compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 6);
g_autofree char *compressed = g_malloc (length);
GConverterResult res;
gsize n_read;
gsize n_written;
*out_length = 0;
res = g_converter_convert (G_CONVERTER (compressor), data, length, compressed, length,
G_CONVERTER_INPUT_AT_END | G_CONVERTER_FLUSH,
&n_read, &n_written, NULL);
if (res == G_CONVERTER_FINISHED)
{
*out_length = n_written;
return g_steal_pointer (&compressed);
}
return NULL;
}
void
_sysprof_recording_add_file_data (SysprofRecording *self,
const char *path,
const char *contents,
gssize length)
gssize length,
gboolean compress)
{
g_autofree char *compress_filename = NULL;
g_autofree char *compress_bytes = NULL;
gsize compress_len = 0;
g_return_if_fail (SYSPROF_IS_RECORDING (self));
g_return_if_fail (path != NULL);
g_return_if_fail (contents != NULL);
@ -725,6 +756,19 @@ _sysprof_recording_add_file_data (SysprofRecording *self,
if (length < 0)
length = strlen (contents);
if (compress)
{
compress_bytes = do_compress (contents, length, &compress_len);
if (compress_bytes)
{
compress_filename = g_strdup_printf ("%s.gz", path);
path = compress_filename;
contents = compress_bytes;
length = compress_len;
}
}
while (length > 0)
{
gsize to_write = MIN (length, ((4096*8)-sizeof (SysprofCaptureFileChunk)));

View File

@ -91,7 +91,8 @@ sysprof_symbols_bundle_augment_fiber (gpointer user_data)
_sysprof_recording_add_file_data (recording,
"__symbols__",
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes));
g_bytes_get_size (bytes),
TRUE);
return dex_future_new_for_boolean (TRUE);
}