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
50209230bSgjelinek * Common Development and Distribution License (the "License").
60209230bSgjelinek * 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 /*
220209230bSgjelinek * 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 #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include "rcapd.h"
307c478bd9Sstevel@tonic-gate #include "utils.h"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * An abstract "collection" of processes. Multiple types of collections can
347c478bd9Sstevel@tonic-gate * exist, one of which is selected at run-time. Currently, the only one
35*bbf21555SRichard Lowe * defined corresponds to project(5)s.
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #define MAX(x, y) (((x) > (y)) ? (x) : (y))
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate typedef struct {
410209230bSgjelinek rcid_t *lfa_colidp;
427c478bd9Sstevel@tonic-gate lcollection_t *lfa_found;
437c478bd9Sstevel@tonic-gate } lcollection_find_arg_t;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate extern void lcollection_update_project(lcollection_update_type_t,
460209230bSgjelinek void(*)(char *, char *, int, uint64_t, int));
470209230bSgjelinek extern void lcollection_update_zone(lcollection_update_type_t,
480209230bSgjelinek void(*)(char *, char *, int, uint64_t, int));
490209230bSgjelinek static void lcollection_update_notification_cb(char *, char *, int, uint64_t,
500209230bSgjelinek int);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate rcid_t(*rc_getidbypsinfo)(psinfo_t *);
537c478bd9Sstevel@tonic-gate uint64_t phys_total = 0;
547c478bd9Sstevel@tonic-gate static lcollection_t *lcollection_head = NULL;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate void
lcollection_update(lcollection_update_type_t ut)577c478bd9Sstevel@tonic-gate lcollection_update(lcollection_update_type_t ut)
587c478bd9Sstevel@tonic-gate {
590209230bSgjelinek lcollection_update_zone(ut, lcollection_update_notification_cb);
600209230bSgjelinek lcollection_update_project(ut, lcollection_update_notification_cb);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Inserts a collection with the supplied identity, or updates the caps of an
657c478bd9Sstevel@tonic-gate * existing one. The return value will have these bits set, depending on the
667c478bd9Sstevel@tonic-gate * previous and new cap values. If no cap was displaced, and the requested cap
677c478bd9Sstevel@tonic-gate * is 0, no collection will be added, and the applicable *ZERO flags will be
687c478bd9Sstevel@tonic-gate * set.
697c478bd9Sstevel@tonic-gate *
707c478bd9Sstevel@tonic-gate * LCST_CAP_CHANGED
717c478bd9Sstevel@tonic-gate * LCST_CAP_REMOVED
727c478bd9Sstevel@tonic-gate * LCSS_CAP_ZERO
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate lcollection_t *
lcollection_insert_update(rcid_t * colidp,uint64_t rss_cap,char * name,int * changes)750209230bSgjelinek lcollection_insert_update(rcid_t *colidp, uint64_t rss_cap, char *name,
767c478bd9Sstevel@tonic-gate int *changes)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate lcollection_t *lcol;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate *changes = 0;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate if (rss_cap == 0)
837c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_ZERO;
847c478bd9Sstevel@tonic-gate
850209230bSgjelinek lcol = lcollection_find(colidp);
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * If the specified collection is capped, add it to lcollection.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate if (lcol == NULL) {
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * If the cap has been zeroed and the collection doesn't exist,
937c478bd9Sstevel@tonic-gate * don't create the collection just to remvoe the cap later.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate if (rss_cap == 0)
967c478bd9Sstevel@tonic-gate return (NULL);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_CHANGED;
997c478bd9Sstevel@tonic-gate lcol = malloc(sizeof (*lcol));
1007c478bd9Sstevel@tonic-gate if (lcol == NULL) {
1017c478bd9Sstevel@tonic-gate debug("not enough memory to monitor %s %s",
1020209230bSgjelinek (colidp->rcid_type == RCIDT_PROJECT ?
1030209230bSgjelinek "project" : "zone"), name);
1047c478bd9Sstevel@tonic-gate return (NULL);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate (void) bzero(lcol, sizeof (*lcol));
1077c478bd9Sstevel@tonic-gate
1080209230bSgjelinek lcol->lcol_id = *colidp;
1097c478bd9Sstevel@tonic-gate debug("added collection %s\n", name);
1107c478bd9Sstevel@tonic-gate lcol->lcol_prev = NULL;
1117c478bd9Sstevel@tonic-gate lcol->lcol_next = lcollection_head;
1127c478bd9Sstevel@tonic-gate lcol->lcol_stat.lcols_min_rss = (uint64_t)-1;
1137c478bd9Sstevel@tonic-gate if (lcollection_head != NULL)
1147c478bd9Sstevel@tonic-gate lcollection_head->lcol_prev = lcol;
1157c478bd9Sstevel@tonic-gate lcollection_head = lcol;
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * Set/update the collection's name.
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate (void) strlcpy(lcol->lcol_name, name, sizeof (lcol->lcol_name));
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Set cap flags.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate if (rss_cap != lcol->lcol_rss_cap) {
1277c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_CHANGED;
1287c478bd9Sstevel@tonic-gate lcol->lcol_rss_cap = rss_cap;
1297c478bd9Sstevel@tonic-gate if (lcol->lcol_rss_cap == 0)
1307c478bd9Sstevel@tonic-gate *changes |= LCST_CAP_REMOVED;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate if (rss_cap > 0)
1347c478bd9Sstevel@tonic-gate lcol->lcol_mark++;
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate return (lcol);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate static void
lcollection_update_notification_cb(char * col_type,char * name,int changes,uint64_t rss_cap,int mark)1400209230bSgjelinek lcollection_update_notification_cb(char *col_type, char *name, int changes,
1410209230bSgjelinek uint64_t rss_cap, int mark)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * Assume the collection has been updated redundantly if its mark count
1457c478bd9Sstevel@tonic-gate * exceeds 1, and that another notification is unnecessary.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate if (mark > 1)
1487c478bd9Sstevel@tonic-gate return;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (changes & LCST_CAP_ZERO)
1510209230bSgjelinek debug("%s %s: %s\n", col_type, name,
1527c478bd9Sstevel@tonic-gate (changes & LCST_CAP_REMOVED) ? "cap removed" : "uncapped");
1537c478bd9Sstevel@tonic-gate else
1540209230bSgjelinek debug("%s %s: cap: %llukB\n", col_type, name,
1557c478bd9Sstevel@tonic-gate (unsigned long long)rss_cap);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Function to walk list of collections and invoke the specified callback with
1607c478bd9Sstevel@tonic-gate * the specified argument. Callbacks are allowed to change the linkage of the
1617c478bd9Sstevel@tonic-gate * collection on which they act.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate void
list_walk_collection(int (* cb)(lcollection_t *,void *),void * arg)1647c478bd9Sstevel@tonic-gate list_walk_collection(int (*cb)(lcollection_t *, void *), void *arg)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate lcollection_t *lcol;
1677c478bd9Sstevel@tonic-gate lcollection_t *next;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate lcol = lcollection_head;
1707c478bd9Sstevel@tonic-gate while (lcol != NULL) {
1717c478bd9Sstevel@tonic-gate next = lcol->lcol_next;
1727c478bd9Sstevel@tonic-gate if (cb(lcol, arg) != 0)
1737c478bd9Sstevel@tonic-gate return;
1747c478bd9Sstevel@tonic-gate lcol = next;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * Returns a nonzero value if an lprocess_t is still a valid member of a given
1807c478bd9Sstevel@tonic-gate * collection.
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate int
lcollection_member(lcollection_t * lcol,lprocess_t * lpc)1837c478bd9Sstevel@tonic-gate lcollection_member(lcollection_t *lcol, lprocess_t *lpc)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate lprocess_t *cur = lcol->lcol_lprocess;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate while (cur != NULL)
1887c478bd9Sstevel@tonic-gate if (cur == lpc)
1897c478bd9Sstevel@tonic-gate return (1);
1907c478bd9Sstevel@tonic-gate else
1917c478bd9Sstevel@tonic-gate cur = cur->lpc_next;
1927c478bd9Sstevel@tonic-gate return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate static int
lcollection_find_cb(lcollection_t * lcol,void * arg)1967c478bd9Sstevel@tonic-gate lcollection_find_cb(lcollection_t *lcol, void *arg)
1977c478bd9Sstevel@tonic-gate {
1980209230bSgjelinek rcid_t *colidp = ((lcollection_find_arg_t *)arg)->lfa_colidp;
1990209230bSgjelinek
2000209230bSgjelinek if (lcol->lcol_id.rcid_type == colidp->rcid_type &&
2010209230bSgjelinek lcol->lcol_id.rcid_val == colidp->rcid_val) {
2027c478bd9Sstevel@tonic-gate ((lcollection_find_arg_t *)arg)->lfa_found = lcol;
2037c478bd9Sstevel@tonic-gate return (1);
2040209230bSgjelinek }
2050209230bSgjelinek
2060209230bSgjelinek return (0);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate lcollection_t *
lcollection_find(rcid_t * colidp)2100209230bSgjelinek lcollection_find(rcid_t *colidp)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate lcollection_find_arg_t lfa;
2137c478bd9Sstevel@tonic-gate
2140209230bSgjelinek lfa.lfa_colidp = colidp;
2157c478bd9Sstevel@tonic-gate lfa.lfa_found = NULL;
2167c478bd9Sstevel@tonic-gate list_walk_collection(lcollection_find_cb, &lfa);
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate return (lfa.lfa_found);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * Unlinks a collection from lcollection.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate void
lcollection_free(lcollection_t * lcol)2257c478bd9Sstevel@tonic-gate lcollection_free(lcollection_t *lcol)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate lprocess_t *lpc;
2287c478bd9Sstevel@tonic-gate lprocess_t *next;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate lpc = lcol->lcol_lprocess;
2317c478bd9Sstevel@tonic-gate while (lpc != NULL) {
2327c478bd9Sstevel@tonic-gate next = lpc->lpc_next;
2337c478bd9Sstevel@tonic-gate if (lpc->lpc_collection == lcol)
2347c478bd9Sstevel@tonic-gate lprocess_free(lpc);
2357c478bd9Sstevel@tonic-gate lpc = next;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Unlink the collection.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate if (lcol->lcol_prev != NULL)
2427c478bd9Sstevel@tonic-gate lcol->lcol_prev->lcol_next = lcol->lcol_next;
2437c478bd9Sstevel@tonic-gate if (lcol->lcol_next != NULL)
2447c478bd9Sstevel@tonic-gate lcol->lcol_next->lcol_prev = lcol->lcol_prev;
2457c478bd9Sstevel@tonic-gate if (lcollection_head == lcol)
2467c478bd9Sstevel@tonic-gate lcollection_head = lcol->lcol_next;
2477c478bd9Sstevel@tonic-gate lcol->lcol_next = lcol->lcol_prev = NULL;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate free(lcol);
2507c478bd9Sstevel@tonic-gate }
251