mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-03-23 21:51:26 +00:00
hostinfo: reuse proc FD and memory when parsing
Re-opening the FD on every poll is pretty expensive. So we should avoid doing that as well re-use the read buffer (page-aligned, <=1 page) for the sample data.
This commit is contained in:
@ -17,20 +17,27 @@
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sources/sp-hostinfo-source.h"
|
||||
|
||||
#define PROC_STAT_BUF_SIZE 4096
|
||||
|
||||
struct _SpHostinfoSource
|
||||
{
|
||||
GObject parent_instance;
|
||||
|
||||
guint handler;
|
||||
gint n_cpu;
|
||||
gint stat_fd;
|
||||
|
||||
SpCaptureWriter *writer;
|
||||
GArray *cpu_info;
|
||||
gchar *stat_buf;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
@ -61,6 +68,30 @@ sp_hostinfo_source_new (void)
|
||||
return g_object_new (SP_TYPE_HOSTINFO_SOURCE, NULL);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
read_stat (SpHostinfoSource *self)
|
||||
{
|
||||
gssize len;
|
||||
|
||||
g_assert (self != NULL);
|
||||
g_assert (self->stat_fd != -1);
|
||||
g_assert (self->stat_buf != NULL);
|
||||
|
||||
if (lseek (self->stat_fd, 0, SEEK_SET) != 0)
|
||||
return FALSE;
|
||||
|
||||
len = read (self->stat_fd, self->stat_buf, PROC_STAT_BUF_SIZE);
|
||||
if (len <= 0)
|
||||
return FALSE;
|
||||
|
||||
if (len < PROC_STAT_BUF_SIZE)
|
||||
self->stat_buf[len] = 0;
|
||||
else
|
||||
self->stat_buf[PROC_STAT_BUF_SIZE-1] = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
poll_cpu (SpHostinfoSource *self)
|
||||
{
|
||||
@ -85,23 +116,24 @@ poll_cpu (SpHostinfoSource *self)
|
||||
glong steal_calc;
|
||||
glong guest_calc;
|
||||
glong guest_nice_calc;
|
||||
gchar *buf = NULL;
|
||||
glong total;
|
||||
gchar *line;
|
||||
gint ret;
|
||||
gint id;
|
||||
gint i;
|
||||
|
||||
if (g_file_get_contents("/proc/stat", &buf, NULL, NULL))
|
||||
if (read_stat (self))
|
||||
{
|
||||
line = buf;
|
||||
for (i = 0; buf[i]; i++)
|
||||
line = self->stat_buf;
|
||||
|
||||
for (gsize i = 0; self->stat_buf[i]; i++)
|
||||
{
|
||||
if (buf[i] == '\n') {
|
||||
buf[i] = '\0';
|
||||
if (g_str_has_prefix(line, "cpu"))
|
||||
if (self->stat_buf[i] == '\n')
|
||||
{
|
||||
if (isdigit(line[3]))
|
||||
self->stat_buf[i] = '\0';
|
||||
|
||||
if (strncmp (line, "cpu", 3) == 0)
|
||||
{
|
||||
if (isdigit (line[3]))
|
||||
{
|
||||
CpuInfo *cpu_info;
|
||||
|
||||
@ -144,18 +176,18 @@ poll_cpu (SpHostinfoSource *self)
|
||||
cpu_info->last_guest = guest;
|
||||
cpu_info->last_guest_nice = guest_nice;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* CPU info comes first. Skip further lines. */
|
||||
break;
|
||||
}
|
||||
|
||||
next:
|
||||
line = &buf[i + 1];
|
||||
line = &self->stat_buf[i + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g_free (buf);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -163,12 +195,11 @@ publish_cpu (SpHostinfoSource *self)
|
||||
{
|
||||
SpCaptureCounterValue *counter_values;
|
||||
guint *counter_ids;
|
||||
gint i;
|
||||
|
||||
counter_ids = alloca (sizeof *counter_ids * self->n_cpu * 2);
|
||||
counter_values = alloca (sizeof *counter_values * self->n_cpu * 2);
|
||||
|
||||
for (i = 0; i < self->n_cpu; i++)
|
||||
for (guint i = 0; i < self->n_cpu; i++)
|
||||
{
|
||||
CpuInfo *info = &g_array_index (self->cpu_info, CpuInfo, i);
|
||||
SpCaptureCounterValue *value = &counter_values[i*2];
|
||||
@ -219,6 +250,7 @@ sp_hostinfo_source_finalize (GObject *object)
|
||||
|
||||
g_clear_pointer (&self->writer, sp_capture_writer_unref);
|
||||
g_clear_pointer (&self->cpu_info, g_array_unref);
|
||||
g_clear_pointer (&self->stat_buf, g_free);
|
||||
|
||||
G_OBJECT_CLASS (sp_hostinfo_source_parent_class)->finalize (object);
|
||||
}
|
||||
@ -234,7 +266,9 @@ sp_hostinfo_source_class_init (SpHostinfoSourceClass *klass)
|
||||
static void
|
||||
sp_hostinfo_source_init (SpHostinfoSource *self)
|
||||
{
|
||||
self->stat_fd = -1;
|
||||
self->cpu_info = g_array_new (FALSE, TRUE, sizeof (CpuInfo));
|
||||
self->stat_buf = g_malloc (PROC_STAT_BUF_SIZE);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -270,6 +304,12 @@ sp_hostinfo_source_stop (SpSource *source)
|
||||
g_source_remove (self->handler);
|
||||
self->handler = 0;
|
||||
|
||||
if (self->stat_fd != -1)
|
||||
{
|
||||
close (self->stat_fd);
|
||||
self->stat_fd = -1;
|
||||
}
|
||||
|
||||
sp_source_emit_finished (SP_SOURCE (self));
|
||||
}
|
||||
|
||||
@ -278,17 +318,17 @@ sp_hostinfo_source_prepare (SpSource *source)
|
||||
{
|
||||
SpHostinfoSource *self = (SpHostinfoSource *)source;
|
||||
SpCaptureCounter *counters;
|
||||
gint i;
|
||||
|
||||
g_assert (SP_IS_HOSTINFO_SOURCE (self));
|
||||
|
||||
self->stat_fd = open ("/proc/stat", O_RDONLY);
|
||||
self->n_cpu = g_get_num_processors ();
|
||||
|
||||
g_array_set_size (self->cpu_info, 0);
|
||||
|
||||
counters = alloca (sizeof *counters * self->n_cpu * 2);
|
||||
|
||||
for (i = 0; i < self->n_cpu; i++)
|
||||
for (guint i = 0; i < self->n_cpu; i++)
|
||||
{
|
||||
SpCaptureCounter *ctr = &counters[i*2];
|
||||
CpuInfo info = { 0 };
|
||||
|
||||
Reference in New Issue
Block a user