[ovs-dev] [PATCH v4 1/2] coverage: Add command for reading counter value

Han Zhou zhouhan at gmail.com
Thu Jun 7 17:00:48 UTC 2018


On Thu, Jun 7, 2018 at 3:38 AM, Jakub Sitnicki <jkbs at redhat.com> wrote:
>
> Facilitate checking coverage counters from scripts and tests with a new
> "coverage/read-counter" command that gets the total count for a counter.
>
> Same could be achieved by scraping the output of "coverage/show" command
> but the difficulties there are that output is in human readable format
> and zero-value counters are not listed.
>
> Signed-off-by: Jakub Sitnicki <jkbs at redhat.com>
> ---
>  lib/coverage-unixctl.man |  2 ++
>  lib/coverage.c           | 42 ++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>
> diff --git a/lib/coverage-unixctl.man b/lib/coverage-unixctl.man
> index 8e5df818e..81eb93902 100644
> --- a/lib/coverage-unixctl.man
> +++ b/lib/coverage-unixctl.man
> @@ -11,3 +11,5 @@ debugging.
>  Displays the averaged per-second rates for the last few seconds, the
>  last minute and the last hour, and the total counts of all of the
>  coverage counters.
> +.IP "\fBcoverage/read-counter\fR \fIcounter\fR"
> +Displays the total count for the given coverage \fIcounter\fR.
> diff --git a/lib/coverage.c b/lib/coverage.c
> index 6cef82614..a95b6aa25 100644
> --- a/lib/coverage.c
> +++ b/lib/coverage.c
> @@ -44,6 +44,8 @@ static unsigned int idx_count = 0;
>  static void coverage_read(struct svec *);
>  static unsigned int coverage_array_sum(const unsigned int *arr,
>                                         const unsigned int len);
> +static bool coverage_read_counter(const char *name,
> +                                  unsigned long long int *count);
>
>  /* Registers a coverage counter with the coverage core */
>  void
> @@ -72,11 +74,32 @@ coverage_unixctl_show(struct unixctl_conn *conn, int
argc OVS_UNUSED,
>      svec_destroy(&lines);
>  }
>
> +static void
> +coverage_unixctl_read_counter(struct unixctl_conn *conn, int argc
OVS_UNUSED,
> +                              const char *argv[], void *aux OVS_UNUSED)
> +{
> +    unsigned long long count;
> +    char *reply;
> +    bool ok;
> +
> +    ok = coverage_read_counter(argv[1], &count);
> +    if (!ok) {
> +        unixctl_command_reply_error(conn, "No such counter");
> +        return;
> +    }
> +
> +    reply = xasprintf("%llu\n", count);
> +    unixctl_command_reply(conn, reply);
> +    free(reply);
> +}
> +
>  void
>  coverage_init(void)
>  {
>      unixctl_command_register("coverage/show", "", 0, 0,
>                               coverage_unixctl_show, NULL);
> +    unixctl_command_register("coverage/read-counter", "COUNTER", 1, 1,
> +                             coverage_unixctl_read_counter, NULL);
>  }
>
>  /* Sorts coverage counters in descending order by total, within equal
> @@ -372,3 +395,22 @@ coverage_array_sum(const unsigned int *arr, const
unsigned int len)
>      ovs_mutex_unlock(&coverage_mutex);
>      return sum;
>  }
> +
> +static bool
> +coverage_read_counter(const char *name, unsigned long long int *count)
> +{
> +    for (size_t i = 0; i < n_coverage_counters; i++) {
> +        struct coverage_counter *c = coverage_counters[i];
> +
> +        if (!strcmp(c->name, name)) {
> +            ovs_mutex_lock(&coverage_mutex);
> +            c->total += c->count();
> +            *count = c->total;
> +            ovs_mutex_unlock(&coverage_mutex);
> +
> +            return true;
> +        }
> +    }
> +
> +    return false;
> +}
> --
> 2.14.4
>

Acked-by: Han Zhou <hzhou8 at ebay.com>


More information about the dev mailing list