File: leaf.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (289 lines) | stat: -rw-r--r-- 6,254 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 1998 The University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Implements pfq_leaf operations.
 *
 * The user entry-points need to disable interrupts around tree-modifying
 * operations since the transmit-done interrupt, which calls reset-path,
 * can happen anytime.
 */

#include <oskit/hpfq.h>
#include <oskit/io/netio.h>
#include <oskit/c/assert.h>
#include <oskit/c/stdlib.h>
#include <oskit/com/queue.h>
#include <oskit/dev/dev.h>

#include "hpfq_impl.h"


/* The special implementation of oskit_netio_t for PFQ leaf nodes. */

struct netio_impl {
	oskit_netio_t	n_ioi;
	oskit_u32_t	n_count;

	struct pfq_impl	*n_leaf;
};

static OSKIT_COMDECL
netio_query(oskit_netio_t *_, const oskit_iid_t *iid, void **out_ihandle)
{ 
	struct netio_impl *n = (void *)_;

	assert(n && n->n_count);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_netio_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &n->n_ioi;
                ++n->n_count;
                return 0;
        }
 
        *out_ihandle = NULL; 
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
netio_addref(oskit_netio_t *_)
{
	struct netio_impl *n = (void *)_;

	assert(n && n->n_count);

        return ++n->n_count;
}

static OSKIT_COMDECL_U 
netio_release(oskit_netio_t *_)
{
	struct netio_impl *n = (void *)_;
 
	assert(n && n->n_count);

	if (--n->n_count)
		return n->n_count;

	free(n);

	return 0;
}               

/*
 * This is what you're looking for.
 * It does the ARRIVE(i, Packet) algorithm from the paper.
 */
static OSKIT_COMDECL
netio_push(oskit_netio_t *_, oskit_bufio_t *b, oskit_size_t size)
{
	struct netio_impl *n = (void *)_;
	struct pfq_impl *p = n->n_leaf;
	oskit_error_t err;
 
	assert(n && n->n_count);

	osenv_intr_disable();

	pfq_stats.arrived++;

	/* Append to end of physical queue. */
	err = oskit_queue_enqueue(p->p_packq, b, 0);
	if (err == OSKIT_E_FAIL)
		pfq_stats.qfull++;

	/* Done if p_logicalq is non-empty. */
	if (p->p_logicalq)
		goto done;

	/* Otherwise, this packet becomes the head of the p_logicalq queue. */
	p->p_logicalq = b;
	oskit_bufio_addref(b);

	/* Update start and finish times. */
	_pfq_update_start_finish(p, 0);

	/* Restart our parent if they're not watching T.V. */
	if (! p->p_parent->p_busy)
		_pfq_restart_node(p->p_parent);

 done:
	osenv_intr_enable();
	return 0;
}

static struct oskit_netio_ops leaf_netio = {
	netio_query,
	netio_addref,
	netio_release,
	netio_push
};


/* The pfq_leaf_t COM methods. */

static OSKIT_COMDECL
leaf_query(pfq_leaf_t *_, const oskit_iid_t *iid, void **out_ihandle)
{ 
	struct pfq_impl *p = (void *)_;

	assert(p && p->p_count);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &pfq_leaf_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &p->p_ioi;
                ++p->p_count;
                return 0;
        }
 
        *out_ihandle = NULL; 
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
leaf_addref(pfq_leaf_t *_)
{
	struct pfq_impl *p = (void *)_;

	assert(p && p->p_count);

        return ++p->p_count;
}

static OSKIT_COMDECL_U 
leaf_release(pfq_leaf_t *_)
{
	struct pfq_impl *p = (void *)_;
 
	assert(p && p->p_count);

	if (--p->p_count)
		return p->p_count;

	oskit_queue_release(p->p_packq);
	free(p);

	return 0;
}               

static OSKIT_COMDECL
leaf_get_netio(pfq_leaf_t *_, oskit_netio_t **out_netio)
{
	struct pfq_impl *p = (void *)_;
	struct netio_impl *n;
 
	assert(p && p->p_count);

	/* Must have been parented. */
	if (p->p_parent == NULL)
		return OSKIT_E_INVALIDARG;

	n = malloc(sizeof *n);
	if (n == NULL)
		return OSKIT_E_OUTOFMEMORY;

	n->n_ioi.ops = &leaf_netio;
	n->n_count = 1;
	n->n_leaf = p;

	*out_netio = &n->n_ioi;
	return 0;
}

static OSKIT_COMDECL
notimpl(void)
{
	return OSKIT_E_NOTIMPL;
}

static struct pfq_leaf_ops leaf_ops = {
	leaf_query,
	leaf_addref,
	leaf_release,
	(void *)notimpl,		/* add_child */
	(void *)notimpl,		/* remove_child */
	(void *)notimpl,		/* set_share */
	leaf_get_netio
};

/*
 * See RESET-PATH(n) in the paper.
 * This is the implementation for leaf nodes.
 * There is a different one for schedulers.
 */
static void
leaf_reset_path(struct pfq_impl *p)
{
	oskit_bufio_t *b;

	assert(p->p_logicalq);
	oskit_bufio_release(p->p_logicalq);
	p->p_logicalq = NULL;

	/* Dequeue and restart. */
	oskit_queue_dequeue(p->p_packq, &b);
	oskit_bufio_release(b);
	if (oskit_queue_size(p->p_packq) != 0) {
		oskit_queue_front(p->p_packq, &p->p_logicalq);
		_pfq_update_start_finish(p, 1);
	}
	_pfq_restart_node(p->p_parent);
}


oskit_error_t
pfq_leaf_create(pfq_leaf_t **out_leaf)
{
	struct pfq_impl *p;
	oskit_error_t err;

	/*
	 * This leaf isn't part of the tree yet -- so we don't need to
	 * disable interrupts.
	 */

	p = malloc(sizeof *p);
	if (p == NULL)
		return OSKIT_E_OUTOFMEMORY;

	/*
	 * Set up some reasonable defaults.
	 */
	p->p_ioi.ops = (struct pfq_sched_ops *)&leaf_ops;
	p->p_count = 1;
	p->p_link = NULL;
	p->p_active_child = NULL;
	p->p_byte_charge = PFQ_TOTAL_SHARE/1;		/* default to 100% */
	p->p_busy = 0;
	p->p_parent = NULL;
	p->p_logicalq = NULL;
	p->p_vtime = 0;
	p->p_stime = 0;
	p->p_ftime = 0;
	p->p_reset_path = leaf_reset_path;
	p->p_update_queue = NULL;

	err = oskit_bounded_com_queue_create(PFQ_LEAF_QUEUE_LENGTH,
					     &p->p_packq);
	if (err) {
		free(p);
		return err;
	}

	*out_leaf = (pfq_leaf_t *)&p->p_ioi;
	return 0;
}