Changeset 5ab626a for lib/trace.c


Ignore:
Timestamp:
02/18/15 17:41:48 (7 years ago)
Author:
Richard Sanger <rsangerarj@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
e4f27d1
Parents:
8bcc925
Message:

Deprecate trace_get_filtered/accepted/recevied/dropped() in favour of a single function

Adds the single trace_get_statistics function. This allows the structure to be filled
at a point in time, rather than making multiple calls to the library during which state
might have changed.

This has been designed such that the structure can be added to in the future without
breaking old code.

The old internal get_captured_packets was removed from the formats as it was never used.
Eventually we should completely remove get_filtered and received from the formats and replace
them with get_statistics.

In additon some extra fields have added, such as error and captured and the pre-existing
fields are better defined.

The linux formats have been updated to use this new API, which combined with reading
/proc/net/dev returns a full set of statistics.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/trace.c

    r4631115 r5ab626a  
    280280        libtrace->first_packets.count = 0;
    281281        libtrace->first_packets.packets = NULL;
    282         libtrace->dropped_packets = UINT64_MAX;
    283         libtrace->received_packets = UINT64_MAX;
     282        libtrace->stats = NULL;
    284283        libtrace->pread = NULL;
    285284        ZERO_USER_CONFIG(libtrace->config);
     
    397396        libtrace->perpkt_threads = NULL;
    398397        libtrace->tracetime = 0;
     398        libtrace->stats = NULL;
    399399        libtrace->pread = NULL;
    400400        ZERO_USER_CONFIG(libtrace->config);
     
    660660        if (libtrace->uridata)
    661661                free(libtrace->uridata);
     662
     663        if (libtrace->stats)
     664                free(libtrace->stats);
    662665       
    663666        /* Empty any packet memory */
     
    19511954                if ((ret = trace->format->get_received_packets(trace)) != UINT64_MAX)
    19521955                        return ret;
    1953         }
    1954         // Read this cached value taken before the trace was closed
    1955         return trace->received_packets;
     1956        } else if (trace->format->get_statistics) {
     1957                struct libtrace_stat_t stat;
     1958                stat.magic = LIBTRACE_STAT_MAGIC;
     1959                trace_get_statistics(trace, &stat);
     1960                if (stat.received_valid)
     1961                        return stat.received;
     1962        }
     1963
     1964        // Read the cached value taken before the trace was paused/closed
     1965        if(trace->stats && trace->stats->received_valid)
     1966                return trace->stats->received;
     1967        else
     1968                return UINT64_MAX;
    19561969}
    19571970
     
    19601973        assert(trace);
    19611974        int i = 0;
    1962         uint64_t ret = trace->filtered_packets;
     1975        uint64_t lib_filtered = trace->filtered_packets;
    19631976        for (i = 0; i < trace->perpkt_thread_count; i++) {
    1964                 ret += trace->perpkt_threads[i].filtered_packets;
     1977                lib_filtered += trace->perpkt_threads[i].filtered_packets;
    19651978        }
    19661979        if (trace->format->get_filtered_packets) {
    1967                 return trace->format->get_filtered_packets(trace)+
    1968                         ret;
    1969         }
    1970         return ret;
     1980                uint64_t trace_filtered = trace->format->get_filtered_packets(trace);
     1981                if (trace_filtered == UINT64_MAX)
     1982                        return UINT64_MAX;
     1983                else
     1984                        return trace_filtered + lib_filtered;
     1985        } else if (trace->format->get_statistics) {
     1986                struct libtrace_stat_t stat;
     1987                stat.magic = LIBTRACE_STAT_MAGIC;
     1988                trace_get_statistics(trace, &stat);
     1989                if (stat.filtered_valid)
     1990                        return lib_filtered + stat.filtered;
     1991                else
     1992                        return UINT64_MAX;
     1993        }
     1994
     1995        // Read the cached value taken before the trace was paused/closed
     1996        if(trace->stats && trace->stats->filtered_valid)
     1997                return trace->stats->filtered + lib_filtered;
     1998        else
     1999                return lib_filtered;
    19712000}
    19722001
     
    19792008                if ((ret = trace->format->get_dropped_packets(trace)) != UINT64_MAX)
    19802009                        return ret;
    1981         }
    1982         // Read this cached value taken before the trace was closed
    1983         return trace->dropped_packets;
     2010        } else if (trace->format->get_statistics) {
     2011                struct libtrace_stat_t stat;
     2012                stat.magic = LIBTRACE_STAT_MAGIC;
     2013                trace_get_statistics(trace, &stat);
     2014                if (stat.dropped_valid)
     2015                        return stat.dropped;
     2016        }
     2017
     2018        // Read the cached value taken before the trace was paused/closed
     2019        if(trace->stats && trace->stats->dropped_valid)
     2020                return trace->stats->dropped;
     2021        else
     2022                return UINT64_MAX;
    19842023}
    19852024
     
    19942033        return ret;
    19952034}
     2035
     2036libtrace_stat_t *trace_get_statistics(libtrace_t *trace, libtrace_stat_t *stat)
     2037{
     2038        uint64_t ret;
     2039        int i;
     2040        assert(trace);
     2041        if (stat == NULL) {
     2042                if (trace->stats == NULL)
     2043                        trace->stats = trace_create_statistics();
     2044                stat = trace->stats;
     2045        }
     2046        assert(stat->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2047               "trace_create_statistics() to allocate statistics");
     2048        stat->reserved1 = 0;
     2049        stat->reserved2 = 0;
     2050#define X(x) stat->x ##_valid = 0;
     2051        LIBTRACE_STAT_FIELDS;
     2052#undef X
     2053        if (trace->state == STATE_PAUSED ||
     2054            trace->state == STATE_FINSHED ||
     2055            trace->state == STATE_JOINED) {
     2056                if (trace->stats && trace->stats != stat)
     2057                        *stat = *trace->stats;
     2058                return stat;
     2059        } else if (trace->format->get_statistics) {
     2060                trace->format->get_statistics(trace, stat);
     2061                ret = trace_get_accepted_packets(trace);
     2062                if (ret != UINT64_MAX) {
     2063                        stat->accepted_valid = 1;
     2064                        stat->accepted = ret;
     2065                }
     2066                /* Now add on any library filtered packets */
     2067                if (stat->filtered_valid) {
     2068                        stat->filtered += trace->filtered_packets;
     2069                        for (i = 0; i < trace->perpkt_thread_count; i++) {
     2070                                stat->filtered += trace->perpkt_threads[i].filtered_packets;
     2071                        }
     2072                }
     2073                return stat;
     2074        }
     2075        ret = trace_get_accepted_packets(trace);
     2076        if (ret != UINT64_MAX) {
     2077                stat->accepted_valid = 1;
     2078                stat->accepted = ret;
     2079        }
     2080        ret = trace_get_received_packets(trace);
     2081        if (ret != UINT64_MAX) {
     2082                stat->received_valid = 1;
     2083                stat->received = ret;
     2084        }
     2085        /* Fallback to the old way */
     2086        ret = trace_get_dropped_packets(trace);
     2087        if (ret != UINT64_MAX) {
     2088                stat->dropped_valid = 1;
     2089                stat->dropped = ret;
     2090        }
     2091        ret = trace_get_filtered_packets(trace);
     2092        if (ret != UINT64_MAX) {
     2093                stat->filtered_valid = 1;
     2094                stat->filtered = ret;
     2095        }
     2096        return stat;
     2097}
     2098
     2099void trace_get_thread_statistics(libtrace_t *trace, libtrace_thread_t *t,
     2100                                 libtrace_stat_t *stat)
     2101{
     2102        assert(trace && stat);
     2103        assert(stat->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2104               "trace_create_statistics() to allocate statistics");
     2105        stat->reserved1 = 0;
     2106        stat->reserved2 = 0;
     2107#define X(x) stat->x ##_valid= 0;
     2108        LIBTRACE_STAT_FIELDS;
     2109#undef X
     2110        if (trace->format->get_thread_statistics) {
     2111                trace->format->get_thread_statistics(trace, t, stat);
     2112        }
     2113        if (t->accepted_packets != UINT64_MAX) {
     2114                stat->accepted_valid = 1;
     2115                stat->accepted = t->accepted_packets;
     2116        }
     2117        /* Now add on any library filtered packets */
     2118        if (stat->filtered_valid) {
     2119                stat->filtered += t->filtered_packets;
     2120        }
     2121        return;
     2122}
     2123
     2124libtrace_stat_t *trace_create_statistics(void) {
     2125        libtrace_stat_t *ret;
     2126        ret = malloc(sizeof(libtrace_stat_t));
     2127        if (ret) {
     2128                memset(ret, 0, sizeof(libtrace_stat_t));
     2129                ret->magic = LIBTRACE_STAT_MAGIC;
     2130        }
     2131        return ret;
     2132}
     2133
     2134void trace_subtract_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b,
     2135                         libtrace_stat_t *c) {
     2136        assert(a->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2137               "trace_create_statistics() to allocate statistics");
     2138        assert(b->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2139               "trace_create_statistics() to allocate statistics");
     2140        assert(c->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2141               "trace_create_statistics() to allocate statistics");
     2142
     2143#define X(x) \
     2144        if (a->x ##_valid && b->x ##_valid) { \
     2145                c->x ##_valid = 1; \
     2146                c->x = a->x - b->x; \
     2147        } else {\
     2148                c->x ##_valid = 0;\
     2149        }
     2150        LIBTRACE_STAT_FIELDS
     2151#undef X
     2152}
     2153
     2154void trace_add_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b,
     2155                         libtrace_stat_t *c) {
     2156        assert(a->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2157               "trace_create_statistics() to allocate statistics");
     2158        assert(b->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2159               "trace_create_statistics() to allocate statistics");
     2160        assert(c->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2161               "trace_create_statistics() to allocate statistics");
     2162
     2163#define X(x) \
     2164        if (a->x ##_valid&& b->x ##_valid) { \
     2165                c->x ##_valid = 1; \
     2166                c->x = a->x + b->x; \
     2167        } else {\
     2168                c->x ##_valid = 0;\
     2169        }
     2170        LIBTRACE_STAT_FIELDS
     2171#undef X
     2172}
     2173
     2174int trace_print_statistics(const libtrace_stat_t *s, FILE *f, const char *format) {
     2175        assert(s->magic == LIBTRACE_STAT_MAGIC && "Please use"
     2176               "trace_create_statistics() to allocate statistics");
     2177        if (format == NULL)
     2178                format = "%s: %"PRIu64"\n";
     2179#define xstr(s) str(s)
     2180#define str(s) #s
     2181#define X(x) \
     2182        if (s->x ##_valid) { \
     2183                if (fprintf(f, format, xstr(x), s->x) < 0) \
     2184                        return -1; \
     2185        }
     2186        LIBTRACE_STAT_FIELDS
     2187#undef X
     2188        return 0;
     2189}
     2190
    19962191
    19972192void trace_clear_cache(libtrace_packet_t *packet) {
Note: See TracChangeset for help on using the changeset viewer.