2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-19 14:37:21 +00:00

system-stats: Use "smap" instead of "shash".

"smap" is now the appropriate data structure for a string-to-string map.

Also changes ovsdb_datum_from_shash() into ovsdb_datum_from_smap() since
system-stats related code was the only client.

Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Ben Pfaff
2012-07-18 10:51:02 -07:00
parent 51c82a49d5
commit 57c8677b51
7 changed files with 66 additions and 54 deletions

View File

@@ -29,6 +29,7 @@
#include "ovsdb-parser.h" #include "ovsdb-parser.h"
#include "json.h" #include "json.h"
#include "shash.h" #include "shash.h"
#include "smap.h"
#include "sort.h" #include "sort.h"
#include "unicode.h" #include "unicode.h"
@@ -1524,27 +1525,26 @@ ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
} }
/* Initializes 'datum' as a string-to-string map whose contents are taken from /* Initializes 'datum' as a string-to-string map whose contents are taken from
* 'sh'. Destroys 'sh'. */ * 'smap'. Destroys 'smap'. */
void 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; size_t i;
datum->n = shash_count(sh); datum->n = smap_count(smap);
datum->keys = xmalloc(datum->n * sizeof *datum->keys); datum->keys = xmalloc(datum->n * sizeof *datum->keys);
datum->values = xmalloc(datum->n * sizeof *datum->values); datum->values = xmalloc(datum->n * sizeof *datum->values);
i = 0; i = 0;
SHASH_FOR_EACH_SAFE (node, next, sh) { SMAP_FOR_EACH_SAFE (node, next, smap) {
datum->keys[i].string = node->name; smap_steal(smap, node,
datum->values[i].string = node->data; &datum->keys[i].string, &datum->values[i].string);
shash_steal(sh, node);
i++; i++;
} }
assert(i == datum->n); assert(i == datum->n);
shash_destroy(sh); smap_destroy(smap);
ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
} }

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2009, 2010, 2011 Nicira, Inc. /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
struct ds; struct ds;
struct ovsdb_symbol_table; struct ovsdb_symbol_table;
struct smap;
/* One value of an atomic type (given by enum ovs_atomic_type). */ /* One value of an atomic type (given by enum ovs_atomic_type). */
union ovsdb_atom { union ovsdb_atom {
@@ -172,7 +173,7 @@ void ovsdb_datum_to_string(const struct ovsdb_datum *,
void ovsdb_datum_to_bare(const struct ovsdb_datum *, void ovsdb_datum_to_bare(const struct ovsdb_datum *,
const struct ovsdb_type *, struct ds *); const struct ovsdb_type *, struct ds *);
void ovsdb_datum_from_shash(struct ovsdb_datum *, struct shash *); void ovsdb_datum_from_smap(struct ovsdb_datum *, struct smap *);
/* Comparison. */ /* Comparison. */
uint32_t ovsdb_datum_hash(const struct ovsdb_datum *, uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,

View File

@@ -125,16 +125,29 @@ smap_remove_node(struct smap *smap, struct smap_node *node)
free(node); free(node);
} }
/* Deletes 'node' from 'sh'. Neither the node's key nor its value is freed; /* Deletes 'node' from 'smap'.
* instead, ownership is transferred to the caller. Returns the node's key. */ *
char * * If 'keyp' is nonnull, stores the node's key in '*keyp' and transfers
smap_steal(struct smap *smap, struct smap_node *node) * ownership to the caller. Otherwise, frees the node's key. Similarly for
* 'valuep' and the node's value. */
void
smap_steal(struct smap *smap, struct smap_node *node,
char **keyp, char **valuep)
{ {
char *key = node->key; if (keyp) {
*keyp = node->key;
} else {
free(node->key);
}
if (valuep) {
*valuep = node->value;
} else {
free(node->value);
}
hmap_remove(&smap->map, &node->node); hmap_remove(&smap->map, &node->node);
free(node); free(node);
return key;
} }
/* Removes all key-value pairs from 'smap'. */ /* Removes all key-value pairs from 'smap'. */

View File

@@ -49,7 +49,7 @@ void smap_replace(struct smap *, const char *, const char *);
void smap_remove(struct smap *, const char *); void smap_remove(struct smap *, const char *);
void smap_remove_node(struct smap *, struct smap_node *); void smap_remove_node(struct smap *, struct smap_node *);
char *smap_steal(struct smap *, struct smap_node *); void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep);
void smap_clear(struct smap *); void smap_clear(struct smap *);
const char *smap_get(const struct smap *, const char *); const char *smap_get(const struct smap *, const char *);

View File

@@ -1875,14 +1875,14 @@ static void
refresh_system_stats(const struct ovsrec_open_vswitch *cfg) refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
{ {
struct ovsdb_datum datum; struct ovsdb_datum datum;
struct shash stats; struct smap stats;
shash_init(&stats); smap_init(&stats);
if (enable_system_stats(cfg)) { if (enable_system_stats(cfg)) {
get_system_stats(&stats); get_system_stats(&stats);
} }
ovsdb_datum_from_shash(&datum, &stats); ovsdb_datum_from_smap(&datum, &stats);
ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics, ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
&datum); &datum);
} }

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2010 Nicira, Inc. /* Copyright (c) 2010, 2012 Nicira, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
#include <config.h> #include <config.h>
#include "system-stats.h"
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <dirent.h> #include <dirent.h>
@@ -34,7 +36,7 @@
#include "dirs.h" #include "dirs.h"
#include "dynamic-string.h" #include "dynamic-string.h"
#include "shash.h" #include "shash.h"
#include "system-stats.h" #include "smap.h"
#include "timeval.h" #include "timeval.h"
#include "vlog.h" #include "vlog.h"
@@ -52,24 +54,23 @@ VLOG_DEFINE_THIS_MODULE(system_stats);
#endif #endif
static void static void
get_cpu_cores(struct shash *stats) get_cpu_cores(struct smap *stats)
{ {
long int n_cores = sysconf(_SC_NPROCESSORS_ONLN); long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (n_cores > 0) { if (n_cores > 0) {
shash_add(stats, "cpu", xasprintf("%ld", n_cores)); smap_add_format(stats, "cpu", "%ld", n_cores);
} }
} }
static void static void
get_load_average(struct shash *stats OVS_UNUSED) get_load_average(struct smap *stats OVS_UNUSED)
{ {
#if HAVE_GETLOADAVG #if HAVE_GETLOADAVG
double loadavg[3]; double loadavg[3];
if (getloadavg(loadavg, 3) == 3) { if (getloadavg(loadavg, 3) == 3) {
shash_add(stats, "load_average", smap_add_format(stats, "load_average", "%.2f,%.2f,%.2f",
xasprintf("%.2f,%.2f,%.2f", loadavg[0], loadavg[1], loadavg[2]);
loadavg[0], loadavg[1], loadavg[2]));
} }
#endif #endif
} }
@@ -90,7 +91,7 @@ get_page_size(void)
} }
static void static void
get_memory_stats(struct shash *stats) get_memory_stats(struct smap *stats)
{ {
if (!LINUX) { if (!LINUX) {
unsigned int pagesize = get_page_size(); unsigned int pagesize = get_page_size();
@@ -108,7 +109,7 @@ get_memory_stats(struct shash *stats)
mem_total = phys_pages * (pagesize / 1024); mem_total = phys_pages * (pagesize / 1024);
mem_used = (phys_pages - avphys_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 { } else {
static const char file_name[] = "/proc/meminfo"; static const char file_name[] = "/proc/meminfo";
int mem_used, mem_cache, swap_used; int mem_used, mem_cache, swap_used;
@@ -152,9 +153,8 @@ get_memory_stats(struct shash *stats)
mem_used = mem_total - mem_free; mem_used = mem_total - mem_free;
mem_cache = buffers + cached; mem_cache = buffers + cached;
swap_used = swap_total - swap_free; swap_used = swap_total - swap_free;
shash_add(stats, "memory", smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
xasprintf("%d,%d,%d,%d,%d", mem_total, mem_used, mem_cache, mem_total, mem_used, mem_cache, swap_total, swap_used);
swap_total, swap_used));
} }
} }
@@ -385,7 +385,7 @@ get_process_info(pid_t pid, struct process_info *pinfo)
} }
static void static void
get_process_stats(struct shash *stats) get_process_stats(struct smap *stats)
{ {
struct dirent *de; struct dirent *de;
DIR *dir; DIR *dir;
@@ -398,9 +398,9 @@ get_process_stats(struct shash *stats)
while ((de = readdir(dir)) != NULL) { while ((de = readdir(dir)) != NULL) {
struct process_info pinfo; struct process_info pinfo;
char *key, *value;
char *file_name; char *file_name;
char *extension; char *extension;
char *key;
pid_t pid; pid_t pid;
#ifdef _DIRENT_HAVE_D_TYPE #ifdef _DIRENT_HAVE_D_TYPE
@@ -423,27 +423,23 @@ get_process_stats(struct shash *stats)
key = xasprintf("process_%.*s", key = xasprintf("process_%.*s",
(int) (extension - de->d_name), de->d_name); (int) (extension - de->d_name), de->d_name);
if (shash_find(stats, key)) { if (!smap_get(stats, key)) {
free(key); if (LINUX && get_process_info(pid, &pinfo)) {
continue; 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, "");
}
} }
free(key);
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("");
}
shash_add_nocopy(stats, key, value);
} }
closedir(dir); closedir(dir);
} }
static void static void
get_filesys_stats(struct shash *stats OVS_UNUSED) get_filesys_stats(struct smap *stats OVS_UNUSED)
{ {
#if HAVE_SETMNTENT && HAVE_STATVFS #if HAVE_SETMNTENT && HAVE_STATVFS
static const char file_name[] = "/etc/mtab"; static const char file_name[] = "/etc/mtab";
@@ -489,14 +485,14 @@ get_filesys_stats(struct shash *stats OVS_UNUSED)
endmntent(stream); endmntent(stream);
if (s.length) { if (s.length) {
shash_add(stats, "file_systems", ds_steal_cstr(&s)); smap_add(stats, "file_systems", ds_cstr(&s));
} }
ds_destroy(&s); ds_destroy(&s);
#endif /* HAVE_SETMNTENT && HAVE_STATVFS */ #endif /* HAVE_SETMNTENT && HAVE_STATVFS */
} }
void void
get_system_stats(struct shash *stats) get_system_stats(struct smap *stats)
{ {
get_cpu_cores(stats); get_cpu_cores(stats);
get_load_average(stats); get_load_average(stats);

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2010 Nicira, Inc. /* Copyright (c) 2010, 2012 Nicira, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
#ifndef VSWITCHD_SYSTEM_STATS #ifndef VSWITCHD_SYSTEM_STATS
#define VSWITCHD_SYSTEM_STATS 1 #define VSWITCHD_SYSTEM_STATS 1
void get_system_stats(struct shash *); struct smap;
void get_system_stats(struct smap *);
#endif /* vswitchd/system-stats.h */ #endif /* vswitchd/system-stats.h */