summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rcap/rcapd/rcapd_collection.c
blob: b6072d37fe0d33c53f39566b55d2f619566e1397 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "rcapd.h"
#include "utils.h"

/*
 * An abstract "collection" of processes.  Multiple types of collections can
 * exist, one of which is selected at run-time.  Currently, the only one
 * defined corresponds to project(5)s.
 */

#define	MAX(x, y) (((x) > (y)) ? (x) : (y))

typedef struct {
	rcid_t		*lfa_colidp;
	lcollection_t	*lfa_found;
} lcollection_find_arg_t;

extern void lcollection_update_project(lcollection_update_type_t,
    void(*)(char *, char *, int, uint64_t, int));
extern void lcollection_update_zone(lcollection_update_type_t,
    void(*)(char *, char *, int, uint64_t, int));
static void lcollection_update_notification_cb(char *, char *, int, uint64_t,
    int);

rcid_t(*rc_getidbypsinfo)(psinfo_t *);
uint64_t phys_total = 0;
static lcollection_t *lcollection_head = NULL;

void
lcollection_update(lcollection_update_type_t ut)
{
	lcollection_update_zone(ut, lcollection_update_notification_cb);
	lcollection_update_project(ut, lcollection_update_notification_cb);
}

/*
 * Inserts a collection with the supplied identity, or updates the caps of an
 * existing one.  The return value will have these bits set, depending on the
 * previous and new cap values.  If no cap was displaced, and the requested cap
 * is 0, no collection will be added, and the applicable *ZERO flags will be
 * set.
 *
 *	LCST_CAP_CHANGED
 *	LCST_CAP_REMOVED
 *	LCSS_CAP_ZERO
 */
lcollection_t *
lcollection_insert_update(rcid_t *colidp, uint64_t rss_cap, char *name,
    int *changes)
{
	lcollection_t *lcol;

	*changes = 0;

	if (rss_cap == 0)
		*changes |= LCST_CAP_ZERO;

	lcol = lcollection_find(colidp);

	/*
	 * If the specified collection is capped, add it to lcollection.
	 */
	if (lcol == NULL) {
		/*
		 * If the cap has been zeroed and the collection doesn't exist,
		 * don't create the collection just to remvoe the cap later.
		 */
		if (rss_cap == 0)
			return (NULL);

		*changes |= LCST_CAP_CHANGED;
		lcol = malloc(sizeof (*lcol));
		if (lcol == NULL) {
			debug("not enough memory to monitor %s %s",
			    (colidp->rcid_type == RCIDT_PROJECT ?
			    "project" : "zone"), name);
			return (NULL);
		}
		(void) bzero(lcol, sizeof (*lcol));

		lcol->lcol_id = *colidp;
		debug("added collection %s\n", name);
		lcol->lcol_prev = NULL;
		lcol->lcol_next = lcollection_head;
		lcol->lcol_stat.lcols_min_rss = (uint64_t)-1;
		if (lcollection_head != NULL)
			lcollection_head->lcol_prev = lcol;
		lcollection_head = lcol;
	}

	/*
	 * Set/update the collection's name.
	 */
	(void) strlcpy(lcol->lcol_name, name, sizeof (lcol->lcol_name));

	/*
	 * Set cap flags.
	 */
	if (rss_cap != lcol->lcol_rss_cap) {
		*changes |= LCST_CAP_CHANGED;
		lcol->lcol_rss_cap = rss_cap;
		if (lcol->lcol_rss_cap == 0)
			*changes |= LCST_CAP_REMOVED;
	}

	if (rss_cap > 0)
		lcol->lcol_mark++;

	return (lcol);
}

static void
lcollection_update_notification_cb(char *col_type, char *name, int changes,
    uint64_t rss_cap, int mark)
{
	/*
	 * Assume the collection has been updated redundantly if its mark count
	 * exceeds 1, and that another notification is unnecessary.
	 */
	if (mark > 1)
		return;

	if (changes & LCST_CAP_ZERO)
		debug("%s %s: %s\n", col_type, name,
		    (changes & LCST_CAP_REMOVED) ? "cap removed" : "uncapped");
	else
		debug("%s %s: cap: %llukB\n", col_type, name,
		    (unsigned long long)rss_cap);
}

/*
 * Function to walk list of collections and invoke the specified callback with
 * the specified argument.  Callbacks are allowed to change the linkage of the
 * collection on which they act.
 */
void
list_walk_collection(int (*cb)(lcollection_t *, void *), void *arg)
{
	lcollection_t *lcol;
	lcollection_t *next;

	lcol = lcollection_head;
	while (lcol != NULL) {
		next = lcol->lcol_next;
		if (cb(lcol, arg) != 0)
			return;
		lcol = next;
	}
}

/*
 * Returns a nonzero value if an lprocess_t is still a valid member of a given
 * collection.
 */
int
lcollection_member(lcollection_t *lcol, lprocess_t *lpc)
{
	lprocess_t *cur = lcol->lcol_lprocess;

	while (cur != NULL)
		if (cur == lpc)
			return (1);
		else
			cur = cur->lpc_next;
	return (0);
}

static int
lcollection_find_cb(lcollection_t *lcol, void *arg)
{
	rcid_t *colidp = ((lcollection_find_arg_t *)arg)->lfa_colidp;

	if (lcol->lcol_id.rcid_type == colidp->rcid_type &&
	    lcol->lcol_id.rcid_val == colidp->rcid_val) {
		((lcollection_find_arg_t *)arg)->lfa_found = lcol;
		return (1);
	}

	return (0);
}

lcollection_t *
lcollection_find(rcid_t *colidp)
{
	lcollection_find_arg_t lfa;

	lfa.lfa_colidp = colidp;
	lfa.lfa_found = NULL;
	list_walk_collection(lcollection_find_cb, &lfa);

	return (lfa.lfa_found);
}

/*
 * Unlinks a collection from lcollection.
 */
void
lcollection_free(lcollection_t *lcol)
{
	lprocess_t *lpc;
	lprocess_t *next;

	lpc = lcol->lcol_lprocess;
	while (lpc != NULL) {
		next = lpc->lpc_next;
		if (lpc->lpc_collection == lcol)
			lprocess_free(lpc);
		lpc = next;
	}

	/*
	 * Unlink the collection.
	 */
	if (lcol->lcol_prev != NULL)
		lcol->lcol_prev->lcol_next = lcol->lcol_next;
	if (lcol->lcol_next != NULL)
		lcol->lcol_next->lcol_prev = lcol->lcol_prev;
	if (lcollection_head == lcol)
		lcollection_head = lcol->lcol_next;
	lcol->lcol_next = lcol->lcol_prev = NULL;

	free(lcol);
}