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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _RCAPD_H
#define _RCAPD_H
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <procfs.h>
#include "rcapd_conf.h"
#define LC_NAME_LEN 32
#define RCAP_FMRI "svc:/system/rcap:default"
#define CONFIG_PG "config"
#define PRESSURE "pressure"
#define RECONFIG_INT "reconfig_interval"
#define REPORT_INT "report_interval"
#define RSS_SAMPLE_INT "rss_sample_interval"
#define WALK_INT "walk_interval"
#define RCAPD_IGNORED_SET_FLUSH_IVAL 10 /* number of scans between */
/* flushes of the ignored set */
/*
* set the buffer length for /proc-based path names based on the actual
* length of the largest pid
*/
#define RCAPD__STR(a) #a
#define RCAPD_STR(macro) RCAPD__STR(macro)
#define PROC_PATH_MAX (sizeof ("/proc/" RCAPD_STR(PID_MAX) \
"/pagedata"))
/*
* lcollection_insert_update() result flags
*/
#define LCST_CAP_CHANGED (1<<0)
#define LCST_CAP_REMOVED (1<<1)
#define LCST_CAP_ZERO (1<<2)
typedef enum {
RCIDT_PROJECT,
RCIDT_ZONE
} rcid_type_t;
typedef struct {
/*
* The following field could just be a rcid_type_t but it gets
* written out to a file as binary data for communication between
* 64-bit rcapd & 32-bit rcapstat, so we need to force a standard size
* and alignment here.
*/
uint64_t rcid_type;
int64_t rcid_val;
} rcid_t;
typedef enum {
LCU_COMPLETE, /* an enumeration of all possible collections */
LCU_ACTIVE_ONLY /* an enumeration of only once-active collections */
} lcollection_update_type_t;
struct lmapping;
struct lprocess;
struct lcollection;
struct prxmap;
struct psinfo;
/*
* Per-process data.
*/
typedef struct lprocess {
struct lprocess *lpc_prev; /* global process list */
struct lprocess *lpc_next;
pid_t lpc_pid; /* ID of this process */
int lpc_unscannable; /* flag indicating zombie or */
/* other unscannable process */
uint64_t lpc_rss; /* resident set size (kB) */
uint64_t lpc_unrm; /* scannable set size (kB) (est.) */
uint64_t lpc_size; /* process image size (kB) */
int lpc_mark; /* mark-and-sweep flag */
struct lcollection *lpc_collection; /* owning collection */
int lpc_psinfo_fd; /* cached psinfo fd */
int lpc_pgdata_fd; /* cached pagedata fd */
int lpc_xmap_fd; /* cached xmap fd */
struct prxmap *lpc_xmap; /* xmap corresponding to */
/* current pagedata */
int lpc_nxmap; /* number of mappings in xmap */
prpageheader_t *lpc_prpageheader; /* accumulated mask of */
/* process's ref/mod bits */
struct lmapping *lpc_ignore; /* empirically-unpageable mappings */
} lprocess_t;
/*
* Collection statistics.
*/
typedef struct {
uint64_t lcols_scan; /* scan attempts */
uint64_t lcols_pg_att; /* kB attempted to page */
uint64_t lcols_pg_eff; /* kB paged out (est.) */
uint64_t lcols_rss_sample; /* RSS samplings */
uint64_t lcols_unenforced_cap; /* times cap could have been */
/* enforced, but wasn't (due to low */
/* global memory pressure, or global */
/* scanner being activated) */
uint64_t lcols_rss_sum; /* sum of sampled RSS values */
uint64_t lcols_rss_act_sum; /* sum of sampled, excess RSS values */
uint64_t lcols_min_rss; /* minimum RSS (kB), this interval */
uint64_t lcols_max_rss; /* maximum RSS (kB), this interval */
uint64_t lcols_proc_in; /* processes tracked */
uint64_t lcols_proc_out; /* processes freed */
hrtime_t lcols_scan_time; /* time spent scanning (ns) */
hrtime_t lcols_scan_time_complete; /* time spent scanning (ns) */
/* at last completion */
uint64_t lcols_scan_count; /* number of complete scans */
uint64_t lcols_scan_ineffective; /* number of uninterrupted */
/* revolutions of clock hand after */
/* which the excess was not */
/* completely reduced */
} lcollection_stat_t;
/*
* Collection.
*/
typedef struct lcollection {
struct lcollection *lcol_prev; /* global collection list */
struct lcollection *lcol_next;
rcid_t lcol_id; /* numerical ID for this collection */
char lcol_name[LC_NAME_LEN]; /* name of this collection, or */
/* "unknown" */
uint64_t lcol_rss; /* RSS of all processes (kB) */
uint64_t lcol_image_size; /* image size of all processes (kB) */
uint64_t lcol_rss_cap; /* RSS cap (kB) */
lcollection_stat_t lcol_stat; /* statistics */
lcollection_stat_t lcol_stat_old; /* previous interval's statistics */
lprocess_t *lcol_lprocess; /* member processes */
int lcol_mark; /* mark-and-sweep flag */
lprocess_t *lcol_victim; /* victim process to resume scanning */
void *lcol_resaddr; /* address to resume scanning from */
} lcollection_t;
/*
* Collection report.
*/
typedef struct lcollection_report {
rcid_t lcol_id; /* numerical ID for this collection */
char lcol_name[LC_NAME_LEN]; /* name of this collection, or */
/* "unknown" */
uint64_t lcol_rss; /* RSS of all processes (kB) */
uint64_t lcol_image_size; /* image size of all processes (kB) */
uint64_t lcol_rss_cap; /* RSS limit (kB) */
lcollection_stat_t lcol_stat; /* statistics */
} lcollection_report_t;
extern int get_psinfo(pid_t, struct psinfo *, int, int(*)(void *, int), void *,
lprocess_t *);
extern lcollection_t *lcollection_find(rcid_t *);
extern void lcollection_freq_move(lprocess_t *);
extern lcollection_t *lcollection_insert_update(rcid_t *, uint64_t, char *,
int *changes);
extern int lcollection_member(lcollection_t *, lprocess_t *);
extern void lcollection_free(lcollection_t *);
extern void lcollection_update(lcollection_update_type_t);
extern void list_walk_collection(int (*)(lcollection_t *, void *), void *);
extern int lprocess_update_psinfo_fd_cb(void *, int);
extern void lprocess_free(lprocess_t *);
extern void scan(lcollection_t *, int64_t);
extern void scan_abort(void);
extern void check_update_statistics(void);
/*
* Global (in rcapd only) variables.
*/
extern rcfg_t rcfg;
extern uint64_t phys_total;
extern int should_run;
#ifdef __cplusplus
}
#endif
#endif /* _RCAPD_H */
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <fcntl.h>
#include <unistd.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libuutil.h>
#include "rcapd.h"
#include "rcapd_conf.h"
#include "rcapd_stat.h"
#include "utils.h"
/*
* Read configuration and set the fields of an rcfg_t correspondingly.
* Verify that the statistics file is writable, with the optional
* verify_stat_file_creation() callback.
*/
int
rcfg_read(rcfg_t *_rcfg, int(*verify_stat_file_creation)(void))
{
scf_simple_handle_t *simple_h;
uint64_t count_val;
int ret = E_ERROR;
rcfg_init(_rcfg);
if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG))
== NULL) {
warn(gettext("SMF initialization problem: %s\n"),
scf_strerror(scf_error()));
goto err;
}
if (scf_read_count_property(simple_h, PRESSURE, &count_val)
== SCF_FAILED) {
warn(gettext("Configuration property '%s' "
"not found. \n"), PRESSURE);
goto err;
} else {
if (count_val > 100)
_rcfg->rcfg_memory_cap_enforcement_pressure = 100;
else
_rcfg->rcfg_memory_cap_enforcement_pressure
= count_val;
debug("cap max pressure: %d%%\n",
_rcfg->rcfg_memory_cap_enforcement_pressure);
}
if (scf_read_count_property(simple_h, RECONFIG_INT, &count_val)
== SCF_FAILED) {
warn(gettext("Configuration property '%s' "
"not found. \n"), RECONFIG_INT);
goto err;
} else {
_rcfg->rcfg_reconfiguration_interval = count_val;
debug("reconfiguration interval: %d seconds\n",
_rcfg->rcfg_reconfiguration_interval);
}
if (scf_read_count_property(simple_h, REPORT_INT, &count_val)
== SCF_FAILED) {
warn(gettext("Configuration property '%s' "
"not found. \n"), REPORT_INT);
goto err;
} else {
_rcfg->rcfg_report_interval = count_val;
debug("report interval: %d seconds\n",
_rcfg->rcfg_report_interval);
}
if (scf_read_count_property(simple_h, RSS_SAMPLE_INT, &count_val)
== SCF_FAILED) {
warn(gettext("Configuration property '%s' "
"not found. \n"), RSS_SAMPLE_INT);
goto err;
} else {
_rcfg->rcfg_rss_sample_interval = count_val;
debug("RSS sample interval: %d seconds\n",
_rcfg->rcfg_rss_sample_interval);
}
if (scf_read_count_property(simple_h, WALK_INT, &count_val)
== SCF_FAILED) {
warn(gettext("Configuration property '%s' "
"not found. \n"), WALK_INT);
goto err;
} else {
_rcfg->rcfg_proc_walk_interval = count_val;
debug("proc_walk interval: %d seconds\n",
_rcfg->rcfg_proc_walk_interval);
}
if (_rcfg->rcfg_mode_name == NULL) {
/*
* Set project mode, by default.
*/
_rcfg->rcfg_mode = rctype_project;
_rcfg->rcfg_mode_name = "project";
debug("mode: %s\n", _rcfg->rcfg_mode_name);
}
if (verify_stat_file_creation != 0 && verify_stat_file_creation()
!= 0) {
warn(gettext("cannot create statistics file, " "%s"),
_rcfg->rcfg_stat_file);
goto err;
}
debug("done parsing\n");
ret = E_SUCCESS;
goto out;
err:
if (scf_error() != SCF_ERROR_NONE) {
warn(gettext("Unexpected libscf error: %s. \n"),
scf_strerror(scf_error()));
}
out:
scf_simple_handle_destroy(simple_h);
return (ret);
}
void
rcfg_init(rcfg_t *rcfg)
{
bzero(rcfg, sizeof (*rcfg));
(void) strcpy(rcfg->rcfg_stat_file, STAT_FILE_DEFAULT);
}
/*
* Modify configuration in repository given the rcfg_t structure.
*/
int
modify_config(rcfg_t *conf)
{
scf_simple_handle_t *simple_h;
scf_transaction_t *tx = NULL;
int rval, ret = E_ERROR;
if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG))
== NULL) {
warn(gettext("SMF initialization problem: %s\n"),
scf_strerror(scf_error()));
goto out;
}
if ((tx = scf_transaction_setup(simple_h)) == NULL) {
warn(gettext("SMF initialization problem: %s\n"),
scf_strerror(scf_error()));
goto out;
}
do {
if (scf_set_count_property(tx, PRESSURE,
conf->rcfg_memory_cap_enforcement_pressure, 0)
!= SCF_SUCCESS) {
warn(gettext("Couldn't set '%s' property. \n"),
PRESSURE);
goto out;
}
if (scf_set_count_property(tx, RECONFIG_INT,
conf->rcfg_reconfiguration_interval, 0) != SCF_SUCCESS) {
warn(gettext("Couldn't set '%s' property. \n"),
RECONFIG_INT);
goto out;
}
if (scf_set_count_property(tx, RSS_SAMPLE_INT,
conf->rcfg_rss_sample_interval, 0) != SCF_SUCCESS) {
warn(gettext("Couldn't set '%s' property. \n"),
RSS_SAMPLE_INT);
goto out;
}
if (scf_set_count_property(tx, REPORT_INT,
conf->rcfg_report_interval, 0) != SCF_SUCCESS) {
warn(gettext("Couldn't set '%s' property. \n"),
REPORT_INT);
goto out;
}
if (scf_set_count_property(tx, WALK_INT,
conf->rcfg_proc_walk_interval, 0) != SCF_SUCCESS) {
warn(gettext("Couldn't set '%s' property. \n"),
WALK_INT);
goto out;
}
if ((rval = scf_transaction_commit(tx)) == -1)
goto out;
if (rval == 0) {
if (scf_transaction_restart(simple_h, tx)
!= SCF_SUCCESS) {
warn(gettext("SMF initialization problem: "
"%s\n"), scf_strerror(scf_error()));
goto out;
}
}
} while (rval == 0);
ret = E_SUCCESS;
out:
if (tx != NULL) {
scf_transaction_destroy_children(tx);
scf_transaction_destroy(tx);
}
scf_simple_handle_destroy(simple_h);
return (ret);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _RCAPD_CONF_H
#define _RCAPD_CONF_H
#include <sys/param.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CFG_TEMPLATE_SUFFIX ".XXXXXX" /* suffix of mkstemp() arg */
/*
* Operating modes
*/
typedef enum {
rctype_project = 1 /* projects are the only collection type */
} rctype_t;
/*
* Configuration
*/
typedef struct {
char rcfg_filename[MAXPATHLEN]; /* cfg filename */
int rcfg_fd; /* reserved cfg fd */
time_t rcfg_last_modification; /* last mod time */
rctype_t rcfg_mode; /* mode (collection type) */
char *rcfg_mode_name; /* mode name ("project" only) */
uint32_t rcfg_proc_walk_interval; /* /proc readdir() */
/* interval (s), */
/* undocumented */
uint32_t rcfg_reconfiguration_interval; /* lnode or project */
/* cap reconfig. */
/* interval (s) */
uint32_t rcfg_report_interval; /* report interval (s) */
int rcfg_memory_cap_enforcement_pressure; /* pressure */
/* above which memory caps are enforced */
char rcfg_stat_file[MAXPATHLEN]; /* statistics file */
uint32_t rcfg_rss_sample_interval; /* RSS sampling interval */
} rcfg_t;
typedef enum {
RCT_MODE_VAR = 257,
RCT_PROC_WALK_INTERVAL_VAR,
RCT_RECONFIGURATION_INTERVAL_VAR,
RCT_REPORT_INTERVAL_VAR,
RCT_RSS_SAMPLE_INTERVAL_VAR,
RCT_MEMORY_CAP_ENFORCEMENT_PRESSURE_VAR,
RCT_STAT_FILE_VAR,
RCT_EQUALS,
RCT_NUMBER,
RCT_PROJECT,
RCT_LNODE,
RCT_ON,
RCT_OFF,
RCT_FILENAME,
RCT_STATE,
RCT_INVALID
} rctoken_t;
extern int rcfg_read(rcfg_t *, int(*)(void));
extern void rcfg_init(rcfg_t *);
extern int modify_config(rcfg_t *);
#ifdef __cplusplus
}
#endif
#endif /* _RCAPD_CONF_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include "rcapd_stat.h"
/*
* Return the pid of the writer of the statistics file.
*/
pid_t
stat_get_rcapd_pid(char *file)
{
int fd;
rcapd_stat_hdr_t hdr;
char procfile[20];
struct stat st;
if ((fd = open(file, O_RDONLY)) < 0)
return (-1);
if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) {
(void) close(fd);
return (-1);
}
(void) close(fd);
(void) snprintf(procfile, 20, "/proc/%d/psinfo", (int)hdr.rs_pid);
if (stat(procfile, &st) == 0)
return (hdr.rs_pid);
else
return (-1);
}
/*
* 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.
*/
#ifndef _RCAPD_STAT_H
#define _RCAPD_STAT_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* Default path to statistics file
*/
#define STAT_FILE_DIR "/var/run/daemon"
#define STAT_FILE_DEFAULT STAT_FILE_DIR "/rcap.stat"
/*
* Statistics file header.
*/
#define RC_MODE_LEN 16
typedef struct rcapd_stat_hdr {
/*
* sizeof pid_t can vary, so we use a fixed 64-bit quantity.
*/
uint64_t rs_pid; /* pid of producer */
hrtime_t rs_time; /* time recorded */
/*
* Physical memory pressure statistics, in percent.
*/
uint32_t rs_pressure_cur; /* current memory pressure */
uint32_t rs_pressure_cap; /* minimum cap enforcement p. */
uint64_t rs_pressure_sample; /* count of pr. samplings */
char rs_mode[RC_MODE_LEN]; /* mode ("project" only) */
} rcapd_stat_hdr_t;
extern pid_t stat_get_rcapd_pid(char *);
#ifdef __cplusplus
}
#endif
#endif /* _RCAPD_STAT_H */
/*
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/param.h>
#include <libintl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include <errno.h>
#include "utils.h"
static char ERRNO_FMT[] = ": %s";
static char *pname = NULL;
static rcm_level_t message_priority = RCM_WARN;
static rcm_dst_t message_dst = RCD_STD;
static void dmesg(int level, char *msg);
/*PRINTFLIKE2*/
void
dprintfe(int level, char *format, ...)
{
va_list alist;
va_start(alist, format);
vdprintfe(level, format, alist);
va_end(alist);
}
/*PRINTFLIKE2*/
void
vdprintfe(int level, const char *format, va_list alist)
{
char buf[LINELEN];
char *c;
int err = errno;
*buf = 0;
if ((strlen(buf) + 1) < LINELEN)
(void) vsnprintf(buf + strlen(buf), LINELEN - 1 - strlen(buf),
format, alist);
if ((c = strchr(buf, '\n')) == NULL) {
if ((strlen(buf) + 1) < LINELEN)
(void) snprintf(buf + strlen(buf), LINELEN - 1 -
strlen(buf), gettext(ERRNO_FMT), strerror(err));
} else
*c = 0;
dmesg(level, buf);
}
#ifdef DEBUG_MSG
/*PRINTFLIKE1*/
void
debug(char *format, ...)
{
va_list alist;
if (get_message_priority() < RCM_DEBUG)
return;
va_start(alist, format);
vdprintfe(RCM_DEBUG, format, alist);
va_end(alist);
}
/*PRINTFLIKE1*/
void
debug_high(char *format, ...)
{
va_list alist;
if (get_message_priority() < RCM_DEBUG_HIGH)
return;
va_start(alist, format);
vdprintfe(RCM_DEBUG_HIGH, format, alist);
va_end(alist);
}
#endif /* DEBUG_MSG */
/*PRINTFLIKE1*/
void
warn(const char *format, ...)
{
va_list alist;
if (get_message_priority() < RCM_WARN)
return;
va_start(alist, format);
vdprintfe(RCM_WARN, format, alist);
va_end(alist);
}
/*PRINTFLIKE1*/
void
die(char *format, ...)
{
va_list alist;
if (get_message_priority() < RCM_ERR)
return;
va_start(alist, format);
vdprintfe(RCM_ERR, format, alist);
va_end(alist);
exit(E_ERROR);
}
/*PRINTFLIKE1*/
void
info(char *format, ...)
{
va_list alist;
if (get_message_priority() < RCM_INFO)
return;
va_start(alist, format);
vdprintfe(RCM_INFO, format, alist);
va_end(alist);
}
char *
setpname(char *arg0)
{
char *p = strrchr(arg0, '/');
if (p == NULL)
p = arg0;
else
p++;
pname = p;
return (pname);
}
/*
* Output a message to the controlling tty or log, depending on which is
* configured. The message should contain no newlines.
*/
static void
dmesg(int level, char *msg)
{
if (message_priority >= level) {
FILE *fp;
int syslog_severity = -1;
switch (message_dst) {
case RCD_STD:
fp = level >= RCM_DEBUG ? stderr : stdout;
if (pname != NULL) {
(void) fputs(pname, fp);
(void) fputs(": ", fp);
}
(void) fputs(msg, fp);
(void) fputc('\n', fp);
(void) fflush(fp);
break;
case RCD_SYSLOG:
switch (level) {
case RCM_ERR:
syslog_severity = LOG_ERR;
break;
case RCM_WARN:
syslog_severity = LOG_WARNING;
break;
case RCM_INFO:
syslog_severity = LOG_INFO;
break;
case RCM_DEBUG:
syslog_severity = LOG_DEBUG;
break;
}
if (syslog_severity >= 0)
(void) syslog(syslog_severity, "%s", msg);
break;
}
}
}
rcm_level_t
get_message_priority(void)
{
return (message_priority);
}
rcm_level_t
set_message_priority(rcm_level_t new_priority)
{
rcm_level_t old_priority = message_priority;
message_priority = new_priority;
return (old_priority);
}
rcm_dst_t
set_message_destination(rcm_dst_t new_dst)
{
rcm_dst_t old_dst = message_dst;
if ((message_dst = new_dst) == RCD_SYSLOG)
openlog(pname, LOG_ODELAY | LOG_PID, LOG_DAEMON);
return (old_dst);
}
void
hrt2ts(hrtime_t hrt, timestruc_t *tsp)
{
tsp->tv_sec = hrt / NANOSEC;
tsp->tv_nsec = hrt % NANOSEC;
}
int
xatoi(char *p)
{
int i;
char *q;
errno = 0;
i = (int)strtol(p, &q, 10);
if (errno != 0 || q == p || i < 0 || *q != '\0') {
warn(gettext("illegal argument -- %s\n"), p);
return (-1);
} else {
return (i);
}
}
/*
* get_running_zones() calls zone_list(2) to find out how many zones are
* running. It then calls zone_list(2) again to fetch the list of running
* zones (stored in *zents).
*/
int
get_running_zones(uint_t *nzents, zone_entry_t **zents)
{
zoneid_t *zids;
uint_t nzents_saved;
int i;
zone_entry_t *zentp;
zone_state_t zstate;
*zents = NULL;
if (zone_list(NULL, nzents) != 0) {
warn(gettext("could not get zoneid list\n"));
return (E_ERROR);
}
again:
if (*nzents == 0)
return (E_SUCCESS);
if ((zids = (zoneid_t *)calloc(*nzents, sizeof (zoneid_t))) == NULL) {
warn(gettext("out of memory: zones will not be capped\n"));
return (E_ERROR);
}
nzents_saved = *nzents;
if (zone_list(zids, nzents) != 0) {
warn(gettext("could not get zone list\n"));
free(zids);
return (E_ERROR);
}
if (*nzents != nzents_saved) {
/* list changed, try again */
free(zids);
goto again;
}
*zents = calloc(*nzents, sizeof (zone_entry_t));
if (*zents == NULL) {
warn(gettext("out of memory: zones will not be capped\n"));
free(zids);
return (E_ERROR);
}
zentp = *zents;
for (i = 0; i < *nzents; i++) {
char name[ZONENAME_MAX];
if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
warn(gettext("could not get name for "
"zoneid %d\n"), zids[i]);
continue;
}
(void) strlcpy(zentp->zname, name, sizeof (zentp->zname));
zentp->zid = zids[i];
if (zone_get_state(name, &zstate) != Z_OK ||
zstate != ZONE_STATE_RUNNING)
continue;
zentp++;
}
*nzents = zentp - *zents;
free(zids);
return (E_SUCCESS);
}
/*
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#ifndef _UTILS_H
#define _UTILS_H
#include <assert.h>
#include <libintl.h>
#include <stdarg.h>
#include <time.h>
#include <libzonecfg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define E_SUCCESS 0 /* Exit status for success */
#define E_ERROR 1 /* Exit status for error */
#define E_USAGE 2 /* Exit status for usage error */
/*
* Message filter levels by priority
*/
typedef enum rcm_level {
RCM_NONE = 0, /* No messages */
RCM_ERR, /* Errors only */
RCM_WARN, /* Warnings */
RCM_INFO, /* Information */
RCM_DEBUG, /* Everything */
RCM_DEBUG_HIGH /* Fire hose */
} rcm_level_t;
/*
* Message destinations
*/
typedef enum rcm_dst {
RCD_STD = 1, /* Standard output/error, depending */
/* on level */
RCD_SYSLOG /* syslog() daemon facility */
} rcm_dst_t;
typedef struct zone_entry {
zoneid_t zid;
char zname[ZONENAME_MAX];
} zone_entry_t;
#define LINELEN 256 /* max. message length */
#ifdef DEBUG
#undef ASSERT
#define ASSERT(x) (assert(x))
#else /* !DEBUG */
#undef ASSERT
#define ASSERT(x) ((void)0)
#endif /* DEBUG */
#ifdef DEBUG_MSG
extern void debug(char *, ...);
extern void debug_high(char *, ...);
#else /* !DEBUG_MSG */
/*LINTED: static unused*/
static void debug(char *format, ...) /*ARGSUSED*/ {}
/*LINTED: static unused*/
static void debug_high(char *format, ...) /*ARGSUSED*/ {}
#endif /* DEBUG_MSG */
extern void die(char *, ...);
extern void info(char *, ...);
extern rcm_level_t get_message_priority(void);
extern rcm_level_t set_message_priority(rcm_level_t);
extern rcm_dst_t set_message_destination(rcm_dst_t);
extern char *setpname(char *);
extern void warn(const char *, ...);
extern int valid_abspath(char *);
extern void vdprintfe(int, const char *, va_list);
extern void dprintfe(int, char *, ...);
extern void hrt2ts(hrtime_t, timestruc_t *);
extern int xatoi(char *);
extern int get_running_zones(uint_t *, zone_entry_t **);
#ifdef __cplusplus
}
#endif
#endif /* _UTILS_H */
|