2 * Copyright (c) 2001-2002 Luigi Rizzo
4 * Supported by: the Xorp Project (www.xorp.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
28 * $DragonFly: src/sys/kern/kern_poll.c,v 1.34 2007/09/30 04:37:27 sephe Exp $
31 #include "opt_polling.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h> /* needed by net/if.h */
37 #include <sys/sysctl.h>
39 #include <sys/thread2.h>
40 #include <sys/msgport2.h>
42 #include <net/if.h> /* for IFF_* flags */
43 #include <net/netisr.h> /* for NETISR_POLL */
44 #include <net/netmsg2.h>
47 * Polling support for [network] device drivers.
49 * Drivers which support this feature try to register with the
52 * If registration is successful, the driver must disable interrupts,
53 * and further I/O is performed through the handler, which is invoked
54 * (at least once per clock tick) with 3 arguments: the "arg" passed at
55 * register time (a struct ifnet pointer), a command, and a "count" limit.
57 * The command can be one of the following:
58 * POLL_ONLY: quick move of "count" packets from input/output queues.
59 * POLL_AND_CHECK_STATUS: as above, plus check status registers or do
60 * other more expensive operations. This command is issued periodically
61 * but less frequently than POLL_ONLY.
62 * POLL_DEREGISTER: deregister and return to interrupt mode.
63 * POLL_REGISTER: register and disable interrupts
65 * The first two commands are only issued if the interface is marked as
66 * 'IFF_UP, IFF_RUNNING and IFF_POLLING', the last two only if IFF_RUNNING
69 * The count limit specifies how much work the handler can do during the
70 * call -- typically this is the number of packets to be received, or
71 * transmitted, etc. (drivers are free to interpret this number, as long
72 * as the max time spent in the function grows roughly linearly with the
75 * Deregistration can be requested by the driver itself (typically in the
76 * *_stop() routine), or by the polling code, by invoking the handler.
78 * Polling can be globally enabled or disabled with the sysctl variable
79 * kern.polling.enable (default is 0, disabled)
81 * A second variable controls the sharing of CPU between polling/kernel
82 * network processing, and other activities (typically userlevel tasks):
83 * kern.polling.user_frac (between 0 and 100, default 50) sets the share
84 * of CPU allocated to user tasks. CPU is allocated proportionally to the
85 * shares, by dynamically adjusting the "count" (poll_burst).
87 * Other parameters can should be left to their default values.
88 * The following constraints hold
90 * 1 <= poll_each_burst <= poll_burst <= poll_burst_max
91 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
94 #define MIN_POLL_BURST_MAX 10
95 #define MAX_POLL_BURST_MAX 1000
97 #ifndef DEVICE_POLLING_FREQ_MAX
98 #define DEVICE_POLLING_FREQ_MAX 30000
100 #define DEVICE_POLLING_FREQ_DEFAULT 2000
102 #define POLL_LIST_LEN 128
107 #define POLLCTX_MAX 32
110 struct sysctl_ctx_list poll_sysctl_ctx;
111 struct sysctl_oid *poll_sysctl_tree;
114 uint32_t poll_each_burst;
115 uint32_t poll_burst_max;
119 uint32_t short_ticks;
121 uint32_t pending_polls;
126 struct timeval poll_start_t;
127 struct timeval prev_t;
129 uint32_t poll_handlers; /* next free entry in pr[]. */
130 struct pollrec pr[POLL_LIST_LEN];
133 struct systimer pollclock;
137 struct netmsg poll_netmsg;
138 struct netmsg poll_more_netmsg;
141 static struct pollctx *poll_context[POLLCTX_MAX];
143 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
144 "Device polling parameters");
146 static int poll_defcpu = -1;
147 SYSCTL_INT(_kern_polling, OID_AUTO, defcpu, CTLFLAG_RD,
148 &poll_defcpu, 0, "default CPU to run device polling");
150 static uint32_t poll_cpumask0 = 0xffffffff;
151 TUNABLE_INT("kern.polling.cpumask", &poll_cpumask0);
153 static uint32_t poll_cpumask;
154 SYSCTL_INT(_kern_polling, OID_AUTO, cpumask, CTLFLAG_RD,
155 &poll_cpumask, 0, "CPUs that can run device polling");
157 static int polling_enabled = 1; /* global polling enable */
158 TUNABLE_INT("kern.polling.enable", &polling_enabled);
160 static int pollhz = DEVICE_POLLING_FREQ_DEFAULT;
161 TUNABLE_INT("kern.polling.pollhz", &pollhz);
163 /* The two netisr handlers */
164 static void netisr_poll(struct netmsg *);
165 static void netisr_pollmore(struct netmsg *);
167 static void poll_register(struct netmsg *);
168 static void poll_deregister(struct netmsg *);
169 static void poll_sysctl_pollhz(struct netmsg *);
170 static void poll_sysctl_polling(struct netmsg *);
172 /* Systimer handler */
173 static void pollclock(systimer_t, struct intrframe *);
175 /* Sysctl handlers */
176 static int sysctl_pollhz(SYSCTL_HANDLER_ARGS);
177 static int sysctl_polling(SYSCTL_HANDLER_ARGS);
178 static void poll_add_sysctl(struct sysctl_ctx_list *,
179 struct sysctl_oid_list *, struct pollctx *);
181 static void schedpoll_oncpu(struct pollctx *, struct netmsg *, netisr_fn_t);
183 void init_device_poll_pcpu(int); /* per-cpu init routine */
186 * Initialize per-cpu polling(4) context. Called from kern_clock.c:
189 init_device_poll_pcpu(int cpuid)
191 struct pollctx *pctx;
194 if (cpuid >= POLLCTX_MAX)
197 if (((1 << cpuid) & poll_cpumask0) == 0)
200 poll_cpumask |= (1 << cpuid);
202 pctx = kmalloc(sizeof(*pctx), M_DEVBUF, M_WAITOK | M_ZERO);
204 pctx->poll_burst = 5;
205 pctx->poll_each_burst = 5;
206 pctx->poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
207 pctx->user_frac = 50;
209 pctx->polling_enabled = polling_enabled;
210 pctx->pollhz = pollhz;
211 pctx->poll_cpuid = cpuid;
212 netmsg_init(&pctx->poll_netmsg, &netisr_adone_rport, 0, NULL);
213 netmsg_init(&pctx->poll_more_netmsg, &netisr_adone_rport, 0, NULL);
215 KASSERT(cpuid < POLLCTX_MAX, ("cpu id must < %d", cpuid));
216 poll_context[cpuid] = pctx;
218 if (poll_defcpu < 0) {
222 * Initialize global sysctl nodes, for compat
224 poll_add_sysctl(NULL, SYSCTL_STATIC_CHILDREN(_kern_polling),
229 * Initialize per-cpu sysctl nodes
231 ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", pctx->poll_cpuid);
233 sysctl_ctx_init(&pctx->poll_sysctl_ctx);
234 pctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&pctx->poll_sysctl_ctx,
235 SYSCTL_STATIC_CHILDREN(_kern_polling),
236 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
237 poll_add_sysctl(&pctx->poll_sysctl_ctx,
238 SYSCTL_CHILDREN(pctx->poll_sysctl_tree), pctx);
241 * Initialize systimer
243 systimer_init_periodic_nq(&pctx->pollclock, pollclock, pctx, 1);
247 schedpoll(struct pollctx *pctx)
249 schedpoll_oncpu(pctx, &pctx->poll_netmsg, netisr_poll);
253 schedpollmore(struct pollctx *pctx)
255 schedpoll_oncpu(pctx, &pctx->poll_more_netmsg, netisr_pollmore);
259 * Set the polling frequency
262 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
264 struct pollctx *pctx = arg1;
270 error = sysctl_handle_int(oidp, &phz, 0, req);
271 if (error || req->newptr == NULL)
275 else if (phz > DEVICE_POLLING_FREQ_MAX)
276 phz = DEVICE_POLLING_FREQ_MAX;
278 netmsg_init(&msg, &curthread->td_msgport, 0, poll_sysctl_pollhz);
279 msg.nm_lmsg.u.ms_result = phz;
281 port = cpu_portfn(pctx->poll_cpuid);
282 lwkt_domsg(port, &msg.nm_lmsg, 0);
287 * Master enable. If polling is disabled, cut the polling systimer
291 sysctl_polling(SYSCTL_HANDLER_ARGS)
293 struct pollctx *pctx = arg1;
298 enabled = pctx->polling_enabled;
299 error = sysctl_handle_int(oidp, &enabled, 0, req);
300 if (error || req->newptr == NULL)
303 netmsg_init(&msg, &curthread->td_msgport, 0, poll_sysctl_polling);
304 msg.nm_lmsg.u.ms_result = enabled;
306 port = cpu_portfn(pctx->poll_cpuid);
307 lwkt_domsg(port, &msg.nm_lmsg, 0);
312 * Hook from hardclock. Tries to schedule a netisr, but keeps track
313 * of lost ticks due to the previous handler taking too long.
314 * Normally, this should not happen, because polling handler should
315 * run for a short time. However, in some cases (e.g. when there are
316 * changes in link status etc.) the drivers take a very long time
317 * (even in the order of milliseconds) to reset and reconfigure the
318 * device, causing apparent lost polls.
320 * The first part of the code is just for debugging purposes, and tries
321 * to count how often hardclock ticks are shorter than they should,
322 * meaning either stray interrupts or delayed events.
324 * WARNING! called from fastint or IPI, the MP lock might not be held.
327 pollclock(systimer_t info, struct intrframe *frame __unused)
329 struct pollctx *pctx = info->data;
333 if (pctx->poll_handlers == 0)
337 delta = (t.tv_usec - pctx->prev_t.tv_usec) +
338 (t.tv_sec - pctx->prev_t.tv_sec)*1000000;
339 if (delta * hz < 500000)
344 if (pctx->pending_polls > 100) {
346 * Too much, assume it has stalled (not always true
347 * see comment above).
350 pctx->pending_polls = 0;
354 if (pctx->phase <= 2) {
355 if (pctx->phase != 0)
361 if (pctx->pending_polls++ > 0)
366 * netisr_pollmore is called after other netisr's, possibly scheduling
367 * another NETISR_POLL call, or adapting the burst size for the next cycle.
369 * It is very bad to fetch large bursts of packets from a single card at once,
370 * because the burst could take a long time to be completely processed, or
371 * could saturate the intermediate queue (ipintrq or similar) leading to
372 * losses or unfairness. To reduce the problem, and also to account better for
373 * time spent in network-related processing, we split the burst in smaller
374 * chunks of fixed size, giving control to the other netisr's between chunks.
375 * This helps in improving the fairness, reducing livelock (because we
376 * emulate more closely the "process to completion" that we have with
377 * fastforwarding) and accounting for the work performed in low level
378 * handling and forwarding.
383 netisr_pollmore(struct netmsg *msg)
385 struct pollctx *pctx;
387 int kern_load, cpuid;
389 cpuid = mycpu->gd_cpuid;
390 KKASSERT(cpuid < POLLCTX_MAX);
392 pctx = poll_context[cpuid];
393 KKASSERT(pctx != NULL);
394 KKASSERT(pctx->poll_cpuid == cpuid);
395 KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
397 lwkt_replymsg(&msg->nm_lmsg, 0);
400 if (pctx->residual_burst > 0) {
402 /* will run immediately on return, followed by netisrs */
405 /* here we can account time spent in netisr's in this tick */
407 kern_load = (t.tv_usec - pctx->poll_start_t.tv_usec) +
408 (t.tv_sec - pctx->poll_start_t.tv_sec)*1000000; /* us */
409 kern_load = (kern_load * hz) / 10000; /* 0..100 */
410 if (kern_load > (100 - pctx->user_frac)) { /* try decrease ticks */
411 if (pctx->poll_burst > 1)
414 if (pctx->poll_burst < pctx->poll_burst_max)
418 pctx->pending_polls--;
419 if (pctx->pending_polls == 0) { /* we are done */
423 * Last cycle was long and caused us to miss one or more
424 * hardclock ticks. Restart processing again, but slightly
425 * reduce the burst size to prevent that this happens again.
427 pctx->poll_burst -= (pctx->poll_burst / 8);
428 if (pctx->poll_burst < 1)
429 pctx->poll_burst = 1;
436 * netisr_poll is scheduled by schednetisr when appropriate, typically once
439 * Note that the message is replied immediately in order to allow a new
440 * ISR to be scheduled in the handler.
442 * XXX each registration should indicate whether it needs a critical
443 * section to operate.
447 netisr_poll(struct netmsg *msg)
449 struct pollctx *pctx;
450 int i, cycles, cpuid;
451 enum poll_cmd arg = POLL_ONLY;
453 cpuid = mycpu->gd_cpuid;
454 KKASSERT(cpuid < POLLCTX_MAX);
456 pctx = poll_context[cpuid];
457 KKASSERT(pctx != NULL);
458 KKASSERT(pctx->poll_cpuid == cpuid);
459 KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
461 lwkt_replymsg(&msg->nm_lmsg, 0);
464 if (pctx->residual_burst == 0) { /* first call in this tick */
465 microuptime(&pctx->poll_start_t);
467 * Check that paremeters are consistent with runtime
468 * variables. Some of these tests could be done at sysctl
469 * time, but the savings would be very limited because we
470 * still have to check against reg_frac_count and
471 * poll_each_burst. So, instead of writing separate sysctl
472 * handlers, we do all here.
475 if (pctx->reg_frac > hz)
477 else if (pctx->reg_frac < 1)
479 if (pctx->reg_frac_count > pctx->reg_frac)
480 pctx->reg_frac_count = pctx->reg_frac - 1;
481 if (pctx->reg_frac_count-- == 0) {
482 arg = POLL_AND_CHECK_STATUS;
483 pctx->reg_frac_count = pctx->reg_frac - 1;
485 if (pctx->poll_burst_max < MIN_POLL_BURST_MAX)
486 pctx->poll_burst_max = MIN_POLL_BURST_MAX;
487 else if (pctx->poll_burst_max > MAX_POLL_BURST_MAX)
488 pctx->poll_burst_max = MAX_POLL_BURST_MAX;
490 if (pctx->poll_each_burst < 1)
491 pctx->poll_each_burst = 1;
492 else if (pctx->poll_each_burst > pctx->poll_burst_max)
493 pctx->poll_each_burst = pctx->poll_burst_max;
495 pctx->residual_burst = pctx->poll_burst;
497 cycles = (pctx->residual_burst < pctx->poll_each_burst) ?
498 pctx->residual_burst : pctx->poll_each_burst;
499 pctx->residual_burst -= cycles;
501 if (pctx->polling_enabled) {
502 for (i = 0 ; i < pctx->poll_handlers ; i++) {
503 struct ifnet *ifp = pctx->pr[i].ifp;
505 if (!lwkt_serialize_try(ifp->if_serializer))
508 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING|IFF_POLLING))
509 == (IFF_UP|IFF_RUNNING|IFF_POLLING))
510 ifp->if_poll(ifp, arg, cycles);
512 lwkt_serialize_exit(ifp->if_serializer);
514 } else { /* unregister */
515 for (i = 0 ; i < pctx->poll_handlers ; i++) {
516 struct ifnet *ifp = pctx->pr[i].ifp;
518 lwkt_serialize_enter(ifp->if_serializer);
520 if ((ifp->if_flags & IFF_POLLING) == 0) {
521 KKASSERT(ifp->if_poll_cpuid < 0);
522 lwkt_serialize_exit(ifp->if_serializer);
525 ifp->if_flags &= ~IFF_POLLING;
526 ifp->if_poll_cpuid = -1;
529 * Only call the interface deregistration
530 * function if the interface is still
533 if (ifp->if_flags & IFF_RUNNING)
534 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
536 lwkt_serialize_exit(ifp->if_serializer);
538 pctx->residual_burst = 0;
539 pctx->poll_handlers = 0;
546 poll_register(struct netmsg *msg)
548 struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
549 struct pollctx *pctx;
552 cpuid = mycpu->gd_cpuid;
553 KKASSERT(cpuid < POLLCTX_MAX);
555 pctx = poll_context[cpuid];
556 KKASSERT(pctx != NULL);
557 KKASSERT(pctx->poll_cpuid == cpuid);
559 if (pctx->polling_enabled == 0) {
560 /* Polling disabled, cannot register */
566 * Check if there is room.
568 if (pctx->poll_handlers >= POLL_LIST_LEN) {
570 * List full, cannot register more entries.
571 * This should never happen; if it does, it is probably a
572 * broken driver trying to register multiple times. Checking
573 * this at runtime is expensive, and won't solve the problem
574 * anyways, so just report a few times and then give up.
576 static int verbose = 10; /* XXX */
578 kprintf("poll handlers list full, "
579 "maybe a broken driver ?\n");
584 pctx->pr[pctx->poll_handlers].ifp = ifp;
585 pctx->poll_handlers++;
588 if (pctx->poll_handlers == 1) {
589 KKASSERT(pctx->polling_enabled);
590 systimer_adjust_periodic(&pctx->pollclock,
595 lwkt_replymsg(&msg->nm_lmsg, rc);
599 * Try to register routine for polling. Returns 1 if successful
600 * (and polling should be enabled), 0 otherwise.
602 * Called from mainline code only, not called from an interrupt.
605 ether_poll_register(struct ifnet *ifp)
609 KKASSERT(poll_defcpu < POLLCTX_MAX);
611 return ether_pollcpu_register(ifp, poll_defcpu);
615 ether_pollcpu_register(struct ifnet *ifp, int cpuid)
621 if (ifp->if_poll == NULL) {
622 /* Device does not support polling */
626 if (cpuid < 0 || cpuid >= POLLCTX_MAX)
629 if (((1 << cpuid) & poll_cpumask) == 0) {
630 /* Polling is not supported on 'cpuid' */
633 KKASSERT(poll_context[cpuid] != NULL);
636 * Attempt to register. Interlock with IFF_POLLING.
638 crit_enter(); /* XXX MP - not mp safe */
640 lwkt_serialize_enter(ifp->if_serializer);
641 if (ifp->if_flags & IFF_POLLING) {
642 /* Already polling */
643 KKASSERT(ifp->if_poll_cpuid >= 0);
644 lwkt_serialize_exit(ifp->if_serializer);
648 KKASSERT(ifp->if_poll_cpuid < 0);
649 ifp->if_flags |= IFF_POLLING;
650 ifp->if_poll_cpuid = cpuid;
651 if (ifp->if_flags & IFF_RUNNING)
652 ifp->if_poll(ifp, POLL_REGISTER, 0);
653 lwkt_serialize_exit(ifp->if_serializer);
655 netmsg_init(&msg, &curthread->td_msgport, 0, poll_register);
656 msg.nm_lmsg.u.ms_resultp = ifp;
658 port = cpu_portfn(cpuid);
659 lwkt_domsg(port, &msg.nm_lmsg, 0);
661 if (msg.nm_lmsg.ms_error) {
662 lwkt_serialize_enter(ifp->if_serializer);
663 ifp->if_flags &= ~IFF_POLLING;
664 ifp->if_poll_cpuid = -1;
665 if (ifp->if_flags & IFF_RUNNING)
666 ifp->if_poll(ifp, POLL_DEREGISTER, 0);
667 lwkt_serialize_exit(ifp->if_serializer);
678 poll_deregister(struct netmsg *msg)
680 struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
681 struct pollctx *pctx;
684 cpuid = mycpu->gd_cpuid;
685 KKASSERT(cpuid < POLLCTX_MAX);
687 pctx = poll_context[cpuid];
688 KKASSERT(pctx != NULL);
689 KKASSERT(pctx->poll_cpuid == cpuid);
691 for (i = 0 ; i < pctx->poll_handlers ; i++) {
692 if (pctx->pr[i].ifp == ifp) /* Found it */
695 if (i == pctx->poll_handlers) {
696 kprintf("ether_poll_deregister: ifp not found!!!\n");
699 pctx->poll_handlers--;
700 if (i < pctx->poll_handlers) {
701 /* Last entry replaces this one. */
702 pctx->pr[i].ifp = pctx->pr[pctx->poll_handlers].ifp;
705 if (pctx->poll_handlers == 0)
706 systimer_adjust_periodic(&pctx->pollclock, 1);
709 lwkt_replymsg(&msg->nm_lmsg, rc);
713 * Remove interface from the polling list. Occurs when polling is turned
714 * off. Called from mainline code only, not called from an interrupt.
717 ether_poll_deregister(struct ifnet *ifp)
723 KKASSERT(ifp != NULL);
725 if (ifp->if_poll == NULL)
730 lwkt_serialize_enter(ifp->if_serializer);
731 if ((ifp->if_flags & IFF_POLLING) == 0) {
732 KKASSERT(ifp->if_poll_cpuid < 0);
733 lwkt_serialize_exit(ifp->if_serializer);
738 cpuid = ifp->if_poll_cpuid;
739 KKASSERT(cpuid >= 0);
740 KKASSERT(poll_context[cpuid] != NULL);
742 ifp->if_flags &= ~IFF_POLLING;
743 ifp->if_poll_cpuid = -1;
744 lwkt_serialize_exit(ifp->if_serializer);
746 netmsg_init(&msg, &curthread->td_msgport, 0, poll_deregister);
747 msg.nm_lmsg.u.ms_resultp = ifp;
749 port = cpu_portfn(cpuid);
750 lwkt_domsg(port, &msg.nm_lmsg, 0);
752 if (!msg.nm_lmsg.ms_error) {
753 lwkt_serialize_enter(ifp->if_serializer);
754 if (ifp->if_flags & IFF_RUNNING)
755 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
756 lwkt_serialize_exit(ifp->if_serializer);
767 poll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
768 struct pollctx *pctx)
770 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "enable",
771 CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_polling,
772 "I", "Polling enabled");
774 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
775 CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_pollhz,
776 "I", "Device polling frequency");
778 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
779 &pctx->phase, 0, "Polling phase");
781 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
782 &pctx->suspect, 0, "suspect event");
784 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
785 &pctx->stalled, 0, "potential stalls");
787 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RW,
788 &pctx->poll_burst, 0, "Current polling burst size");
790 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "each_burst", CTLFLAG_RW,
791 &pctx->poll_each_burst, 0, "Max size of each burst");
793 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst_max", CTLFLAG_RW,
794 &pctx->poll_burst_max, 0, "Max Polling burst size");
796 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
798 "Desired user fraction of cpu time");
800 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "reg_frac", CTLFLAG_RW,
802 "Every this many cycles poll register");
804 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
805 &pctx->short_ticks, 0,
806 "Hardclock ticks shorter than they should be");
808 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
809 &pctx->lost_polls, 0,
810 "How many times we would have lost a poll tick");
812 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
813 &pctx->pending_polls, 0, "Do we need to poll again");
815 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RW,
816 &pctx->residual_burst, 0,
817 "# of residual cycles in burst");
819 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
820 &pctx->poll_handlers, 0,
821 "Number of registered poll handlers");
825 schedpoll_oncpu(struct pollctx *pctx, struct netmsg *msg, netisr_fn_t handler)
827 if (msg->nm_lmsg.ms_flags & MSGF_DONE) {
830 netmsg_init(msg, &netisr_adone_rport, 0, handler);
832 msg->nm_lmsg.u.ms_resultp = pctx;
834 port = cpu_portfn(mycpu->gd_cpuid);
835 lwkt_sendmsg(port, &msg->nm_lmsg);
840 poll_sysctl_pollhz(struct netmsg *msg)
842 struct pollctx *pctx;
845 cpuid = mycpu->gd_cpuid;
846 KKASSERT(cpuid < POLLCTX_MAX);
848 pctx = poll_context[cpuid];
849 KKASSERT(pctx != NULL);
850 KKASSERT(pctx->poll_cpuid == cpuid);
852 pctx->pollhz = msg->nm_lmsg.u.ms_result;
853 if (pctx->polling_enabled && pctx->poll_handlers)
854 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
855 lwkt_replymsg(&msg->nm_lmsg, 0);
859 poll_sysctl_polling(struct netmsg *msg)
861 struct pollctx *pctx;
864 cpuid = mycpu->gd_cpuid;
865 KKASSERT(cpuid < POLLCTX_MAX);
867 pctx = poll_context[cpuid];
868 KKASSERT(pctx != NULL);
869 KKASSERT(pctx->poll_cpuid == cpuid);
871 pctx->polling_enabled = msg->nm_lmsg.u.ms_result;
872 if (pctx->polling_enabled && pctx->poll_handlers)
873 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
875 systimer_adjust_periodic(&pctx->pollclock, 1);
876 lwkt_replymsg(&msg->nm_lmsg, 0);