Exemplo n.º 1
0
/*
 * Handle a sample callback from the PkaSourceSimple.
 */
static inline void
cpu_sample (PkaSourceSimple *source,
            gpointer user_data)
{
  CpuData *cpud = user_data;
  gchar *curr;
  gchar fileBuf[1024];
  gint cpuData[10];

  /*
   * Create and deliver our manifest if it has not yet been done.
   */
  if (G_UNLIKELY(!cpud->manifest)) {
    cpud->manifest = pka_manifest_sized_new(17);
    pka_manifest_append(cpud->manifest, "CPU Number", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "User", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "Nice", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "System", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "Idle", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "I/O Wait", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "IRQ", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "Soft IRQ", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "VM Stolen", G_TYPE_INT);
    pka_manifest_append(cpud->manifest, "VM Guest", G_TYPE_INT);
    pka_source_deliver_manifest(PKA_SOURCE(source), cpud->manifest);
  }

  // Read in /proc/stat first.
  src_utils_read_file("/proc/stat", fileBuf, 1024);
  curr = fileBuf;

  while (curr != NULL) {
     gchar *next = src_utils_str_tok('\n', curr);
     PkaSample *s;
     gint i;

     if (parse_cpu_stat_line(curr, cpuData)) {
        // Then cpuData has valid data.  Return a sample.
        s = pka_sample_new();

        for (i = 0; i < 10; i++)
           pka_sample_append_int(s, i+1, cpuData[i]);

        pka_source_deliver_sample(PKA_SOURCE(source), s);
        pka_sample_unref(s);
     }
     curr = next;
  }
}
Exemplo n.º 2
0
/**
 * pka_manager_add_source:
 * @context: A #PkaContext.
 * @plugin: A #PkaPlugin.
 * @source: A location for a #PkaSource.
 * @error: A location for a #GError or %NULL.
 *
 * Creates a new source in the Perfkit Agent.  If the context does
 * not have permissions to create the source, this operation will fail.
 *
 * If successful, the newly created instance is stored in @source.
 *
 * The caller owns a reference to the source and should free it with
 * g_object_unref().
 *
 * Returns: %TRUE if successful; otherwise %FALSE.
 * Side effects: None.
 */
gboolean
pka_manager_add_source (PkaContext  *context, /* IN */
                        PkaPlugin   *plugin,  /* IN */
                        PkaSource  **source,  /* OUT */
                        GError     **error)   /* OUT */
{
	gint source_id;

	g_return_val_if_fail(context != NULL, FALSE);
	g_return_val_if_fail(source != NULL, FALSE);
	g_return_val_if_fail(PKA_IS_PLUGIN(plugin), FALSE);

	ENTRY;
	AUTHORIZE_IOCTL(context, ADD_SOURCE);
	if (pka_plugin_get_plugin_type(plugin) != PKA_PLUGIN_SOURCE) {
		g_set_error(error, PKA_PLUGIN_ERROR, PKA_PLUGIN_ERROR_INVALID_TYPE,
		            "The specified plugin is not a source plugin.");
		RETURN(FALSE);
	}
	if (!(*source = PKA_SOURCE(pka_plugin_create(plugin, error)))) {
		RETURN(FALSE);
	}
	source_id = pka_source_get_id(*source);
	INFO(Source, "Added source %d on behalf of context %d.",
	     source_id, pka_context_get_id(context));
	G_LOCK(sources);
	g_ptr_array_add(manager.sources, g_object_ref(*source));
	G_UNLOCK(sources);
	NOTIFY_LISTENERS(source_added, source_id);
	RETURN(TRUE);
}