17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0209230bSgjelinek * Common Development and Distribution License (the "License").
6*0209230bSgjelinek * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*0209230bSgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include "rcapd.h"
327c478bd9Sstevel@tonic-gate #include "utils.h"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * An abstract "collection" of processes. Multiple types of collections can
367c478bd9Sstevel@tonic-gate * exist, one of which is selected at run-time. Currently, the only one
377c478bd9Sstevel@tonic-gate * defined corresponds to project(4)s.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #define MAX(x, y) (((x) > (y)) ? (x) : (y))
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate typedef struct {
43*0209230bSgjelinek rcid_t *lfa_colidp;
447c478bd9Sstevel@tonic-gate lcollection_t *lfa_found;
457c478bd9Sstevel@tonic-gate } lcollection_find_arg_t;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate extern void lcollection_update_project(lcollection_update_type_t,
48*0209230bSgjelinek void(*)(char *, char *, int, uint64_t, int));
49*0209230bSgjelinek extern void lcollection_update_zone(lcollection_update_type_t,
50*0209230bSgjelinek void(*)(char *, char *, int, uint64_t, int));
51*0209230bSgjelinek static void lcollection_update_notification_cb(char *, char *, int, uint64_t,
52*0209230bSgjelinek int);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate rcid_t(*rc_getidbypsinfo)(psinfo_t *);
557c478bd9Sstevel@tonic-gate uint64_t phys_total = 0;
567c478bd9Sstevel@tonic-gate static lcollection_t *lcollection_head = NULL;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate void
lcollection_update(lcollection_update_type_t ut)597c478bd9Sstevel@tonic-gate lcollection_update(lcollection_update_type_t ut)
607c478bd9Sstevel@tonic-gate {
61*0209230bSgjelinek lcollection_update_zone(ut, lcollection_update_notification_cb);
62*0209230bSgjelinek lcollection_update_project(ut, lcollection_update_notification_cb);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * Inserts a collection with the supplied identity, or updates the caps of an
677c478bd9Sstevel@tonic-gate * existing one. The return value will have these bits set, depending on the
687c478bd9Sstevel@tonic-gate * previous and new cap values. If no cap was displaced, and the requested cap
697c478bd9Sstevel@tonic-gate * is 0, no collection will be added, and the applicable *ZERO flags will be
707c478bd9Sstevel@tonic-gate * set.
717c478bd9Sstevel@tonic-gate *
727c478bd9Sstevel@tonic-gate * LCST_CAP_CHANGED
737c478bd9Sstevel@tonic-gate * LCST_CAP_REMOVED
747c478bd9Sstevel@tonic-gate * LCSS_CAP_ZERO
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate lcollection_t *
lcollection_insert_update(rcid_t * colidp,uint64_t rss_cap,char * name,int * changes)77*0209230bSgjelinek lcollection_insert_update(rcid_t *colidp, uint64_t rss_cap, char *name,
787c478bd9Sstevel@tonic-gate int *changes)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate lcollection_t *lcol;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate *changes = 0;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (rss_cap == 0)
857c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_ZERO;
867c478bd9Sstevel@tonic-gate
87*0209230bSgjelinek lcol = lcollection_find(colidp);
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * If the specified collection is capped, add it to lcollection.
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate if (lcol == NULL) {
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * If the cap has been zeroed and the collection doesn't exist,
957c478bd9Sstevel@tonic-gate * don't create the collection just to remvoe the cap later.
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate if (rss_cap == 0)
987c478bd9Sstevel@tonic-gate return (NULL);
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_CHANGED;
1017c478bd9Sstevel@tonic-gate lcol = malloc(sizeof (*lcol));
1027c478bd9Sstevel@tonic-gate if (lcol == NULL) {
1037c478bd9Sstevel@tonic-gate debug("not enough memory to monitor %s %s",
104*0209230bSgjelinek (colidp->rcid_type == RCIDT_PROJECT ?
105*0209230bSgjelinek "project" : "zone"), name);
1067c478bd9Sstevel@tonic-gate return (NULL);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate (void) bzero(lcol, sizeof (*lcol));
1097c478bd9Sstevel@tonic-gate
110*0209230bSgjelinek lcol->lcol_id = *colidp;
1117c478bd9Sstevel@tonic-gate debug("added collection %s\n", name);
1127c478bd9Sstevel@tonic-gate lcol->lcol_prev = NULL;
1137c478bd9Sstevel@tonic-gate lcol->lcol_next = lcollection_head;
1147c478bd9Sstevel@tonic-gate lcol->lcol_stat.lcols_min_rss = (uint64_t)-1;
1157c478bd9Sstevel@tonic-gate if (lcollection_head != NULL)
1167c478bd9Sstevel@tonic-gate lcollection_head->lcol_prev = lcol;
1177c478bd9Sstevel@tonic-gate lcollection_head = lcol;
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Set/update the collection's name.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate (void) strlcpy(lcol->lcol_name, name, sizeof (lcol->lcol_name));
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * Set cap flags.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate if (rss_cap != lcol->lcol_rss_cap) {
1297c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_CHANGED;
1307c478bd9Sstevel@tonic-gate lcol->lcol_rss_cap = rss_cap;
1317c478bd9Sstevel@tonic-gate if (lcol->lcol_rss_cap == 0)
1327c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_REMOVED;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate if (rss_cap > 0)
1367c478bd9Sstevel@tonic-gate lcol->lcol_mark++;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate return (lcol);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate static void
lcollection_update_notification_cb(char * col_type,char * name,int changes,uint64_t rss_cap,int mark)142*0209230bSgjelinek lcollection_update_notification_cb(char *col_type, char *name, int changes,
143*0209230bSgjelinek uint64_t rss_cap, int mark)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * Assume the collection has been updated redundantly if its mark count
1477c478bd9Sstevel@tonic-gate * exceeds 1, and that another notification is unnecessary.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate if (mark > 1)
1507c478bd9Sstevel@tonic-gate return;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (changes & LCST_CAP_ZERO)
153*0209230bSgjelinek debug("%s %s: %s\n", col_type, name,
1547c478bd9Sstevel@tonic-gate (changes & LCST_CAP_REMOVED) ? "cap removed" : "uncapped");
1557c478bd9Sstevel@tonic-gate else
156*0209230bSgjelinek debug("%s %s: cap: %llukB\n", col_type, name,
1577c478bd9Sstevel@tonic-gate (unsigned long long)rss_cap);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * Function to walk list of collections and invoke the specified callback with
1627c478bd9Sstevel@tonic-gate * the specified argument. Callbacks are allowed to change the linkage of the
1637c478bd9Sstevel@tonic-gate * collection on which they act.
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate void
list_walk_collection(int (* cb)(lcollection_t *,void *),void * arg)1667c478bd9Sstevel@tonic-gate list_walk_collection(int (*cb)(lcollection_t *, void *), void *arg)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate lcollection_t *lcol;
1697c478bd9Sstevel@tonic-gate lcollection_t *next;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate lcol = lcollection_head;
1727c478bd9Sstevel@tonic-gate while (lcol != NULL) {
1737c478bd9Sstevel@tonic-gate next = lcol->lcol_next;
1747c478bd9Sstevel@tonic-gate if (cb(lcol, arg) != 0)
1757c478bd9Sstevel@tonic-gate return;
1767c478bd9Sstevel@tonic-gate lcol = next;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate * Returns a nonzero value if an lprocess_t is still a valid member of a given
1827c478bd9Sstevel@tonic-gate * collection.
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate int
lcollection_member(lcollection_t * lcol,lprocess_t * lpc)1857c478bd9Sstevel@tonic-gate lcollection_member(lcollection_t *lcol, lprocess_t *lpc)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate lprocess_t *cur = lcol->lcol_lprocess;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate while (cur != NULL)
1907c478bd9Sstevel@tonic-gate if (cur == lpc)
1917c478bd9Sstevel@tonic-gate return (1);
1927c478bd9Sstevel@tonic-gate else
1937c478bd9Sstevel@tonic-gate cur = cur->lpc_next;
1947c478bd9Sstevel@tonic-gate return (0);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate static int
lcollection_find_cb(lcollection_t * lcol,void * arg)1987c478bd9Sstevel@tonic-gate lcollection_find_cb(lcollection_t *lcol, void *arg)
1997c478bd9Sstevel@tonic-gate {
200*0209230bSgjelinek rcid_t *colidp = ((lcollection_find_arg_t *)arg)->lfa_colidp;
201*0209230bSgjelinek
202*0209230bSgjelinek if (lcol->lcol_id.rcid_type == colidp->rcid_type &&
203*0209230bSgjelinek lcol->lcol_id.rcid_val == colidp->rcid_val) {
2047c478bd9Sstevel@tonic-gate ((lcollection_find_arg_t *)arg)->lfa_found = lcol;
2057c478bd9Sstevel@tonic-gate return (1);
206*0209230bSgjelinek }
207*0209230bSgjelinek
2087c478bd9Sstevel@tonic-gate return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate lcollection_t *
lcollection_find(rcid_t * colidp)212*0209230bSgjelinek lcollection_find(rcid_t *colidp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate lcollection_find_arg_t lfa;
2157c478bd9Sstevel@tonic-gate
216*0209230bSgjelinek lfa.lfa_colidp = colidp;
2177c478bd9Sstevel@tonic-gate lfa.lfa_found = NULL;
2187c478bd9Sstevel@tonic-gate list_walk_collection(lcollection_find_cb, &lfa);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate return (lfa.lfa_found);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * Unlinks a collection from lcollection.
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate void
lcollection_free(lcollection_t * lcol)2277c478bd9Sstevel@tonic-gate lcollection_free(lcollection_t *lcol)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate lprocess_t *lpc;
2307c478bd9Sstevel@tonic-gate lprocess_t *next;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate lpc = lcol->lcol_lprocess;
2337c478bd9Sstevel@tonic-gate while (lpc != NULL) {
2347c478bd9Sstevel@tonic-gate next = lpc->lpc_next;
2357c478bd9Sstevel@tonic-gate if (lpc->lpc_collection == lcol)
2367c478bd9Sstevel@tonic-gate lprocess_free(lpc);
2377c478bd9Sstevel@tonic-gate lpc = next;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * Unlink the collection.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate if (lcol->lcol_prev != NULL)
2447c478bd9Sstevel@tonic-gate lcol->lcol_prev->lcol_next = lcol->lcol_next;
2457c478bd9Sstevel@tonic-gate if (lcol->lcol_next != NULL)
2467c478bd9Sstevel@tonic-gate lcol->lcol_next->lcol_prev = lcol->lcol_prev;
2477c478bd9Sstevel@tonic-gate if (lcollection_head == lcol)
2487c478bd9Sstevel@tonic-gate lcollection_head = lcol->lcol_next;
2497c478bd9Sstevel@tonic-gate lcol->lcol_next = lcol->lcol_prev = NULL;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate free(lcol);
2527c478bd9Sstevel@tonic-gate }
253