コード例 #1
0
ファイル: hashmap.c プロジェクト: melver/uniwork-kelvin
void hmap_set(struct hash_map *map, void *key, void *data)
{
	size_t hashed_key = map->hash_fn(key);
	struct linked_list *list = &map->buckets_array[hashed_key % map->num_buckets];
	struct ll_node *node;

	node = llist_search(list, _find_key, (void*)&hashed_key);
	if(!node) {
		if(!(node = llist_append(list))) {
			return;
		}
	}

	/* set data */
	{
		struct keydata_pair *kdp;

		if(!node->data) {
			if(!(node->data = (struct keydata_pair*)malloc(sizeof(struct keydata_pair)))) {
				llist_remove(list, node, NULL, NULL);
				ERROR("hmap_set: out of memory!");
				return;
			}
		}

		kdp = (struct keydata_pair*)node->data;
		kdp->hashed_key = hashed_key;
		kdp->data = data;
	}
}
コード例 #2
0
ファイル: main_string.c プロジェクト: zyxstar/exam_c
struct score *search(const char *name)
{
	int ind;
        ind = hash(name) % LIST_NR;

	return llist_search(list[ind], name, name_cmp);
}
コード例 #3
0
ファイル: gmetad.c プロジェクト: skemper/monitor-core
/* Sums the metric summaries from all data sources. */
static int
do_root_summary( datum_t *key, datum_t *val, void *arg )
{
   Source_t *source = (Source_t*) val->data;
   int rc;
   llist_entry *le;

   /* We skip dead sources. */
   if (source->ds->dead)
      return 0;

   /* We skip metrics not to be summarized. */
   if (llist_search(&(gmetad_config.unsummarized_metrics), (void *)key->data, llist_strncmp, &le) == 0)
      return 0;

   /* Need to be sure the source has a complete sum for its metrics. */
   pthread_mutex_lock(source->sum_finished);

   /* We know that all these metrics are numeric. */
   rc = hash_foreach(source->metric_summary, sum_metrics, arg);

   /* Update the top level root source */
   root.hosts_up += source->hosts_up;
   root.hosts_down += source->hosts_down;

   /* summary completed for source */
   pthread_mutex_unlock(source->sum_finished);

   return rc;
}
コード例 #4
0
ファイル: plugin.c プロジェクト: cstenac/collectd
static int register_callback (llist_t **list, /* {{{ */
		const char *name, callback_func_t *cf)
{
	llentry_t *le;
	char *key;

	if (*list == NULL)
	{
		*list = llist_create ();
		if (*list == NULL)
		{
			ERROR ("plugin: register_callback: "
					"llist_create failed.");
			destroy_callback (cf);
			return (-1);
		}
	}

	key = strdup (name);
	if (key == NULL)
	{
		ERROR ("plugin: register_callback: strdup failed.");
		destroy_callback (cf);
		return (-1);
	}

	le = llist_search (*list, name);
	if (le == NULL)
	{
		le = llentry_create (key, cf);
		if (le == NULL)
		{
			ERROR ("plugin: register_callback: "
					"llentry_create failed.");
			free (key);
			destroy_callback (cf);
			return (-1);
		}

		llist_append (*list, le);
	}
	else
	{
		callback_func_t *old_cf;

		old_cf = le->value;
		le->value = cf;

		WARNING ("plugin: register_callback: "
				"a callback named `%s' already exists - "
				"overwriting the old entry!", name);

		destroy_callback (old_cf);
		sfree (key);
	}

	return (0);
} /* }}} int register_callback */
コード例 #5
0
ファイル: fslist.c プロジェクト: mnv104/capfs
fsinfo_p fs_search(fslist_p fsl_p, ino_t fs_ino)
{
	fsinfo_p fs_p;

	fslist_rdlock(fsl_p);
	fs_p = (fsinfo_p) llist_search(fsl_p->list, (void *) (&fs_ino), fs_ino_cmp);
	fslist_unlock(fsl_p);
	return fs_p;
}
コード例 #6
0
ファイル: process_xml.c プロジェクト: gc3-uzh-ch/ganglia
/* Write a metric summary value to the RRD database. */
static int
finish_processing_source(datum_t *key, datum_t *val, void *arg)
{
   xmldata_t *xmldata = (xmldata_t *) arg;
   char *name, *type;
   char sum[512];
   char num[256];
   Metric_t *metric;
   struct type_tag *tt;
   llist_entry *le;

   name = (char*) key->data;
   metric = (Metric_t*) val->data;
   type = getfield(metric->strings, metric->type);

   /* Don't save to RRD if the datasource is dead or write_rrds is off */
   if( xmldata->ds->dead || gmetad_config.write_rrds != 1)
      return 1;

   tt = in_type_list(type, strlen(type));
   if (!tt) return 0;

   /* Don't save to RRD if this is a metric not to be summarized */
   if (llist_search(&(gmetad_config.unsummarized_metrics), (void *)name, llist_strncmp, &le) == 0)
      return 0;

   switch (tt->type)
      {
         case INT:
         case UINT:
            sprintf(sum, "%.f", metric->val.d);
            break;
         case FLOAT:
            sprintf(sum, "%.*f", (int) metric->precision, metric->val.d);
            break;
         default:
            break;
      }
   sprintf(num, "%u", metric->num);

   /* Save the data to a round robin database if this data source is 
    * alive. */
   if (!xmldata->ds->dead && !xmldata->rval)
     {

       debug_msg("Writing Summary data for source %s, metric %s",
                 xmldata->sourcename, name);

       xmldata->rval = write_data_to_rrd(xmldata->sourcename, NULL, name,
                                         sum, num, xmldata->ds->step,
                                         xmldata->source.localtime,
                                         cstr_to_slope(getfield(metric->strings, metric->slope)));
     }

   return xmldata->rval;
}
コード例 #7
0
static int
write_root_summary(datum_t *key, datum_t *val, void *arg)
{
   char *name, *type;
   char sum[256];
   char num[256];
   Metric_t *metric;
   int rc;
   struct type_tag *tt;
   llist_entry *le;
   char *p;

   name = (char*) key->data;
   metric = (Metric_t*) val->data;
   type = getfield(metric->strings, metric->type);

   /* Summarize all numeric metrics */
   tt = in_type_list(type, strlen(type));
   /* Don't write a summary for an unknown or STRING type */
   if (!tt || (tt->type == STRING))
       return 0;

   /* Don't write a summary for metrics not to be summarized */
   if (llist_search(&(gmetad_config.unsummarized_metrics), (void *)key->data, llist_strncmp, &le) == 0)
       return 0;

   /* Don't write a summary for metris that appears to be sFlow VM metrics */
   if (gmetad_config.unsummarized_sflow_vm_metrics && (p = strchr(name, '.')) != NULL && *(p+1) == 'v')
     return 0;

   ganglia_scoreboard_inc(METS_SUMRZ_ROOT);
   /* We log all our sums in double which does not suffer from
      wraparound errors: for example memory KB exceeding 4TB. -twitham */
   sprintf(sum, "%.5f", metric->val.d);

   sprintf(num, "%u", metric->num);

   /* err_msg("Writing Overall Summary for metric %s (%s)", name, sum); */

   /* Save the data to a rrd file unless write_rrds == off */
	 if (gmetad_config.write_rrds == 0)
	     return 0;

   debug_msg("Writing Root Summary data for metric %s", name);

   rc = write_data_to_rrd( NULL, NULL, name, sum, num, 15, 0,
      cstr_to_slope(getfield(metric->strings, metric->slope)));
   if (rc)
      {
         err_msg("Unable to write meta data for metric %s to RRD", name);
      }
   return 0;
}
コード例 #8
0
ファイル: zfs_arc.c プロジェクト: ajdiaz/collectd
static long long get_zfs_value(kstat_t *ksp, const char *key) {
  llentry_t *e;
  value_t *v;

  e = llist_search(ksp, key);
  if (e == NULL) {
    return (-1);
  }

  v = e->value;
  return ((long long)v->derive);
}
コード例 #9
0
ファイル: hashmap.c プロジェクト: melver/uniwork-kelvin
void *hmap_get(struct hash_map *map, void *key)
{
	size_t hashed_key = map->hash_fn(key);
	struct linked_list *list = &map->buckets_array[hashed_key % map->num_buckets];
	struct ll_node *node;

	node = llist_search(list, _find_key, (void*)&hashed_key);

	if(!node)
		return NULL;

	return ((struct keydata_pair*)node->data)->data;
}
コード例 #10
0
ファイル: gmetad.c プロジェクト: HemantaKG/monitor-core
/* Sums the metric summaries from all data sources. */
static int
do_root_summary( datum_t *key, datum_t *val, void *arg )
{
   Source_t *source = (Source_t*) val->data;
   int rc;
   llist_entry *le;

   /* We skip dead sources. */
   if (source->ds->dead)
      return 0;

   /* Need to be sure the source has a complete sum for its metrics. */
   pthread_mutex_lock(source->sum_finished);

   if (gmetad_config.summarized_metrics != NULL) {
      for (le = gmetad_config.summarized_metrics; le != NULL; le = le->next) {
         datum_t skey, *r;
         
         skey.data = le->val;
         skey.size = strlen(le->val) + 1;

         r = hash_lookup(&skey, source->metric_summary);

         if (r != NULL) {
            sum_metrics(&skey, r, NULL);
            datum_free(r);
         }
      }

      rc = 0;
   } else {
      /* We skip metrics not to be summarized. */
      if (llist_search(&(gmetad_config.unsummarized_metrics), (void *)key->data, llist_strncmp, &le) == 0) {
         rc = 0;
         goto out;
      }

      /* We know that all these metrics are numeric. */
      rc = hash_foreach(source->metric_summary, sum_metrics, arg);
   }

   /* Update the top level root source */
   root.hosts_up += source->hosts_up;
   root.hosts_down += source->hosts_down;

out:
   /* summary completed for source */
   pthread_mutex_unlock(source->sum_finished);

   return rc;
}
コード例 #11
0
ファイル: match_regex.c プロジェクト: BrandonArp/collectd
static int mr_config_add_meta_regex(llist_t **meta, /* {{{ */
                                    oconfig_item_t *ci) {
  char *meta_key;
  llentry_t *entry;
  mr_regex_t *re_head;
  int status;
  char buffer[1024];

  if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
      (ci->values[1].type != OCONFIG_TYPE_STRING)) {
    log_warn("`%s' needs exactly two string arguments.", ci->key);
    return -1;
  }

  if (*meta == NULL) {
    *meta = llist_create();
    if (*meta == NULL) {
      log_err("mr_config_add_meta_regex: llist_create failed.");
      return -1;
    }
  }

  meta_key = ci->values[0].value.string;
  entry = llist_search(*meta, meta_key);
  if (entry == NULL) {
    meta_key = strdup(meta_key);
    if (meta_key == NULL) {
      log_err("mr_config_add_meta_regex: strdup failed.");
      return -1;
    }
    entry = llentry_create(meta_key, NULL);
    if (entry == NULL) {
      log_err("mr_config_add_meta_regex: llentry_create failed.");
      sfree(meta_key);
      return -1;
    }
    /* meta_key and entry will now be freed by mr_free_match(). */
    llist_append(*meta, entry);
  }

  snprintf(buffer, sizeof(buffer), "%s `%s'", ci->key, meta_key);
  /* Can't pass &entry->value into mr_add_regex, so copy in/out. */
  re_head = entry->value;
  status = mr_add_regex(&re_head, ci->values[1].value.string, buffer);
  if (status == 0) {
    entry->value = re_head;
  }
  return status;
} /* }}} int mr_config_add_meta_regex */
コード例 #12
0
ファイル: zfs_arc.c プロジェクト: augustollenz/collectd
static long long get_zfs_value(kstat_t *ksp, const char *key)
{
	llentry_t *e;
	value_t *v;

	e = llist_search (ksp, key);
	if (e == NULL)
	{
		ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", key);
		return (-1);
	}

	v = e->value;
	return ((long long) v->derive);
}
コード例 #13
0
ファイル: plugin.c プロジェクト: cstenac/collectd
static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
{
	llentry_t *e;

	if (list == NULL)
		return (-1);

	e = llist_search (list, name);
	if (e == NULL)
		return (-1);

	llist_remove (list, e);

	sfree (e->key);
	destroy_callback (e->value);

	llentry_destroy (e);

	return (0);
} /* }}} int plugin_unregister */
コード例 #14
0
ファイル: server.c プロジェクト: tstarling/monitor-core
void *
server_thread (void *arg)
{
    int interactive = (arg != NULL);
    socklen_t len;
    int request_len;
    client_t client;
    char remote_ip[16];
    char request[REQUESTLEN + 1];
    llist_entry *le;
    datum_t rootdatum;

    for (;;)
    {
        client.valid = 0;
        len = sizeof(client.addr);

        if (interactive)
        {
            pthread_mutex_lock(&server_interactive_mutex);
            SYS_CALL( client.fd, accept(interactive_socket->sockfd, (struct sockaddr *) &(client.addr), &len));
            pthread_mutex_unlock(&server_interactive_mutex);
        }
        else
        {
            pthread_mutex_lock  ( &server_socket_mutex );
            SYS_CALL( client.fd, accept(server_socket->sockfd, (struct sockaddr *) &(client.addr), &len));
            pthread_mutex_unlock( &server_socket_mutex );
        }
        if ( client.fd < 0 )
        {
            err_ret("server_thread() error");
            debug_msg("server_thread() %lx clientfd = %d errno=%d\n",
                      (unsigned long) pthread_self(), client.fd, errno);
            continue;
        }

        my_inet_ntop( AF_INET, (void *)&(client.addr.sin_addr), remote_ip, 16 );

        if ( !strcmp(remote_ip, "127.0.0.1")
                || gmetad_config.all_trusted
                || (llist_search(&(gmetad_config.trusted_hosts), (void *)remote_ip, strcmp, &le) == 0) )
        {
            client.valid = 1;
        }

        if(! client.valid )
        {
            debug_msg("server_thread() %s tried to connect and is not a trusted host",
                      remote_ip);
            close( client.fd );
            continue;
        }

        client.filter=0;
        client.http=0;
        gettimeofday(&client.now, NULL);

        if (interactive)
        {
            request_len = readline(client.fd, request, REQUESTLEN);
            if (request_len < 0)
            {
                err_msg("server_thread() could not read request from %s", remote_ip);
                close(client.fd);
                continue;
            }
            debug_msg("server_thread() received request \"%s\" from %s", request, remote_ip);

            if (process_request(&client, request))
            {
                err_msg("Got a malformed path request from %s", remote_ip);
                close(client.fd);
                continue;
            }
        }
        else
            strcpy(request, "/");

        if(root_report_start(&client))
        {
            err_msg("server_thread() %lx unable to write root preamble (DTD, etc)",
                    (unsigned long) pthread_self() );
            close(client.fd);
            continue;
        }

        /* Start search at the root node. */
        rootdatum.data = &root;
        rootdatum.size = sizeof(root);

        if (process_path(&client, request, &rootdatum, NULL))
        {
            err_msg("server_thread() %lx unable to write XML tree info",
                    (unsigned long) pthread_self() );
            close(client.fd);
            continue;
        }

        if(root_report_end(&client))
        {
            err_msg("server_thread() %lx unable to write root epilog",
                    (unsigned long) pthread_self() );
        }

        close(client.fd);
    }
}