4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2012 Joyent, Inc. All rights reserved.
38 list_alloc(list_t
*list
, int size
)
42 list
->l_ptrs
= Zalloc(sizeof (void *) * (size
+ 1));
48 list_free(list_t
*list
)
50 if (list
&& list
->l_ptrs
) {
60 get_cpu_from_psinfo(void *lwp
)
63 FRC2PCT((((lwp_info_t
*)lwp
)->li_info
.pr_lwp
.pr_pctcpu
)*1000));
67 get_cpu_from_usage(void *lwp
)
69 lwp_info_t
*p
= (lwp_info_t
*)lwp
;
74 return ((ulong_t
)cpu
);
80 return ((ulong_t
)TIME2SEC(((lwp_info_t
*)lwp
)->li_info
.pr_lwp
.pr_time
));
86 return ((ulong_t
)((lwp_info_t
*)lwp
)->li_info
.pr_size
);
92 return ((ulong_t
)((lwp_info_t
*)lwp
)->li_info
.pr_rssize
);
98 return ((ulong_t
)((lwp_info_t
*)lwp
)->li_info
.pr_lwp
.pr_pri
);
104 return (((id_info_t
*)id
)->id_key
);
108 list_setkeyfunc(char *arg
, optdesc_t
*opt
, list_t
*list
, int type
)
113 list
->l_sortorder
= opt
->o_sortorder
;
115 if (arg
== NULL
) { /* special case for id_infos */
116 list
->l_func
= get_idkey
;
119 if (strcmp("cpu", arg
) == 0) {
120 if (opt
->o_outpmode
& OPT_MSACCT
)
121 list
->l_func
= get_cpu_from_usage
;
123 list
->l_func
= get_cpu_from_psinfo
;
126 if (strcmp("time", arg
) == 0) {
127 list
->l_func
= get_time
;
130 if (strcmp("size", arg
) == 0) {
131 list
->l_func
= get_size
;
134 if (strcmp("rss", arg
) == 0) {
135 list
->l_func
= get_rssize
;
138 if (strcmp("pri", arg
) == 0) {
139 list
->l_func
= get_pri
;
142 Die(gettext("invalid sort key -- %s\n"), arg
);
146 list_getkeyval(list_t
*list
, void *ptr
)
148 return (list
->l_func(ptr
));
152 compare_keys(list_t
*list
, ulong_t key1
, ulong_t key2
)
157 return (1 * list
->l_sortorder
);
159 return (-1 * list
->l_sortorder
);
163 list_insert(list_t
*list
, void *ptr
)
168 for (i
= 0; i
< list
->l_used
; i
++) { /* insert in the middle */
169 k1
= list_getkeyval(list
, ptr
);
170 k2
= list_getkeyval(list
, list
->l_ptrs
[i
]);
171 if (compare_keys(list
, k1
, k2
) >= 0) {
172 for (j
= list
->l_used
- 1; j
>= i
; j
--)
173 list
->l_ptrs
[j
+1] = list
->l_ptrs
[j
];
174 list
->l_ptrs
[i
] = ptr
;
175 if (list
->l_used
< list
->l_size
)
180 if (i
+ 1 <= list
->l_size
) { /* insert at the tail */
181 list
->l_ptrs
[list
->l_used
] = ptr
;
188 list_preinsert(list_t
*list
, void *ptr
)
192 if (list
->l_used
< list
->l_size
) { /* just add */
193 list_insert(list
, ptr
);
196 k1
= list_getkeyval(list
, list
->l_ptrs
[list
->l_used
- 1]);
197 k2
= list_getkeyval(list
, ptr
);
198 if (compare_keys(list
, k1
, k2
) >= 0) /* skip insertion */
200 k1
= list_getkeyval(list
, list
->l_ptrs
[0]);
201 if (compare_keys(list
, k2
, k1
) >= 0) { /* add at the head */
202 list_insert(list
, ptr
);
205 list_insert(list
, ptr
);
209 list_sort(list_t
*list
)
212 if (list
->l_size
== 0)
215 (void) memset(list
->l_ptrs
, 0, sizeof (void *) * list
->l_size
);
217 if (list
->l_type
== LT_LWPS
) {
218 lwp_info_t
*lwp
= list
->l_head
;
221 list_preinsert(list
, (void *)lwp
);
225 id_info_t
*id
= list
->l_head
;
228 list_preinsert(list
, (void *)id
);