xref: /onnv-gate/usr/src/cmd/rcap/rcapd/rcapd_collection.c (revision 3247:e05001c14ea2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3247Sgjelinek  * Common Development and Distribution License (the "License").
6*3247Sgjelinek  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3247Sgjelinek  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include "rcapd.h"
320Sstevel@tonic-gate #include "utils.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * An abstract "collection" of processes.  Multiple types of collections can
360Sstevel@tonic-gate  * exist, one of which is selected at run-time.  Currently, the only one
370Sstevel@tonic-gate  * defined corresponds to project(4)s.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	MAX(x, y) (((x) > (y)) ? (x) : (y))
410Sstevel@tonic-gate 
420Sstevel@tonic-gate typedef struct {
43*3247Sgjelinek 	rcid_t		*lfa_colidp;
440Sstevel@tonic-gate 	lcollection_t	*lfa_found;
450Sstevel@tonic-gate } lcollection_find_arg_t;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate extern void lcollection_update_project(lcollection_update_type_t,
48*3247Sgjelinek     void(*)(char *, char *, int, uint64_t, int));
49*3247Sgjelinek extern void lcollection_update_zone(lcollection_update_type_t,
50*3247Sgjelinek     void(*)(char *, char *, int, uint64_t, int));
51*3247Sgjelinek static void lcollection_update_notification_cb(char *, char *, int, uint64_t,
52*3247Sgjelinek     int);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate rcid_t(*rc_getidbypsinfo)(psinfo_t *);
550Sstevel@tonic-gate uint64_t phys_total = 0;
560Sstevel@tonic-gate static lcollection_t *lcollection_head = NULL;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate void
lcollection_update(lcollection_update_type_t ut)590Sstevel@tonic-gate lcollection_update(lcollection_update_type_t ut)
600Sstevel@tonic-gate {
61*3247Sgjelinek 	lcollection_update_zone(ut, lcollection_update_notification_cb);
62*3247Sgjelinek 	lcollection_update_project(ut, lcollection_update_notification_cb);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Inserts a collection with the supplied identity, or updates the caps of an
670Sstevel@tonic-gate  * existing one.  The return value will have these bits set, depending on the
680Sstevel@tonic-gate  * previous and new cap values.  If no cap was displaced, and the requested cap
690Sstevel@tonic-gate  * is 0, no collection will be added, and the applicable *ZERO flags will be
700Sstevel@tonic-gate  * set.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  *	LCST_CAP_CHANGED
730Sstevel@tonic-gate  *	LCST_CAP_REMOVED
740Sstevel@tonic-gate  *	LCSS_CAP_ZERO
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate lcollection_t *
lcollection_insert_update(rcid_t * colidp,uint64_t rss_cap,char * name,int * changes)77*3247Sgjelinek lcollection_insert_update(rcid_t *colidp, uint64_t rss_cap, char *name,
780Sstevel@tonic-gate     int *changes)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	lcollection_t *lcol;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	*changes = 0;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (rss_cap == 0)
850Sstevel@tonic-gate 		*changes |= LCST_CAP_ZERO;
860Sstevel@tonic-gate 
87*3247Sgjelinek 	lcol = lcollection_find(colidp);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * If the specified collection is capped, add it to lcollection.
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	if (lcol == NULL) {
930Sstevel@tonic-gate 		/*
940Sstevel@tonic-gate 		 * If the cap has been zeroed and the collection doesn't exist,
950Sstevel@tonic-gate 		 * don't create the collection just to remvoe the cap later.
960Sstevel@tonic-gate 		 */
970Sstevel@tonic-gate 		if (rss_cap == 0)
980Sstevel@tonic-gate 			return (NULL);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		*changes |= LCST_CAP_CHANGED;
1010Sstevel@tonic-gate 		lcol = malloc(sizeof (*lcol));
1020Sstevel@tonic-gate 		if (lcol == NULL) {
1030Sstevel@tonic-gate 			debug("not enough memory to monitor %s %s",
104*3247Sgjelinek 			    (colidp->rcid_type == RCIDT_PROJECT ?
105*3247Sgjelinek 			    "project" : "zone"), name);
1060Sstevel@tonic-gate 			return (NULL);
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 		(void) bzero(lcol, sizeof (*lcol));
1090Sstevel@tonic-gate 
110*3247Sgjelinek 		lcol->lcol_id = *colidp;
1110Sstevel@tonic-gate 		debug("added collection %s\n", name);
1120Sstevel@tonic-gate 		lcol->lcol_prev = NULL;
1130Sstevel@tonic-gate 		lcol->lcol_next = lcollection_head;
1140Sstevel@tonic-gate 		lcol->lcol_stat.lcols_min_rss = (uint64_t)-1;
1150Sstevel@tonic-gate 		if (lcollection_head != NULL)
1160Sstevel@tonic-gate 			lcollection_head->lcol_prev = lcol;
1170Sstevel@tonic-gate 		lcollection_head = lcol;
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/*
1210Sstevel@tonic-gate 	 * Set/update the collection's name.
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	(void) strlcpy(lcol->lcol_name, name, sizeof (lcol->lcol_name));
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * Set cap flags.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	if (rss_cap != lcol->lcol_rss_cap) {
1290Sstevel@tonic-gate 		*changes |= LCST_CAP_CHANGED;
1300Sstevel@tonic-gate 		lcol->lcol_rss_cap = rss_cap;
1310Sstevel@tonic-gate 		if (lcol->lcol_rss_cap == 0)
1320Sstevel@tonic-gate 			*changes |= LCST_CAP_REMOVED;
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (rss_cap > 0)
1360Sstevel@tonic-gate 		lcol->lcol_mark++;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	return (lcol);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static void
lcollection_update_notification_cb(char * col_type,char * name,int changes,uint64_t rss_cap,int mark)142*3247Sgjelinek lcollection_update_notification_cb(char *col_type, char *name, int changes,
143*3247Sgjelinek     uint64_t rss_cap, int mark)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Assume the collection has been updated redundantly if its mark count
1470Sstevel@tonic-gate 	 * exceeds 1, and that another notification is unnecessary.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	if (mark > 1)
1500Sstevel@tonic-gate 		return;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (changes & LCST_CAP_ZERO)
153*3247Sgjelinek 		debug("%s %s: %s\n", col_type, name,
1540Sstevel@tonic-gate 		    (changes & LCST_CAP_REMOVED) ? "cap removed" : "uncapped");
1550Sstevel@tonic-gate 	else
156*3247Sgjelinek 		debug("%s %s: cap: %llukB\n", col_type, name,
1570Sstevel@tonic-gate 		    (unsigned long long)rss_cap);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Function to walk list of collections and invoke the specified callback with
1620Sstevel@tonic-gate  * the specified argument.  Callbacks are allowed to change the linkage of the
1630Sstevel@tonic-gate  * collection on which they act.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate void
list_walk_collection(int (* cb)(lcollection_t *,void *),void * arg)1660Sstevel@tonic-gate list_walk_collection(int (*cb)(lcollection_t *, void *), void *arg)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	lcollection_t *lcol;
1690Sstevel@tonic-gate 	lcollection_t *next;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	lcol = lcollection_head;
1720Sstevel@tonic-gate 	while (lcol != NULL) {
1730Sstevel@tonic-gate 		next = lcol->lcol_next;
1740Sstevel@tonic-gate 		if (cb(lcol, arg) != 0)
1750Sstevel@tonic-gate 			return;
1760Sstevel@tonic-gate 		lcol = next;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Returns a nonzero value if an lprocess_t is still a valid member of a given
1820Sstevel@tonic-gate  * collection.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate int
lcollection_member(lcollection_t * lcol,lprocess_t * lpc)1850Sstevel@tonic-gate lcollection_member(lcollection_t *lcol, lprocess_t *lpc)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	lprocess_t *cur = lcol->lcol_lprocess;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	while (cur != NULL)
1900Sstevel@tonic-gate 		if (cur == lpc)
1910Sstevel@tonic-gate 			return (1);
1920Sstevel@tonic-gate 		else
1930Sstevel@tonic-gate 			cur = cur->lpc_next;
1940Sstevel@tonic-gate 	return (0);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static int
lcollection_find_cb(lcollection_t * lcol,void * arg)1980Sstevel@tonic-gate lcollection_find_cb(lcollection_t *lcol, void *arg)
1990Sstevel@tonic-gate {
200*3247Sgjelinek 	rcid_t *colidp = ((lcollection_find_arg_t *)arg)->lfa_colidp;
201*3247Sgjelinek 
202*3247Sgjelinek 	if (lcol->lcol_id.rcid_type == colidp->rcid_type &&
203*3247Sgjelinek 	    lcol->lcol_id.rcid_val == colidp->rcid_val) {
2040Sstevel@tonic-gate 		((lcollection_find_arg_t *)arg)->lfa_found = lcol;
2050Sstevel@tonic-gate 		return (1);
206*3247Sgjelinek 	}
207*3247Sgjelinek 
208*3247Sgjelinek 	return (0);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate lcollection_t *
lcollection_find(rcid_t * colidp)212*3247Sgjelinek lcollection_find(rcid_t *colidp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	lcollection_find_arg_t lfa;
2150Sstevel@tonic-gate 
216*3247Sgjelinek 	lfa.lfa_colidp = colidp;
2170Sstevel@tonic-gate 	lfa.lfa_found = NULL;
2180Sstevel@tonic-gate 	list_walk_collection(lcollection_find_cb, &lfa);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (lfa.lfa_found);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Unlinks a collection from lcollection.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate void
lcollection_free(lcollection_t * lcol)2270Sstevel@tonic-gate lcollection_free(lcollection_t *lcol)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	lprocess_t *lpc;
2300Sstevel@tonic-gate 	lprocess_t *next;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	lpc = lcol->lcol_lprocess;
2330Sstevel@tonic-gate 	while (lpc != NULL) {
2340Sstevel@tonic-gate 		next = lpc->lpc_next;
2350Sstevel@tonic-gate 		if (lpc->lpc_collection == lcol)
2360Sstevel@tonic-gate 			lprocess_free(lpc);
2370Sstevel@tonic-gate 		lpc = next;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * Unlink the collection.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if (lcol->lcol_prev != NULL)
2440Sstevel@tonic-gate 		lcol->lcol_prev->lcol_next = lcol->lcol_next;
2450Sstevel@tonic-gate 	if (lcol->lcol_next != NULL)
2460Sstevel@tonic-gate 		lcol->lcol_next->lcol_prev = lcol->lcol_prev;
2470Sstevel@tonic-gate 	if (lcollection_head == lcol)
2480Sstevel@tonic-gate 		lcollection_head = lcol->lcol_next;
2490Sstevel@tonic-gate 	lcol->lcol_next = lcol->lcol_prev = NULL;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	free(lcol);
2520Sstevel@tonic-gate }
253