Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/modules Update the t_modctl test to ensure that static...



details:   https://anonhg.NetBSD.org/src/rev/2469f8a097bf
branches:  trunk
changeset: 1007553:2469f8a097bf
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Sat Feb 22 19:54:34 2020 +0000

description:
Update the t_modctl test to ensure that static evcnts are added.

While here, remove the explicit call to sysctl_setup() routine,
since the module infrastructure already invokes such routines
automatically.

diffstat:

 tests/modules/k_helper/k_helper.c |  15 ++++---
 tests/modules/t_modctl.c          |  71 +++++++++++++++++++++++++++++++++++---
 2 files changed, 72 insertions(+), 14 deletions(-)

diffs (205 lines):

diff -r 5322edecedda -r 2469f8a097bf tests/modules/k_helper/k_helper.c
--- a/tests/modules/k_helper/k_helper.c Sat Feb 22 19:51:57 2020 +0000
+++ b/tests/modules/k_helper/k_helper.c Sat Feb 22 19:54:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: k_helper.c,v 1.6 2012/06/03 10:59:44 dsl Exp $ */
+/*     $NetBSD: k_helper.c,v 1.7 2020/02/22 19:54:35 pgoyette Exp $    */
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -27,12 +27,13 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: k_helper.c,v 1.6 2012/06/03 10:59:44 dsl Exp $");
+__KERNEL_RCSID(0, "$NetBSD: k_helper.c,v 1.7 2020/02/22 19:54:35 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
 #include <sys/module.h>
 #include <sys/sysctl.h>
+#include <sys/evcnt.h>
 
 #include <prop/proplib.h>
 
@@ -45,7 +46,6 @@
 /* TODO: Change the integer variables below that represent booleans to
  * bools, once sysctl(8) supports CTLTYPE_BOOL nodes. */
 
-static struct sysctllog *clogp;
 static int present = 1;
 static int prop_str_ok;
 static char prop_str_val[128];
@@ -53,6 +53,11 @@
 static int64_t prop_int_val;
 static int prop_int_load;
 
+static struct evcnt my_counter =
+    EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "k_helper", "my_counter");
+
+EVCNT_ATTACH_STATIC(my_counter);
+
 #define K_HELPER 0x12345678
 #define K_HELPER_PRESENT 0
 #define K_HELPER_PROP_STR_OK 1
@@ -163,8 +168,6 @@
        } else
                prop_int_load = -2;
 
-       sysctl_k_helper_setup(&clogp);
-
        return 0;
 }
 
@@ -173,8 +176,6 @@
 k_helper_fini(void *arg)
 {
 
-       sysctl_teardown(&clogp);
-
        return 0;
 }
 
diff -r 5322edecedda -r 2469f8a097bf tests/modules/t_modctl.c
--- a/tests/modules/t_modctl.c  Sat Feb 22 19:51:57 2020 +0000
+++ b/tests/modules/t_modctl.c  Sat Feb 22 19:54:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_modctl.c,v 1.15 2020/02/22 00:24:15 kamil Exp $      */
+/*     $NetBSD: t_modctl.c,v 1.16 2020/02/22 19:54:34 pgoyette Exp $   */
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v 1.15 2020/02/22 00:24:15 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v 1.16 2020/02/22 19:54:34 pgoyette Exp $");
 
 #include <sys/module.h>
 #include <sys/sysctl.h>
@@ -39,18 +39,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/evcnt.h>
 
 #include <prop/proplib.h>
 
 #include <atf-c.h>
 
-enum presence_check { both_checks, stat_check, sysctl_check };
+enum presence_check { all_checks, stat_check, sysctl_check, evcnt_check };
 
 static void    check_permission(void);
 static bool    get_modstat_info(const char *, modstat_t *);
 static bool    get_sysctl(const char *, void *buf, const size_t);
 static bool    k_helper_is_present_stat(void);
 static bool    k_helper_is_present_sysctl(void);
+static bool    k_helper_is_present_evcnt(void);
 static bool    k_helper_is_present(enum presence_check);
 static int     load(prop_dictionary_t, bool, const char *, ...);
 static int     unload(const char *, bool);
@@ -173,6 +175,56 @@
 
 /*
  * Returns a boolean indicating if the k_helper module was loaded
+ * successfully.  This implementation uses the module's evcnt
+ * to do the check.
+ */
+static bool
+k_helper_is_present_evcnt(void)
+{
+       const int mib[4] = {CTL_KERN, KERN_EVCNT, EVCNT_TYPE_ANY,
+           KERN_EVCNT_COUNT_ANY };
+       int error;
+       size_t newlen, buflen = 0;
+       void *buf0, *buf = NULL;
+       const struct evcnt_sysctl *evs, *last_evs;
+
+       for (;;) {
+               if (buflen)
+                       buf = malloc(buflen);
+               error = sysctl(mib, __arraycount(mib), buf, &newlen, NULL, 0);
+               if (error) {
+                       if (buf)
+                               free(buf);
+                       return false;
+               }
+               if (newlen <= buflen) {
+                       buflen = newlen;
+                       break;
+               }
+               if (buf)
+                       free(buf);
+               buflen = newlen;
+       }
+       evs = buf0 = buf;
+       last_evs = (void *)((char *)buf + buflen);
+       buflen /= sizeof(uint64_t);
+       while (evs < last_evs
+           && buflen >= sizeof(*evs)/sizeof(uint64_t)
+           && buflen >= evs->ev_len) {
+               if ( strncmp(evs->ev_strings, "k_helper", evs->ev_grouplen)
+                   == 0) {
+                       free(buf);
+                       return true;
+               }
+               buflen -= evs->ev_len;
+               evs = (const void *)((const uint64_t *)evs + evs->ev_len);
+       }
+       free(buf);
+       return false;
+}
+
+/*
+ * Returns a boolean indicating if the k_helper module was loaded
  * successfully.  The 'how' parameter specifies the implementation to
  * use to do the check.
  */
@@ -182,9 +234,10 @@
        bool found;
 
        switch (how) {
-       case both_checks:
+       case all_checks:
                found = k_helper_is_present_stat();
                ATF_CHECK(k_helper_is_present_sysctl() == found);
+               ATF_CHECK(k_helper_is_present_evcnt() == found);
                break;
 
        case stat_check:
@@ -195,6 +248,10 @@
                found = k_helper_is_present_sysctl();
                break;
 
+       case evcnt_check:
+               found = k_helper_is_present_evcnt();
+               break;
+
        default:
                found = false;
                assert(found);
@@ -435,11 +492,11 @@
 }
 ATF_TC_BODY(cmd_stat, tc)
 {
-       ATF_CHECK(!k_helper_is_present(both_checks));
+       ATF_CHECK(!k_helper_is_present(all_checks));
 
        load(NULL, true, "%s/k_helper/k_helper.kmod",
            atf_tc_get_config_var(tc, "srcdir"));
-       ATF_CHECK(k_helper_is_present(both_checks));
+       ATF_CHECK(k_helper_is_present(all_checks));
        {
                modstat_t ms;
                ATF_CHECK(get_modstat_info("k_helper", &ms));
@@ -450,7 +507,7 @@
        }
        unload("k_helper", true);
 
-       ATF_CHECK(!k_helper_is_present(both_checks));
+       ATF_CHECK(!k_helper_is_present(all_checks));
 }
 ATF_TC_CLEANUP(cmd_stat, tc)
 {



Home | Main Index | Thread Index | Old Index