static void sp_hostinfo_source_prepare (SpSource *source) { SpHostinfoSource *self = (SpHostinfoSource *)source; SpCaptureCounter *counters; 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 (guint i = 0; i < self->n_cpu; i++) { SpCaptureCounter *ctr = &counters[i*2]; CpuInfo info = { 0 }; /* * Request 2 counter values. * One for CPU and one for Frequency. */ info.counter_base = sp_capture_writer_request_counter (self->writer, 2); /* * Define counters for capture file. */ ctr->id = info.counter_base; ctr->type = SP_CAPTURE_COUNTER_DOUBLE; ctr->value.vdbl = 0; g_strlcpy (ctr->category, "CPU Percent", sizeof ctr->category); g_snprintf (ctr->name, sizeof ctr->name, "Total CPU %d", i); g_snprintf (ctr->description, sizeof ctr->description, "Total CPU usage %d", i); ctr++; ctr->id = info.counter_base + 1; ctr->type = SP_CAPTURE_COUNTER_DOUBLE; ctr->value.vdbl = 0; g_strlcpy (ctr->category, "CPU Frequency", sizeof ctr->category); g_snprintf (ctr->name, sizeof ctr->name, "CPU %d", i); g_snprintf (ctr->description, sizeof ctr->description, "Frequency of CPU %d", i); g_array_append_val (self->cpu_info, info); } sp_capture_writer_define_counters (self->writer, SP_CAPTURE_CURRENT_TIME, -1, getpid (), counters, self->n_cpu * 2); sp_source_emit_ready (SP_SOURCE (self)); }
static void sp_hostinfo_source_stop (SpSource *source) { SpHostinfoSource *self = (SpHostinfoSource *)source; g_assert (SP_IS_HOSTINFO_SOURCE (self)); g_source_remove (self->handler); self->handler = 0; sp_source_emit_finished (SP_SOURCE (self)); }