xref: /illumos-gate/usr/src/cmd/rcap/rcapd/rcapd_collection_project.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <procfs.h>
277c478bd9Sstevel@tonic-gate #include <project.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include "rcapd.h"
317c478bd9Sstevel@tonic-gate #include "utils.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 				/* absolute cap name */
347c478bd9Sstevel@tonic-gate #define	PJ_ABS_ATTR_NAME	"rcap.max-rss"
357c478bd9Sstevel@tonic-gate 				/* round up to next y = 2^n */
367c478bd9Sstevel@tonic-gate #define	ROUNDUP(x, y)		(((x) + ((y) - 1)) & ~((y) - 1))
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static int
lcollection_update_project_cb(const struct project * proj,void * walk_data)397c478bd9Sstevel@tonic-gate lcollection_update_project_cb(const struct project *proj, void *walk_data)
407c478bd9Sstevel@tonic-gate {
41*0209230bSgjelinek 	void(*update_notification_cb)(char *, char *, int, uint64_t, int) =
42*0209230bSgjelinek 	    (void(*)(char *, char *, int, uint64_t, int))walk_data;
437c478bd9Sstevel@tonic-gate 	char *capattr_abs;
447c478bd9Sstevel@tonic-gate 	char *end;
457c478bd9Sstevel@tonic-gate 	int changes;
467c478bd9Sstevel@tonic-gate 	int64_t max_rss;
477c478bd9Sstevel@tonic-gate 	lcollection_t *lcol;
48*0209230bSgjelinek 	rcid_t colid;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	capattr_abs = strstr(proj->pj_attr, PJ_ABS_ATTR_NAME "=");
517c478bd9Sstevel@tonic-gate 	if (capattr_abs != NULL) {
527c478bd9Sstevel@tonic-gate 		if (capattr_abs > proj->pj_attr)
537c478bd9Sstevel@tonic-gate 			if (*(capattr_abs - 1) != ';') {
547c478bd9Sstevel@tonic-gate 				/*
557c478bd9Sstevel@tonic-gate 				 * PJ_ABS_ATTR_NAME only matched part
567c478bd9Sstevel@tonic-gate 				 * of an attribute.
577c478bd9Sstevel@tonic-gate 				 */
587c478bd9Sstevel@tonic-gate 				return (0);
597c478bd9Sstevel@tonic-gate 			}
607c478bd9Sstevel@tonic-gate 		capattr_abs += strlen(PJ_ABS_ATTR_NAME "=");
617c478bd9Sstevel@tonic-gate 		max_rss = ROUNDUP(strtoll(capattr_abs, &end, 10), 1024) / 1024;
627c478bd9Sstevel@tonic-gate 		if (end == capattr_abs || *end != ';' && *end != 0)
63*0209230bSgjelinek 			warn(gettext("project %s: malformed %s value '%s'\n"),
64*0209230bSgjelinek 			    proj->pj_name, PJ_ABS_ATTR_NAME, capattr_abs);
657c478bd9Sstevel@tonic-gate 	} else
667c478bd9Sstevel@tonic-gate 		max_rss = 0;
677c478bd9Sstevel@tonic-gate 
68*0209230bSgjelinek 	colid.rcid_type = RCIDT_PROJECT;
69*0209230bSgjelinek 	colid.rcid_val = proj->pj_projid;
70*0209230bSgjelinek 
71*0209230bSgjelinek 	lcol = lcollection_insert_update(&colid, max_rss, proj->pj_name,
72*0209230bSgjelinek 	    &changes);
737c478bd9Sstevel@tonic-gate 	if (update_notification_cb != NULL)
74*0209230bSgjelinek 		update_notification_cb("project", proj->pj_name, changes,
75*0209230bSgjelinek 		    max_rss, (lcol != NULL) ? lcol->lcol_mark : 0);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static int
lcollection_update_project_byid_cb(const projid_t id,void * walk_data)817c478bd9Sstevel@tonic-gate lcollection_update_project_byid_cb(const projid_t id, void *walk_data)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	char buf[PROJECT_BUFSZ];
847c478bd9Sstevel@tonic-gate 	struct project proj;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (getprojbyid(id, &proj, buf, sizeof (buf)) != NULL && proj.pj_attr !=
877c478bd9Sstevel@tonic-gate 	    NULL)
887c478bd9Sstevel@tonic-gate 		return (lcollection_update_project_cb(&proj, walk_data));
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int
lcollection_update_onceactive_cb(lcollection_t * lcol,void * walk_data)947c478bd9Sstevel@tonic-gate lcollection_update_onceactive_cb(lcollection_t *lcol, void *walk_data)
957c478bd9Sstevel@tonic-gate {
96*0209230bSgjelinek 	void(*update_notification_cb)(char *, char *, int, uint64_t, int) =
97*0209230bSgjelinek 	    (void(*)(char *, char *, int, uint64_t, int))walk_data;
987c478bd9Sstevel@tonic-gate 
99*0209230bSgjelinek 	if (lcol->lcol_id.rcid_type != RCIDT_PROJECT)
100*0209230bSgjelinek 		return (0);
101*0209230bSgjelinek 
102*0209230bSgjelinek 	return (lcollection_update_project_byid_cb(lcol->lcol_id.rcid_val,
1037c478bd9Sstevel@tonic-gate 	    (void *)update_notification_cb));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static int
project_walk_all(int (* cb)(const struct project *,void *),void * walk_data)1077c478bd9Sstevel@tonic-gate project_walk_all(int(*cb)(const struct project *, void *), void *walk_data)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	char buf[PROJECT_BUFSZ];
1107c478bd9Sstevel@tonic-gate 	struct project proj;
1117c478bd9Sstevel@tonic-gate 	int res = 0;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	setprojent();
1147c478bd9Sstevel@tonic-gate 	while (getprojent(&proj, buf, sizeof (buf)) != NULL && res == 0)
1157c478bd9Sstevel@tonic-gate 		res = cb(&proj, walk_data);
1167c478bd9Sstevel@tonic-gate 	endprojent();
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	return (res);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
lcollection_update_project(lcollection_update_type_t ut,void (* update_notification_cb)(char *,char *,int,uint64_t,int))1227c478bd9Sstevel@tonic-gate lcollection_update_project(lcollection_update_type_t ut,
123*0209230bSgjelinek     void(*update_notification_cb)(char *, char *, int, uint64_t, int))
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	switch (ut) {
1267c478bd9Sstevel@tonic-gate 	case LCU_ACTIVE_ONLY:
1277c478bd9Sstevel@tonic-gate 		/*
1287c478bd9Sstevel@tonic-gate 		 * Enumerate active projects.  This is much faster than
1297c478bd9Sstevel@tonic-gate 		 * enumerating all projects (as is done below, in the default
1307c478bd9Sstevel@tonic-gate 		 * case), and is done to efficiently and incrementally update
1317c478bd9Sstevel@tonic-gate 		 * lcollection with capped projects.  The default case performs
1327c478bd9Sstevel@tonic-gate 		 * the initialization.
1337c478bd9Sstevel@tonic-gate 		 */
1347c478bd9Sstevel@tonic-gate 		(void) project_walk(lcollection_update_project_byid_cb,
1357c478bd9Sstevel@tonic-gate 		    (void *)update_notification_cb);
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * Enumerate once-active projects, including the active
1387c478bd9Sstevel@tonic-gate 		 * projects just enumerated, meaning active projects will be
1397c478bd9Sstevel@tonic-gate 		 * updated and marked twice.
1407c478bd9Sstevel@tonic-gate 		 */
1417c478bd9Sstevel@tonic-gate 		list_walk_collection(lcollection_update_onceactive_cb,
1427c478bd9Sstevel@tonic-gate 		    (void *)update_notification_cb);
1437c478bd9Sstevel@tonic-gate 		break;
1447c478bd9Sstevel@tonic-gate 	default:
1457c478bd9Sstevel@tonic-gate 		/*
1467c478bd9Sstevel@tonic-gate 		 * Enumerate all projects.
1477c478bd9Sstevel@tonic-gate 		 */
1487c478bd9Sstevel@tonic-gate 		(void) project_walk_all(lcollection_update_project_cb,
1497c478bd9Sstevel@tonic-gate 		    (void *)update_notification_cb);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate }
152