2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
5 #include <sys/syscalls.h>
6 #include <newos/errors.h>
12 typedef struct thread_time
{
13 struct thread_time
*next
;
15 struct thread_info info
;
17 bigtime_t last_user_time
;
18 bigtime_t last_kernel_time
;
20 bigtime_t delta_user_time
;
21 bigtime_t delta_kernel_time
;
26 static thread_time
*times
= NULL
;
28 static thread_time
*find_in_list(thread_id id
)
32 for(tt
= times
; tt
; tt
= tt
->next
) {
39 static void insert_in_list(thread_time
*tt
)
45 static void prune_untouched(void)
47 thread_time
*tt
, *last
;
58 last
->next
= tt
->next
;
72 static void sort_times(void)
74 thread_time
*curr
, *next
, *last
;
86 if((curr
->delta_user_time
+ curr
->delta_kernel_time
) > (next
->delta_user_time
+ next
->delta_kernel_time
)) {
94 curr
->next
= next
->next
;
104 static int gather_info(void)
107 struct thread_info ti
;
110 uint32 cookie
, cookie2
;
112 // walk through each thread in the system
115 err
= _kern_proc_get_next_proc_info(&cookie
, &pi
);
121 err
= _kern_thread_get_next_thread_info(&cookie2
, pi
.pid
, &ti
);
125 tt
= find_in_list(ti
.id
);
128 tt
= malloc(sizeof(thread_time
));
130 return ERR_NO_MEMORY
;
132 memset(&tt
->info
, 0, sizeof(struct thread_info
));
137 // save the last user and kernel time
138 tt
->last_user_time
= tt
->info
.user_time
;
139 tt
->last_kernel_time
= tt
->info
.kernel_time
;
142 memcpy(&tt
->info
, &ti
, sizeof(ti
));
144 // calculate the delta time
145 tt
->delta_user_time
= tt
->info
.user_time
- tt
->last_user_time
;
146 tt
->delta_kernel_time
= tt
->info
.kernel_time
- tt
->last_kernel_time
;
152 // prune any untouched entries
161 static int display_info(void)
164 bigtime_t total_user
= 0;
165 bigtime_t total_kernel
= 0;
167 // clear and home the screen
168 printf("%c[2J%c[H", 0x1b, 0x1b);
170 // print the thread dump
171 printf(" tid pid pri usertime kerneltime name\n");
172 for(tt
= times
; tt
; tt
= tt
->next
) {
173 printf("%6d%6d%6d%12Ld%12Ld%32s\n",
174 tt
->info
.id
, tt
->info
.owner_proc_id
, tt
->info
.priority
, tt
->delta_user_time
, tt
->delta_kernel_time
, tt
->info
.name
);
175 total_user
+= tt
->delta_user_time
;
176 total_kernel
+= tt
->delta_kernel_time
;
178 printf("%18s%12Ld%12Ld\n", "total:", total_user
, total_kernel
);
183 int main(int argc
, char **argv
)
188 // gather data about each of the threads in the system
193 err
= display_info();