Module Name:    src
Committed By:   pgoyette
Date:           Sat Feb 22 19:54:35 UTC 2020

Modified Files:
        src/tests/modules: t_modctl.c
        src/tests/modules/k_helper: k_helper.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/tests/modules/t_modctl.c
cvs rdiff -u -r1.6 -r1.7 src/tests/modules/k_helper/k_helper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/modules/t_modctl.c
diff -u src/tests/modules/t_modctl.c:1.15 src/tests/modules/t_modctl.c:1.16
--- src/tests/modules/t_modctl.c:1.15	Sat Feb 22 00:24:15 2020
+++ src/tests/modules/t_modctl.c	Sat Feb 22 19:54:34 2020
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v
 #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 @@ k_helper_is_present_sysctl(void)
 
 /*
  * 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 @@ k_helper_is_present(enum presence_check 
 	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 @@ k_helper_is_present(enum presence_check 
 		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_HEAD(cmd_stat, tc)
 }
 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 @@ ATF_TC_BODY(cmd_stat, tc)
 	}
 	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)
 {

Index: src/tests/modules/k_helper/k_helper.c
diff -u src/tests/modules/k_helper/k_helper.c:1.6 src/tests/modules/k_helper/k_helper.c:1.7
--- src/tests/modules/k_helper/k_helper.c:1.6	Sun Jun  3 10:59:44 2012
+++ src/tests/modules/k_helper/k_helper.c	Sat Feb 22 19:54:35 2020
@@ -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 @@ MODULE(MODULE_CLASS_MISC, k_helper, NULL
 /* 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 int prop_int_ok;
 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 @@ k_helper_init(prop_dictionary_t props)
 	} else
 		prop_int_load = -2;
 
-	sysctl_k_helper_setup(&clogp);
-
 	return 0;
 }
 
@@ -173,8 +176,6 @@ int
 k_helper_fini(void *arg)
 {
 
-	sysctl_teardown(&clogp);
-
 	return 0;
 }
 

Reply via email to