summaryrefslogtreecommitdiffstats
path: root/src/mpq.h
blob: f32bf7571fb3a8d465f1db69ffb3fa17538bcc6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _MPQ_H_
#define _MPQ_H_

#include <sys/param.h>
#include <sys/kthread.h>
#include <sys/mbuf.h>
#include <sys/systm.h>

struct mpq {
	uint mpq_sactive;
	size_t mpq_maxlen;
	size_t mpq_nthreads;
	struct mutex mpq_mtx;
	struct mbuf *mpq_cursor;
	struct mbuf_list mpq_list;
	struct mbuf *(*mpq_parallel_fn)(struct mbuf *);
	void (*mpq_serial_fn)(struct mbuf *);
};

void mpq_init(struct mpq *, int, int, struct mbuf *(*)(struct mbuf *), void (*)(struct mbuf *));
void mpq_barrier(struct mpq *);
void mpq_destroy(struct mpq *);
int mpq_enqueue(struct mpq *, struct mbuf *);
int mpq_load(struct mpq *);

#define MPQ_DEFAULTLEN 1024

/* Internal functions */
void
_mpq_thread(struct mpq *mpq)
{
	struct mbuf *mc;

	while (1) {
		/* dequeue */
		mtx_enter(&mpq->mpq_mtx);
		if ((mc = mpq->mpq_cursor) == NULL) {

			/* check if mpq is dead */
			if (mpq->mpq_parallel_fn == NULL) {
				mpq->mpq_nthreads--;
				mtx_leave(&mpq->mpq_mtx);
				wakeup(&mpq->mpq_nthreads);
				kthread_exit(0);
			}

			/* sleep */
			mtx_leave(&mpq->mpq_mtx);
			tsleep(&mpq->mpq_cursor, PSOCK, "bored", 0);
			continue;
		}
		mpq->mpq_cursor = mc->m_nextpkt;
		mtx_leave(&mpq->mpq_mtx);

		/* run time consuming function in parallel */
		if (mpq->mpq_parallel_fn != NULL && mc->m_next != NULL)
			mc->m_next = mpq->mpq_parallel_fn(mc->m_next);

		/* signal packet processed */
		mc->m_flags |= M_PROTO1;

		if (atomic_cas_uint(&mpq->mpq_sactive, 0, 1) == 1)
			continue;

		/* run function in queue order */
		while (1) {
			/* check if head is ready to process */
			mtx_enter(&mpq->mpq_mtx);
			mc = mpq->mpq_list.ml_head;
			if (mc == NULL || ~mc->m_flags & M_PROTO1) {
				mtx_leave(&mpq->mpq_mtx);
				break;
			}
			mc = ml_dequeue(&mpq->mpq_list);
			mtx_leave(&mpq->mpq_mtx);

			/* process */
			if (mpq->mpq_serial_fn != NULL && mc->m_next != NULL) {
				mpq->mpq_serial_fn(mc->m_next);
				mc->m_next = NULL;
			}

			m_freem(mc);
		}

		atomic_swap_uint(&mpq->mpq_sactive, 0);
	}
}

/* External functions */
void
mpq_init(struct mpq *mpq, int ipl, int maxlen, struct mbuf *(*parallel)(struct mbuf *), void (*serial)(struct mbuf *))
{
	assert(parallel != NULL);

	mpq->mpq_sactive = 0;
	mpq->mpq_maxlen = maxlen;
	mtx_init(&mpq->mpq_mtx, ipl);
	ml_init(&mpq->mpq_list);
	mpq->mpq_cursor = NULL;
	mpq->mpq_parallel_fn = parallel;
	mpq->mpq_serial_fn = serial;

	for(mpq->mpq_nthreads = 0; mpq->mpq_nthreads < ncpus; mpq->mpq_nthreads++)
		kthread_create((void (*)(void *))_mpq_thread, mpq, NULL, "wg");
}

void
mpq_barrier(struct mpq *mpq)
{
	size_t maxlen, len;
	mtx_enter(&mpq->mpq_mtx);
	maxlen = mpq->mpq_maxlen;
	mpq->mpq_maxlen = 0;
	mtx_leave(&mpq->mpq_mtx);

	do {
		mtx_enter(&mpq->mpq_mtx);
		len = ml_len(&mpq->mpq_list);
		mtx_leave(&mpq->mpq_mtx);
		tsleep(&mpq->mpq_list, PWAIT, "wg_barrier", 1);
	} while (len > 0);

	mtx_enter(&mpq->mpq_mtx);
	mpq->mpq_maxlen = maxlen;
	mtx_leave(&mpq->mpq_mtx);
}

void
mpq_destroy(struct mpq *mpq)
{
	mpq->mpq_serial_fn = NULL;
	mpq->mpq_parallel_fn = NULL;
	wakeup(&mpq->mpq_cursor);
	/* TODO timo */
	while (mpq->mpq_nthreads > 0)
		tsleep(&mpq->mpq_nthreads, PWAIT, "wg_destroy", 0);
}

int
mpq_enqueue(struct mpq *mpq, struct mbuf *m)
{
	int dropped = 0;
	struct mbuf *mc = m_get(M_WAITOK, MT_CONTROL);
	mc->m_next = m;

	mtx_enter(&mpq->mpq_mtx);
	if (ml_len(&mpq->mpq_list) < mpq->mpq_maxlen) {
		ml_enqueue(&mpq->mpq_list, mc);
		if (mpq->mpq_cursor == NULL)
			mpq->mpq_cursor = mc;
	} else {
		dropped = 1;
	}
	mtx_leave(&mpq->mpq_mtx);

	if (dropped)
		m_freem(mc);
	else
		wakeup_one(&mpq->mpq_cursor);

	return dropped;
}

int
mpq_load(struct mpq *mpq)
{
	return 100 * ml_len(&mpq->mpq_list) / mpq->mpq_maxlen;
}

#endif /* _MPQ_H_ */