#include "ovsdb-parser.h"
#include "json.h"
#include "shash.h"
+#include "smap.h"
#include "sort.h"
#include "unicode.h"
}
/* Initializes 'datum' as a string-to-string map whose contents are taken from
- * 'sh'. Destroys 'sh'. */
+ * 'smap'. Destroys 'smap'. */
void
-ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh)
+ovsdb_datum_from_smap(struct ovsdb_datum *datum, struct smap *smap)
{
- struct shash_node *node, *next;
+ struct smap_node *node, *next;
size_t i;
- datum->n = shash_count(sh);
+ datum->n = smap_count(smap);
datum->keys = xmalloc(datum->n * sizeof *datum->keys);
datum->values = xmalloc(datum->n * sizeof *datum->values);
i = 0;
- SHASH_FOR_EACH_SAFE (node, next, sh) {
- datum->keys[i].string = node->name;
- datum->values[i].string = node->data;
- shash_steal(sh, node);
+ SMAP_FOR_EACH_SAFE (node, next, smap) {
+ smap_steal(smap, node,
+ &datum->keys[i].string, &datum->values[i].string);
i++;
}
assert(i == datum->n);
- shash_destroy(sh);
+ smap_destroy(smap);
ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
}
-/* Copyright (c) 2010 Nicira, Inc.
+/* Copyright (c) 2010, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <config.h>
+#include "system-stats.h"
+
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include "dirs.h"
#include "dynamic-string.h"
#include "shash.h"
-#include "system-stats.h"
+#include "smap.h"
#include "timeval.h"
#include "vlog.h"
#endif
static void
-get_cpu_cores(struct shash *stats)
+get_cpu_cores(struct smap *stats)
{
long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (n_cores > 0) {
- shash_add(stats, "cpu", xasprintf("%ld", n_cores));
+ smap_add_format(stats, "cpu", "%ld", n_cores);
}
}
static void
-get_load_average(struct shash *stats OVS_UNUSED)
+get_load_average(struct smap *stats OVS_UNUSED)
{
#if HAVE_GETLOADAVG
double loadavg[3];
if (getloadavg(loadavg, 3) == 3) {
- shash_add(stats, "load_average",
- xasprintf("%.2f,%.2f,%.2f",
- loadavg[0], loadavg[1], loadavg[2]));
+ smap_add_format(stats, "load_average", "%.2f,%.2f,%.2f",
+ loadavg[0], loadavg[1], loadavg[2]);
}
#endif
}
}
static void
-get_memory_stats(struct shash *stats)
+get_memory_stats(struct smap *stats)
{
if (!LINUX) {
unsigned int pagesize = get_page_size();
mem_total = phys_pages * (pagesize / 1024);
mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
- shash_add(stats, "memory", xasprintf("%d,%d", mem_total, mem_used));
+ smap_add_format(stats, "memory", "%d,%d", mem_total, mem_used);
} else {
static const char file_name[] = "/proc/meminfo";
int mem_used, mem_cache, swap_used;
mem_used = mem_total - mem_free;
mem_cache = buffers + cached;
swap_used = swap_total - swap_free;
- shash_add(stats, "memory",
- xasprintf("%d,%d,%d,%d,%d", mem_total, mem_used, mem_cache,
- swap_total, swap_used));
+ smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
+ mem_total, mem_used, mem_cache, swap_total, swap_used);
}
}
}
static void
-get_process_stats(struct shash *stats)
+get_process_stats(struct smap *stats)
{
struct dirent *de;
DIR *dir;
while ((de = readdir(dir)) != NULL) {
struct process_info pinfo;
- char *key, *value;
char *file_name;
char *extension;
+ char *key;
pid_t pid;
#ifdef _DIRENT_HAVE_D_TYPE
key = xasprintf("process_%.*s",
(int) (extension - de->d_name), de->d_name);
- if (shash_find(stats, key)) {
- free(key);
- continue;
- }
-
- if (LINUX && get_process_info(pid, &pinfo)) {
- value = xasprintf("%lu,%lu,%lld,%d,%lld,%lld",
- pinfo.vsz, pinfo.rss, pinfo.cputime,
- pinfo.crashes, pinfo.booted, pinfo.uptime);
- } else {
- value = xstrdup("");
+ if (!smap_get(stats, key)) {
+ if (LINUX && get_process_info(pid, &pinfo)) {
+ smap_add_format(stats, key, "%lu,%lu,%lld,%d,%lld,%lld",
+ pinfo.vsz, pinfo.rss, pinfo.cputime,
+ pinfo.crashes, pinfo.booted, pinfo.uptime);
+ } else {
+ smap_add(stats, key, "");
+ }
}
-
- shash_add_nocopy(stats, key, value);
+ free(key);
}
closedir(dir);
}
static void
-get_filesys_stats(struct shash *stats OVS_UNUSED)
+get_filesys_stats(struct smap *stats OVS_UNUSED)
{
#if HAVE_SETMNTENT && HAVE_STATVFS
static const char file_name[] = "/etc/mtab";
endmntent(stream);
if (s.length) {
- shash_add(stats, "file_systems", ds_steal_cstr(&s));
+ smap_add(stats, "file_systems", ds_cstr(&s));
}
ds_destroy(&s);
#endif /* HAVE_SETMNTENT && HAVE_STATVFS */
}
void
-get_system_stats(struct shash *stats)
+get_system_stats(struct smap *stats)
{
get_cpu_cores(stats);
get_load_average(stats);